Annotation of mandoc/mdoc.c, Revision 1.140
1.140 ! kristaps 1: /* $Id: mdoc.c,v 1.139 2010/05/26 09:35:35 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.136 kristaps 31: #include "mandoc.h"
1.70 kristaps 32: #include "libmdoc.h"
1.113 kristaps 33: #include "libmandoc.h"
1.1 kristaps 34:
35: const char *const __mdoc_macronames[MDOC_MAX] = {
1.82 kristaps 36: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 37: "Sh", "Ss", "Pp", "D1",
38: "Dl", "Bd", "Ed", "Bl",
39: "El", "It", "Ad", "An",
40: "Ar", "Cd", "Cm", "Dv",
41: "Er", "Ev", "Ex", "Fa",
42: "Fd", "Fl", "Fn", "Ft",
43: "Ic", "In", "Li", "Nd",
44: "Nm", "Op", "Ot", "Pa",
45: "Rv", "St", "Va", "Vt",
46: /* LINTED */
1.114 kristaps 47: "Xr", "%A", "%B", "%D",
1.1 kristaps 48: /* LINTED */
1.114 kristaps 49: "%I", "%J", "%N", "%O",
1.1 kristaps 50: /* LINTED */
1.114 kristaps 51: "%P", "%R", "%T", "%V",
1.1 kristaps 52: "Ac", "Ao", "Aq", "At",
53: "Bc", "Bf", "Bo", "Bq",
54: "Bsx", "Bx", "Db", "Dc",
55: "Do", "Dq", "Ec", "Ef",
56: "Em", "Eo", "Fx", "Ms",
57: "No", "Ns", "Nx", "Ox",
58: "Pc", "Pf", "Po", "Pq",
59: "Qc", "Ql", "Qo", "Qq",
60: "Re", "Rs", "Sc", "So",
61: "Sq", "Sm", "Sx", "Sy",
62: "Tn", "Ux", "Xc", "Xo",
63: "Fo", "Fc", "Oo", "Oc",
64: "Bk", "Ek", "Bt", "Hf",
1.82 kristaps 65: "Fr", "Ud", "Lb", "Lp",
66: "Lk", "Mt", "Brq", "Bro",
1.64 kristaps 67: /* LINTED */
1.114 kristaps 68: "Brc", "%C", "Es", "En",
1.69 kristaps 69: /* LINTED */
1.114 kristaps 70: "Dx", "%Q", "br", "sp",
1.110 kristaps 71: /* LINTED */
1.114 kristaps 72: "%U"
1.1 kristaps 73: };
74:
75: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
76: "split", "nosplit", "ragged",
77: "unfilled", "literal", "file",
78: "offset", "bullet", "dash",
79: "hyphen", "item", "enum",
80: "tag", "diag", "hang",
81: "ohang", "inset", "column",
82: "width", "compact", "std",
1.52 kristaps 83: "filled", "words", "emphasis",
1.108 kristaps 84: "symbolic", "nested", "centered"
1.1 kristaps 85: };
86:
87: const char * const *mdoc_macronames = __mdoc_macronames;
88: const char * const *mdoc_argnames = __mdoc_argnames;
89:
1.121 kristaps 90: static void mdoc_node_free(struct mdoc_node *);
91: static void mdoc_node_unlink(struct mdoc *,
92: struct mdoc_node *);
1.73 kristaps 93: static void mdoc_free1(struct mdoc *);
1.113 kristaps 94: static void mdoc_alloc1(struct mdoc *);
1.73 kristaps 95: static struct mdoc_node *node_alloc(struct mdoc *, int, int,
1.117 kristaps 96: enum mdoct, enum mdoc_type);
1.73 kristaps 97: static int node_append(struct mdoc *,
1.71 kristaps 98: struct mdoc_node *);
1.135 kristaps 99: static int mdoc_ptext(struct mdoc *, int, char *, int);
100: static int mdoc_pmacro(struct mdoc *, int, char *, int);
101: static int macrowarn(struct mdoc *, int,
102: const char *, int);
1.124 kristaps 103:
1.88 kristaps 104:
1.1 kristaps 105: const struct mdoc_node *
1.71 kristaps 106: mdoc_node(const struct mdoc *m)
1.1 kristaps 107: {
108:
1.71 kristaps 109: return(MDOC_HALT & m->flags ? NULL : m->first);
1.1 kristaps 110: }
111:
112:
1.37 kristaps 113: const struct mdoc_meta *
1.71 kristaps 114: mdoc_meta(const struct mdoc *m)
1.37 kristaps 115: {
116:
1.71 kristaps 117: return(MDOC_HALT & m->flags ? NULL : &m->meta);
1.37 kristaps 118: }
119:
120:
1.85 kristaps 121: /*
122: * Frees volatile resources (parse tree, meta-data, fields).
123: */
1.73 kristaps 124: static void
125: mdoc_free1(struct mdoc *mdoc)
1.67 kristaps 126: {
127:
128: if (mdoc->first)
1.121 kristaps 129: mdoc_node_delete(mdoc, mdoc->first);
1.67 kristaps 130: if (mdoc->meta.title)
131: free(mdoc->meta.title);
132: if (mdoc->meta.os)
133: free(mdoc->meta.os);
134: if (mdoc->meta.name)
135: free(mdoc->meta.name);
136: if (mdoc->meta.arch)
137: free(mdoc->meta.arch);
138: if (mdoc->meta.vol)
139: free(mdoc->meta.vol);
1.133 kristaps 140: if (mdoc->meta.msec)
141: free(mdoc->meta.msec);
1.73 kristaps 142: }
143:
144:
1.85 kristaps 145: /*
146: * Allocate all volatile resources (parse tree, meta-data, fields).
147: */
1.113 kristaps 148: static void
1.73 kristaps 149: mdoc_alloc1(struct mdoc *mdoc)
150: {
1.67 kristaps 151:
1.112 kristaps 152: memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67 kristaps 153: mdoc->flags = 0;
1.85 kristaps 154: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113 kristaps 155: mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70 kristaps 156: mdoc->first = mdoc->last;
1.67 kristaps 157: mdoc->last->type = MDOC_ROOT;
158: mdoc->next = MDOC_NEXT_CHILD;
1.73 kristaps 159: }
160:
161:
162: /*
1.85 kristaps 163: * Free up volatile resources (see mdoc_free1()) then re-initialises the
164: * data with mdoc_alloc1(). After invocation, parse data has been reset
165: * and the parser is ready for re-invocation on a new tree; however,
166: * cross-parse non-volatile data is kept intact.
1.73 kristaps 167: */
1.113 kristaps 168: void
1.73 kristaps 169: mdoc_reset(struct mdoc *mdoc)
170: {
171:
172: mdoc_free1(mdoc);
1.113 kristaps 173: mdoc_alloc1(mdoc);
1.67 kristaps 174: }
175:
176:
1.68 kristaps 177: /*
1.85 kristaps 178: * Completely free up all volatile and non-volatile parse resources.
179: * After invocation, the pointer is no longer usable.
1.68 kristaps 180: */
1.67 kristaps 181: void
1.38 kristaps 182: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 183: {
184:
1.73 kristaps 185: mdoc_free1(mdoc);
1.1 kristaps 186: free(mdoc);
187: }
188:
189:
1.85 kristaps 190: /*
191: * Allocate volatile and non-volatile parse resources.
192: */
1.1 kristaps 193: struct mdoc *
1.136 kristaps 194: mdoc_alloc(void *data, int pflags, mandocmsg msg)
1.1 kristaps 195: {
196: struct mdoc *p;
197:
1.113 kristaps 198: p = mandoc_calloc(1, sizeof(struct mdoc));
199:
1.136 kristaps 200: p->msg = msg;
1.1 kristaps 201: p->data = data;
1.73 kristaps 202: p->pflags = pflags;
203:
1.113 kristaps 204: mdoc_hash_init();
205: mdoc_alloc1(p);
206: return(p);
1.1 kristaps 207: }
208:
209:
1.68 kristaps 210: /*
211: * Climb back up the parse tree, validating open scopes. Mostly calls
1.85 kristaps 212: * through to macro_end() in macro.c.
1.68 kristaps 213: */
1.1 kristaps 214: int
1.72 kristaps 215: mdoc_endparse(struct mdoc *m)
1.20 kristaps 216: {
217:
1.72 kristaps 218: if (MDOC_HALT & m->flags)
1.20 kristaps 219: return(0);
1.72 kristaps 220: else if (mdoc_macroend(m))
1.20 kristaps 221: return(1);
1.72 kristaps 222: m->flags |= MDOC_HALT;
223: return(0);
1.20 kristaps 224: }
225:
226:
1.50 kristaps 227: /*
1.53 kristaps 228: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 229: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 230: */
1.20 kristaps 231: int
1.135 kristaps 232: mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 233: {
234:
1.53 kristaps 235: if (MDOC_HALT & m->flags)
1.20 kristaps 236: return(0);
1.50 kristaps 237:
1.130 kristaps 238: m->flags |= MDOC_NEWLINE;
1.135 kristaps 239: return(('.' == buf[offs] || '\'' == buf[offs]) ?
240: mdoc_pmacro(m, ln, buf, offs) :
241: mdoc_ptext(m, ln, buf, offs));
1.1 kristaps 242: }
243:
244:
245: int
1.136 kristaps 246: mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
247: int ln, int pos, const char *fmt, ...)
1.1 kristaps 248: {
1.31 kristaps 249: char buf[256];
250: va_list ap;
1.1 kristaps 251:
1.31 kristaps 252: va_start(ap, fmt);
1.136 kristaps 253: vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.31 kristaps 254: va_end(ap);
1.88 kristaps 255:
1.136 kristaps 256: return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
1.88 kristaps 257: }
258:
259:
260: int
1.117 kristaps 261: mdoc_macro(struct mdoc *m, enum mdoct tok,
1.88 kristaps 262: int ln, int pp, int *pos, char *buf)
263: {
1.122 kristaps 264: assert(tok < MDOC_MAX);
265:
266: /* If we're in the body, deny prologue calls. */
1.117 kristaps 267:
1.88 kristaps 268: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122 kristaps 269: MDOC_PBODY & m->flags)
1.136 kristaps 270: return(mdoc_pmsg(m, ln, pp, MANDOCERR_BADBODY));
1.122 kristaps 271:
272: /* If we're in the prologue, deny "body" macros. */
273:
274: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
275: ! (MDOC_PBODY & m->flags)) {
1.136 kristaps 276: if ( ! mdoc_pmsg(m, ln, pp, MANDOCERR_BADPROLOG))
1.120 kristaps 277: return(0);
278: if (NULL == m->meta.title)
1.140 ! kristaps 279: m->meta.title = mandoc_strdup("UNKNOWN");
1.120 kristaps 280: if (NULL == m->meta.vol)
1.140 ! kristaps 281: m->meta.vol = mandoc_strdup("LOCAL");
1.120 kristaps 282: if (NULL == m->meta.os)
1.140 ! kristaps 283: m->meta.os = mandoc_strdup("LOCAL");
1.120 kristaps 284: if (0 == m->meta.date)
285: m->meta.date = time(NULL);
286: m->flags |= MDOC_PBODY;
287: }
1.88 kristaps 288:
289: return((*mdoc_macros[tok].fp)(m, tok, ln, pp, pos, buf));
1.73 kristaps 290: }
291:
292:
293: static int
294: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 295: {
296:
1.25 kristaps 297: assert(mdoc->last);
298: assert(mdoc->first);
299: assert(MDOC_ROOT != p->type);
1.1 kristaps 300:
1.13 kristaps 301: switch (mdoc->next) {
302: case (MDOC_NEXT_SIBLING):
1.6 kristaps 303: mdoc->last->next = p;
304: p->prev = mdoc->last;
1.13 kristaps 305: p->parent = mdoc->last->parent;
1.1 kristaps 306: break;
1.13 kristaps 307: case (MDOC_NEXT_CHILD):
308: mdoc->last->child = p;
1.1 kristaps 309: p->parent = mdoc->last;
310: break;
311: default:
1.13 kristaps 312: abort();
313: /* NOTREACHED */
1.1 kristaps 314: }
315:
1.86 kristaps 316: p->parent->nchild++;
317:
1.23 kristaps 318: if ( ! mdoc_valid_pre(mdoc, p))
319: return(0);
1.68 kristaps 320: if ( ! mdoc_action_pre(mdoc, p))
321: return(0);
1.27 kristaps 322:
323: switch (p->type) {
324: case (MDOC_HEAD):
325: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 326: p->parent->head = p;
1.27 kristaps 327: break;
328: case (MDOC_TAIL):
329: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 330: p->parent->tail = p;
1.27 kristaps 331: break;
332: case (MDOC_BODY):
333: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 334: p->parent->body = p;
1.27 kristaps 335: break;
336: default:
337: break;
338: }
339:
1.1 kristaps 340: mdoc->last = p;
1.71 kristaps 341:
342: switch (p->type) {
343: case (MDOC_TEXT):
344: if ( ! mdoc_valid_post(mdoc))
345: return(0);
346: if ( ! mdoc_action_post(mdoc))
347: return(0);
348: break;
349: default:
350: break;
351: }
352:
1.23 kristaps 353: return(1);
1.1 kristaps 354: }
355:
356:
1.45 kristaps 357: static struct mdoc_node *
1.117 kristaps 358: node_alloc(struct mdoc *m, int line, int pos,
359: enum mdoct tok, enum mdoc_type type)
1.45 kristaps 360: {
1.46 kristaps 361: struct mdoc_node *p;
362:
1.113 kristaps 363: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91 kristaps 364: p->sec = m->lastsec;
1.73 kristaps 365: p->line = line;
366: p->pos = pos;
367: p->tok = tok;
1.118 kristaps 368: p->type = type;
1.130 kristaps 369: if (MDOC_NEWLINE & m->flags)
370: p->flags |= MDOC_LINE;
371: m->flags &= ~MDOC_NEWLINE;
1.46 kristaps 372: return(p);
1.45 kristaps 373: }
374:
375:
1.23 kristaps 376: int
1.117 kristaps 377: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17 kristaps 378: {
379: struct mdoc_node *p;
380:
1.91 kristaps 381: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102 kristaps 382: if ( ! node_append(m, p))
383: return(0);
384: m->next = MDOC_NEXT_CHILD;
385: return(1);
1.17 kristaps 386: }
387:
388:
1.23 kristaps 389: int
1.117 kristaps 390: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 391: {
392: struct mdoc_node *p;
393:
1.91 kristaps 394: assert(m->first);
395: assert(m->last);
1.1 kristaps 396:
1.91 kristaps 397: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102 kristaps 398: if ( ! node_append(m, p))
399: return(0);
400: m->next = MDOC_NEXT_CHILD;
401: return(1);
1.1 kristaps 402: }
403:
404:
1.23 kristaps 405: int
1.117 kristaps 406: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 407: {
408: struct mdoc_node *p;
409:
1.91 kristaps 410: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102 kristaps 411: if ( ! node_append(m, p))
412: return(0);
413: m->next = MDOC_NEXT_CHILD;
414: return(1);
1.1 kristaps 415: }
416:
417:
1.23 kristaps 418: int
1.91 kristaps 419: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 420: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 421: {
422: struct mdoc_node *p;
423:
1.91 kristaps 424: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77 kristaps 425: p->args = args;
426: if (p->args)
1.53 kristaps 427: (args->refcnt)++;
1.102 kristaps 428: if ( ! node_append(m, p))
429: return(0);
430: m->next = MDOC_NEXT_CHILD;
431: return(1);
1.1 kristaps 432: }
433:
434:
1.23 kristaps 435: int
1.91 kristaps 436: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 437: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 438: {
439: struct mdoc_node *p;
440:
1.91 kristaps 441: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77 kristaps 442: p->args = args;
443: if (p->args)
1.53 kristaps 444: (args->refcnt)++;
1.102 kristaps 445: if ( ! node_append(m, p))
446: return(0);
447: m->next = MDOC_NEXT_CHILD;
448: return(1);
1.1 kristaps 449: }
450:
451:
1.124 kristaps 452: int
453: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1 kristaps 454: {
1.91 kristaps 455: struct mdoc_node *n;
1.124 kristaps 456: size_t sv, len;
457:
458: len = strlen(p);
1.1 kristaps 459:
1.125 kristaps 460: n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113 kristaps 461: n->string = mandoc_malloc(len + 1);
1.91 kristaps 462: sv = strlcpy(n->string, p, len + 1);
463:
464: /* Prohibit truncation. */
465: assert(sv < len + 1);
466:
1.101 kristaps 467: if ( ! node_append(m, n))
468: return(0);
1.124 kristaps 469:
1.101 kristaps 470: m->next = MDOC_NEXT_SIBLING;
471: return(1);
1.91 kristaps 472: }
473:
474:
1.53 kristaps 475: void
476: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 477: {
478:
1.53 kristaps 479: if (p->string)
480: free(p->string);
481: if (p->args)
482: mdoc_argv_free(p->args);
1.1 kristaps 483: free(p);
484: }
485:
486:
1.121 kristaps 487: static void
488: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
489: {
490:
491: /* Adjust siblings. */
492:
493: if (n->prev)
494: n->prev->next = n->next;
495: if (n->next)
496: n->next->prev = n->prev;
497:
498: /* Adjust parent. */
499:
500: if (n->parent) {
501: n->parent->nchild--;
502: if (n->parent->child == n)
503: n->parent->child = n->prev ? n->prev : n->next;
504: }
505:
506: /* Adjust parse point, if applicable. */
507:
508: if (m && m->last == n) {
509: if (n->prev) {
510: m->last = n->prev;
511: m->next = MDOC_NEXT_SIBLING;
512: } else {
513: m->last = n->parent;
514: m->next = MDOC_NEXT_CHILD;
515: }
516: }
517:
518: if (m && m->first == n)
519: m->first = NULL;
520: }
521:
522:
1.53 kristaps 523: void
1.121 kristaps 524: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 525: {
526:
1.121 kristaps 527: while (p->child) {
528: assert(p->nchild);
529: mdoc_node_delete(m, p->child);
530: }
531: assert(0 == p->nchild);
1.1 kristaps 532:
1.121 kristaps 533: mdoc_node_unlink(m, p);
1.53 kristaps 534: mdoc_node_free(p);
1.1 kristaps 535: }
536:
537:
1.53 kristaps 538: /*
539: * Parse free-form text, that is, a line that does not begin with the
540: * control character.
541: */
542: static int
1.135 kristaps 543: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1 kristaps 544: {
1.137 schwarze 545: char *c, *ws, *end;
1.1 kristaps 546:
1.123 kristaps 547: /* Ignore bogus comments. */
548:
1.135 kristaps 549: if ('\\' == buf[offs] &&
550: '.' == buf[offs + 1] &&
551: '"' == buf[offs + 2])
1.136 kristaps 552: return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.123 kristaps 553:
1.124 kristaps 554: /* No text before an initial macro. */
555:
1.85 kristaps 556: if (SEC_NONE == m->lastnamed)
1.136 kristaps 557: return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.124 kristaps 558:
1.137 schwarze 559: /*
560: * Search for the beginning of unescaped trailing whitespace (ws)
561: * and for the first character not to be output (end).
562: */
1.139 kristaps 563:
564: /* FIXME: replace with strcspn(). */
1.137 schwarze 565: ws = NULL;
566: for (c = end = buf + offs; *c; c++) {
567: switch (*c) {
1.138 kristaps 568: case '-':
569: if (mandoc_hyph(buf + offs, c))
570: *c = ASCII_HYPH;
571: break;
1.137 schwarze 572: case ' ':
573: if (NULL == ws)
574: ws = c;
575: continue;
576: case '\t':
577: /*
578: * Always warn about trailing tabs,
579: * even outside literal context,
580: * where they should be put on the next line.
581: */
582: if (NULL == ws)
583: ws = c;
584: /*
585: * Strip trailing tabs in literal context only;
586: * outside, they affect the next line.
587: */
588: if (MDOC_LITERAL & m->flags)
589: continue;
590: break;
591: case '\\':
592: /* Skip the escaped character, too, if any. */
593: if (c[1])
594: c++;
595: /* FALLTHROUGH */
596: default:
597: ws = NULL;
598: break;
599: }
600: end = c + 1;
601: }
602: *end = '\0';
1.91 kristaps 603:
1.137 schwarze 604: if (ws)
605: if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
606: return(0);
1.115 kristaps 607:
1.137 schwarze 608: if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
609: if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.119 kristaps 610: return(0);
1.124 kristaps 611:
1.119 kristaps 612: /*
1.124 kristaps 613: * Insert a `Pp' in the case of a blank line. Technically,
614: * blank lines aren't allowed, but enough manuals assume this
615: * behaviour that we want to work around it.
1.119 kristaps 616: */
1.135 kristaps 617: if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
1.119 kristaps 618: return(0);
1.124 kristaps 619:
620: m->next = MDOC_NEXT_SIBLING;
621: return(1);
1.119 kristaps 622: }
1.68 kristaps 623:
1.137 schwarze 624: if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
625: return(0);
1.91 kristaps 626:
1.137 schwarze 627: if (MDOC_LITERAL & m->flags)
628: return(1);
1.128 kristaps 629:
630: /*
631: * End-of-sentence check. If the last character is an unescaped
632: * EOS character, then flag the node as being the end of a
633: * sentence. The front-end will know how to interpret this.
634: */
1.132 kristaps 635:
1.137 schwarze 636: assert(buf < end);
637:
638: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.128 kristaps 639: m->last->flags |= MDOC_EOS;
640:
641: return(1);
1.1 kristaps 642: }
643:
644:
1.58 kristaps 645: static int
1.135 kristaps 646: macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
1.58 kristaps 647: {
1.136 kristaps 648: int rc;
649:
650: rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
651: "unknown macro: %s%s",
652: buf, strlen(buf) > 3 ? "..." : "");
653:
654: /* FIXME: logic should be in driver. */
655: return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
1.58 kristaps 656: }
657:
658:
1.53 kristaps 659: /*
660: * Parse a macro line, that is, a line beginning with the control
661: * character.
662: */
663: int
1.135 kristaps 664: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 665: {
1.125 kristaps 666: enum mdoct tok;
1.130 kristaps 667: int i, j, sv;
1.125 kristaps 668: char mac[5];
1.1 kristaps 669:
1.81 kristaps 670: /* Empty lines are ignored. */
1.63 kristaps 671:
1.135 kristaps 672: offs++;
673:
674: if ('\0' == buf[offs])
1.63 kristaps 675: return(1);
676:
1.135 kristaps 677: i = offs;
1.100 kristaps 678:
679: /* Accept whitespace after the initial control char. */
680:
681: if (' ' == buf[i]) {
682: i++;
1.65 kristaps 683: while (buf[i] && ' ' == buf[i])
1.63 kristaps 684: i++;
1.115 kristaps 685: if ('\0' == buf[i])
1.63 kristaps 686: return(1);
687: }
1.1 kristaps 688:
1.130 kristaps 689: sv = i;
690:
1.53 kristaps 691: /* Copy the first word into a nil-terminated buffer. */
1.1 kristaps 692:
1.100 kristaps 693: for (j = 0; j < 4; j++, i++) {
1.115 kristaps 694: if ('\0' == (mac[j] = buf[i]))
1.53 kristaps 695: break;
1.65 kristaps 696: else if (' ' == buf[i])
1.53 kristaps 697: break;
1.104 kristaps 698:
699: /* Check for invalid characters. */
700:
701: if (isgraph((u_char)buf[i]))
702: continue;
1.136 kristaps 703: if ( ! mdoc_pmsg(m, ln, i, MANDOCERR_BADCHAR))
704: return(0);
705: i--;
1.53 kristaps 706: }
1.1 kristaps 707:
1.135 kristaps 708: mac[j] = '\0';
1.1 kristaps 709:
1.100 kristaps 710: if (j == 4 || j < 2) {
1.135 kristaps 711: if ( ! macrowarn(m, ln, mac, sv))
1.58 kristaps 712: goto err;
713: return(1);
1.53 kristaps 714: }
715:
1.125 kristaps 716: if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.135 kristaps 717: if ( ! macrowarn(m, ln, mac, sv))
1.58 kristaps 718: goto err;
719: return(1);
1.53 kristaps 720: }
1.1 kristaps 721:
1.53 kristaps 722: /* The macro is sane. Jump to the next word. */
1.1 kristaps 723:
1.65 kristaps 724: while (buf[i] && ' ' == buf[i])
1.53 kristaps 725: i++;
1.1 kristaps 726:
1.125 kristaps 727: /*
728: * Trailing whitespace. Note that tabs are allowed to be passed
729: * into the parser as "text", so we only warn about spaces here.
730: */
1.115 kristaps 731:
732: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.136 kristaps 733: if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.115 kristaps 734: goto err;
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.130 kristaps 740: if ( ! mdoc_macro(m, tok, ln, sv, &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