]> git.cameronkatri.com Git - mandoc.git/blob - mdoc.h
7c06697315442d8821d76cc090d77d929532ad46
[mandoc.git] / mdoc.h
1 /* $Id: mdoc.h,v 1.37 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 #ifndef MDOC_H
20 #define MDOC_H
21
22 /*
23 * This library implements a validating scanner/parser for ``mdoc'' roff
24 * macro documents, a.k.a. BSD manual page documents. The mdoc.c file
25 * drives the parser, while macro.c describes the macro ontologies.
26 * validate.c pre- and post-validates parsed macros, and action.c
27 * performs actions on parsed and validated macros.
28 */
29
30 /* What follows is a list of ALL possible macros. */
31
32 #define MDOC___ 0
33 #define MDOC_Dd 1
34 #define MDOC_Dt 2
35 #define MDOC_Os 3
36 #define MDOC_Sh 4
37 #define MDOC_Ss 5
38 #define MDOC_Pp 6
39 #define MDOC_D1 7
40 #define MDOC_Dl 8
41 #define MDOC_Bd 9
42 #define MDOC_Ed 10
43 #define MDOC_Bl 11
44 #define MDOC_El 12
45 #define MDOC_It 13
46 #define MDOC_Ad 14
47 #define MDOC_An 15
48 #define MDOC_Ar 16
49 #define MDOC_Cd 17
50 #define MDOC_Cm 18
51 #define MDOC_Dv 19
52 #define MDOC_Er 20
53 #define MDOC_Ev 21
54 #define MDOC_Ex 22
55 #define MDOC_Fa 23
56 #define MDOC_Fd 24
57 #define MDOC_Fl 25
58 #define MDOC_Fn 26
59 #define MDOC_Ft 27
60 #define MDOC_Ic 28
61 #define MDOC_In 29
62 #define MDOC_Li 30
63 #define MDOC_Nd 31
64 #define MDOC_Nm 32
65 #define MDOC_Op 33
66 #define MDOC_Ot 34
67 #define MDOC_Pa 35
68 #define MDOC_Rv 36
69 #define MDOC_St 37
70 #define MDOC_Va 38
71 #define MDOC_Vt 39
72 #define MDOC_Xr 40
73 #define MDOC__A 41
74 #define MDOC__B 42
75 #define MDOC__D 43
76 #define MDOC__I 44
77 #define MDOC__J 45
78 #define MDOC__N 46
79 #define MDOC__O 47
80 #define MDOC__P 48
81 #define MDOC__R 49
82 #define MDOC__T 50
83 #define MDOC__V 51
84 #define MDOC_Ac 52
85 #define MDOC_Ao 53
86 #define MDOC_Aq 54
87 #define MDOC_At 55
88 #define MDOC_Bc 56
89 #define MDOC_Bf 57
90 #define MDOC_Bo 58
91 #define MDOC_Bq 59
92 #define MDOC_Bsx 60
93 #define MDOC_Bx 61
94 #define MDOC_Db 62
95 #define MDOC_Dc 63
96 #define MDOC_Do 64
97 #define MDOC_Dq 65
98 #define MDOC_Ec 66
99 #define MDOC_Ef 67
100 #define MDOC_Em 68
101 #define MDOC_Eo 69
102 #define MDOC_Fx 70
103 #define MDOC_Ms 71
104 #define MDOC_No 72
105 #define MDOC_Ns 73
106 #define MDOC_Nx 74
107 #define MDOC_Ox 75
108 #define MDOC_Pc 76
109 #define MDOC_Pf 77
110 #define MDOC_Po 78
111 #define MDOC_Pq 79
112 #define MDOC_Qc 80
113 #define MDOC_Ql 81
114 #define MDOC_Qo 82
115 #define MDOC_Qq 83
116 #define MDOC_Re 84
117 #define MDOC_Rs 85
118 #define MDOC_Sc 86
119 #define MDOC_So 87
120 #define MDOC_Sq 88
121 #define MDOC_Sm 89
122 #define MDOC_Sx 90
123 #define MDOC_Sy 91
124 #define MDOC_Tn 92
125 #define MDOC_Ux 93
126 #define MDOC_Xc 94
127 #define MDOC_Xo 95
128 #define MDOC_Fo 96
129 #define MDOC_Fc 97
130 #define MDOC_Oo 98
131 #define MDOC_Oc 99
132 #define MDOC_Bk 100
133 #define MDOC_Ek 101
134 #define MDOC_Bt 102
135 #define MDOC_Hf 103
136 #define MDOC_Fr 104
137 #define MDOC_Ud 105
138 #define MDOC_Lb 106
139 #define MDOC_MAX 107
140
141 /* What follows is a list of ALL possible macro arguments. */
142
143 #define MDOC_Split 0
144 #define MDOC_Nosplit 1
145 #define MDOC_Ragged 2
146 #define MDOC_Unfilled 3
147 #define MDOC_Literal 4
148 #define MDOC_File 5
149 #define MDOC_Offset 6
150 #define MDOC_Bullet 7
151 #define MDOC_Dash 8
152 #define MDOC_Hyphen 9
153 #define MDOC_Item 10
154 #define MDOC_Enum 11
155 #define MDOC_Tag 12
156 #define MDOC_Diag 13
157 #define MDOC_Hang 14
158 #define MDOC_Ohang 15
159 #define MDOC_Inset 16
160 #define MDOC_Column 17
161 #define MDOC_Width 18
162 #define MDOC_Compact 19
163 #define MDOC_Std 20
164 #define MDOC_Filled 21
165 #define MDOC_Words 22
166 #define MDOC_Emphasis 23
167 #define MDOC_Symbolic 24
168 #define MDOC_ARG_MAX 25
169
170 /* Warnings are either syntax or groff-compatibility. */
171 enum mdoc_warn {
172 WARN_SYNTAX,
173 WARN_COMPAT
174 };
175
176 /* An argument to a macro (multiple values = `It -column'). */
177 struct mdoc_arg {
178 int arg;
179 int line;
180 int pos;
181 size_t sz;
182 char **value;
183 };
184
185 /* Type of a syntax node. */
186 enum mdoc_type {
187 MDOC_TEXT,
188 MDOC_ELEM,
189 MDOC_HEAD,
190 MDOC_TAIL,
191 MDOC_BODY,
192 MDOC_BLOCK,
193 MDOC_ROOT
194 };
195
196 /* Section (named/unnamed) of `Sh'. */
197 enum mdoc_sec {
198 SEC_PROLOGUE = 0,
199 SEC_BODY = 1,
200 SEC_NAME = 2,
201 SEC_LIBRARY = 3,
202 SEC_SYNOPSIS = 4,
203 SEC_DESCRIPTION = 5,
204 SEC_IMPLEMENTATION = 6,
205 SEC_RETURN_VALUES = 7,
206 SEC_ENVIRONMENT = 8,
207 SEC_FILES = 9,
208 SEC_EXAMPLES = 10,
209 SEC_DIAGNOSTICS = 11,
210 SEC_COMPATIBILITY = 12,
211 SEC_ERRORS = 13,
212 SEC_SEE_ALSO = 14,
213 SEC_STANDARDS = 15,
214 SEC_HISTORY = 16,
215 SEC_AUTHORS = 17,
216 SEC_CAVEATS = 18,
217 SEC_BUGS = 19,
218 SEC_CUSTOM
219 };
220
221 /* Information from prologue. */
222 struct mdoc_meta {
223 int msec;
224 char *vol;
225 char *arch;
226 time_t date;
227 char *title;
228 char *os;
229 char *name;
230 };
231
232 /* Text-only node. */
233 struct mdoc_text {
234 char *string;
235 };
236
237 /* Block (scoped) node. */
238 struct mdoc_block {
239 size_t argc;
240 struct mdoc_arg *argv;
241 struct mdoc_node *head;
242 struct mdoc_node *body;
243 struct mdoc_node *tail;
244 };
245
246 /* In-line element node. */
247 struct mdoc_elem {
248 size_t argc;
249 struct mdoc_arg *argv;
250 };
251
252 /* Typed nodes of an AST node. */
253 union mdoc_data {
254 struct mdoc_text text;
255 struct mdoc_elem elem;
256 struct mdoc_block block;
257 };
258
259 /* Node in AST. */
260 struct mdoc_node {
261 struct mdoc_node *parent;
262 struct mdoc_node *child;
263 struct mdoc_node *next;
264 struct mdoc_node *prev;
265 int line;
266 int pos;
267 int tok;
268 int flags;
269 #define MDOC_VALID (1 << 0)
270 #define MDOC_ACTED (1 << 1)
271 enum mdoc_type type;
272 union mdoc_data data;
273 enum mdoc_sec sec;
274 };
275
276 /* Call-backs for parse messages. */
277 struct mdoc_cb {
278 void (*mdoc_msg)(void *, int, int, const char *);
279 int (*mdoc_err)(void *, int, int, const char *);
280 int (*mdoc_warn)(void *, int, int,
281 enum mdoc_warn, const char *);
282 };
283
284 /* Global table of macro names (`Bd', `Ed', etc.). */
285 extern const char *const *mdoc_macronames;
286
287 /* Global table of argument names (`column', `tag', etc.). */
288 extern const char *const *mdoc_argnames;
289
290 __BEGIN_DECLS
291
292 struct mdoc;
293
294 /* Free memory allocated with mdoc_alloc. */
295 void mdoc_free(struct mdoc *);
296
297 /* Allocate a new parser instance. */
298 struct mdoc *mdoc_alloc(void *data, const struct mdoc_cb *);
299
300 /* Parse a single line in a stream (boolean retval). */
301 int mdoc_parseln(struct mdoc *, int, char *buf);
302
303 /* Get result first node (after mdoc_endparse!). */
304 const struct mdoc_node *mdoc_node(const struct mdoc *);
305
306 /* Get result meta-information (after mdoc_endparse!). */
307 const struct mdoc_meta *mdoc_meta(const struct mdoc *);
308
309 /* Signal end of parse sequence (boolean retval). */
310 int mdoc_endparse(struct mdoc *);
311
312 /* The following are utility functions. */
313
314 const char *mdoc_a2att(const char *);
315 const char *mdoc_a2lib(const char *);
316 const char *mdoc_a2st(const char *);
317
318 int mdoc_isdelim(const char *);
319
320 __END_DECLS
321
322 #endif /*!MDOC_H*/