Annotation of mandoc/man.c, Revision 1.52
1.52 ! kristaps 1: /* $Id: man.c,v 1.51 2010/03/22 14:03:03 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,
70: enum man_type, int);
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.16 kristaps 245: man_node_alloc(int line, int pos, enum man_type type, int 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.30 kristaps 259: man_elem_alloc(struct man *m, int line, int pos, int 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.29 kristaps 272: man_head_alloc(struct man *m, int line, int pos, int tok)
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
285: man_body_alloc(struct man *m, int line, int pos, int tok)
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
298: man_block_alloc(struct man *m, int line, int pos, int tok)
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.31 kristaps 317: n = man_node_alloc(line, pos, MAN_TEXT, -1);
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.34 kristaps 472: int i, j, c, ppos, fl;
473: char mac[5];
474: struct man_node *n;
1.1 kristaps 475:
476: /* Comments and empties are quickly ignored. */
477:
1.29 kristaps 478: fl = m->flags;
1.11 kristaps 479:
1.46 kristaps 480: if ('\0' == buf[1])
481: return(1);
1.1 kristaps 482:
1.9 kristaps 483: i = 1;
484:
485: if (' ' == buf[i]) {
486: i++;
1.1 kristaps 487: while (buf[i] && ' ' == buf[i])
488: i++;
1.48 kristaps 489: if ('\0' == buf[i])
1.11 kristaps 490: goto out;
1.1 kristaps 491: }
492:
1.10 kristaps 493: ppos = i;
494:
1.1 kristaps 495: /* Copy the first word into a nil-terminated buffer. */
496:
1.10 kristaps 497: for (j = 0; j < 4; j++, i++) {
1.48 kristaps 498: if ('\0' == (mac[j] = buf[i]))
1.1 kristaps 499: break;
1.10 kristaps 500: else if (' ' == buf[i])
1.1 kristaps 501: break;
1.38 kristaps 502:
503: /* Check for invalid characters. */
504:
505: if (isgraph((u_char)buf[i]))
506: continue;
507: return(man_perr(m, ln, i, WNPRINT));
1.1 kristaps 508: }
509:
1.46 kristaps 510: mac[j] = '\0';
1.1 kristaps 511:
1.9 kristaps 512: if (j == 4 || j < 1) {
1.7 kristaps 513: if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.29 kristaps 514: (void)man_perr(m, ln, ppos, WMACROFORM);
1.7 kristaps 515: goto err;
516: }
1.29 kristaps 517: if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
1.7 kristaps 518: goto err;
1.12 kristaps 519: return(1);
1.7 kristaps 520: }
1.1 kristaps 521:
1.40 kristaps 522: if (MAN_MAX == (c = man_hash_find(mac))) {
1.43 kristaps 523: if ( ! macrowarn(m, ln, mac))
1.7 kristaps 524: goto err;
1.12 kristaps 525: return(1);
1.1 kristaps 526: }
527:
528: /* The macro is sane. Jump to the next word. */
529:
530: while (buf[i] && ' ' == buf[i])
531: i++;
532:
1.48 kristaps 533: /* Trailing whitespace? */
534:
535: if ('\0' == buf[i] && ' ' == buf[i - 1])
536: if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
537: goto err;
538:
1.50 kristaps 539: /*
1.51 kristaps 540: * Remove prior ELINE macro, as it's being clobbering by a new
541: * macro. Note that NSCOPED macros do not close out ELINE
542: * macros---they don't print text---so we let those slip by.
1.50 kristaps 543: */
1.34 kristaps 544:
1.51 kristaps 545: if ( ! (MAN_NSCOPED & man_macros[c].flags) &&
546: m->flags & MAN_ELINE) {
547: assert(MAN_TEXT != m->last->type);
548:
549: /*
550: * This occurs in the following construction:
551: * .B
552: * .br
553: * .B
554: * .br
555: * I hate man macros.
556: * Flat-out disallow this madness.
557: */
558: if (MAN_NSCOPED & man_macros[m->last->tok].flags)
559: return(man_perr(m, ln, ppos, WLNSCOPE));
560:
1.34 kristaps 561: n = m->last;
1.51 kristaps 562:
563: assert(n);
1.34 kristaps 564: assert(NULL == n->child);
1.37 kristaps 565: assert(0 == n->nchild);
1.51 kristaps 566:
1.34 kristaps 567: if ( ! man_nwarn(m, n, WLNSCOPE))
568: return(0);
569:
1.51 kristaps 570: man_node_unlink(m, n);
1.34 kristaps 571: man_node_free(n);
572: m->flags &= ~MAN_ELINE;
573: }
574:
1.1 kristaps 575: /* Begin recursive parse sequence. */
576:
1.29 kristaps 577: assert(man_macros[c].fp);
578:
579: if ( ! (*man_macros[c].fp)(m, c, ln, ppos, &i, buf))
1.1 kristaps 580: goto err;
581:
1.11 kristaps 582: out:
1.50 kristaps 583: /*
584: * We weren't in a block-line scope when entering the
585: * above-parsed macro, so return.
586: */
587:
588: if ( ! (MAN_BLINE & fl)) {
589: m->flags &= ~MAN_ILINE;
1.29 kristaps 590: return(1);
1.50 kristaps 591: }
592:
593: /*
594: * If we're in a block scope, then allow this macro to slip by
595: * without closing scope around it.
596: */
597:
598: if (MAN_ILINE & m->flags) {
599: m->flags &= ~MAN_ILINE;
600: return(1);
601: }
1.29 kristaps 602:
603: /*
604: * If we've opened a new next-line element scope, then return
605: * now, as the next line will close out the block scope.
606: */
607:
608: if (MAN_ELINE & m->flags)
609: return(1);
610:
611: /* Close out the block scope opened in the prior line. */
1.11 kristaps 612:
1.29 kristaps 613: assert(MAN_BLINE & m->flags);
614: m->flags &= ~MAN_BLINE;
1.11 kristaps 615:
1.29 kristaps 616: if ( ! man_unscope(m, m->last->parent))
617: return(0);
618: return(man_body_alloc(m, ln, 0, m->last->tok));
1.1 kristaps 619:
620: err: /* Error out. */
621:
1.2 kristaps 622: m->flags |= MAN_HALT;
1.1 kristaps 623: return(0);
624: }
1.3 kristaps 625:
1.4 kristaps 626:
627: int
628: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
629: {
630: char buf[256];
631: va_list ap;
632:
633: if (NULL == man->cb.man_err)
634: return(0);
635:
636: va_start(ap, fmt);
637: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
638: va_end(ap);
639: return((*man->cb.man_err)(man->data, ln, pos, buf));
640: }
641:
642:
643: int
644: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
645: {
646: char buf[256];
647: va_list ap;
648:
649: if (NULL == man->cb.man_warn)
650: return(0);
651:
652: va_start(ap, fmt);
653: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
654: va_end(ap);
655: return((*man->cb.man_warn)(man->data, ln, pos, buf));
656: }
657:
658:
1.23 kristaps 659: int
1.27 kristaps 660: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.23 kristaps 661: {
662: const char *p;
663:
1.27 kristaps 664: p = __man_merrnames[(int)type];
1.23 kristaps 665: assert(p);
666:
667: if (iserr)
668: return(man_verr(m, line, pos, p));
669:
670: return(man_vwarn(m, line, pos, p));
1.51 kristaps 671: }
672:
673:
674: void
675: man_node_unlink(struct man *m, struct man_node *n)
676: {
677:
678: if (n->prev) {
679: n->prev->next = n->next;
680: if (m->last == n) {
681: assert(NULL == n->next);
682: m->last = n->prev;
683: m->next = MAN_NEXT_SIBLING;
684: }
685: } else {
686: n->parent->child = n->next;
687: if (m->last == n) {
688: assert(NULL == n->next);
689: m->last = n->parent;
690: m->next = MAN_NEXT_CHILD;
691: }
692: }
693:
694: if (n->next)
695: n->next->prev = n->prev;
1.23 kristaps 696: }
CVSweb