]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
b62c2adf7f2ad3820cc045c96f4553a8545fb79c
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.6 2009/02/21 21:00:06 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <stdlib.h>
20 #include <stdio.h>
21
22 #include "mdoc.h"
23
24 #define xprintf (void)printf
25
26 static void treeprint_r(const struct mdoc_node *, int);
27
28 static void
29 treeprint_r(const struct mdoc_node *n, int indent)
30 {
31 const char *p, *t;
32 int i, j;
33 size_t argc, sz;
34 char **params;
35 struct mdoc_arg *argv;
36
37 argv = NULL;
38 argc = sz = 0;
39 params = NULL;
40
41 switch (n->type) {
42 case (MDOC_ROOT):
43 t = "root";
44 break;
45 case (MDOC_BLOCK):
46 t = "block";
47 break;
48 case (MDOC_HEAD):
49 t = "block-head";
50 break;
51 case (MDOC_BODY):
52 t = "block-body";
53 break;
54 case (MDOC_TAIL):
55 t = "block-tail";
56 break;
57 case (MDOC_ELEM):
58 t = "elem";
59 break;
60 case (MDOC_TEXT):
61 t = "text";
62 break;
63 default:
64 abort();
65 /* NOTREACHED */
66 }
67
68 switch (n->type) {
69 case (MDOC_TEXT):
70 p = n->data.text.string;
71 break;
72 case (MDOC_BODY):
73 p = mdoc_macronames[n->tok];
74 break;
75 case (MDOC_HEAD):
76 p = mdoc_macronames[n->tok];
77 break;
78 case (MDOC_TAIL):
79 p = mdoc_macronames[n->tok];
80 break;
81 case (MDOC_ELEM):
82 p = mdoc_macronames[n->tok];
83 argv = n->data.elem.argv;
84 argc = n->data.elem.argc;
85 break;
86 case (MDOC_BLOCK):
87 p = mdoc_macronames[n->tok];
88 argv = n->data.block.argv;
89 argc = n->data.block.argc;
90 break;
91 case (MDOC_ROOT):
92 p = "root";
93 break;
94 default:
95 abort();
96 /* NOTREACHED */
97 }
98
99 for (i = 0; i < indent; i++)
100 xprintf(" ");
101 xprintf("%s (%s)", p, t);
102
103 for (i = 0; i < (int)argc; i++) {
104 xprintf(" -%s", mdoc_argnames[argv[i].arg]);
105 if (argv[i].sz > 0)
106 xprintf(" [");
107 for (j = 0; j < (int)argv[i].sz; j++)
108 xprintf(" [%s]", argv[i].value[j]);
109 if (argv[i].sz > 0)
110 xprintf(" ]");
111 }
112
113 for (i = 0; i < (int)sz; i++)
114 xprintf(" [%s]", params[i]);
115
116 xprintf(" %d:%d\n", n->line, n->pos);
117
118 if (n->child)
119 treeprint_r(n->child, indent + 1);
120 if (n->next)
121 treeprint_r(n->next, indent);
122 }
123
124
125 /* ARGSUSED */
126 void
127 treeprint(const struct mdoc_node *node,
128 const struct mdoc_meta *meta)
129 {
130
131 treeprint_r(node, 0);
132 }