Annotation of mandoc/mdoc.c, Revision 1.64
1.64 ! kristaps 1: /* $Id: mdoc.c,v 1.63 2009/03/12 15:55:11 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.61 kristaps 75: "Lp", "Lk", "Mt", "Brq",
1.64 ! kristaps 76: /* LINTED */
! 77: "Bro", "Brc", "\%C"
1.1 kristaps 78: };
79:
80: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
81: "split", "nosplit", "ragged",
82: "unfilled", "literal", "file",
83: "offset", "bullet", "dash",
84: "hyphen", "item", "enum",
85: "tag", "diag", "hang",
86: "ohang", "inset", "column",
87: "width", "compact", "std",
1.52 kristaps 88: "filled", "words", "emphasis",
1.64 ! kristaps 89: "symbolic", "nested"
1.1 kristaps 90: };
91:
92: const char * const *mdoc_macronames = __mdoc_macronames;
93: const char * const *mdoc_argnames = __mdoc_argnames;
94:
1.45 kristaps 95:
1.1 kristaps 96: const struct mdoc_node *
1.47 kristaps 97: mdoc_node(const struct mdoc *mdoc)
1.1 kristaps 98: {
99:
100: return(mdoc->first);
101: }
102:
103:
1.37 kristaps 104: const struct mdoc_meta *
1.47 kristaps 105: mdoc_meta(const struct mdoc *mdoc)
1.37 kristaps 106: {
107:
108: return(&mdoc->meta);
109: }
110:
111:
1.1 kristaps 112: void
1.38 kristaps 113: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 114: {
115:
1.38 kristaps 116: if (mdoc->first)
117: mdoc_node_freelist(mdoc->first);
118: if (mdoc->htab)
119: mdoc_tokhash_free(mdoc->htab);
1.34 kristaps 120: if (mdoc->meta.title)
121: free(mdoc->meta.title);
122: if (mdoc->meta.os)
123: free(mdoc->meta.os);
124: if (mdoc->meta.name)
125: free(mdoc->meta.name);
1.52 kristaps 126: if (mdoc->meta.arch)
127: free(mdoc->meta.arch);
128: if (mdoc->meta.vol)
129: free(mdoc->meta.vol);
1.34 kristaps 130:
1.1 kristaps 131: free(mdoc);
132: }
133:
134:
135: struct mdoc *
1.55 kristaps 136: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1 kristaps 137: {
138: struct mdoc *p;
139:
140: p = xcalloc(1, sizeof(struct mdoc));
141:
142: p->data = data;
1.33 kristaps 143: if (cb)
144: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1 kristaps 145:
1.25 kristaps 146: p->last = xcalloc(1, sizeof(struct mdoc_node));
147: p->last->type = MDOC_ROOT;
148: p->first = p->last;
1.55 kristaps 149: p->pflags = pflags;
1.25 kristaps 150: p->next = MDOC_NEXT_CHILD;
1.4 kristaps 151: p->htab = mdoc_tokhash_alloc();
1.25 kristaps 152:
1.1 kristaps 153: return(p);
154: }
155:
156:
157: int
1.20 kristaps 158: mdoc_endparse(struct mdoc *mdoc)
159: {
160:
161: if (MDOC_HALT & mdoc->flags)
162: return(0);
163: if (NULL == mdoc->first)
164: return(1);
165:
166: assert(mdoc->last);
167: if ( ! macro_end(mdoc)) {
168: mdoc->flags |= MDOC_HALT;
169: return(0);
170: }
171: return(1);
172: }
173:
174:
1.50 kristaps 175: /*
1.53 kristaps 176: * Main parse routine. Parses a single line -- really just hands off to
177: * the macro or text parser.
1.50 kristaps 178: */
1.20 kristaps 179: int
1.53 kristaps 180: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1 kristaps 181: {
182:
1.53 kristaps 183: /* If in error-mode, then we parse no more. */
1.50 kristaps 184:
1.53 kristaps 185: if (MDOC_HALT & m->flags)
1.20 kristaps 186: return(0);
1.50 kristaps 187:
1.53 kristaps 188: return('.' == *buf ? parsemacro(m, ln, buf) :
189: parsetext(m, ln, buf));
1.1 kristaps 190: }
191:
192:
193: void
1.31 kristaps 194: mdoc_vmsg(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 195: {
1.31 kristaps 196: char buf[256];
1.23 kristaps 197: va_list ap;
1.1 kristaps 198:
199: if (NULL == mdoc->cb.mdoc_msg)
200: return;
201:
202: va_start(ap, fmt);
1.31 kristaps 203: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.1 kristaps 204: va_end(ap);
1.31 kristaps 205: (*mdoc->cb.mdoc_msg)(mdoc->data, ln, pos, buf);
1.1 kristaps 206: }
207:
208:
209: int
1.31 kristaps 210: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
211: const char *fmt, ...)
1.1 kristaps 212: {
1.31 kristaps 213: char buf[256];
214: va_list ap;
1.1 kristaps 215:
216: if (NULL == mdoc->cb.mdoc_err)
217: return(0);
1.31 kristaps 218:
219: va_start(ap, fmt);
220: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
221: va_end(ap);
222: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 223: }
224:
225:
226: int
1.31 kristaps 227: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos,
228: enum mdoc_warn type, const char *fmt, ...)
1.1 kristaps 229: {
1.31 kristaps 230: char buf[256];
231: va_list ap;
1.1 kristaps 232:
233: if (NULL == mdoc->cb.mdoc_warn)
234: return(0);
1.31 kristaps 235:
236: va_start(ap, fmt);
237: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
238: va_end(ap);
239: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, type, buf));
1.1 kristaps 240: }
241:
242:
243: int
1.53 kristaps 244: mdoc_macro(struct mdoc *m, int tok,
245: int ln, int pp, int *pos, char *buf)
1.1 kristaps 246: {
247:
1.53 kristaps 248: /* FIXME - these should happen during validation. */
1.31 kristaps 249:
1.44 kristaps 250: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.53 kristaps 251: SEC_PROLOGUE != m->lastnamed)
252: return(mdoc_perr(m, ln, pp,
253: "disallowed in document body"));
254:
1.44 kristaps 255: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
1.53 kristaps 256: SEC_PROLOGUE == m->lastnamed)
257: return(mdoc_perr(m, ln, pp,
258: "disallowed in prologue"));
259:
260: if (1 != pp && ! (MDOC_CALLABLE & mdoc_macros[tok].flags))
1.64 ! kristaps 261: return(mdoc_perr(m, ln, pp, "%s not callable",
! 262: mdoc_macronames[tok]));
1.53 kristaps 263:
264: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.1 kristaps 265: }
266:
267:
1.23 kristaps 268: static int
269: mdoc_node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 270: {
271:
1.25 kristaps 272: assert(mdoc->last);
273: assert(mdoc->first);
274: assert(MDOC_ROOT != p->type);
1.1 kristaps 275:
1.13 kristaps 276: switch (mdoc->next) {
277: case (MDOC_NEXT_SIBLING):
1.6 kristaps 278: mdoc->last->next = p;
279: p->prev = mdoc->last;
1.13 kristaps 280: p->parent = mdoc->last->parent;
1.1 kristaps 281: break;
1.13 kristaps 282: case (MDOC_NEXT_CHILD):
283: mdoc->last->child = p;
1.1 kristaps 284: p->parent = mdoc->last;
285: break;
286: default:
1.13 kristaps 287: abort();
288: /* NOTREACHED */
1.1 kristaps 289: }
290:
1.23 kristaps 291: if ( ! mdoc_valid_pre(mdoc, p))
292: return(0);
1.27 kristaps 293:
294: switch (p->type) {
295: case (MDOC_HEAD):
296: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 297: p->parent->head = p;
1.27 kristaps 298: break;
299: case (MDOC_TAIL):
300: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 301: p->parent->tail = p;
1.27 kristaps 302: break;
303: case (MDOC_BODY):
304: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 305: p->parent->body = p;
1.27 kristaps 306: break;
307: default:
308: break;
309: }
310:
1.1 kristaps 311: mdoc->last = p;
1.23 kristaps 312: return(1);
1.1 kristaps 313: }
314:
315:
1.45 kristaps 316: static struct mdoc_node *
1.46 kristaps 317: mdoc_node_alloc(const struct mdoc *mdoc)
1.45 kristaps 318: {
1.46 kristaps 319: struct mdoc_node *p;
320:
321: p = xcalloc(1, sizeof(struct mdoc_node));
322: p->sec = mdoc->lastsec;
1.45 kristaps 323:
1.46 kristaps 324: return(p);
1.45 kristaps 325: }
326:
327:
1.23 kristaps 328: int
1.22 kristaps 329: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.17 kristaps 330: {
331: struct mdoc_node *p;
332:
333: assert(mdoc->first);
334: assert(mdoc->last);
335:
1.46 kristaps 336: p = mdoc_node_alloc(mdoc);
1.17 kristaps 337:
1.22 kristaps 338: p->line = line;
339: p->pos = pos;
1.17 kristaps 340: p->type = MDOC_TAIL;
1.27 kristaps 341: p->tok = tok;
1.17 kristaps 342:
1.23 kristaps 343: return(mdoc_node_append(mdoc, p));
1.17 kristaps 344: }
345:
346:
1.23 kristaps 347: int
1.22 kristaps 348: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 349: {
350: struct mdoc_node *p;
351:
352: assert(mdoc->first);
353: assert(mdoc->last);
354:
1.46 kristaps 355: p = mdoc_node_alloc(mdoc);
1.13 kristaps 356:
1.22 kristaps 357: p->line = line;
358: p->pos = pos;
1.1 kristaps 359: p->type = MDOC_HEAD;
1.27 kristaps 360: p->tok = tok;
1.1 kristaps 361:
1.23 kristaps 362: return(mdoc_node_append(mdoc, p));
1.1 kristaps 363: }
364:
365:
1.23 kristaps 366: int
1.22 kristaps 367: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, int tok)
1.1 kristaps 368: {
369: struct mdoc_node *p;
370:
371: assert(mdoc->first);
372: assert(mdoc->last);
373:
1.46 kristaps 374: p = mdoc_node_alloc(mdoc);
1.1 kristaps 375:
1.22 kristaps 376: p->line = line;
377: p->pos = pos;
1.1 kristaps 378: p->type = MDOC_BODY;
1.27 kristaps 379: p->tok = tok;
1.1 kristaps 380:
1.23 kristaps 381: return(mdoc_node_append(mdoc, p));
1.1 kristaps 382: }
383:
384:
1.23 kristaps 385: int
1.25 kristaps 386: mdoc_root_alloc(struct mdoc *mdoc)
387: {
388: struct mdoc_node *p;
389:
1.46 kristaps 390: p = mdoc_node_alloc(mdoc);
1.25 kristaps 391:
392: p->type = MDOC_ROOT;
393:
394: return(mdoc_node_append(mdoc, p));
395: }
396:
397:
398: int
1.22 kristaps 399: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.53 kristaps 400: int tok, struct mdoc_arg *args)
1.1 kristaps 401: {
402: struct mdoc_node *p;
403:
1.46 kristaps 404: p = mdoc_node_alloc(mdoc);
1.1 kristaps 405:
1.22 kristaps 406: p->pos = pos;
407: p->line = line;
1.1 kristaps 408: p->type = MDOC_BLOCK;
1.27 kristaps 409: p->tok = tok;
1.53 kristaps 410: p->args = args;
411:
412: if (args)
413: (args->refcnt)++;
1.1 kristaps 414:
1.23 kristaps 415: return(mdoc_node_append(mdoc, p));
1.1 kristaps 416: }
417:
418:
1.23 kristaps 419: int
1.22 kristaps 420: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.53 kristaps 421: int tok, struct mdoc_arg *args)
1.1 kristaps 422: {
423: struct mdoc_node *p;
424:
1.46 kristaps 425: p = mdoc_node_alloc(mdoc);
1.22 kristaps 426:
427: p->line = line;
428: p->pos = pos;
1.1 kristaps 429: p->type = MDOC_ELEM;
1.27 kristaps 430: p->tok = tok;
1.53 kristaps 431: p->args = args;
432:
433: if (args)
434: (args->refcnt)++;
1.1 kristaps 435:
1.23 kristaps 436: return(mdoc_node_append(mdoc, p));
1.1 kristaps 437: }
438:
439:
1.23 kristaps 440: int
1.22 kristaps 441: mdoc_word_alloc(struct mdoc *mdoc,
442: int line, int pos, const char *word)
1.1 kristaps 443: {
444: struct mdoc_node *p;
445:
1.46 kristaps 446: p = mdoc_node_alloc(mdoc);
1.45 kristaps 447:
1.22 kristaps 448: p->line = line;
449: p->pos = pos;
1.1 kristaps 450: p->type = MDOC_TEXT;
1.53 kristaps 451: p->string = xstrdup(word);
1.1 kristaps 452:
1.23 kristaps 453: return(mdoc_node_append(mdoc, p));
1.1 kristaps 454: }
455:
456:
1.53 kristaps 457: void
458: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 459: {
460:
1.53 kristaps 461: if (p->string)
462: free(p->string);
463: if (p->args)
464: mdoc_argv_free(p->args);
1.1 kristaps 465: free(p);
466: }
467:
468:
1.53 kristaps 469: void
470: mdoc_node_freelist(struct mdoc_node *p)
1.1 kristaps 471: {
472:
1.53 kristaps 473: if (p->child)
474: mdoc_node_freelist(p->child);
475: if (p->next)
476: mdoc_node_freelist(p->next);
1.1 kristaps 477:
1.53 kristaps 478: mdoc_node_free(p);
1.1 kristaps 479: }
480:
481:
1.53 kristaps 482: /*
483: * Parse free-form text, that is, a line that does not begin with the
484: * control character.
485: */
486: static int
487: parsetext(struct mdoc *mdoc, int line, char *buf)
1.1 kristaps 488: {
489:
1.53 kristaps 490: if (SEC_PROLOGUE == mdoc->lastnamed)
491: return(mdoc_perr(mdoc, line, 0,
492: "text disallowed in prologue"));
1.1 kristaps 493:
1.53 kristaps 494: if ( ! mdoc_word_alloc(mdoc, line, 0, buf))
495: return(0);
1.1 kristaps 496:
1.53 kristaps 497: mdoc->next = MDOC_NEXT_SIBLING;
498: return(1);
1.1 kristaps 499: }
500:
501:
1.58 kristaps 502: static int
503: macrowarn(struct mdoc *m, int ln, const char *buf)
504: {
505: if ( ! (MDOC_IGN_MACRO & m->pflags))
506: return(mdoc_perr(m, ln, 1, "unknown macro: %s%s",
1.59 kristaps 507: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 508: return(mdoc_pwarn(m, ln, 1, WARN_SYNTAX,
509: "unknown macro: %s%s",
1.59 kristaps 510: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 511: }
512:
513:
514:
1.53 kristaps 515: /*
516: * Parse a macro line, that is, a line beginning with the control
517: * character.
518: */
519: int
520: parsemacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 521: {
1.53 kristaps 522: int i, c;
523: char mac[5];
1.1 kristaps 524:
1.63 kristaps 525: /* Comments and empties are quickly ignored. */
526:
527: if (0 == buf[1])
528: return(1);
529:
530: if (isspace((unsigned char)buf[1])) {
531: i = 2;
532: while (buf[i] && isspace((unsigned char)buf[i]))
533: i++;
534: if (0 == buf[i])
535: return(1);
536: return(mdoc_perr(m, ln, 1, "invalid syntax"));
537: }
1.1 kristaps 538:
1.53 kristaps 539: if (buf[1] && '\\' == buf[1])
540: if (buf[2] && '\"' == buf[2])
541: return(1);
1.1 kristaps 542:
1.53 kristaps 543: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 544:
1.53 kristaps 545: for (i = 1; i < 5; i++) {
546: if (0 == (mac[i - 1] = buf[i]))
547: break;
548: else if (isspace((unsigned char)buf[i]))
549: break;
550: }
1.1 kristaps 551:
1.53 kristaps 552: mac[i - 1] = 0;
1.1 kristaps 553:
1.53 kristaps 554: if (i == 5 || i <= 2) {
1.58 kristaps 555: if ( ! macrowarn(m, ln, mac))
556: goto err;
557: return(1);
1.53 kristaps 558: }
559:
560: if (MDOC_MAX == (c = mdoc_tokhash_find(m->htab, mac))) {
1.58 kristaps 561: if ( ! macrowarn(m, ln, mac))
562: goto err;
563: return(1);
1.53 kristaps 564: }
1.1 kristaps 565:
1.53 kristaps 566: /* The macro is sane. Jump to the next word. */
1.1 kristaps 567:
1.53 kristaps 568: while (buf[i] && isspace((unsigned char)buf[i]))
569: i++;
1.1 kristaps 570:
1.53 kristaps 571: /* Begin recursive parse sequence. */
1.1 kristaps 572:
1.53 kristaps 573: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
574: goto err;
1.1 kristaps 575:
1.53 kristaps 576: return(1);
1.1 kristaps 577:
1.53 kristaps 578: err: /* Error out. */
1.1 kristaps 579:
1.53 kristaps 580: m->flags |= MDOC_HALT;
581: return(0);
1.1 kristaps 582: }
CVSweb