]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
mdoc_tokhash -> hash
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.10 2009/03/23 15:20:51 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
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
7 * above copyright notice and this permission notice appear in all
8 * copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19 #include <assert.h>
20 #include <err.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #include "mdoc.h"
25 #include "man.h"
26
27 static void tree_mdoc(const struct mdoc_node *, int);
28 static void tree_man(const struct man_node *, int);
29
30
31 /* ARGSUSED */
32 int
33 tree_run(void *arg, const struct man *man,
34 const struct mdoc *mdoc)
35 {
36
37 if (man)
38 tree_man(man_node(man), 0);
39 if (mdoc)
40 tree_mdoc(mdoc_node(mdoc), 0);
41 return(1);
42 }
43
44
45 static void
46 tree_mdoc(const struct mdoc_node *n, int indent)
47 {
48 const char *p, *t;
49 int i, j;
50 size_t argc, sz;
51 char **params;
52 struct mdoc_argv *argv;
53
54 argv = NULL;
55 argc = sz = 0;
56 params = NULL;
57
58 switch (n->type) {
59 case (MDOC_ROOT):
60 t = "root";
61 break;
62 case (MDOC_BLOCK):
63 t = "block";
64 break;
65 case (MDOC_HEAD):
66 t = "block-head";
67 break;
68 case (MDOC_BODY):
69 t = "block-body";
70 break;
71 case (MDOC_TAIL):
72 t = "block-tail";
73 break;
74 case (MDOC_ELEM):
75 t = "elem";
76 break;
77 case (MDOC_TEXT):
78 t = "text";
79 break;
80 default:
81 abort();
82 /* NOTREACHED */
83 }
84
85 switch (n->type) {
86 case (MDOC_TEXT):
87 p = n->string;
88 break;
89 case (MDOC_BODY):
90 p = mdoc_macronames[n->tok];
91 break;
92 case (MDOC_HEAD):
93 p = mdoc_macronames[n->tok];
94 break;
95 case (MDOC_TAIL):
96 p = mdoc_macronames[n->tok];
97 break;
98 case (MDOC_ELEM):
99 p = mdoc_macronames[n->tok];
100 if (n->args) {
101 argv = n->args->argv;
102 argc = n->args->argc;
103 }
104 break;
105 case (MDOC_BLOCK):
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_ROOT):
113 p = "root";
114 break;
115 default:
116 abort();
117 /* NOTREACHED */
118 }
119
120 for (i = 0; i < indent; i++)
121 (void)printf(" ");
122 (void)printf("%s (%s)", p, t);
123
124 for (i = 0; i < (int)argc; i++) {
125 (void)printf(" -%s", mdoc_argnames[argv[i].arg]);
126 if (argv[i].sz > 0)
127 (void)printf(" [");
128 for (j = 0; j < (int)argv[i].sz; j++)
129 (void)printf(" [%s]", argv[i].value[j]);
130 if (argv[i].sz > 0)
131 (void)printf(" ]");
132 }
133
134 for (i = 0; i < (int)sz; i++)
135 (void)printf(" [%s]", params[i]);
136
137 (void)printf(" %d:%d\n", n->line, n->pos);
138
139 if (n->child)
140 tree_mdoc(n->child, indent + 1);
141 if (n->next)
142 tree_mdoc(n->next, indent);
143 }
144
145
146 static void
147 tree_man(const struct man_node *n, int indent)
148 {
149 const char *p, *t;
150 int i;
151
152 switch (n->type) {
153 case (MAN_ROOT):
154 t = "root";
155 break;
156 case (MAN_ELEM):
157 t = "elem";
158 break;
159 case (MAN_TEXT):
160 t = "text";
161 break;
162 default:
163 abort();
164 /* NOTREACHED */
165 }
166
167 switch (n->type) {
168 case (MAN_TEXT):
169 p = n->string;
170 break;
171 case (MAN_ELEM):
172 p = man_macronames[n->tok];
173 break;
174 case (MAN_ROOT):
175 p = "root";
176 break;
177 default:
178 abort();
179 /* NOTREACHED */
180 }
181
182 for (i = 0; i < indent; i++)
183 (void)printf(" ");
184 (void)printf("%s (%s) %d:%d\n", p, t, n->line, n->pos);
185
186 if (n->child)
187 tree_man(n->child, indent + 1);
188 if (n->next)
189 tree_man(n->next, indent);
190 }