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