summaryrefslogtreecommitdiff
path: root/mk/help/help.awk
blob: c319ea7599a5abcb6f97dda823d3bf3cce03eae3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# $NetBSD: help.awk,v 1.1 2006/10/23 14:40:15 rillig Exp $
#

BEGIN {
	no = 0; yes = 1;
	hline = "===============";
	hline = hline hline hline hline hline;
	found = no; var = no; comment = no; n = 0;
	rcsid = "";
	last_line_was_rcsid = no;
	last_line_was_empty = yes;
	topic = ENVIRON["TOPIC"];
	uctopic = toupper(topic);
}

/.*/ {
	if ($0 ~ /^#.*\$.*\$$/) {
		rcsid = $0;
		last_line_was_rcsid = yes;
	} else {
		if (last_line_was_rcsid && $0 == "#") {
			# Skip this line
		} else if ($0 == "") {
			# Skip completely empty lines, too.
		} else {
			lines[n++] = $0;
		}
		last_line_was_rcsid = no;
	}
}

/./ {
	# When looking for "configure", catch lines that contain
	# "configure" and "CONFIGURE", but not "Configure".
	w1 = ($1 == tolower($1)) ? toupper($1) : $1;
	w2 = ($2 == tolower($2)) ? toupper($2) : $2;

	if ((w1 == uctopic"?=") ||
	    (w1 == "#"uctopic"=") ||
	    (w1 == "#" && last_line_was_empty &&
	     (w2 == uctopic || w2 == uctopic":"))) {
		var = 1;
	}
}

/^#/ {
	comment = 1;
}

/^$/ {
	if (var && comment) {
		found = yes;
		print hline;
		if (rcsid != "") { print rcsid; print "#"; }
		for (i = 0; i < n; i++) { print lines[i]; }
	}
	var = no; comment = no; n = 0;
}

/./ {
	last_line_was_empty = no;
}
/^#$/ || /^$/ {
	last_line_was_empty = yes;
}

END {
	if (found) {
		print hline;
	} else {
		print "No help found for "topic".";
	}
}