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