1 /* $Id: tree.c,v 1.47 2011/09/18 14:14:15 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2011 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 <limits.h>
23 #include <stdio.h>
41 {
42
43 print_mdoc(mdoc_node(mdoc), 0);
44 }
45
46
47 /* ARGSUSED */
48 void
49 tree_man(void *arg, const struct man *man)
50 {
51
52 print_man(man_node(man), 0);
53 }
54
55
56 static void
57 print_mdoc(const struct mdoc_node *n, int indent)
58 {
59 const char *p, *t;
60 int i, j;
61 size_t argc, sz;
62 char **params;
63 struct mdoc_argv *argv;
64
65 argv = NULL;
66 argc = sz = 0;
67 params = NULL;
68 t = p = NULL;
69
70 switch (n->type) {
71 case (MDOC_ROOT):
72 t = "root";
73 break;
74 case (MDOC_BLOCK):
75 t = "block";
76 break;
77 case (MDOC_HEAD):
78 t = "block-head";
79 break;
80 case (MDOC_BODY):
81 if (n->end)
82 t = "body-end";
83 else
84 t = "block-body";
85 break;
86 case (MDOC_TAIL):
87 t = "block-tail";
145 print_span(n->span, indent);
146 } else if (n->eqn) {
147 assert(NULL == p && NULL == t);
148 print_box(n->eqn->root, indent);
149 } else {
150 for (i = 0; i < indent; i++)
151 putchar('\t');
152
153 printf("%s (%s)", p, t);
154
155 for (i = 0; i < (int)argc; i++) {
156 printf(" -%s", mdoc_argnames[argv[i].arg]);
157 if (argv[i].sz > 0)
158 printf(" [");
159 for (j = 0; j < (int)argv[i].sz; j++)
160 printf(" [%s]", argv[i].value[j]);
161 if (argv[i].sz > 0)
162 printf(" ]");
163 }
164
165 for (i = 0; i < (int)sz; i++)
166 printf(" [%s]", params[i]);
167
168 printf(" %d:%d\n", n->line, n->pos);
169 }
170
171 if (n->child)
172 print_mdoc(n->child, indent + 1);
173 if (n->next)
174 print_mdoc(n->next, indent);
175 }
176
177
178 static void
179 print_man(const struct man_node *n, int indent)
180 {
181 const char *p, *t;
182 int i;
183
184 t = p = NULL;
185
186 switch (n->type) {
187 case (MAN_ROOT):
188 t = "root";
|
1 /* $Id: tree.c,v 1.50 2013/12/24 19:11:46 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2013 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 AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <assert.h>
23 #include <limits.h>
24 #include <stdio.h>
42 {
43
44 print_mdoc(mdoc_node(mdoc), 0);
45 }
46
47
48 /* ARGSUSED */
49 void
50 tree_man(void *arg, const struct man *man)
51 {
52
53 print_man(man_node(man), 0);
54 }
55
56
57 static void
58 print_mdoc(const struct mdoc_node *n, int indent)
59 {
60 const char *p, *t;
61 int i, j;
62 size_t argc;
63 struct mdoc_argv *argv;
64
65 argv = NULL;
66 argc = 0;
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";
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 putchar(' ');
165 if (MDOC_LINE & n->flags)
166 putchar('*');
167 printf("%d:%d", n->line, n->pos);
168 if (n->lastline != n->line)
169 printf("-%d", n->lastline);
170 putchar('\n');
171 }
172
173 if (n->child)
174 print_mdoc(n->child, indent + 1);
175 if (n->next)
176 print_mdoc(n->next, indent);
177 }
178
179
180 static void
181 print_man(const struct man_node *n, int indent)
182 {
183 const char *p, *t;
184 int i;
185
186 t = p = NULL;
187
188 switch (n->type) {
189 case (MAN_ROOT):
190 t = "root";
|