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