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