]> git.cameronkatri.com Git - mandoc.git/blob - mdoctree.c
Memory-corruption fix.
[mandoc.git] / mdoctree.c
1 /* $Id: mdoctree.c,v 1.5 2009/03/06 14:13:47 kristaps Exp $ */
2 /*
3 * Copyright (c) 2008 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
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 <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include "mmain.h"
26
27 #define xprintf (void)printf
28
29 static void doprint(const struct mdoc_node *, int);
30
31 int
32 main(int argc, char *argv[])
33 {
34 struct mmain *p;
35 int c;
36 const struct mdoc *mdoc;
37
38 p = mmain_alloc();
39
40 c = mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL);
41 if (1 != c)
42 mmain_exit(p, -1 == c ? 1 : 0);
43
44 if (NULL == (mdoc = mmain_mdoc(p)))
45 mmain_exit(p, 1);
46
47 doprint(mdoc_node(mdoc), 0);
48 mmain_exit(p, 0);
49 /* NOTREACHED */
50 }
51
52
53 static void
54 doprint(const struct mdoc_node *n, int indent)
55 {
56 const char *p, *t;
57 int i, j;
58 size_t argc, sz;
59 char **params;
60 struct mdoc_arg *argv;
61
62 argv = NULL;
63 argc = sz = 0;
64 params = NULL;
65
66 switch (n->type) {
67 case (MDOC_ROOT):
68 t = "root";
69 break;
70 case (MDOC_BLOCK):
71 t = "block";
72 break;
73 case (MDOC_HEAD):
74 t = "block-head";
75 break;
76 case (MDOC_BODY):
77 t = "block-body";
78 break;
79 case (MDOC_TAIL):
80 t = "block-tail";
81 break;
82 case (MDOC_ELEM):
83 t = "elem";
84 break;
85 case (MDOC_TEXT):
86 t = "text";
87 break;
88 default:
89 abort();
90 /* NOTREACHED */
91 }
92
93 switch (n->type) {
94 case (MDOC_TEXT):
95 p = n->data.text.string;
96 break;
97 case (MDOC_BODY):
98 p = mdoc_macronames[n->tok];
99 break;
100 case (MDOC_HEAD):
101 p = mdoc_macronames[n->tok];
102 break;
103 case (MDOC_TAIL):
104 p = mdoc_macronames[n->tok];
105 break;
106 case (MDOC_ELEM):
107 p = mdoc_macronames[n->tok];
108 argv = n->data.elem.argv;
109 argc = n->data.elem.argc;
110 break;
111 case (MDOC_BLOCK):
112 p = mdoc_macronames[n->tok];
113 argv = n->data.block.argv;
114 argc = n->data.block.argc;
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 xprintf(" ");
126 xprintf("%s (%s)", p, t);
127
128 for (i = 0; i < (int)argc; i++) {
129 xprintf(" -%s", mdoc_argnames[argv[i].arg]);
130 if (argv[i].sz > 0)
131 xprintf(" [");
132 for (j = 0; j < (int)argv[i].sz; j++)
133 xprintf(" [%s]", argv[i].value[j]);
134 if (argv[i].sz > 0)
135 xprintf(" ]");
136 }
137
138 for (i = 0; i < (int)sz; i++)
139 xprintf(" [%s]", params[i]);
140
141 xprintf(" %d:%d\n", n->line, n->pos);
142
143 if (n->child)
144 doprint(n->child, indent + 1);
145 if (n->next)
146 doprint(n->next, indent);
147 }