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