Annotation of mandoc/mdoc.c, Revision 1.161
1.161 ! kristaps 1: /* $Id: mdoc.c,v 1.160 2010/08/07 20:33:55 kristaps Exp $ */
1.1 kristaps 2: /*
1.157 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.75 kristaps 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.75 kristaps 10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 17: */
1.114 kristaps 18: #ifdef HAVE_CONFIG_H
19: #include "config.h"
20: #endif
21:
1.106 kristaps 22: #include <sys/types.h>
23:
1.1 kristaps 24: #include <assert.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.141 kristaps 72: "%U", "Ta"
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.149 kristaps 99: static int mdoc_ptext(struct mdoc *, int, char *, int);
100: static int mdoc_pmacro(struct mdoc *, int, char *, int);
1.135 kristaps 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.150 kristaps 194: mdoc_alloc(struct regset *regs, void *data,
195: int pflags, mandocmsg msg)
1.1 kristaps 196: {
197: struct mdoc *p;
198:
1.113 kristaps 199: p = mandoc_calloc(1, sizeof(struct mdoc));
200:
1.136 kristaps 201: p->msg = msg;
1.1 kristaps 202: p->data = data;
1.73 kristaps 203: p->pflags = pflags;
1.149 kristaps 204: p->regs = regs;
1.73 kristaps 205:
1.113 kristaps 206: mdoc_hash_init();
207: mdoc_alloc1(p);
208: return(p);
1.1 kristaps 209: }
210:
211:
1.68 kristaps 212: /*
213: * Climb back up the parse tree, validating open scopes. Mostly calls
1.85 kristaps 214: * through to macro_end() in macro.c.
1.68 kristaps 215: */
1.1 kristaps 216: int
1.72 kristaps 217: mdoc_endparse(struct mdoc *m)
1.20 kristaps 218: {
219:
1.72 kristaps 220: if (MDOC_HALT & m->flags)
1.20 kristaps 221: return(0);
1.72 kristaps 222: else if (mdoc_macroend(m))
1.20 kristaps 223: return(1);
1.72 kristaps 224: m->flags |= MDOC_HALT;
225: return(0);
1.20 kristaps 226: }
227:
228:
1.50 kristaps 229: /*
1.53 kristaps 230: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 231: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 232: */
1.20 kristaps 233: int
1.149 kristaps 234: mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 235: {
236:
1.53 kristaps 237: if (MDOC_HALT & m->flags)
1.20 kristaps 238: return(0);
1.50 kristaps 239:
1.130 kristaps 240: m->flags |= MDOC_NEWLINE;
1.153 schwarze 241:
242: /*
243: * Let the roff nS register switch SYNOPSIS mode early,
244: * such that the parser knows at all times
245: * whether this mode is on or off.
246: * Note that this mode is also switched by the Sh macro.
247: */
248: if (m->regs->regs[(int)REG_nS].set) {
249: if (m->regs->regs[(int)REG_nS].v.u)
250: m->flags |= MDOC_SYNOPSIS;
251: else
252: m->flags &= ~MDOC_SYNOPSIS;
253: }
254:
1.135 kristaps 255: return(('.' == buf[offs] || '\'' == buf[offs]) ?
1.149 kristaps 256: mdoc_pmacro(m, ln, buf, offs) :
257: mdoc_ptext(m, ln, buf, offs));
1.1 kristaps 258: }
259:
260:
261: int
1.136 kristaps 262: mdoc_vmsg(struct mdoc *mdoc, enum mandocerr t,
263: int ln, int pos, const char *fmt, ...)
1.1 kristaps 264: {
1.31 kristaps 265: char buf[256];
266: va_list ap;
1.1 kristaps 267:
1.31 kristaps 268: va_start(ap, fmt);
1.136 kristaps 269: vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
1.31 kristaps 270: va_end(ap);
1.88 kristaps 271:
1.136 kristaps 272: return((*mdoc->msg)(t, mdoc->data, ln, pos, buf));
1.88 kristaps 273: }
274:
275:
276: int
1.148 kristaps 277: mdoc_macro(MACRO_PROT_ARGS)
1.88 kristaps 278: {
1.122 kristaps 279: assert(tok < MDOC_MAX);
280:
281: /* If we're in the body, deny prologue calls. */
1.117 kristaps 282:
1.88 kristaps 283: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.122 kristaps 284: MDOC_PBODY & m->flags)
1.148 kristaps 285: return(mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY));
1.122 kristaps 286:
287: /* If we're in the prologue, deny "body" macros. */
288:
289: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
290: ! (MDOC_PBODY & m->flags)) {
1.148 kristaps 291: if ( ! mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG))
1.120 kristaps 292: return(0);
293: if (NULL == m->meta.title)
1.140 kristaps 294: m->meta.title = mandoc_strdup("UNKNOWN");
1.120 kristaps 295: if (NULL == m->meta.vol)
1.140 kristaps 296: m->meta.vol = mandoc_strdup("LOCAL");
1.120 kristaps 297: if (NULL == m->meta.os)
1.140 kristaps 298: m->meta.os = mandoc_strdup("LOCAL");
1.120 kristaps 299: if (0 == m->meta.date)
300: m->meta.date = time(NULL);
301: m->flags |= MDOC_PBODY;
302: }
1.88 kristaps 303:
1.149 kristaps 304: return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
1.73 kristaps 305: }
306:
307:
308: static int
309: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 310: {
311:
1.25 kristaps 312: assert(mdoc->last);
313: assert(mdoc->first);
314: assert(MDOC_ROOT != p->type);
1.1 kristaps 315:
1.13 kristaps 316: switch (mdoc->next) {
317: case (MDOC_NEXT_SIBLING):
1.6 kristaps 318: mdoc->last->next = p;
319: p->prev = mdoc->last;
1.13 kristaps 320: p->parent = mdoc->last->parent;
1.1 kristaps 321: break;
1.13 kristaps 322: case (MDOC_NEXT_CHILD):
323: mdoc->last->child = p;
1.1 kristaps 324: p->parent = mdoc->last;
325: break;
326: default:
1.13 kristaps 327: abort();
328: /* NOTREACHED */
1.1 kristaps 329: }
330:
1.86 kristaps 331: p->parent->nchild++;
332:
1.23 kristaps 333: if ( ! mdoc_valid_pre(mdoc, p))
334: return(0);
1.68 kristaps 335: if ( ! mdoc_action_pre(mdoc, p))
336: return(0);
1.27 kristaps 337:
338: switch (p->type) {
339: case (MDOC_HEAD):
340: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 341: p->parent->head = p;
1.27 kristaps 342: break;
343: case (MDOC_TAIL):
344: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 345: p->parent->tail = p;
1.27 kristaps 346: break;
347: case (MDOC_BODY):
1.152 schwarze 348: if (p->end)
349: break;
1.27 kristaps 350: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 351: p->parent->body = p;
1.27 kristaps 352: break;
353: default:
354: break;
355: }
356:
1.1 kristaps 357: mdoc->last = p;
1.71 kristaps 358:
359: switch (p->type) {
360: case (MDOC_TEXT):
361: if ( ! mdoc_valid_post(mdoc))
362: return(0);
363: if ( ! mdoc_action_post(mdoc))
364: return(0);
365: break;
366: default:
367: break;
368: }
369:
1.23 kristaps 370: return(1);
1.1 kristaps 371: }
372:
373:
1.45 kristaps 374: static struct mdoc_node *
1.117 kristaps 375: node_alloc(struct mdoc *m, int line, int pos,
376: enum mdoct tok, enum mdoc_type type)
1.45 kristaps 377: {
1.46 kristaps 378: struct mdoc_node *p;
379:
1.113 kristaps 380: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91 kristaps 381: p->sec = m->lastsec;
1.73 kristaps 382: p->line = line;
383: p->pos = pos;
384: p->tok = tok;
1.118 kristaps 385: p->type = type;
1.150 kristaps 386:
387: /* Flag analysis. */
388:
1.153 schwarze 389: if (MDOC_SYNOPSIS & m->flags)
390: p->flags |= MDOC_SYNPRETTY;
391: else
392: p->flags &= ~MDOC_SYNPRETTY;
1.130 kristaps 393: if (MDOC_NEWLINE & m->flags)
394: p->flags |= MDOC_LINE;
395: m->flags &= ~MDOC_NEWLINE;
1.150 kristaps 396:
1.46 kristaps 397: return(p);
1.45 kristaps 398: }
399:
400:
1.23 kristaps 401: int
1.117 kristaps 402: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17 kristaps 403: {
404: struct mdoc_node *p;
405:
1.91 kristaps 406: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102 kristaps 407: if ( ! node_append(m, p))
408: return(0);
409: m->next = MDOC_NEXT_CHILD;
410: return(1);
1.17 kristaps 411: }
412:
413:
1.23 kristaps 414: int
1.117 kristaps 415: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 416: {
417: struct mdoc_node *p;
418:
1.91 kristaps 419: assert(m->first);
420: assert(m->last);
1.1 kristaps 421:
1.91 kristaps 422: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102 kristaps 423: if ( ! node_append(m, p))
424: return(0);
425: m->next = MDOC_NEXT_CHILD;
426: return(1);
1.1 kristaps 427: }
428:
429:
1.23 kristaps 430: int
1.117 kristaps 431: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 432: {
433: struct mdoc_node *p;
434:
1.91 kristaps 435: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102 kristaps 436: if ( ! node_append(m, p))
437: return(0);
438: m->next = MDOC_NEXT_CHILD;
1.152 schwarze 439: return(1);
440: }
441:
442:
443: int
444: mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
445: struct mdoc_node *body, enum mdoc_endbody end)
446: {
447: struct mdoc_node *p;
448:
449: p = node_alloc(m, line, pos, tok, MDOC_BODY);
450: p->pending = body;
451: p->end = end;
452: if ( ! node_append(m, p))
453: return(0);
454: m->next = MDOC_NEXT_SIBLING;
1.102 kristaps 455: return(1);
1.1 kristaps 456: }
457:
458:
1.23 kristaps 459: int
1.91 kristaps 460: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 461: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 462: {
463: struct mdoc_node *p;
464:
1.91 kristaps 465: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77 kristaps 466: p->args = args;
467: if (p->args)
1.53 kristaps 468: (args->refcnt)++;
1.102 kristaps 469: if ( ! node_append(m, p))
470: return(0);
471: m->next = MDOC_NEXT_CHILD;
472: return(1);
1.1 kristaps 473: }
474:
475:
1.23 kristaps 476: int
1.91 kristaps 477: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 478: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 479: {
480: struct mdoc_node *p;
481:
1.91 kristaps 482: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77 kristaps 483: p->args = args;
484: if (p->args)
1.53 kristaps 485: (args->refcnt)++;
1.102 kristaps 486: if ( ! node_append(m, p))
487: return(0);
488: m->next = MDOC_NEXT_CHILD;
489: return(1);
1.1 kristaps 490: }
491:
492:
1.124 kristaps 493: int
494: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1 kristaps 495: {
1.91 kristaps 496: struct mdoc_node *n;
1.124 kristaps 497: size_t sv, len;
498:
499: len = strlen(p);
1.1 kristaps 500:
1.125 kristaps 501: n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113 kristaps 502: n->string = mandoc_malloc(len + 1);
1.91 kristaps 503: sv = strlcpy(n->string, p, len + 1);
504:
505: /* Prohibit truncation. */
506: assert(sv < len + 1);
507:
1.101 kristaps 508: if ( ! node_append(m, n))
509: return(0);
1.124 kristaps 510:
1.101 kristaps 511: m->next = MDOC_NEXT_SIBLING;
512: return(1);
1.91 kristaps 513: }
514:
515:
1.155 kristaps 516: static void
1.53 kristaps 517: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 518: {
519:
1.156 kristaps 520: /*
521: * XXX: if these end up being problematic in terms of memory
522: * management and dereferencing freed blocks, then make them
523: * into reference-counted double-pointers.
524: */
525:
1.154 kristaps 526: if (MDOC_Bd == p->tok && MDOC_BLOCK == p->type)
527: if (p->data.Bd)
528: free(p->data.Bd);
529: if (MDOC_Bl == p->tok && MDOC_BLOCK == p->type)
530: if (p->data.Bl)
531: free(p->data.Bl);
1.156 kristaps 532: if (MDOC_Bf == p->tok && MDOC_HEAD == p->type)
533: if (p->data.Bf)
534: free(p->data.Bf);
1.154 kristaps 535:
1.53 kristaps 536: if (p->string)
537: free(p->string);
538: if (p->args)
539: mdoc_argv_free(p->args);
1.1 kristaps 540: free(p);
541: }
542:
543:
1.121 kristaps 544: static void
545: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
546: {
547:
548: /* Adjust siblings. */
549:
550: if (n->prev)
551: n->prev->next = n->next;
552: if (n->next)
553: n->next->prev = n->prev;
554:
555: /* Adjust parent. */
556:
557: if (n->parent) {
558: n->parent->nchild--;
559: if (n->parent->child == n)
560: n->parent->child = n->prev ? n->prev : n->next;
561: }
562:
563: /* Adjust parse point, if applicable. */
564:
565: if (m && m->last == n) {
566: if (n->prev) {
567: m->last = n->prev;
568: m->next = MDOC_NEXT_SIBLING;
569: } else {
570: m->last = n->parent;
571: m->next = MDOC_NEXT_CHILD;
572: }
573: }
574:
575: if (m && m->first == n)
576: m->first = NULL;
577: }
578:
579:
1.53 kristaps 580: void
1.121 kristaps 581: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 582: {
583:
1.121 kristaps 584: while (p->child) {
585: assert(p->nchild);
586: mdoc_node_delete(m, p->child);
587: }
588: assert(0 == p->nchild);
1.1 kristaps 589:
1.121 kristaps 590: mdoc_node_unlink(m, p);
1.53 kristaps 591: mdoc_node_free(p);
1.1 kristaps 592: }
593:
594:
1.53 kristaps 595: /*
596: * Parse free-form text, that is, a line that does not begin with the
597: * control character.
598: */
599: static int
1.149 kristaps 600: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1 kristaps 601: {
1.142 kristaps 602: char *c, *ws, *end;
603: struct mdoc_node *n;
1.1 kristaps 604:
1.123 kristaps 605: /* Ignore bogus comments. */
606:
1.135 kristaps 607: if ('\\' == buf[offs] &&
608: '.' == buf[offs + 1] &&
609: '"' == buf[offs + 2])
1.136 kristaps 610: return(mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT));
1.123 kristaps 611:
1.124 kristaps 612: /* No text before an initial macro. */
613:
1.85 kristaps 614: if (SEC_NONE == m->lastnamed)
1.136 kristaps 615: return(mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT));
1.142 kristaps 616:
617: assert(m->last);
618: n = m->last;
619:
620: /*
1.144 kristaps 621: * Divert directly to list processing if we're encountering a
1.142 kristaps 622: * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
1.144 kristaps 623: * (a MDOC_BODY means it's already open, in which case we should
624: * process within its context in the normal way).
1.142 kristaps 625: */
626:
1.143 kristaps 627: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.154 kristaps 628: LIST_column == n->data.Bl->type) {
1.144 kristaps 629: /* `Bl' is open without any children. */
1.142 kristaps 630: m->flags |= MDOC_FREECOL;
1.149 kristaps 631: return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
1.142 kristaps 632: }
633:
634: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
635: NULL != n->parent &&
636: MDOC_Bl == n->parent->tok &&
1.154 kristaps 637: LIST_column == n->parent->data.Bl->type) {
1.144 kristaps 638: /* `Bl' has block-level `It' children. */
1.142 kristaps 639: m->flags |= MDOC_FREECOL;
1.149 kristaps 640: return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
1.142 kristaps 641: }
1.124 kristaps 642:
1.137 schwarze 643: /*
644: * Search for the beginning of unescaped trailing whitespace (ws)
645: * and for the first character not to be output (end).
646: */
1.139 kristaps 647:
648: /* FIXME: replace with strcspn(). */
1.137 schwarze 649: ws = NULL;
650: for (c = end = buf + offs; *c; c++) {
651: switch (*c) {
1.138 kristaps 652: case '-':
653: if (mandoc_hyph(buf + offs, c))
654: *c = ASCII_HYPH;
1.145 kristaps 655: ws = NULL;
1.138 kristaps 656: break;
1.137 schwarze 657: case ' ':
658: if (NULL == ws)
659: ws = c;
660: continue;
661: case '\t':
662: /*
663: * Always warn about trailing tabs,
664: * even outside literal context,
665: * where they should be put on the next line.
666: */
667: if (NULL == ws)
668: ws = c;
669: /*
670: * Strip trailing tabs in literal context only;
671: * outside, they affect the next line.
672: */
673: if (MDOC_LITERAL & m->flags)
674: continue;
675: break;
676: case '\\':
677: /* Skip the escaped character, too, if any. */
678: if (c[1])
679: c++;
680: /* FALLTHROUGH */
681: default:
682: ws = NULL;
683: break;
684: }
685: end = c + 1;
686: }
687: *end = '\0';
1.91 kristaps 688:
1.137 schwarze 689: if (ws)
690: if ( ! mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE))
691: return(0);
1.115 kristaps 692:
1.137 schwarze 693: if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
694: if ( ! mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN))
1.119 kristaps 695: return(0);
1.124 kristaps 696:
1.119 kristaps 697: /*
1.124 kristaps 698: * Insert a `Pp' in the case of a blank line. Technically,
699: * blank lines aren't allowed, but enough manuals assume this
700: * behaviour that we want to work around it.
1.119 kristaps 701: */
1.135 kristaps 702: if ( ! mdoc_elem_alloc(m, line, offs, MDOC_Pp, NULL))
1.119 kristaps 703: return(0);
1.124 kristaps 704:
705: m->next = MDOC_NEXT_SIBLING;
706: return(1);
1.119 kristaps 707: }
1.68 kristaps 708:
1.137 schwarze 709: if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
710: return(0);
1.91 kristaps 711:
1.137 schwarze 712: if (MDOC_LITERAL & m->flags)
713: return(1);
1.128 kristaps 714:
715: /*
716: * End-of-sentence check. If the last character is an unescaped
717: * EOS character, then flag the node as being the end of a
718: * sentence. The front-end will know how to interpret this.
719: */
1.132 kristaps 720:
1.137 schwarze 721: assert(buf < end);
722:
1.159 schwarze 723: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
1.128 kristaps 724: m->last->flags |= MDOC_EOS;
725:
726: return(1);
1.1 kristaps 727: }
728:
729:
1.58 kristaps 730: static int
1.135 kristaps 731: macrowarn(struct mdoc *m, int ln, const char *buf, int offs)
1.58 kristaps 732: {
1.136 kristaps 733: int rc;
734:
735: rc = mdoc_vmsg(m, MANDOCERR_MACRO, ln, offs,
736: "unknown macro: %s%s",
737: buf, strlen(buf) > 3 ? "..." : "");
738:
739: /* FIXME: logic should be in driver. */
1.144 kristaps 740: /* FIXME: broken, will error out and not omit a message. */
1.136 kristaps 741: return(MDOC_IGN_MACRO & m->pflags ? rc : 0);
1.58 kristaps 742: }
743:
744:
1.53 kristaps 745: /*
746: * Parse a macro line, that is, a line beginning with the control
747: * character.
748: */
1.155 kristaps 749: static int
1.149 kristaps 750: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 751: {
1.144 kristaps 752: enum mdoct tok;
753: int i, j, sv;
754: char mac[5];
755: struct mdoc_node *n;
1.1 kristaps 756:
1.81 kristaps 757: /* Empty lines are ignored. */
1.63 kristaps 758:
1.135 kristaps 759: offs++;
760:
761: if ('\0' == buf[offs])
1.63 kristaps 762: return(1);
763:
1.135 kristaps 764: i = offs;
1.100 kristaps 765:
1.160 kristaps 766: /* Accept tabs/whitespace after the initial control char. */
1.100 kristaps 767:
1.160 kristaps 768: if (' ' == buf[i] || '\t' == buf[i]) {
1.100 kristaps 769: i++;
1.160 kristaps 770: while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.63 kristaps 771: i++;
1.115 kristaps 772: if ('\0' == buf[i])
1.63 kristaps 773: return(1);
774: }
1.1 kristaps 775:
1.130 kristaps 776: sv = i;
777:
1.160 kristaps 778: /*
779: * Copy the first word into a nil-terminated buffer. Stop
780: * copying when a tab, space, or eoln is encountered.
781: */
1.1 kristaps 782:
1.161 ! kristaps 783: for (j = 0; j < 4; j++, i++)
1.115 kristaps 784: if ('\0' == (mac[j] = buf[i]))
1.53 kristaps 785: break;
1.160 kristaps 786: else if (' ' == buf[i] || '\t' == buf[i])
1.53 kristaps 787: break;
1.1 kristaps 788:
1.135 kristaps 789: mac[j] = '\0';
1.1 kristaps 790:
1.100 kristaps 791: if (j == 4 || j < 2) {
1.135 kristaps 792: if ( ! macrowarn(m, ln, mac, sv))
1.58 kristaps 793: goto err;
794: return(1);
1.53 kristaps 795: }
796:
1.125 kristaps 797: if (MDOC_MAX == (tok = mdoc_hash_find(mac))) {
1.135 kristaps 798: if ( ! macrowarn(m, ln, mac, sv))
1.58 kristaps 799: goto err;
800: return(1);
1.53 kristaps 801: }
1.1 kristaps 802:
1.160 kristaps 803: /* Disregard the first trailing tab, if applicable. */
804:
805: if ('\t' == buf[i])
806: i++;
807:
808: /* Jump to the next non-whitespace word. */
1.1 kristaps 809:
1.65 kristaps 810: while (buf[i] && ' ' == buf[i])
1.53 kristaps 811: i++;
1.1 kristaps 812:
1.125 kristaps 813: /*
814: * Trailing whitespace. Note that tabs are allowed to be passed
815: * into the parser as "text", so we only warn about spaces here.
816: */
1.115 kristaps 817:
818: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.136 kristaps 819: if ( ! mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE))
1.115 kristaps 820: goto err;
821:
1.144 kristaps 822: /*
823: * If an initial macro or a list invocation, divert directly
824: * into macro processing.
825: */
826:
827: if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
1.149 kristaps 828: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.144 kristaps 829: goto err;
830: return(1);
831: }
832:
833: n = m->last;
834: assert(m->last);
835:
836: /*
837: * If the first macro of a `Bl -column', open an `It' block
838: * context around the parsed macro.
839: */
840:
841: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.154 kristaps 842: LIST_column == n->data.Bl->type) {
1.144 kristaps 843: m->flags |= MDOC_FREECOL;
1.149 kristaps 844: if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.144 kristaps 845: goto err;
846: return(1);
847: }
848:
849: /*
850: * If we're following a block-level `It' within a `Bl -column'
851: * context (perhaps opened in the above block or in ptext()),
852: * then open an `It' block context around the parsed macro.
1.98 kristaps 853: */
1.144 kristaps 854:
855: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
856: NULL != n->parent &&
857: MDOC_Bl == n->parent->tok &&
1.154 kristaps 858: LIST_column == n->parent->data.Bl->type) {
1.144 kristaps 859: m->flags |= MDOC_FREECOL;
1.149 kristaps 860: if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.144 kristaps 861: goto err;
862: return(1);
863: }
864:
865: /* Normal processing of a macro. */
866:
1.149 kristaps 867: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.53 kristaps 868: goto err;
1.1 kristaps 869:
1.53 kristaps 870: return(1);
1.1 kristaps 871:
1.53 kristaps 872: err: /* Error out. */
1.1 kristaps 873:
1.53 kristaps 874: m->flags |= MDOC_HALT;
875: return(0);
1.1 kristaps 876: }
1.100 kristaps 877:
878:
CVSweb