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