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