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