Annotation of src/usr.bin/mandoc/man.c, Revision 1.45
1.45 ! schwarze 1: /* $Id: man.c,v 1.44 2010/11/28 19:35:33 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.32 schwarze 42: "Sp", "Vb", "Ve", "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: if ( ! man_action_post(man))
232: return(0);
233: break;
234: default:
235: break;
236: }
237:
238: return(1);
239: }
240:
241:
242: static struct man_node *
1.22 schwarze 243: man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
1.1 kristaps 244: {
245: struct man_node *p;
246:
1.16 schwarze 247: p = mandoc_calloc(1, sizeof(struct man_node));
1.1 kristaps 248: p->line = line;
249: p->pos = pos;
250: p->type = type;
251: p->tok = tok;
252: return(p);
253: }
254:
255:
256: int
1.22 schwarze 257: man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
1.1 kristaps 258: {
259: struct man_node *p;
260:
261: p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.10 schwarze 262: if ( ! man_node_append(m, p))
263: return(0);
264: m->next = MAN_NEXT_CHILD;
265: return(1);
1.1 kristaps 266: }
267:
268:
269: int
1.22 schwarze 270: man_head_alloc(struct man *m, int line, int pos, enum mant tok)
1.9 schwarze 271: {
272: struct man_node *p;
273:
274: p = man_node_alloc(line, pos, MAN_HEAD, tok);
275: if ( ! man_node_append(m, p))
276: return(0);
277: m->next = MAN_NEXT_CHILD;
278: return(1);
279: }
280:
281:
282: int
1.22 schwarze 283: man_body_alloc(struct man *m, int line, int pos, enum mant tok)
1.9 schwarze 284: {
285: struct man_node *p;
286:
287: p = man_node_alloc(line, pos, MAN_BODY, tok);
288: if ( ! man_node_append(m, p))
289: return(0);
290: m->next = MAN_NEXT_CHILD;
291: return(1);
292: }
293:
294:
295: int
1.22 schwarze 296: man_block_alloc(struct man *m, int line, int pos, enum mant tok)
1.9 schwarze 297: {
298: struct man_node *p;
299:
300: p = man_node_alloc(line, pos, MAN_BLOCK, tok);
301: if ( ! man_node_append(m, p))
302: return(0);
303: m->next = MAN_NEXT_CHILD;
304: return(1);
305: }
306:
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);
1.41 schwarze 341: if (p->data.TS)
342: tbl_free(p->data.TS);
1.1 kristaps 343: free(p);
344: }
345:
346:
347: void
1.22 schwarze 348: man_node_delete(struct man *m, struct man_node *p)
1.1 kristaps 349: {
350:
1.22 schwarze 351: while (p->child)
352: man_node_delete(m, p->child);
353:
354: man_node_unlink(m, p);
1.1 kristaps 355: man_node_free(p);
356: }
357:
358:
359: static int
1.31 schwarze 360: man_ptext(struct man *m, int line, char *buf, int offs)
1.1 kristaps 361: {
1.27 schwarze 362: int i;
1.26 schwarze 363:
364: /* Ignore bogus comments. */
365:
1.31 schwarze 366: if ('\\' == buf[offs] &&
367: '.' == buf[offs + 1] &&
368: '"' == buf[offs + 2])
1.33 schwarze 369: return(man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.10 schwarze 370:
371: /* Literal free-form text whitespace is preserved. */
372:
373: if (MAN_LITERAL & m->flags) {
1.31 schwarze 374: if ( ! man_word_alloc(m, line, offs, buf + offs))
1.10 schwarze 375: return(0);
376: goto descope;
377: }
378:
1.27 schwarze 379: /* Pump blank lines directly into the backend. */
1.10 schwarze 380:
1.31 schwarze 381: for (i = offs; ' ' == buf[i]; i++)
1.10 schwarze 382: /* Skip leading whitespace. */ ;
1.18 schwarze 383:
384: if ('\0' == buf[i]) {
1.27 schwarze 385: /* Allocate a blank entry. */
1.31 schwarze 386: if ( ! man_word_alloc(m, line, offs, ""))
1.10 schwarze 387: return(0);
388: goto descope;
389: }
1.1 kristaps 390:
1.27 schwarze 391: /*
392: * Warn if the last un-escaped character is whitespace. Then
393: * strip away the remaining spaces (tabs stay!).
394: */
1.18 schwarze 395:
1.27 schwarze 396: i = (int)strlen(buf);
397: assert(i);
1.18 schwarze 398:
1.27 schwarze 399: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
400: if (i > 1 && '\\' != buf[i - 2])
1.33 schwarze 401: if ( ! man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE))
1.18 schwarze 402: return(0);
403:
1.27 schwarze 404: for (--i; i && ' ' == buf[i]; i--)
405: /* Spin back to non-space. */ ;
1.10 schwarze 406:
1.27 schwarze 407: /* Jump ahead of escaped whitespace. */
408: i += '\\' == buf[i] ? 2 : 1;
1.18 schwarze 409:
1.27 schwarze 410: buf[i] = '\0';
1.10 schwarze 411: }
1.9 schwarze 412:
1.31 schwarze 413: if ( ! man_word_alloc(m, line, offs, buf + offs))
1.1 kristaps 414: return(0);
1.28 schwarze 415:
416: /*
417: * End-of-sentence check. If the last character is an unescaped
418: * EOS character, then flag the node as being the end of a
419: * sentence. The front-end will know how to interpret this.
420: */
421:
422: assert(i);
1.37 schwarze 423: if (mandoc_eos(buf, (size_t)i, 0))
1.28 schwarze 424: m->last->flags |= MAN_EOS;
1.10 schwarze 425:
426: descope:
1.1 kristaps 427: /*
1.9 schwarze 428: * Co-ordinate what happens with having a next-line scope open:
429: * first close out the element scope (if applicable), then close
430: * out the block scope (also if applicable).
1.1 kristaps 431: */
432:
1.9 schwarze 433: if (MAN_ELINE & m->flags) {
434: m->flags &= ~MAN_ELINE;
1.33 schwarze 435: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.9 schwarze 436: return(0);
437: }
438:
439: if ( ! (MAN_BLINE & m->flags))
1.1 kristaps 440: return(1);
1.9 schwarze 441: m->flags &= ~MAN_BLINE;
1.1 kristaps 442:
1.33 schwarze 443: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.1 kristaps 444: return(0);
1.31 schwarze 445: return(man_body_alloc(m, line, offs, m->last->tok));
1.1 kristaps 446: }
447:
448:
449: int
1.31 schwarze 450: man_pmacro(struct man *m, int ln, char *buf, int offs)
1.1 kristaps 451: {
1.24 schwarze 452: int i, j, ppos;
1.22 schwarze 453: enum mant tok;
1.10 schwarze 454: char mac[5];
455: struct man_node *n;
1.1 kristaps 456:
457: /* Comments and empties are quickly ignored. */
458:
1.31 schwarze 459: offs++;
460:
461: if ('\0' == buf[offs])
1.17 schwarze 462: return(1);
1.1 kristaps 463:
1.31 schwarze 464: i = offs;
1.1 kristaps 465:
1.23 schwarze 466: /*
467: * Skip whitespace between the control character and initial
468: * text. "Whitespace" is both spaces and tabs.
469: */
1.27 schwarze 470:
1.23 schwarze 471: if (' ' == buf[i] || '\t' == buf[i]) {
1.1 kristaps 472: i++;
1.23 schwarze 473: while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1 kristaps 474: i++;
1.18 schwarze 475: if ('\0' == buf[i])
1.1 kristaps 476: goto out;
477: }
478:
479: ppos = i;
480:
1.39 schwarze 481: /*
482: * Copy the first word into a nil-terminated buffer.
483: * Stop copying when a tab, space, or eoln is encountered.
484: */
1.1 kristaps 485:
1.39 schwarze 486: j = 0;
487: while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
488: mac[j++] = buf[i++];
1.17 schwarze 489: mac[j] = '\0';
1.1 kristaps 490:
1.40 schwarze 491: tok = (j > 0 && j < 4) ? man_hash_find(mac) : MAN_MAX;
492: if (MAN_MAX == tok) {
1.44 schwarze 493: man_vmsg(m, MANDOCERR_MACRO, ln, ppos, "%s", buf + ppos - 1);
1.1 kristaps 494: return(1);
495: }
496:
497: /* The macro is sane. Jump to the next word. */
498:
499: while (buf[i] && ' ' == buf[i])
500: i++;
1.18 schwarze 501:
1.27 schwarze 502: /*
503: * Trailing whitespace. Note that tabs are allowed to be passed
504: * into the parser as "text", so we only warn about spaces here.
505: */
1.18 schwarze 506:
507: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.33 schwarze 508: if ( ! man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.18 schwarze 509: goto err;
1.1 kristaps 510:
1.21 schwarze 511: /*
512: * Remove prior ELINE macro, as it's being clobbering by a new
513: * macro. Note that NSCOPED macros do not close out ELINE
514: * macros---they don't print text---so we let those slip by.
515: */
516:
1.22 schwarze 517: if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.21 schwarze 518: m->flags & MAN_ELINE) {
519: assert(MAN_TEXT != m->last->type);
520:
521: /*
522: * This occurs in the following construction:
523: * .B
524: * .br
525: * .B
526: * .br
527: * I hate man macros.
528: * Flat-out disallow this madness.
529: */
1.33 schwarze 530: if (MAN_NSCOPED & man_macros[m->last->tok].flags) {
531: man_pmsg(m, ln, ppos, MANDOCERR_SYNTLINESCOPE);
532: return(0);
533: }
1.10 schwarze 534:
535: n = m->last;
1.21 schwarze 536:
537: assert(n);
1.10 schwarze 538: assert(NULL == n->child);
539: assert(0 == n->nchild);
1.21 schwarze 540:
1.33 schwarze 541: if ( ! man_nmsg(m, n, MANDOCERR_LINESCOPE))
1.10 schwarze 542: return(0);
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: }