More correct validation.
[mandoc.git] / mdoc.h
1 /* $Id: mdoc.h,v 1.27 2009/01/19 17:51:33 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 /* What follows is a list of ALL possible macros. */
23
24 #define MDOC___ 0
25 #define MDOC_Dd 1
26 #define MDOC_Dt 2
27 #define MDOC_Os 3
28 #define MDOC_Sh 4
29 #define MDOC_Ss 5
30 #define MDOC_Pp 6
31 #define MDOC_D1 7
32 #define MDOC_Dl 8
33 #define MDOC_Bd 9
34 #define MDOC_Ed 10
35 #define MDOC_Bl 11
36 #define MDOC_El 12
37 #define MDOC_It 13
38 #define MDOC_Ad 14
39 #define MDOC_An 15
40 #define MDOC_Ar 16
41 #define MDOC_Cd 17
42 #define MDOC_Cm 18
43 #define MDOC_Dv 19
44 #define MDOC_Er 20
45 #define MDOC_Ev 21
46 #define MDOC_Ex 22
47 #define MDOC_Fa 23
48 #define MDOC_Fd 24
49 #define MDOC_Fl 25
50 #define MDOC_Fn 26
51 #define MDOC_Ft 27
52 #define MDOC_Ic 28
53 #define MDOC_In 29
54 #define MDOC_Li 30
55 #define MDOC_Nd 31
56 #define MDOC_Nm 32
57 #define MDOC_Op 33
58 #define MDOC_Ot 34
59 #define MDOC_Pa 35
60 #define MDOC_Rv 36
61 #define MDOC_St 37
62 #define MDOC_Va 38
63 #define MDOC_Vt 39
64 #define MDOC_Xr 40
65 #define MDOC__A 41
66 #define MDOC__B 42
67 #define MDOC__D 43
68 #define MDOC__I 44
69 #define MDOC__J 45
70 #define MDOC__N 46
71 #define MDOC__O 47
72 #define MDOC__P 48
73 #define MDOC__R 49
74 #define MDOC__T 50
75 #define MDOC__V 51
76 #define MDOC_Ac 52
77 #define MDOC_Ao 53
78 #define MDOC_Aq 54
79 #define MDOC_At 55
80 #define MDOC_Bc 56
81 #define MDOC_Bf 57
82 #define MDOC_Bo 58
83 #define MDOC_Bq 59
84 #define MDOC_Bsx 60
85 #define MDOC_Bx 61
86 #define MDOC_Db 62
87 #define MDOC_Dc 63
88 #define MDOC_Do 64
89 #define MDOC_Dq 65
90 #define MDOC_Ec 66
91 #define MDOC_Ef 67
92 #define MDOC_Em 68
93 #define MDOC_Eo 69
94 #define MDOC_Fx 70
95 #define MDOC_Ms 71
96 #define MDOC_No 72
97 #define MDOC_Ns 73
98 #define MDOC_Nx 74
99 #define MDOC_Ox 75
100 #define MDOC_Pc 76
101 #define MDOC_Pf 77
102 #define MDOC_Po 78
103 #define MDOC_Pq 79
104 #define MDOC_Qc 80
105 #define MDOC_Ql 81
106 #define MDOC_Qo 82
107 #define MDOC_Qq 83
108 #define MDOC_Re 84
109 #define MDOC_Rs 85
110 #define MDOC_Sc 86
111 #define MDOC_So 87
112 #define MDOC_Sq 88
113 #define MDOC_Sm 89
114 #define MDOC_Sx 90
115 #define MDOC_Sy 91
116 #define MDOC_Tn 92
117 #define MDOC_Ux 93
118 #define MDOC_Xc 94
119 #define MDOC_Xo 95
120 #define MDOC_Fo 96
121 #define MDOC_Fc 97
122 #define MDOC_Oo 98
123 #define MDOC_Oc 99
124 #define MDOC_Bk 100
125 #define MDOC_Ek 101
126 #define MDOC_Bt 102
127 #define MDOC_Hf 103
128 #define MDOC_Fr 104
129 #define MDOC_Ud 105
130 #define MDOC_MAX 106
131
132 /* What follows is a list of ALL possible macro arguments. */
133
134 #define MDOC_Split 0
135 #define MDOC_Nosplit 1
136 #define MDOC_Ragged 2
137 #define MDOC_Unfilled 3
138 #define MDOC_Literal 4
139 #define MDOC_File 5
140 #define MDOC_Offset 6
141 #define MDOC_Bullet 7
142 #define MDOC_Dash 8
143 #define MDOC_Hyphen 9
144 #define MDOC_Item 10
145 #define MDOC_Enum 11
146 #define MDOC_Tag 12
147 #define MDOC_Diag 13
148 #define MDOC_Hang 14
149 #define MDOC_Ohang 15
150 #define MDOC_Inset 16
151 #define MDOC_Column 17
152 #define MDOC_Width 18
153 #define MDOC_Compact 19
154 #define MDOC_Std 20
155 #define MDOC_p1003_1_88 21
156 #define MDOC_p1003_1_90 22
157 #define MDOC_p1003_1_96 23
158 #define MDOC_p1003_1_2001 24
159 #define MDOC_p1003_1_2004 25
160 #define MDOC_p1003_1 26
161 #define MDOC_p1003_1b 27
162 #define MDOC_p1003_1b_93 28
163 #define MDOC_p1003_1c_95 29
164 #define MDOC_p1003_1g_2000 30
165 #define MDOC_p1003_2_92 31
166 #define MDOC_p1387_2_95 32
167 #define MDOC_p1003_2 33
168 #define MDOC_p1387_2 34
169 #define MDOC_isoC_90 35
170 #define MDOC_isoC_amd1 36
171 #define MDOC_isoC_tcor1 37
172 #define MDOC_isoC_tcor2 38
173 #define MDOC_isoC_99 39
174 #define MDOC_ansiC 40
175 #define MDOC_ansiC_89 41
176 #define MDOC_ansiC_99 42
177 #define MDOC_ieee754 43
178 #define MDOC_iso8802_3 44
179 #define MDOC_xpg3 45
180 #define MDOC_xpg4 46
181 #define MDOC_xpg4_2 47
182 #define MDOC_xpg4_3 48
183 #define MDOC_xbd5 49
184 #define MDOC_xcu5 50
185 #define MDOC_xsh5 51
186 #define MDOC_xns5 52
187 #define MDOC_xns5_2d2_0 53
188 #define MDOC_xcurses4_2 54
189 #define MDOC_susv2 55
190 #define MDOC_susv3 56
191 #define MDOC_svid4 57
192 #define MDOC_Filled 58
193 #define MDOC_Words 59
194 #define MDOC_Emphasis 60
195 #define MDOC_Symbolic 61
196 #define MDOC_ARG_MAX 62
197
198 enum mdoc_warn {
199 WARN_SYNTAX, /* Syntax warn (at line/col). */
200 WARN_COMPAT /* Groff compat warn (at line/col). */
201 };
202
203 /* Possible values for the `At' macro. */
204 enum mdoc_att {
205 ATT_DEFAULT = 0,
206 ATT_v1,
207 ATT_v2,
208 ATT_v3,
209 ATT_v4,
210 ATT_v5,
211 ATT_v6,
212 ATT_v7,
213 ATT_32v,
214 ATT_V1,
215 ATT_V2,
216 ATT_V3,
217 ATT_V4
218 };
219
220 /* An argument to a macro (multiple values = `It -column'). */
221 struct mdoc_arg {
222 int arg;
223 int line;
224 int pos;
225 size_t sz;
226 char **value;
227 };
228
229 /*
230 * Simplified grammar of syntax tree:
231 *
232 * MDOC_ROOT: root of tree
233 * MDOC_TEXT: free-form text
234 * MDOC_ELEM: elem [args] MDOC_TEXT...
235 * MDOC_BLOCK, MDOC_HEAD, MDOC_BODY, MDOC_TAIL:
236 * MDOC_BLOCK:
237 * MDOC_HEAD [args] (MDOC_TEXT|MDOC_ELEM|MDOC_BLOCK)...
238 * MDOC_BODY (MDOC_TEXT|MDOC_ELEM|MDOC_BLOCK)...
239 * MDOC_TAIL (optional) (MDOC_TEXT|MDOC_ELEM|MDOC_BLOCK)...
240 */
241
242 /* Type of a syntax node. */
243 enum mdoc_type {
244 MDOC_TEXT,
245 MDOC_ELEM,
246 MDOC_HEAD,
247 MDOC_TAIL,
248 MDOC_BODY,
249 MDOC_BLOCK,
250 MDOC_ROOT
251 };
252
253 /* Manual section. */
254 enum mdoc_msec {
255 MSEC_DEFAULT = 0,
256 MSEC_1,
257 MSEC_2,
258 MSEC_3,
259 MSEC_3f,
260 MSEC_3p,
261 MSEC_4,
262 MSEC_5,
263 MSEC_6,
264 MSEC_7,
265 MSEC_8,
266 MSEC_9,
267 MSEC_X11,
268 MSEC_X11R6,
269 MSEC_local,
270 MSEC_n,
271 MSEC_unass,
272 MSEC_draft,
273 MSEC_paper
274 };
275
276 /* Section (named/unnamed) of `Ss'. */
277 enum mdoc_sec {
278 SEC_PROLOGUE = 0,
279 SEC_BODY,
280 SEC_NAME,
281 SEC_SYNOPSIS,
282 SEC_DESCRIPTION,
283 SEC_RETURN_VALUES,
284 SEC_ENVIRONMENT,
285 SEC_FILES,
286 SEC_EXAMPLES,
287 SEC_DIAGNOSTICS,
288 SEC_ERRORS,
289 SEC_SEE_ALSO,
290 SEC_STANDARDS,
291 SEC_HISTORY,
292 SEC_AUTHORS,
293 SEC_CAVEATS,
294 SEC_BUGS,
295 SEC_CUSTOM
296 };
297
298 /* Volume of `Dt'. */
299 enum mdoc_vol {
300 VOL_DEFAULT = 0,
301 VOL_AMD,
302 VOL_IND,
303 VOL_KM,
304 VOL_LOCAL,
305 VOL_PRM,
306 VOL_PS1,
307 VOL_SMM,
308 VOL_URM,
309 VOL_USD
310 };
311
312 /* Architecture of `Dt'. */
313 enum mdoc_arch {
314 ARCH_DEFAULT = 0,
315 ARCH_alpha,
316 ARCH_amd64,
317 ARCH_amiga,
318 ARCH_arc,
319 ARCH_arm,
320 ARCH_armish,
321 ARCH_aviion,
322 ARCH_hp300,
323 ARCH_hppa,
324 ARCH_hppa64,
325 ARCH_i386,
326 ARCH_landisk,
327 ARCH_luna88k,
328 ARCH_mac68k,
329 ARCH_macppc,
330 ARCH_mvme68k,
331 ARCH_mvme88k,
332 ARCH_mvmeppc,
333 ARCH_pmax,
334 ARCH_sgi,
335 ARCH_socppc,
336 ARCH_sparc,
337 ARCH_sparc64,
338 ARCH_sun3,
339 ARCH_vax,
340 ARCH_zaurus
341 };
342
343 /* Meta-information from prologue. */
344 struct mdoc_meta {
345 enum mdoc_msec msec;
346 enum mdoc_vol vol;
347 enum mdoc_arch arch;
348 time_t date;
349 char *title;
350 char *os;
351 char *name;
352 };
353
354 struct mdoc_text {
355 char *string;
356 };
357
358 struct mdoc_block {
359 size_t argc;
360 struct mdoc_arg *argv;
361 struct mdoc_node *head;
362 struct mdoc_node *body;
363 struct mdoc_node *tail;
364 };
365
366 struct mdoc_elem {
367 size_t sz;
368 char **args;
369 size_t argc;
370 struct mdoc_arg *argv;
371 };
372
373 union mdoc_data {
374 struct mdoc_text text;
375 struct mdoc_elem elem;
376 struct mdoc_block block;
377 };
378
379 /* Syntax node in parse tree. */
380 struct mdoc_node {
381 struct mdoc_node *parent;
382 struct mdoc_node *child;
383 struct mdoc_node *next;
384 struct mdoc_node *prev;
385 int line;
386 int pos;
387 int tok;
388 int flags;
389 #define MDOC_VALID (1 << 0)
390 #define MDOC_ACTED (1 << 1)
391 enum mdoc_type type;
392 union mdoc_data data;
393 };
394
395 /* Call-backs for parse messages. */
396 struct mdoc_cb {
397 void (*mdoc_msg)(void *, int, int, const char *);
398 int (*mdoc_err)(void *, int, int, const char *);
399 int (*mdoc_warn)(void *, int, int,
400 enum mdoc_warn, const char *);
401 };
402
403 extern const char *const *mdoc_macronames;
404 extern const char *const *mdoc_argnames;
405
406 __BEGIN_DECLS
407
408 struct mdoc;
409
410 /* Free memory allocated with mdoc_alloc. */
411 void mdoc_free(struct mdoc *);
412
413 /* Allocate a new parser instance. */
414 struct mdoc *mdoc_alloc(void *data, const struct mdoc_cb *);
415
416 /* Parse a single line (boolean retval). */
417 int mdoc_parseln(struct mdoc *, int, char *buf);
418
419 /* Get result first node. */
420 const struct mdoc_node *mdoc_node(struct mdoc *);
421
422 /* Get result meta-information. */
423 const struct mdoc_meta *mdoc_meta(struct mdoc *);
424
425 /* Signal end of parse sequence (boolean retval). */
426 int mdoc_endparse(struct mdoc *);
427
428 __END_DECLS
429
430 #endif /*!MDOC_H*/