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