Annotation of mandoc/mdoc.c, Revision 1.134
1.134 ! kristaps 1: /* $Id: mdoc.c,v 1.133 2010/05/15 16:24:37 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 */
1.133 kristaps 71: "invalid manual section", /* EBADMSEC */
1.89 kristaps 72: "invalid font mode", /* EFONT */
73: "invalid date syntax", /* EBADDATE */
74: "invalid number format", /* ENUMFMT */
75: "superfluous width argument", /* ENOWIDTH */
76: "system: utsname error", /* EUTSNAME */
77: "obsolete macro", /* EOBS */
78: "end-of-line scope violation", /* EIMPBRK */
79: "empty macro ignored", /* EIGNE */
80: "unclosed explicit scope", /* EOPEN */
81: "unterminated quoted phrase", /* EQUOTPHR */
82: "closure macro without prior context", /* ENOCTX */
1.107 kristaps 83: "no description found for library", /* ELIB */
84: "bad child for parent context", /* EBADCHILD */
1.109 kristaps 85: "list arguments preceding type", /* ENOTYPE */
1.123 kristaps 86: "deprecated comment style", /* EBADCOMMENT */
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 *);
1.123 kristaps 153: static int mdoc_ptext(struct mdoc *, int, char *);
154: static int mdoc_pmacro(struct mdoc *, int, char *);
1.71 kristaps 155: static int macrowarn(struct mdoc *, int, const char *);
1.124 kristaps 156:
1.88 kristaps 157:
1.1 kristaps 158: const struct mdoc_node *
1.71 kristaps 159: mdoc_node(const struct mdoc *m)
1.1 kristaps 160: {
161:
1.71 kristaps 162: return(MDOC_HALT & m->flags ? NULL : m->first);
1.1 kristaps 163: }
164:
165:
1.37 kristaps 166: const struct mdoc_meta *
1.71 kristaps 167: mdoc_meta(const struct mdoc *m)
1.37 kristaps 168: {
169:
1.71 kristaps 170: return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37 kristaps 171: }
172:
173:
1.85 kristaps 174: /*
175: * Frees volatile resources (parse tree, meta-data, fields).
176: */
1.73 kristaps 177: static void
178: mdoc_free1(struct mdoc *mdoc)
1.67 kristaps 179: {
180:
181: if (mdoc->first)
1.121 kristaps 182: mdoc_node_delete(mdoc, mdoc->first);
1.67 kristaps 183: if (mdoc->meta.title)
184: free(mdoc->meta.title);
185: if (mdoc->meta.os)
186: free(mdoc->meta.os);
187: if (mdoc->meta.name)
188: free(mdoc->meta.name);
189: if (mdoc->meta.arch)
190: free(mdoc->meta.arch);
191: if (mdoc->meta.vol)
192: free(mdoc->meta.vol);
1.133 kristaps 193: if (mdoc->meta.msec)
194: free(mdoc->meta.msec);
1.73 kristaps 195: }
196:
197:
1.85 kristaps 198: /*
199: * Allocate all volatile resources (parse tree, meta-data, fields).
200: */
1.113 kristaps 201: static void
1.73 kristaps 202: mdoc_alloc1(struct mdoc *mdoc)
203: {
1.67 kristaps 204:
1.112 kristaps 205: memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67 kristaps 206: mdoc->flags = 0;
1.85 kristaps 207: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113 kristaps 208: mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70 kristaps 209: mdoc->first = mdoc->last;
1.67 kristaps 210: mdoc->last->type = MDOC_ROOT;
211: mdoc->next = MDOC_NEXT_CHILD;
1.73 kristaps 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: */
1.113 kristaps 221: void
1.73 kristaps 222: mdoc_reset(struct mdoc *mdoc)
223: {
224:
225: mdoc_free1(mdoc);
1.113 kristaps 226: 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.113 kristaps 251: p = mandoc_calloc(1, sizeof(struct mdoc));
252:
1.74 kristaps 253: if (cb)
1.113 kristaps 254: memcpy(&p->cb, cb, sizeof(struct mdoc_cb));
1.105 kristaps 255:
1.1 kristaps 256: p->data = data;
1.73 kristaps 257: p->pflags = pflags;
258:
1.113 kristaps 259: mdoc_hash_init();
260: mdoc_alloc1(p);
261: return(p);
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.123 kristaps 284: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
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.130 kristaps 293: m->flags |= MDOC_NEWLINE;
1.134 ! kristaps 294: return(('.' == *buf || '\'' == *buf) ?
1.130 kristaps 295: mdoc_pmacro(m, ln, buf) :
1.123 kristaps 296: mdoc_ptext(m, ln, buf));
1.1 kristaps 297: }
298:
299:
300: int
1.31 kristaps 301: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
302: const char *fmt, ...)
1.1 kristaps 303: {
1.31 kristaps 304: char buf[256];
305: va_list ap;
1.1 kristaps 306:
307: if (NULL == mdoc->cb.mdoc_err)
308: return(0);
1.31 kristaps 309:
310: va_start(ap, fmt);
311: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
312: va_end(ap);
1.88 kristaps 313:
1.31 kristaps 314: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 315: }
316:
317:
318: int
1.87 kristaps 319: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 320: {
1.31 kristaps 321: char buf[256];
322: va_list ap;
1.1 kristaps 323:
324: if (NULL == mdoc->cb.mdoc_warn)
325: return(0);
1.31 kristaps 326:
327: va_start(ap, fmt);
328: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
329: va_end(ap);
1.88 kristaps 330:
1.87 kristaps 331: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1 kristaps 332: }
333:
334:
335: int
1.88 kristaps 336: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78 kristaps 337: {
1.89 kristaps 338: const char *p;
1.88 kristaps 339:
1.89 kristaps 340: p = __mdoc_merrnames[(int)type];
1.73 kristaps 341: assert(p);
1.88 kristaps 342:
343: if (iserr)
344: return(mdoc_verr(m, line, pos, p));
345:
346: return(mdoc_vwarn(m, line, pos, p));
347: }
348:
349:
350: int
1.117 kristaps 351: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88 kristaps 352: int ln, int pp, int *pos, char *buf)
353: {
1.122 kristaps 354: assert(tok < MDOC_MAX);
355:
356: /* If we're in the body, deny prologue calls. */
1.117 kristaps 357:
1.88 kristaps 358: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122 kristaps 359: MDOC_PBODY & m->flags)
360: return(mdoc_perr(m, ln, pp, EPROLBODY));
361:
362: /* If we're in the prologue, deny "body" macros. */
363:
364: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
365: ! (MDOC_PBODY & m->flags)) {
1.120 kristaps 366: if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
367: return(0);
368: if (NULL == m->meta.title)
369: m->meta.title = mandoc_strdup("unknown");
370: if (NULL == m->meta.vol)
371: m->meta.vol = mandoc_strdup("local");
372: if (NULL == m->meta.os)
373: m->meta.os = mandoc_strdup("local");
374: if (0 == m->meta.date)
375: m->meta.date = time(NULL);
376: m->flags |= MDOC_PBODY;
377: }
1.88 kristaps 378:
379: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73 kristaps 380: }
381:
382:
383: static int
384: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 385: {
386:
1.25 kristaps 387: assert(mdoc->last);
388: assert(mdoc->first);
389: assert(MDOC_ROOT != p->type);
1.1 kristaps 390:
1.13 kristaps 391: switch (mdoc->next) {
392: case (MDOC_NEXT_SIBLING):
1.6 kristaps 393: mdoc->last->next = p;
394: p->prev = mdoc->last;
1.13 kristaps 395: p->parent = mdoc->last->parent;
1.1 kristaps 396: break;
1.13 kristaps 397: case (MDOC_NEXT_CHILD):
398: mdoc->last->child = p;
1.1 kristaps 399: p->parent = mdoc->last;
400: break;
401: default:
1.13 kristaps 402: abort();
403: /* NOTREACHED */
1.1 kristaps 404: }
405:
1.86 kristaps 406: p->parent->nchild++;
407:
1.23 kristaps 408: if ( ! mdoc_valid_pre(mdoc, p))
409: return(0);
1.68 kristaps 410: if ( ! mdoc_action_pre(mdoc, p))
411: return(0);
1.27 kristaps 412:
413: switch (p->type) {
414: case (MDOC_HEAD):
415: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 416: p->parent->head = p;
1.27 kristaps 417: break;
418: case (MDOC_TAIL):
419: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 420: p->parent->tail = p;
1.27 kristaps 421: break;
422: case (MDOC_BODY):
423: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 424: p->parent->body = p;
1.27 kristaps 425: break;
426: default:
427: break;
428: }
429:
1.1 kristaps 430: mdoc->last = p;
1.71 kristaps 431:
432: switch (p->type) {
433: case (MDOC_TEXT):
434: if ( ! mdoc_valid_post(mdoc))
435: return(0);
436: if ( ! mdoc_action_post(mdoc))
437: return(0);
438: break;
439: default:
440: break;
441: }
442:
1.23 kristaps 443: return(1);
1.1 kristaps 444: }
445:
446:
1.45 kristaps 447: static struct mdoc_node *
1.117 kristaps 448: node_alloc(struct mdoc *m, int line, int pos,
449: enum mdoct tok, enum mdoc_type type)
1.45 kristaps 450: {
1.46 kristaps 451: struct mdoc_node *p;
452:
1.113 kristaps 453: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91 kristaps 454: p->sec = m->lastsec;
1.73 kristaps 455: p->line = line;
456: p->pos = pos;
457: p->tok = tok;
1.118 kristaps 458: p->type = type;
1.130 kristaps 459: if (MDOC_NEWLINE & m->flags)
460: p->flags |= MDOC_LINE;
461: m->flags &= ~MDOC_NEWLINE;
1.46 kristaps 462: return(p);
1.45 kristaps 463: }
464:
465:
1.23 kristaps 466: int
1.117 kristaps 467: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17 kristaps 468: {
469: struct mdoc_node *p;
470:
1.91 kristaps 471: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102 kristaps 472: if ( ! node_append(m, p))
473: return(0);
474: m->next = MDOC_NEXT_CHILD;
475: return(1);
1.17 kristaps 476: }
477:
478:
1.23 kristaps 479: int
1.117 kristaps 480: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 481: {
482: struct mdoc_node *p;
483:
1.91 kristaps 484: assert(m->first);
485: assert(m->last);
1.1 kristaps 486:
1.91 kristaps 487: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102 kristaps 488: if ( ! node_append(m, p))
489: return(0);
490: m->next = MDOC_NEXT_CHILD;
491: return(1);
1.1 kristaps 492: }
493:
494:
1.23 kristaps 495: int
1.117 kristaps 496: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 497: {
498: struct mdoc_node *p;
499:
1.91 kristaps 500: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102 kristaps 501: if ( ! node_append(m, p))
502: return(0);
503: m->next = MDOC_NEXT_CHILD;
504: return(1);
1.1 kristaps 505: }
506:
507:
1.23 kristaps 508: int
1.91 kristaps 509: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 510: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 511: {
512: struct mdoc_node *p;
513:
1.91 kristaps 514: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77 kristaps 515: p->args = args;
516: if (p->args)
1.53 kristaps 517: (args->refcnt)++;
1.102 kristaps 518: if ( ! node_append(m, p))
519: return(0);
520: m->next = MDOC_NEXT_CHILD;
521: return(1);
1.1 kristaps 522: }
523:
524:
1.23 kristaps 525: int
1.91 kristaps 526: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 527: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 528: {
529: struct mdoc_node *p;
530:
1.91 kristaps 531: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77 kristaps 532: p->args = args;
533: if (p->args)
1.53 kristaps 534: (args->refcnt)++;
1.102 kristaps 535: if ( ! node_append(m, p))
536: return(0);
537: m->next = MDOC_NEXT_CHILD;
538: return(1);
1.1 kristaps 539: }
540:
541:
1.124 kristaps 542: int
543: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1 kristaps 544: {
1.91 kristaps 545: struct mdoc_node *n;
1.124 kristaps 546: size_t sv, len;
547:
548: len = strlen(p);
1.1 kristaps 549:
1.125 kristaps 550: n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113 kristaps 551: n->string = mandoc_malloc(len + 1);
1.91 kristaps 552: sv = strlcpy(n->string, p, len + 1);
553:
554: /* Prohibit truncation. */
555: assert(sv < len + 1);
556:
1.101 kristaps 557: if ( ! node_append(m, n))
558: return(0);
1.124 kristaps 559:
1.101 kristaps 560: m->next = MDOC_NEXT_SIBLING;
561: return(1);
1.91 kristaps 562: }
563:
564:
1.53 kristaps 565: void
566: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 567: {
568:
1.53 kristaps 569: if (p->string)
570: free(p->string);
571: if (p->args)
572: mdoc_argv_free(p->args);
1.1 kristaps 573: free(p);
574: }
575:
576:
1.121 kristaps 577: static void
578: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
579: {
580:
581: /* Adjust siblings. */
582:
583: if (n->prev)
584: n->prev->next = n->next;
585: if (n->next)
586: n->next->prev = n->prev;
587:
588: /* Adjust parent. */
589:
590: if (n->parent) {
591: n->parent->nchild--;
592: if (n->parent->child == n)
593: n->parent->child = n->prev ? n->prev : n->next;
594: }
595:
596: /* Adjust parse point, if applicable. */
597:
598: if (m && m->last == n) {
599: if (n->prev) {
600: m->last = n->prev;
601: m->next = MDOC_NEXT_SIBLING;
602: } else {
603: m->last = n->parent;
604: m->next = MDOC_NEXT_CHILD;
605: }
606: }
607:
608: if (m && m->first == n)
609: m->first = NULL;
610: }
611:
612:
1.53 kristaps 613: void
1.121 kristaps 614: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 615: {
616:
1.121 kristaps 617: while (p->child) {
618: assert(p->nchild);
619: mdoc_node_delete(m, p->child);
620: }
621: assert(0 == p->nchild);
1.1 kristaps 622:
1.121 kristaps 623: mdoc_node_unlink(m, p);
1.53 kristaps 624: mdoc_node_free(p);
1.1 kristaps 625: }
626:
627:
1.53 kristaps 628: /*
629: * Parse free-form text, that is, a line that does not begin with the
630: * control character.
631: */
632: static int
1.123 kristaps 633: mdoc_ptext(struct mdoc *m, int line, char *buf)
1.1 kristaps 634: {
1.124 kristaps 635: int i;
1.1 kristaps 636:
1.123 kristaps 637: /* Ignore bogus comments. */
638:
639: if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
640: return(mdoc_pwarn(m, line, 0, EBADCOMMENT));
641:
1.124 kristaps 642: /* No text before an initial macro. */
643:
1.85 kristaps 644: if (SEC_NONE == m->lastnamed)
1.88 kristaps 645: return(mdoc_perr(m, line, 0, ETEXTPROL));
1.124 kristaps 646:
647: /* Literal just gets pulled in as-is. */
1.91 kristaps 648:
1.101 kristaps 649: if (MDOC_LITERAL & m->flags)
650: return(mdoc_word_alloc(m, line, 0, buf));
1.91 kristaps 651:
1.124 kristaps 652: /* Check for a blank line, which may also consist of spaces. */
1.1 kristaps 653:
1.91 kristaps 654: for (i = 0; ' ' == buf[i]; i++)
1.124 kristaps 655: /* Skip to first non-space. */ ;
1.115 kristaps 656:
1.119 kristaps 657: if ('\0' == buf[i]) {
658: if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
659: return(0);
1.124 kristaps 660:
1.119 kristaps 661: /*
1.124 kristaps 662: * Insert a `Pp' in the case of a blank line. Technically,
663: * blank lines aren't allowed, but enough manuals assume this
664: * behaviour that we want to work around it.
1.119 kristaps 665: */
666: if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
667: return(0);
1.124 kristaps 668:
669: m->next = MDOC_NEXT_SIBLING;
670: return(1);
1.119 kristaps 671: }
1.68 kristaps 672:
1.126 kristaps 673: /*
674: * Warn if the last un-escaped character is whitespace. Then
675: * strip away the remaining spaces (tabs stay!).
676: */
1.91 kristaps 677:
1.124 kristaps 678: i = (int)strlen(buf);
679: assert(i);
1.116 kristaps 680:
1.126 kristaps 681: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.127 kristaps 682: if (i > 1 && '\\' != buf[i - 2])
1.116 kristaps 683: if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
684: return(0);
1.126 kristaps 685:
686: for (--i; i && ' ' == buf[i]; i--)
687: /* Spin back to non-space. */ ;
688:
689: /* Jump ahead of escaped whitespace. */
690: i += '\\' == buf[i] ? 2 : 1;
691:
692: buf[i] = '\0';
693: }
1.116 kristaps 694:
1.124 kristaps 695: /* Allocate the whole word. */
1.1 kristaps 696:
1.128 kristaps 697: if ( ! mdoc_word_alloc(m, line, 0, buf))
698: return(0);
699:
700: /*
701: * End-of-sentence check. If the last character is an unescaped
702: * EOS character, then flag the node as being the end of a
703: * sentence. The front-end will know how to interpret this.
704: */
1.132 kristaps 705:
706: /* FIXME: chain of close delims. */
1.128 kristaps 707:
708: assert(i);
709:
1.129 kristaps 710: if (mandoc_eos(buf, (size_t)i))
1.128 kristaps 711: m->last->flags |= MDOC_EOS;
712:
713: return(1);
1.1 kristaps 714: }
715:
716:
1.58 kristaps 717: static int
718: macrowarn(struct mdoc *m, int ln, const char *buf)
719: {
720: if ( ! (MDOC_IGN_MACRO & m->pflags))
1.125 kristaps 721: return(mdoc_verr(m, ln, 0, "unknown macro: %s%s",
1.59 kristaps 722: buf, strlen(buf) > 3 ? "..." : ""));
1.96 kristaps 723: return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59 kristaps 724: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 725: }
726:
727:
1.53 kristaps 728: /*
729: * Parse a macro line, that is, a line beginning with the control
730: * character.
731: */
732: int
1.123 kristaps 733: mdoc_pmacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 734: {
1.125 kristaps 735: enum mdoct tok;
1.130 kristaps 736: int i, j, sv;
1.125 kristaps 737: char mac[5];
1.1 kristaps 738:
1.81 kristaps 739: /* Empty lines are ignored. */
1.63 kristaps 740:
1.115 kristaps 741: if ('\0' == buf[1])
1.63 kristaps 742: return(1);
743:
1.100 kristaps 744: i = 1;
745:
746: /* Accept whitespace after the initial control char. */
747:
748: if (' ' == buf[i]) {
749: i++;
1.65 kristaps 750: while (buf[i] && ' ' == buf[i])
1.63 kristaps 751: i++;
1.115 kristaps 752: if ('\0' == buf[i])
1.63 kristaps 753: return(1);
754: }
1.1 kristaps 755:
1.130 kristaps 756: sv = i;
757:
1.53 kristaps 758: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 759:
1.100 kristaps 760: for (j = 0; j < 4; j++, i++) {
1.115 kristaps 761: if ('\0' == (mac[j] = buf[i]))
1.53 kristaps 762: break;
1.65 kristaps 763: else if (' ' == buf[i])
1.53 kristaps 764: break;
1.104 kristaps 765:
766: /* Check for invalid characters. */
767:
768: if (isgraph((u_char)buf[i]))
769: continue;
770: return(mdoc_perr(m, ln, i, EPRINT));
1.53 kristaps 771: }
1.1 kristaps 772:
1.100 kristaps 773: mac[j] = 0;
1.1 kristaps 774:
1.100 kristaps 775: if (j == 4 || j < 2) {
1.58 kristaps 776: if ( ! macrowarn(m, ln, mac))
777: goto err;
778: return(1);
1.53 kristaps 779: }
780:
1.125 kristaps 781: if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.58 kristaps 782: if ( ! macrowarn(m, ln, mac))
783: goto err;
784: return(1);
1.53 kristaps 785: }
1.1 kristaps 786:
1.53 kristaps 787: /* The macro is sane. Jump to the next word. */
1.1 kristaps 788:
1.65 kristaps 789: while (buf[i] && ' ' == buf[i])
1.53 kristaps 790: i++;
1.1 kristaps 791:
1.125 kristaps 792: /*
793: * Trailing whitespace. Note that tabs are allowed to be passed
794: * into the parser as "text", so we only warn about spaces here.
795: */
1.115 kristaps 796:
797: if ('\0' == buf[i] && ' ' == buf[i - 1])
798: if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
799: goto err;
800:
1.98 kristaps 801: /*
802: * Begin recursive parse sequence. Since we're at the start of
803: * the line, we don't need to do callable/parseable checks.
804: */
1.130 kristaps 805: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.53 kristaps 806: goto err;
1.1 kristaps 807:
1.53 kristaps 808: return(1);
1.1 kristaps 809:
1.53 kristaps 810: err: /* Error out. */
1.1 kristaps 811:
1.53 kristaps 812: m->flags |= MDOC_HALT;
813: return(0);
1.1 kristaps 814: }
1.100 kristaps 815:
816:
CVSweb