Annotation of src/usr.bin/mandoc/man.c, Revision 1.52
1.52 ! schwarze 1: /* $Id: man.c,v 1.51 2011/01/01 17:38:11 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:
63: return(MAN_HALT & m->flags ? NULL : m->first);
64: }
65:
66:
67: const struct man_meta *
68: man_meta(const struct man *m)
69: {
70:
71: return(MAN_HALT & m->flags ? NULL : &m->meta);
72: }
73:
74:
1.16 schwarze 75: void
1.1 kristaps 76: man_reset(struct man *man)
77: {
78:
79: man_free1(man);
1.16 schwarze 80: man_alloc1(man);
1.1 kristaps 81: }
82:
83:
84: void
85: man_free(struct man *man)
86: {
87:
88: man_free1(man);
89: free(man);
90: }
91:
92:
93: struct man *
1.40 schwarze 94: man_alloc(struct regset *regs, void *data, 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;
1.33 schwarze 102: p->msg = msg;
1.35 schwarze 103: p->regs = regs;
1.16 schwarze 104:
105: man_alloc1(p);
1.1 kristaps 106: return(p);
107: }
108:
109:
110: int
111: man_endparse(struct man *m)
112: {
113:
114: if (MAN_HALT & m->flags)
115: return(0);
116: else if (man_macroend(m))
117: return(1);
118: m->flags |= MAN_HALT;
119: return(0);
120: }
121:
122:
123: int
1.31 schwarze 124: man_parseln(struct man *m, int ln, char *buf, int offs)
1.1 kristaps 125: {
1.25 schwarze 126:
1.31 schwarze 127: if (MAN_HALT & m->flags)
128: return(0);
1.1 kristaps 129:
1.31 schwarze 130: return(('.' == buf[offs] || '\'' == buf[offs]) ?
131: man_pmacro(m, ln, buf, offs) :
132: man_ptext(m, ln, buf, offs));
1.1 kristaps 133: }
134:
135:
136: static void
137: man_free1(struct man *man)
138: {
139:
140: if (man->first)
1.22 schwarze 141: man_node_delete(man, man->first);
1.1 kristaps 142: if (man->meta.title)
143: free(man->meta.title);
144: if (man->meta.source)
145: free(man->meta.source);
1.34 schwarze 146: if (man->meta.rawdate)
147: free(man->meta.rawdate);
1.1 kristaps 148: if (man->meta.vol)
149: free(man->meta.vol);
1.29 schwarze 150: if (man->meta.msec)
151: free(man->meta.msec);
1.1 kristaps 152: }
153:
154:
1.16 schwarze 155: static void
1.1 kristaps 156: man_alloc1(struct man *m)
157: {
158:
1.16 schwarze 159: memset(&m->meta, 0, sizeof(struct man_meta));
1.1 kristaps 160: m->flags = 0;
1.16 schwarze 161: m->last = mandoc_calloc(1, sizeof(struct man_node));
1.1 kristaps 162: m->first = m->last;
163: m->last->type = MAN_ROOT;
1.22 schwarze 164: m->last->tok = MAN_MAX;
1.1 kristaps 165: m->next = MAN_NEXT_CHILD;
166: }
167:
168:
169: static int
170: man_node_append(struct man *man, struct man_node *p)
171: {
172:
173: assert(man->last);
174: assert(man->first);
175: assert(MAN_ROOT != p->type);
176:
177: switch (man->next) {
178: case (MAN_NEXT_SIBLING):
179: man->last->next = p;
180: p->prev = man->last;
181: p->parent = man->last->parent;
182: break;
183: case (MAN_NEXT_CHILD):
184: man->last->child = p;
185: p->parent = man->last;
186: break;
187: default:
188: abort();
189: /* NOTREACHED */
190: }
1.5 schwarze 191:
1.22 schwarze 192: assert(p->parent);
1.5 schwarze 193: p->parent->nchild++;
1.1 kristaps 194:
1.9 schwarze 195: if ( ! man_valid_pre(man, p))
196: return(0);
197:
198: switch (p->type) {
199: case (MAN_HEAD):
200: assert(MAN_BLOCK == p->parent->type);
201: p->parent->head = p;
202: break;
203: case (MAN_BODY):
204: assert(MAN_BLOCK == p->parent->type);
205: p->parent->body = p;
206: break;
207: default:
208: break;
209: }
210:
1.1 kristaps 211: man->last = p;
212:
213: switch (p->type) {
1.52 ! schwarze 214: case (MAN_TBL):
! 215: /* FALLTHROUGH */
1.1 kristaps 216: case (MAN_TEXT):
217: if ( ! man_valid_post(man))
218: return(0);
219: break;
220: default:
221: break;
222: }
223:
224: return(1);
225: }
226:
227:
228: static struct man_node *
1.22 schwarze 229: man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
1.1 kristaps 230: {
231: struct man_node *p;
232:
1.16 schwarze 233: p = mandoc_calloc(1, sizeof(struct man_node));
1.1 kristaps 234: p->line = line;
235: p->pos = pos;
236: p->type = type;
237: p->tok = tok;
238: return(p);
239: }
240:
241:
242: int
1.22 schwarze 243: man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
1.1 kristaps 244: {
245: struct man_node *p;
246:
247: p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.10 schwarze 248: if ( ! man_node_append(m, p))
249: return(0);
250: m->next = MAN_NEXT_CHILD;
251: return(1);
1.1 kristaps 252: }
253:
254:
255: int
1.22 schwarze 256: man_head_alloc(struct man *m, int line, int pos, enum mant tok)
1.9 schwarze 257: {
258: struct man_node *p;
259:
260: p = man_node_alloc(line, pos, MAN_HEAD, tok);
261: if ( ! man_node_append(m, p))
262: return(0);
263: m->next = MAN_NEXT_CHILD;
264: return(1);
265: }
266:
267:
268: int
1.22 schwarze 269: man_body_alloc(struct man *m, int line, int pos, enum mant tok)
1.9 schwarze 270: {
271: struct man_node *p;
272:
273: p = man_node_alloc(line, pos, MAN_BODY, tok);
274: if ( ! man_node_append(m, p))
275: return(0);
276: m->next = MAN_NEXT_CHILD;
277: return(1);
278: }
279:
280:
281: int
1.22 schwarze 282: man_block_alloc(struct man *m, int line, int pos, enum mant tok)
1.9 schwarze 283: {
284: struct man_node *p;
285:
286: p = man_node_alloc(line, pos, MAN_BLOCK, tok);
287: if ( ! man_node_append(m, p))
288: return(0);
289: m->next = MAN_NEXT_CHILD;
290: return(1);
291: }
292:
1.52 ! schwarze 293: static int
! 294: man_span_alloc(struct man *m, const struct tbl_span *span)
! 295: {
! 296: struct man_node *n;
! 297:
! 298: /* FIXME: grab from span */
! 299: n = man_node_alloc(0, 0, MAN_TBL, MAN_MAX);
! 300: n->span = span;
! 301:
! 302: if ( ! man_node_append(m, n))
! 303: return(0);
! 304:
! 305: m->next = MAN_NEXT_SIBLING;
! 306: return(1);
! 307: }
1.9 schwarze 308:
1.27 schwarze 309: int
310: man_word_alloc(struct man *m, int line, int pos, const char *word)
1.1 kristaps 311: {
1.10 schwarze 312: struct man_node *n;
1.27 schwarze 313: size_t sv, len;
314:
315: len = strlen(word);
1.1 kristaps 316:
1.22 schwarze 317: n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
1.16 schwarze 318: n->string = mandoc_malloc(len + 1);
1.27 schwarze 319: sv = strlcpy(n->string, word, len + 1);
1.10 schwarze 320:
321: /* Prohibit truncation. */
322: assert(sv < len + 1);
323:
324: if ( ! man_node_append(m, n))
1.1 kristaps 325: return(0);
1.27 schwarze 326:
1.10 schwarze 327: m->next = MAN_NEXT_SIBLING;
328: return(1);
329: }
330:
331:
1.22 schwarze 332: /*
333: * Free all of the resources held by a node. This does NOT unlink a
334: * node from its context; for that, see man_node_unlink().
335: */
336: static void
1.1 kristaps 337: man_node_free(struct man_node *p)
338: {
339:
340: if (p->string)
341: free(p->string);
342: free(p);
343: }
344:
345:
346: void
1.22 schwarze 347: man_node_delete(struct man *m, struct man_node *p)
1.1 kristaps 348: {
349:
1.22 schwarze 350: while (p->child)
351: man_node_delete(m, p->child);
352:
353: man_node_unlink(m, p);
1.1 kristaps 354: man_node_free(p);
355: }
356:
357:
1.52 ! schwarze 358: int
! 359: man_addspan(struct man *m, const struct tbl_span *sp)
! 360: {
! 361:
! 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:
464: 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: }