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