]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.90 2009/07/12 20:50:08 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 const char *const __mdoc_merrnames
[MERRMAX
] = {
27 "trailing whitespace", /* ETAILWS */
28 "empty last list column", /* ECOLEMPTY */
29 "argument-like parameter", /* EARGVPARM */
30 "unexpected quoted parameter", /* EQUOTPARM */
31 "unterminated quoted parameter", /* EQUOTTERM */
32 "system: malloc error", /* EMALLOC */
33 "argument parameter suggested", /* EARGVAL */
34 "macro not callable", /* ENOCALL */
35 "macro disallowed in prologue", /* EBODYPROL */
36 "macro disallowed in body", /* EPROLBODY */
37 "text disallowed in prologue", /* ETEXTPROL */
38 "blank line disallowed", /* ENOBLANK */
39 "text parameter too long", /* ETOOLONG */
40 "invalid escape sequence", /* EESCAPE */
41 "invalid character", /* EPRINT */
42 "document has no body", /* ENODAT */
43 "document has no prologue", /* ENOPROLOGUE */
44 "expected line arguments", /* ELINE */
45 "invalid AT&T argument", /* EATT */
46 "default name not yet set", /* ENAME */
47 "missing list type", /* ELISTTYPE */
48 "missing display type", /* EDISPTYPE */
49 "too many display types", /* EMULTIDISP */
50 "too many list types", /* EMULTILIST */
51 "NAME section must be first", /* ESECNAME */
52 "badly-formed NAME section", /* ENAMESECINC */
53 "argument repeated", /* EARGREP */
54 "expected boolean parameter", /* EBOOL */
55 "inconsistent column syntax", /* ECOLMIS */
56 "nested display invalid", /* ENESTDISP */
57 "width argument missing", /* EMISSWIDTH */
58 "invalid section for this manual section", /* EWRONGMSEC */
59 "section out of conventional order", /* ESECOOO */
60 "section repeated", /* ESECREP */
61 "invalid standard argument", /* EBADSTAND */
62 "multi-line arguments discouraged", /* ENOMULTILINE */
63 "multi-line arguments suggested", /* EMULTILINE */
64 "line arguments discouraged", /* ENOLINE */
65 "prologue macro out of conventional order", /* EPROLOOO */
66 "prologue macro repeated", /* EPROLREP */
67 "invalid manual section", /* EBADMSEC */
68 "invalid section", /* EBADSEC */
69 "invalid font mode", /* EFONT */
70 "invalid date syntax", /* EBADDATE */
71 "invalid number format", /* ENUMFMT */
72 "superfluous width argument", /* ENOWIDTH */
73 "system: utsname error", /* EUTSNAME */
74 "obsolete macro", /* EOBS */
75 "macro-like parameter", /* EMACPARM */
76 "end-of-line scope violation", /* EIMPBRK */
77 "empty macro ignored", /* EIGNE */
78 "unclosed explicit scope", /* EOPEN */
79 "unterminated quoted phrase", /* EQUOTPHR */
80 "closure macro without prior context", /* ENOCTX */
81 "invalid whitespace after control character", /* ESPACE */
82 "no description found for library" /* ELIB */
85 const char *const __mdoc_macronames
[MDOC_MAX
] = {
86 "Ap", "Dd", "Dt", "Os",
87 "Sh", "Ss", "Pp", "D1",
88 "Dl", "Bd", "Ed", "Bl",
89 "El", "It", "Ad", "An",
90 "Ar", "Cd", "Cm", "Dv",
91 "Er", "Ev", "Ex", "Fa",
92 "Fd", "Fl", "Fn", "Ft",
93 "Ic", "In", "Li", "Nd",
94 "Nm", "Op", "Ot", "Pa",
95 "Rv", "St", "Va", "Vt",
97 "Xr", "\%A", "\%B", "\%D",
99 "\%I", "\%J", "\%N", "\%O",
101 "\%P", "\%R", "\%T", "\%V",
102 "Ac", "Ao", "Aq", "At",
103 "Bc", "Bf", "Bo", "Bq",
104 "Bsx", "Bx", "Db", "Dc",
105 "Do", "Dq", "Ec", "Ef",
106 "Em", "Eo", "Fx", "Ms",
107 "No", "Ns", "Nx", "Ox",
108 "Pc", "Pf", "Po", "Pq",
109 "Qc", "Ql", "Qo", "Qq",
110 "Re", "Rs", "Sc", "So",
111 "Sq", "Sm", "Sx", "Sy",
112 "Tn", "Ux", "Xc", "Xo",
113 "Fo", "Fc", "Oo", "Oc",
114 "Bk", "Ek", "Bt", "Hf",
115 "Fr", "Ud", "Lb", "Lp",
116 "Lk", "Mt", "Brq", "Bro",
118 "Brc", "\%C", "Es", "En",
123 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
124 "split", "nosplit", "ragged",
125 "unfilled", "literal", "file",
126 "offset", "bullet", "dash",
127 "hyphen", "item", "enum",
128 "tag", "diag", "hang",
129 "ohang", "inset", "column",
130 "width", "compact", "std",
131 "filled", "words", "emphasis",
135 const char * const *mdoc_macronames
= __mdoc_macronames
;
136 const char * const *mdoc_argnames
= __mdoc_argnames
;
138 static void mdoc_free1(struct mdoc
*);
139 static int mdoc_alloc1(struct mdoc
*);
140 static struct mdoc_node
*node_alloc(struct mdoc
*, int, int,
141 int, enum mdoc_type
);
142 static int node_append(struct mdoc
*,
144 static int parsetext(struct mdoc
*, int, char *);
145 static int parsemacro(struct mdoc
*, int, char *);
146 static int macrowarn(struct mdoc
*, int, const char *);
149 const struct mdoc_node
*
150 mdoc_node(const struct mdoc
*m
)
153 return(MDOC_HALT
& m
->flags
? NULL
: m
->first
);
157 const struct mdoc_meta
*
158 mdoc_meta(const struct mdoc
*m
)
161 return(MDOC_HALT
& m
->flags
? NULL
: &m
->meta
);
166 * Frees volatile resources (parse tree, meta-data, fields).
169 mdoc_free1(struct mdoc
*mdoc
)
173 mdoc_node_freelist(mdoc
->first
);
174 if (mdoc
->meta
.title
)
175 free(mdoc
->meta
.title
);
179 free(mdoc
->meta
.name
);
181 free(mdoc
->meta
.arch
);
183 free(mdoc
->meta
.vol
);
188 * Allocate all volatile resources (parse tree, meta-data, fields).
191 mdoc_alloc1(struct mdoc
*mdoc
)
194 bzero(&mdoc
->meta
, sizeof(struct mdoc_meta
));
196 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
197 mdoc
->last
= calloc(1, sizeof(struct mdoc_node
));
198 if (NULL
== mdoc
->last
)
201 mdoc
->first
= mdoc
->last
;
202 mdoc
->last
->type
= MDOC_ROOT
;
203 mdoc
->next
= MDOC_NEXT_CHILD
;
209 * Free up volatile resources (see mdoc_free1()) then re-initialises the
210 * data with mdoc_alloc1(). After invocation, parse data has been reset
211 * and the parser is ready for re-invocation on a new tree; however,
212 * cross-parse non-volatile data is kept intact.
215 mdoc_reset(struct mdoc
*mdoc
)
219 return(mdoc_alloc1(mdoc
));
224 * Completely free up all volatile and non-volatile parse resources.
225 * After invocation, the pointer is no longer usable.
228 mdoc_free(struct mdoc
*mdoc
)
233 mdoc_hash_free(mdoc
->htab
);
239 * Allocate volatile and non-volatile parse resources.
242 mdoc_alloc(void *data
, int pflags
, const struct mdoc_cb
*cb
)
246 if (NULL
== (p
= calloc(1, sizeof(struct mdoc
))))
249 (void)memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
254 if (NULL
== (p
->htab
= mdoc_hash_alloc())) {
257 } else if (mdoc_alloc1(p
))
266 * Climb back up the parse tree, validating open scopes. Mostly calls
267 * through to macro_end() in macro.c.
270 mdoc_endparse(struct mdoc
*m
)
273 if (MDOC_HALT
& m
->flags
)
275 else if (mdoc_macroend(m
))
277 m
->flags
|= MDOC_HALT
;
283 * Main parse routine. Parses a single line -- really just hands off to
284 * the macro (parsemacro()) or text parser (parsetext()).
287 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
)
290 if (MDOC_HALT
& m
->flags
)
293 return('.' == *buf
? parsemacro(m
, ln
, buf
) :
294 parsetext(m
, ln
, buf
));
299 mdoc_verr(struct mdoc
*mdoc
, int ln
, int pos
,
300 const char *fmt
, ...)
305 if (NULL
== mdoc
->cb
.mdoc_err
)
309 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
312 return((*mdoc
->cb
.mdoc_err
)(mdoc
->data
, ln
, pos
, buf
));
317 mdoc_vwarn(struct mdoc
*mdoc
, int ln
, int pos
, const char *fmt
, ...)
322 if (NULL
== mdoc
->cb
.mdoc_warn
)
326 (void)vsnprintf(buf
, sizeof(buf
) - 1, fmt
, ap
);
329 return((*mdoc
->cb
.mdoc_warn
)(mdoc
->data
, ln
, pos
, buf
));
334 mdoc_err(struct mdoc
*m
, int line
, int pos
, int iserr
, enum merr type
)
338 p
= __mdoc_merrnames
[(int)type
];
342 return(mdoc_verr(m
, line
, pos
, p
));
344 return(mdoc_vwarn(m
, line
, pos
, p
));
349 mdoc_macro(struct mdoc
*m
, int tok
,
350 int ln
, int pp
, int *pos
, char *buf
)
353 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
354 MDOC_PBODY
& m
->flags
)
355 return(mdoc_perr(m
, ln
, pp
, EPROLBODY
));
356 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
357 ! (MDOC_PBODY
& m
->flags
))
358 return(mdoc_perr(m
, ln
, pp
, EBODYPROL
));
360 if (1 != pp
&& ! (MDOC_CALLABLE
& mdoc_macros
[tok
].flags
))
361 return(mdoc_perr(m
, ln
, pp
, ENOCALL
));
363 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
368 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
373 assert(MDOC_ROOT
!= p
->type
);
375 switch (mdoc
->next
) {
376 case (MDOC_NEXT_SIBLING
):
377 mdoc
->last
->next
= p
;
378 p
->prev
= mdoc
->last
;
379 p
->parent
= mdoc
->last
->parent
;
381 case (MDOC_NEXT_CHILD
):
382 mdoc
->last
->child
= p
;
383 p
->parent
= mdoc
->last
;
392 if ( ! mdoc_valid_pre(mdoc
, p
))
394 if ( ! mdoc_action_pre(mdoc
, p
))
399 assert(MDOC_BLOCK
== p
->parent
->type
);
403 assert(MDOC_BLOCK
== p
->parent
->type
);
407 assert(MDOC_BLOCK
== p
->parent
->type
);
418 if ( ! mdoc_valid_post(mdoc
))
420 if ( ! mdoc_action_post(mdoc
))
431 static struct mdoc_node
*
432 node_alloc(struct mdoc
*mdoc
, int line
,
433 int pos
, int tok
, enum mdoc_type type
)
437 if (NULL
== (p
= calloc(1, sizeof(struct mdoc_node
)))) {
438 (void)mdoc_nerr(mdoc
, mdoc
->last
, EMALLOC
);
442 p
->sec
= mdoc
->lastsec
;
446 if (MDOC_TEXT
!= (p
->type
= type
))
454 mdoc_tail_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
458 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_TAIL
);
461 return(node_append(mdoc
, p
));
466 mdoc_head_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
473 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_HEAD
);
476 return(node_append(mdoc
, p
));
481 mdoc_body_alloc(struct mdoc
*mdoc
, int line
, int pos
, int tok
)
485 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BODY
);
488 return(node_append(mdoc
, p
));
493 mdoc_block_alloc(struct mdoc
*mdoc
, int line
, int pos
,
494 int tok
, struct mdoc_arg
*args
)
498 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_BLOCK
);
504 return(node_append(mdoc
, p
));
509 mdoc_elem_alloc(struct mdoc
*mdoc
, int line
, int pos
,
510 int tok
, struct mdoc_arg
*args
)
514 p
= node_alloc(mdoc
, line
, pos
, tok
, MDOC_ELEM
);
520 return(node_append(mdoc
, p
));
525 mdoc_word_alloc(struct mdoc
*mdoc
,
526 int line
, int pos
, const char *word
)
530 p
= node_alloc(mdoc
, line
, pos
, -1, MDOC_TEXT
);
533 if (NULL
== (p
->string
= strdup(word
))) {
534 (void)mdoc_nerr(mdoc
, mdoc
->last
, EMALLOC
);
538 return(node_append(mdoc
, p
));
543 mdoc_node_free(struct mdoc_node
*p
)
551 mdoc_argv_free(p
->args
);
557 mdoc_node_freelist(struct mdoc_node
*p
)
561 mdoc_node_freelist(p
->child
);
563 mdoc_node_freelist(p
->next
);
565 assert(0 == p
->nchild
);
571 * Parse free-form text, that is, a line that does not begin with the
575 parsetext(struct mdoc
*m
, int line
, char *buf
)
578 if (SEC_NONE
== m
->lastnamed
)
579 return(mdoc_perr(m
, line
, 0, ETEXTPROL
));
581 if (0 == buf
[0] && ! (MDOC_LITERAL
& m
->flags
))
582 return(mdoc_perr(m
, line
, 0, ENOBLANK
));
584 if ( ! mdoc_word_alloc(m
, line
, 0, buf
))
587 m
->next
= MDOC_NEXT_SIBLING
;
593 macrowarn(struct mdoc
*m
, int ln
, const char *buf
)
595 if ( ! (MDOC_IGN_MACRO
& m
->pflags
))
596 return(mdoc_verr(m
, ln
, 1,
597 "unknown macro: %s%s",
598 buf
, strlen(buf
) > 3 ? "..." : ""));
599 return(mdoc_vwarn(m
, ln
, 1, "unknown macro: %s%s",
600 buf
, strlen(buf
) > 3 ? "..." : ""));
605 * Parse a macro line, that is, a line beginning with the control
609 parsemacro(struct mdoc
*m
, int ln
, char *buf
)
614 /* Empty lines are ignored. */
621 while (buf
[i
] && ' ' == buf
[i
])
625 return(mdoc_perr(m
, ln
, 1, ESPACE
));
628 /* Copy the first word into a nil-terminated buffer. */
630 for (i
= 1; i
< 5; i
++) {
631 if (0 == (mac
[i
- 1] = buf
[i
]))
633 else if (' ' == buf
[i
])
639 if (i
== 5 || i
<= 2) {
640 if ( ! macrowarn(m
, ln
, mac
))
645 if (MDOC_MAX
== (c
= mdoc_hash_find(m
->htab
, mac
))) {
646 if ( ! macrowarn(m
, ln
, mac
))
651 /* The macro is sane. Jump to the next word. */
653 while (buf
[i
] && ' ' == buf
[i
])
656 /* Begin recursive parse sequence. */
658 if ( ! mdoc_macro(m
, c
, ln
, 1, &i
, buf
))
663 err
: /* Error out. */
665 m
->flags
|= MDOC_HALT
;