1 /* $Id: man.c,v 1.59 2010/03/29 10:10:35 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.
17 #include <sys/types.h>
27 #include "libmandoc.h"
29 const char *const __man_merrnames[WERRMAX] = {
30 "invalid character", /* WNPRINT */
31 "invalid manual section", /* WMSEC */
32 "invalid date format", /* WDATE */
33 "scope of prior line violated", /* WLNSCOPE */
34 "over-zealous prior line scope violation", /* WLNSCOPE2 */
35 "trailing whitespace", /* WTSPACE */
36 "unterminated quoted parameter", /* WTQUOTE */
37 "document has no body", /* WNODATA */
38 "document has no title/section", /* WNOTITLE */
39 "invalid escape sequence", /* WESCAPE */
40 "invalid number format", /* WNUMFMT */
41 "expected block head arguments", /* WHEADARGS */
42 "expected block body arguments", /* WBODYARGS */
43 "expected empty block head", /* WNHEADARGS */
44 "ill-formed macro", /* WMACROFORM */
45 "scope open on exit", /* WEXITSCOPE */
46 "no scope context", /* WNOSCOPE */
47 "literal context already open", /* WOLITERAL */
48 "no literal context open", /* WNLITERAL */
49 "invalid nesting of roff declarations", /* WROFFNEST */
50 "scope in roff instructions broken", /* WROFFSCOPE */
51 "document title should be uppercase", /* WTITLECASE */
54 const char *const __man_macronames[MAN_MAX] = {
55 "br", "TH", "SH", "SS",
56 "TP", "LP", "PP", "P",
57 "IP", "HP", "SM", "SB",
58 "BI", "IB", "BR", "RB",
60 "RI", "na", "i", "sp",
61 "nf", "fi", "r", "RE",
62 "RS", "DT", "UC", "PD",
63 "Sp", "Vb", "Ve", "de",
64 "dei", "am", "ami", "ig",
68 const char * const *man_macronames = __man_macronames;
70 static struct man_node *man_node_alloc(int, int,
71 enum man_type, enum mant);
72 static int man_node_append(struct man *,
74 static void man_node_free(struct man_node *);
75 static void man_node_unlink(struct man *,
77 static int man_ptext(struct man *, int, char *);
78 static int man_pmacro(struct man *, int, char *);
79 static void man_free1(struct man *);
80 static void man_alloc1(struct man *);
81 static int pstring(struct man *, int, int,
82 const char *, size_t);
83 static int macrowarn(struct man *, int, const char *);
86 const struct man_node *
87 man_node(const struct man *m)
90 return(MAN_HALT & m->flags ? NULL : m->first);
94 const struct man_meta *
95 man_meta(const struct man *m)
98 return(MAN_HALT & m->flags ? NULL : &m->meta);
103 man_reset(struct man *man)
112 man_free(struct man *man)
121 man_alloc(void *data, int pflags, const struct man_cb *cb)
125 p = mandoc_calloc(1, sizeof(struct man));
128 memcpy(&p->cb, cb, sizeof(struct man_cb));
140 man_endparse(struct man *m)
143 if (MAN_HALT & m->flags)
145 else if (man_macroend(m))
147 m->flags |= MAN_HALT;
153 man_parseln(struct man *m, int ln, char *buf)
156 return('.' == *buf || '\'' == *buf ?
157 man_pmacro(m, ln, buf) :
158 man_ptext(m, ln, buf));
163 man_free1(struct man *man)
167 man_node_delete(man, man->first);
169 free(man->meta.title);
170 if (man->meta.source)
171 free(man->meta.source);
178 man_alloc1(struct man *m)
181 memset(&m->meta, 0, sizeof(struct man_meta));
183 m->last = mandoc_calloc(1, sizeof(struct man_node));
185 m->last->type = MAN_ROOT;
186 m->last->tok = MAN_MAX;
187 m->next = MAN_NEXT_CHILD;
192 man_node_append(struct man *man, struct man_node *p)
197 assert(MAN_ROOT != p->type);
200 case (MAN_NEXT_SIBLING):
203 p->parent = man->last->parent;
205 case (MAN_NEXT_CHILD):
206 man->last->child = p;
207 p->parent = man->last;
217 if ( ! man_valid_pre(man, p))
222 assert(MAN_BLOCK == p->parent->type);
226 assert(MAN_BLOCK == p->parent->type);
237 if ( ! man_valid_post(man))
239 if ( ! man_action_post(man))
250 static struct man_node *
251 man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
255 p = mandoc_calloc(1, sizeof(struct man_node));
265 man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
269 p = man_node_alloc(line, pos, MAN_ELEM, tok);
270 if ( ! man_node_append(m, p))
272 m->next = MAN_NEXT_CHILD;
278 man_head_alloc(struct man *m, int line, int pos, enum mant tok)
282 p = man_node_alloc(line, pos, MAN_HEAD, tok);
283 if ( ! man_node_append(m, p))
285 m->next = MAN_NEXT_CHILD;
291 man_body_alloc(struct man *m, int line, int pos, enum mant tok)
295 p = man_node_alloc(line, pos, MAN_BODY, tok);
296 if ( ! man_node_append(m, p))
298 m->next = MAN_NEXT_CHILD;
304 man_block_alloc(struct man *m, int line, int pos, enum mant tok)
308 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
309 if ( ! man_node_append(m, p))
311 m->next = MAN_NEXT_CHILD;
317 pstring(struct man *m, int line, int pos,
318 const char *p, size_t len)
323 n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
324 n->string = mandoc_malloc(len + 1);
325 sv = strlcpy(n->string, p, len + 1);
327 /* Prohibit truncation. */
328 assert(sv < len + 1);
330 if ( ! man_node_append(m, n))
332 m->next = MAN_NEXT_SIBLING;
338 man_word_alloc(struct man *m, int line, int pos, const char *word)
341 return(pstring(m, line, pos, word, strlen(word)));
346 * Free all of the resources held by a node. This does NOT unlink a
347 * node from its context; for that, see man_node_unlink().
350 man_node_free(struct man_node *p)
360 man_node_delete(struct man *m, struct man_node *p)
364 man_node_delete(m, p->child);
366 man_node_unlink(m, p);
372 man_ptext(struct man *m, int line, char *buf)
377 /* Literal free-form text whitespace is preserved. */
379 if (MAN_LITERAL & m->flags) {
380 if ( ! man_word_alloc(m, line, 0, buf))
385 /* First de-chunk and allocate words. */
387 for (i = 0; ' ' == buf[i]; i++)
388 /* Skip leading whitespace. */ ;
390 if ('\0' == buf[i]) {
391 /* Trailing whitespace? */
392 if (i && ' ' == buf[i - 1])
393 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
395 if ( ! pstring(m, line, 0, &buf[i], 0))
400 for (j = i; buf[i]; i++) {
404 /* Escaped whitespace. */
405 if (i && ' ' == buf[i] && '\\' == buf[i - 1])
411 if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
414 /* Trailing whitespace? Check at overwritten byte. */
416 if (' ' == sv && '\0' == buf[i])
417 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
420 for ( ; ' ' == buf[i]; i++)
421 /* Skip trailing whitespace. */ ;
425 /* Trailing whitespace? */
427 if (' ' == buf[i - 1] && '\0' == buf[i])
428 if ( ! man_pwarn(m, line, i - 1, WTSPACE))
435 if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
441 * Co-ordinate what happens with having a next-line scope open:
442 * first close out the element scope (if applicable), then close
443 * out the block scope (also if applicable).
446 if (MAN_ELINE & m->flags) {
447 m->flags &= ~MAN_ELINE;
448 if ( ! man_unscope(m, m->last->parent, WERRMAX))
452 if ( ! (MAN_BLINE & m->flags))
454 m->flags &= ~MAN_BLINE;
456 if ( ! man_unscope(m, m->last->parent, WERRMAX))
458 return(man_body_alloc(m, line, 0, m->last->tok));
463 macrowarn(struct man *m, int ln, const char *buf)
465 if ( ! (MAN_IGN_MACRO & m->pflags))
466 return(man_verr(m, ln, 0,
467 "unknown macro: %s%s",
468 buf, strlen(buf) > 3 ? "..." : ""));
469 return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
470 buf, strlen(buf) > 3 ? "..." : ""));
475 man_pmacro(struct man *m, int ln, char *buf)
482 /* Comments and empties are quickly ignored. */
490 * Skip whitespace between the control character and initial
491 * text. "Whitespace" is both spaces and tabs.
493 if (' ' == buf[i] || '\t' == buf[i]) {
495 while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
503 /* Copy the first word into a nil-terminated buffer. */
505 for (j = 0; j < 4; j++, i++) {
506 if ('\0' == (mac[j] = buf[i]))
508 else if (' ' == buf[i])
511 /* Check for invalid characters. */
513 if (isgraph((u_char)buf[i]))
515 return(man_perr(m, ln, i, WNPRINT));
520 if (j == 4 || j < 1) {
521 if ( ! (MAN_IGN_MACRO & m->pflags)) {
522 (void)man_perr(m, ln, ppos, WMACROFORM);
525 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
530 if (MAN_MAX == (tok = man_hash_find(mac))) {
531 if ( ! macrowarn(m, ln, mac))
536 /* The macro is sane. Jump to the next word. */
538 while (buf[i] && ' ' == buf[i])
541 /* Trailing whitespace? */
543 if ('\0' == buf[i] && ' ' == buf[i - 1])
544 if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
548 * Remove prior ELINE macro, as it's being clobbering by a new
549 * macro. Note that NSCOPED macros do not close out ELINE
550 * macros---they don't print text---so we let those slip by.
551 * NOTE: we don't allow roff blocks (NOCLOSE) to be embedded
552 * here because that would stipulate blocks as children of
556 if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
557 m->flags & MAN_ELINE) {
558 assert(MAN_TEXT != m->last->type);
561 * This occurs in the following construction:
567 * Flat-out disallow this madness.
569 if (MAN_NSCOPED & man_macros[m->last->tok].flags)
570 return(man_perr(m, ln, ppos, WLNSCOPE));
575 assert(NULL == n->child);
576 assert(0 == n->nchild);
578 if ( ! man_nwarn(m, n, WLNSCOPE))
581 man_node_delete(m, n);
582 m->flags &= ~MAN_ELINE;
586 * Save the fact that we're in the next-line for a block. In
587 * this way, embedded roff instructions can "remember" state
591 if (MAN_BLINE & m->flags)
592 m->flags |= MAN_BPLINE;
594 /* Call to handler... */
596 assert(man_macros[tok].fp);
597 if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
602 * We weren't in a block-line scope when entering the
603 * above-parsed macro, so return.
606 if ( ! (MAN_BPLINE & m->flags)) {
607 m->flags &= ~MAN_ILINE;
610 m->flags &= ~MAN_BPLINE;
613 * If we're in a block scope, then allow this macro to slip by
614 * without closing scope around it.
617 if (MAN_ILINE & m->flags) {
618 m->flags &= ~MAN_ILINE;
623 * If we've opened a new next-line element scope, then return
624 * now, as the next line will close out the block scope.
627 if (MAN_ELINE & m->flags)
630 /* Close out the block scope opened in the prior line. */
632 assert(MAN_BLINE & m->flags);
633 m->flags &= ~MAN_BLINE;
635 if ( ! man_unscope(m, m->last->parent, WERRMAX))
637 return(man_body_alloc(m, ln, 0, m->last->tok));
639 err: /* Error out. */
641 m->flags |= MAN_HALT;
647 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
652 if (NULL == man->cb.man_err)
656 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
658 return((*man->cb.man_err)(man->data, ln, pos, buf));
663 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
668 if (NULL == man->cb.man_warn)
672 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
674 return((*man->cb.man_warn)(man->data, ln, pos, buf));
679 man_err(struct man *m, int line, int pos, int iserr, enum merr type)
683 p = __man_merrnames[(int)type];
687 return(man_verr(m, line, pos, p));
689 return(man_vwarn(m, line, pos, p));
694 * Unlink a node from its context. If "m" is provided, the last parse
695 * point will also be adjusted accordingly.
698 man_node_unlink(struct man *m, struct man_node *n)
701 /* Adjust siblings. */
704 n->prev->next = n->next;
706 n->next->prev = n->prev;
712 if (n->parent->child == n)
713 n->parent->child = n->prev ? n->prev : n->next;
716 /* Adjust parse point, if applicable. */
718 if (m && m->last == n) {
719 /*XXX: this can occur when bailing from validation. */
720 /*assert(NULL == n->next);*/
723 m->next = MAN_NEXT_SIBLING;
726 m->next = MAN_NEXT_CHILD;
730 if (m && m->first == n)