Annotation of mandoc/tree.c, Revision 1.66
1.66 ! schwarze 1: /* $Id: tree.c,v 1.65 2015/04/18 16:06:41 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: /* NOTREACHED */
102: }
1.1 kristaps 103:
104: switch (n->type) {
1.63 schwarze 105: case ROFFT_TEXT:
1.8 kristaps 106: p = n->string;
1.1 kristaps 107: break;
1.63 schwarze 108: case ROFFT_BODY:
1.1 kristaps 109: p = mdoc_macronames[n->tok];
110: break;
1.63 schwarze 111: case ROFFT_HEAD:
1.1 kristaps 112: p = mdoc_macronames[n->tok];
113: break;
1.63 schwarze 114: case ROFFT_TAIL:
1.1 kristaps 115: p = mdoc_macronames[n->tok];
116: break;
1.63 schwarze 117: case ROFFT_ELEM:
1.1 kristaps 118: p = mdoc_macronames[n->tok];
1.8 kristaps 119: if (n->args) {
120: argv = n->args->argv;
121: argc = n->args->argc;
122: }
1.1 kristaps 123: break;
1.63 schwarze 124: case ROFFT_BLOCK:
1.1 kristaps 125: p = mdoc_macronames[n->tok];
1.8 kristaps 126: if (n->args) {
127: argv = n->args->argv;
128: argc = n->args->argc;
129: }
1.1 kristaps 130: break;
1.63 schwarze 131: case ROFFT_TBL:
1.59 schwarze 132: break;
1.63 schwarze 133: case ROFFT_EQN:
1.59 schwarze 134: p = "EQ";
1.33 kristaps 135: break;
1.63 schwarze 136: case ROFFT_ROOT:
1.1 kristaps 137: p = "root";
138: break;
139: default:
140: abort();
141: /* NOTREACHED */
142: }
143:
1.26 kristaps 144: if (n->span) {
1.38 kristaps 145: assert(NULL == p && NULL == t);
1.30 kristaps 146: print_span(n->span, indent);
1.26 kristaps 147: } else {
1.30 kristaps 148: for (i = 0; i < indent; i++)
1.61 schwarze 149: putchar(' ');
1.30 kristaps 150:
1.26 kristaps 151: printf("%s (%s)", p, t);
152:
153: for (i = 0; i < (int)argc; i++) {
154: printf(" -%s", mdoc_argnames[argv[i].arg]);
155: if (argv[i].sz > 0)
156: printf(" [");
157: for (j = 0; j < (int)argv[i].sz; j++)
158: printf(" [%s]", argv[i].value[j]);
159: if (argv[i].sz > 0)
160: printf(" ]");
161: }
1.49 schwarze 162:
163: putchar(' ');
164: if (MDOC_LINE & n->flags)
165: putchar('*');
1.62 schwarze 166: printf("%d:%d\n", n->line, n->pos + 1);
1.1 kristaps 167: }
168:
1.59 schwarze 169: if (n->eqn)
1.61 schwarze 170: print_box(n->eqn->root->first, indent + 4);
1.1 kristaps 171: if (n->child)
1.61 schwarze 172: print_mdoc(n->child, indent +
1.63 schwarze 173: (n->type == ROFFT_BLOCK ? 2 : 4));
1.1 kristaps 174: if (n->next)
1.11 kristaps 175: print_mdoc(n->next, indent);
1.1 kristaps 176: }
1.2 kristaps 177:
1.10 kristaps 178: static void
1.64 schwarze 179: print_man(const struct roff_node *n, int indent)
1.10 kristaps 180: {
181: const char *p, *t;
182: int i;
183:
1.61 schwarze 184: if (n == NULL)
185: return;
186:
1.38 kristaps 187: t = p = NULL;
188:
1.10 kristaps 189: switch (n->type) {
1.63 schwarze 190: case ROFFT_ROOT:
1.10 kristaps 191: t = "root";
192: break;
1.63 schwarze 193: case ROFFT_ELEM:
1.10 kristaps 194: t = "elem";
195: break;
1.63 schwarze 196: case ROFFT_TEXT:
1.10 kristaps 197: t = "text";
198: break;
1.63 schwarze 199: case ROFFT_BLOCK:
1.14 kristaps 200: t = "block";
201: break;
1.63 schwarze 202: case ROFFT_HEAD:
1.14 kristaps 203: t = "block-head";
204: break;
1.63 schwarze 205: case ROFFT_BODY:
1.14 kristaps 206: t = "block-body";
207: break;
1.63 schwarze 208: case ROFFT_TBL:
1.59 schwarze 209: break;
1.63 schwarze 210: case ROFFT_EQN:
1.59 schwarze 211: t = "eqn";
1.33 kristaps 212: break;
1.10 kristaps 213: default:
214: abort();
215: /* NOTREACHED */
216: }
217:
218: switch (n->type) {
1.63 schwarze 219: case ROFFT_TEXT:
1.10 kristaps 220: p = n->string;
221: break;
1.63 schwarze 222: case ROFFT_ELEM:
1.14 kristaps 223: /* FALLTHROUGH */
1.63 schwarze 224: case ROFFT_BLOCK:
1.14 kristaps 225: /* FALLTHROUGH */
1.63 schwarze 226: case ROFFT_HEAD:
1.14 kristaps 227: /* FALLTHROUGH */
1.63 schwarze 228: case ROFFT_BODY:
1.10 kristaps 229: p = man_macronames[n->tok];
230: break;
1.63 schwarze 231: case ROFFT_ROOT:
1.10 kristaps 232: p = "root";
1.25 kristaps 233: break;
1.63 schwarze 234: case ROFFT_TBL:
1.59 schwarze 235: break;
1.63 schwarze 236: case ROFFT_EQN:
1.59 schwarze 237: p = "EQ";
1.10 kristaps 238: break;
239: default:
240: abort();
241: /* NOTREACHED */
242: }
243:
1.26 kristaps 244: if (n->span) {
1.38 kristaps 245: assert(NULL == p && NULL == t);
1.30 kristaps 246: print_span(n->span, indent);
247: } else {
248: for (i = 0; i < indent; i++)
1.61 schwarze 249: putchar(' ');
1.51 schwarze 250: printf("%s (%s) ", p, t);
251: if (MAN_LINE & n->flags)
252: putchar('*');
1.53 schwarze 253: printf("%d:%d\n", n->line, n->pos + 1);
1.30 kristaps 254: }
1.26 kristaps 255:
1.59 schwarze 256: if (n->eqn)
1.61 schwarze 257: print_box(n->eqn->root->first, indent + 4);
1.10 kristaps 258: if (n->child)
1.61 schwarze 259: print_man(n->child, indent +
1.63 schwarze 260: (n->type == ROFFT_BLOCK ? 2 : 4));
1.10 kristaps 261: if (n->next)
1.11 kristaps 262: print_man(n->next, indent);
1.28 kristaps 263: }
264:
265: static void
1.38 kristaps 266: print_box(const struct eqn_box *ep, int indent)
267: {
268: int i;
1.46 kristaps 269: const char *t;
1.38 kristaps 270:
1.57 schwarze 271: static const char *posnames[] = {
272: NULL, "sup", "subsup", "sub",
273: "to", "from", "fromto",
274: "over", "sqrt", NULL };
275:
1.38 kristaps 276: if (NULL == ep)
277: return;
278: for (i = 0; i < indent; i++)
1.61 schwarze 279: putchar(' ');
1.38 kristaps 280:
1.46 kristaps 281: t = NULL;
1.38 kristaps 282: switch (ep->type) {
1.52 schwarze 283: case EQN_ROOT:
1.46 kristaps 284: t = "eqn-root";
1.45 kristaps 285: break;
1.56 kristaps 286: case EQN_LISTONE:
1.52 schwarze 287: case EQN_LIST:
1.46 kristaps 288: t = "eqn-list";
1.39 kristaps 289: break;
1.52 schwarze 290: case EQN_SUBEXPR:
1.46 kristaps 291: t = "eqn-expr";
1.39 kristaps 292: break;
1.52 schwarze 293: case EQN_TEXT:
1.46 kristaps 294: t = "eqn-text";
1.39 kristaps 295: break;
1.56 kristaps 296: case EQN_PILE:
297: t = "eqn-pile";
298: break;
1.52 schwarze 299: case EQN_MATRIX:
1.46 kristaps 300: t = "eqn-matrix";
1.38 kristaps 301: break;
302: }
1.39 kristaps 303:
1.57 schwarze 304: fputs(t, stdout);
305: if (ep->pos)
306: printf(" pos=%s", posnames[ep->pos]);
307: if (ep->left)
308: printf(" left=\"%s\"", ep->left);
309: if (ep->right)
310: printf(" right=\"%s\"", ep->right);
311: if (ep->top)
312: printf(" top=\"%s\"", ep->top);
313: if (ep->bottom)
314: printf(" bottom=\"%s\"", ep->bottom);
315: if (ep->text)
316: printf(" text=\"%s\"", ep->text);
317: if (ep->font)
318: printf(" font=%d", ep->font);
319: if (ep->size != EQN_DEFSIZE)
320: printf(" size=%d", ep->size);
321: if (ep->expectargs != UINT_MAX && ep->expectargs != ep->args)
322: printf(" badargs=%zu(%zu)", ep->args, ep->expectargs);
323: else if (ep->args)
324: printf(" args=%zu", ep->args);
325: putchar('\n');
1.46 kristaps 326:
1.61 schwarze 327: print_box(ep->first, indent + 4);
1.39 kristaps 328: print_box(ep->next, indent);
1.38 kristaps 329: }
330:
331: static void
1.30 kristaps 332: print_span(const struct tbl_span *sp, int indent)
1.28 kristaps 333: {
334: const struct tbl_dat *dp;
1.30 kristaps 335: int i;
336:
337: for (i = 0; i < indent; i++)
1.61 schwarze 338: putchar(' ');
1.28 kristaps 339:
340: switch (sp->pos) {
1.52 schwarze 341: case TBL_SPAN_HORIZ:
1.28 kristaps 342: putchar('-');
343: return;
1.52 schwarze 344: case TBL_SPAN_DHORIZ:
1.28 kristaps 345: putchar('=');
346: return;
347: default:
348: break;
349: }
350:
351: for (dp = sp->first; dp; dp = dp->next) {
352: switch (dp->pos) {
1.52 schwarze 353: case TBL_DATA_HORIZ:
1.28 kristaps 354: /* FALLTHROUGH */
1.52 schwarze 355: case TBL_DATA_NHORIZ:
1.28 kristaps 356: putchar('-');
357: continue;
1.52 schwarze 358: case TBL_DATA_DHORIZ:
1.28 kristaps 359: /* FALLTHROUGH */
1.52 schwarze 360: case TBL_DATA_NDHORIZ:
1.28 kristaps 361: putchar('=');
362: continue;
363: default:
364: break;
365: }
1.32 kristaps 366: printf("[\"%s\"", dp->string ? dp->string : "");
367: if (dp->spans)
368: printf("(%d)", dp->spans);
369: if (NULL == dp->layout)
370: putchar('*');
371: putchar(']');
1.34 kristaps 372: putchar(' ');
1.28 kristaps 373: }
1.34 kristaps 374:
1.38 kristaps 375: printf("(tbl) %d:1\n", sp->line);
1.10 kristaps 376: }
CVSweb