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