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