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