Annotation of mandoc/mdoc.c, Revision 1.108
1.108 ! kristaps 1: /* $Id: mdoc.c,v 1.107 2009/09/24 15:01:06 kristaps Exp $ */
1.1 kristaps 2: /*
1.76 kristaps 3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1 kristaps 4: *
5: * Permission to use, copy, modify, and distribute this software for any
1.75 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.75 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.106 kristaps 17: #include <sys/types.h>
18:
1.1 kristaps 19: #include <assert.h>
20: #include <ctype.h>
21: #include <stdarg.h>
1.73 kristaps 22: #include <stdio.h>
1.1 kristaps 23: #include <stdlib.h>
24: #include <string.h>
25:
1.70 kristaps 26: #include "libmdoc.h"
1.1 kristaps 27:
1.89 kristaps 28: const char *const __mdoc_merrnames[MERRMAX] = {
29: "trailing whitespace", /* ETAILWS */
30: "unexpected quoted parameter", /* EQUOTPARM */
31: "unterminated quoted parameter", /* EQUOTTERM */
32: "system: malloc error", /* EMALLOC */
33: "argument parameter suggested", /* EARGVAL */
34: "macro disallowed in prologue", /* EBODYPROL */
35: "macro disallowed in body", /* EPROLBODY */
36: "text disallowed in prologue", /* ETEXTPROL */
37: "blank line disallowed", /* ENOBLANK */
38: "text parameter too long", /* ETOOLONG */
39: "invalid escape sequence", /* EESCAPE */
40: "invalid character", /* EPRINT */
41: "document has no body", /* ENODAT */
42: "document has no prologue", /* ENOPROLOGUE */
43: "expected line arguments", /* ELINE */
44: "invalid AT&T argument", /* EATT */
45: "default name not yet set", /* ENAME */
46: "missing list type", /* ELISTTYPE */
47: "missing display type", /* EDISPTYPE */
48: "too many display types", /* EMULTIDISP */
49: "too many list types", /* EMULTILIST */
50: "NAME section must be first", /* ESECNAME */
51: "badly-formed NAME section", /* ENAMESECINC */
52: "argument repeated", /* EARGREP */
53: "expected boolean parameter", /* EBOOL */
54: "inconsistent column syntax", /* ECOLMIS */
55: "nested display invalid", /* ENESTDISP */
56: "width argument missing", /* EMISSWIDTH */
57: "invalid section for this manual section", /* EWRONGMSEC */
58: "section out of conventional order", /* ESECOOO */
59: "section repeated", /* ESECREP */
60: "invalid standard argument", /* EBADSTAND */
61: "multi-line arguments discouraged", /* ENOMULTILINE */
62: "multi-line arguments suggested", /* EMULTILINE */
63: "line arguments discouraged", /* ENOLINE */
64: "prologue macro out of conventional order", /* EPROLOOO */
65: "prologue macro repeated", /* EPROLREP */
66: "invalid manual section", /* EBADMSEC */
67: "invalid section", /* EBADSEC */
68: "invalid font mode", /* EFONT */
69: "invalid date syntax", /* EBADDATE */
70: "invalid number format", /* ENUMFMT */
71: "superfluous width argument", /* ENOWIDTH */
72: "system: utsname error", /* EUTSNAME */
73: "obsolete macro", /* EOBS */
74: "end-of-line scope violation", /* EIMPBRK */
75: "empty macro ignored", /* EIGNE */
76: "unclosed explicit scope", /* EOPEN */
77: "unterminated quoted phrase", /* EQUOTPHR */
78: "closure macro without prior context", /* ENOCTX */
1.107 kristaps 79: "no description found for library", /* ELIB */
80: "bad child for parent context", /* EBADCHILD */
1.89 kristaps 81: };
82:
1.1 kristaps 83: const char *const __mdoc_macronames[MDOC_MAX] = {
1.82 kristaps 84: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 85: "Sh", "Ss", "Pp", "D1",
86: "Dl", "Bd", "Ed", "Bl",
87: "El", "It", "Ad", "An",
88: "Ar", "Cd", "Cm", "Dv",
89: "Er", "Ev", "Ex", "Fa",
90: "Fd", "Fl", "Fn", "Ft",
91: "Ic", "In", "Li", "Nd",
92: "Nm", "Op", "Ot", "Pa",
93: "Rv", "St", "Va", "Vt",
94: /* LINTED */
95: "Xr", "\%A", "\%B", "\%D",
96: /* LINTED */
97: "\%I", "\%J", "\%N", "\%O",
98: /* LINTED */
99: "\%P", "\%R", "\%T", "\%V",
100: "Ac", "Ao", "Aq", "At",
101: "Bc", "Bf", "Bo", "Bq",
102: "Bsx", "Bx", "Db", "Dc",
103: "Do", "Dq", "Ec", "Ef",
104: "Em", "Eo", "Fx", "Ms",
105: "No", "Ns", "Nx", "Ox",
106: "Pc", "Pf", "Po", "Pq",
107: "Qc", "Ql", "Qo", "Qq",
108: "Re", "Rs", "Sc", "So",
109: "Sq", "Sm", "Sx", "Sy",
110: "Tn", "Ux", "Xc", "Xo",
111: "Fo", "Fc", "Oo", "Oc",
112: "Bk", "Ek", "Bt", "Hf",
1.82 kristaps 113: "Fr", "Ud", "Lb", "Lp",
114: "Lk", "Mt", "Brq", "Bro",
1.64 kristaps 115: /* LINTED */
1.82 kristaps 116: "Brc", "\%C", "Es", "En",
1.69 kristaps 117: /* LINTED */
1.93 kristaps 118: "Dx", "\%Q", "br", "sp"
1.1 kristaps 119: };
120:
121: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
122: "split", "nosplit", "ragged",
123: "unfilled", "literal", "file",
124: "offset", "bullet", "dash",
125: "hyphen", "item", "enum",
126: "tag", "diag", "hang",
127: "ohang", "inset", "column",
128: "width", "compact", "std",
1.52 kristaps 129: "filled", "words", "emphasis",
1.108 ! kristaps 130: "symbolic", "nested", "centered"
1.1 kristaps 131: };
132:
133: const char * const *mdoc_macronames = __mdoc_macronames;
134: const char * const *mdoc_argnames = __mdoc_argnames;
135:
1.73 kristaps 136: static void mdoc_free1(struct mdoc *);
137: static int mdoc_alloc1(struct mdoc *);
138: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
139: int, enum mdoc_type);
140: static int node_append(struct mdoc *,
1.71 kristaps 141: struct mdoc_node *);
142: static int parsetext(struct mdoc *, int, char *);
143: static int parsemacro(struct mdoc *, int, char *);
144: static int macrowarn(struct mdoc *, int, const char *);
1.91 kristaps 145: static int pstring(struct mdoc *, int, int,
146: const char *, size_t);
1.88 kristaps 147:
1.92 kristaps 148: #ifdef __linux__
149: extern size_t strlcpy(char *, const char *, size_t);
150: #endif
151:
1.71 kristaps 152:
1.1 kristaps 153: const struct mdoc_node *
1.71 kristaps 154: mdoc_node(const struct mdoc *m)
1.1 kristaps 155: {
156:
1.71 kristaps 157: return(MDOC_HALT & m->flags ? NULL : m->first);
1.1 kristaps 158: }
159:
160:
1.37 kristaps 161: const struct mdoc_meta *
1.71 kristaps 162: mdoc_meta(const struct mdoc *m)
1.37 kristaps 163: {
164:
1.71 kristaps 165: return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37 kristaps 166: }
167:
168:
1.85 kristaps 169: /*
170: * Frees volatile resources (parse tree, meta-data, fields).
171: */
1.73 kristaps 172: static void
173: mdoc_free1(struct mdoc *mdoc)
1.67 kristaps 174: {
175:
176: if (mdoc->first)
177: mdoc_node_freelist(mdoc->first);
178: if (mdoc->meta.title)
179: free(mdoc->meta.title);
180: if (mdoc->meta.os)
181: free(mdoc->meta.os);
182: if (mdoc->meta.name)
183: free(mdoc->meta.name);
184: if (mdoc->meta.arch)
185: free(mdoc->meta.arch);
186: if (mdoc->meta.vol)
187: free(mdoc->meta.vol);
1.73 kristaps 188: }
189:
190:
1.85 kristaps 191: /*
192: * Allocate all volatile resources (parse tree, meta-data, fields).
193: */
1.73 kristaps 194: static int
195: mdoc_alloc1(struct mdoc *mdoc)
196: {
1.67 kristaps 197:
198: bzero(&mdoc->meta, sizeof(struct mdoc_meta));
199: mdoc->flags = 0;
1.85 kristaps 200: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.70 kristaps 201: mdoc->last = calloc(1, sizeof(struct mdoc_node));
202: if (NULL == mdoc->last)
1.73 kristaps 203: return(0);
204:
1.70 kristaps 205: mdoc->first = mdoc->last;
1.67 kristaps 206: mdoc->last->type = MDOC_ROOT;
207: mdoc->next = MDOC_NEXT_CHILD;
1.73 kristaps 208: return(1);
209: }
210:
211:
212: /*
1.85 kristaps 213: * Free up volatile resources (see mdoc_free1()) then re-initialises the
214: * data with mdoc_alloc1(). After invocation, parse data has been reset
215: * and the parser is ready for re-invocation on a new tree; however,
216: * cross-parse non-volatile data is kept intact.
1.73 kristaps 217: */
218: int
219: mdoc_reset(struct mdoc *mdoc)
220: {
221:
222: mdoc_free1(mdoc);
223: return(mdoc_alloc1(mdoc));
1.67 kristaps 224: }
225:
226:
1.68 kristaps 227: /*
1.85 kristaps 228: * Completely free up all volatile and non-volatile parse resources.
229: * After invocation, the pointer is no longer usable.
1.68 kristaps 230: */
1.67 kristaps 231: void
1.38 kristaps 232: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 233: {
234:
1.73 kristaps 235: mdoc_free1(mdoc);
1.1 kristaps 236: free(mdoc);
237: }
238:
239:
1.85 kristaps 240: /*
241: * Allocate volatile and non-volatile parse resources.
242: */
1.1 kristaps 243: struct mdoc *
1.55 kristaps 244: mdoc_alloc(void *data, int pflags, const struct mdoc_cb *cb)
1.1 kristaps 245: {
246: struct mdoc *p;
247:
1.70 kristaps 248: if (NULL == (p = calloc(1, sizeof(struct mdoc))))
1.73 kristaps 249: return(NULL);
1.74 kristaps 250: if (cb)
251: (void)memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.1 kristaps 252:
1.105 kristaps 253: mdoc_hash_init();
254:
1.1 kristaps 255: p->data = data;
1.73 kristaps 256: p->pflags = pflags;
257:
1.105 kristaps 258: if (mdoc_alloc1(p))
1.74 kristaps 259: return(p);
1.1 kristaps 260:
1.73 kristaps 261: free(p);
262: return(NULL);
1.1 kristaps 263: }
264:
265:
1.68 kristaps 266: /*
267: * Climb back up the parse tree, validating open scopes. Mostly calls
1.85 kristaps 268: * through to macro_end() in macro.c.
1.68 kristaps 269: */
1.1 kristaps 270: int
1.72 kristaps 271: mdoc_endparse(struct mdoc *m)
1.20 kristaps 272: {
273:
1.72 kristaps 274: if (MDOC_HALT & m->flags)
1.20 kristaps 275: return(0);
1.72 kristaps 276: else if (mdoc_macroend(m))
1.20 kristaps 277: return(1);
1.72 kristaps 278: m->flags |= MDOC_HALT;
279: return(0);
1.20 kristaps 280: }
281:
282:
1.50 kristaps 283: /*
1.53 kristaps 284: * Main parse routine. Parses a single line -- really just hands off to
1.85 kristaps 285: * the macro (parsemacro()) or text parser (parsetext()).
1.50 kristaps 286: */
1.20 kristaps 287: int
1.53 kristaps 288: mdoc_parseln(struct mdoc *m, int ln, char *buf)
1.1 kristaps 289: {
290:
1.53 kristaps 291: if (MDOC_HALT & m->flags)
1.20 kristaps 292: return(0);
1.50 kristaps 293:
1.53 kristaps 294: return('.' == *buf ? parsemacro(m, ln, buf) :
295: parsetext(m, ln, buf));
1.1 kristaps 296: }
297:
298:
299: int
1.31 kristaps 300: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
301: const char *fmt, ...)
1.1 kristaps 302: {
1.31 kristaps 303: char buf[256];
304: va_list ap;
1.1 kristaps 305:
306: if (NULL == mdoc->cb.mdoc_err)
307: return(0);
1.31 kristaps 308:
309: va_start(ap, fmt);
310: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
311: va_end(ap);
1.88 kristaps 312:
1.31 kristaps 313: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 314: }
315:
316:
317: int
1.87 kristaps 318: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 319: {
1.31 kristaps 320: char buf[256];
321: va_list ap;
1.1 kristaps 322:
323: if (NULL == mdoc->cb.mdoc_warn)
324: return(0);
1.31 kristaps 325:
326: va_start(ap, fmt);
327: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
328: va_end(ap);
1.88 kristaps 329:
1.87 kristaps 330: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1 kristaps 331: }
332:
333:
334: int
1.88 kristaps 335: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78 kristaps 336: {
1.89 kristaps 337: const char *p;
1.88 kristaps 338:
1.89 kristaps 339: p = __mdoc_merrnames[(int)type];
1.73 kristaps 340: assert(p);
1.88 kristaps 341:
342: if (iserr)
343: return(mdoc_verr(m, line, pos, p));
344:
345: return(mdoc_vwarn(m, line, pos, p));
346: }
347:
348:
349: int
350: mdoc_macro(struct mdoc *m, int tok,
351: int ln, int pp, int *pos, char *buf)
352: {
1.97 kristaps 353: /*
354: * If we're in the prologue, deny "body" macros. Similarly, if
355: * we're in the body, deny prologue calls.
356: */
1.88 kristaps 357: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
358: MDOC_PBODY & m->flags)
359: return(mdoc_perr(m, ln, pp, EPROLBODY));
360: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
361: ! (MDOC_PBODY & m->flags))
362: return(mdoc_perr(m, ln, pp, EBODYPROL));
363:
364: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73 kristaps 365: }
366:
367:
368: static int
369: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 370: {
371:
1.25 kristaps 372: assert(mdoc->last);
373: assert(mdoc->first);
374: assert(MDOC_ROOT != p->type);
1.1 kristaps 375:
1.13 kristaps 376: switch (mdoc->next) {
377: case (MDOC_NEXT_SIBLING):
1.6 kristaps 378: mdoc->last->next = p;
379: p->prev = mdoc->last;
1.13 kristaps 380: p->parent = mdoc->last->parent;
1.1 kristaps 381: break;
1.13 kristaps 382: case (MDOC_NEXT_CHILD):
383: mdoc->last->child = p;
1.1 kristaps 384: p->parent = mdoc->last;
385: break;
386: default:
1.13 kristaps 387: abort();
388: /* NOTREACHED */
1.1 kristaps 389: }
390:
1.86 kristaps 391: p->parent->nchild++;
392:
1.23 kristaps 393: if ( ! mdoc_valid_pre(mdoc, p))
394: return(0);
1.68 kristaps 395: if ( ! mdoc_action_pre(mdoc, p))
396: return(0);
1.27 kristaps 397:
398: switch (p->type) {
399: case (MDOC_HEAD):
400: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 401: p->parent->head = p;
1.27 kristaps 402: break;
403: case (MDOC_TAIL):
404: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 405: p->parent->tail = p;
1.27 kristaps 406: break;
407: case (MDOC_BODY):
408: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 409: p->parent->body = p;
1.27 kristaps 410: break;
411: default:
412: break;
413: }
414:
1.1 kristaps 415: mdoc->last = p;
1.71 kristaps 416:
417: switch (p->type) {
418: case (MDOC_TEXT):
419: if ( ! mdoc_valid_post(mdoc))
420: return(0);
421: if ( ! mdoc_action_post(mdoc))
422: return(0);
423: break;
424: default:
425: break;
426: }
427:
1.23 kristaps 428: return(1);
1.1 kristaps 429: }
430:
431:
1.45 kristaps 432: static struct mdoc_node *
1.91 kristaps 433: node_alloc(struct mdoc *m, int line,
1.73 kristaps 434: int pos, int tok, enum mdoc_type type)
1.45 kristaps 435: {
1.46 kristaps 436: struct mdoc_node *p;
437:
1.73 kristaps 438: if (NULL == (p = calloc(1, sizeof(struct mdoc_node)))) {
1.91 kristaps 439: (void)mdoc_nerr(m, m->last, EMALLOC);
1.73 kristaps 440: return(NULL);
441: }
442:
1.91 kristaps 443: p->sec = m->lastsec;
1.73 kristaps 444: p->line = line;
445: p->pos = pos;
446: p->tok = tok;
447: if (MDOC_TEXT != (p->type = type))
448: assert(p->tok >= 0);
1.45 kristaps 449:
1.46 kristaps 450: return(p);
1.45 kristaps 451: }
452:
453:
1.23 kristaps 454: int
1.91 kristaps 455: mdoc_tail_alloc(struct mdoc *m, int line, int pos, int tok)
1.17 kristaps 456: {
457: struct mdoc_node *p;
458:
1.91 kristaps 459: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.73 kristaps 460: if (NULL == p)
461: return(0);
1.102 kristaps 462: if ( ! node_append(m, p))
463: return(0);
464: m->next = MDOC_NEXT_CHILD;
465: return(1);
1.17 kristaps 466: }
467:
468:
1.23 kristaps 469: int
1.91 kristaps 470: mdoc_head_alloc(struct mdoc *m, int line, int pos, int tok)
1.1 kristaps 471: {
472: struct mdoc_node *p;
473:
1.91 kristaps 474: assert(m->first);
475: assert(m->last);
1.1 kristaps 476:
1.91 kristaps 477: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.73 kristaps 478: if (NULL == p)
479: return(0);
1.102 kristaps 480: if ( ! node_append(m, p))
481: return(0);
482: m->next = MDOC_NEXT_CHILD;
483: return(1);
1.1 kristaps 484: }
485:
486:
1.23 kristaps 487: int
1.91 kristaps 488: mdoc_body_alloc(struct mdoc *m, int line, int pos, int tok)
1.1 kristaps 489: {
490: struct mdoc_node *p;
491:
1.91 kristaps 492: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.73 kristaps 493: if (NULL == p)
494: return(0);
1.102 kristaps 495: if ( ! node_append(m, p))
496: return(0);
497: m->next = MDOC_NEXT_CHILD;
498: return(1);
1.1 kristaps 499: }
500:
501:
1.23 kristaps 502: int
1.91 kristaps 503: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.53 kristaps 504: int tok, struct mdoc_arg *args)
1.1 kristaps 505: {
506: struct mdoc_node *p;
507:
1.91 kristaps 508: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.73 kristaps 509: if (NULL == p)
510: return(0);
1.77 kristaps 511: p->args = args;
512: if (p->args)
1.53 kristaps 513: (args->refcnt)++;
1.102 kristaps 514: if ( ! node_append(m, p))
515: return(0);
516: m->next = MDOC_NEXT_CHILD;
517: return(1);
1.1 kristaps 518: }
519:
520:
1.23 kristaps 521: int
1.91 kristaps 522: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.53 kristaps 523: int tok, struct mdoc_arg *args)
1.1 kristaps 524: {
525: struct mdoc_node *p;
526:
1.91 kristaps 527: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.73 kristaps 528: if (NULL == p)
529: return(0);
1.77 kristaps 530: p->args = args;
531: if (p->args)
1.53 kristaps 532: (args->refcnt)++;
1.102 kristaps 533: if ( ! node_append(m, p))
534: return(0);
535: m->next = MDOC_NEXT_CHILD;
536: return(1);
1.1 kristaps 537: }
538:
539:
1.91 kristaps 540: static int
541: pstring(struct mdoc *m, int line, int pos, const char *p, size_t len)
1.1 kristaps 542: {
1.91 kristaps 543: struct mdoc_node *n;
544: size_t sv;
1.1 kristaps 545:
1.91 kristaps 546: n = node_alloc(m, line, pos, -1, MDOC_TEXT);
547: if (NULL == n)
548: return(mdoc_nerr(m, m->last, EMALLOC));
549:
550: n->string = malloc(len + 1);
551: if (NULL == n->string) {
552: free(n);
553: return(mdoc_nerr(m, m->last, EMALLOC));
1.73 kristaps 554: }
1.88 kristaps 555:
1.91 kristaps 556: sv = strlcpy(n->string, p, len + 1);
557:
558: /* Prohibit truncation. */
559: assert(sv < len + 1);
560:
1.101 kristaps 561: if ( ! node_append(m, n))
562: return(0);
563: m->next = MDOC_NEXT_SIBLING;
564: return(1);
1.91 kristaps 565: }
566:
567:
568: int
569: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
570: {
571:
572: return(pstring(m, line, pos, p, strlen(p)));
1.1 kristaps 573: }
574:
575:
1.53 kristaps 576: void
577: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 578: {
579:
1.86 kristaps 580: if (p->parent)
581: p->parent->nchild--;
1.53 kristaps 582: if (p->string)
583: free(p->string);
584: if (p->args)
585: mdoc_argv_free(p->args);
1.1 kristaps 586: free(p);
587: }
588:
589:
1.53 kristaps 590: void
591: mdoc_node_freelist(struct mdoc_node *p)
1.1 kristaps 592: {
593:
1.53 kristaps 594: if (p->child)
595: mdoc_node_freelist(p->child);
596: if (p->next)
597: mdoc_node_freelist(p->next);
1.1 kristaps 598:
1.86 kristaps 599: assert(0 == p->nchild);
1.53 kristaps 600: mdoc_node_free(p);
1.1 kristaps 601: }
602:
603:
1.53 kristaps 604: /*
605: * Parse free-form text, that is, a line that does not begin with the
606: * control character.
607: */
608: static int
1.68 kristaps 609: parsetext(struct mdoc *m, int line, char *buf)
1.1 kristaps 610: {
1.91 kristaps 611: int i, j;
1.1 kristaps 612:
1.85 kristaps 613: if (SEC_NONE == m->lastnamed)
1.88 kristaps 614: return(mdoc_perr(m, line, 0, ETEXTPROL));
1.91 kristaps 615:
616: /*
617: * If in literal mode, then pass the buffer directly to the
618: * back-end, as it should be preserved as a single term.
619: */
620:
1.101 kristaps 621: if (MDOC_LITERAL & m->flags)
622: return(mdoc_word_alloc(m, line, 0, buf));
1.91 kristaps 623:
624: /* Disallow blank/white-space lines in non-literal mode. */
1.1 kristaps 625:
1.91 kristaps 626: for (i = 0; ' ' == buf[i]; i++)
627: /* Skip leading whitespace. */ ;
628: if (0 == buf[i])
1.88 kristaps 629: return(mdoc_perr(m, line, 0, ENOBLANK));
1.68 kristaps 630:
1.91 kristaps 631: /*
632: * Break apart a free-form line into tokens. Spaces are
633: * stripped out of the input.
634: */
635:
636: for (j = i; buf[i]; i++) {
637: if (' ' != buf[i])
638: continue;
639:
640: /* Escaped whitespace. */
641: if (i && ' ' == buf[i] && '\\' == buf[i - 1])
642: continue;
643:
644: buf[i++] = 0;
645: if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
646: return(0);
647:
648: for ( ; ' ' == buf[i]; i++)
649: /* Skip trailing whitespace. */ ;
650:
651: j = i;
652: if (0 == buf[i])
653: break;
654: }
655:
656: if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.53 kristaps 657: return(0);
1.1 kristaps 658:
1.68 kristaps 659: m->next = MDOC_NEXT_SIBLING;
1.53 kristaps 660: return(1);
1.1 kristaps 661: }
662:
663:
1.91 kristaps 664:
665:
1.58 kristaps 666: static int
667: macrowarn(struct mdoc *m, int ln, const char *buf)
668: {
669: if ( ! (MDOC_IGN_MACRO & m->pflags))
1.96 kristaps 670: return(mdoc_verr(m, ln, 0,
1.73 kristaps 671: "unknown macro: %s%s",
1.59 kristaps 672: buf, strlen(buf) > 3 ? "..." : ""));
1.96 kristaps 673: return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59 kristaps 674: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 675: }
676:
677:
1.53 kristaps 678: /*
679: * Parse a macro line, that is, a line beginning with the control
680: * character.
681: */
682: int
683: parsemacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 684: {
1.103 kristaps 685: int i, j, c;
1.53 kristaps 686: char mac[5];
1.1 kristaps 687:
1.81 kristaps 688: /* Empty lines are ignored. */
1.63 kristaps 689:
690: if (0 == buf[1])
691: return(1);
692:
1.100 kristaps 693: i = 1;
694:
695: /* Accept whitespace after the initial control char. */
696:
697: if (' ' == buf[i]) {
698: i++;
1.65 kristaps 699: while (buf[i] && ' ' == buf[i])
1.63 kristaps 700: i++;
701: if (0 == buf[i])
702: return(1);
703: }
1.1 kristaps 704:
1.53 kristaps 705: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 706:
1.100 kristaps 707: for (j = 0; j < 4; j++, i++) {
708: if (0 == (mac[j] = buf[i]))
1.53 kristaps 709: break;
1.65 kristaps 710: else if (' ' == buf[i])
1.53 kristaps 711: break;
1.104 kristaps 712:
713: /* Check for invalid characters. */
714:
715: if (isgraph((u_char)buf[i]))
716: continue;
717: return(mdoc_perr(m, ln, i, EPRINT));
1.53 kristaps 718: }
1.1 kristaps 719:
1.100 kristaps 720: mac[j] = 0;
1.1 kristaps 721:
1.100 kristaps 722: if (j == 4 || j < 2) {
1.58 kristaps 723: if ( ! macrowarn(m, ln, mac))
724: goto err;
725: return(1);
1.53 kristaps 726: }
727:
1.105 kristaps 728: if (MDOC_MAX == (c = mdoc_hash_find(mac))) {
1.58 kristaps 729: if ( ! macrowarn(m, ln, mac))
730: goto err;
731: return(1);
1.53 kristaps 732: }
1.1 kristaps 733:
1.53 kristaps 734: /* The macro is sane. Jump to the next word. */
1.1 kristaps 735:
1.65 kristaps 736: while (buf[i] && ' ' == buf[i])
1.53 kristaps 737: i++;
1.1 kristaps 738:
1.98 kristaps 739: /*
740: * Begin recursive parse sequence. Since we're at the start of
741: * the line, we don't need to do callable/parseable checks.
742: */
1.103 kristaps 743: if ( ! mdoc_macro(m, c, ln, 1, &i, buf))
1.53 kristaps 744: goto err;
1.1 kristaps 745:
1.53 kristaps 746: return(1);
1.1 kristaps 747:
1.53 kristaps 748: err: /* Error out. */
1.1 kristaps 749:
1.53 kristaps 750: m->flags |= MDOC_HALT;
751: return(0);
1.1 kristaps 752: }
1.100 kristaps 753:
754:
CVSweb