]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
Delete five standards that are:
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.59 2014/10/20 01:43:48 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_TAIL:
204 t = "block-tail";
205 break;
206 case MAN_TBL:
207 break;
208 case MAN_EQN:
209 t = "eqn";
210 break;
211 default:
212 abort();
213 /* NOTREACHED */
214 }
215
216 switch (n->type) {
217 case MAN_TEXT:
218 p = n->string;
219 break;
220 case MAN_ELEM:
221 /* FALLTHROUGH */
222 case MAN_BLOCK:
223 /* FALLTHROUGH */
224 case MAN_HEAD:
225 /* FALLTHROUGH */
226 case MAN_TAIL:
227 /* FALLTHROUGH */
228 case MAN_BODY:
229 p = man_macronames[n->tok];
230 break;
231 case MAN_ROOT:
232 p = "root";
233 break;
234 case MAN_TBL:
235 break;
236 case MAN_EQN:
237 p = "EQ";
238 break;
239 default:
240 abort();
241 /* NOTREACHED */
242 }
243
244 if (n->span) {
245 assert(NULL == p && NULL == t);
246 print_span(n->span, indent);
247 } else {
248 for (i = 0; i < indent; i++)
249 putchar('\t');
250 printf("%s (%s) ", p, t);
251 if (MAN_LINE & n->flags)
252 putchar('*');
253 printf("%d:%d\n", n->line, n->pos + 1);
254 }
255
256 if (n->eqn)
257 print_box(n->eqn->root->first, indent + 1);
258 if (n->child)
259 print_man(n->child, indent + 1);
260 if (n->next)
261 print_man(n->next, indent);
262 }
263
264 static void
265 print_box(const struct eqn_box *ep, int indent)
266 {
267 int i;
268 const char *t;
269
270 static const char *posnames[] = {
271 NULL, "sup", "subsup", "sub",
272 "to", "from", "fromto",
273 "over", "sqrt", NULL };
274
275 if (NULL == ep)
276 return;
277 for (i = 0; i < indent; i++)
278 putchar('\t');
279
280 t = NULL;
281 switch (ep->type) {
282 case EQN_ROOT:
283 t = "eqn-root";
284 break;
285 case EQN_LISTONE:
286 case EQN_LIST:
287 t = "eqn-list";
288 break;
289 case EQN_SUBEXPR:
290 t = "eqn-expr";
291 break;
292 case EQN_TEXT:
293 t = "eqn-text";
294 break;
295 case EQN_PILE:
296 t = "eqn-pile";
297 break;
298 case EQN_MATRIX:
299 t = "eqn-matrix";
300 break;
301 }
302
303 fputs(t, stdout);
304 if (ep->pos)
305 printf(" pos=%s", posnames[ep->pos]);
306 if (ep->left)
307 printf(" left=\"%s\"", ep->left);
308 if (ep->right)
309 printf(" right=\"%s\"", ep->right);
310 if (ep->top)
311 printf(" top=\"%s\"", ep->top);
312 if (ep->bottom)
313 printf(" bottom=\"%s\"", ep->bottom);
314 if (ep->text)
315 printf(" text=\"%s\"", ep->text);
316 if (ep->font)
317 printf(" font=%d", ep->font);
318 if (ep->size != EQN_DEFSIZE)
319 printf(" size=%d", ep->size);
320 if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
321 printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
322 else if (ep->args)
323 printf(" args=%zu", ep->args);
324 putchar('\n');
325
326 print_box(ep->first, indent + 1);
327 print_box(ep->next, indent);
328 }
329
330 static void
331 print_span(const struct tbl_span *sp, int indent)
332 {
333 const struct tbl_dat *dp;
334 int i;
335
336 for (i = 0; i < indent; i++)
337 putchar('\t');
338
339 switch (sp->pos) {
340 case TBL_SPAN_HORIZ:
341 putchar('-');
342 return;
343 case TBL_SPAN_DHORIZ:
344 putchar('=');
345 return;
346 default:
347 break;
348 }
349
350 for (dp = sp->first; dp; dp = dp->next) {
351 switch (dp->pos) {
352 case TBL_DATA_HORIZ:
353 /* FALLTHROUGH */
354 case TBL_DATA_NHORIZ:
355 putchar('-');
356 continue;
357 case TBL_DATA_DHORIZ:
358 /* FALLTHROUGH */
359 case TBL_DATA_NDHORIZ:
360 putchar('=');
361 continue;
362 default:
363 break;
364 }
365 printf("[\"%s\"", dp->string ? dp->string : "");
366 if (dp->spans)
367 printf("(%d)", dp->spans);
368 if (NULL == dp->layout)
369 putchar('*');
370 putchar(']');
371 putchar(' ');
372 }
373
374 printf("(tbl) %d:1\n", sp->line);
375 }