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