]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
Lint fixes.
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.17 2009/10/26 17:05:44 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 #include <assert.h>
18 #include <err.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <time.h>
22
23 #include "mdoc.h"
24 #include "man.h"
25 #include "main.h"
26
27 static void print_mdoc(const struct mdoc_node *, int);
28 static void print_man(const struct man_node *, int);
29
30
31 /* ARGSUSED */
32 void
33 tree_mdoc(void *arg, const struct mdoc *mdoc)
34 {
35
36 print_mdoc(mdoc_node(mdoc), 0);
37 }
38
39
40 /* ARGSUSED */
41 void
42 tree_man(void *arg, const struct man *man)
43 {
44
45 print_man(man_node(man), 0);
46 }
47
48
49 static void
50 print_mdoc(const struct mdoc_node *n, int indent)
51 {
52 const char *p, *t;
53 int i, j;
54 size_t argc, sz;
55 char **params;
56 struct mdoc_argv *argv;
57
58 argv = NULL;
59 argc = sz = 0;
60 params = NULL;
61
62 switch (n->type) {
63 case (MDOC_ROOT):
64 t = "root";
65 break;
66 case (MDOC_BLOCK):
67 t = "block";
68 break;
69 case (MDOC_HEAD):
70 t = "block-head";
71 break;
72 case (MDOC_BODY):
73 t = "block-body";
74 break;
75 case (MDOC_TAIL):
76 t = "block-tail";
77 break;
78 case (MDOC_ELEM):
79 t = "elem";
80 break;
81 case (MDOC_TEXT):
82 t = "text";
83 break;
84 default:
85 abort();
86 /* NOTREACHED */
87 }
88
89 switch (n->type) {
90 case (MDOC_TEXT):
91 p = n->string;
92 break;
93 case (MDOC_BODY):
94 p = mdoc_macronames[n->tok];
95 break;
96 case (MDOC_HEAD):
97 p = mdoc_macronames[n->tok];
98 break;
99 case (MDOC_TAIL):
100 p = mdoc_macronames[n->tok];
101 break;
102 case (MDOC_ELEM):
103 p = mdoc_macronames[n->tok];
104 if (n->args) {
105 argv = n->args->argv;
106 argc = n->args->argc;
107 }
108 break;
109 case (MDOC_BLOCK):
110 p = mdoc_macronames[n->tok];
111 if (n->args) {
112 argv = n->args->argv;
113 argc = n->args->argc;
114 }
115 break;
116 case (MDOC_ROOT):
117 p = "root";
118 break;
119 default:
120 abort();
121 /* NOTREACHED */
122 }
123
124 for (i = 0; i < indent; i++)
125 (void)printf(" ");
126 (void)printf("%s (%s)", p, t);
127
128 for (i = 0; i < (int)argc; i++) {
129 (void)printf(" -%s", mdoc_argnames[argv[i].arg]);
130 if (argv[i].sz > 0)
131 (void)printf(" [");
132 for (j = 0; j < (int)argv[i].sz; j++)
133 (void)printf(" [%s]", argv[i].value[j]);
134 if (argv[i].sz > 0)
135 (void)printf(" ]");
136 }
137
138 for (i = 0; i < (int)sz; i++)
139 (void)printf(" [%s]", params[i]);
140
141 (void)printf(" %d:%d\n", n->line, n->pos);
142
143 if (n->child)
144 print_mdoc(n->child, indent + 1);
145 if (n->next)
146 print_mdoc(n->next, indent);
147 }
148
149
150 static void
151 print_man(const struct man_node *n, int indent)
152 {
153 const char *p, *t;
154 int i;
155
156 switch (n->type) {
157 case (MAN_ROOT):
158 t = "root";
159 break;
160 case (MAN_ELEM):
161 t = "elem";
162 break;
163 case (MAN_TEXT):
164 t = "text";
165 break;
166 case (MAN_BLOCK):
167 t = "block";
168 break;
169 case (MAN_HEAD):
170 t = "block-head";
171 break;
172 case (MAN_BODY):
173 t = "block-body";
174 break;
175 default:
176 abort();
177 /* NOTREACHED */
178 }
179
180 switch (n->type) {
181 case (MAN_TEXT):
182 p = n->string;
183 break;
184 case (MAN_ELEM):
185 /* FALLTHROUGH */
186 case (MAN_BLOCK):
187 /* FALLTHROUGH */
188 case (MAN_HEAD):
189 /* FALLTHROUGH */
190 case (MAN_BODY):
191 p = man_macronames[n->tok];
192 break;
193 case (MAN_ROOT):
194 p = "root";
195 break;
196 default:
197 abort();
198 /* NOTREACHED */
199 }
200
201 for (i = 0; i < indent; i++)
202 (void)printf(" ");
203 (void)printf("%s (%s) %d:%d\n", p, t, n->line, n->pos);
204
205 if (n->child)
206 print_man(n->child, indent + 1);
207 if (n->next)
208 print_man(n->next, indent);
209 }