summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/texinfo/util/prepinfo.awk
blob: 273c97f8460697f811eb197cf15c11d438fa3c09 (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#! /usr/local/bin/gawk -f

# prepinfo.awk --- fix node lines and menus
#
# Copyright, 1998, Arnold Robbins, arnold@gnu.org
#
# PREPINFO is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# PREPINFO is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA

BEGIN	\
{
	# manifest constants
	TRUE = 1
	FALSE = 0

	# Levels at which different nodes can be
	Level["@top"] =	0
	Level["@appendix"] = 1
	Level["@chapter"] = 1
	Level["@majorheading"] = 1
	Level["@unnumbered"] = 1
	Level["@appendixsec"] = 2
	Level["@heading"] = 2
	Level["@section"] = 2
	Level["@unnumberedsec"] = 2
	Level["@unnumberedsubsec"] = 3
	Level["@appendixsubsec"] = 3
	Level["@subheading"] = 3
	Level["@subsection"] = 3
	Level["@appendixsubsubsec"] = 4
	Level["@subsubheading"] = 4
	Level["@subsubsection"] = 4
	Level["@unnumberedsubsubsec"] = 4

	# Length of menus
	Menumargin = 78

	# Length of menu item
	Min_menitem_length = 29

	# insure that we were called correctly
	if (ARGC != 2) {
		printf("usage: %s texinfo-file\n", ARGV[0]) > "/dev/stderr"
		exit 1
	}

	# Arrange for two passes over input file
	Pass = 1
	ARGV[2] = "Pass=2"
	ARGV[3] = ARGV[1]
	ARGC = 4
	Lastlevel = -1

	# Initialize stacks
	Up[-1] = "(dir)"
	Prev[0] = "(dir)"

	if (Debug == "args") {
		for (i = 0; i < ARGC; i++)
			printf("ARGV[%d] = %s\n", i, ARGV[i]) > "/dev/stderr"
	}
}

$1 == "@node"	\
{
	Name = getnodename($0)
	Nodeseen = TRUE

	if ((l = length(Name)) > Maxlen)
		Maxlen = l

	if (Debug == "nodenames")
		printf("Name = %s\n", Name) > "/dev/stderr"

	if (Pass == 1)
		next
}

Pass == 1 && /^@c(omment)?[ \t]+fakenode/ \
{
	if (Debug == "fakenodes")
		printf("fakenode at %d\n", FNR) > "/dev/stderr"
	Fakenode = TRUE
	next
}

Pass == 1 && ($1 in Level)	\
{
	# skip fake nodes --- titles without associated @node lines
	if (Fakenode) {
		if (Debug == "fakenodes")
			printf("%s at %d is a fakenode\n", $1, FNR) > "/dev/stderr"
		Fakenode = FALSE
		next
	}

	if (Debug == "titles")
		printf("Processing %s: Name = %s\n", $1, Name) > "/dev/stderr"

	# save type
	type = $1

	if (! Nodeseen) {
		err_prefix()
		printf("%s line with no @node or fakenode line\n",
			type) > "/dev/stderr"
		Badheading[FNR] = 1
		# ??? used ???
		next
	} else
		Nodeseen = FALSE	# reset it

	# Squirrel away the info
	levelnum = Level[type]
	Node[Name ".level"] = levelnum
	Node[Name ".name"] = Name
	if (Debug == "titles") {
		printf("Node[%s\".level\"] = %s\n", Name, Node[Name ".level"]) > "/dev/stderr"
		printf("Node[%s\".name\"] = %s\n", Name, Node[Name ".name"]) > "/dev/stderr"
	}

	if (levelnum == Lastlevel) {	# e.g., two sections in a row
		Node[Name ".up"] = Up[levelnum - 1]
		if (levelnum in Prev) {
			Node[Prev[levelnum] ".next"] = Name
			Node[Name ".prev"] = Prev[levelnum]
		}
		Prev[levelnum] = Name
		Up[levelnum] = Name	# ???
	} else if (levelnum < Lastlevel) {	# section, now chapter
		Lastlevel = levelnum
		Node[Name ".up"] = Up[levelnum - 1]
		if (levelnum in Prev) {
			Node[Name ".prev"] = Prev[levelnum]
			Node[Prev[levelnum] ".next"] = Name
		}
		Prev[levelnum] = Name
		Up[levelnum] = Name
	} else {		# chapter, now section, levelnum > Lastlevel
		Node[Name ".up"] = Up[levelnum - 1]
		Node[Up[Lastlevel] ".child"] = Name
		Up[levelnum] = Name
		Prev[levelnum] = Name
		Lastlevel = levelnum
	}

	# For master menu
	if (Level[$1] >= 2)
		List[++Sequence] = Name

	if (Debug == "titles") {
		printf("Node[%s\".prev\"] = %s\n", Name, Node[Name ".prev"]) > "/dev/stderr"
		printf("Node[%s\".up\"] = %s\n", Name, Node[Name ".up"]) > "/dev/stderr"
		printf("Node[%s\".child\"] = %s\n", Name, Node[Name ".child"]) > "/dev/stderr"
	}
}

Pass == 2 && Debug == "dumptitles" && FNR <= 1	\
{
	for (i in Node)
		printf("Node[%s] = %s\n", i, Node[i]) | "sort 1>&2"
	close("sort 1>&2")
}

/^@menu/ && Pass == 1, /^@end[ \t]+menu/ && Pass == 1	\
{
	if (/^@menu/ || /^@end[ \t]+menu/)
		next

#	if (Debug == "menu")
#		printf("processing: %s\n", $0) > "/dev/stderr"

	if (/^\*/) {
		if (In_menitem) {	# file away info from previousline
			Node[node ".mendesc"] = desc
			Node[node ".longdesc"] = longdesc
			if (Debug == "mendesc") {
				printf("Node[%s.mendesc] = %s\n",
					node, Node[node ".mendesc"]) > "/dev/stderr"
				printf("Node[%s.longdesc] = %s\n",
					node, Node[node ".longdesc"]) > "/dev/stderr"
			}
		}
		In_menitem = TRUE

		# pull apart menu item
		$1 = ""		# nuke ``*''
		$0 = $0		# reparse line
		i1 = index($0, ":")
		if (i1 <= 0) {
			err_prefix()
			printf("badly formed menu item") > "/dev/stderr"
			next
		}
		if (substr($0, i1+1, 1) != ":") { # desc: node.  long desc
			i2 = index($0, ".")
			if (i2 <= 0) {
				err_prefix()
				printf("badly formed menu item") > "/dev/stderr"
				next
			}
			desc = substr($0, 1, i1 - 1)
			sub(/^[ \t]+/, "", node)
			sub(/[ \t]+$/, "", node)
			longdesc = substr($0, i2 + 1)
		} else {	# nodname:: long desc
			desc = ""
			node = substr($0, 1, i1 - 1)
			sub(/^[ \t]+/, "", node)
			sub(/[ \t]+$/, "", node)
			longdesc = substr($0, i1 + 2)
		}
	} else if (In_menitem) {	# continuation line
		longdesc = longdesc " " $0
	} else
		In_menitem = FALSE

	Node[node ".mendesc"] = desc
	Node[node ".longdesc"] = longdesc
	if (Debug == "mendesc") {
		printf("Node[%s.mendesc] = %s\n",
			node, Node[node ".mendesc"]) > "/dev/stderr"
		printf("Node[%s.longdesc] = %s\n",
			node, Node[node ".longdesc"]) > "/dev/stderr"
	}

	if (Debug == "menu")
		printf("Menu:: Name %s: desc %s: longdesc %s\n",
			node, desc, longdesc) > "/dev/stderr"
}

function err_prefix()
{
	printf("%s: %s: %d: ", ARGV[0], FILENAME, FNR) > "/dev/stderr"
}

function getnodename(str)
{
	sub(/@node[ \t]+/, "", str)
	sub(/,.*/, "", str)
	if (Debug == "nodenames")
		printf("getnodename: return %s\n", str) > "/dev/stderr"
	return str
}

Pass == 2 && /^@node/	\
{
	Name = getnodename($0)

	# Top node is special. It's next is the first child
	n = Node[Name ".next"]
	if (Node[Name ".level"] == 0 && n == "")
		n = Node[Name ".child"]

	printf("@node %s, %s, %s, %s\n", Name, n,
		Node[Name ".prev"] ? Node[Name ".prev"] : Node[Name ".up"],
		Node[Name ".up"])
	next
}

Pass == 2 && /^@menu/	\
{
	# First, nuke current contents of menu
	do {
		if ((getline) <= 0) {
			err_prefix()
			printf("unexpected EOF inside menu\n") > "/dev/stderr"
			exit 1
		}
	} while (! /^@end[ \t]+menu/)

	# next, compute maximum length of a node name
	max = 0
	for (n = Node[Name ".child"]; (n ".next") in Node; n = Node[n ".next"]) {
		if ((n ".desc") in Node)
			s = Node[n ".desc"] ": " n "."
		else
			s = n "::"
		l = length(s)
		if (l > max)
			max = l
	}
	if (max < Min_menitem_length)
		max = Min_menitem_length

	# now dump the menu
	print "@menu"

	for (n = Node[Name ".child"]; (n ".next") in Node; n = Node[n ".next"]) {
		print_menuitem(n, max)
	}
	print_menuitem(n, max)

	if (Name == "Top") {	# Master Menu
		if (Maxlen < Min_menitem_length)
			Maxlen = Min_menitem_length
		print ""
		for (i = 1; i <= Sequence; i++)
			print_menuitem(List[i], Maxlen)
		print ""
	}
	print "@end menu"
	next
}

Pass == 2	# print


function print_menuitem(n, max,		nodesc, i, dwords, count, p)
{
	nodesc = FALSE
	if (! ((n ".longdesc") in Node)) {
		err_prefix()
		printf("warning: %s: no long description\n", n) > "/dev/stderr"
		nodesc = TRUE
	} else {
		for (i in dwords)
			delete dwords[i]
		count = split(Node[n ".longdesc"], dwords, "[ \t\n]+")
	}
	if ((n ".desc") in Node)
		s = Node[n ".desc"] ": " n "."
	else
		s = n "::"
	printf("* %-*s", max, s)

	if (Debug == "mendescitem")
		printf("<* %-*s>\n", max, s) > "/dev/stderr"

	p = max + 2
	if (! nodesc) {
		for (i = 1; i <= count; i++) {
			l = length(dwords[i])
			if (l == 0)
				continue
			if (p + l + 1 > Menumargin) {
				printf("\n%*s", max + 2, " ")
				p = max + 2
			}
			printf(" %s", dwords[i])
			p += l + 1
		}
	}
	print ""
}