1 /* $Id: mdoc.c,v 1.188 2011/03/28 23:52:13 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <sys/types.h>
34 #include "libmandoc.h"
36 const char *const __mdoc_macronames[MDOC_MAX] = {
37 "Ap", "Dd", "Dt", "Os",
38 "Sh", "Ss", "Pp", "D1",
39 "Dl", "Bd", "Ed", "Bl",
40 "El", "It", "Ad", "An",
41 "Ar", "Cd", "Cm", "Dv",
42 "Er", "Ev", "Ex", "Fa",
43 "Fd", "Fl", "Fn", "Ft",
44 "Ic", "In", "Li", "Nd",
45 "Nm", "Op", "Ot", "Pa",
46 "Rv", "St", "Va", "Vt",
48 "Xr", "%A", "%B", "%D",
50 "%I", "%J", "%N", "%O",
52 "%P", "%R", "%T", "%V",
53 "Ac", "Ao", "Aq", "At",
54 "Bc", "Bf", "Bo", "Bq",
55 "Bsx", "Bx", "Db", "Dc",
56 "Do", "Dq", "Ec", "Ef",
57 "Em", "Eo", "Fx", "Ms",
58 "No", "Ns", "Nx", "Ox",
59 "Pc", "Pf", "Po", "Pq",
60 "Qc", "Ql", "Qo", "Qq",
61 "Re", "Rs", "Sc", "So",
62 "Sq", "Sm", "Sx", "Sy",
63 "Tn", "Ux", "Xc", "Xo",
64 "Fo", "Fc", "Oo", "Oc",
65 "Bk", "Ek", "Bt", "Hf",
66 "Fr", "Ud", "Lb", "Lp",
67 "Lk", "Mt", "Brq", "Bro",
69 "Brc", "%C", "Es", "En",
71 "Dx", "%Q", "br", "sp",
76 const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
77 "split", "nosplit", "ragged",
78 "unfilled", "literal", "file",
79 "offset", "bullet", "dash",
80 "hyphen", "item", "enum",
81 "tag", "diag", "hang",
82 "ohang", "inset", "column",
83 "width", "compact", "std",
84 "filled", "words", "emphasis",
85 "symbolic", "nested", "centered"
88 const char * const *mdoc_macronames = __mdoc_macronames;
89 const char * const *mdoc_argnames = __mdoc_argnames;
91 static void mdoc_node_free(struct mdoc_node *);
92 static void mdoc_node_unlink(struct mdoc *,
94 static void mdoc_free1(struct mdoc *);
95 static void mdoc_alloc1(struct mdoc *);
96 static struct mdoc_node *node_alloc(struct mdoc *, int, int,
97 enum mdoct, enum mdoc_type);
98 static int node_append(struct mdoc *,
100 static int mdoc_ptext(struct mdoc *, int, char *, int);
101 static int mdoc_pmacro(struct mdoc *, int, char *, int);
103 const struct mdoc_node *
104 mdoc_node(const struct mdoc *m)
107 assert( ! (MDOC_HALT & m->flags));
112 const struct mdoc_meta *
113 mdoc_meta(const struct mdoc *m)
116 assert( ! (MDOC_HALT & m->flags));
122 * Frees volatile resources (parse tree, meta-data, fields).
125 mdoc_free1(struct mdoc *mdoc)
129 mdoc_node_delete(mdoc, mdoc->first);
130 if (mdoc->meta.title)
131 free(mdoc->meta.title);
135 free(mdoc->meta.name);
137 free(mdoc->meta.arch);
139 free(mdoc->meta.vol);
141 free(mdoc->meta.msec);
143 free(mdoc->meta.date);
148 * Allocate all volatile resources (parse tree, meta-data, fields).
151 mdoc_alloc1(struct mdoc *mdoc)
154 memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
156 mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
157 mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
158 mdoc->first = mdoc->last;
159 mdoc->last->type = MDOC_ROOT;
160 mdoc->next = MDOC_NEXT_CHILD;
165 * Free up volatile resources (see mdoc_free1()) then re-initialises the
166 * data with mdoc_alloc1(). After invocation, parse data has been reset
167 * and the parser is ready for re-invocation on a new tree; however,
168 * cross-parse non-volatile data is kept intact.
171 mdoc_reset(struct mdoc *mdoc)
180 * Completely free up all volatile and non-volatile parse resources.
181 * After invocation, the pointer is no longer usable.
184 mdoc_free(struct mdoc *mdoc)
193 * Allocate volatile and non-volatile parse resources.
196 mdoc_alloc(struct regset *regs, struct mparse *parse)
200 p = mandoc_calloc(1, sizeof(struct mdoc));
212 * Climb back up the parse tree, validating open scopes. Mostly calls
213 * through to macro_end() in macro.c.
216 mdoc_endparse(struct mdoc *m)
219 assert( ! (MDOC_HALT & m->flags));
220 if (mdoc_macroend(m))
222 m->flags |= MDOC_HALT;
227 mdoc_addeqn(struct mdoc *m, const struct eqn *ep)
231 assert( ! (MDOC_HALT & m->flags));
233 /* No text before an initial macro. */
235 if (SEC_NONE == m->lastnamed) {
236 mdoc_pmsg(m, ep->line, ep->pos, MANDOCERR_NOTEXT);
240 n = node_alloc(m, ep->line, ep->pos, MDOC_MAX, MDOC_EQN);
243 if ( ! node_append(m, n))
246 m->next = MDOC_NEXT_SIBLING;
251 mdoc_addspan(struct mdoc *m, const struct tbl_span *sp)
255 assert( ! (MDOC_HALT & m->flags));
257 /* No text before an initial macro. */
259 if (SEC_NONE == m->lastnamed) {
260 mdoc_pmsg(m, sp->line, 0, MANDOCERR_NOTEXT);
264 n = node_alloc(m, sp->line, 0, MDOC_MAX, MDOC_TBL);
267 if ( ! node_append(m, n))
270 m->next = MDOC_NEXT_SIBLING;
276 * Main parse routine. Parses a single line -- really just hands off to
277 * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
280 mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
283 assert( ! (MDOC_HALT & m->flags));
285 m->flags |= MDOC_NEWLINE;
288 * Let the roff nS register switch SYNOPSIS mode early,
289 * such that the parser knows at all times
290 * whether this mode is on or off.
291 * Note that this mode is also switched by the Sh macro.
293 if (m->regs->regs[(int)REG_nS].set) {
294 if (m->regs->regs[(int)REG_nS].v.u)
295 m->flags |= MDOC_SYNOPSIS;
297 m->flags &= ~MDOC_SYNOPSIS;
300 return(mandoc_getcontrol(buf, &offs) ?
301 mdoc_pmacro(m, ln, buf, offs) :
302 mdoc_ptext(m, ln, buf, offs));
306 mdoc_macro(MACRO_PROT_ARGS)
308 assert(tok < MDOC_MAX);
310 /* If we're in the body, deny prologue calls. */
312 if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
313 MDOC_PBODY & m->flags) {
314 mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY);
318 /* If we're in the prologue, deny "body" macros. */
320 if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
321 ! (MDOC_PBODY & m->flags)) {
322 mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG);
323 if (NULL == m->meta.msec)
324 m->meta.msec = mandoc_strdup("1");
325 if (NULL == m->meta.title)
326 m->meta.title = mandoc_strdup("UNKNOWN");
327 if (NULL == m->meta.vol)
328 m->meta.vol = mandoc_strdup("LOCAL");
329 if (NULL == m->meta.os)
330 m->meta.os = mandoc_strdup("LOCAL");
331 if (NULL == m->meta.date)
332 m->meta.date = mandoc_normdate
333 (m->parse, NULL, line, ppos);
334 m->flags |= MDOC_PBODY;
337 return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
342 node_append(struct mdoc *mdoc, struct mdoc_node *p)
347 assert(MDOC_ROOT != p->type);
349 switch (mdoc->next) {
350 case (MDOC_NEXT_SIBLING):
351 mdoc->last->next = p;
352 p->prev = mdoc->last;
353 p->parent = mdoc->last->parent;
355 case (MDOC_NEXT_CHILD):
356 mdoc->last->child = p;
357 p->parent = mdoc->last;
367 * Copy over the normalised-data pointer of our parent. Not
368 * everybody has one, but copying a null pointer is fine.
377 p->norm = p->parent->norm;
383 if ( ! mdoc_valid_pre(mdoc, p))
388 assert(MDOC_BLOCK == p->parent->type);
392 assert(MDOC_BLOCK == p->parent->type);
398 assert(MDOC_BLOCK == p->parent->type);
411 if ( ! mdoc_valid_post(mdoc))
422 static struct mdoc_node *
423 node_alloc(struct mdoc *m, int line, int pos,
424 enum mdoct tok, enum mdoc_type type)
428 p = mandoc_calloc(1, sizeof(struct mdoc_node));
437 if (MDOC_SYNOPSIS & m->flags)
438 p->flags |= MDOC_SYNPRETTY;
440 p->flags &= ~MDOC_SYNPRETTY;
441 if (MDOC_NEWLINE & m->flags)
442 p->flags |= MDOC_LINE;
443 m->flags &= ~MDOC_NEWLINE;
450 mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
454 p = node_alloc(m, line, pos, tok, MDOC_TAIL);
455 if ( ! node_append(m, p))
457 m->next = MDOC_NEXT_CHILD;
463 mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
470 p = node_alloc(m, line, pos, tok, MDOC_HEAD);
471 if ( ! node_append(m, p))
473 m->next = MDOC_NEXT_CHILD;
479 mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
483 p = node_alloc(m, line, pos, tok, MDOC_BODY);
484 if ( ! node_append(m, p))
486 m->next = MDOC_NEXT_CHILD;
492 mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
493 struct mdoc_node *body, enum mdoc_endbody end)
497 p = node_alloc(m, line, pos, tok, MDOC_BODY);
500 if ( ! node_append(m, p))
502 m->next = MDOC_NEXT_SIBLING;
508 mdoc_block_alloc(struct mdoc *m, int line, int pos,
509 enum mdoct tok, struct mdoc_arg *args)
513 p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
526 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
532 if ( ! node_append(m, p))
534 m->next = MDOC_NEXT_CHILD;
540 mdoc_elem_alloc(struct mdoc *m, int line, int pos,
541 enum mdoct tok, struct mdoc_arg *args)
545 p = node_alloc(m, line, pos, tok, MDOC_ELEM);
552 p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
558 if ( ! node_append(m, p))
560 m->next = MDOC_NEXT_CHILD;
565 mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
572 n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
573 n->string = mandoc_malloc(len + 1);
574 sv = strlcpy(n->string, p, len + 1);
576 /* Prohibit truncation. */
577 assert(sv < len + 1);
579 if ( ! node_append(m, n))
582 m->next = MDOC_NEXT_SIBLING;
588 mdoc_node_free(struct mdoc_node *p)
591 if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
596 mdoc_argv_free(p->args);
602 mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
605 /* Adjust siblings. */
608 n->prev->next = n->next;
610 n->next->prev = n->prev;
616 if (n->parent->child == n)
617 n->parent->child = n->prev ? n->prev : n->next;
618 if (n->parent->last == n)
619 n->parent->last = n->prev ? n->prev : NULL;
622 /* Adjust parse point, if applicable. */
624 if (m && m->last == n) {
627 m->next = MDOC_NEXT_SIBLING;
630 m->next = MDOC_NEXT_CHILD;
634 if (m && m->first == n)
640 mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
645 mdoc_node_delete(m, p->child);
647 assert(0 == p->nchild);
649 mdoc_node_unlink(m, p);
655 * Parse free-form text, that is, a line that does not begin with the
659 mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
664 /* No text before an initial macro. */
666 if (SEC_NONE == m->lastnamed) {
667 mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT);
675 * Divert directly to list processing if we're encountering a
676 * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
677 * (a MDOC_BODY means it's already open, in which case we should
678 * process within its context in the normal way).
681 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
682 LIST_column == n->norm->Bl.type) {
683 /* `Bl' is open without any children. */
684 m->flags |= MDOC_FREECOL;
685 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
688 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
690 MDOC_Bl == n->parent->tok &&
691 LIST_column == n->parent->norm->Bl.type) {
692 /* `Bl' has block-level `It' children. */
693 m->flags |= MDOC_FREECOL;
694 return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
698 * Search for the beginning of unescaped trailing whitespace (ws)
699 * and for the first character not to be output (end).
702 /* FIXME: replace with strcspn(). */
704 for (c = end = buf + offs; *c; c++) {
707 if (mandoc_hyph(buf + offs, c))
717 * Always warn about trailing tabs,
718 * even outside literal context,
719 * where they should be put on the next line.
724 * Strip trailing tabs in literal context only;
725 * outside, they affect the next line.
727 if (MDOC_LITERAL & m->flags)
731 /* Skip the escaped character, too, if any. */
744 mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE);
746 if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
747 mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN);
750 * Insert a `sp' in the case of a blank line. Technically,
751 * blank lines aren't allowed, but enough manuals assume this
752 * behaviour that we want to work around it.
754 if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
757 m->next = MDOC_NEXT_SIBLING;
761 if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
764 if (MDOC_LITERAL & m->flags)
768 * End-of-sentence check. If the last character is an unescaped
769 * EOS character, then flag the node as being the end of a
770 * sentence. The front-end will know how to interpret this.
775 if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
776 m->last->flags |= MDOC_EOS;
783 * Parse a macro line, that is, a line beginning with the control
787 mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
794 /* Empty post-control lines are ignored. */
796 if ('"' == buf[offs]) {
797 mdoc_pmsg(m, ln, offs, MANDOCERR_BADCOMMENT);
799 } else if ('\0' == buf[offs])
805 * Copy the first word into a nil-terminated buffer.
806 * Stop copying when a tab, space, or eoln is encountered.
810 while (i < 4 && '\0' != buf[offs] &&
811 ' ' != buf[offs] && '\t' != buf[offs])
812 mac[i++] = buf[offs++];
816 tok = (i > 1 || i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
818 if (MDOC_MAX == tok) {
819 mandoc_vmsg(MANDOCERR_MACRO, m->parse,
820 ln, sv, "%s", buf + sv - 1);
824 /* Disregard the first trailing tab, if applicable. */
826 if ('\t' == buf[offs])
829 /* Jump to the next non-whitespace word. */
831 while (buf[offs] && ' ' == buf[offs])
835 * Trailing whitespace. Note that tabs are allowed to be passed
836 * into the parser as "text", so we only warn about spaces here.
839 if ('\0' == buf[offs] && ' ' == buf[offs - 1])
840 mdoc_pmsg(m, ln, offs - 1, MANDOCERR_EOLNSPACE);
843 * If an initial macro or a list invocation, divert directly
844 * into macro processing.
847 if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
848 if ( ! mdoc_macro(m, tok, ln, sv, &offs, buf))
857 * If the first macro of a `Bl -column', open an `It' block
858 * context around the parsed macro.
861 if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
862 LIST_column == n->norm->Bl.type) {
863 m->flags |= MDOC_FREECOL;
864 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
870 * If we're following a block-level `It' within a `Bl -column'
871 * context (perhaps opened in the above block or in ptext()),
872 * then open an `It' block context around the parsed macro.
875 if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
877 MDOC_Bl == n->parent->tok &&
878 LIST_column == n->parent->norm->Bl.type) {
879 m->flags |= MDOC_FREECOL;
880 if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
885 /* Normal processing of a macro. */
887 if ( ! mdoc_macro(m, tok, ln, sv, &offs, buf))
892 err: /* Error out. */
894 m->flags |= MDOC_HALT;
899 mdoc_isdelim(const char *p)
912 return(DELIM_MIDDLE);
936 if (0 == strcmp(p + 1, "."))
938 if (0 == strcmp(p + 1, "*(Ba"))
939 return(DELIM_MIDDLE);