Annotation of mandoc/mdoc.c, Revision 1.130
1.130 ! kristaps 1: /* $Id: mdoc.c,v 1.129 2010/05/12 17:08:03 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.123 kristaps 87: "deprecated comment style", /* EBADCOMMENT */
1.89 kristaps 88: };
89:
1.1 kristaps 90: const char *const __mdoc_macronames[MDOC_MAX] = {
1.82 kristaps 91: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 92: "Sh", "Ss", "Pp", "D1",
93: "Dl", "Bd", "Ed", "Bl",
94: "El", "It", "Ad", "An",
95: "Ar", "Cd", "Cm", "Dv",
96: "Er", "Ev", "Ex", "Fa",
97: "Fd", "Fl", "Fn", "Ft",
98: "Ic", "In", "Li", "Nd",
99: "Nm", "Op", "Ot", "Pa",
100: "Rv", "St", "Va", "Vt",
101: /* LINTED */
1.114 kristaps 102: "Xr", "%A", "%B", "%D",
1.1 kristaps 103: /* LINTED */
1.114 kristaps 104: "%I", "%J", "%N", "%O",
1.1 kristaps 105: /* LINTED */
1.114 kristaps 106: "%P", "%R", "%T", "%V",
1.1 kristaps 107: "Ac", "Ao", "Aq", "At",
108: "Bc", "Bf", "Bo", "Bq",
109: "Bsx", "Bx", "Db", "Dc",
110: "Do", "Dq", "Ec", "Ef",
111: "Em", "Eo", "Fx", "Ms",
112: "No", "Ns", "Nx", "Ox",
113: "Pc", "Pf", "Po", "Pq",
114: "Qc", "Ql", "Qo", "Qq",
115: "Re", "Rs", "Sc", "So",
116: "Sq", "Sm", "Sx", "Sy",
117: "Tn", "Ux", "Xc", "Xo",
118: "Fo", "Fc", "Oo", "Oc",
119: "Bk", "Ek", "Bt", "Hf",
1.82 kristaps 120: "Fr", "Ud", "Lb", "Lp",
121: "Lk", "Mt", "Brq", "Bro",
1.64 kristaps 122: /* LINTED */
1.114 kristaps 123: "Brc", "%C", "Es", "En",
1.69 kristaps 124: /* LINTED */
1.114 kristaps 125: "Dx", "%Q", "br", "sp",
1.110 kristaps 126: /* LINTED */
1.114 kristaps 127: "%U"
1.1 kristaps 128: };
129:
130: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
131: "split", "nosplit", "ragged",
132: "unfilled", "literal", "file",
133: "offset", "bullet", "dash",
134: "hyphen", "item", "enum",
135: "tag", "diag", "hang",
136: "ohang", "inset", "column",
137: "width", "compact", "std",
1.52 kristaps 138: "filled", "words", "emphasis",
1.108 kristaps 139: "symbolic", "nested", "centered"
1.1 kristaps 140: };
141:
142: const char * const *mdoc_macronames = __mdoc_macronames;
143: const char * const *mdoc_argnames = __mdoc_argnames;
144:
1.121 kristaps 145: static void mdoc_node_free(struct mdoc_node *);
146: static void mdoc_node_unlink(struct mdoc *,
147: struct mdoc_node *);
1.73 kristaps 148: static void mdoc_free1(struct mdoc *);
1.113 kristaps 149: static void mdoc_alloc1(struct mdoc *);
1.73 kristaps 150: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117 kristaps 151: enum mdoct, enum mdoc_type);
1.73 kristaps 152: static int node_append(struct mdoc *,
1.71 kristaps 153: struct mdoc_node *);
1.123 kristaps 154: static int mdoc_ptext(struct mdoc *, int, char *);
155: static int mdoc_pmacro(struct mdoc *, int, char *);
1.71 kristaps 156: static int macrowarn(struct mdoc *, int, const char *);
1.124 kristaps 157:
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.123 kristaps 283: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
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.130 ! kristaps 292: m->flags |= MDOC_NEWLINE;
! 293: return('.' == *buf ?
! 294: mdoc_pmacro(m, ln, buf) :
1.123 kristaps 295: mdoc_ptext(m, ln, buf));
1.1 kristaps 296: }
297:
298:
299: int
1.31 kristaps 300: mdoc_verr(struct mdoc *mdoc, int ln, int pos,
301: const char *fmt, ...)
1.1 kristaps 302: {
1.31 kristaps 303: char buf[256];
304: va_list ap;
1.1 kristaps 305:
306: if (NULL == mdoc->cb.mdoc_err)
307: return(0);
1.31 kristaps 308:
309: va_start(ap, fmt);
310: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
311: va_end(ap);
1.88 kristaps 312:
1.31 kristaps 313: return((*mdoc->cb.mdoc_err)(mdoc->data, ln, pos, buf));
1.1 kristaps 314: }
315:
316:
317: int
1.87 kristaps 318: mdoc_vwarn(struct mdoc *mdoc, int ln, int pos, const char *fmt, ...)
1.1 kristaps 319: {
1.31 kristaps 320: char buf[256];
321: va_list ap;
1.1 kristaps 322:
323: if (NULL == mdoc->cb.mdoc_warn)
324: return(0);
1.31 kristaps 325:
326: va_start(ap, fmt);
327: (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
328: va_end(ap);
1.88 kristaps 329:
1.87 kristaps 330: return((*mdoc->cb.mdoc_warn)(mdoc->data, ln, pos, buf));
1.1 kristaps 331: }
332:
333:
334: int
1.88 kristaps 335: mdoc_err(struct mdoc *m, int line, int pos, int iserr, enum merr type)
1.78 kristaps 336: {
1.89 kristaps 337: const char *p;
1.88 kristaps 338:
1.89 kristaps 339: p = __mdoc_merrnames[(int)type];
1.73 kristaps 340: assert(p);
1.88 kristaps 341:
342: if (iserr)
343: return(mdoc_verr(m, line, pos, p));
344:
345: return(mdoc_vwarn(m, line, pos, p));
346: }
347:
348:
349: int
1.117 kristaps 350: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88 kristaps 351: int ln, int pp, int *pos, char *buf)
352: {
1.122 kristaps 353: assert(tok < MDOC_MAX);
354:
355: /* If we're in the body, deny prologue calls. */
1.117 kristaps 356:
1.88 kristaps 357: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122 kristaps 358: MDOC_PBODY & m->flags)
359: return(mdoc_perr(m, ln, pp, EPROLBODY));
360:
361: /* If we're in the prologue, deny "body" macros. */
362:
363: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
364: ! (MDOC_PBODY & m->flags)) {
1.120 kristaps 365: if ( ! mdoc_pwarn(m, ln, pp, EBODYPROL))
366: return(0);
367: if (NULL == m->meta.title)
368: m->meta.title = mandoc_strdup("unknown");
369: if (NULL == m->meta.vol)
370: m->meta.vol = mandoc_strdup("local");
371: if (NULL == m->meta.os)
372: m->meta.os = mandoc_strdup("local");
373: if (0 == m->meta.date)
374: m->meta.date = time(NULL);
375: m->flags |= MDOC_PBODY;
376: }
1.88 kristaps 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.130 ! kristaps 458: if (MDOC_NEWLINE & m->flags)
! 459: p->flags |= MDOC_LINE;
! 460: m->flags &= ~MDOC_NEWLINE;
1.46 kristaps 461: return(p);
1.45 kristaps 462: }
463:
464:
1.23 kristaps 465: int
1.117 kristaps 466: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17 kristaps 467: {
468: struct mdoc_node *p;
469:
1.91 kristaps 470: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102 kristaps 471: if ( ! node_append(m, p))
472: return(0);
473: m->next = MDOC_NEXT_CHILD;
474: return(1);
1.17 kristaps 475: }
476:
477:
1.23 kristaps 478: int
1.117 kristaps 479: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 480: {
481: struct mdoc_node *p;
482:
1.91 kristaps 483: assert(m->first);
484: assert(m->last);
1.1 kristaps 485:
1.91 kristaps 486: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102 kristaps 487: if ( ! node_append(m, p))
488: return(0);
489: m->next = MDOC_NEXT_CHILD;
490: return(1);
1.1 kristaps 491: }
492:
493:
1.23 kristaps 494: int
1.117 kristaps 495: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 496: {
497: struct mdoc_node *p;
498:
1.91 kristaps 499: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102 kristaps 500: if ( ! node_append(m, p))
501: return(0);
502: m->next = MDOC_NEXT_CHILD;
503: return(1);
1.1 kristaps 504: }
505:
506:
1.23 kristaps 507: int
1.91 kristaps 508: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 509: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 510: {
511: struct mdoc_node *p;
512:
1.91 kristaps 513: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
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.117 kristaps 526: enum mdoct 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.77 kristaps 531: p->args = args;
532: if (p->args)
1.53 kristaps 533: (args->refcnt)++;
1.102 kristaps 534: if ( ! node_append(m, p))
535: return(0);
536: m->next = MDOC_NEXT_CHILD;
537: return(1);
1.1 kristaps 538: }
539:
540:
1.124 kristaps 541: int
542: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1 kristaps 543: {
1.91 kristaps 544: struct mdoc_node *n;
1.124 kristaps 545: size_t sv, len;
546:
547: len = strlen(p);
1.1 kristaps 548:
1.125 kristaps 549: n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113 kristaps 550: n->string = mandoc_malloc(len + 1);
1.91 kristaps 551: sv = strlcpy(n->string, p, len + 1);
552:
553: /* Prohibit truncation. */
554: assert(sv < len + 1);
555:
1.101 kristaps 556: if ( ! node_append(m, n))
557: return(0);
1.124 kristaps 558:
1.101 kristaps 559: m->next = MDOC_NEXT_SIBLING;
560: return(1);
1.91 kristaps 561: }
562:
563:
1.53 kristaps 564: void
565: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 566: {
567:
1.53 kristaps 568: if (p->string)
569: free(p->string);
570: if (p->args)
571: mdoc_argv_free(p->args);
1.1 kristaps 572: free(p);
573: }
574:
575:
1.121 kristaps 576: static void
577: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
578: {
579:
580: /* Adjust siblings. */
581:
582: if (n->prev)
583: n->prev->next = n->next;
584: if (n->next)
585: n->next->prev = n->prev;
586:
587: /* Adjust parent. */
588:
589: if (n->parent) {
590: n->parent->nchild--;
591: if (n->parent->child == n)
592: n->parent->child = n->prev ? n->prev : n->next;
593: }
594:
595: /* Adjust parse point, if applicable. */
596:
597: if (m && m->last == n) {
598: if (n->prev) {
599: m->last = n->prev;
600: m->next = MDOC_NEXT_SIBLING;
601: } else {
602: m->last = n->parent;
603: m->next = MDOC_NEXT_CHILD;
604: }
605: }
606:
607: if (m && m->first == n)
608: m->first = NULL;
609: }
610:
611:
1.53 kristaps 612: void
1.121 kristaps 613: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 614: {
615:
1.121 kristaps 616: while (p->child) {
617: assert(p->nchild);
618: mdoc_node_delete(m, p->child);
619: }
620: assert(0 == p->nchild);
1.1 kristaps 621:
1.121 kristaps 622: mdoc_node_unlink(m, p);
1.53 kristaps 623: mdoc_node_free(p);
1.1 kristaps 624: }
625:
626:
1.53 kristaps 627: /*
628: * Parse free-form text, that is, a line that does not begin with the
629: * control character.
630: */
631: static int
1.123 kristaps 632: mdoc_ptext(struct mdoc *m, int line, char *buf)
1.1 kristaps 633: {
1.124 kristaps 634: int i;
1.1 kristaps 635:
1.123 kristaps 636: /* Ignore bogus comments. */
637:
638: if ('\\' == buf[0] && '.' == buf[1] && '\"' == buf[2])
639: return(mdoc_pwarn(m, line, 0, EBADCOMMENT));
640:
1.124 kristaps 641: /* No text before an initial macro. */
642:
1.85 kristaps 643: if (SEC_NONE == m->lastnamed)
1.88 kristaps 644: return(mdoc_perr(m, line, 0, ETEXTPROL));
1.124 kristaps 645:
646: /* Literal just gets pulled in as-is. */
1.91 kristaps 647:
1.101 kristaps 648: if (MDOC_LITERAL & m->flags)
649: return(mdoc_word_alloc(m, line, 0, buf));
1.91 kristaps 650:
1.124 kristaps 651: /* Check for a blank line, which may also consist of spaces. */
1.1 kristaps 652:
1.91 kristaps 653: for (i = 0; ' ' == buf[i]; i++)
1.124 kristaps 654: /* Skip to first non-space. */ ;
1.115 kristaps 655:
1.119 kristaps 656: if ('\0' == buf[i]) {
657: if ( ! mdoc_pwarn(m, line, 0, ENOBLANK))
658: return(0);
1.124 kristaps 659:
1.119 kristaps 660: /*
1.124 kristaps 661: * Insert a `Pp' in the case of a blank line. Technically,
662: * blank lines aren't allowed, but enough manuals assume this
663: * behaviour that we want to work around it.
1.119 kristaps 664: */
665: if ( ! mdoc_elem_alloc(m, line, 0, MDOC_Pp, NULL))
666: return(0);
1.124 kristaps 667:
668: m->next = MDOC_NEXT_SIBLING;
669: return(1);
1.119 kristaps 670: }
1.68 kristaps 671:
1.126 kristaps 672: /*
673: * Warn if the last un-escaped character is whitespace. Then
674: * strip away the remaining spaces (tabs stay!).
675: */
1.91 kristaps 676:
1.124 kristaps 677: i = (int)strlen(buf);
678: assert(i);
1.116 kristaps 679:
1.126 kristaps 680: if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.127 kristaps 681: if (i > 1 && '\\' != buf[i - 2])
1.116 kristaps 682: if ( ! mdoc_pwarn(m, line, i - 1, ETAILWS))
683: return(0);
1.126 kristaps 684:
685: for (--i; i && ' ' == buf[i]; i--)
686: /* Spin back to non-space. */ ;
687:
688: /* Jump ahead of escaped whitespace. */
689: i += '\\' == buf[i] ? 2 : 1;
690:
691: buf[i] = '\0';
692: }
1.116 kristaps 693:
1.124 kristaps 694: /* Allocate the whole word. */
1.1 kristaps 695:
1.128 kristaps 696: if ( ! mdoc_word_alloc(m, line, 0, buf))
697: return(0);
698:
699: /*
700: * End-of-sentence check. If the last character is an unescaped
701: * EOS character, then flag the node as being the end of a
702: * sentence. The front-end will know how to interpret this.
703: */
704:
705: assert(i);
706:
1.129 kristaps 707: if (mandoc_eos(buf, (size_t)i))
1.128 kristaps 708: m->last->flags |= MDOC_EOS;
709:
710: return(1);
1.1 kristaps 711: }
712:
713:
1.58 kristaps 714: static int
715: macrowarn(struct mdoc *m, int ln, const char *buf)
716: {
717: if ( ! (MDOC_IGN_MACRO & m->pflags))
1.125 kristaps 718: return(mdoc_verr(m, ln, 0, "unknown macro: %s%s",
1.59 kristaps 719: buf, strlen(buf) > 3 ? "..." : ""));
1.96 kristaps 720: return(mdoc_vwarn(m, ln, 0, "unknown macro: %s%s",
1.59 kristaps 721: buf, strlen(buf) > 3 ? "..." : ""));
1.58 kristaps 722: }
723:
724:
1.53 kristaps 725: /*
726: * Parse a macro line, that is, a line beginning with the control
727: * character.
728: */
729: int
1.123 kristaps 730: mdoc_pmacro(struct mdoc *m, int ln, char *buf)
1.1 kristaps 731: {
1.125 kristaps 732: enum mdoct tok;
1.130 ! kristaps 733: int i, j, sv;
1.125 kristaps 734: char mac[5];
1.1 kristaps 735:
1.81 kristaps 736: /* Empty lines are ignored. */
1.63 kristaps 737:
1.115 kristaps 738: if ('\0' == buf[1])
1.63 kristaps 739: return(1);
740:
1.100 kristaps 741: i = 1;
742:
743: /* Accept whitespace after the initial control char. */
744:
745: if (' ' == buf[i]) {
746: i++;
1.65 kristaps 747: while (buf[i] && ' ' == buf[i])
1.63 kristaps 748: i++;
1.115 kristaps 749: if ('\0' == buf[i])
1.63 kristaps 750: return(1);
751: }
1.1 kristaps 752:
1.130 ! kristaps 753: sv = i;
! 754:
1.53 kristaps 755: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 756:
1.100 kristaps 757: for (j = 0; j < 4; j++, i++) {
1.115 kristaps 758: if ('\0' == (mac[j] = buf[i]))
1.53 kristaps 759: break;
1.65 kristaps 760: else if (' ' == buf[i])
1.53 kristaps 761: break;
1.104 kristaps 762:
763: /* Check for invalid characters. */
764:
765: if (isgraph((u_char)buf[i]))
766: continue;
767: return(mdoc_perr(m, ln, i, EPRINT));
1.53 kristaps 768: }
1.1 kristaps 769:
1.100 kristaps 770: mac[j] = 0;
1.1 kristaps 771:
1.100 kristaps 772: if (j == 4 || j < 2) {
1.58 kristaps 773: if ( ! macrowarn(m, ln, mac))
774: goto err;
775: return(1);
1.53 kristaps 776: }
777:
1.125 kristaps 778: if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.58 kristaps 779: if ( ! macrowarn(m, ln, mac))
780: goto err;
781: return(1);
1.53 kristaps 782: }
1.1 kristaps 783:
1.53 kristaps 784: /* The macro is sane. Jump to the next word. */
1.1 kristaps 785:
1.65 kristaps 786: while (buf[i] && ' ' == buf[i])
1.53 kristaps 787: i++;
1.1 kristaps 788:
1.125 kristaps 789: /*
790: * Trailing whitespace. Note that tabs are allowed to be passed
791: * into the parser as "text", so we only warn about spaces here.
792: */
1.115 kristaps 793:
794: if ('\0' == buf[i] && ' ' == buf[i - 1])
795: if ( ! mdoc_pwarn(m, ln, i - 1, ETAILWS))
796: goto err;
797:
1.98 kristaps 798: /*
799: * Begin recursive parse sequence. Since we're at the start of
800: * the line, we don't need to do callable/parseable checks.
801: */
1.130 ! kristaps 802: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.53 kristaps 803: goto err;
1.1 kristaps 804:
1.53 kristaps 805: return(1);
1.1 kristaps 806:
1.53 kristaps 807: err: /* Error out. */
1.1 kristaps 808:
1.53 kristaps 809: m->flags |= MDOC_HALT;
810: return(0);
1.1 kristaps 811: }
1.100 kristaps 812:
813:
CVSweb