Annotation of mandoc/mdoc.c, Revision 1.185
1.185 ! kristaps 1: /* $Id: mdoc.c,v 1.184 2011/03/17 11:30:23 kristaps Exp $ */
1.1 kristaps 2: /*
1.182 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.157 schwarze 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.88 kristaps 101:
1.1 kristaps 102: const struct mdoc_node *
1.71 kristaps 103: mdoc_node(const struct mdoc *m)
1.1 kristaps 104: {
105:
1.177 kristaps 106: assert( ! (MDOC_HALT & m->flags));
107: return(m->first);
1.1 kristaps 108: }
109:
110:
1.37 kristaps 111: const struct mdoc_meta *
1.71 kristaps 112: mdoc_meta(const struct mdoc *m)
1.37 kristaps 113: {
114:
1.177 kristaps 115: assert( ! (MDOC_HALT & m->flags));
116: return(&m->meta);
1.37 kristaps 117: }
118:
119:
1.85 kristaps 120: /*
121: * Frees volatile resources (parse tree, meta-data, fields).
122: */
1.73 kristaps 123: static void
124: mdoc_free1(struct mdoc *mdoc)
1.67 kristaps 125: {
126:
127: if (mdoc->first)
1.121 kristaps 128: mdoc_node_delete(mdoc, mdoc->first);
1.67 kristaps 129: if (mdoc->meta.title)
130: free(mdoc->meta.title);
131: if (mdoc->meta.os)
132: free(mdoc->meta.os);
133: if (mdoc->meta.name)
134: free(mdoc->meta.name);
135: if (mdoc->meta.arch)
136: free(mdoc->meta.arch);
137: if (mdoc->meta.vol)
138: free(mdoc->meta.vol);
1.133 kristaps 139: if (mdoc->meta.msec)
140: free(mdoc->meta.msec);
1.183 kristaps 141: if (mdoc->meta.date)
142: free(mdoc->meta.date);
1.73 kristaps 143: }
144:
145:
1.85 kristaps 146: /*
147: * Allocate all volatile resources (parse tree, meta-data, fields).
148: */
1.113 kristaps 149: static void
1.73 kristaps 150: mdoc_alloc1(struct mdoc *mdoc)
151: {
1.67 kristaps 152:
1.112 kristaps 153: memset(&mdoc->meta, 0, sizeof(struct mdoc_meta));
1.67 kristaps 154: mdoc->flags = 0;
1.85 kristaps 155: mdoc->lastnamed = mdoc->lastsec = SEC_NONE;
1.113 kristaps 156: mdoc->last = mandoc_calloc(1, sizeof(struct mdoc_node));
1.70 kristaps 157: mdoc->first = mdoc->last;
1.67 kristaps 158: mdoc->last->type = MDOC_ROOT;
159: mdoc->next = MDOC_NEXT_CHILD;
1.73 kristaps 160: }
161:
162:
163: /*
1.85 kristaps 164: * Free up volatile resources (see mdoc_free1()) then re-initialises the
165: * data with mdoc_alloc1(). After invocation, parse data has been reset
166: * and the parser is ready for re-invocation on a new tree; however,
167: * cross-parse non-volatile data is kept intact.
1.73 kristaps 168: */
1.113 kristaps 169: void
1.73 kristaps 170: mdoc_reset(struct mdoc *mdoc)
171: {
172:
173: mdoc_free1(mdoc);
1.113 kristaps 174: mdoc_alloc1(mdoc);
1.67 kristaps 175: }
176:
177:
1.68 kristaps 178: /*
1.85 kristaps 179: * Completely free up all volatile and non-volatile parse resources.
180: * After invocation, the pointer is no longer usable.
1.68 kristaps 181: */
1.67 kristaps 182: void
1.38 kristaps 183: mdoc_free(struct mdoc *mdoc)
1.34 kristaps 184: {
185:
1.73 kristaps 186: mdoc_free1(mdoc);
1.1 kristaps 187: free(mdoc);
188: }
189:
190:
1.85 kristaps 191: /*
192: * Allocate volatile and non-volatile parse resources.
193: */
1.1 kristaps 194: struct mdoc *
1.185 ! kristaps 195: mdoc_alloc(struct regset *regs, struct mparse *parse)
1.1 kristaps 196: {
197: struct mdoc *p;
198:
1.113 kristaps 199: p = mandoc_calloc(1, sizeof(struct mdoc));
200:
1.185 ! kristaps 201: p->parse = parse;
1.149 kristaps 202: p->regs = regs;
1.73 kristaps 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.177 kristaps 218: assert( ! (MDOC_HALT & m->flags));
219: if (mdoc_macroend(m))
1.20 kristaps 220: return(1);
1.72 kristaps 221: m->flags |= MDOC_HALT;
222: return(0);
1.181 kristaps 223: }
224:
225: int
226: mdoc_addeqn(struct mdoc *m, const struct eqn *ep)
227: {
228: struct mdoc_node *n;
229:
230: assert( ! (MDOC_HALT & m->flags));
231:
232: /* No text before an initial macro. */
233:
234: if (SEC_NONE == m->lastnamed) {
235: mdoc_pmsg(m, ep->line, ep->pos, MANDOCERR_NOTEXT);
236: return(1);
237: }
238:
239: n = node_alloc(m, ep->line, ep->pos, MDOC_MAX, MDOC_EQN);
240: n->eqn = ep;
241:
242: if ( ! node_append(m, n))
243: return(0);
244:
245: m->next = MDOC_NEXT_SIBLING;
246: return(1);
1.20 kristaps 247: }
248:
1.175 kristaps 249: int
250: mdoc_addspan(struct mdoc *m, const struct tbl_span *sp)
251: {
1.180 kristaps 252: struct mdoc_node *n;
1.175 kristaps 253:
1.177 kristaps 254: assert( ! (MDOC_HALT & m->flags));
1.175 kristaps 255:
256: /* No text before an initial macro. */
257:
258: if (SEC_NONE == m->lastnamed) {
1.179 kristaps 259: mdoc_pmsg(m, sp->line, 0, MANDOCERR_NOTEXT);
1.175 kristaps 260: return(1);
261: }
262:
1.180 kristaps 263: n = node_alloc(m, sp->line, 0, MDOC_MAX, MDOC_TBL);
264: n->span = sp;
265:
266: if ( ! node_append(m, n))
267: return(0);
268:
269: m->next = MDOC_NEXT_SIBLING;
270: return(1);
1.175 kristaps 271: }
272:
1.20 kristaps 273:
1.50 kristaps 274: /*
1.53 kristaps 275: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 276: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 277: */
1.20 kristaps 278: int
1.149 kristaps 279: mdoc_parseln(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 280: {
281:
1.177 kristaps 282: assert( ! (MDOC_HALT & m->flags));
1.50 kristaps 283:
1.130 kristaps 284: m->flags |= MDOC_NEWLINE;
1.153 schwarze 285:
286: /*
287: * Let the roff nS register switch SYNOPSIS mode early,
288: * such that the parser knows at all times
289: * whether this mode is on or off.
290: * Note that this mode is also switched by the Sh macro.
291: */
292: if (m->regs->regs[(int)REG_nS].set) {
293: if (m->regs->regs[(int)REG_nS].v.u)
294: m->flags |= MDOC_SYNOPSIS;
295: else
296: m->flags &= ~MDOC_SYNOPSIS;
297: }
298:
1.135 kristaps 299: return(('.' == buf[offs] || '\'' == buf[offs]) ?
1.149 kristaps 300: mdoc_pmacro(m, ln, buf, offs) :
301: mdoc_ptext(m, ln, buf, offs));
1.1 kristaps 302: }
303:
1.88 kristaps 304: int
1.148 kristaps 305: mdoc_macro(MACRO_PROT_ARGS)
1.88 kristaps 306: {
1.122 kristaps 307: assert(tok < MDOC_MAX);
308:
309: /* If we're in the body, deny prologue calls. */
1.117 kristaps 310:
1.88 kristaps 311: if (MDOC_PROLOGUE & mdoc_macros[tok].flags &&
1.174 kristaps 312: MDOC_PBODY & m->flags) {
313: mdoc_pmsg(m, line, ppos, MANDOCERR_BADBODY);
314: return(1);
315: }
1.122 kristaps 316:
317: /* If we're in the prologue, deny "body" macros. */
318:
319: if ( ! (MDOC_PROLOGUE & mdoc_macros[tok].flags) &&
320: ! (MDOC_PBODY & m->flags)) {
1.174 kristaps 321: mdoc_pmsg(m, line, ppos, MANDOCERR_BADPROLOG);
1.167 kristaps 322: if (NULL == m->meta.msec)
323: m->meta.msec = mandoc_strdup("1");
1.120 kristaps 324: if (NULL == m->meta.title)
1.140 kristaps 325: m->meta.title = mandoc_strdup("UNKNOWN");
1.120 kristaps 326: if (NULL == m->meta.vol)
1.140 kristaps 327: m->meta.vol = mandoc_strdup("LOCAL");
1.120 kristaps 328: if (NULL == m->meta.os)
1.140 kristaps 329: m->meta.os = mandoc_strdup("LOCAL");
1.182 schwarze 330: if (NULL == m->meta.date)
1.185 ! kristaps 331: m->meta.date = mandoc_normdate
! 332: (m->parse, NULL, line, ppos);
1.120 kristaps 333: m->flags |= MDOC_PBODY;
334: }
1.88 kristaps 335:
1.149 kristaps 336: return((*mdoc_macros[tok].fp)(m, tok, line, ppos, pos, buf));
1.73 kristaps 337: }
338:
339:
340: static int
341: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 342: {
343:
1.25 kristaps 344: assert(mdoc->last);
345: assert(mdoc->first);
346: assert(MDOC_ROOT != p->type);
1.1 kristaps 347:
1.13 kristaps 348: switch (mdoc->next) {
349: case (MDOC_NEXT_SIBLING):
1.6 kristaps 350: mdoc->last->next = p;
351: p->prev = mdoc->last;
1.13 kristaps 352: p->parent = mdoc->last->parent;
1.1 kristaps 353: break;
1.13 kristaps 354: case (MDOC_NEXT_CHILD):
355: mdoc->last->child = p;
1.1 kristaps 356: p->parent = mdoc->last;
357: break;
358: default:
1.13 kristaps 359: abort();
360: /* NOTREACHED */
1.1 kristaps 361: }
362:
1.86 kristaps 363: p->parent->nchild++;
364:
1.172 kristaps 365: /*
366: * Copy over the normalised-data pointer of our parent. Not
367: * everybody has one, but copying a null pointer is fine.
368: */
369:
370: switch (p->type) {
371: case (MDOC_BODY):
372: /* FALLTHROUGH */
373: case (MDOC_TAIL):
374: /* FALLTHROUGH */
375: case (MDOC_HEAD):
376: p->norm = p->parent->norm;
377: break;
378: default:
379: break;
380: }
381:
1.23 kristaps 382: if ( ! mdoc_valid_pre(mdoc, p))
383: return(0);
1.27 kristaps 384:
385: switch (p->type) {
386: case (MDOC_HEAD):
387: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 388: p->parent->head = p;
1.27 kristaps 389: break;
390: case (MDOC_TAIL):
391: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 392: p->parent->tail = p;
1.27 kristaps 393: break;
394: case (MDOC_BODY):
1.152 schwarze 395: if (p->end)
396: break;
1.27 kristaps 397: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 398: p->parent->body = p;
1.27 kristaps 399: break;
400: default:
401: break;
402: }
403:
1.1 kristaps 404: mdoc->last = p;
1.71 kristaps 405:
406: switch (p->type) {
1.176 kristaps 407: case (MDOC_TBL):
408: /* FALLTHROUGH */
1.71 kristaps 409: case (MDOC_TEXT):
410: if ( ! mdoc_valid_post(mdoc))
411: return(0);
412: break;
413: default:
414: break;
415: }
416:
1.23 kristaps 417: return(1);
1.1 kristaps 418: }
419:
420:
1.45 kristaps 421: static struct mdoc_node *
1.117 kristaps 422: node_alloc(struct mdoc *m, int line, int pos,
423: enum mdoct tok, enum mdoc_type type)
1.45 kristaps 424: {
1.46 kristaps 425: struct mdoc_node *p;
426:
1.113 kristaps 427: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.91 kristaps 428: p->sec = m->lastsec;
1.73 kristaps 429: p->line = line;
430: p->pos = pos;
431: p->tok = tok;
1.118 kristaps 432: p->type = type;
1.150 kristaps 433:
434: /* Flag analysis. */
435:
1.153 schwarze 436: if (MDOC_SYNOPSIS & m->flags)
437: p->flags |= MDOC_SYNPRETTY;
438: else
439: p->flags &= ~MDOC_SYNPRETTY;
1.130 kristaps 440: if (MDOC_NEWLINE & m->flags)
441: p->flags |= MDOC_LINE;
442: m->flags &= ~MDOC_NEWLINE;
1.150 kristaps 443:
1.46 kristaps 444: return(p);
1.45 kristaps 445: }
446:
447:
1.23 kristaps 448: int
1.117 kristaps 449: mdoc_tail_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.17 kristaps 450: {
451: struct mdoc_node *p;
452:
1.91 kristaps 453: p = node_alloc(m, line, pos, tok, MDOC_TAIL);
1.102 kristaps 454: if ( ! node_append(m, p))
455: return(0);
456: m->next = MDOC_NEXT_CHILD;
457: return(1);
1.17 kristaps 458: }
459:
460:
1.23 kristaps 461: int
1.117 kristaps 462: mdoc_head_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 463: {
464: struct mdoc_node *p;
465:
1.91 kristaps 466: assert(m->first);
467: assert(m->last);
1.1 kristaps 468:
1.91 kristaps 469: p = node_alloc(m, line, pos, tok, MDOC_HEAD);
1.102 kristaps 470: if ( ! node_append(m, p))
471: return(0);
472: m->next = MDOC_NEXT_CHILD;
473: return(1);
1.1 kristaps 474: }
475:
476:
1.23 kristaps 477: int
1.117 kristaps 478: mdoc_body_alloc(struct mdoc *m, int line, int pos, enum mdoct tok)
1.1 kristaps 479: {
480: struct mdoc_node *p;
481:
1.91 kristaps 482: p = node_alloc(m, line, pos, tok, MDOC_BODY);
1.102 kristaps 483: if ( ! node_append(m, p))
484: return(0);
485: m->next = MDOC_NEXT_CHILD;
1.152 schwarze 486: return(1);
487: }
488:
489:
490: int
491: mdoc_endbody_alloc(struct mdoc *m, int line, int pos, enum mdoct tok,
492: struct mdoc_node *body, enum mdoc_endbody end)
493: {
494: struct mdoc_node *p;
495:
496: p = node_alloc(m, line, pos, tok, MDOC_BODY);
497: p->pending = body;
498: p->end = end;
499: if ( ! node_append(m, p))
500: return(0);
501: m->next = MDOC_NEXT_SIBLING;
1.102 kristaps 502: return(1);
1.1 kristaps 503: }
504:
505:
1.23 kristaps 506: int
1.91 kristaps 507: mdoc_block_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 508: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 509: {
510: struct mdoc_node *p;
511:
1.91 kristaps 512: p = node_alloc(m, line, pos, tok, MDOC_BLOCK);
1.77 kristaps 513: p->args = args;
514: if (p->args)
1.53 kristaps 515: (args->refcnt)++;
1.172 kristaps 516:
517: switch (tok) {
518: case (MDOC_Bd):
519: /* FALLTHROUGH */
520: case (MDOC_Bf):
521: /* FALLTHROUGH */
522: case (MDOC_Bl):
1.173 kristaps 523: /* FALLTHROUGH */
524: case (MDOC_Rs):
1.172 kristaps 525: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
526: break;
527: default:
528: break;
529: }
530:
1.102 kristaps 531: if ( ! node_append(m, p))
532: return(0);
533: m->next = MDOC_NEXT_CHILD;
534: return(1);
1.1 kristaps 535: }
536:
537:
1.23 kristaps 538: int
1.91 kristaps 539: mdoc_elem_alloc(struct mdoc *m, int line, int pos,
1.117 kristaps 540: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 541: {
542: struct mdoc_node *p;
543:
1.91 kristaps 544: p = node_alloc(m, line, pos, tok, MDOC_ELEM);
1.77 kristaps 545: p->args = args;
546: if (p->args)
1.53 kristaps 547: (args->refcnt)++;
1.172 kristaps 548:
549: switch (tok) {
550: case (MDOC_An):
551: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
552: break;
553: default:
554: break;
555: }
556:
1.102 kristaps 557: if ( ! node_append(m, p))
558: return(0);
559: m->next = MDOC_NEXT_CHILD;
1.175 kristaps 560: return(1);
561: }
1.1 kristaps 562:
1.124 kristaps 563: int
564: mdoc_word_alloc(struct mdoc *m, int line, int pos, const char *p)
1.1 kristaps 565: {
1.91 kristaps 566: struct mdoc_node *n;
1.124 kristaps 567: size_t sv, len;
568:
569: len = strlen(p);
1.1 kristaps 570:
1.125 kristaps 571: n = node_alloc(m, line, pos, MDOC_MAX, MDOC_TEXT);
1.113 kristaps 572: n->string = mandoc_malloc(len + 1);
1.91 kristaps 573: sv = strlcpy(n->string, p, len + 1);
574:
575: /* Prohibit truncation. */
576: assert(sv < len + 1);
577:
1.101 kristaps 578: if ( ! node_append(m, n))
579: return(0);
1.124 kristaps 580:
1.101 kristaps 581: m->next = MDOC_NEXT_SIBLING;
582: return(1);
1.91 kristaps 583: }
584:
585:
1.155 kristaps 586: static void
1.53 kristaps 587: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 588: {
589:
1.172 kristaps 590: if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
1.171 kristaps 591: free(p->norm);
1.53 kristaps 592: if (p->string)
593: free(p->string);
594: if (p->args)
595: mdoc_argv_free(p->args);
1.1 kristaps 596: free(p);
597: }
598:
599:
1.121 kristaps 600: static void
601: mdoc_node_unlink(struct mdoc *m, struct mdoc_node *n)
602: {
603:
604: /* Adjust siblings. */
605:
606: if (n->prev)
607: n->prev->next = n->next;
608: if (n->next)
609: n->next->prev = n->prev;
610:
611: /* Adjust parent. */
612:
613: if (n->parent) {
614: n->parent->nchild--;
615: if (n->parent->child == n)
616: n->parent->child = n->prev ? n->prev : n->next;
1.169 kristaps 617: if (n->parent->last == n)
618: n->parent->last = n->prev ? n->prev : NULL;
1.121 kristaps 619: }
620:
621: /* Adjust parse point, if applicable. */
622:
623: if (m && m->last == n) {
624: if (n->prev) {
625: m->last = n->prev;
626: m->next = MDOC_NEXT_SIBLING;
627: } else {
628: m->last = n->parent;
629: m->next = MDOC_NEXT_CHILD;
630: }
631: }
632:
633: if (m && m->first == n)
634: m->first = NULL;
635: }
636:
637:
1.53 kristaps 638: void
1.121 kristaps 639: mdoc_node_delete(struct mdoc *m, struct mdoc_node *p)
1.1 kristaps 640: {
641:
1.121 kristaps 642: while (p->child) {
643: assert(p->nchild);
644: mdoc_node_delete(m, p->child);
645: }
646: assert(0 == p->nchild);
1.1 kristaps 647:
1.121 kristaps 648: mdoc_node_unlink(m, p);
1.53 kristaps 649: mdoc_node_free(p);
1.1 kristaps 650: }
651:
652:
1.53 kristaps 653: /*
654: * Parse free-form text, that is, a line that does not begin with the
655: * control character.
656: */
657: static int
1.149 kristaps 658: mdoc_ptext(struct mdoc *m, int line, char *buf, int offs)
1.1 kristaps 659: {
1.142 kristaps 660: char *c, *ws, *end;
661: struct mdoc_node *n;
1.1 kristaps 662:
1.123 kristaps 663: /* Ignore bogus comments. */
664:
1.135 kristaps 665: if ('\\' == buf[offs] &&
666: '.' == buf[offs + 1] &&
1.174 kristaps 667: '"' == buf[offs + 2]) {
668: mdoc_pmsg(m, line, offs, MANDOCERR_BADCOMMENT);
669: return(1);
670: }
1.123 kristaps 671:
1.124 kristaps 672: /* No text before an initial macro. */
673:
1.174 kristaps 674: if (SEC_NONE == m->lastnamed) {
675: mdoc_pmsg(m, line, offs, MANDOCERR_NOTEXT);
676: return(1);
677: }
1.142 kristaps 678:
679: assert(m->last);
680: n = m->last;
681:
682: /*
1.144 kristaps 683: * Divert directly to list processing if we're encountering a
1.142 kristaps 684: * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
1.144 kristaps 685: * (a MDOC_BODY means it's already open, in which case we should
686: * process within its context in the normal way).
1.142 kristaps 687: */
688:
1.143 kristaps 689: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.172 kristaps 690: LIST_column == n->norm->Bl.type) {
1.144 kristaps 691: /* `Bl' is open without any children. */
1.142 kristaps 692: m->flags |= MDOC_FREECOL;
1.149 kristaps 693: return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
1.142 kristaps 694: }
695:
696: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
697: NULL != n->parent &&
698: MDOC_Bl == n->parent->tok &&
1.172 kristaps 699: LIST_column == n->parent->norm->Bl.type) {
1.144 kristaps 700: /* `Bl' has block-level `It' children. */
1.142 kristaps 701: m->flags |= MDOC_FREECOL;
1.149 kristaps 702: return(mdoc_macro(m, MDOC_It, line, offs, &offs, buf));
1.142 kristaps 703: }
1.124 kristaps 704:
1.137 schwarze 705: /*
706: * Search for the beginning of unescaped trailing whitespace (ws)
707: * and for the first character not to be output (end).
708: */
1.139 kristaps 709:
710: /* FIXME: replace with strcspn(). */
1.137 schwarze 711: ws = NULL;
712: for (c = end = buf + offs; *c; c++) {
713: switch (*c) {
1.138 kristaps 714: case '-':
715: if (mandoc_hyph(buf + offs, c))
716: *c = ASCII_HYPH;
1.145 kristaps 717: ws = NULL;
1.138 kristaps 718: break;
1.137 schwarze 719: case ' ':
720: if (NULL == ws)
721: ws = c;
722: continue;
723: case '\t':
724: /*
725: * Always warn about trailing tabs,
726: * even outside literal context,
727: * where they should be put on the next line.
728: */
729: if (NULL == ws)
730: ws = c;
731: /*
732: * Strip trailing tabs in literal context only;
733: * outside, they affect the next line.
734: */
735: if (MDOC_LITERAL & m->flags)
736: continue;
737: break;
738: case '\\':
739: /* Skip the escaped character, too, if any. */
740: if (c[1])
741: c++;
742: /* FALLTHROUGH */
743: default:
744: ws = NULL;
745: break;
746: }
747: end = c + 1;
748: }
749: *end = '\0';
1.91 kristaps 750:
1.137 schwarze 751: if (ws)
1.174 kristaps 752: mdoc_pmsg(m, line, (int)(ws-buf), MANDOCERR_EOLNSPACE);
1.115 kristaps 753:
1.137 schwarze 754: if ('\0' == buf[offs] && ! (MDOC_LITERAL & m->flags)) {
1.174 kristaps 755: mdoc_pmsg(m, line, (int)(c-buf), MANDOCERR_NOBLANKLN);
1.124 kristaps 756:
1.119 kristaps 757: /*
1.165 schwarze 758: * Insert a `sp' in the case of a blank line. Technically,
1.124 kristaps 759: * blank lines aren't allowed, but enough manuals assume this
760: * behaviour that we want to work around it.
1.119 kristaps 761: */
1.165 schwarze 762: if ( ! mdoc_elem_alloc(m, line, offs, MDOC_sp, NULL))
1.119 kristaps 763: return(0);
1.124 kristaps 764:
765: m->next = MDOC_NEXT_SIBLING;
766: return(1);
1.119 kristaps 767: }
1.68 kristaps 768:
1.137 schwarze 769: if ( ! mdoc_word_alloc(m, line, offs, buf+offs))
770: return(0);
1.91 kristaps 771:
1.137 schwarze 772: if (MDOC_LITERAL & m->flags)
773: return(1);
1.128 kristaps 774:
775: /*
776: * End-of-sentence check. If the last character is an unescaped
777: * EOS character, then flag the node as being the end of a
778: * sentence. The front-end will know how to interpret this.
779: */
1.132 kristaps 780:
1.137 schwarze 781: assert(buf < end);
782:
1.159 schwarze 783: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs), 0))
1.128 kristaps 784: m->last->flags |= MDOC_EOS;
785:
786: return(1);
1.1 kristaps 787: }
788:
789:
1.53 kristaps 790: /*
791: * Parse a macro line, that is, a line beginning with the control
792: * character.
793: */
1.155 kristaps 794: static int
1.149 kristaps 795: mdoc_pmacro(struct mdoc *m, int ln, char *buf, int offs)
1.1 kristaps 796: {
1.144 kristaps 797: enum mdoct tok;
798: int i, j, sv;
799: char mac[5];
800: struct mdoc_node *n;
1.1 kristaps 801:
1.81 kristaps 802: /* Empty lines are ignored. */
1.63 kristaps 803:
1.135 kristaps 804: offs++;
805:
806: if ('\0' == buf[offs])
1.63 kristaps 807: return(1);
808:
1.135 kristaps 809: i = offs;
1.100 kristaps 810:
1.160 kristaps 811: /* Accept tabs/whitespace after the initial control char. */
1.100 kristaps 812:
1.160 kristaps 813: if (' ' == buf[i] || '\t' == buf[i]) {
1.100 kristaps 814: i++;
1.160 kristaps 815: while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.63 kristaps 816: i++;
1.115 kristaps 817: if ('\0' == buf[i])
1.63 kristaps 818: return(1);
819: }
1.1 kristaps 820:
1.130 kristaps 821: sv = i;
822:
1.160 kristaps 823: /*
1.162 schwarze 824: * Copy the first word into a nil-terminated buffer.
1.165 schwarze 825: * Stop copying when a tab, space, or eoln is encountered.
1.160 kristaps 826: */
1.1 kristaps 827:
1.162 schwarze 828: j = 0;
1.165 schwarze 829: while (j < 4 && '\0' != buf[i] && ' ' != buf[i] && '\t' != buf[i])
1.162 schwarze 830: mac[j++] = buf[i++];
1.135 kristaps 831: mac[j] = '\0';
1.1 kristaps 832:
1.163 schwarze 833: tok = (j > 1 || j < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
834: if (MDOC_MAX == tok) {
1.185 ! kristaps 835: mandoc_vmsg(MANDOCERR_MACRO, m->parse,
! 836: ln, sv, "%s", buf + sv - 1);
1.58 kristaps 837: return(1);
1.53 kristaps 838: }
1.1 kristaps 839:
1.160 kristaps 840: /* Disregard the first trailing tab, if applicable. */
841:
842: if ('\t' == buf[i])
843: i++;
844:
845: /* Jump to the next non-whitespace word. */
1.1 kristaps 846:
1.65 kristaps 847: while (buf[i] && ' ' == buf[i])
1.53 kristaps 848: i++;
1.1 kristaps 849:
1.125 kristaps 850: /*
851: * Trailing whitespace. Note that tabs are allowed to be passed
852: * into the parser as "text", so we only warn about spaces here.
853: */
1.115 kristaps 854:
855: if ('\0' == buf[i] && ' ' == buf[i - 1])
1.174 kristaps 856: mdoc_pmsg(m, ln, i - 1, MANDOCERR_EOLNSPACE);
1.115 kristaps 857:
1.144 kristaps 858: /*
859: * If an initial macro or a list invocation, divert directly
860: * into macro processing.
861: */
862:
863: if (NULL == m->last || MDOC_It == tok || MDOC_El == tok) {
1.149 kristaps 864: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.144 kristaps 865: goto err;
866: return(1);
867: }
868:
869: n = m->last;
870: assert(m->last);
871:
872: /*
873: * If the first macro of a `Bl -column', open an `It' block
874: * context around the parsed macro.
875: */
876:
877: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.172 kristaps 878: LIST_column == n->norm->Bl.type) {
1.144 kristaps 879: m->flags |= MDOC_FREECOL;
1.149 kristaps 880: if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.144 kristaps 881: goto err;
882: return(1);
883: }
884:
885: /*
886: * If we're following a block-level `It' within a `Bl -column'
887: * context (perhaps opened in the above block or in ptext()),
888: * then open an `It' block context around the parsed macro.
1.98 kristaps 889: */
1.144 kristaps 890:
891: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
892: NULL != n->parent &&
893: MDOC_Bl == n->parent->tok &&
1.172 kristaps 894: LIST_column == n->parent->norm->Bl.type) {
1.144 kristaps 895: m->flags |= MDOC_FREECOL;
1.149 kristaps 896: if ( ! mdoc_macro(m, MDOC_It, ln, sv, &sv, buf))
1.144 kristaps 897: goto err;
898: return(1);
899: }
900:
901: /* Normal processing of a macro. */
902:
1.149 kristaps 903: if ( ! mdoc_macro(m, tok, ln, sv, &i, buf))
1.53 kristaps 904: goto err;
1.1 kristaps 905:
1.53 kristaps 906: return(1);
1.1 kristaps 907:
1.53 kristaps 908: err: /* Error out. */
1.1 kristaps 909:
1.53 kristaps 910: m->flags |= MDOC_HALT;
911: return(0);
1.1 kristaps 912: }
1.100 kristaps 913:
914:
CVSweb