xref: /illumos-gate/usr/src/cmd/mandoc/tree.c (revision f22cbd2db87ae3945ed6a9166f8b9d61b65c6ab9)
1 /*	$Id: tree.c,v 1.77 2017/07/08 14:51:05 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2013, 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 #include "config.h"
19 
20 #include <sys/types.h>
21 
22 #include <assert.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <time.h>
27 
28 #include "mandoc.h"
29 #include "roff.h"
30 #include "mdoc.h"
31 #include "man.h"
32 #include "main.h"
33 
34 static	void	print_box(const struct eqn_box *, int);
35 static	void	print_man(const struct roff_node *, int);
36 static	void	print_meta(const struct roff_meta *);
37 static	void	print_mdoc(const struct roff_node *, int);
38 static	void	print_span(const struct tbl_span *, int);
39 
40 
41 void
42 tree_mdoc(void *arg, const struct roff_man *mdoc)
43 {
44 	print_meta(&mdoc->meta);
45 	putchar('\n');
46 	print_mdoc(mdoc->first->child, 0);
47 }
48 
49 void
50 tree_man(void *arg, const struct roff_man *man)
51 {
52 	print_meta(&man->meta);
53 	if (man->meta.hasbody == 0)
54 		puts("body  = empty");
55 	putchar('\n');
56 	print_man(man->first->child, 0);
57 }
58 
59 static void
60 print_meta(const struct roff_meta *meta)
61 {
62 	if (meta->title != NULL)
63 		printf("title = \"%s\"\n", meta->title);
64 	if (meta->name != NULL)
65 		printf("name  = \"%s\"\n", meta->name);
66 	if (meta->msec != NULL)
67 		printf("sec   = \"%s\"\n", meta->msec);
68 	if (meta->vol != NULL)
69 		printf("vol   = \"%s\"\n", meta->vol);
70 	if (meta->arch != NULL)
71 		printf("arch  = \"%s\"\n", meta->arch);
72 	if (meta->os != NULL)
73 		printf("os    = \"%s\"\n", meta->os);
74 	if (meta->date != NULL)
75 		printf("date  = \"%s\"\n", meta->date);
76 }
77 
78 static void
79 print_mdoc(const struct roff_node *n, int indent)
80 {
81 	const char	 *p, *t;
82 	int		  i, j;
83 	size_t		  argc;
84 	struct mdoc_argv *argv;
85 
86 	if (n == NULL)
87 		return;
88 
89 	argv = NULL;
90 	argc = 0;
91 	t = p = NULL;
92 
93 	switch (n->type) {
94 	case ROFFT_ROOT:
95 		t = "root";
96 		break;
97 	case ROFFT_BLOCK:
98 		t = "block";
99 		break;
100 	case ROFFT_HEAD:
101 		t = "head";
102 		break;
103 	case ROFFT_BODY:
104 		if (n->end)
105 			t = "body-end";
106 		else
107 			t = "body";
108 		break;
109 	case ROFFT_TAIL:
110 		t = "tail";
111 		break;
112 	case ROFFT_ELEM:
113 		t = "elem";
114 		break;
115 	case ROFFT_TEXT:
116 		t = "text";
117 		break;
118 	case ROFFT_TBL:
119 		break;
120 	case ROFFT_EQN:
121 		t = "eqn";
122 		break;
123 	default:
124 		abort();
125 	}
126 
127 	switch (n->type) {
128 	case ROFFT_TEXT:
129 		p = n->string;
130 		break;
131 	case ROFFT_BODY:
132 		p = roff_name[n->tok];
133 		break;
134 	case ROFFT_HEAD:
135 		p = roff_name[n->tok];
136 		break;
137 	case ROFFT_TAIL:
138 		p = roff_name[n->tok];
139 		break;
140 	case ROFFT_ELEM:
141 		p = roff_name[n->tok];
142 		if (n->args) {
143 			argv = n->args->argv;
144 			argc = n->args->argc;
145 		}
146 		break;
147 	case ROFFT_BLOCK:
148 		p = roff_name[n->tok];
149 		if (n->args) {
150 			argv = n->args->argv;
151 			argc = n->args->argc;
152 		}
153 		break;
154 	case ROFFT_TBL:
155 		break;
156 	case ROFFT_EQN:
157 		p = "EQ";
158 		break;
159 	case ROFFT_ROOT:
160 		p = "root";
161 		break;
162 	default:
163 		abort();
164 	}
165 
166 	if (n->span) {
167 		assert(NULL == p && NULL == t);
168 		print_span(n->span, indent);
169 	} else {
170 		for (i = 0; i < indent; i++)
171 			putchar(' ');
172 
173 		printf("%s (%s)", p, t);
174 
175 		for (i = 0; i < (int)argc; i++) {
176 			printf(" -%s", mdoc_argnames[argv[i].arg]);
177 			if (argv[i].sz > 0)
178 				printf(" [");
179 			for (j = 0; j < (int)argv[i].sz; j++)
180 				printf(" [%s]", argv[i].value[j]);
181 			if (argv[i].sz > 0)
182 				printf(" ]");
183 		}
184 
185 		putchar(' ');
186 		if (NODE_DELIMO & n->flags)
187 			putchar('(');
188 		if (NODE_LINE & n->flags)
189 			putchar('*');
190 		printf("%d:%d", n->line, n->pos + 1);
191 		if (NODE_DELIMC & n->flags)
192 			putchar(')');
193 		if (NODE_EOS & n->flags)
194 			putchar('.');
195 		if (NODE_BROKEN & n->flags)
196 			printf(" BROKEN");
197 		if (NODE_NOSRC & n->flags)
198 			printf(" NOSRC");
199 		if (NODE_NOPRT & n->flags)
200 			printf(" NOPRT");
201 		putchar('\n');
202 	}
203 
204 	if (n->eqn)
205 		print_box(n->eqn->first, indent + 4);
206 	if (n->child)
207 		print_mdoc(n->child, indent +
208 		    (n->type == ROFFT_BLOCK ? 2 : 4));
209 	if (n->next)
210 		print_mdoc(n->next, indent);
211 }
212 
213 static void
214 print_man(const struct roff_node *n, int indent)
215 {
216 	const char	 *p, *t;
217 	int		  i;
218 
219 	if (n == NULL)
220 		return;
221 
222 	t = p = NULL;
223 
224 	switch (n->type) {
225 	case ROFFT_ROOT:
226 		t = "root";
227 		break;
228 	case ROFFT_ELEM:
229 		t = "elem";
230 		break;
231 	case ROFFT_TEXT:
232 		t = "text";
233 		break;
234 	case ROFFT_BLOCK:
235 		t = "block";
236 		break;
237 	case ROFFT_HEAD:
238 		t = "head";
239 		break;
240 	case ROFFT_BODY:
241 		t = "body";
242 		break;
243 	case ROFFT_TBL:
244 		break;
245 	case ROFFT_EQN:
246 		t = "eqn";
247 		break;
248 	default:
249 		abort();
250 	}
251 
252 	switch (n->type) {
253 	case ROFFT_TEXT:
254 		p = n->string;
255 		break;
256 	case ROFFT_ELEM:
257 	case ROFFT_BLOCK:
258 	case ROFFT_HEAD:
259 	case ROFFT_BODY:
260 		p = roff_name[n->tok];
261 		break;
262 	case ROFFT_ROOT:
263 		p = "root";
264 		break;
265 	case ROFFT_TBL:
266 		break;
267 	case ROFFT_EQN:
268 		p = "EQ";
269 		break;
270 	default:
271 		abort();
272 	}
273 
274 	if (n->span) {
275 		assert(NULL == p && NULL == t);
276 		print_span(n->span, indent);
277 	} else {
278 		for (i = 0; i < indent; i++)
279 			putchar(' ');
280 		printf("%s (%s) ", p, t);
281 		if (NODE_LINE & n->flags)
282 			putchar('*');
283 		printf("%d:%d", n->line, n->pos + 1);
284 		if (NODE_EOS & n->flags)
285 			putchar('.');
286 		putchar('\n');
287 	}
288 
289 	if (n->eqn)
290 		print_box(n->eqn->first, indent + 4);
291 	if (n->child)
292 		print_man(n->child, indent +
293 		    (n->type == ROFFT_BLOCK ? 2 : 4));
294 	if (n->next)
295 		print_man(n->next, indent);
296 }
297 
298 static void
299 print_box(const struct eqn_box *ep, int indent)
300 {
301 	int		 i;
302 	const char	*t;
303 
304 	static const char *posnames[] = {
305 	    NULL, "sup", "subsup", "sub",
306 	    "to", "from", "fromto",
307 	    "over", "sqrt", NULL };
308 
309 	if (NULL == ep)
310 		return;
311 	for (i = 0; i < indent; i++)
312 		putchar(' ');
313 
314 	t = NULL;
315 	switch (ep->type) {
316 	case EQN_LIST:
317 		t = "eqn-list";
318 		break;
319 	case EQN_SUBEXPR:
320 		t = "eqn-expr";
321 		break;
322 	case EQN_TEXT:
323 		t = "eqn-text";
324 		break;
325 	case EQN_PILE:
326 		t = "eqn-pile";
327 		break;
328 	case EQN_MATRIX:
329 		t = "eqn-matrix";
330 		break;
331 	}
332 
333 	fputs(t, stdout);
334 	if (ep->pos)
335 		printf(" pos=%s", posnames[ep->pos]);
336 	if (ep->left)
337 		printf(" left=\"%s\"", ep->left);
338 	if (ep->right)
339 		printf(" right=\"%s\"", ep->right);
340 	if (ep->top)
341 		printf(" top=\"%s\"", ep->top);
342 	if (ep->bottom)
343 		printf(" bottom=\"%s\"", ep->bottom);
344 	if (ep->text)
345 		printf(" text=\"%s\"", ep->text);
346 	if (ep->font)
347 		printf(" font=%d", ep->font);
348 	if (ep->size != EQN_DEFSIZE)
349 		printf(" size=%d", ep->size);
350 	if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
351 		printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
352 	else if (ep->args)
353 		printf(" args=%zu", ep->args);
354 	putchar('\n');
355 
356 	print_box(ep->first, indent + 4);
357 	print_box(ep->next, indent);
358 }
359 
360 static void
361 print_span(const struct tbl_span *sp, int indent)
362 {
363 	const struct tbl_dat *dp;
364 	int		 i;
365 
366 	for (i = 0; i < indent; i++)
367 		putchar(' ');
368 
369 	switch (sp->pos) {
370 	case TBL_SPAN_HORIZ:
371 		putchar('-');
372 		return;
373 	case TBL_SPAN_DHORIZ:
374 		putchar('=');
375 		return;
376 	default:
377 		break;
378 	}
379 
380 	for (dp = sp->first; dp; dp = dp->next) {
381 		switch (dp->pos) {
382 		case TBL_DATA_HORIZ:
383 		case TBL_DATA_NHORIZ:
384 			putchar('-');
385 			continue;
386 		case TBL_DATA_DHORIZ:
387 		case TBL_DATA_NDHORIZ:
388 			putchar('=');
389 			continue;
390 		default:
391 			break;
392 		}
393 		printf("[\"%s\"", dp->string ? dp->string : "");
394 		if (dp->spans)
395 			printf("(%d)", dp->spans);
396 		if (NULL == dp->layout)
397 			putchar('*');
398 		putchar(']');
399 		putchar(' ');
400 	}
401 
402 	printf("(tbl) %d:1\n", sp->line);
403 }
404