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