1 /* $Id: tree.c,v 1.77 2017/07/08 14:51:05 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013, 2014, 2015, 2017 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
20 #include <sys/types.h>
34 static void print_box(const struct eqn_box *, int);
35 static void print_man(const struct roff_node *, int);
36 static void print_meta(const struct roff_meta *);
37 static void print_mdoc(const struct roff_node *, int);
38 static void print_span(const struct tbl_span *, int);
42 tree_mdoc(void *arg, const struct roff_man *mdoc)
44 print_meta(&mdoc->meta);
46 print_mdoc(mdoc->first->child, 0);
50 tree_man(void *arg, const struct roff_man *man)
52 print_meta(&man->meta);
53 if (man->meta.hasbody == 0)
56 print_man(man->first->child, 0);
60 print_meta(const struct roff_meta *meta)
62 if (meta->title != NULL)
63 printf("title = \"%s\"\n", meta->title);
64 if (meta->name != NULL)
65 printf("name = \"%s\"\n", meta->name);
66 if (meta->msec != NULL)
67 printf("sec = \"%s\"\n", meta->msec);
68 if (meta->vol != NULL)
69 printf("vol = \"%s\"\n", meta->vol);
70 if (meta->arch != NULL)
71 printf("arch = \"%s\"\n", meta->arch);
73 printf("os = \"%s\"\n", meta->os);
74 if (meta->date != NULL)
75 printf("date = \"%s\"\n", meta->date);
79 print_mdoc(const struct roff_node *n, int indent)
84 struct mdoc_argv *argv;
132 p = roff_name[n->tok];
135 p = roff_name[n->tok];
138 p = roff_name[n->tok];
141 p = roff_name[n->tok];
143 argv = n->args->argv;
144 argc = n->args->argc;
148 p = roff_name[n->tok];
150 argv = n->args->argv;
151 argc = n->args->argc;
167 assert(NULL == p && NULL == t);
168 print_span(n->span, indent);
170 for (i = 0; i < indent; i++)
173 printf("%s (%s)", p, t);
175 for (i = 0; i < (int)argc; i++) {
176 printf(" -%s", mdoc_argnames[argv[i].arg]);
179 for (j = 0; j < (int)argv[i].sz; j++)
180 printf(" [%s]", argv[i].value[j]);
186 if (NODE_DELIMO & n->flags)
188 if (NODE_LINE & n->flags)
190 printf("%d:%d", n->line, n->pos + 1);
191 if (NODE_DELIMC & n->flags)
193 if (NODE_EOS & n->flags)
195 if (NODE_BROKEN & n->flags)
197 if (NODE_NOSRC & n->flags)
199 if (NODE_NOPRT & n->flags)
205 print_box(n->eqn->first, indent + 4);
207 print_mdoc(n->child, indent +
208 (n->type == ROFFT_BLOCK ? 2 : 4));
210 print_mdoc(n->next, indent);
214 print_man(const struct roff_node *n, int indent)
260 p = roff_name[n->tok];
275 assert(NULL == p && NULL == t);
276 print_span(n->span, indent);
278 for (i = 0; i < indent; i++)
280 printf("%s (%s) ", p, t);
281 if (NODE_LINE & n->flags)
283 printf("%d:%d", n->line, n->pos + 1);
284 if (NODE_EOS & n->flags)
290 print_box(n->eqn->first, indent + 4);
292 print_man(n->child, indent +
293 (n->type == ROFFT_BLOCK ? 2 : 4));
295 print_man(n->next, indent);
299 print_box(const struct eqn_box *ep, int indent)
304 static const char *posnames[] = {
305 NULL, "sup", "subsup", "sub",
306 "to", "from", "fromto",
307 "over", "sqrt", NULL };
311 for (i = 0; i < indent; i++)
335 printf(" pos=%s", posnames[ep->pos]);
337 printf(" left=\"%s\"", ep->left);
339 printf(" right=\"%s\"", ep->right);
341 printf(" top=\"%s\"", ep->top);
343 printf(" bottom=\"%s\"", ep->bottom);
345 printf(" text=\"%s\"", ep->text);
347 printf(" font=%d", ep->font);
348 if (ep->size != EQN_DEFSIZE)
349 printf(" size=%d", ep->size);
350 if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
351 printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
353 printf(" args=%zu", ep->args);
356 print_box(ep->first, indent + 4);
357 print_box(ep->next, indent);
361 print_span(const struct tbl_span *sp, int indent)
363 const struct tbl_dat *dp;
366 for (i = 0; i < indent; i++)
373 case TBL_SPAN_DHORIZ:
380 for (dp = sp->first; dp; dp = dp->next) {
383 case TBL_DATA_NHORIZ:
386 case TBL_DATA_DHORIZ:
387 case TBL_DATA_NDHORIZ:
393 printf("[\"%s\"", dp->string ? dp->string : "");
395 printf("(%d)", dp->spans);
396 if (NULL == dp->layout)
402 printf("(tbl) %d:1\n", sp->line);