1*544c191cSchristos /* Id: tree.c,v 1.84 2019/01/01 05:56:34 schwarze Exp */
24154958bSjoerg /*
3fec65c98Schristos * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4*544c191cSchristos * 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 *
109ff1f2acSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
114154958bSjoerg * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
129ff1f2acSchristos * 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"
19fec65c98Schristos
20fec65c98Schristos #include <sys/types.h>
21d5e63c8dSjoerg
224154958bSjoerg #include <assert.h>
23c5f73b34Sjoerg #include <limits.h>
244154958bSjoerg #include <stdio.h>
254154958bSjoerg #include <stdlib.h>
263514411fSjoerg #include <time.h>
274154958bSjoerg
280a84adc5Sjoerg #include "mandoc.h"
299ff1f2acSchristos #include "roff.h"
304154958bSjoerg #include "mdoc.h"
314154958bSjoerg #include "man.h"
32*544c191cSchristos #include "tbl.h"
33*544c191cSchristos #include "eqn.h"
344154958bSjoerg #include "main.h"
354154958bSjoerg
36c5f73b34Sjoerg static void print_box(const struct eqn_box *, int);
379ff1f2acSchristos static void print_man(const struct roff_node *, int);
389508192eSchristos static void print_meta(const struct roff_meta *);
399ff1f2acSchristos static void print_mdoc(const struct roff_node *, int);
40c0d9444aSjoerg static void print_span(const struct tbl_span *, int);
414154958bSjoerg
424154958bSjoerg
434154958bSjoerg void
tree_mdoc(void * arg,const struct roff_meta * mdoc)44*544c191cSchristos tree_mdoc(void *arg, const struct roff_meta *mdoc)
454154958bSjoerg {
46*544c191cSchristos print_meta(mdoc);
479508192eSchristos putchar('\n');
489ff1f2acSchristos print_mdoc(mdoc->first->child, 0);
494154958bSjoerg }
504154958bSjoerg
514154958bSjoerg void
tree_man(void * arg,const struct roff_meta * man)52*544c191cSchristos tree_man(void *arg, const struct roff_meta *man)
534154958bSjoerg {
54*544c191cSchristos print_meta(man);
55*544c191cSchristos if (man->hasbody == 0)
569508192eSchristos puts("body = empty");
579508192eSchristos putchar('\n');
589ff1f2acSchristos print_man(man->first->child, 0);
594154958bSjoerg }
604154958bSjoerg
614154958bSjoerg static void
print_meta(const struct roff_meta * meta)629508192eSchristos print_meta(const struct roff_meta *meta)
639508192eSchristos {
649508192eSchristos if (meta->title != NULL)
659508192eSchristos printf("title = \"%s\"\n", meta->title);
669508192eSchristos if (meta->name != NULL)
679508192eSchristos printf("name = \"%s\"\n", meta->name);
689508192eSchristos if (meta->msec != NULL)
699508192eSchristos printf("sec = \"%s\"\n", meta->msec);
709508192eSchristos if (meta->vol != NULL)
719508192eSchristos printf("vol = \"%s\"\n", meta->vol);
729508192eSchristos if (meta->arch != NULL)
739508192eSchristos printf("arch = \"%s\"\n", meta->arch);
749508192eSchristos if (meta->os != NULL)
759508192eSchristos printf("os = \"%s\"\n", meta->os);
769508192eSchristos if (meta->date != NULL)
779508192eSchristos printf("date = \"%s\"\n", meta->date);
789508192eSchristos }
799508192eSchristos
809508192eSchristos static void
print_mdoc(const struct roff_node * n,int indent)819ff1f2acSchristos print_mdoc(const struct roff_node *n, int indent)
824154958bSjoerg {
834154958bSjoerg const char *p, *t;
844154958bSjoerg int i, j;
8570f041f9Sjoerg size_t argc;
864154958bSjoerg struct mdoc_argv *argv;
874154958bSjoerg
88fec65c98Schristos if (n == NULL)
89fec65c98Schristos return;
90fec65c98Schristos
914154958bSjoerg argv = NULL;
9270f041f9Sjoerg argc = 0;
93c5f73b34Sjoerg t = p = NULL;
944154958bSjoerg
954154958bSjoerg switch (n->type) {
969ff1f2acSchristos case ROFFT_ROOT:
974154958bSjoerg t = "root";
984154958bSjoerg break;
999ff1f2acSchristos case ROFFT_BLOCK:
1004154958bSjoerg t = "block";
1014154958bSjoerg break;
1029ff1f2acSchristos case ROFFT_HEAD:
1039ff1f2acSchristos t = "head";
1044154958bSjoerg break;
1059ff1f2acSchristos case ROFFT_BODY:
10682361f10Sjoerg if (n->end)
10782361f10Sjoerg t = "body-end";
10882361f10Sjoerg else
1099ff1f2acSchristos t = "body";
1104154958bSjoerg break;
1119ff1f2acSchristos case ROFFT_TAIL:
1129ff1f2acSchristos t = "tail";
1134154958bSjoerg break;
1149ff1f2acSchristos case ROFFT_ELEM:
1154154958bSjoerg t = "elem";
1164154958bSjoerg break;
1179ff1f2acSchristos case ROFFT_TEXT:
1184154958bSjoerg t = "text";
1194154958bSjoerg break;
120c9bcef03Schristos case ROFFT_COMMENT:
121c9bcef03Schristos t = "comment";
122c9bcef03Schristos break;
1239ff1f2acSchristos case ROFFT_TBL:
124fec65c98Schristos break;
1259ff1f2acSchristos case ROFFT_EQN:
126fec65c98Schristos t = "eqn";
12748741257Sjoerg break;
1284154958bSjoerg default:
1294154958bSjoerg abort();
1304154958bSjoerg }
1314154958bSjoerg
1324154958bSjoerg switch (n->type) {
1339ff1f2acSchristos case ROFFT_TEXT:
134c9bcef03Schristos case ROFFT_COMMENT:
1354154958bSjoerg p = n->string;
1364154958bSjoerg break;
1379ff1f2acSchristos case ROFFT_BODY:
138c9bcef03Schristos p = roff_name[n->tok];
1394154958bSjoerg break;
1409ff1f2acSchristos case ROFFT_HEAD:
141c9bcef03Schristos p = roff_name[n->tok];
1424154958bSjoerg break;
1439ff1f2acSchristos case ROFFT_TAIL:
144c9bcef03Schristos p = roff_name[n->tok];
1454154958bSjoerg break;
1469ff1f2acSchristos case ROFFT_ELEM:
147c9bcef03Schristos p = roff_name[n->tok];
1484154958bSjoerg if (n->args) {
1494154958bSjoerg argv = n->args->argv;
1504154958bSjoerg argc = n->args->argc;
1514154958bSjoerg }
1524154958bSjoerg break;
1539ff1f2acSchristos case ROFFT_BLOCK:
154c9bcef03Schristos p = roff_name[n->tok];
1554154958bSjoerg if (n->args) {
1564154958bSjoerg argv = n->args->argv;
1574154958bSjoerg argc = n->args->argc;
1584154958bSjoerg }
1594154958bSjoerg break;
1609ff1f2acSchristos case ROFFT_TBL:
16148741257Sjoerg break;
1629ff1f2acSchristos case ROFFT_EQN:
163fec65c98Schristos p = "EQ";
164fec65c98Schristos break;
1659ff1f2acSchristos case ROFFT_ROOT:
1664154958bSjoerg p = "root";
1674154958bSjoerg break;
1684154958bSjoerg default:
1694154958bSjoerg abort();
1704154958bSjoerg }
1714154958bSjoerg
172c0d9444aSjoerg if (n->span) {
173c5f73b34Sjoerg assert(NULL == p && NULL == t);
174c0d9444aSjoerg print_span(n->span, indent);
175c0d9444aSjoerg } else {
1764154958bSjoerg for (i = 0; i < indent; i++)
177fec65c98Schristos putchar(' ');
178c0d9444aSjoerg
179c0d9444aSjoerg printf("%s (%s)", p, t);
1804154958bSjoerg
1814154958bSjoerg for (i = 0; i < (int)argc; i++) {
182c0d9444aSjoerg printf(" -%s", mdoc_argnames[argv[i].arg]);
1834154958bSjoerg if (argv[i].sz > 0)
184c0d9444aSjoerg printf(" [");
1854154958bSjoerg for (j = 0; j < (int)argv[i].sz; j++)
186c0d9444aSjoerg printf(" [%s]", argv[i].value[j]);
1874154958bSjoerg if (argv[i].sz > 0)
188c0d9444aSjoerg printf(" ]");
1894154958bSjoerg }
1904154958bSjoerg
19170f041f9Sjoerg putchar(' ');
192*544c191cSchristos if (n->flags & NODE_DELIMO)
1939ff1f2acSchristos putchar('(');
194*544c191cSchristos if (n->flags & NODE_LINE)
19570f041f9Sjoerg putchar('*');
1969ff1f2acSchristos printf("%d:%d", n->line, n->pos + 1);
197*544c191cSchristos if (n->flags & NODE_DELIMC)
1989ff1f2acSchristos putchar(')');
199*544c191cSchristos if (n->flags & NODE_EOS)
2009ff1f2acSchristos putchar('.');
201*544c191cSchristos if (n->flags & NODE_BROKEN)
2029508192eSchristos printf(" BROKEN");
203*544c191cSchristos if (n->flags & NODE_NOFILL)
204*544c191cSchristos printf(" NOFILL");
205*544c191cSchristos if (n->flags & NODE_NOSRC)
2069508192eSchristos printf(" NOSRC");
207*544c191cSchristos if (n->flags & NODE_NOPRT)
2089508192eSchristos printf(" NOPRT");
2099ff1f2acSchristos putchar('\n');
210c0d9444aSjoerg }
211c0d9444aSjoerg
212fec65c98Schristos if (n->eqn)
213c9bcef03Schristos print_box(n->eqn->first, indent + 4);
2144154958bSjoerg if (n->child)
215fec65c98Schristos print_mdoc(n->child, indent +
2169ff1f2acSchristos (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)2229ff1f2acSchristos print_man(const struct roff_node *n, int indent)
2234154958bSjoerg {
2244154958bSjoerg const char *p, *t;
2254154958bSjoerg int i;
2264154958bSjoerg
227fec65c98Schristos if (n == NULL)
228fec65c98Schristos return;
229fec65c98Schristos
230c5f73b34Sjoerg t = p = NULL;
231c5f73b34Sjoerg
2324154958bSjoerg switch (n->type) {
2339ff1f2acSchristos case ROFFT_ROOT:
2344154958bSjoerg t = "root";
2354154958bSjoerg break;
2369ff1f2acSchristos case ROFFT_ELEM:
2374154958bSjoerg t = "elem";
2384154958bSjoerg break;
2399ff1f2acSchristos case ROFFT_TEXT:
2404154958bSjoerg t = "text";
2414154958bSjoerg break;
242c9bcef03Schristos case ROFFT_COMMENT:
243c9bcef03Schristos t = "comment";
244c9bcef03Schristos break;
2459ff1f2acSchristos case ROFFT_BLOCK:
2464154958bSjoerg t = "block";
2474154958bSjoerg break;
2489ff1f2acSchristos case ROFFT_HEAD:
2499ff1f2acSchristos t = "head";
2504154958bSjoerg break;
2519ff1f2acSchristos case ROFFT_BODY:
2529ff1f2acSchristos t = "body";
2534154958bSjoerg break;
2549ff1f2acSchristos case ROFFT_TBL:
25548741257Sjoerg break;
2569ff1f2acSchristos case ROFFT_EQN:
257fec65c98Schristos t = "eqn";
25848741257Sjoerg break;
2594154958bSjoerg default:
2604154958bSjoerg abort();
2614154958bSjoerg }
2624154958bSjoerg
2634154958bSjoerg switch (n->type) {
2649ff1f2acSchristos case ROFFT_TEXT:
265c9bcef03Schristos case ROFFT_COMMENT:
2664154958bSjoerg p = n->string;
2674154958bSjoerg break;
2689ff1f2acSchristos case ROFFT_ELEM:
2699ff1f2acSchristos case ROFFT_BLOCK:
2709ff1f2acSchristos case ROFFT_HEAD:
2719ff1f2acSchristos case ROFFT_BODY:
272c9bcef03Schristos p = roff_name[n->tok];
2734154958bSjoerg break;
2749ff1f2acSchristos case ROFFT_ROOT:
2754154958bSjoerg p = "root";
2764154958bSjoerg break;
2779ff1f2acSchristos case ROFFT_TBL:
278fec65c98Schristos break;
2799ff1f2acSchristos case ROFFT_EQN:
280fec65c98Schristos p = "EQ";
28148741257Sjoerg break;
2824154958bSjoerg default:
2834154958bSjoerg abort();
2844154958bSjoerg }
2854154958bSjoerg
286c0d9444aSjoerg if (n->span) {
287c5f73b34Sjoerg assert(NULL == p && NULL == t);
288c0d9444aSjoerg print_span(n->span, indent);
289c0d9444aSjoerg } else {
2904154958bSjoerg for (i = 0; i < indent; i++)
291fec65c98Schristos putchar(' ');
292fec65c98Schristos printf("%s (%s) ", p, t);
293*544c191cSchristos if (n->flags & NODE_LINE)
294fec65c98Schristos putchar('*');
2959ff1f2acSchristos printf("%d:%d", n->line, n->pos + 1);
296*544c191cSchristos if (n->flags & NODE_DELIMC)
297*544c191cSchristos putchar(')');
298*544c191cSchristos if (n->flags & NODE_EOS)
2999ff1f2acSchristos putchar('.');
300*544c191cSchristos if (n->flags & NODE_NOFILL)
301*544c191cSchristos printf(" NOFILL");
3029ff1f2acSchristos putchar('\n');
303c0d9444aSjoerg }
304c0d9444aSjoerg
305fec65c98Schristos if (n->eqn)
306c9bcef03Schristos print_box(n->eqn->first, indent + 4);
3074154958bSjoerg if (n->child)
308fec65c98Schristos print_man(n->child, indent +
3099ff1f2acSchristos (n->type == ROFFT_BLOCK ? 2 : 4));
3104154958bSjoerg if (n->next)
3114154958bSjoerg print_man(n->next, indent);
3124154958bSjoerg }
313c0d9444aSjoerg
314c0d9444aSjoerg static void
print_box(const struct eqn_box * ep,int indent)315c5f73b34Sjoerg print_box(const struct eqn_box *ep, int indent)
316c5f73b34Sjoerg {
317c5f73b34Sjoerg int i;
318c5f73b34Sjoerg const char *t;
319c5f73b34Sjoerg
320fec65c98Schristos static const char *posnames[] = {
321fec65c98Schristos NULL, "sup", "subsup", "sub",
322fec65c98Schristos "to", "from", "fromto",
323fec65c98Schristos "over", "sqrt", NULL };
324fec65c98Schristos
325c5f73b34Sjoerg if (NULL == ep)
326c5f73b34Sjoerg return;
327c5f73b34Sjoerg for (i = 0; i < indent; i++)
328fec65c98Schristos putchar(' ');
329c5f73b34Sjoerg
330c5f73b34Sjoerg t = NULL;
331c5f73b34Sjoerg switch (ep->type) {
332fec65c98Schristos case EQN_LIST:
333c5f73b34Sjoerg t = "eqn-list";
334c5f73b34Sjoerg break;
335fec65c98Schristos case EQN_SUBEXPR:
336c5f73b34Sjoerg t = "eqn-expr";
337c5f73b34Sjoerg break;
338fec65c98Schristos case EQN_TEXT:
339c5f73b34Sjoerg t = "eqn-text";
340c5f73b34Sjoerg break;
341fec65c98Schristos case EQN_PILE:
342fec65c98Schristos t = "eqn-pile";
343fec65c98Schristos break;
344fec65c98Schristos case EQN_MATRIX:
345c5f73b34Sjoerg t = "eqn-matrix";
346c5f73b34Sjoerg break;
347c5f73b34Sjoerg }
348c5f73b34Sjoerg
349fec65c98Schristos fputs(t, stdout);
350fec65c98Schristos if (ep->pos)
351fec65c98Schristos printf(" pos=%s", posnames[ep->pos]);
352fec65c98Schristos if (ep->left)
353fec65c98Schristos printf(" left=\"%s\"", ep->left);
354fec65c98Schristos if (ep->right)
355fec65c98Schristos printf(" right=\"%s\"", ep->right);
356fec65c98Schristos if (ep->top)
357fec65c98Schristos printf(" top=\"%s\"", ep->top);
358fec65c98Schristos if (ep->bottom)
359fec65c98Schristos printf(" bottom=\"%s\"", ep->bottom);
360fec65c98Schristos if (ep->text)
361fec65c98Schristos printf(" text=\"%s\"", ep->text);
362fec65c98Schristos if (ep->font)
363fec65c98Schristos printf(" font=%d", ep->font);
364fec65c98Schristos if (ep->size != EQN_DEFSIZE)
365fec65c98Schristos printf(" size=%d", ep->size);
366fec65c98Schristos if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
367fec65c98Schristos printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
368fec65c98Schristos else if (ep->args)
369fec65c98Schristos printf(" args=%zu", ep->args);
370fec65c98Schristos putchar('\n');
371c5f73b34Sjoerg
372fec65c98Schristos print_box(ep->first, indent + 4);
373c5f73b34Sjoerg print_box(ep->next, indent);
374c5f73b34Sjoerg }
375c5f73b34Sjoerg
376c5f73b34Sjoerg static void
print_span(const struct tbl_span * sp,int indent)377c0d9444aSjoerg print_span(const struct tbl_span *sp, int indent)
378c0d9444aSjoerg {
379c0d9444aSjoerg const struct tbl_dat *dp;
380c0d9444aSjoerg int i;
381c0d9444aSjoerg
382c0d9444aSjoerg for (i = 0; i < indent; i++)
383fec65c98Schristos putchar(' ');
384c0d9444aSjoerg
385c0d9444aSjoerg switch (sp->pos) {
386fec65c98Schristos case TBL_SPAN_HORIZ:
387c0d9444aSjoerg putchar('-');
388*544c191cSchristos putchar(' ');
389*544c191cSchristos break;
390fec65c98Schristos case TBL_SPAN_DHORIZ:
391c0d9444aSjoerg putchar('=');
392*544c191cSchristos putchar(' ');
393c0d9444aSjoerg break;
394*544c191cSchristos default:
395c0d9444aSjoerg for (dp = sp->first; dp; dp = dp->next) {
396c0d9444aSjoerg switch (dp->pos) {
397fec65c98Schristos case TBL_DATA_HORIZ:
398fec65c98Schristos case TBL_DATA_NHORIZ:
399c0d9444aSjoerg putchar('-');
400*544c191cSchristos putchar(' ');
401c0d9444aSjoerg continue;
402fec65c98Schristos case TBL_DATA_DHORIZ:
403fec65c98Schristos case TBL_DATA_NDHORIZ:
404c0d9444aSjoerg putchar('=');
405*544c191cSchristos putchar(' ');
406c0d9444aSjoerg continue;
407c0d9444aSjoerg default:
408c0d9444aSjoerg break;
409c0d9444aSjoerg }
41048741257Sjoerg printf("[\"%s\"", dp->string ? dp->string : "");
411*544c191cSchristos if (dp->hspans)
412*544c191cSchristos printf(">%d", dp->hspans);
413*544c191cSchristos if (dp->vspans)
414*544c191cSchristos printf("v%d", dp->vspans);
415*544c191cSchristos if (dp->layout == NULL)
41648741257Sjoerg putchar('*');
417*544c191cSchristos else if (dp->layout->pos == TBL_CELL_DOWN)
418*544c191cSchristos putchar('^');
41948741257Sjoerg putchar(']');
420c0d9444aSjoerg putchar(' ');
421c0d9444aSjoerg }
422*544c191cSchristos break;
423*544c191cSchristos }
424c5f73b34Sjoerg printf("(tbl) %d:1\n", sp->line);
425c0d9444aSjoerg }
426