xref: /openbsd-src/usr.bin/mandoc/tree.c (revision e5157e49389faebcb42b7237d55fbf096d9c2523)
1 /*	$OpenBSD: tree.c,v 1.26 2014/10/20 01:43:06 schwarze Exp $ */
2 /*
3  * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4  * Copyright (c) 2013, 2014 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 AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 <sys/types.h>
19 
20 #include <assert.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25 
26 #include "mandoc.h"
27 #include "mdoc.h"
28 #include "man.h"
29 #include "main.h"
30 
31 static	void	print_box(const struct eqn_box *, int);
32 static	void	print_man(const struct man_node *, int);
33 static	void	print_mdoc(const struct mdoc_node *, int);
34 static	void	print_span(const struct tbl_span *, int);
35 
36 
37 void
38 tree_mdoc(void *arg, const struct mdoc *mdoc)
39 {
40 
41 	print_mdoc(mdoc_node(mdoc), 0);
42 }
43 
44 void
45 tree_man(void *arg, const struct man *man)
46 {
47 
48 	print_man(man_node(man), 0);
49 }
50 
51 static void
52 print_mdoc(const struct mdoc_node *n, int indent)
53 {
54 	const char	 *p, *t;
55 	int		  i, j;
56 	size_t		  argc;
57 	struct mdoc_argv *argv;
58 
59 	argv = NULL;
60 	argc = 0;
61 	t = p = NULL;
62 
63 	switch (n->type) {
64 	case MDOC_ROOT:
65 		t = "root";
66 		break;
67 	case MDOC_BLOCK:
68 		t = "block";
69 		break;
70 	case MDOC_HEAD:
71 		t = "block-head";
72 		break;
73 	case MDOC_BODY:
74 		if (n->end)
75 			t = "body-end";
76 		else
77 			t = "block-body";
78 		break;
79 	case MDOC_TAIL:
80 		t = "block-tail";
81 		break;
82 	case MDOC_ELEM:
83 		t = "elem";
84 		break;
85 	case MDOC_TEXT:
86 		t = "text";
87 		break;
88 	case MDOC_TBL:
89 		break;
90 	case MDOC_EQN:
91 		t = "eqn";
92 		break;
93 	default:
94 		abort();
95 		/* NOTREACHED */
96 	}
97 
98 	switch (n->type) {
99 	case MDOC_TEXT:
100 		p = n->string;
101 		break;
102 	case MDOC_BODY:
103 		p = mdoc_macronames[n->tok];
104 		break;
105 	case MDOC_HEAD:
106 		p = mdoc_macronames[n->tok];
107 		break;
108 	case MDOC_TAIL:
109 		p = mdoc_macronames[n->tok];
110 		break;
111 	case MDOC_ELEM:
112 		p = mdoc_macronames[n->tok];
113 		if (n->args) {
114 			argv = n->args->argv;
115 			argc = n->args->argc;
116 		}
117 		break;
118 	case MDOC_BLOCK:
119 		p = mdoc_macronames[n->tok];
120 		if (n->args) {
121 			argv = n->args->argv;
122 			argc = n->args->argc;
123 		}
124 		break;
125 	case MDOC_TBL:
126 		break;
127 	case MDOC_EQN:
128 		p = "EQ";
129 		break;
130 	case MDOC_ROOT:
131 		p = "root";
132 		break;
133 	default:
134 		abort();
135 		/* NOTREACHED */
136 	}
137 
138 	if (n->span) {
139 		assert(NULL == p && NULL == t);
140 		print_span(n->span, indent);
141 	} else {
142 		for (i = 0; i < indent; i++)
143 			putchar('\t');
144 
145 		printf("%s (%s)", p, t);
146 
147 		for (i = 0; i < (int)argc; i++) {
148 			printf(" -%s", mdoc_argnames[argv[i].arg]);
149 			if (argv[i].sz > 0)
150 				printf(" [");
151 			for (j = 0; j < (int)argv[i].sz; j++)
152 				printf(" [%s]", argv[i].value[j]);
153 			if (argv[i].sz > 0)
154 				printf(" ]");
155 		}
156 
157 		putchar(' ');
158 		if (MDOC_LINE & n->flags)
159 			putchar('*');
160 		printf("%d:%d", n->line, n->pos + 1);
161 		if (n->lastline != n->line)
162 			printf("-%d", n->lastline);
163 		putchar('\n');
164 	}
165 
166 	if (n->eqn)
167 		print_box(n->eqn->root->first, indent + 1);
168 	if (n->child)
169 		print_mdoc(n->child, indent + 1);
170 	if (n->next)
171 		print_mdoc(n->next, indent);
172 }
173 
174 static void
175 print_man(const struct man_node *n, int indent)
176 {
177 	const char	 *p, *t;
178 	int		  i;
179 
180 	t = p = NULL;
181 
182 	switch (n->type) {
183 	case MAN_ROOT:
184 		t = "root";
185 		break;
186 	case MAN_ELEM:
187 		t = "elem";
188 		break;
189 	case MAN_TEXT:
190 		t = "text";
191 		break;
192 	case MAN_BLOCK:
193 		t = "block";
194 		break;
195 	case MAN_HEAD:
196 		t = "block-head";
197 		break;
198 	case MAN_BODY:
199 		t = "block-body";
200 		break;
201 	case MAN_TAIL:
202 		t = "block-tail";
203 		break;
204 	case MAN_TBL:
205 		break;
206 	case MAN_EQN:
207 		t = "eqn";
208 		break;
209 	default:
210 		abort();
211 		/* NOTREACHED */
212 	}
213 
214 	switch (n->type) {
215 	case MAN_TEXT:
216 		p = n->string;
217 		break;
218 	case MAN_ELEM:
219 		/* FALLTHROUGH */
220 	case MAN_BLOCK:
221 		/* FALLTHROUGH */
222 	case MAN_HEAD:
223 		/* FALLTHROUGH */
224 	case MAN_TAIL:
225 		/* FALLTHROUGH */
226 	case MAN_BODY:
227 		p = man_macronames[n->tok];
228 		break;
229 	case MAN_ROOT:
230 		p = "root";
231 		break;
232 	case MAN_TBL:
233 		break;
234 	case MAN_EQN:
235 		p = "EQ";
236 		break;
237 	default:
238 		abort();
239 		/* NOTREACHED */
240 	}
241 
242 	if (n->span) {
243 		assert(NULL == p && NULL == t);
244 		print_span(n->span, indent);
245 	} else {
246 		for (i = 0; i < indent; i++)
247 			putchar('\t');
248 		printf("%s (%s) ", p, t);
249 		if (MAN_LINE & n->flags)
250 			putchar('*');
251 		printf("%d:%d\n", n->line, n->pos + 1);
252 	}
253 
254 	if (n->eqn)
255 		print_box(n->eqn->root->first, indent + 1);
256 	if (n->child)
257 		print_man(n->child, indent + 1);
258 	if (n->next)
259 		print_man(n->next, indent);
260 }
261 
262 static void
263 print_box(const struct eqn_box *ep, int indent)
264 {
265 	int		 i;
266 	const char	*t;
267 
268 	static const char *posnames[] = {
269 	    NULL, "sup", "subsup", "sub",
270 	    "to", "from", "fromto",
271 	    "over", "sqrt", NULL };
272 
273 	if (NULL == ep)
274 		return;
275 	for (i = 0; i < indent; i++)
276 		putchar('\t');
277 
278 	t = NULL;
279 	switch (ep->type) {
280 	case EQN_ROOT:
281 		t = "eqn-root";
282 		break;
283 	case EQN_LISTONE:
284 	case EQN_LIST:
285 		t = "eqn-list";
286 		break;
287 	case EQN_SUBEXPR:
288 		t = "eqn-expr";
289 		break;
290 	case EQN_TEXT:
291 		t = "eqn-text";
292 		break;
293 	case EQN_PILE:
294 		t = "eqn-pile";
295 		break;
296 	case EQN_MATRIX:
297 		t = "eqn-matrix";
298 		break;
299 	}
300 
301 	fputs(t, stdout);
302 	if (ep->pos)
303 		printf(" pos=%s", posnames[ep->pos]);
304 	if (ep->left)
305 		printf(" left=\"%s\"", ep->left);
306 	if (ep->right)
307 		printf(" right=\"%s\"", ep->right);
308 	if (ep->top)
309 		printf(" top=\"%s\"", ep->top);
310 	if (ep->bottom)
311 		printf(" bottom=\"%s\"", ep->bottom);
312 	if (ep->text)
313 		printf(" text=\"%s\"", ep->text);
314 	if (ep->font)
315 		printf(" font=%d", ep->font);
316 	if (ep->size != EQN_DEFSIZE)
317 		printf(" size=%d", ep->size);
318 	if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
319 		printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
320 	else if (ep->args)
321 		printf(" args=%zu", ep->args);
322 	putchar('\n');
323 
324 	print_box(ep->first, indent + 1);
325 	print_box(ep->next, indent);
326 }
327 
328 static void
329 print_span(const struct tbl_span *sp, int indent)
330 {
331 	const struct tbl_dat *dp;
332 	int		 i;
333 
334 	for (i = 0; i < indent; i++)
335 		putchar('\t');
336 
337 	switch (sp->pos) {
338 	case TBL_SPAN_HORIZ:
339 		putchar('-');
340 		return;
341 	case TBL_SPAN_DHORIZ:
342 		putchar('=');
343 		return;
344 	default:
345 		break;
346 	}
347 
348 	for (dp = sp->first; dp; dp = dp->next) {
349 		switch (dp->pos) {
350 		case TBL_DATA_HORIZ:
351 			/* FALLTHROUGH */
352 		case TBL_DATA_NHORIZ:
353 			putchar('-');
354 			continue;
355 		case TBL_DATA_DHORIZ:
356 			/* FALLTHROUGH */
357 		case TBL_DATA_NDHORIZ:
358 			putchar('=');
359 			continue;
360 		default:
361 			break;
362 		}
363 		printf("[\"%s\"", dp->string ? dp->string : "");
364 		if (dp->spans)
365 			printf("(%d)", dp->spans);
366 		if (NULL == dp->layout)
367 			putchar('*');
368 		putchar(']');
369 		putchar(' ');
370 	}
371 
372 	printf("(tbl) %d:1\n", sp->line);
373 }
374