Annotation of mandoc/man.c, Revision 1.53
1.53 ! kristaps 1: /* $Id: man.c,v 1.52 2010/03/23 11:30:48 kristaps Exp $ */
1.1 kristaps 2: /*
1.19 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.18 kristaps 6: * purpose with or without fee is hereby granted, provided that the above
7: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 8: *
1.18 kristaps 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.
1.1 kristaps 16: */
1.47 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.41 kristaps 21: #include <sys/types.h>
22:
1.1 kristaps 23: #include <assert.h>
24: #include <ctype.h>
25: #include <stdarg.h>
26: #include <stdlib.h>
27: #include <stdio.h>
28: #include <string.h>
29:
30: #include "libman.h"
1.45 kristaps 31: #include "libmandoc.h"
1.1 kristaps 32:
1.27 kristaps 33: const char *const __man_merrnames[WERRMAX] = {
34: "invalid character", /* WNPRINT */
35: "invalid manual section", /* WMSEC */
36: "invalid date format", /* WDATE */
37: "scope of prior line violated", /* WLNSCOPE */
1.51 kristaps 38: "over-zealous prior line scope violation", /* WLNSCOPE2 */
1.27 kristaps 39: "trailing whitespace", /* WTSPACE */
40: "unterminated quoted parameter", /* WTQUOTE */
41: "document has no body", /* WNODATA */
42: "document has no title/section", /* WNOTITLE */
43: "invalid escape sequence", /* WESCAPE */
1.28 kristaps 44: "invalid number format", /* WNUMFMT */
1.29 kristaps 45: "expected block head arguments", /* WHEADARGS */
46: "expected block body arguments", /* WBODYARGS */
47: "expected empty block head", /* WNHEADARGS */
48: "ill-formed macro", /* WMACROFORM */
1.30 kristaps 49: "scope open on exit", /* WEXITSCOPE */
1.35 kristaps 50: "no scope context", /* WNOSCOPE */
51: "literal context already open", /* WOLITERAL */
52: "no literal context open" /* WNLITERAL */
1.27 kristaps 53: };
54:
1.1 kristaps 55: const char *const __man_macronames[MAN_MAX] = {
1.21 kristaps 56: "br", "TH", "SH", "SS",
1.1 kristaps 57: "TP", "LP", "PP", "P",
58: "IP", "HP", "SM", "SB",
59: "BI", "IB", "BR", "RB",
1.11 kristaps 60: "R", "B", "I", "IR",
1.29 kristaps 61: "RI", "na", "i", "sp",
1.30 kristaps 62: "nf", "fi", "r", "RE",
1.52 kristaps 63: "RS", "DT", "UC", "PD",
64: "Sp", "Vb", "Ve",
1.1 kristaps 65: };
66:
67: const char * const *man_macronames = __man_macronames;
68:
1.16 kristaps 69: static struct man_node *man_node_alloc(int, int,
1.53 ! kristaps 70: enum man_type, enum mant);
1.1 kristaps 71: static int man_node_append(struct man *,
72: struct man_node *);
73: static int man_ptext(struct man *, int, char *);
74: static int man_pmacro(struct man *, int, char *);
1.2 kristaps 75: static void man_free1(struct man *);
1.45 kristaps 76: static void man_alloc1(struct man *);
1.31 kristaps 77: static int pstring(struct man *, int, int,
78: const char *, size_t);
1.43 kristaps 79: static int macrowarn(struct man *, int, const char *);
1.1 kristaps 80:
81:
82: const struct man_node *
1.2 kristaps 83: man_node(const struct man *m)
1.1 kristaps 84: {
85:
1.2 kristaps 86: return(MAN_HALT & m->flags ? NULL : m->first);
1.1 kristaps 87: }
88:
89:
90: const struct man_meta *
1.2 kristaps 91: man_meta(const struct man *m)
1.1 kristaps 92: {
93:
1.2 kristaps 94: return(MAN_HALT & m->flags ? NULL : &m->meta);
1.1 kristaps 95: }
96:
97:
1.45 kristaps 98: void
1.1 kristaps 99: man_reset(struct man *man)
100: {
101:
1.2 kristaps 102: man_free1(man);
1.45 kristaps 103: man_alloc1(man);
1.1 kristaps 104: }
105:
106:
107: void
108: man_free(struct man *man)
109: {
110:
1.2 kristaps 111: man_free1(man);
1.1 kristaps 112: free(man);
113: }
114:
115:
116: struct man *
1.7 kristaps 117: man_alloc(void *data, int pflags, const struct man_cb *cb)
1.1 kristaps 118: {
119: struct man *p;
120:
1.45 kristaps 121: p = mandoc_calloc(1, sizeof(struct man));
1.2 kristaps 122:
1.45 kristaps 123: if (cb)
124: memcpy(&p->cb, cb, sizeof(struct man_cb));
1.1 kristaps 125:
1.40 kristaps 126: man_hash_init();
1.4 kristaps 127: p->data = data;
1.7 kristaps 128: p->pflags = pflags;
1.45 kristaps 129:
130: man_alloc1(p);
1.1 kristaps 131: return(p);
132: }
133:
134:
135: int
136: man_endparse(struct man *m)
137: {
138:
1.3 kristaps 139: if (MAN_HALT & m->flags)
140: return(0);
141: else if (man_macroend(m))
142: return(1);
143: m->flags |= MAN_HALT;
144: return(0);
1.1 kristaps 145: }
146:
147:
148: int
149: man_parseln(struct man *m, int ln, char *buf)
150: {
151:
152: return('.' == *buf ?
153: man_pmacro(m, ln, buf) :
154: man_ptext(m, ln, buf));
155: }
156:
157:
1.2 kristaps 158: static void
159: man_free1(struct man *man)
160: {
161:
162: if (man->first)
163: man_node_freelist(man->first);
164: if (man->meta.title)
165: free(man->meta.title);
1.6 kristaps 166: if (man->meta.source)
167: free(man->meta.source);
1.2 kristaps 168: if (man->meta.vol)
169: free(man->meta.vol);
170: }
171:
172:
1.45 kristaps 173: static void
1.2 kristaps 174: man_alloc1(struct man *m)
175: {
176:
1.44 kristaps 177: memset(&m->meta, 0, sizeof(struct man_meta));
1.2 kristaps 178: m->flags = 0;
1.45 kristaps 179: m->last = mandoc_calloc(1, sizeof(struct man_node));
1.2 kristaps 180: m->first = m->last;
181: m->last->type = MAN_ROOT;
182: m->next = MAN_NEXT_CHILD;
183: }
184:
185:
1.1 kristaps 186: static int
187: man_node_append(struct man *man, struct man_node *p)
188: {
189:
190: assert(man->last);
191: assert(man->first);
192: assert(MAN_ROOT != p->type);
193:
194: switch (man->next) {
195: case (MAN_NEXT_SIBLING):
196: man->last->next = p;
197: p->prev = man->last;
198: p->parent = man->last->parent;
199: break;
200: case (MAN_NEXT_CHILD):
201: man->last->child = p;
202: p->parent = man->last;
203: break;
204: default:
205: abort();
206: /* NOTREACHED */
207: }
1.22 kristaps 208:
209: p->parent->nchild++;
1.1 kristaps 210:
1.29 kristaps 211: if ( ! man_valid_pre(man, p))
212: return(0);
213:
214: switch (p->type) {
215: case (MAN_HEAD):
216: assert(MAN_BLOCK == p->parent->type);
217: p->parent->head = p;
218: break;
219: case (MAN_BODY):
220: assert(MAN_BLOCK == p->parent->type);
221: p->parent->body = p;
222: break;
223: default:
224: break;
225: }
226:
1.2 kristaps 227: man->last = p;
228:
1.1 kristaps 229: switch (p->type) {
1.2 kristaps 230: case (MAN_TEXT):
231: if ( ! man_valid_post(man))
232: return(0);
233: if ( ! man_action_post(man))
234: return(0);
1.1 kristaps 235: break;
236: default:
237: break;
238: }
239:
240: return(1);
241: }
242:
243:
244: static struct man_node *
1.53 ! kristaps 245: man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
1.1 kristaps 246: {
247: struct man_node *p;
248:
1.45 kristaps 249: p = mandoc_calloc(1, sizeof(struct man_node));
1.1 kristaps 250: p->line = line;
251: p->pos = pos;
252: p->type = type;
1.16 kristaps 253: p->tok = tok;
1.1 kristaps 254: return(p);
255: }
256:
257:
258: int
1.53 ! kristaps 259: man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
1.1 kristaps 260: {
261: struct man_node *p;
262:
1.16 kristaps 263: p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.30 kristaps 264: if ( ! man_node_append(m, p))
265: return(0);
266: m->next = MAN_NEXT_CHILD;
267: return(1);
1.1 kristaps 268: }
269:
270:
271: int
1.53 ! kristaps 272: man_head_alloc(struct man *m, int line, int pos, enum mant tok)
1.29 kristaps 273: {
274: struct man_node *p;
275:
276: p = man_node_alloc(line, pos, MAN_HEAD, tok);
277: if ( ! man_node_append(m, p))
278: return(0);
279: m->next = MAN_NEXT_CHILD;
280: return(1);
281: }
282:
283:
284: int
1.53 ! kristaps 285: man_body_alloc(struct man *m, int line, int pos, enum mant tok)
1.29 kristaps 286: {
287: struct man_node *p;
288:
289: p = man_node_alloc(line, pos, MAN_BODY, tok);
290: if ( ! man_node_append(m, p))
291: return(0);
292: m->next = MAN_NEXT_CHILD;
293: return(1);
294: }
295:
296:
297: int
1.53 ! kristaps 298: man_block_alloc(struct man *m, int line, int pos, enum mant tok)
1.29 kristaps 299: {
300: struct man_node *p;
301:
302: p = man_node_alloc(line, pos, MAN_BLOCK, tok);
303: if ( ! man_node_append(m, p))
304: return(0);
305: m->next = MAN_NEXT_CHILD;
306: return(1);
307: }
308:
309:
1.31 kristaps 310: static int
311: pstring(struct man *m, int line, int pos,
312: const char *p, size_t len)
1.1 kristaps 313: {
1.31 kristaps 314: struct man_node *n;
315: size_t sv;
1.1 kristaps 316:
1.53 ! kristaps 317: n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
1.45 kristaps 318: n->string = mandoc_malloc(len + 1);
1.31 kristaps 319: sv = strlcpy(n->string, p, len + 1);
320:
321: /* Prohibit truncation. */
322: assert(sv < len + 1);
323:
324: if ( ! man_node_append(m, n))
1.30 kristaps 325: return(0);
326: m->next = MAN_NEXT_SIBLING;
327: return(1);
1.1 kristaps 328: }
329:
330:
1.31 kristaps 331: int
332: man_word_alloc(struct man *m, int line, int pos, const char *word)
333: {
334:
335: return(pstring(m, line, pos, word, strlen(word)));
336: }
337:
338:
1.1 kristaps 339: void
340: man_node_free(struct man_node *p)
341: {
342:
343: if (p->string)
344: free(p->string);
1.24 kristaps 345: if (p->parent)
346: p->parent->nchild--;
1.1 kristaps 347: free(p);
348: }
349:
350:
351: void
352: man_node_freelist(struct man_node *p)
353: {
1.37 kristaps 354: struct man_node *n;
1.1 kristaps 355:
356: if (p->child)
357: man_node_freelist(p->child);
1.24 kristaps 358: assert(0 == p->nchild);
1.37 kristaps 359: n = p->next;
1.1 kristaps 360: man_node_free(p);
1.37 kristaps 361: if (n)
362: man_node_freelist(n);
1.1 kristaps 363: }
364:
365:
366: static int
367: man_ptext(struct man *m, int line, char *buf)
368: {
1.31 kristaps 369: int i, j;
1.49 kristaps 370: char sv;
1.31 kristaps 371:
1.35 kristaps 372: /* Literal free-form text whitespace is preserved. */
373:
374: if (MAN_LITERAL & m->flags) {
375: if ( ! man_word_alloc(m, line, 0, buf))
376: return(0);
377: goto descope;
378: }
379:
1.31 kristaps 380: /* First de-chunk and allocate words. */
381:
382: for (i = 0; ' ' == buf[i]; i++)
383: /* Skip leading whitespace. */ ;
1.48 kristaps 384:
1.49 kristaps 385: if ('\0' == buf[i]) {
386: /* Trailing whitespace? */
387: if (i && ' ' == buf[i - 1])
388: if ( ! man_pwarn(m, line, i - 1, WTSPACE))
389: return(0);
1.31 kristaps 390: if ( ! pstring(m, line, 0, &buf[i], 0))
391: return(0);
392: goto descope;
393: }
394:
395: for (j = i; buf[i]; i++) {
396: if (' ' != buf[i])
397: continue;
398:
399: /* Escaped whitespace. */
400: if (i && ' ' == buf[i] && '\\' == buf[i - 1])
401: continue;
1.1 kristaps 402:
1.49 kristaps 403: sv = buf[i];
404: buf[i++] = '\0';
405:
1.31 kristaps 406: if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
407: return(0);
1.29 kristaps 408:
1.49 kristaps 409: /* Trailing whitespace? Check at overwritten byte. */
410:
411: if (' ' == sv && '\0' == buf[i])
412: if ( ! man_pwarn(m, line, i - 1, WTSPACE))
413: return(0);
414:
1.31 kristaps 415: for ( ; ' ' == buf[i]; i++)
416: /* Skip trailing whitespace. */ ;
1.30 kristaps 417:
1.31 kristaps 418: j = i;
1.49 kristaps 419:
420: /* Trailing whitespace? */
421:
422: if (' ' == buf[i - 1] && '\0' == buf[i])
423: if ( ! man_pwarn(m, line, i - 1, WTSPACE))
424: return(0);
425:
426: if ('\0' == buf[i])
1.31 kristaps 427: break;
428: }
429:
430: if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.1 kristaps 431: return(0);
1.31 kristaps 432:
433: descope:
1.11 kristaps 434:
435: /*
1.29 kristaps 436: * Co-ordinate what happens with having a next-line scope open:
437: * first close out the element scope (if applicable), then close
438: * out the block scope (also if applicable).
1.11 kristaps 439: */
440:
1.29 kristaps 441: if (MAN_ELINE & m->flags) {
442: m->flags &= ~MAN_ELINE;
443: if ( ! man_unscope(m, m->last->parent))
444: return(0);
445: }
446:
447: if ( ! (MAN_BLINE & m->flags))
1.11 kristaps 448: return(1);
1.29 kristaps 449: m->flags &= ~MAN_BLINE;
1.11 kristaps 450:
1.29 kristaps 451: if ( ! man_unscope(m, m->last->parent))
1.11 kristaps 452: return(0);
1.29 kristaps 453: return(man_body_alloc(m, line, 0, m->last->tok));
1.1 kristaps 454: }
455:
456:
1.43 kristaps 457: static int
458: macrowarn(struct man *m, int ln, const char *buf)
459: {
460: if ( ! (MAN_IGN_MACRO & m->pflags))
461: return(man_verr(m, ln, 0,
462: "unknown macro: %s%s",
463: buf, strlen(buf) > 3 ? "..." : ""));
464: return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
465: buf, strlen(buf) > 3 ? "..." : ""));
466: }
467:
468:
1.1 kristaps 469: int
470: man_pmacro(struct man *m, int ln, char *buf)
471: {
1.53 ! kristaps 472: int i, j, ppos, fl;
! 473: enum mant tok;
1.34 kristaps 474: char mac[5];
475: struct man_node *n;
1.1 kristaps 476:
477: /* Comments and empties are quickly ignored. */
478:
1.29 kristaps 479: fl = m->flags;
1.11 kristaps 480:
1.46 kristaps 481: if ('\0' == buf[1])
482: return(1);
1.1 kristaps 483:
1.9 kristaps 484: i = 1;
485:
486: if (' ' == buf[i]) {
487: i++;
1.1 kristaps 488: while (buf[i] && ' ' == buf[i])
489: i++;
1.48 kristaps 490: if ('\0' == buf[i])
1.11 kristaps 491: goto out;
1.1 kristaps 492: }
493:
1.10 kristaps 494: ppos = i;
495:
1.1 kristaps 496: /* Copy the first word into a nil-terminated buffer. */
497:
1.10 kristaps 498: for (j = 0; j < 4; j++, i++) {
1.48 kristaps 499: if ('\0' == (mac[j] = buf[i]))
1.1 kristaps 500: break;
1.10 kristaps 501: else if (' ' == buf[i])
1.1 kristaps 502: break;
1.38 kristaps 503:
504: /* Check for invalid characters. */
505:
506: if (isgraph((u_char)buf[i]))
507: continue;
508: return(man_perr(m, ln, i, WNPRINT));
1.1 kristaps 509: }
510:
1.46 kristaps 511: mac[j] = '\0';
1.1 kristaps 512:
1.9 kristaps 513: if (j == 4 || j < 1) {
1.7 kristaps 514: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.29 kristaps 515: (void)man_perr(m, ln, ppos, WMACROFORM);
1.7 kristaps 516: goto err;
517: }
1.29 kristaps 518: if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
1.7 kristaps 519: goto err;
1.12 kristaps 520: return(1);
1.7 kristaps 521: }
1.1 kristaps 522:
1.53 ! kristaps 523: if (MAN_MAX == (tok = man_hash_find(mac))) {
1.43 kristaps 524: if ( ! macrowarn(m, ln, mac))
1.7 kristaps 525: goto err;
1.12 kristaps 526: return(1);
1.1 kristaps 527: }
528:
529: /* The macro is sane. Jump to the next word. */
530:
531: while (buf[i] && ' ' == buf[i])
532: i++;
533:
1.48 kristaps 534: /* Trailing whitespace? */
535:
536: if ('\0' == buf[i] && ' ' == buf[i - 1])
537: if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
538: goto err;
539:
1.50 kristaps 540: /*
1.51 kristaps 541: * Remove prior ELINE macro, as it's being clobbering by a new
542: * macro. Note that NSCOPED macros do not close out ELINE
543: * macros---they don't print text---so we let those slip by.
1.50 kristaps 544: */
1.34 kristaps 545:
1.53 ! kristaps 546: if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.51 kristaps 547: m->flags & MAN_ELINE) {
548: assert(MAN_TEXT != m->last->type);
549:
550: /*
551: * This occurs in the following construction:
552: * .B
553: * .br
554: * .B
555: * .br
556: * I hate man macros.
557: * Flat-out disallow this madness.
558: */
559: if (MAN_NSCOPED & man_macros[m->last->tok].flags)
560: return(man_perr(m, ln, ppos, WLNSCOPE));
561:
1.34 kristaps 562: n = m->last;
1.51 kristaps 563:
564: assert(n);
1.34 kristaps 565: assert(NULL == n->child);
1.37 kristaps 566: assert(0 == n->nchild);
1.51 kristaps 567:
1.34 kristaps 568: if ( ! man_nwarn(m, n, WLNSCOPE))
569: return(0);
570:
1.51 kristaps 571: man_node_unlink(m, n);
1.34 kristaps 572: man_node_free(n);
573: m->flags &= ~MAN_ELINE;
574: }
575:
1.1 kristaps 576: /* Begin recursive parse sequence. */
577:
1.53 ! kristaps 578: assert(man_macros[tok].fp);
1.29 kristaps 579:
1.53 ! kristaps 580: if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
1.1 kristaps 581: goto err;
582:
1.11 kristaps 583: out:
1.50 kristaps 584: /*
585: * We weren't in a block-line scope when entering the
586: * above-parsed macro, so return.
587: */
588:
589: if ( ! (MAN_BLINE & fl)) {
590: m->flags &= ~MAN_ILINE;
1.29 kristaps 591: return(1);
1.50 kristaps 592: }
593:
594: /*
595: * If we're in a block scope, then allow this macro to slip by
596: * without closing scope around it.
597: */
598:
599: if (MAN_ILINE & m->flags) {
600: m->flags &= ~MAN_ILINE;
601: return(1);
602: }
1.29 kristaps 603:
604: /*
605: * If we've opened a new next-line element scope, then return
606: * now, as the next line will close out the block scope.
607: */
608:
609: if (MAN_ELINE & m->flags)
610: return(1);
611:
612: /* Close out the block scope opened in the prior line. */
1.11 kristaps 613:
1.29 kristaps 614: assert(MAN_BLINE & m->flags);
615: m->flags &= ~MAN_BLINE;
1.11 kristaps 616:
1.29 kristaps 617: if ( ! man_unscope(m, m->last->parent))
618: return(0);
619: return(man_body_alloc(m, ln, 0, m->last->tok));
1.1 kristaps 620:
621: err: /* Error out. */
622:
1.2 kristaps 623: m->flags |= MAN_HALT;
1.1 kristaps 624: return(0);
625: }
1.3 kristaps 626:
1.4 kristaps 627:
628: int
629: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
630: {
631: char buf[256];
632: va_list ap;
633:
634: if (NULL == man->cb.man_err)
635: return(0);
636:
637: va_start(ap, fmt);
638: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
639: va_end(ap);
640: return((*man->cb.man_err)(man->data, ln, pos, buf));
641: }
642:
643:
644: int
645: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
646: {
647: char buf[256];
648: va_list ap;
649:
650: if (NULL == man->cb.man_warn)
651: return(0);
652:
653: va_start(ap, fmt);
654: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
655: va_end(ap);
656: return((*man->cb.man_warn)(man->data, ln, pos, buf));
657: }
658:
659:
1.23 kristaps 660: int
1.27 kristaps 661: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.23 kristaps 662: {
663: const char *p;
664:
1.27 kristaps 665: p = __man_merrnames[(int)type];
1.23 kristaps 666: assert(p);
667:
668: if (iserr)
669: return(man_verr(m, line, pos, p));
670:
671: return(man_vwarn(m, line, pos, p));
1.51 kristaps 672: }
673:
674:
675: void
676: man_node_unlink(struct man *m, struct man_node *n)
677: {
678:
679: if (n->prev) {
680: n->prev->next = n->next;
681: if (m->last == n) {
682: assert(NULL == n->next);
683: m->last = n->prev;
684: m->next = MAN_NEXT_SIBLING;
685: }
686: } else {
687: n->parent->child = n->next;
688: if (m->last == n) {
689: assert(NULL == n->next);
690: m->last = n->parent;
691: m->next = MAN_NEXT_CHILD;
692: }
693: }
694:
695: if (n->next)
696: n->next->prev = n->prev;
1.23 kristaps 697: }
CVSweb