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