1*fc3ee6fdSchristos /* Id: tree.c,v 1.84 2019/01/01 05:56:34 schwarze Exp */
24154958bSjoerg /*
30511d63cSchristos * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4*fc3ee6fdSchristos * Copyright (c) 2013-2015, 2017-2019 Ingo Schwarze <schwarze@openbsd.org>
54154958bSjoerg *
64154958bSjoerg * Permission to use, copy, modify, and distribute this software for any
74154958bSjoerg * purpose with or without fee is hereby granted, provided that the above
84154958bSjoerg * copyright notice and this permission notice appear in all copies.
94154958bSjoerg *
1047a17e0dSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
114154958bSjoerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1247a17e0dSchristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
134154958bSjoerg * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
144154958bSjoerg * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
154154958bSjoerg * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
164154958bSjoerg * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
174154958bSjoerg */
18d5e63c8dSjoerg #include "config.h"
190511d63cSchristos
200511d63cSchristos #include <sys/types.h>
21d5e63c8dSjoerg
224154958bSjoerg #include <assert.h>
23f8126693Sjoerg #include <limits.h>
244154958bSjoerg #include <stdio.h>
254154958bSjoerg #include <stdlib.h>
263514411fSjoerg #include <time.h>
274154958bSjoerg
280a84adc5Sjoerg #include "mandoc.h"
2947a17e0dSchristos #include "roff.h"
304154958bSjoerg #include "mdoc.h"
314154958bSjoerg #include "man.h"
32*fc3ee6fdSchristos #include "tbl.h"
33*fc3ee6fdSchristos #include "eqn.h"
344154958bSjoerg #include "main.h"
354154958bSjoerg
36f8126693Sjoerg static void print_box(const struct eqn_box *, int);
3747a17e0dSchristos static void print_man(const struct roff_node *, int);
38b2070ba5Schristos static void print_meta(const struct roff_meta *);
3947a17e0dSchristos static void print_mdoc(const struct roff_node *, int);
40e4fbeb7eSjoerg static void print_span(const struct tbl_span *, int);
414154958bSjoerg
424154958bSjoerg
434154958bSjoerg void
tree_mdoc(void * arg,const struct roff_meta * mdoc)44*fc3ee6fdSchristos tree_mdoc(void *arg, const struct roff_meta *mdoc)
454154958bSjoerg {
46*fc3ee6fdSchristos print_meta(mdoc);
47b2070ba5Schristos putchar('\n');
4847a17e0dSchristos print_mdoc(mdoc->first->child, 0);
494154958bSjoerg }
504154958bSjoerg
514154958bSjoerg void
tree_man(void * arg,const struct roff_meta * man)52*fc3ee6fdSchristos tree_man(void *arg, const struct roff_meta *man)
534154958bSjoerg {
54*fc3ee6fdSchristos print_meta(man);
55*fc3ee6fdSchristos if (man->hasbody == 0)
56b2070ba5Schristos puts("body = empty");
57b2070ba5Schristos putchar('\n');
5847a17e0dSchristos print_man(man->first->child, 0);
594154958bSjoerg }
604154958bSjoerg
614154958bSjoerg static void
print_meta(const struct roff_meta * meta)62b2070ba5Schristos print_meta(const struct roff_meta *meta)
63b2070ba5Schristos {
64b2070ba5Schristos if (meta->title != NULL)
65b2070ba5Schristos printf("title = \"%s\"\n", meta->title);
66b2070ba5Schristos if (meta->name != NULL)
67b2070ba5Schristos printf("name = \"%s\"\n", meta->name);
68b2070ba5Schristos if (meta->msec != NULL)
69b2070ba5Schristos printf("sec = \"%s\"\n", meta->msec);
70b2070ba5Schristos if (meta->vol != NULL)
71b2070ba5Schristos printf("vol = \"%s\"\n", meta->vol);
72b2070ba5Schristos if (meta->arch != NULL)
73b2070ba5Schristos printf("arch = \"%s\"\n", meta->arch);
74b2070ba5Schristos if (meta->os != NULL)
75b2070ba5Schristos printf("os = \"%s\"\n", meta->os);
76b2070ba5Schristos if (meta->date != NULL)
77b2070ba5Schristos printf("date = \"%s\"\n", meta->date);
78b2070ba5Schristos }
79b2070ba5Schristos
80b2070ba5Schristos static void
print_mdoc(const struct roff_node * n,int indent)8147a17e0dSchristos print_mdoc(const struct roff_node *n, int indent)
824154958bSjoerg {
834154958bSjoerg const char *p, *t;
844154958bSjoerg int i, j;
85ed7c7912Sjoerg size_t argc;
864154958bSjoerg struct mdoc_argv *argv;
874154958bSjoerg
880511d63cSchristos if (n == NULL)
890511d63cSchristos return;
900511d63cSchristos
914154958bSjoerg argv = NULL;
92ed7c7912Sjoerg argc = 0;
93f8126693Sjoerg t = p = NULL;
944154958bSjoerg
954154958bSjoerg switch (n->type) {
9647a17e0dSchristos case ROFFT_ROOT:
974154958bSjoerg t = "root";
984154958bSjoerg break;
9947a17e0dSchristos case ROFFT_BLOCK:
1004154958bSjoerg t = "block";
1014154958bSjoerg break;
10247a17e0dSchristos case ROFFT_HEAD:
10347a17e0dSchristos t = "head";
1044154958bSjoerg break;
10547a17e0dSchristos case ROFFT_BODY:
10682361f10Sjoerg if (n->end)
10782361f10Sjoerg t = "body-end";
10882361f10Sjoerg else
10947a17e0dSchristos t = "body";
1104154958bSjoerg break;
11147a17e0dSchristos case ROFFT_TAIL:
11247a17e0dSchristos t = "tail";
1134154958bSjoerg break;
11447a17e0dSchristos case ROFFT_ELEM:
1154154958bSjoerg t = "elem";
1164154958bSjoerg break;
11747a17e0dSchristos case ROFFT_TEXT:
1184154958bSjoerg t = "text";
1194154958bSjoerg break;
1209d9b81f7Schristos case ROFFT_COMMENT:
1219d9b81f7Schristos t = "comment";
1229d9b81f7Schristos break;
12347a17e0dSchristos case ROFFT_TBL:
1240511d63cSchristos break;
12547a17e0dSchristos case ROFFT_EQN:
1260511d63cSchristos t = "eqn";
12754a4f7feSjoerg break;
1284154958bSjoerg default:
1294154958bSjoerg abort();
1304154958bSjoerg }
1314154958bSjoerg
1324154958bSjoerg switch (n->type) {
13347a17e0dSchristos case ROFFT_TEXT:
1349d9b81f7Schristos case ROFFT_COMMENT:
1354154958bSjoerg p = n->string;
1364154958bSjoerg break;
13747a17e0dSchristos case ROFFT_BODY:
1389d9b81f7Schristos p = roff_name[n->tok];
1394154958bSjoerg break;
14047a17e0dSchristos case ROFFT_HEAD:
1419d9b81f7Schristos p = roff_name[n->tok];
1424154958bSjoerg break;
14347a17e0dSchristos case ROFFT_TAIL:
1449d9b81f7Schristos p = roff_name[n->tok];
1454154958bSjoerg break;
14647a17e0dSchristos case ROFFT_ELEM:
1479d9b81f7Schristos p = roff_name[n->tok];
1484154958bSjoerg if (n->args) {
1494154958bSjoerg argv = n->args->argv;
1504154958bSjoerg argc = n->args->argc;
1514154958bSjoerg }
1524154958bSjoerg break;
15347a17e0dSchristos case ROFFT_BLOCK:
1549d9b81f7Schristos p = roff_name[n->tok];
1554154958bSjoerg if (n->args) {
1564154958bSjoerg argv = n->args->argv;
1574154958bSjoerg argc = n->args->argc;
1584154958bSjoerg }
1594154958bSjoerg break;
16047a17e0dSchristos case ROFFT_TBL:
16154a4f7feSjoerg break;
16247a17e0dSchristos case ROFFT_EQN:
1630511d63cSchristos p = "EQ";
1640511d63cSchristos break;
16547a17e0dSchristos case ROFFT_ROOT:
1664154958bSjoerg p = "root";
1674154958bSjoerg break;
1684154958bSjoerg default:
1694154958bSjoerg abort();
1704154958bSjoerg }
1714154958bSjoerg
172e4fbeb7eSjoerg if (n->span) {
173f8126693Sjoerg assert(NULL == p && NULL == t);
174e4fbeb7eSjoerg print_span(n->span, indent);
175e4fbeb7eSjoerg } else {
1764154958bSjoerg for (i = 0; i < indent; i++)
1770511d63cSchristos putchar(' ');
178e4fbeb7eSjoerg
179e4fbeb7eSjoerg printf("%s (%s)", p, t);
1804154958bSjoerg
1814154958bSjoerg for (i = 0; i < (int)argc; i++) {
182e4fbeb7eSjoerg printf(" -%s", mdoc_argnames[argv[i].arg]);
1834154958bSjoerg if (argv[i].sz > 0)
184e4fbeb7eSjoerg printf(" [");
1854154958bSjoerg for (j = 0; j < (int)argv[i].sz; j++)
186e4fbeb7eSjoerg printf(" [%s]", argv[i].value[j]);
1874154958bSjoerg if (argv[i].sz > 0)
188e4fbeb7eSjoerg printf(" ]");
1894154958bSjoerg }
1904154958bSjoerg
191ed7c7912Sjoerg putchar(' ');
192*fc3ee6fdSchristos if (n->flags & NODE_DELIMO)
19347a17e0dSchristos putchar('(');
194*fc3ee6fdSchristos if (n->flags & NODE_LINE)
195ed7c7912Sjoerg putchar('*');
19647a17e0dSchristos printf("%d:%d", n->line, n->pos + 1);
197*fc3ee6fdSchristos if (n->flags & NODE_DELIMC)
19847a17e0dSchristos putchar(')');
199*fc3ee6fdSchristos if (n->flags & NODE_EOS)
20047a17e0dSchristos putchar('.');
201*fc3ee6fdSchristos if (n->flags & NODE_BROKEN)
202b2070ba5Schristos printf(" BROKEN");
203*fc3ee6fdSchristos if (n->flags & NODE_NOFILL)
204*fc3ee6fdSchristos printf(" NOFILL");
205*fc3ee6fdSchristos if (n->flags & NODE_NOSRC)
206b2070ba5Schristos printf(" NOSRC");
207*fc3ee6fdSchristos if (n->flags & NODE_NOPRT)
208b2070ba5Schristos printf(" NOPRT");
20947a17e0dSchristos putchar('\n');
210e4fbeb7eSjoerg }
211e4fbeb7eSjoerg
2120511d63cSchristos if (n->eqn)
2139d9b81f7Schristos print_box(n->eqn->first, indent + 4);
2144154958bSjoerg if (n->child)
2150511d63cSchristos print_mdoc(n->child, indent +
21647a17e0dSchristos (n->type == ROFFT_BLOCK ? 2 : 4));
2174154958bSjoerg if (n->next)
2184154958bSjoerg print_mdoc(n->next, indent);
2194154958bSjoerg }
2204154958bSjoerg
2214154958bSjoerg static void
print_man(const struct roff_node * n,int indent)22247a17e0dSchristos print_man(const struct roff_node *n, int indent)
2234154958bSjoerg {
2244154958bSjoerg const char *p, *t;
2254154958bSjoerg int i;
2264154958bSjoerg
2270511d63cSchristos if (n == NULL)
2280511d63cSchristos return;
2290511d63cSchristos
230f8126693Sjoerg t = p = NULL;
231f8126693Sjoerg
2324154958bSjoerg switch (n->type) {
23347a17e0dSchristos case ROFFT_ROOT:
2344154958bSjoerg t = "root";
2354154958bSjoerg break;
23647a17e0dSchristos case ROFFT_ELEM:
2374154958bSjoerg t = "elem";
2384154958bSjoerg break;
23947a17e0dSchristos case ROFFT_TEXT:
2404154958bSjoerg t = "text";
2414154958bSjoerg break;
2429d9b81f7Schristos case ROFFT_COMMENT:
2439d9b81f7Schristos t = "comment";
2449d9b81f7Schristos break;
24547a17e0dSchristos case ROFFT_BLOCK:
2464154958bSjoerg t = "block";
2474154958bSjoerg break;
24847a17e0dSchristos case ROFFT_HEAD:
24947a17e0dSchristos t = "head";
2504154958bSjoerg break;
25147a17e0dSchristos case ROFFT_BODY:
25247a17e0dSchristos t = "body";
2534154958bSjoerg break;
25447a17e0dSchristos case ROFFT_TBL:
25554a4f7feSjoerg break;
25647a17e0dSchristos case ROFFT_EQN:
2570511d63cSchristos t = "eqn";
25854a4f7feSjoerg break;
2594154958bSjoerg default:
2604154958bSjoerg abort();
2614154958bSjoerg }
2624154958bSjoerg
2634154958bSjoerg switch (n->type) {
26447a17e0dSchristos case ROFFT_TEXT:
2659d9b81f7Schristos case ROFFT_COMMENT:
2664154958bSjoerg p = n->string;
2674154958bSjoerg break;
26847a17e0dSchristos case ROFFT_ELEM:
26947a17e0dSchristos case ROFFT_BLOCK:
27047a17e0dSchristos case ROFFT_HEAD:
27147a17e0dSchristos case ROFFT_BODY:
2729d9b81f7Schristos p = roff_name[n->tok];
2734154958bSjoerg break;
27447a17e0dSchristos case ROFFT_ROOT:
2754154958bSjoerg p = "root";
2764154958bSjoerg break;
27747a17e0dSchristos case ROFFT_TBL:
2780511d63cSchristos break;
27947a17e0dSchristos case ROFFT_EQN:
2800511d63cSchristos p = "EQ";
28154a4f7feSjoerg break;
2824154958bSjoerg default:
2834154958bSjoerg abort();
2844154958bSjoerg }
2854154958bSjoerg
286e4fbeb7eSjoerg if (n->span) {
287f8126693Sjoerg assert(NULL == p && NULL == t);
288e4fbeb7eSjoerg print_span(n->span, indent);
289e4fbeb7eSjoerg } else {
2904154958bSjoerg for (i = 0; i < indent; i++)
2910511d63cSchristos putchar(' ');
2920511d63cSchristos printf("%s (%s) ", p, t);
293*fc3ee6fdSchristos if (n->flags & NODE_LINE)
2940511d63cSchristos putchar('*');
29547a17e0dSchristos printf("%d:%d", n->line, n->pos + 1);
296*fc3ee6fdSchristos if (n->flags & NODE_DELIMC)
297*fc3ee6fdSchristos putchar(')');
298*fc3ee6fdSchristos if (n->flags & NODE_EOS)
29947a17e0dSchristos putchar('.');
300*fc3ee6fdSchristos if (n->flags & NODE_NOFILL)
301*fc3ee6fdSchristos printf(" NOFILL");
30247a17e0dSchristos putchar('\n');
303e4fbeb7eSjoerg }
304e4fbeb7eSjoerg
3050511d63cSchristos if (n->eqn)
3069d9b81f7Schristos print_box(n->eqn->first, indent + 4);
3074154958bSjoerg if (n->child)
3080511d63cSchristos print_man(n->child, indent +
30947a17e0dSchristos (n->type == ROFFT_BLOCK ? 2 : 4));
3104154958bSjoerg if (n->next)
3114154958bSjoerg print_man(n->next, indent);
3124154958bSjoerg }
313e4fbeb7eSjoerg
314e4fbeb7eSjoerg static void
print_box(const struct eqn_box * ep,int indent)315f8126693Sjoerg print_box(const struct eqn_box *ep, int indent)
316f8126693Sjoerg {
317f8126693Sjoerg int i;
318f8126693Sjoerg const char *t;
319f8126693Sjoerg
3200511d63cSchristos static const char *posnames[] = {
3210511d63cSchristos NULL, "sup", "subsup", "sub",
3220511d63cSchristos "to", "from", "fromto",
3230511d63cSchristos "over", "sqrt", NULL };
3240511d63cSchristos
325f8126693Sjoerg if (NULL == ep)
326f8126693Sjoerg return;
327f8126693Sjoerg for (i = 0; i < indent; i++)
3280511d63cSchristos putchar(' ');
329f8126693Sjoerg
330f8126693Sjoerg t = NULL;
331f8126693Sjoerg switch (ep->type) {
3320511d63cSchristos case EQN_LIST:
333f8126693Sjoerg t = "eqn-list";
334f8126693Sjoerg break;
3350511d63cSchristos case EQN_SUBEXPR:
336f8126693Sjoerg t = "eqn-expr";
337f8126693Sjoerg break;
3380511d63cSchristos case EQN_TEXT:
339f8126693Sjoerg t = "eqn-text";
340f8126693Sjoerg break;
3410511d63cSchristos case EQN_PILE:
3420511d63cSchristos t = "eqn-pile";
3430511d63cSchristos break;
3440511d63cSchristos case EQN_MATRIX:
345f8126693Sjoerg t = "eqn-matrix";
346f8126693Sjoerg break;
347f8126693Sjoerg }
348f8126693Sjoerg
3490511d63cSchristos fputs(t, stdout);
3500511d63cSchristos if (ep->pos)
3510511d63cSchristos printf(" pos=%s", posnames[ep->pos]);
3520511d63cSchristos if (ep->left)
3530511d63cSchristos printf(" left=\"%s\"", ep->left);
3540511d63cSchristos if (ep->right)
3550511d63cSchristos printf(" right=\"%s\"", ep->right);
3560511d63cSchristos if (ep->top)
3570511d63cSchristos printf(" top=\"%s\"", ep->top);
3580511d63cSchristos if (ep->bottom)
3590511d63cSchristos printf(" bottom=\"%s\"", ep->bottom);
3600511d63cSchristos if (ep->text)
3610511d63cSchristos printf(" text=\"%s\"", ep->text);
3620511d63cSchristos if (ep->font)
3630511d63cSchristos printf(" font=%d", ep->font);
3640511d63cSchristos if (ep->size != EQN_DEFSIZE)
3650511d63cSchristos printf(" size=%d", ep->size);
3660511d63cSchristos if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
3670511d63cSchristos printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
3680511d63cSchristos else if (ep->args)
3690511d63cSchristos printf(" args=%zu", ep->args);
3700511d63cSchristos putchar('\n');
371f8126693Sjoerg
3720511d63cSchristos print_box(ep->first, indent + 4);
373f8126693Sjoerg print_box(ep->next, indent);
374f8126693Sjoerg }
375f8126693Sjoerg
376f8126693Sjoerg static void
print_span(const struct tbl_span * sp,int indent)377e4fbeb7eSjoerg print_span(const struct tbl_span *sp, int indent)
378e4fbeb7eSjoerg {
379e4fbeb7eSjoerg const struct tbl_dat *dp;
380e4fbeb7eSjoerg int i;
381e4fbeb7eSjoerg
382e4fbeb7eSjoerg for (i = 0; i < indent; i++)
3830511d63cSchristos putchar(' ');
384e4fbeb7eSjoerg
385e4fbeb7eSjoerg switch (sp->pos) {
3860511d63cSchristos case TBL_SPAN_HORIZ:
387e4fbeb7eSjoerg putchar('-');
388*fc3ee6fdSchristos putchar(' ');
389*fc3ee6fdSchristos break;
3900511d63cSchristos case TBL_SPAN_DHORIZ:
391e4fbeb7eSjoerg putchar('=');
392*fc3ee6fdSchristos putchar(' ');
393e4fbeb7eSjoerg break;
394*fc3ee6fdSchristos default:
395e4fbeb7eSjoerg for (dp = sp->first; dp; dp = dp->next) {
396e4fbeb7eSjoerg switch (dp->pos) {
3970511d63cSchristos case TBL_DATA_HORIZ:
3980511d63cSchristos case TBL_DATA_NHORIZ:
399e4fbeb7eSjoerg putchar('-');
400*fc3ee6fdSchristos putchar(' ');
401e4fbeb7eSjoerg continue;
4020511d63cSchristos case TBL_DATA_DHORIZ:
4030511d63cSchristos case TBL_DATA_NDHORIZ:
404e4fbeb7eSjoerg putchar('=');
405*fc3ee6fdSchristos putchar(' ');
406e4fbeb7eSjoerg continue;
407e4fbeb7eSjoerg default:
408e4fbeb7eSjoerg break;
409e4fbeb7eSjoerg }
41054a4f7feSjoerg printf("[\"%s\"", dp->string ? dp->string : "");
411*fc3ee6fdSchristos if (dp->hspans)
412*fc3ee6fdSchristos printf(">%d", dp->hspans);
413*fc3ee6fdSchristos if (dp->vspans)
414*fc3ee6fdSchristos printf("v%d", dp->vspans);
415*fc3ee6fdSchristos if (dp->layout == NULL)
41654a4f7feSjoerg putchar('*');
417*fc3ee6fdSchristos else if (dp->layout->pos == TBL_CELL_DOWN)
418*fc3ee6fdSchristos putchar('^');
41954a4f7feSjoerg putchar(']');
420e4fbeb7eSjoerg putchar(' ');
421e4fbeb7eSjoerg }
422*fc3ee6fdSchristos break;
423*fc3ee6fdSchristos }
424f8126693Sjoerg printf("(tbl) %d:1\n", sp->line);
425e4fbeb7eSjoerg }
426