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