]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
libmdoc.h no longer needs mdoc.h
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.82 2018/12/13 05:23:38 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013,2014,2015,2017,2018 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 "tbl.h"
33 #include "eqn.h"
34 #include "main.h"
35
36 static void print_box(const struct eqn_box *, int);
37 static void print_man(const struct roff_node *, int);
38 static void print_meta(const struct roff_meta *);
39 static void print_mdoc(const struct roff_node *, int);
40 static void print_span(const struct tbl_span *, int);
41
42
43 void
44 tree_mdoc(void *arg, const struct roff_man *mdoc)
45 {
46 print_meta(&mdoc->meta);
47 putchar('\n');
48 print_mdoc(mdoc->first->child, 0);
49 }
50
51 void
52 tree_man(void *arg, const struct roff_man *man)
53 {
54 print_meta(&man->meta);
55 if (man->meta.hasbody == 0)
56 puts("body = empty");
57 putchar('\n');
58 print_man(man->first->child, 0);
59 }
60
61 static void
62 print_meta(const struct roff_meta *meta)
63 {
64 if (meta->title != NULL)
65 printf("title = \"%s\"\n", meta->title);
66 if (meta->name != NULL)
67 printf("name = \"%s\"\n", meta->name);
68 if (meta->msec != NULL)
69 printf("sec = \"%s\"\n", meta->msec);
70 if (meta->vol != NULL)
71 printf("vol = \"%s\"\n", meta->vol);
72 if (meta->arch != NULL)
73 printf("arch = \"%s\"\n", meta->arch);
74 if (meta->os != NULL)
75 printf("os = \"%s\"\n", meta->os);
76 if (meta->date != NULL)
77 printf("date = \"%s\"\n", meta->date);
78 }
79
80 static void
81 print_mdoc(const struct roff_node *n, int indent)
82 {
83 const char *p, *t;
84 int i, j;
85 size_t argc;
86 struct mdoc_argv *argv;
87
88 if (n == NULL)
89 return;
90
91 argv = NULL;
92 argc = 0;
93 t = p = NULL;
94
95 switch (n->type) {
96 case ROFFT_ROOT:
97 t = "root";
98 break;
99 case ROFFT_BLOCK:
100 t = "block";
101 break;
102 case ROFFT_HEAD:
103 t = "head";
104 break;
105 case ROFFT_BODY:
106 if (n->end)
107 t = "body-end";
108 else
109 t = "body";
110 break;
111 case ROFFT_TAIL:
112 t = "tail";
113 break;
114 case ROFFT_ELEM:
115 t = "elem";
116 break;
117 case ROFFT_TEXT:
118 t = "text";
119 break;
120 case ROFFT_COMMENT:
121 t = "comment";
122 break;
123 case ROFFT_TBL:
124 break;
125 case ROFFT_EQN:
126 t = "eqn";
127 break;
128 default:
129 abort();
130 }
131
132 switch (n->type) {
133 case ROFFT_TEXT:
134 case ROFFT_COMMENT:
135 p = n->string;
136 break;
137 case ROFFT_BODY:
138 p = roff_name[n->tok];
139 break;
140 case ROFFT_HEAD:
141 p = roff_name[n->tok];
142 break;
143 case ROFFT_TAIL:
144 p = roff_name[n->tok];
145 break;
146 case ROFFT_ELEM:
147 p = roff_name[n->tok];
148 if (n->args) {
149 argv = n->args->argv;
150 argc = n->args->argc;
151 }
152 break;
153 case ROFFT_BLOCK:
154 p = roff_name[n->tok];
155 if (n->args) {
156 argv = n->args->argv;
157 argc = n->args->argc;
158 }
159 break;
160 case ROFFT_TBL:
161 break;
162 case ROFFT_EQN:
163 p = "EQ";
164 break;
165 case ROFFT_ROOT:
166 p = "root";
167 break;
168 default:
169 abort();
170 }
171
172 if (n->span) {
173 assert(NULL == p && NULL == t);
174 print_span(n->span, indent);
175 } else {
176 for (i = 0; i < indent; i++)
177 putchar(' ');
178
179 printf("%s (%s)", p, t);
180
181 for (i = 0; i < (int)argc; i++) {
182 printf(" -%s", mdoc_argnames[argv[i].arg]);
183 if (argv[i].sz > 0)
184 printf(" [");
185 for (j = 0; j < (int)argv[i].sz; j++)
186 printf(" [%s]", argv[i].value[j]);
187 if (argv[i].sz > 0)
188 printf(" ]");
189 }
190
191 putchar(' ');
192 if (NODE_DELIMO & n->flags)
193 putchar('(');
194 if (NODE_LINE & n->flags)
195 putchar('*');
196 printf("%d:%d", n->line, n->pos + 1);
197 if (NODE_DELIMC & n->flags)
198 putchar(')');
199 if (NODE_EOS & n->flags)
200 putchar('.');
201 if (NODE_BROKEN & n->flags)
202 printf(" BROKEN");
203 if (NODE_NOSRC & n->flags)
204 printf(" NOSRC");
205 if (NODE_NOPRT & n->flags)
206 printf(" NOPRT");
207 putchar('\n');
208 }
209
210 if (n->eqn)
211 print_box(n->eqn->first, indent + 4);
212 if (n->child)
213 print_mdoc(n->child, indent +
214 (n->type == ROFFT_BLOCK ? 2 : 4));
215 if (n->next)
216 print_mdoc(n->next, indent);
217 }
218
219 static void
220 print_man(const struct roff_node *n, int indent)
221 {
222 const char *p, *t;
223 int i;
224
225 if (n == NULL)
226 return;
227
228 t = p = NULL;
229
230 switch (n->type) {
231 case ROFFT_ROOT:
232 t = "root";
233 break;
234 case ROFFT_ELEM:
235 t = "elem";
236 break;
237 case ROFFT_TEXT:
238 t = "text";
239 break;
240 case ROFFT_COMMENT:
241 t = "comment";
242 break;
243 case ROFFT_BLOCK:
244 t = "block";
245 break;
246 case ROFFT_HEAD:
247 t = "head";
248 break;
249 case ROFFT_BODY:
250 t = "body";
251 break;
252 case ROFFT_TBL:
253 break;
254 case ROFFT_EQN:
255 t = "eqn";
256 break;
257 default:
258 abort();
259 }
260
261 switch (n->type) {
262 case ROFFT_TEXT:
263 case ROFFT_COMMENT:
264 p = n->string;
265 break;
266 case ROFFT_ELEM:
267 case ROFFT_BLOCK:
268 case ROFFT_HEAD:
269 case ROFFT_BODY:
270 p = roff_name[n->tok];
271 break;
272 case ROFFT_ROOT:
273 p = "root";
274 break;
275 case ROFFT_TBL:
276 break;
277 case ROFFT_EQN:
278 p = "EQ";
279 break;
280 default:
281 abort();
282 }
283
284 if (n->span) {
285 assert(NULL == p && NULL == t);
286 print_span(n->span, indent);
287 } else {
288 for (i = 0; i < indent; i++)
289 putchar(' ');
290 printf("%s (%s) ", p, t);
291 if (NODE_LINE & n->flags)
292 putchar('*');
293 printf("%d:%d", n->line, n->pos + 1);
294 if (NODE_DELIMC & n->flags)
295 putchar(')');
296 if (NODE_EOS & n->flags)
297 putchar('.');
298 putchar('\n');
299 }
300
301 if (n->eqn)
302 print_box(n->eqn->first, indent + 4);
303 if (n->child)
304 print_man(n->child, indent +
305 (n->type == ROFFT_BLOCK ? 2 : 4));
306 if (n->next)
307 print_man(n->next, indent);
308 }
309
310 static void
311 print_box(const struct eqn_box *ep, int indent)
312 {
313 int i;
314 const char *t;
315
316 static const char *posnames[] = {
317 NULL, "sup", "subsup", "sub",
318 "to", "from", "fromto",
319 "over", "sqrt", NULL };
320
321 if (NULL == ep)
322 return;
323 for (i = 0; i < indent; i++)
324 putchar(' ');
325
326 t = NULL;
327 switch (ep->type) {
328 case EQN_LIST:
329 t = "eqn-list";
330 break;
331 case EQN_SUBEXPR:
332 t = "eqn-expr";
333 break;
334 case EQN_TEXT:
335 t = "eqn-text";
336 break;
337 case EQN_PILE:
338 t = "eqn-pile";
339 break;
340 case EQN_MATRIX:
341 t = "eqn-matrix";
342 break;
343 }
344
345 fputs(t, stdout);
346 if (ep->pos)
347 printf(" pos=%s", posnames[ep->pos]);
348 if (ep->left)
349 printf(" left=\"%s\"", ep->left);
350 if (ep->right)
351 printf(" right=\"%s\"", ep->right);
352 if (ep->top)
353 printf(" top=\"%s\"", ep->top);
354 if (ep->bottom)
355 printf(" bottom=\"%s\"", ep->bottom);
356 if (ep->text)
357 printf(" text=\"%s\"", ep->text);
358 if (ep->font)
359 printf(" font=%d", ep->font);
360 if (ep->size != EQN_DEFSIZE)
361 printf(" size=%d", ep->size);
362 if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
363 printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
364 else if (ep->args)
365 printf(" args=%zu", ep->args);
366 putchar('\n');
367
368 print_box(ep->first, indent + 4);
369 print_box(ep->next, indent);
370 }
371
372 static void
373 print_span(const struct tbl_span *sp, int indent)
374 {
375 const struct tbl_dat *dp;
376 int i;
377
378 for (i = 0; i < indent; i++)
379 putchar(' ');
380
381 switch (sp->pos) {
382 case TBL_SPAN_HORIZ:
383 putchar('-');
384 putchar(' ');
385 break;
386 case TBL_SPAN_DHORIZ:
387 putchar('=');
388 putchar(' ');
389 break;
390 default:
391 for (dp = sp->first; dp; dp = dp->next) {
392 switch (dp->pos) {
393 case TBL_DATA_HORIZ:
394 case TBL_DATA_NHORIZ:
395 putchar('-');
396 putchar(' ');
397 continue;
398 case TBL_DATA_DHORIZ:
399 case TBL_DATA_NDHORIZ:
400 putchar('=');
401 putchar(' ');
402 continue;
403 default:
404 break;
405 }
406 printf("[\"%s\"", dp->string ? dp->string : "");
407 if (dp->hspans)
408 printf(">%d", dp->hspans);
409 if (dp->vspans)
410 printf("v%d", dp->vspans);
411 if (dp->layout == NULL)
412 putchar('*');
413 else if (dp->layout->pos == TBL_CELL_DOWN)
414 putchar('^');
415 putchar(']');
416 putchar(' ');
417 }
418 break;
419 }
420 printf("(tbl) %d:1\n", sp->line);
421 }