]> git.cameronkatri.com Git - mandoc.git/blob - mdoctree.c
Adding mdoclint.1 manual.
[mandoc.git] / mdoctree.c
1 /* $Id: mdoctree.c,v 1.4 2009/02/23 12:45:19 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 const struct mdoc *mdoc;
36
37 p = mmain_alloc();
38
39 if ( ! mmain_getopt(p, argc, argv, NULL, NULL, NULL, NULL))
40 mmain_exit(p, 1);
41
42 if (NULL == (mdoc = mmain_mdoc(p)))
43 mmain_exit(p, 1);
44
45 doprint(mdoc_node(mdoc), 0);
46 mmain_exit(p, 0);
47 /* NOTREACHED */
48 }
49
50
51 static void
52 doprint(const struct mdoc_node *n, int indent)
53 {
54 const char *p, *t;
55 int i, j;
56 size_t argc, sz;
57 char **params;
58 struct mdoc_arg *argv;
59
60 argv = NULL;
61 argc = sz = 0;
62 params = NULL;
63
64 switch (n->type) {
65 case (MDOC_ROOT):
66 t = "root";
67 break;
68 case (MDOC_BLOCK):
69 t = "block";
70 break;
71 case (MDOC_HEAD):
72 t = "block-head";
73 break;
74 case (MDOC_BODY):
75 t = "block-body";
76 break;
77 case (MDOC_TAIL):
78 t = "block-tail";
79 break;
80 case (MDOC_ELEM):
81 t = "elem";
82 break;
83 case (MDOC_TEXT):
84 t = "text";
85 break;
86 default:
87 abort();
88 /* NOTREACHED */
89 }
90
91 switch (n->type) {
92 case (MDOC_TEXT):
93 p = n->data.text.string;
94 break;
95 case (MDOC_BODY):
96 p = mdoc_macronames[n->tok];
97 break;
98 case (MDOC_HEAD):
99 p = mdoc_macronames[n->tok];
100 break;
101 case (MDOC_TAIL):
102 p = mdoc_macronames[n->tok];
103 break;
104 case (MDOC_ELEM):
105 p = mdoc_macronames[n->tok];
106 argv = n->data.elem.argv;
107 argc = n->data.elem.argc;
108 break;
109 case (MDOC_BLOCK):
110 p = mdoc_macronames[n->tok];
111 argv = n->data.block.argv;
112 argc = n->data.block.argc;
113 break;
114 case (MDOC_ROOT):
115 p = "root";
116 break;
117 default:
118 abort();
119 /* NOTREACHED */
120 }
121
122 for (i = 0; i < indent; i++)
123 xprintf(" ");
124 xprintf("%s (%s)", p, t);
125
126 for (i = 0; i < (int)argc; i++) {
127 xprintf(" -%s", mdoc_argnames[argv[i].arg]);
128 if (argv[i].sz > 0)
129 xprintf(" [");
130 for (j = 0; j < (int)argv[i].sz; j++)
131 xprintf(" [%s]", argv[i].value[j]);
132 if (argv[i].sz > 0)
133 xprintf(" ]");
134 }
135
136 for (i = 0; i < (int)sz; i++)
137 xprintf(" [%s]", params[i]);
138
139 xprintf(" %d:%d\n", n->line, n->pos);
140
141 if (n->child)
142 doprint(n->child, indent + 1);
143 if (n->next)
144 doprint(n->next, indent);
145 }