Annotation of mandoc/mdoc.c, Revision 1.60
1.60 ! kristaps 1: /* $Id: mdoc.c,v 1.59 2009/03/09 13:35:09 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
19: #include <assert.h>
20: #include <ctype.h>
21: #include <err.h>
22: #include <stdarg.h>
23: #include <stdlib.h>
24: #include <stdio.h>
25: #include <string.h>
26:
27: #include "private.h"
28:
1.41 kristaps 29: /*
30: * Main caller in the libmdoc library. This begins the parsing routine,
31: * handles allocation of data, and so forth. Most of the "work" is done
1.48 kristaps 32: * in macro.c and validate.c.
1.41 kristaps 33: */
34:
1.48 kristaps 35: static struct mdoc_node *mdoc_node_alloc(const struct mdoc *);
36: static int mdoc_node_append(struct mdoc *,
37: struct mdoc_node *);
1.53 kristaps 38:
39: static int parsetext(struct mdoc *, int, char *);
40: static int parsemacro(struct mdoc *, int, char *);
1.58 kristaps 41: static int macrowarn(struct mdoc *, int, const char *);
1.48 kristaps 42:
43:
1.1 kristaps 44: const char *const __mdoc_macronames[MDOC_MAX] = {
45: "\\\"", "Dd", "Dt", "Os",
46: "Sh", "Ss", "Pp", "D1",
47: "Dl", "Bd", "Ed", "Bl",
48: "El", "It", "Ad", "An",
49: "Ar", "Cd", "Cm", "Dv",
50: "Er", "Ev", "Ex", "Fa",
51: "Fd", "Fl", "Fn", "Ft",
52: "Ic", "In", "Li", "Nd",
53: "Nm", "Op", "Ot", "Pa",
54: "Rv", "St", "Va", "Vt",
55: /* LINTED */
56: "Xr", "\%A", "\%B", "\%D",
57: /* LINTED */
58: "\%I", "\%J", "\%N", "\%O",
59: /* LINTED */
60: "\%P", "\%R", "\%T", "\%V",
61: "Ac", "Ao", "Aq", "At",
62: "Bc", "Bf", "Bo", "Bq",
63: "Bsx", "Bx", "Db", "Dc",
64: "Do", "Dq", "Ec", "Ef",
65: "Em", "Eo", "Fx", "Ms",
66: "No", "Ns", "Nx", "Ox",
67: "Pc", "Pf", "Po", "Pq",
68: "Qc", "Ql", "Qo", "Qq",
69: "Re", "Rs", "Sc", "So",
70: "Sq", "Sm", "Sx", "Sy",
71: "Tn", "Ux", "Xc", "Xo",
72: "Fo", "Fc", "Oo", "Oc",
73: "Bk", "Ek", "Bt", "Hf",
1.57 kristaps 74: "Fr", "Ud", "Lb", "Ap",
1.60 ! kristaps 75: "Lp", "Lk", "Mt"
1.1 kristaps 76: };
77:
78: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
79: "split", "nosplit", "ragged",
80: "unfilled", "literal", "file",
81: "offset", "bullet", "dash",
82: "hyphen", "item", "enum",
83: "tag", "diag", "hang",
84: "ohang", "inset", "column",
85: "width", "compact", "std",
1.52 kristaps 86: "filled", "words", "emphasis",
87: "symbolic"
1.1 kristaps 88: };
89:
90: const char * const *mdoc_macronames = __mdoc_macronames;
91: const char * const *mdoc_argnames = __mdoc_argnames;
92:
1.45 kristaps 93:
1.1 kristaps 94: const struct mdoc_node *
1.47 kristaps 95: mdoc_node(const struct mdoc *mdoc)
1.1 kristaps 96: {
97:
98: return(mdoc->first);
99: }
100:
101:
1.37 kristaps 102: const struct mdoc_meta *
1.47 kristaps 103: mdoc_meta(const struct mdoc *mdoc)
1.37 kristaps 104: {
105:
106: return(&mdoc->meta);
107: }
108:
109:
1.1 kristaps 110: void
1.38 kristaps 111: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 112: {
113:
1.38 kristaps 114: if (mdoc->first)
115: mdoc_node_freelist(mdoc->first);
116: if (mdoc->htab)
117: mdoc_tokhash_free(mdoc->htab);
1.34 kristaps 118: if (mdoc->meta.title)
119: free(mdoc->meta.title);
120: if (mdoc->meta.os)
121: free(mdoc->meta.os);
122: if (mdoc->meta.name)
123: free(mdoc->meta.name);
1.52 kristaps 124: if (mdoc->meta.arch)
125: free(mdoc->meta.arch);
126: if (mdoc->meta.vol)
127: free(mdoc->meta.vol);
1.34 kristaps 128:
1.1 kristaps 129: free(mdoc);
130: }
131:
132:
133: struct mdoc *
1.55 kristaps 134: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1 kristaps 135: {
136: struct mdoc *p;
137:
138: p = xcalloc(1, sizeof(struct mdoc));
139:
140: p->data = data;
1.33 kristaps 141: if (cb)
142: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1 kristaps 143:
1.25 kristaps 144: p->last = xcalloc(1, sizeof(struct mdoc_node));
145: p->last->type = MDOC_ROOT;
146: p->first = p->last;
1.55 kristaps 147: p->pflags = pflags;
1.25 kristaps 148: p->next = MDOC_NEXT_CHILD;
1.4 kristaps 149: p->htab = mdoc_tokhash_alloc();
1.25 kristaps 150:
1.1 kristaps 151: return(p);
152: }
153:
154:
155: int
1.20 kristaps 156: mdoc_endparse(struct mdoc *mdoc)
157: {
158:
159: if (MDOC_HALT & mdoc->flags)
160: return(0);
161: if (NULL == mdoc->first)
162: return(1);
163:
164: assert(mdoc->last);
165: if ( ! macro_end(mdoc)) {
166: mdoc->flags |= MDOC_HALT;
167: return(0);
168: }
169: return(1);
170: }
171:
172:
1.50 kristaps 173: /*
1.53 kristaps 174: * Main parse routine. Parses a single line -- really just hands off to
175: * the macro or text parser.
1.50 kristaps 176: */
1.20 kristaps 177: int
1.53 kristaps 178: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1 kristaps 179: {
180:
1.53 kristaps 181: /* If in error-mode, then we parse no more. */
1.50 kristaps 182:
1.53 kristaps 183: if (MDOC_HALT & m->flags)
1.20 kristaps 184: return(0);
1.50 kristaps 185:
1.53 kristaps 186: return('.' == *buf ? parsemacro(m, ln, buf) :
187: parsetext(m, ln, buf));
1.1 kristaps 188: }
189:
190:
191: void
1.31 kristaps 192: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 193: {
1.31 kristaps 194: char buf[256];
1.23 kristaps 195: va_list ap;
1.1 kristaps 196:
197: if (NULL == mdoc->cb.mdoc_msg)
198: return;
199:
200: va_start(ap, fmt);
1.31 kristaps 201: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1 kristaps 202: va_end(ap);
1.31 kristaps 203: (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1 kristaps 204: }
205:
206:
207: int
1.31 kristaps 208: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
209: const char *fmt, ...)
1.1 kristaps 210: {
1.31 kristaps 211: char buf[256];
212: va_list ap;
1.1 kristaps 213:
214: if (NULL == mdoc->cb.mdoc_err)
215: return(0);
1.31 kristaps 216:
217: va_start(ap, fmt);
218: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
219: va_end(ap);
220: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 221: }
222:
223:
224: int
1.31 kristaps 225: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
226: enum mdoc_warn type, const char *fmt, ...)
1.1 kristaps 227: {
1.31 kristaps 228: char buf[256];
229: va_list ap;
1.1 kristaps 230:
231: if (NULL == mdoc->cb.mdoc_warn)
232: return(0);
1.31 kristaps 233:
234: va_start(ap, fmt);
235: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
236: va_end(ap);
237: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1 kristaps 238: }
239:
240:
241: int
1.53 kristaps 242: mdoc_macro(struct mdoc *m, int tok,
243: int ln, int pp, int *pos, char *buf)
1.1 kristaps 244: {
245:
1.53 kristaps 246: /* FIXME - these should happen during validation. */
1.31 kristaps 247:
1.44 kristaps 248: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53 kristaps 249: SEC_PROLOGUE != m->lastnamed)
250: return(mdoc_perr(m, ln, pp,
251: "disallowed in document body"));
252:
1.44 kristaps 253: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.53 kristaps 254: SEC_PROLOGUE == m->lastnamed)
255: return(mdoc_perr(m, ln, pp,
256: "disallowed in prologue"));
257:
258: if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
259: return(mdoc_perr(m, ln, pp, "not callable"));
260:
261: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1 kristaps 262: }
263:
264:
1.23 kristaps 265: static int
266: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 267: {
268:
1.25 kristaps 269: assert(mdoc->last);
270: assert(mdoc->first);
271: assert(MDOC_ROOT != p->type);
1.1 kristaps 272:
1.13 kristaps 273: switch (mdoc->next) {
274: case (MDOC_NEXT_SIBLING):
1.6 kristaps 275: mdoc->last->next = p;
276: p->prev = mdoc->last;
1.13 kristaps 277: p->parent = mdoc->last->parent;
1.1 kristaps 278: break;
1.13 kristaps 279: case (MDOC_NEXT_CHILD):
280: mdoc->last->child = p;
1.1 kristaps 281: p->parent = mdoc->last;
282: break;
283: default:
1.13 kristaps 284: abort();
285: /* NOTREACHED */
1.1 kristaps 286: }
287:
1.23 kristaps 288: if ( ! mdoc_valid_pre(mdoc, p))
289: return(0);
1.27 kristaps 290:
291: switch (p->type) {
292: case (MDOC_HEAD):
293: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 294: p->parent->head = p;
1.27 kristaps 295: break;
296: case (MDOC_TAIL):
297: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 298: p->parent->tail = p;
1.27 kristaps 299: break;
300: case (MDOC_BODY):
301: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 302: p->parent->body = p;
1.27 kristaps 303: break;
304: default:
305: break;
306: }
307:
1.1 kristaps 308: mdoc->last = p;
1.23 kristaps 309: return(1);
1.1 kristaps 310: }
311:
312:
1.45 kristaps 313: static struct mdoc_node *
1.46 kristaps 314: mdoc_node_alloc(const struct mdoc *mdoc)
1.45 kristaps 315: {
1.46 kristaps 316: struct mdoc_node *p;
317:
318: p = xcalloc(1, sizeof(struct mdoc_node));
319: p->sec = mdoc->lastsec;
1.45 kristaps 320:
1.46 kristaps 321: return(p);
1.45 kristaps 322: }
323:
324:
1.23 kristaps 325: int
1.22 kristaps 326: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17 kristaps 327: {
328: struct mdoc_node *p;
329:
330: assert(mdoc->first);
331: assert(mdoc->last);
332:
1.46 kristaps 333: p = mdoc_node_alloc(mdoc);
1.17 kristaps 334:
1.22 kristaps 335: p->line = line;
336: p->pos = pos;
1.17 kristaps 337: p->type = MDOC_TAIL;
1.27 kristaps 338: p->tok = tok;
1.17 kristaps 339:
1.23 kristaps 340: return(mdoc_node_append(mdoc, p));
1.17 kristaps 341: }
342:
343:
1.23 kristaps 344: int
1.22 kristaps 345: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 346: {
347: struct mdoc_node *p;
348:
349: assert(mdoc->first);
350: assert(mdoc->last);
351:
1.46 kristaps 352: p = mdoc_node_alloc(mdoc);
1.13 kristaps 353:
1.22 kristaps 354: p->line = line;
355: p->pos = pos;
1.1 kristaps 356: p->type = MDOC_HEAD;
1.27 kristaps 357: p->tok = tok;
1.1 kristaps 358:
1.23 kristaps 359: return(mdoc_node_append(mdoc, p));
1.1 kristaps 360: }
361:
362:
1.23 kristaps 363: int
1.22 kristaps 364: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 365: {
366: struct mdoc_node *p;
367:
368: assert(mdoc->first);
369: assert(mdoc->last);
370:
1.46 kristaps 371: p = mdoc_node_alloc(mdoc);
1.1 kristaps 372:
1.22 kristaps 373: p->line = line;
374: p->pos = pos;
1.1 kristaps 375: p->type = MDOC_BODY;
1.27 kristaps 376: p->tok = tok;
1.1 kristaps 377:
1.23 kristaps 378: return(mdoc_node_append(mdoc, p));
1.1 kristaps 379: }
380:
381:
1.23 kristaps 382: int
1.25 kristaps 383: mdoc_root_alloc(struct mdoc *mdoc)
384: {
385: struct mdoc_node *p;
386:
1.46 kristaps 387: p = mdoc_node_alloc(mdoc);
1.25 kristaps 388:
389: p->type = MDOC_ROOT;
390:
391: return(mdoc_node_append(mdoc, p));
392: }
393:
394:
395: int
1.22 kristaps 396: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53 kristaps 397: int tok, struct mdoc_arg *args)
1.1 kristaps 398: {
399: struct mdoc_node *p;
400:
1.46 kristaps 401: p = mdoc_node_alloc(mdoc);
1.1 kristaps 402:
1.22 kristaps 403: p->pos = pos;
404: p->line = line;
1.1 kristaps 405: p->type = MDOC_BLOCK;
1.27 kristaps 406: p->tok = tok;
1.53 kristaps 407: p->args = args;
408:
409: if (args)
410: (args->refcnt)++;
1.1 kristaps 411:
1.23 kristaps 412: return(mdoc_node_append(mdoc, p));
1.1 kristaps 413: }
414:
415:
1.23 kristaps 416: int
1.22 kristaps 417: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53 kristaps 418: int tok, struct mdoc_arg *args)
1.1 kristaps 419: {
420: struct mdoc_node *p;
421:
1.46 kristaps 422: p = mdoc_node_alloc(mdoc);
1.22 kristaps 423:
424: p->line = line;
425: p->pos = pos;
1.1 kristaps 426: p->type = MDOC_ELEM;
1.27 kristaps 427: p->tok = tok;
1.53 kristaps 428: p->args = args;
429:
430: if (args)
431: (args->refcnt)++;
1.1 kristaps 432:
1.23 kristaps 433: return(mdoc_node_append(mdoc, p));
1.1 kristaps 434: }
435:
436:
1.23 kristaps 437: int
1.22 kristaps 438: mdoc_word_alloc(struct mdoc *mdoc,
439: int line, int pos, const char *word)
1.1 kristaps 440: {
441: struct mdoc_node *p;
442:
1.46 kristaps 443: p = mdoc_node_alloc(mdoc);
1.45 kristaps 444:
1.22 kristaps 445: p->line = line;
446: p->pos = pos;
1.1 kristaps 447: p->type = MDOC_TEXT;
1.53 kristaps 448: p->string = xstrdup(word);
1.1 kristaps 449:
1.23 kristaps 450: return(mdoc_node_append(mdoc, p));
1.1 kristaps 451: }
452:
453:
1.53 kristaps 454: void
455: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 456: {
457:
1.53 kristaps 458: if (p->string)
459: free(p->string);
460: if (p->args)
461: mdoc_argv_free(p->args);
1.1 kristaps 462: free(p);
463: }
464:
465:
1.53 kristaps 466: void
467: mdoc_node_freelist(struct mdoc_node *p)
1.1 kristaps 468: {
469:
1.53 kristaps 470: if (p->child)
471: mdoc_node_freelist(p->child);
472: if (p->next)
473: mdoc_node_freelist(p->next);
1.1 kristaps 474:
1.53 kristaps 475: mdoc_node_free(p);
1.1 kristaps 476: }
477:
478:
1.53 kristaps 479: /*
480: * Parse free-form text, that is, a line that does not begin with the
481: * control character.
482: */
483: static int
484: parsetext(struct mdoc *mdoc, int line, char *buf)
1.1 kristaps 485: {
486:
1.53 kristaps 487: if (SEC_PROLOGUE == mdoc->lastnamed)
488: return(mdoc_perr(mdoc, line, 0,
489: "text disallowed in prologue"));
1.1 kristaps 490:
1.53 kristaps 491: if ( ! mdoc_word_alloc(mdoc, line, 0, buf))
492: return(0);
1.1 kristaps 493:
1.53 kristaps 494: mdoc->next = MDOC_NEXT_SIBLING;
495: return(1);
1.1 kristaps 496: }
497:
498:
1.58 kristaps 499: static int
500: macrowarn(struct mdoc *m, int ln, const char *buf)
501: {
502: if ( ! (MDOC_IGN_MACRO & m->pflags))
503: return(mdoc_perr(m, ln, 1, "unknown macro: %s%s",
1.59 kristaps 504: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 505: return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
506: "unknown macro: %s%s",
1.59 kristaps 507: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 508: }
509:
510:
511:
1.53 kristaps 512: /*
513: * Parse a macro line, that is, a line beginning with the control
514: * character.
515: */
516: int
517: parsemacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 518: {
1.53 kristaps 519: int i, c;
520: char mac[5];
1.1 kristaps 521:
1.53 kristaps 522: /* Comments are quickly ignored. */
1.1 kristaps 523:
1.53 kristaps 524: if (buf[1] && '\\' == buf[1])
525: if (buf[2] && '\"' == buf[2])
526: return(1);
1.1 kristaps 527:
1.53 kristaps 528: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 529:
1.53 kristaps 530: for (i = 1; i < 5; i++) {
531: if (0 == (mac[i - 1] = buf[i]))
532: break;
533: else if (isspace((unsigned char)buf[i]))
534: break;
535: }
1.1 kristaps 536:
1.54 kristaps 537: /* FIXME: be able to skip unknown macro lines! */
538:
1.53 kristaps 539: mac[i - 1] = 0;
1.1 kristaps 540:
1.53 kristaps 541: if (i == 5 || i <= 2) {
1.58 kristaps 542: if ( ! macrowarn(m, ln, mac))
543: goto err;
544: return(1);
1.53 kristaps 545: }
546:
547: if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) {
1.58 kristaps 548: if ( ! macrowarn(m, ln, mac))
549: goto err;
550: return(1);
1.53 kristaps 551: }
1.1 kristaps 552:
1.53 kristaps 553: /* The macro is sane. Jump to the next word. */
1.1 kristaps 554:
1.53 kristaps 555: while (buf[i] && isspace((unsigned char)buf[i]))
556: i++;
1.1 kristaps 557:
1.53 kristaps 558: /* Begin recursive parse sequence. */
1.1 kristaps 559:
1.53 kristaps 560: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
561: goto err;
1.1 kristaps 562:
1.53 kristaps 563: return(1);
1.1 kristaps 564:
1.53 kristaps 565: err: /* Error out. */
1.1 kristaps 566:
1.53 kristaps 567: m->flags |= MDOC_HALT;
568: return(0);
1.1 kristaps 569: }
CVSweb