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