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