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