]> git.cameronkatri.com Git - mandoc.git/blob - tree.c
POSIX -> POSIX.1 change (submitted by uqs@sporlein.net, ok jmc@openbsd.org, ingo...
[mandoc.git] / tree.c
1 /* $Id: tree.c,v 1.13 2009/06/10 20:18: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
22 #include "mdoc.h"
23 #include "man.h"
24
25 static void print_mdoc(const struct mdoc_node *, int);
26 static void print_man(const struct man_node *, int);
27
28
29 /* ARGSUSED */
30 int
31 tree_mdoc(void *arg, const struct mdoc *mdoc)
32 {
33
34 print_mdoc(mdoc_node(mdoc), 0);
35 return(1);
36 }
37
38
39 /* ARGSUSED */
40 int
41 tree_man(void *arg, const struct man *man)
42 {
43
44 print_man(man_node(man), 0);
45 return(1);
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 default:
167 abort();
168 /* NOTREACHED */
169 }
170
171 switch (n->type) {
172 case (MAN_TEXT):
173 p = n->string;
174 break;
175 case (MAN_ELEM):
176 p = man_macronames[n->tok];
177 break;
178 case (MAN_ROOT):
179 p = "root";
180 break;
181 default:
182 abort();
183 /* NOTREACHED */
184 }
185
186 for (i = 0; i < indent; i++)
187 (void)printf(" ");
188 (void)printf("%s (%s) %d:%d\n", p, t, n->line, n->pos);
189
190 if (n->child)
191 print_man(n->child, indent + 1);
192 if (n->next)
193 print_man(n->next, indent);
194 }