1 /* $Id: man.c,v 1.14 2009/10/19 10:20:24 schwarze 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>
28 const char *const __man_merrnames[WERRMAX] = {
29 "invalid character", /* WNPRINT */
30 "system: malloc error", /* WNMEM */
31 "invalid manual section", /* WMSEC */
32 "invalid date format", /* WDATE */
33 "scope of prior line violated", /* WLNSCOPE */
34 "trailing whitespace", /* WTSPACE */
35 "unterminated quoted parameter", /* WTQUOTE */
36 "document has no body", /* WNODATA */
37 "document has no title/section", /* WNOTITLE */
38 "invalid escape sequence", /* WESCAPE */
39 "invalid number format", /* WNUMFMT */
40 "expected block head arguments", /* WHEADARGS */
41 "expected block body arguments", /* WBODYARGS */
42 "expected empty block head", /* WNHEADARGS */
43 "unknown macro", /* WMACRO */
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 */
51 const char *const __man_macronames[MAN_MAX] = {
52 "br", "TH", "SH", "SS",
53 "TP", "LP", "PP", "P",
54 "IP", "HP", "SM", "SB",
55 "BI", "IB", "BR", "RB",
57 "RI", "na", "i", "sp",
58 "nf", "fi", "r", "RE",
62 const char * const *man_macronames = __man_macronames;
64 static struct man_node *man_node_alloc(int, int,
66 static int man_node_append(struct man *,
68 static int man_ptext(struct man *, int, char *);
69 static int man_pmacro(struct man *, int, char *);
70 static void man_free1(struct man *);
71 static int man_alloc1(struct man *);
72 static int pstring(struct man *, int, int,
73 const char *, size_t);
76 const struct man_node *
77 man_node(const struct man *m)
80 return(MAN_HALT & m->flags ? NULL : m->first);
84 const struct man_meta *
85 man_meta(const struct man *m)
88 return(MAN_HALT & m->flags ? NULL : &m->meta);
93 man_reset(struct man *man)
97 return(man_alloc1(man));
102 man_free(struct man *man)
111 man_alloc(void *data, int pflags, const struct man_cb *cb)
115 if (NULL == (p = calloc(1, sizeof(struct man))))
118 if ( ! man_alloc1(p)) {
127 (void)memcpy(&p->cb, cb, sizeof(struct man_cb));
133 man_endparse(struct man *m)
136 if (MAN_HALT & m->flags)
138 else if (man_macroend(m))
140 m->flags |= MAN_HALT;
146 man_parseln(struct man *m, int ln, char *buf)
150 man_pmacro(m, ln, buf) :
151 man_ptext(m, ln, buf));
156 man_free1(struct man *man)
160 man_node_freelist(man->first);
162 free(man->meta.title);
163 if (man->meta.source)
164 free(man->meta.source);
171 man_alloc1(struct man *m)
174 bzero(&m->meta, sizeof(struct man_meta));
176 m->last = calloc(1, sizeof(struct man_node));
180 m->last->type = MAN_ROOT;
181 m->next = MAN_NEXT_CHILD;
187 man_node_append(struct man *man, struct man_node *p)
192 assert(MAN_ROOT != p->type);
195 case (MAN_NEXT_SIBLING):
198 p->parent = man->last->parent;
200 case (MAN_NEXT_CHILD):
201 man->last->child = p;
202 p->parent = man->last;
211 if ( ! man_valid_pre(man, p))
216 assert(MAN_BLOCK == p->parent->type);
220 assert(MAN_BLOCK == p->parent->type);
231 if ( ! man_valid_post(man))
233 if ( ! man_action_post(man))
244 static struct man_node *
245 man_node_alloc(int line, int pos, enum man_type type, int tok)
249 p = calloc(1, sizeof(struct man_node));
262 man_elem_alloc(struct man *m, int line, int pos, int tok)
266 p = man_node_alloc(line, pos, MAN_ELEM, tok);
269 if ( ! man_node_append(m, p))
271 m->next = MAN_NEXT_CHILD;
277 man_head_alloc(struct man *m, int line, int pos, int tok)
281 p = man_node_alloc(line, pos, MAN_HEAD, tok);
284 if ( ! man_node_append(m, p))
286 m->next = MAN_NEXT_CHILD;
292 man_body_alloc(struct man *m, int line, int pos, int tok)
296 p = man_node_alloc(line, pos, MAN_BODY, tok);
299 if ( ! man_node_append(m, p))
301 m->next = MAN_NEXT_CHILD;
307 man_block_alloc(struct man *m, int line, int pos, int tok)
311 p = man_node_alloc(line, pos, MAN_BLOCK, tok);
314 if ( ! man_node_append(m, p))
316 m->next = MAN_NEXT_CHILD;
322 pstring(struct man *m, int line, int pos,
323 const char *p, size_t len)
328 n = man_node_alloc(line, pos, MAN_TEXT, -1);
332 n->string = malloc(len + 1);
333 if (NULL == n->string) {
338 sv = strlcpy(n->string, p, len + 1);
340 /* Prohibit truncation. */
341 assert(sv < len + 1);
343 if ( ! man_node_append(m, n))
345 m->next = MAN_NEXT_SIBLING;
351 man_word_alloc(struct man *m, int line, int pos, const char *word)
354 return(pstring(m, line, pos, word, strlen(word)));
359 man_node_free(struct man_node *p)
371 man_node_freelist(struct man_node *p)
376 man_node_freelist(p->child);
377 assert(0 == p->nchild);
381 man_node_freelist(n);
386 man_ptext(struct man *m, int line, char *buf)
390 /* Literal free-form text whitespace is preserved. */
392 if (MAN_LITERAL & m->flags) {
393 if ( ! man_word_alloc(m, line, 0, buf))
398 /* First de-chunk and allocate words. */
400 for (i = 0; ' ' == buf[i]; i++)
401 /* Skip leading whitespace. */ ;
403 if ( ! pstring(m, line, 0, &buf[i], 0))
408 for (j = i; buf[i]; i++) {
412 /* Escaped whitespace. */
413 if (i && ' ' == buf[i] && '\\' == buf[i - 1])
417 if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
420 for ( ; ' ' == buf[i]; i++)
421 /* Skip trailing whitespace. */ ;
428 if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
434 * Co-ordinate what happens with having a next-line scope open:
435 * first close out the element scope (if applicable), then close
436 * out the block scope (also if applicable).
439 if (MAN_ELINE & m->flags) {
440 m->flags &= ~MAN_ELINE;
441 if ( ! man_unscope(m, m->last->parent))
445 if ( ! (MAN_BLINE & m->flags))
447 m->flags &= ~MAN_BLINE;
449 if ( ! man_unscope(m, m->last->parent))
451 return(man_body_alloc(m, line, 0, m->last->tok));
456 man_pmacro(struct man *m, int ln, char *buf)
458 int i, j, c, ppos, fl;
462 /* Comments and empties are quickly ignored. */
473 while (buf[i] && ' ' == buf[i])
481 /* Copy the first word into a nil-terminated buffer. */
483 for (j = 0; j < 4; j++, i++) {
484 if (0 == (mac[j] = buf[i]))
486 else if (' ' == buf[i])
489 /* Check for invalid characters. */
491 if (isgraph((u_char)buf[i]))
493 return(man_perr(m, ln, i, WNPRINT));
498 if (j == 4 || j < 1) {
499 if ( ! (MAN_IGN_MACRO & m->pflags)) {
500 (void)man_perr(m, ln, ppos, WMACROFORM);
503 if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
508 if (MAN_MAX == (c = man_hash_find(mac))) {
509 if ( ! (MAN_IGN_MACRO & m->pflags)) {
510 (void)man_perr(m, ln, ppos, WMACRO);
513 if ( ! man_pwarn(m, ln, ppos, WMACRO))
518 /* The macro is sane. Jump to the next word. */
520 while (buf[i] && ' ' == buf[i])
523 /* Remove prior ELINE macro, if applicable. */
525 if (m->flags & MAN_ELINE) {
527 assert(NULL == n->child);
528 assert(0 == n->nchild);
529 if ( ! man_nwarn(m, n, WLNSCOPE))
533 assert(n != n->parent->child);
534 assert(n == n->prev->next);
535 n->prev->next = NULL;
537 m->next = MAN_NEXT_SIBLING;
539 assert(n == n->parent->child);
540 n->parent->child = NULL;
542 m->next = MAN_NEXT_CHILD;
546 m->flags &= ~MAN_ELINE;
549 /* Begin recursive parse sequence. */
551 assert(man_macros[c].fp);
553 if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
557 if ( ! (MAN_BLINE & fl))
561 * If we've opened a new next-line element scope, then return
562 * now, as the next line will close out the block scope.
565 if (MAN_ELINE & m->flags)
568 /* Close out the block scope opened in the prior line. */
570 assert(MAN_BLINE & m->flags);
571 m->flags &= ~MAN_BLINE;
573 if ( ! man_unscope(m, m->last->parent))
575 return(man_body_alloc(m, ln, 0, m->last->tok));
577 err: /* Error out. */
579 m->flags |= MAN_HALT;
585 man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
590 if (NULL == man->cb.man_err)
594 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
596 return((*man->cb.man_err)(man->data, ln, pos, buf));
601 man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
606 if (NULL == man->cb.man_warn)
610 (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
612 return((*man->cb.man_warn)(man->data, ln, pos, buf));
617 man_err(struct man *m, int line, int pos, int iserr, enum merr type)
621 p = __man_merrnames[(int)type];
625 return(man_verr(m, line, pos, p));
627 return(man_vwarn(m, line, pos, p));