]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
Enable the unified error/warning enumeration in mandoc.h that's
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.20 2010/05/17 22:11:42 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 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 above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17 #ifdef HAVE_CONFIG_H
18 #include "config.h"
19 #endif
20
21 #include <assert.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "mandoc.h"
27 #include "mdoc.h"
28 #include "man.h"
29 #include "main.h"
30
31 static void print_mdoc(const struct mdoc_node *, int);
32 static void print_man(const struct man_node *, int);
33
34
35 /* ARGSUSED */
36 void
37 tree_mdoc(void *arg, const struct mdoc *mdoc)
38 {
39
40 print_mdoc(mdoc_node(mdoc), 0);
41 }
42
43
44 /* ARGSUSED */
45 void
46 tree_man(void *arg, const struct man *man)
47 {
48
49 print_man(man_node(man), 0);
50 }
51
52
53 static void
54 print_mdoc(const struct mdoc_node *n, int indent)
55 {
56 const char *p, *t;
57 int i, j;
58 size_t argc, sz;
59 char **params;
60 struct mdoc_argv *argv;
61
62 argv = NULL;
63 argc = sz = 0;
64 params = NULL;
65
66 switch (n->type) {
67 case (MDOC_ROOT):
68 t = "root";
69 break;
70 case (MDOC_BLOCK):
71 t = "block";
72 break;
73 case (MDOC_HEAD):
74 t = "block-head";
75 break;
76 case (MDOC_BODY):
77 t = "block-body";
78 break;
79 case (MDOC_TAIL):
80 t = "block-tail";
81 break;
82 case (MDOC_ELEM):
83 t = "elem";
84 break;
85 case (MDOC_TEXT):
86 t = "text";
87 break;
88 default:
89 abort();
90 /* NOTREACHED */
91 }
92
93 switch (n->type) {
94 case (MDOC_TEXT):
95 p = n->string;
96 break;
97 case (MDOC_BODY):
98 p = mdoc_macronames[n->tok];
99 break;
100 case (MDOC_HEAD):
101 p = mdoc_macronames[n->tok];
102 break;
103 case (MDOC_TAIL):
104 p = mdoc_macronames[n->tok];
105 break;
106 case (MDOC_ELEM):
107 p = mdoc_macronames[n->tok];
108 if (n->args) {
109 argv = n->args->argv;
110 argc = n->args->argc;
111 }
112 break;
113 case (MDOC_BLOCK):
114 p = mdoc_macronames[n->tok];
115 if (n->args) {
116 argv = n->args->argv;
117 argc = n->args->argc;
118 }
119 break;
120 case (MDOC_ROOT):
121 p = "root";
122 break;
123 default:
124 abort();
125 /* NOTREACHED */
126 }
127
128 for (i = 0; i < indent; i++)
129 (void)printf(" ");
130 (void)printf("%s (%s)", p, t);
131
132 for (i = 0; i < (int)argc; i++) {
133 (void)printf(" -%s", mdoc_argnames[argv[i].arg]);
134 if (argv[i].sz > 0)
135 (void)printf(" [");
136 for (j = 0; j < (int)argv[i].sz; j++)
137 (void)printf(" [%s]", argv[i].value[j]);
138 if (argv[i].sz > 0)
139 (void)printf(" ]");
140 }
141
142 for (i = 0; i < (int)sz; i++)
143 (void)printf(" [%s]", params[i]);
144
145 (void)printf(" %d:%d\n", n->line, n->pos);
146
147 if (n->child)
148 print_mdoc(n->child, indent + 1);
149 if (n->next)
150 print_mdoc(n->next, indent);
151 }
152
153
154 static void
155 print_man(const struct man_node *n, int indent)
156 {
157 const char *p, *t;
158 int i;
159
160 switch (n->type) {
161 case (MAN_ROOT):
162 t = "root";
163 break;
164 case (MAN_ELEM):
165 t = "elem";
166 break;
167 case (MAN_TEXT):
168 t = "text";
169 break;
170 case (MAN_BLOCK):
171 t = "block";
172 break;
173 case (MAN_HEAD):
174 t = "block-head";
175 break;
176 case (MAN_BODY):
177 t = "block-body";
178 break;
179 default:
180 abort();
181 /* NOTREACHED */
182 }
183
184 switch (n->type) {
185 case (MAN_TEXT):
186 p = n->string;
187 break;
188 case (MAN_ELEM):
189 /* FALLTHROUGH */
190 case (MAN_BLOCK):
191 /* FALLTHROUGH */
192 case (MAN_HEAD):
193 /* FALLTHROUGH */
194 case (MAN_BODY):
195 p = man_macronames[n->tok];
196 break;
197 case (MAN_ROOT):
198 p = "root";
199 break;
200 default:
201 abort();
202 /* NOTREACHED */
203 }
204
205 for (i = 0; i < indent; i++)
206 (void)printf(" ");
207 (void)printf("%s (%s) %d:%d\n", p, t, n->line, n->pos);
208
209 if (n->child)
210 print_man(n->child, indent + 1);
211 if (n->next)
212 print_man(n->next, indent);
213 }