]> git.cameronkatri.com Git - mandoc.git/blob - mdoctree.c
Bumped minor-number.
[mandoc.git] / mdoctree.c
1 /* $Id: mdoctree.c,v 1.6 2009/03/08 14:01:46 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_argv *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->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 if (n->args) {
109 argv = n->args->argv;
110 argc = n->args->argc;
111 }
112 break;
113 case (MDOC_BLOCK):
114 p = mdoc_macronames[n->tok];
115 if (n->args) {
116 argv = n->args->argv;
117 argc = n->args->argc;
118 }
119 break;
120 case (MDOC_ROOT):
121 p = "root";
122 break;
123 default:
124 abort();
125 /* NOTREACHED */
126 }
127
128 for (i = 0; i < indent; i++)
129 xprintf(" ");
130 xprintf("%s (%s)", p, t);
131
132 for (i = 0; i < (int)argc; i++) {
133 xprintf(" -%s", mdoc_argnames[argv[i].arg]);
134 if (argv[i].sz > 0)
135 xprintf(" [");
136 for (j = 0; j < (int)argv[i].sz; j++)
137 xprintf(" [%s]", argv[i].value[j]);
138 if (argv[i].sz > 0)
139 xprintf(" ]");
140 }
141
142 for (i = 0; i < (int)sz; i++)
143 xprintf(" [%s]", params[i]);
144
145 xprintf(" %d:%d\n", n->line, n->pos);
146
147 if (n->child)
148 doprint(n->child, indent + 1);
149 if (n->next)
150 doprint(n->next, indent);
151 }