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