Annotation of src/usr.bin/mandoc/man.c, Revision 1.53
1.53 ! schwarze 1: /* $Id: man.c,v 1.52 2011/01/04 22:28:17 schwarze Exp $ */
1.1 kristaps 2: /*
1.52 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 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.50 schwarze 35: "RI", "na", "sp", "nf",
36: "fi", "RE", "RS", "DT",
37: "UC", "PD", "AT", "in",
1.52 schwarze 38: "ft"
1.1 kristaps 39: };
40:
41: const char * const *man_macronames = __man_macronames;
42:
43: static struct man_node *man_node_alloc(int, int,
1.22 schwarze 44: enum man_type, enum mant);
1.1 kristaps 45: static int man_node_append(struct man *,
46: struct man_node *);
1.52 schwarze 47: static int man_span_alloc(struct man *,
48: const struct tbl_span *);
1.22 schwarze 49: static void man_node_free(struct man_node *);
50: static void man_node_unlink(struct man *,
51: struct man_node *);
1.31 schwarze 52: static int man_ptext(struct man *, int, char *, int);
53: static int man_pmacro(struct man *, int, char *, int);
1.1 kristaps 54: static void man_free1(struct man *);
1.16 schwarze 55: static void man_alloc1(struct man *);
1.52 schwarze 56: static int man_descope(struct man *, int, int);
1.1 kristaps 57:
58:
59: const struct man_node *
60: man_node(const struct man *m)
61: {
62:
1.53 ! schwarze 63: assert( ! (MAN_HALT & m->flags));
! 64: return(m->first);
1.1 kristaps 65: }
66:
67:
68: const struct man_meta *
69: man_meta(const struct man *m)
70: {
71:
1.53 ! schwarze 72: assert( ! (MAN_HALT & m->flags));
! 73: return(&m->meta);
1.1 kristaps 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:
1.53 ! schwarze 116: assert( ! (MAN_HALT & m->flags));
! 117: if (man_macroend(m))
1.1 kristaps 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.53 ! schwarze 128: assert( ! (MAN_HALT & m->flags));
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) {
1.52 schwarze 213: case (MAN_TBL):
214: /* FALLTHROUGH */
1.1 kristaps 215: case (MAN_TEXT):
216: if ( ! man_valid_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:
1.52 schwarze 292: static int
293: man_span_alloc(struct man *m, const struct tbl_span *span)
294: {
295: struct man_node *n;
296:
297: /* FIXME: grab from span */
298: n = man_node_alloc(0, 0, MAN_TBL, MAN_MAX);
299: n->span = span;
300:
301: if ( ! man_node_append(m, n))
302: return(0);
303:
304: m->next = MAN_NEXT_SIBLING;
305: return(1);
306: }
1.9 schwarze 307:
1.27 schwarze 308: int
309: man_word_alloc(struct man *m, int line, int pos, const char *word)
1.1 kristaps 310: {
1.10 schwarze 311: struct man_node *n;
1.27 schwarze 312: size_t sv, len;
313:
314: len = strlen(word);
1.1 kristaps 315:
1.22 schwarze 316: n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
1.16 schwarze 317: n->string = mandoc_malloc(len + 1);
1.27 schwarze 318: sv = strlcpy(n->string, word, len + 1);
1.10 schwarze 319:
320: /* Prohibit truncation. */
321: assert(sv < len + 1);
322:
323: if ( ! man_node_append(m, n))
1.1 kristaps 324: return(0);
1.27 schwarze 325:
1.10 schwarze 326: m->next = MAN_NEXT_SIBLING;
327: return(1);
328: }
329:
330:
1.22 schwarze 331: /*
332: * Free all of the resources held by a node. This does NOT unlink a
333: * node from its context; for that, see man_node_unlink().
334: */
335: static void
1.1 kristaps 336: man_node_free(struct man_node *p)
337: {
338:
339: if (p->string)
340: free(p->string);
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:
1.52 schwarze 357: int
358: man_addspan(struct man *m, const struct tbl_span *sp)
359: {
360:
1.53 ! schwarze 361: assert( ! (MAN_HALT & m->flags));
1.52 schwarze 362: if ( ! man_span_alloc(m, sp))
363: return(0);
364: return(man_descope(m, 0, 0));
365: }
366:
367: static int
368: man_descope(struct man *m, int line, int offs)
369: {
370: /*
371: * Co-ordinate what happens with having a next-line scope open:
372: * first close out the element scope (if applicable), then close
373: * out the block scope (also if applicable).
374: */
375:
376: if (MAN_ELINE & m->flags) {
377: m->flags &= ~MAN_ELINE;
378: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
379: return(0);
380: }
381:
382: if ( ! (MAN_BLINE & m->flags))
383: return(1);
384: m->flags &= ~MAN_BLINE;
385:
386: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
387: return(0);
388: return(man_body_alloc(m, line, offs, m->last->tok));
389: }
390:
391:
1.1 kristaps 392: static int
1.31 schwarze 393: man_ptext(struct man *m, int line, char *buf, int offs)
1.1 kristaps 394: {
1.27 schwarze 395: int i;
1.26 schwarze 396:
397: /* Ignore bogus comments. */
398:
1.31 schwarze 399: if ('\\' == buf[offs] &&
400: '.' == buf[offs + 1] &&
1.51 schwarze 401: '"' == buf[offs + 2]) {
402: man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT);
403: return(1);
404: }
1.10 schwarze 405:
406: /* Literal free-form text whitespace is preserved. */
407:
408: if (MAN_LITERAL & m->flags) {
1.31 schwarze 409: if ( ! man_word_alloc(m, line, offs, buf + offs))
1.10 schwarze 410: return(0);
1.52 schwarze 411: return(man_descope(m, line, offs));
1.10 schwarze 412: }
413:
1.27 schwarze 414: /* Pump blank lines directly into the backend. */
1.10 schwarze 415:
1.31 schwarze 416: for (i = offs; ' ' == buf[i]; i++)
1.10 schwarze 417: /* Skip leading whitespace. */ ;
1.18 schwarze 418:
419: if ('\0' == buf[i]) {
1.27 schwarze 420: /* Allocate a blank entry. */
1.31 schwarze 421: if ( ! man_word_alloc(m, line, offs, ""))
1.10 schwarze 422: return(0);
1.52 schwarze 423: return(man_descope(m, line, offs));
1.10 schwarze 424: }
1.1 kristaps 425:
1.27 schwarze 426: /*
427: * Warn if the last un-escaped character is whitespace. Then
428: * strip away the remaining spaces (tabs stay!).
429: */
1.18 schwarze 430:
1.27 schwarze 431: i = (int)strlen(buf);
432: assert(i);
1.18 schwarze 433:
1.27 schwarze 434: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
435: if (i > 1 && '\\' != buf[i - 2])
1.51 schwarze 436: man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE);
1.18 schwarze 437:
1.27 schwarze 438: for (--i; i && ' ' == buf[i]; i--)
439: /* Spin back to non-space. */ ;
1.10 schwarze 440:
1.27 schwarze 441: /* Jump ahead of escaped whitespace. */
442: i += '\\' == buf[i] ? 2 : 1;
1.18 schwarze 443:
1.27 schwarze 444: buf[i] = '\0';
1.10 schwarze 445: }
1.9 schwarze 446:
1.31 schwarze 447: if ( ! man_word_alloc(m, line, offs, buf + offs))
1.1 kristaps 448: return(0);
1.28 schwarze 449:
450: /*
451: * End-of-sentence check. If the last character is an unescaped
452: * EOS character, then flag the node as being the end of a
453: * sentence. The front-end will know how to interpret this.
454: */
455:
456: assert(i);
1.37 schwarze 457: if (mandoc_eos(buf, (size_t)i, 0))
1.28 schwarze 458: m->last->flags |= MAN_EOS;
1.10 schwarze 459:
1.52 schwarze 460: return(man_descope(m, line, offs));
1.1 kristaps 461: }
462:
463:
1.53 ! schwarze 464: static int
1.31 schwarze 465: man_pmacro(struct man *m, int ln, char *buf, int offs)
1.1 kristaps 466: {
1.24 schwarze 467: int i, j, ppos;
1.22 schwarze 468: enum mant tok;
1.10 schwarze 469: char mac[5];
470: struct man_node *n;
1.1 kristaps 471:
472: /* Comments and empties are quickly ignored. */
473:
1.31 schwarze 474: offs++;
475:
476: if ('\0' == buf[offs])
1.17 schwarze 477: return(1);
1.1 kristaps 478:
1.31 schwarze 479: i = offs;
1.1 kristaps 480:
1.23 schwarze 481: /*
482: * Skip whitespace between the control character and initial
483: * text. "Whitespace" is both spaces and tabs.
484: */
1.27 schwarze 485:
1.23 schwarze 486: if (' ' == buf[i] || '\t' == buf[i]) {
1.1 kristaps 487: i++;
1.23 schwarze 488: while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1 kristaps 489: i++;
1.18 schwarze 490: if ('\0' == buf[i])
1.1 kristaps 491: goto out;
492: }
493:
494: ppos = i;
495:
1.39 schwarze 496: /*
497: * Copy the first word into a nil-terminated buffer.
498: * Stop copying when a tab, space, or eoln is encountered.
499: */
1.1 kristaps 500:
1.39 schwarze 501: j = 0;
502: while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
503: mac[j++] = buf[i++];
1.17 schwarze 504: mac[j] = '\0';
1.1 kristaps 505:
1.40 schwarze 506: tok = (j > 0 && j < 4) ? man_hash_find(mac) : MAN_MAX;
507: if (MAN_MAX == tok) {
1.44 schwarze 508: man_vmsg(m, MANDOCERR_MACRO, ln, ppos, "%s", buf + ppos - 1);
1.1 kristaps 509: return(1);
510: }
511:
512: /* The macro is sane. Jump to the next word. */
513:
514: while (buf[i] && ' ' == buf[i])
515: i++;
1.18 schwarze 516:
1.27 schwarze 517: /*
518: * Trailing whitespace. Note that tabs are allowed to be passed
519: * into the parser as "text", so we only warn about spaces here.
520: */
1.18 schwarze 521:
522: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.51 schwarze 523: man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE);
1.1 kristaps 524:
1.21 schwarze 525: /*
1.46 schwarze 526: * Remove prior ELINE macro, as it's being clobbered by a new
1.21 schwarze 527: * macro. Note that NSCOPED macros do not close out ELINE
528: * macros---they don't print text---so we let those slip by.
529: */
530:
1.22 schwarze 531: if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.21 schwarze 532: m->flags & MAN_ELINE) {
1.10 schwarze 533: n = m->last;
1.46 schwarze 534: assert(MAN_TEXT != n->type);
1.21 schwarze 535:
1.49 schwarze 536: /* Remove repeated NSCOPED macros causing ELINE. */
537:
1.46 schwarze 538: if (MAN_NSCOPED & man_macros[n->tok].flags)
539: n = n->parent;
1.21 schwarze 540:
1.46 schwarze 541: man_vmsg(m, MANDOCERR_LINESCOPE, n->line, n->pos,
1.49 schwarze 542: "%s", man_macronames[n->tok]);
1.10 schwarze 543:
1.22 schwarze 544: man_node_delete(m, n);
1.10 schwarze 545: m->flags &= ~MAN_ELINE;
546: }
547:
1.24 schwarze 548: /*
549: * Save the fact that we're in the next-line for a block. In
550: * this way, embedded roff instructions can "remember" state
551: * when they exit.
552: */
553:
554: if (MAN_BLINE & m->flags)
555: m->flags |= MAN_BPLINE;
556:
557: /* Call to handler... */
1.1 kristaps 558:
1.22 schwarze 559: assert(man_macros[tok].fp);
560: if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
1.1 kristaps 561: goto err;
562:
563: out:
1.21 schwarze 564: /*
565: * We weren't in a block-line scope when entering the
566: * above-parsed macro, so return.
567: */
568:
1.24 schwarze 569: if ( ! (MAN_BPLINE & m->flags)) {
1.21 schwarze 570: m->flags &= ~MAN_ILINE;
1.9 schwarze 571: return(1);
1.21 schwarze 572: }
1.24 schwarze 573: m->flags &= ~MAN_BPLINE;
1.21 schwarze 574:
575: /*
576: * If we're in a block scope, then allow this macro to slip by
577: * without closing scope around it.
578: */
579:
580: if (MAN_ILINE & m->flags) {
581: m->flags &= ~MAN_ILINE;
582: return(1);
583: }
1.9 schwarze 584:
585: /*
586: * If we've opened a new next-line element scope, then return
587: * now, as the next line will close out the block scope.
588: */
589:
590: if (MAN_ELINE & m->flags)
591: return(1);
592:
593: /* Close out the block scope opened in the prior line. */
1.1 kristaps 594:
1.9 schwarze 595: assert(MAN_BLINE & m->flags);
596: m->flags &= ~MAN_BLINE;
1.1 kristaps 597:
1.33 schwarze 598: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.9 schwarze 599: return(0);
1.31 schwarze 600: return(man_body_alloc(m, ln, offs, m->last->tok));
1.1 kristaps 601:
602: err: /* Error out. */
603:
604: m->flags |= MAN_HALT;
605: return(0);
606: }
607:
608:
609: int
1.33 schwarze 610: man_vmsg(struct man *man, enum mandocerr t,
611: int ln, int pos, const char *fmt, ...)
1.1 kristaps 612: {
613: char buf[256];
614: va_list ap;
615:
616: va_start(ap, fmt);
1.33 schwarze 617: vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1 kristaps 618: va_end(ap);
1.33 schwarze 619: return((*man->msg)(t, man->data, ln, pos, buf));
1.21 schwarze 620: }
621:
622:
1.22 schwarze 623: /*
624: * Unlink a node from its context. If "m" is provided, the last parse
625: * point will also be adjusted accordingly.
626: */
627: static void
1.21 schwarze 628: man_node_unlink(struct man *m, struct man_node *n)
629: {
630:
1.22 schwarze 631: /* Adjust siblings. */
632:
633: if (n->prev)
1.21 schwarze 634: n->prev->next = n->next;
1.22 schwarze 635: if (n->next)
636: n->next->prev = n->prev;
637:
638: /* Adjust parent. */
639:
640: if (n->parent) {
641: n->parent->nchild--;
642: if (n->parent->child == n)
643: n->parent->child = n->prev ? n->prev : n->next;
644: }
645:
646: /* Adjust parse point, if applicable. */
647:
648: if (m && m->last == n) {
649: /*XXX: this can occur when bailing from validation. */
650: /*assert(NULL == n->next);*/
651: if (n->prev) {
1.21 schwarze 652: m->last = n->prev;
653: m->next = MAN_NEXT_SIBLING;
1.22 schwarze 654: } else {
1.21 schwarze 655: m->last = n->parent;
656: m->next = MAN_NEXT_CHILD;
657: }
658: }
659:
1.22 schwarze 660: if (m && m->first == n)
661: m->first = NULL;
1.4 schwarze 662: }