]>
git.cameronkatri.com Git - mandoc.git/blob - mdoc.c
1 /* $Id: mdoc.c,v 1.123 2010/04/08 07:53:01 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.
21 #include <sys/types.h>
32 #include "libmandoc.h"
34 const char *const __mdoc_merrnames
[MERRMAX
] = {
35 "trailing whitespace", /* ETAILWS */
36 "unexpected quoted parameter", /* EQUOTPARM */
37 "unterminated quoted parameter", /* EQUOTTERM */
38 "argument parameter suggested", /* EARGVAL */
39 "macro disallowed in prologue", /* EBODYPROL */
40 "macro disallowed in body", /* EPROLBODY */
41 "text disallowed in prologue", /* ETEXTPROL */
42 "blank line disallowed", /* ENOBLANK */
43 "text parameter too long", /* ETOOLONG */
44 "invalid escape sequence", /* EESCAPE */
45 "invalid character", /* EPRINT */
46 "document has no body", /* ENODAT */
47 "document has no prologue", /* ENOPROLOGUE */
48 "expected line arguments", /* ELINE */
49 "invalid AT&T argument", /* EATT */
50 "default name not yet set", /* ENAME */
51 "missing list type", /* ELISTTYPE */
52 "missing display type", /* EDISPTYPE */
53 "too many display types", /* EMULTIDISP */
54 "too many list types", /* EMULTILIST */
55 "NAME section must be first", /* ESECNAME */
56 "badly-formed NAME section", /* ENAMESECINC */
57 "argument repeated", /* EARGREP */
58 "expected boolean parameter", /* EBOOL */
59 "inconsistent column syntax", /* ECOLMIS */
60 "nested display invalid", /* ENESTDISP */
61 "width argument missing", /* EMISSWIDTH */
62 "invalid section for this manual section", /* EWRONGMSEC */
63 "section out of conventional order", /* ESECOOO */
64 "section repeated", /* ESECREP */
65 "invalid standard argument", /* EBADSTAND */
66 "multi-line arguments discouraged", /* ENOMULTILINE */
67 "multi-line arguments suggested", /* EMULTILINE */
68 "line arguments discouraged", /* ENOLINE */
69 "prologue macro out of conventional order", /* EPROLOOO */
70 "prologue macro repeated", /* EPROLREP */
71 "invalid manual section", /* EBADMSEC */
72 "invalid section", /* EBADSEC */
73 "invalid font mode", /* EFONT */
74 "invalid date syntax", /* EBADDATE */
75 "invalid number format", /* ENUMFMT */
76 "superfluous width argument", /* ENOWIDTH */
77 "system: utsname error", /* EUTSNAME */
78 "obsolete macro", /* EOBS */
79 "end-of-line scope violation", /* EIMPBRK */
80 "empty macro ignored", /* EIGNE */
81 "unclosed explicit scope", /* EOPEN */
82 "unterminated quoted phrase", /* EQUOTPHR */
83 "closure macro without prior context", /* ENOCTX */
84 "no description found for library", /* ELIB */
85 "bad child for parent context", /* EBADCHILD */
86 "list arguments preceding type", /* ENOTYPE */
87 "deprecated comment style", /* EBADCOMMENT */
90 const char *const __mdoc_macronames
[MDOC_MAX
] = {
91 "Ap", "Dd", "Dt", "Os",
92 "Sh", "Ss", "Pp", "D1",
93 "Dl", "Bd", "Ed", "Bl",
94 "El", "It", "Ad", "An",
95 "Ar", "Cd", "Cm", "Dv",
96 "Er", "Ev", "Ex", "Fa",
97 "Fd", "Fl", "Fn", "Ft",
98 "Ic", "In", "Li", "Nd",
99 "Nm", "Op", "Ot", "Pa",
100 "Rv", "St", "Va", "Vt",
102 "Xr", "%A", "%B", "%D",
104 "%I", "%J", "%N", "%O",
106 "%P", "%R", "%T", "%V",
107 "Ac", "Ao", "Aq", "At",
108 "Bc", "Bf", "Bo", "Bq",
109 "Bsx", "Bx", "Db", "Dc",
110 "Do", "Dq", "Ec", "Ef",
111 "Em", "Eo", "Fx", "Ms",
112 "No", "Ns", "Nx", "Ox",
113 "Pc", "Pf", "Po", "Pq",
114 "Qc", "Ql", "Qo", "Qq",
115 "Re", "Rs", "Sc", "So",
116 "Sq", "Sm", "Sx", "Sy",
117 "Tn", "Ux", "Xc", "Xo",
118 "Fo", "Fc", "Oo", "Oc",
119 "Bk", "Ek", "Bt", "Hf",
120 "Fr", "Ud", "Lb", "Lp",
121 "Lk", "Mt", "Brq", "Bro",
123 "Brc", "%C", "Es", "En",
125 "Dx", "%Q", "br", "sp",
130 const char *const __mdoc_argnames
[MDOC_ARG_MAX
] = {
131 "split", "nosplit", "ragged",
132 "unfilled", "literal", "file",
133 "offset", "bullet", "dash",
134 "hyphen", "item", "enum",
135 "tag", "diag", "hang",
136 "ohang", "inset", "column",
137 "width", "compact", "std",
138 "filled", "words", "emphasis",
139 "symbolic", "nested", "centered"
142 const char * const *mdoc_macronames
= __mdoc_macronames
;
143 const char * const *mdoc_argnames
= __mdoc_argnames
;
145 static void mdoc_node_free(struct mdoc_node
*);
146 static void mdoc_node_unlink(struct mdoc
*,
148 static void mdoc_free1(struct mdoc
*);
149 static void mdoc_alloc1(struct mdoc
*);
150 static struct mdoc_node
*node_alloc(struct mdoc
*, int, int,
151 enum mdoct
, enum mdoc_type
);
152 static int node_append(struct mdoc
*,
154 static int mdoc_ptext(struct mdoc
*, int, char *);
155 static int mdoc_pmacro(struct mdoc
*, int, char *);
156 static int macrowarn(struct mdoc
*, int, const char *);
157 static int pstring(struct mdoc
*, int, int,
158 const char *, size_t);
160 const struct mdoc_node
*
161 mdoc_node(const struct mdoc
*m
)
164 return(MDOC_HALT
& m
->flags
? NULL
: m
->first
);
168 const struct mdoc_meta
*
169 mdoc_meta(const struct mdoc
*m
)
172 return(MDOC_HALT
& m
->flags
? NULL
: &m
->meta
);
177 * Frees volatile resources (parse tree, meta-data, fields).
180 mdoc_free1(struct mdoc
*mdoc
)
184 mdoc_node_delete(mdoc
, mdoc
->first
);
185 if (mdoc
->meta
.title
)
186 free(mdoc
->meta
.title
);
190 free(mdoc
->meta
.name
);
192 free(mdoc
->meta
.arch
);
194 free(mdoc
->meta
.vol
);
199 * Allocate all volatile resources (parse tree, meta-data, fields).
202 mdoc_alloc1(struct mdoc
*mdoc
)
205 memset(&mdoc
->meta
, 0, sizeof(struct mdoc_meta
));
207 mdoc
->lastnamed
= mdoc
->lastsec
= SEC_NONE
;
208 mdoc
->last
= mandoc_calloc(1, sizeof(struct mdoc_node
));
209 mdoc
->first
= mdoc
->last
;
210 mdoc
->last
->type
= MDOC_ROOT
;
211 mdoc
->next
= MDOC_NEXT_CHILD
;
216 * Free up volatile resources (see mdoc_free1()) then re-initialises the
217 * data with mdoc_alloc1(). After invocation, parse data has been reset
218 * and the parser is ready for re-invocation on a new tree; however,
219 * cross-parse non-volatile data is kept intact.
222 mdoc_reset(struct mdoc
*mdoc
)
231 * Completely free up all volatile and non-volatile parse resources.
232 * After invocation, the pointer is no longer usable.
235 mdoc_free(struct mdoc
*mdoc
)
244 * Allocate volatile and non-volatile parse resources.
247 mdoc_alloc(void *data
, int pflags
, const struct mdoc_cb
*cb
)
251 p
= mandoc_calloc(1, sizeof(struct mdoc
));
254 memcpy(&p
->cb
, cb
, sizeof(struct mdoc_cb
));
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 (mdoc_pmacro()) or text parser (mdoc_ptext()).
287 mdoc_parseln(struct mdoc
*m
, int ln
, char *buf
)
290 if (MDOC_HALT
& m
->flags
)
293 return('.' == *buf
? mdoc_pmacro(m
, ln
, buf
) :
294 mdoc_ptext(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
, enum mdoct tok
,
350 int ln
, int pp
, int *pos
, char *buf
)
352 assert(tok
< MDOC_MAX
);
354 /* If we're in the body, deny prologue calls. */
356 if (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
&&
357 MDOC_PBODY
& m
->flags
)
358 return(mdoc_perr(m
, ln
, pp
, EPROLBODY
));
360 /* If we're in the prologue, deny "body" macros. */
362 if ( ! (MDOC_PROLOGUE
& mdoc_macros
[tok
].flags
) &&
363 ! (MDOC_PBODY
& m
->flags
)) {
364 if ( ! mdoc_pwarn(m
, ln
, pp
, EBODYPROL
))
366 if (NULL
== m
->meta
.title
)
367 m
->meta
.title
= mandoc_strdup("unknown");
368 if (NULL
== m
->meta
.vol
)
369 m
->meta
.vol
= mandoc_strdup("local");
370 if (NULL
== m
->meta
.os
)
371 m
->meta
.os
= mandoc_strdup("local");
372 if (0 == m
->meta
.date
)
373 m
->meta
.date
= time(NULL
);
374 m
->flags
|= MDOC_PBODY
;
377 return((*mdoc_macros
[tok
].fp
)(m
, tok
, ln
, pp
, pos
, buf
));
382 node_append(struct mdoc
*mdoc
, struct mdoc_node
*p
)
387 assert(MDOC_ROOT
!= p
->type
);
389 switch (mdoc
->next
) {
390 case (MDOC_NEXT_SIBLING
):
391 mdoc
->last
->next
= p
;
392 p
->prev
= mdoc
->last
;
393 p
->parent
= mdoc
->last
->parent
;
395 case (MDOC_NEXT_CHILD
):
396 mdoc
->last
->child
= p
;
397 p
->parent
= mdoc
->last
;
406 if ( ! mdoc_valid_pre(mdoc
, p
))
408 if ( ! mdoc_action_pre(mdoc
, p
))
413 assert(MDOC_BLOCK
== p
->parent
->type
);
417 assert(MDOC_BLOCK
== p
->parent
->type
);
421 assert(MDOC_BLOCK
== p
->parent
->type
);
432 if ( ! mdoc_valid_post(mdoc
))
434 if ( ! mdoc_action_post(mdoc
))
445 static struct mdoc_node
*
446 node_alloc(struct mdoc
*m
, int line
, int pos
,
447 enum mdoct tok
, enum mdoc_type type
)
451 p
= mandoc_calloc(1, sizeof(struct mdoc_node
));
463 mdoc_tail_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
467 p
= node_alloc(m
, line
, pos
, tok
, MDOC_TAIL
);
468 if ( ! node_append(m
, p
))
470 m
->next
= MDOC_NEXT_CHILD
;
476 mdoc_head_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
483 p
= node_alloc(m
, line
, pos
, tok
, MDOC_HEAD
);
484 if ( ! node_append(m
, p
))
486 m
->next
= MDOC_NEXT_CHILD
;
492 mdoc_body_alloc(struct mdoc
*m
, int line
, int pos
, enum mdoct tok
)
496 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BODY
);
497 if ( ! node_append(m
, p
))
499 m
->next
= MDOC_NEXT_CHILD
;
505 mdoc_block_alloc(struct mdoc
*m
, int line
, int pos
,
506 enum mdoct tok
, struct mdoc_arg
*args
)
510 p
= node_alloc(m
, line
, pos
, tok
, MDOC_BLOCK
);
514 if ( ! node_append(m
, p
))
516 m
->next
= MDOC_NEXT_CHILD
;
522 mdoc_elem_alloc(struct mdoc
*m
, int line
, int pos
,
523 enum mdoct tok
, struct mdoc_arg
*args
)
527 p
= node_alloc(m
, line
, pos
, tok
, MDOC_ELEM
);
531 if ( ! node_append(m
, p
))
533 m
->next
= MDOC_NEXT_CHILD
;
539 pstring(struct mdoc
*m
, int line
, int pos
, const char *p
, size_t len
)
544 n
= node_alloc(m
, line
, pos
, -1, MDOC_TEXT
);
545 n
->string
= mandoc_malloc(len
+ 1);
546 sv
= strlcpy(n
->string
, p
, len
+ 1);
548 /* Prohibit truncation. */
549 assert(sv
< len
+ 1);
551 if ( ! node_append(m
, n
))
553 m
->next
= MDOC_NEXT_SIBLING
;
559 mdoc_word_alloc(struct mdoc
*m
, int line
, int pos
, const char *p
)
562 return(pstring(m
, line
, pos
, p
, strlen(p
)));
567 mdoc_node_free(struct mdoc_node
*p
)
573 mdoc_argv_free(p
->args
);
579 mdoc_node_unlink(struct mdoc
*m
, struct mdoc_node
*n
)
582 /* Adjust siblings. */
585 n
->prev
->next
= n
->next
;
587 n
->next
->prev
= n
->prev
;
593 if (n
->parent
->child
== n
)
594 n
->parent
->child
= n
->prev
? n
->prev
: n
->next
;
597 /* Adjust parse point, if applicable. */
599 if (m
&& m
->last
== n
) {
602 m
->next
= MDOC_NEXT_SIBLING
;
605 m
->next
= MDOC_NEXT_CHILD
;
609 if (m
&& m
->first
== n
)
615 mdoc_node_delete(struct mdoc
*m
, struct mdoc_node
*p
)
620 mdoc_node_delete(m
, p
->child
);
622 assert(0 == p
->nchild
);
624 mdoc_node_unlink(m
, p
);
630 * Parse free-form text, that is, a line that does not begin with the
634 mdoc_ptext(struct mdoc
*m
, int line
, char *buf
)
639 /* Ignore bogus comments. */
641 if ('\\' == buf
[0] && '.' == buf
[1] && '\"' == buf
[2])
642 return(mdoc_pwarn(m
, line
, 0, EBADCOMMENT
));
644 if (SEC_NONE
== m
->lastnamed
)
645 return(mdoc_perr(m
, line
, 0, ETEXTPROL
));
648 * If in literal mode, then pass the buffer directly to the
649 * back-end, as it should be preserved as a single term.
652 if (MDOC_LITERAL
& m
->flags
)
653 return(mdoc_word_alloc(m
, line
, 0, buf
));
655 /* Disallow blank/white-space lines in non-literal mode. */
657 for (i
= 0; ' ' == buf
[i
]; i
++)
658 /* Skip leading whitespace. */ ;
660 if ('\0' == buf
[i
]) {
661 if ( ! mdoc_pwarn(m
, line
, 0, ENOBLANK
))
664 * Assume that a `Pp' should be inserted in the case of
665 * a blank line. Technically, blank lines aren't
666 * allowed, but enough manuals assume this behaviour
667 * that we want to work around it.
669 if ( ! mdoc_elem_alloc(m
, line
, 0, MDOC_Pp
, NULL
))
674 * Break apart a free-form line into tokens. Spaces are
675 * stripped out of the input.
678 for (j
= i
; buf
[i
]; i
++) {
682 /* Escaped whitespace. */
683 if (i
&& ' ' == buf
[i
] && '\\' == buf
[i
- 1])
689 if ( ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
692 /* Trailing whitespace? Check at overwritten byte. */
694 if (' ' == sv
&& '\0' == buf
[i
])
695 if ( ! mdoc_pwarn(m
, line
, i
- 1, ETAILWS
))
698 for ( ; ' ' == buf
[i
]; i
++)
699 /* Skip trailing whitespace. */ ;
703 /* Trailing whitespace? */
705 if (' ' == buf
[i
- 1] && '\0' == buf
[i
])
706 if ( ! mdoc_pwarn(m
, line
, i
- 1, ETAILWS
))
713 if (j
!= i
&& ! pstring(m
, line
, j
, &buf
[j
], (size_t)(i
- j
)))
716 m
->next
= MDOC_NEXT_SIBLING
;
723 macrowarn(struct mdoc
*m
, int ln
, const char *buf
)
725 if ( ! (MDOC_IGN_MACRO
& m
->pflags
))
726 return(mdoc_verr(m
, ln
, 0,
727 "unknown macro: %s%s",
728 buf
, strlen(buf
) > 3 ? "..." : ""));
729 return(mdoc_vwarn(m
, ln
, 0, "unknown macro: %s%s",
730 buf
, strlen(buf
) > 3 ? "..." : ""));
735 * Parse a macro line, that is, a line beginning with the control
739 mdoc_pmacro(struct mdoc
*m
, int ln
, char *buf
)
744 /* Empty lines are ignored. */
751 /* Accept whitespace after the initial control char. */
755 while (buf
[i
] && ' ' == buf
[i
])
761 /* Copy the first word into a nil-terminated buffer. */
763 for (j
= 0; j
< 4; j
++, i
++) {
764 if ('\0' == (mac
[j
] = buf
[i
]))
766 else if (' ' == buf
[i
])
769 /* Check for invalid characters. */
771 if (isgraph((u_char
)buf
[i
]))
773 return(mdoc_perr(m
, ln
, i
, EPRINT
));
778 if (j
== 4 || j
< 2) {
779 if ( ! macrowarn(m
, ln
, mac
))
784 if (MDOC_MAX
== (c
= mdoc_hash_find(mac
))) {
785 if ( ! macrowarn(m
, ln
, mac
))
790 /* The macro is sane. Jump to the next word. */
792 while (buf
[i
] && ' ' == buf
[i
])
795 /* Trailing whitespace? */
797 if ('\0' == buf
[i
] && ' ' == buf
[i
- 1])
798 if ( ! mdoc_pwarn(m
, ln
, i
- 1, ETAILWS
))
802 * Begin recursive parse sequence. Since we're at the start of
803 * the line, we don't need to do callable/parseable checks.
805 if ( ! mdoc_macro(m
, c
, ln
, 1, &i
, buf
))
810 err
: /* Error out. */
812 m
->flags
|= MDOC_HALT
;