]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
Full support for eqn positionals (above, over, sup, sub, etc.).
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.42 2011/07/21 14:13:00 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@bsd.lv>
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_box(const struct eqn_box *, int);
32 static void print_man(const struct man_node *, int);
33 static void print_mdoc(const struct mdoc_node *, int);
34 static void print_span(const struct tbl_span *, int);
35
36
37 /* ARGSUSED */
38 void
39 tree_mdoc(void *arg, const struct mdoc *mdoc)
40 {
41
42 print_mdoc(mdoc_node(mdoc), 0);
43 }
44
45
46 /* ARGSUSED */
47 void
48 tree_man(void *arg, const struct man *man)
49 {
50
51 print_man(man_node(man), 0);
52 }
53
54
55 static void
56 print_mdoc(const struct mdoc_node *n, int indent)
57 {
58 const char *p, *t;
59 int i, j;
60 size_t argc, sz;
61 char **params;
62 struct mdoc_argv *argv;
63
64 argv = NULL;
65 argc = sz = 0;
66 params = NULL;
67 t = p = NULL;
68
69 switch (n->type) {
70 case (MDOC_ROOT):
71 t = "root";
72 break;
73 case (MDOC_BLOCK):
74 t = "block";
75 break;
76 case (MDOC_HEAD):
77 t = "block-head";
78 break;
79 case (MDOC_BODY):
80 if (n->end)
81 t = "body-end";
82 else
83 t = "block-body";
84 break;
85 case (MDOC_TAIL):
86 t = "block-tail";
87 break;
88 case (MDOC_ELEM):
89 t = "elem";
90 break;
91 case (MDOC_TEXT):
92 t = "text";
93 break;
94 case (MDOC_TBL):
95 /* FALLTHROUGH */
96 case (MDOC_EQN):
97 break;
98 default:
99 abort();
100 /* NOTREACHED */
101 }
102
103 switch (n->type) {
104 case (MDOC_TEXT):
105 p = n->string;
106 break;
107 case (MDOC_BODY):
108 p = mdoc_macronames[n->tok];
109 break;
110 case (MDOC_HEAD):
111 p = mdoc_macronames[n->tok];
112 break;
113 case (MDOC_TAIL):
114 p = mdoc_macronames[n->tok];
115 break;
116 case (MDOC_ELEM):
117 p = mdoc_macronames[n->tok];
118 if (n->args) {
119 argv = n->args->argv;
120 argc = n->args->argc;
121 }
122 break;
123 case (MDOC_BLOCK):
124 p = mdoc_macronames[n->tok];
125 if (n->args) {
126 argv = n->args->argv;
127 argc = n->args->argc;
128 }
129 break;
130 case (MDOC_TBL):
131 /* FALLTHROUGH */
132 case (MDOC_EQN):
133 break;
134 case (MDOC_ROOT):
135 p = "root";
136 break;
137 default:
138 abort();
139 /* NOTREACHED */
140 }
141
142 if (n->span) {
143 assert(NULL == p && NULL == t);
144 print_span(n->span, indent);
145 } else if (n->eqn) {
146 assert(NULL == p && NULL == t);
147 print_box(n->eqn->root, indent);
148 } else {
149 for (i = 0; i < indent; i++)
150 putchar('\t');
151
152 printf("%s (%s)", p, t);
153
154 for (i = 0; i < (int)argc; i++) {
155 printf(" -%s", mdoc_argnames[argv[i].arg]);
156 if (argv[i].sz > 0)
157 printf(" [");
158 for (j = 0; j < (int)argv[i].sz; j++)
159 printf(" [%s]", argv[i].value[j]);
160 if (argv[i].sz > 0)
161 printf(" ]");
162 }
163
164 for (i = 0; i < (int)sz; i++)
165 printf(" [%s]", params[i]);
166
167 printf(" %d:%d\n", n->line, n->pos);
168 }
169
170 if (n->child)
171 print_mdoc(n->child, indent + 1);
172 if (n->next)
173 print_mdoc(n->next, indent);
174 }
175
176
177 static void
178 print_man(const struct man_node *n, int indent)
179 {
180 const char *p, *t;
181 int i;
182
183 t = p = NULL;
184
185 switch (n->type) {
186 case (MAN_ROOT):
187 t = "root";
188 break;
189 case (MAN_ELEM):
190 t = "elem";
191 break;
192 case (MAN_TEXT):
193 t = "text";
194 break;
195 case (MAN_BLOCK):
196 t = "block";
197 break;
198 case (MAN_HEAD):
199 t = "block-head";
200 break;
201 case (MAN_BODY):
202 t = "block-body";
203 break;
204 case (MAN_TAIL):
205 t = "block-tail";
206 break;
207 case (MAN_TBL):
208 /* FALLTHROUGH */
209 case (MAN_EQN):
210 break;
211 default:
212 abort();
213 /* NOTREACHED */
214 }
215
216 switch (n->type) {
217 case (MAN_TEXT):
218 p = n->string;
219 break;
220 case (MAN_ELEM):
221 /* FALLTHROUGH */
222 case (MAN_BLOCK):
223 /* FALLTHROUGH */
224 case (MAN_HEAD):
225 /* FALLTHROUGH */
226 case (MAN_TAIL):
227 /* FALLTHROUGH */
228 case (MAN_BODY):
229 p = man_macronames[n->tok];
230 break;
231 case (MAN_ROOT):
232 p = "root";
233 break;
234 case (MAN_TBL):
235 /* FALLTHROUGH */
236 case (MAN_EQN):
237 break;
238 default:
239 abort();
240 /* NOTREACHED */
241 }
242
243 if (n->span) {
244 assert(NULL == p && NULL == t);
245 print_span(n->span, indent);
246 } else if (n->eqn) {
247 assert(NULL == p && NULL == t);
248 print_box(n->eqn->root, indent);
249 } else {
250 for (i = 0; i < indent; i++)
251 putchar('\t');
252 printf("%s (%s) %d:%d\n", p, t, n->line, n->pos);
253 }
254
255 if (n->child)
256 print_man(n->child, indent + 1);
257 if (n->next)
258 print_man(n->next, indent);
259 }
260
261 static void
262 print_box(const struct eqn_box *ep, int indent)
263 {
264 int i;
265
266 if (NULL == ep)
267 return;
268 for (i = 0; i < indent; i++)
269 putchar('\t');
270
271 switch (ep->type) {
272 case (EQN_ROOT):
273 printf("eqn-root(%d, %d, %d)\n",
274 ep->pos, ep->font, ep->mark);
275 print_box(ep->child, indent + 1);
276 break;
277 case (EQN_SUBEXPR):
278 printf("eqn-subxpr(%d, %d, %d)\n",
279 ep->pos, ep->font, ep->mark);
280 print_box(ep->child, indent + 1);
281 break;
282 case (EQN_TEXT):
283 printf("eqn-text(%d, %d, %d): [%s]\n",
284 ep->pos, ep->font, ep->mark, ep->text);
285 break;
286 default:
287 break;
288 }
289
290 print_box(ep->next, indent);
291 }
292
293 static void
294 print_span(const struct tbl_span *sp, int indent)
295 {
296 const struct tbl_dat *dp;
297 int i;
298
299 for (i = 0; i < indent; i++)
300 putchar('\t');
301
302 switch (sp->pos) {
303 case (TBL_SPAN_HORIZ):
304 putchar('-');
305 return;
306 case (TBL_SPAN_DHORIZ):
307 putchar('=');
308 return;
309 default:
310 break;
311 }
312
313 for (dp = sp->first; dp; dp = dp->next) {
314 switch (dp->pos) {
315 case (TBL_DATA_HORIZ):
316 /* FALLTHROUGH */
317 case (TBL_DATA_NHORIZ):
318 putchar('-');
319 continue;
320 case (TBL_DATA_DHORIZ):
321 /* FALLTHROUGH */
322 case (TBL_DATA_NDHORIZ):
323 putchar('=');
324 continue;
325 default:
326 break;
327 }
328 printf("[\"%s\"", dp->string ? dp->string : "");
329 if (dp->spans)
330 printf("(%d)", dp->spans);
331 if (NULL == dp->layout)
332 putchar('*');
333 putchar(']');
334 putchar(' ');
335 }
336
337 printf("(tbl) %d:1\n", sp->line);
338 }