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