Annotation of mandoc/tree.c, Revision 1.67
1.67 ! schwarze 1: /* $Id: tree.c,v 1.66 2015/04/18 17:53:21 schwarze Exp $ */
1.1 kristaps 2: /*
1.58 schwarze 3: * Copyright (c) 2008, 2009, 2011, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.61 schwarze 4: * Copyright (c) 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.12 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.63 schwarze 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.12 kristaps 11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.63 schwarze 12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.12 kristaps 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.
1.1 kristaps 17: */
1.19 kristaps 18: #include "config.h"
1.54 schwarze 19:
20: #include <sys/types.h>
1.19 kristaps 21:
1.8 kristaps 22: #include <assert.h>
1.43 kristaps 23: #include <limits.h>
1.8 kristaps 24: #include <stdio.h>
1.1 kristaps 25: #include <stdlib.h>
1.17 kristaps 26: #include <time.h>
1.1 kristaps 27:
1.20 kristaps 28: #include "mandoc.h"
1.63 schwarze 29: #include "roff.h"
1.1 kristaps 30: #include "mdoc.h"
1.10 kristaps 31: #include "man.h"
1.16 kristaps 32: #include "main.h"
1.1 kristaps 33:
1.38 kristaps 34: static void print_box(const struct eqn_box *, int);
1.64 schwarze 35: static void print_man(const struct roff_node *, int);
36: static void print_mdoc(const struct roff_node *, int);
1.30 kristaps 37: static void print_span(const struct tbl_span *, int);
1.8 kristaps 38:
39:
1.15 kristaps 40: void
1.65 schwarze 41: tree_mdoc(void *arg, const struct roff_man *mdoc)
1.8 kristaps 42: {
43:
1.66 schwarze 44: print_mdoc(mdoc->first->child, 0);
1.11 kristaps 45: }
46:
1.15 kristaps 47: void
1.65 schwarze 48: tree_man(void *arg, const struct roff_man *man)
1.11 kristaps 49: {
50:
1.66 schwarze 51: print_man(man->first->child, 0);
1.8 kristaps 52: }
1.2 kristaps 53:
1.1 kristaps 54: static void
1.64 schwarze 55: print_mdoc(const struct roff_node *n, int indent)
1.1 kristaps 56: {
57: const char *p, *t;
58: int i, j;
1.48 schwarze 59: size_t argc;
1.8 kristaps 60: struct mdoc_argv *argv;
1.1 kristaps 61:
1.61 schwarze 62: if (n == NULL)
63: return;
64:
1.1 kristaps 65: argv = NULL;
1.48 schwarze 66: argc = 0;
1.38 kristaps 67: t = p = NULL;
1.1 kristaps 68:
1.2 kristaps 69: switch (n->type) {
1.63 schwarze 70: case ROFFT_ROOT:
1.2 kristaps 71: t = "root";
72: break;
1.63 schwarze 73: case ROFFT_BLOCK:
1.2 kristaps 74: t = "block";
75: break;
1.63 schwarze 76: case ROFFT_HEAD:
1.2 kristaps 77: t = "block-head";
78: break;
1.63 schwarze 79: case ROFFT_BODY:
1.23 schwarze 80: if (n->end)
81: t = "body-end";
82: else
83: t = "block-body";
1.2 kristaps 84: break;
1.63 schwarze 85: case ROFFT_TAIL:
1.2 kristaps 86: t = "block-tail";
87: break;
1.63 schwarze 88: case ROFFT_ELEM:
1.2 kristaps 89: t = "elem";
90: break;
1.63 schwarze 91: case ROFFT_TEXT:
1.2 kristaps 92: t = "text";
93: break;
1.63 schwarze 94: case ROFFT_TBL:
1.59 schwarze 95: break;
1.63 schwarze 96: case ROFFT_EQN:
1.59 schwarze 97: t = "eqn";
1.33 kristaps 98: break;
1.2 kristaps 99: default:
100: abort();
101: }
1.1 kristaps 102:
103: switch (n->type) {
1.63 schwarze 104: case ROFFT_TEXT:
1.8 kristaps 105: p = n->string;
1.1 kristaps 106: break;
1.63 schwarze 107: case ROFFT_BODY:
1.1 kristaps 108: p = mdoc_macronames[n->tok];
109: break;
1.63 schwarze 110: case ROFFT_HEAD:
1.1 kristaps 111: p = mdoc_macronames[n->tok];
112: break;
1.63 schwarze 113: case ROFFT_TAIL:
1.1 kristaps 114: p = mdoc_macronames[n->tok];
115: break;
1.63 schwarze 116: case ROFFT_ELEM:
1.1 kristaps 117: p = mdoc_macronames[n->tok];
1.8 kristaps 118: if (n->args) {
119: argv = n->args->argv;
120: argc = n->args->argc;
121: }
1.1 kristaps 122: break;
1.63 schwarze 123: case ROFFT_BLOCK:
1.1 kristaps 124: p = mdoc_macronames[n->tok];
1.8 kristaps 125: if (n->args) {
126: argv = n->args->argv;
127: argc = n->args->argc;
128: }
1.1 kristaps 129: break;
1.63 schwarze 130: case ROFFT_TBL:
1.59 schwarze 131: break;
1.63 schwarze 132: case ROFFT_EQN:
1.59 schwarze 133: p = "EQ";
1.33 kristaps 134: break;
1.63 schwarze 135: case ROFFT_ROOT:
1.1 kristaps 136: p = "root";
137: break;
138: default:
139: abort();
140: }
141:
1.26 kristaps 142: if (n->span) {
1.38 kristaps 143: assert(NULL == p && NULL == t);
1.30 kristaps 144: print_span(n->span, indent);
1.26 kristaps 145: } else {
1.30 kristaps 146: for (i = 0; i < indent; i++)
1.61 schwarze 147: putchar(' ');
1.30 kristaps 148:
1.26 kristaps 149: printf("%s (%s)", p, t);
150:
151: for (i = 0; i < (int)argc; i++) {
152: printf(" -%s", mdoc_argnames[argv[i].arg]);
153: if (argv[i].sz > 0)
154: printf(" [");
155: for (j = 0; j < (int)argv[i].sz; j++)
156: printf(" [%s]", argv[i].value[j]);
157: if (argv[i].sz > 0)
158: printf(" ]");
159: }
1.49 schwarze 160:
161: putchar(' ');
162: if (MDOC_LINE & n->flags)
163: putchar('*');
1.62 schwarze 164: printf("%d:%d\n", n->line, n->pos + 1);
1.1 kristaps 165: }
166:
1.59 schwarze 167: if (n->eqn)
1.61 schwarze 168: print_box(n->eqn->root->first, indent + 4);
1.1 kristaps 169: if (n->child)
1.61 schwarze 170: print_mdoc(n->child, indent +
1.63 schwarze 171: (n->type == ROFFT_BLOCK ? 2 : 4));
1.1 kristaps 172: if (n->next)
1.11 kristaps 173: print_mdoc(n->next, indent);
1.1 kristaps 174: }
1.2 kristaps 175:
1.10 kristaps 176: static void
1.64 schwarze 177: print_man(const struct roff_node *n, int indent)
1.10 kristaps 178: {
179: const char *p, *t;
180: int i;
181:
1.61 schwarze 182: if (n == NULL)
183: return;
184:
1.38 kristaps 185: t = p = NULL;
186:
1.10 kristaps 187: switch (n->type) {
1.63 schwarze 188: case ROFFT_ROOT:
1.10 kristaps 189: t = "root";
190: break;
1.63 schwarze 191: case ROFFT_ELEM:
1.10 kristaps 192: t = "elem";
193: break;
1.63 schwarze 194: case ROFFT_TEXT:
1.10 kristaps 195: t = "text";
196: break;
1.63 schwarze 197: case ROFFT_BLOCK:
1.14 kristaps 198: t = "block";
199: break;
1.63 schwarze 200: case ROFFT_HEAD:
1.14 kristaps 201: t = "block-head";
202: break;
1.63 schwarze 203: case ROFFT_BODY:
1.14 kristaps 204: t = "block-body";
205: break;
1.63 schwarze 206: case ROFFT_TBL:
1.59 schwarze 207: break;
1.63 schwarze 208: case ROFFT_EQN:
1.59 schwarze 209: t = "eqn";
1.33 kristaps 210: break;
1.10 kristaps 211: default:
212: abort();
213: }
214:
215: switch (n->type) {
1.63 schwarze 216: case ROFFT_TEXT:
1.10 kristaps 217: p = n->string;
218: break;
1.63 schwarze 219: case ROFFT_ELEM:
1.14 kristaps 220: /* FALLTHROUGH */
1.63 schwarze 221: case ROFFT_BLOCK:
1.14 kristaps 222: /* FALLTHROUGH */
1.63 schwarze 223: case ROFFT_HEAD:
1.14 kristaps 224: /* FALLTHROUGH */
1.63 schwarze 225: case ROFFT_BODY:
1.10 kristaps 226: p = man_macronames[n->tok];
227: break;
1.63 schwarze 228: case ROFFT_ROOT:
1.10 kristaps 229: p = "root";
1.25 kristaps 230: break;
1.63 schwarze 231: case ROFFT_TBL:
1.59 schwarze 232: break;
1.63 schwarze 233: case ROFFT_EQN:
1.59 schwarze 234: p = "EQ";
1.10 kristaps 235: break;
236: default:
237: abort();
238: }
239:
1.26 kristaps 240: if (n->span) {
1.38 kristaps 241: assert(NULL == p && NULL == t);
1.30 kristaps 242: print_span(n->span, indent);
243: } else {
244: for (i = 0; i < indent; i++)
1.61 schwarze 245: putchar(' ');
1.51 schwarze 246: printf("%s (%s) ", p, t);
247: if (MAN_LINE & n->flags)
248: putchar('*');
1.53 schwarze 249: printf("%d:%d\n", n->line, n->pos + 1);
1.30 kristaps 250: }
1.26 kristaps 251:
1.59 schwarze 252: if (n->eqn)
1.61 schwarze 253: print_box(n->eqn->root->first, indent + 4);
1.10 kristaps 254: if (n->child)
1.61 schwarze 255: print_man(n->child, indent +
1.63 schwarze 256: (n->type == ROFFT_BLOCK ? 2 : 4));
1.10 kristaps 257: if (n->next)
1.11 kristaps 258: print_man(n->next, indent);
1.28 kristaps 259: }
260:
261: static void
1.38 kristaps 262: print_box(const struct eqn_box *ep, int indent)
263: {
264: int i;
1.46 kristaps 265: const char *t;
1.38 kristaps 266:
1.57 schwarze 267: static const char *posnames[] = {
268: NULL, "sup", "subsup", "sub",
269: "to", "from", "fromto",
270: "over", "sqrt", NULL };
271:
1.38 kristaps 272: if (NULL == ep)
273: return;
274: for (i = 0; i < indent; i++)
1.61 schwarze 275: putchar(' ');
1.38 kristaps 276:
1.46 kristaps 277: t = NULL;
1.38 kristaps 278: switch (ep->type) {
1.52 schwarze 279: case EQN_ROOT:
1.46 kristaps 280: t = "eqn-root";
1.45 kristaps 281: break;
1.56 kristaps 282: case EQN_LISTONE:
1.52 schwarze 283: case EQN_LIST:
1.46 kristaps 284: t = "eqn-list";
1.39 kristaps 285: break;
1.52 schwarze 286: case EQN_SUBEXPR:
1.46 kristaps 287: t = "eqn-expr";
1.39 kristaps 288: break;
1.52 schwarze 289: case EQN_TEXT:
1.46 kristaps 290: t = "eqn-text";
1.39 kristaps 291: break;
1.56 kristaps 292: case EQN_PILE:
293: t = "eqn-pile";
294: break;
1.52 schwarze 295: case EQN_MATRIX:
1.46 kristaps 296: t = "eqn-matrix";
1.38 kristaps 297: break;
298: }
1.39 kristaps 299:
1.57 schwarze 300: fputs(t, stdout);
301: if (ep->pos)
302: printf(" pos=%s", posnames[ep->pos]);
303: if (ep->left)
304: printf(" left=\"%s\"", ep->left);
305: if (ep->right)
306: printf(" right=\"%s\"", ep->right);
307: if (ep->top)
308: printf(" top=\"%s\"", ep->top);
309: if (ep->bottom)
310: printf(" bottom=\"%s\"", ep->bottom);
311: if (ep->text)
312: printf(" text=\"%s\"", ep->text);
313: if (ep->font)
314: printf(" font=%d", ep->font);
315: if (ep->size != EQN_DEFSIZE)
316: printf(" size=%d", ep->size);
317: if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
318: printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
319: else if (ep->args)
320: printf(" args=%zu", ep->args);
321: putchar('\n');
1.46 kristaps 322:
1.61 schwarze 323: print_box(ep->first, indent + 4);
1.39 kristaps 324: print_box(ep->next, indent);
1.38 kristaps 325: }
326:
327: static void
1.30 kristaps 328: print_span(const struct tbl_span *sp, int indent)
1.28 kristaps 329: {
330: const struct tbl_dat *dp;
1.30 kristaps 331: int i;
332:
333: for (i = 0; i < indent; i++)
1.61 schwarze 334: putchar(' ');
1.28 kristaps 335:
336: switch (sp->pos) {
1.52 schwarze 337: case TBL_SPAN_HORIZ:
1.28 kristaps 338: putchar('-');
339: return;
1.52 schwarze 340: case TBL_SPAN_DHORIZ:
1.28 kristaps 341: putchar('=');
342: return;
343: default:
344: break;
345: }
346:
347: for (dp = sp->first; dp; dp = dp->next) {
348: switch (dp->pos) {
1.52 schwarze 349: case TBL_DATA_HORIZ:
1.28 kristaps 350: /* FALLTHROUGH */
1.52 schwarze 351: case TBL_DATA_NHORIZ:
1.28 kristaps 352: putchar('-');
353: continue;
1.52 schwarze 354: case TBL_DATA_DHORIZ:
1.28 kristaps 355: /* FALLTHROUGH */
1.52 schwarze 356: case TBL_DATA_NDHORIZ:
1.28 kristaps 357: putchar('=');
358: continue;
359: default:
360: break;
361: }
1.32 kristaps 362: printf("[\"%s\"", dp->string ? dp->string : "");
363: if (dp->spans)
364: printf("(%d)", dp->spans);
365: if (NULL == dp->layout)
366: putchar('*');
367: putchar(']');
1.34 kristaps 368: putchar(' ');
1.28 kristaps 369: }
1.34 kristaps 370:
1.38 kristaps 371: printf("(tbl) %d:1\n", sp->line);
1.10 kristaps 372: }
CVSweb