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