Annotation of mandoc/man.c, Revision 1.88
1.88 ! kristaps 1: /* $Id: man.c,v 1.87 2010/08/20 01:02:07 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);
1.1 kristaps 219: break;
220: default:
221: break;
222: }
223:
224: return(1);
225: }
226:
227:
228: static struct man_node *
1.53 kristaps 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.45 kristaps 233: p = mandoc_calloc(1, sizeof(struct man_node));
1.1 kristaps 234: p->line = line;
235: p->pos = pos;
236: p->type = type;
1.16 kristaps 237: p->tok = tok;
1.1 kristaps 238: return(p);
239: }
240:
241:
242: int
1.53 kristaps 243: man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
1.1 kristaps 244: {
245: struct man_node *p;
246:
1.16 kristaps 247: p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.30 kristaps 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.53 kristaps 256: man_head_alloc(struct man *m, int line, int pos, enum mant tok)
1.29 kristaps 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.53 kristaps 269: man_body_alloc(struct man *m, int line, int pos, enum mant tok)
1.29 kristaps 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.53 kristaps 282: man_block_alloc(struct man *m, int line, int pos, enum mant tok)
1.29 kristaps 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:
293:
1.61 kristaps 294: int
295: man_word_alloc(struct man *m, int line, int pos, const char *word)
1.1 kristaps 296: {
1.31 kristaps 297: struct man_node *n;
1.61 kristaps 298: size_t sv, len;
299:
300: len = strlen(word);
1.1 kristaps 301:
1.53 kristaps 302: n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
1.45 kristaps 303: n->string = mandoc_malloc(len + 1);
1.61 kristaps 304: sv = strlcpy(n->string, word, len + 1);
1.31 kristaps 305:
306: /* Prohibit truncation. */
307: assert(sv < len + 1);
308:
309: if ( ! man_node_append(m, n))
1.30 kristaps 310: return(0);
1.61 kristaps 311:
1.30 kristaps 312: m->next = MAN_NEXT_SIBLING;
313: return(1);
1.1 kristaps 314: }
315:
316:
1.54 kristaps 317: /*
318: * Free all of the resources held by a node. This does NOT unlink a
319: * node from its context; for that, see man_node_unlink().
320: */
321: static void
1.1 kristaps 322: man_node_free(struct man_node *p)
323: {
324:
325: if (p->string)
326: free(p->string);
327: free(p);
328: }
329:
330:
331: void
1.54 kristaps 332: man_node_delete(struct man *m, struct man_node *p)
1.1 kristaps 333: {
334:
1.54 kristaps 335: while (p->child)
336: man_node_delete(m, p->child);
337:
338: man_node_unlink(m, p);
1.1 kristaps 339: man_node_free(p);
340: }
341:
342:
343: static int
1.72 kristaps 344: man_ptext(struct man *m, int line, char *buf, int offs)
1.1 kristaps 345: {
1.61 kristaps 346: int i;
1.60 kristaps 347:
348: /* Ignore bogus comments. */
349:
1.72 kristaps 350: if ('\\' == buf[offs] &&
351: '.' == buf[offs + 1] &&
352: '"' == buf[offs + 2])
1.74 kristaps 353: return(man_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.31 kristaps 354:
1.35 kristaps 355: /* Literal free-form text whitespace is preserved. */
356:
357: if (MAN_LITERAL & m->flags) {
1.72 kristaps 358: if ( ! man_word_alloc(m, line, offs, buf + offs))
1.35 kristaps 359: return(0);
360: goto descope;
361: }
362:
1.61 kristaps 363: /* Pump blank lines directly into the backend. */
1.31 kristaps 364:
1.72 kristaps 365: for (i = offs; ' ' == buf[i]; i++)
1.31 kristaps 366: /* Skip leading whitespace. */ ;
1.48 kristaps 367:
1.49 kristaps 368: if ('\0' == buf[i]) {
1.61 kristaps 369: /* Allocate a blank entry. */
1.72 kristaps 370: if ( ! man_word_alloc(m, line, offs, ""))
1.31 kristaps 371: return(0);
372: goto descope;
373: }
374:
1.63 kristaps 375: /*
376: * Warn if the last un-escaped character is whitespace. Then
377: * strip away the remaining spaces (tabs stay!).
378: */
1.29 kristaps 379:
1.61 kristaps 380: i = (int)strlen(buf);
381: assert(i);
1.49 kristaps 382:
1.63 kristaps 383: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64 kristaps 384: if (i > 1 && '\\' != buf[i - 2])
1.74 kristaps 385: if ( ! man_pmsg(m, line, i - 1, MANDOCERR_EOLNSPACE))
1.49 kristaps 386: return(0);
1.63 kristaps 387:
388: for (--i; i && ' ' == buf[i]; i--)
389: /* Spin back to non-space. */ ;
390:
391: /* Jump ahead of escaped whitespace. */
392: i += '\\' == buf[i] ? 2 : 1;
393:
394: buf[i] = '\0';
395: }
1.49 kristaps 396:
1.72 kristaps 397: if ( ! man_word_alloc(m, line, offs, buf + offs))
1.1 kristaps 398: return(0);
1.65 kristaps 399:
400: /*
401: * End-of-sentence check. If the last character is an unescaped
402: * EOS character, then flag the node as being the end of a
403: * sentence. The front-end will know how to interpret this.
404: */
1.67 kristaps 405:
1.65 kristaps 406: assert(i);
1.83 schwarze 407: if (mandoc_eos(buf, (size_t)i, 0))
1.65 kristaps 408: m->last->flags |= MAN_EOS;
1.31 kristaps 409:
410: descope:
1.11 kristaps 411: /*
1.29 kristaps 412: * Co-ordinate what happens with having a next-line scope open:
413: * first close out the element scope (if applicable), then close
414: * out the block scope (also if applicable).
1.11 kristaps 415: */
416:
1.29 kristaps 417: if (MAN_ELINE & m->flags) {
418: m->flags &= ~MAN_ELINE;
1.74 kristaps 419: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.29 kristaps 420: return(0);
421: }
422:
423: if ( ! (MAN_BLINE & m->flags))
1.11 kristaps 424: return(1);
1.29 kristaps 425: m->flags &= ~MAN_BLINE;
1.11 kristaps 426:
1.74 kristaps 427: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.11 kristaps 428: return(0);
1.72 kristaps 429: return(man_body_alloc(m, line, offs, m->last->tok));
1.1 kristaps 430: }
431:
432:
433: int
1.79 kristaps 434: man_pmacro(struct man *m, int ln, char *buf, int offs)
1.1 kristaps 435: {
1.58 kristaps 436: int i, j, ppos;
1.53 kristaps 437: enum mant tok;
1.34 kristaps 438: char mac[5];
439: struct man_node *n;
1.1 kristaps 440:
441: /* Comments and empties are quickly ignored. */
442:
1.72 kristaps 443: offs++;
444:
445: if ('\0' == buf[offs])
1.46 kristaps 446: return(1);
1.1 kristaps 447:
1.72 kristaps 448: i = offs;
1.9 kristaps 449:
1.56 kristaps 450: /*
451: * Skip whitespace between the control character and initial
452: * text. "Whitespace" is both spaces and tabs.
453: */
1.62 kristaps 454:
1.57 kristaps 455: if (' ' == buf[i] || '\t' == buf[i]) {
1.9 kristaps 456: i++;
1.56 kristaps 457: while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1 kristaps 458: i++;
1.48 kristaps 459: if ('\0' == buf[i])
1.11 kristaps 460: goto out;
1.1 kristaps 461: }
462:
1.10 kristaps 463: ppos = i;
464:
1.86 schwarze 465: /*
466: * Copy the first word into a nil-terminated buffer.
467: * Stop copying when a tab, space, or eoln is encountered.
468: */
1.1 kristaps 469:
1.86 schwarze 470: j = 0;
471: while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
472: mac[j++] = buf[i++];
1.46 kristaps 473: mac[j] = '\0';
1.1 kristaps 474:
1.87 schwarze 475: tok = (j > 0 && j < 4) ? man_hash_find(mac) : MAN_MAX;
476: if (MAN_MAX == tok) {
477: man_vmsg(m, MANDOCERR_MACRO, ln, ppos,
478: "unknown macro: %s%s",
479: buf, strlen(buf) > 3 ? "..." : "");
1.12 kristaps 480: return(1);
1.1 kristaps 481: }
482:
483: /* The macro is sane. Jump to the next word. */
484:
485: while (buf[i] && ' ' == buf[i])
486: i++;
487:
1.62 kristaps 488: /*
489: * Trailing whitespace. Note that tabs are allowed to be passed
490: * into the parser as "text", so we only warn about spaces here.
491: */
1.48 kristaps 492:
493: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.74 kristaps 494: if ( ! man_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.48 kristaps 495: goto err;
496:
1.50 kristaps 497: /*
1.51 kristaps 498: * Remove prior ELINE macro, as it's being clobbering by a new
499: * macro. Note that NSCOPED macros do not close out ELINE
500: * macros---they don't print text---so we let those slip by.
1.50 kristaps 501: */
1.34 kristaps 502:
1.53 kristaps 503: if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.51 kristaps 504: m->flags & MAN_ELINE) {
505: assert(MAN_TEXT != m->last->type);
506:
507: /*
508: * This occurs in the following construction:
509: * .B
510: * .br
511: * .B
512: * .br
513: * I hate man macros.
514: * Flat-out disallow this madness.
515: */
1.74 kristaps 516: if (MAN_NSCOPED & man_macros[m->last->tok].flags) {
517: man_pmsg(m, ln, ppos, MANDOCERR_SYNTLINESCOPE);
518: return(0);
519: }
1.51 kristaps 520:
1.34 kristaps 521: n = m->last;
1.51 kristaps 522:
523: assert(n);
1.34 kristaps 524: assert(NULL == n->child);
1.37 kristaps 525: assert(0 == n->nchild);
1.51 kristaps 526:
1.74 kristaps 527: if ( ! man_nmsg(m, n, MANDOCERR_LINESCOPE))
1.34 kristaps 528: return(0);
529:
1.54 kristaps 530: man_node_delete(m, n);
1.34 kristaps 531: m->flags &= ~MAN_ELINE;
532: }
533:
1.59 kristaps 534: /*
535: * Save the fact that we're in the next-line for a block. In
536: * this way, embedded roff instructions can "remember" state
537: * when they exit.
538: */
539:
540: if (MAN_BLINE & m->flags)
541: m->flags |= MAN_BPLINE;
542:
543: /* Call to handler... */
1.1 kristaps 544:
1.53 kristaps 545: assert(man_macros[tok].fp);
1.79 kristaps 546: if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
1.1 kristaps 547: goto err;
548:
1.11 kristaps 549: out:
1.50 kristaps 550: /*
551: * We weren't in a block-line scope when entering the
552: * above-parsed macro, so return.
553: */
554:
1.58 kristaps 555: if ( ! (MAN_BPLINE & m->flags)) {
1.50 kristaps 556: m->flags &= ~MAN_ILINE;
1.29 kristaps 557: return(1);
1.50 kristaps 558: }
1.58 kristaps 559: m->flags &= ~MAN_BPLINE;
1.50 kristaps 560:
561: /*
562: * If we're in a block scope, then allow this macro to slip by
563: * without closing scope around it.
564: */
565:
566: if (MAN_ILINE & m->flags) {
567: m->flags &= ~MAN_ILINE;
568: return(1);
569: }
1.29 kristaps 570:
571: /*
572: * If we've opened a new next-line element scope, then return
573: * now, as the next line will close out the block scope.
574: */
575:
576: if (MAN_ELINE & m->flags)
577: return(1);
578:
579: /* Close out the block scope opened in the prior line. */
1.11 kristaps 580:
1.29 kristaps 581: assert(MAN_BLINE & m->flags);
582: m->flags &= ~MAN_BLINE;
1.11 kristaps 583:
1.74 kristaps 584: if ( ! man_unscope(m, m->last->parent, MANDOCERR_MAX))
1.29 kristaps 585: return(0);
1.72 kristaps 586: return(man_body_alloc(m, ln, offs, m->last->tok));
1.1 kristaps 587:
588: err: /* Error out. */
589:
1.2 kristaps 590: m->flags |= MAN_HALT;
1.1 kristaps 591: return(0);
592: }
1.3 kristaps 593:
1.4 kristaps 594:
595: int
1.74 kristaps 596: man_vmsg(struct man *man, enum mandocerr t,
597: int ln, int pos, const char *fmt, ...)
1.4 kristaps 598: {
599: char buf[256];
600: va_list ap;
601:
602: va_start(ap, fmt);
1.74 kristaps 603: vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.4 kristaps 604: va_end(ap);
1.74 kristaps 605: return((*man->msg)(t, man->data, ln, pos, buf));
1.51 kristaps 606: }
607:
608:
1.54 kristaps 609: /*
610: * Unlink a node from its context. If "m" is provided, the last parse
611: * point will also be adjusted accordingly.
612: */
613: static void
1.51 kristaps 614: man_node_unlink(struct man *m, struct man_node *n)
615: {
616:
1.54 kristaps 617: /* Adjust siblings. */
618:
619: if (n->prev)
1.51 kristaps 620: n->prev->next = n->next;
1.54 kristaps 621: if (n->next)
622: n->next->prev = n->prev;
623:
624: /* Adjust parent. */
625:
626: if (n->parent) {
627: n->parent->nchild--;
628: if (n->parent->child == n)
629: n->parent->child = n->prev ? n->prev : n->next;
630: }
631:
632: /* Adjust parse point, if applicable. */
633:
634: if (m && m->last == n) {
635: /*XXX: this can occur when bailing from validation. */
636: /*assert(NULL == n->next);*/
637: if (n->prev) {
1.51 kristaps 638: m->last = n->prev;
639: m->next = MAN_NEXT_SIBLING;
1.54 kristaps 640: } else {
1.51 kristaps 641: m->last = n->parent;
642: m->next = MAN_NEXT_CHILD;
643: }
644: }
645:
1.54 kristaps 646: if (m && m->first == n)
647: m->first = NULL;
1.23 kristaps 648: }
CVSweb