Annotation of mandoc/mdoc.c, Revision 1.230
1.230 ! schwarze 1: /* $Id: mdoc.c,v 1.229 2014/11/19 03:08:17 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:
193: int
1.203 schwarze 194: mdoc_endparse(struct mdoc *mdoc)
1.20 kristaps 195: {
196:
1.221 schwarze 197: return(mdoc_macroend(mdoc));
1.181 kristaps 198: }
199:
200: int
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.181 kristaps 211: return(1);
1.20 kristaps 212: }
213:
1.175 kristaps 214: int
1.203 schwarze 215: mdoc_addspan(struct mdoc *mdoc, const struct tbl_span *sp)
1.175 kristaps 216: {
1.180 kristaps 217: struct mdoc_node *n;
1.175 kristaps 218:
1.203 schwarze 219: n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, MDOC_TBL);
1.180 kristaps 220: n->span = sp;
1.230 ! schwarze 221: node_append(mdoc, n);
1.203 schwarze 222: mdoc->next = MDOC_NEXT_SIBLING;
1.180 kristaps 223: return(1);
1.175 kristaps 224: }
225:
1.50 kristaps 226: /*
1.53 kristaps 227: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 228: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 229: */
1.20 kristaps 230: int
1.203 schwarze 231: mdoc_parseln(struct mdoc *mdoc, int ln, char *buf, int offs)
1.1 kristaps 232: {
233:
1.228 schwarze 234: if (mdoc->last->type != MDOC_EQN || ln > mdoc->last->line)
235: mdoc->flags |= MDOC_NEWLINE;
1.153 schwarze 236:
237: /*
238: * Let the roff nS register switch SYNOPSIS mode early,
239: * such that the parser knows at all times
240: * whether this mode is on or off.
241: * Note that this mode is also switched by the Sh macro.
242: */
1.204 schwarze 243: if (roff_getreg(mdoc->roff, "nS"))
244: mdoc->flags |= MDOC_SYNOPSIS;
245: else
246: mdoc->flags &= ~MDOC_SYNOPSIS;
1.153 schwarze 247:
1.203 schwarze 248: return(roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213 schwarze 249: mdoc_pmacro(mdoc, ln, buf, offs) :
250: mdoc_ptext(mdoc, ln, buf, offs));
1.1 kristaps 251: }
252:
1.88 kristaps 253: int
1.148 kristaps 254: mdoc_macro(MACRO_PROT_ARGS)
1.88 kristaps 255: {
1.122 kristaps 256: assert(tok < MDOC_MAX);
257:
1.223 schwarze 258: if (mdoc->flags & MDOC_PBODY) {
259: if (tok == MDOC_Dt) {
260: mandoc_vmsg(MANDOCERR_DT_LATE,
261: mdoc->parse, line, ppos,
262: "Dt %s", buf + *pos);
263: return(1);
264: }
265: } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
266: if (mdoc->meta.title == NULL) {
267: mandoc_vmsg(MANDOCERR_DT_NOTITLE,
268: mdoc->parse, line, ppos, "%s %s",
269: mdoc_macronames[tok], buf + *pos);
270: mdoc->meta.title = mandoc_strdup("UNTITLED");
271: }
1.203 schwarze 272: if (NULL == mdoc->meta.vol)
273: mdoc->meta.vol = mandoc_strdup("LOCAL");
274: mdoc->flags |= MDOC_PBODY;
1.120 kristaps 275: }
1.88 kristaps 276:
1.203 schwarze 277: return((*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf));
1.73 kristaps 278: }
279:
280:
1.230 ! schwarze 281: static void
1.73 kristaps 282: node_append(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 283: {
284:
1.25 kristaps 285: assert(mdoc->last);
286: assert(mdoc->first);
287: assert(MDOC_ROOT != p->type);
1.1 kristaps 288:
1.13 kristaps 289: switch (mdoc->next) {
1.213 schwarze 290: case MDOC_NEXT_SIBLING:
1.6 kristaps 291: mdoc->last->next = p;
292: p->prev = mdoc->last;
1.13 kristaps 293: p->parent = mdoc->last->parent;
1.1 kristaps 294: break;
1.213 schwarze 295: case MDOC_NEXT_CHILD:
1.13 kristaps 296: mdoc->last->child = p;
1.1 kristaps 297: p->parent = mdoc->last;
298: break;
299: default:
1.13 kristaps 300: abort();
301: /* NOTREACHED */
1.1 kristaps 302: }
303:
1.86 kristaps 304: p->parent->nchild++;
305:
1.172 kristaps 306: /*
307: * Copy over the normalised-data pointer of our parent. Not
308: * everybody has one, but copying a null pointer is fine.
309: */
310:
311: switch (p->type) {
1.213 schwarze 312: case MDOC_BODY:
1.202 schwarze 313: if (ENDBODY_NOT != p->end)
314: break;
1.172 kristaps 315: /* FALLTHROUGH */
1.213 schwarze 316: case MDOC_TAIL:
1.172 kristaps 317: /* FALLTHROUGH */
1.213 schwarze 318: case MDOC_HEAD:
1.172 kristaps 319: p->norm = p->parent->norm;
320: break;
321: default:
322: break;
323: }
324:
1.230 ! schwarze 325: mdoc_valid_pre(mdoc, p);
1.27 kristaps 326:
327: switch (p->type) {
1.213 schwarze 328: case MDOC_HEAD:
1.27 kristaps 329: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 330: p->parent->head = p;
1.27 kristaps 331: break;
1.213 schwarze 332: case MDOC_TAIL:
1.27 kristaps 333: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 334: p->parent->tail = p;
1.27 kristaps 335: break;
1.213 schwarze 336: case MDOC_BODY:
1.152 schwarze 337: if (p->end)
338: break;
1.27 kristaps 339: assert(MDOC_BLOCK == p->parent->type);
1.53 kristaps 340: p->parent->body = p;
1.27 kristaps 341: break;
342: default:
343: break;
344: }
345:
1.1 kristaps 346: mdoc->last = p;
1.71 kristaps 347:
348: switch (p->type) {
1.213 schwarze 349: case MDOC_TBL:
1.176 kristaps 350: /* FALLTHROUGH */
1.213 schwarze 351: case MDOC_TEXT:
1.230 ! schwarze 352: mdoc_valid_post(mdoc);
1.71 kristaps 353: break;
354: default:
355: break;
356: }
1.1 kristaps 357: }
358:
1.45 kristaps 359: static struct mdoc_node *
1.213 schwarze 360: node_alloc(struct mdoc *mdoc, int line, int pos,
1.117 kristaps 361: enum mdoct tok, enum mdoc_type type)
1.45 kristaps 362: {
1.46 kristaps 363: struct mdoc_node *p;
364:
1.113 kristaps 365: p = mandoc_calloc(1, sizeof(struct mdoc_node));
1.203 schwarze 366: p->sec = mdoc->lastsec;
1.73 kristaps 367: p->line = line;
368: p->pos = pos;
1.206 schwarze 369: p->lastline = line;
1.73 kristaps 370: p->tok = tok;
1.118 kristaps 371: p->type = type;
1.150 kristaps 372:
373: /* Flag analysis. */
374:
1.203 schwarze 375: if (MDOC_SYNOPSIS & mdoc->flags)
1.153 schwarze 376: p->flags |= MDOC_SYNPRETTY;
377: else
378: p->flags &= ~MDOC_SYNPRETTY;
1.203 schwarze 379: if (MDOC_NEWLINE & mdoc->flags)
1.130 kristaps 380: p->flags |= MDOC_LINE;
1.203 schwarze 381: mdoc->flags &= ~MDOC_NEWLINE;
1.150 kristaps 382:
1.46 kristaps 383: return(p);
1.45 kristaps 384: }
385:
1.23 kristaps 386: int
1.203 schwarze 387: mdoc_tail_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
1.17 kristaps 388: {
389: struct mdoc_node *p;
390:
1.203 schwarze 391: p = node_alloc(mdoc, line, pos, tok, MDOC_TAIL);
1.230 ! schwarze 392: node_append(mdoc, p);
1.203 schwarze 393: mdoc->next = MDOC_NEXT_CHILD;
1.102 kristaps 394: return(1);
1.17 kristaps 395: }
396:
1.23 kristaps 397: int
1.203 schwarze 398: mdoc_head_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
1.1 kristaps 399: {
400: struct mdoc_node *p;
401:
1.203 schwarze 402: assert(mdoc->first);
403: assert(mdoc->last);
1.1 kristaps 404:
1.203 schwarze 405: p = node_alloc(mdoc, line, pos, tok, MDOC_HEAD);
1.230 ! schwarze 406: node_append(mdoc, p);
1.203 schwarze 407: mdoc->next = MDOC_NEXT_CHILD;
1.102 kristaps 408: return(1);
1.1 kristaps 409: }
410:
1.23 kristaps 411: int
1.203 schwarze 412: mdoc_body_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok)
1.1 kristaps 413: {
414: struct mdoc_node *p;
415:
1.203 schwarze 416: p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
1.230 ! schwarze 417: node_append(mdoc, p);
1.203 schwarze 418: mdoc->next = MDOC_NEXT_CHILD;
1.152 schwarze 419: return(1);
420: }
421:
422: int
1.203 schwarze 423: mdoc_endbody_alloc(struct mdoc *mdoc, int line, int pos, enum mdoct tok,
1.152 schwarze 424: struct mdoc_node *body, enum mdoc_endbody end)
425: {
426: struct mdoc_node *p;
427:
1.203 schwarze 428: p = node_alloc(mdoc, line, pos, tok, MDOC_BODY);
1.152 schwarze 429: p->pending = body;
1.202 schwarze 430: p->norm = body->norm;
1.152 schwarze 431: p->end = end;
1.230 ! schwarze 432: node_append(mdoc, p);
1.203 schwarze 433: mdoc->next = MDOC_NEXT_SIBLING;
1.102 kristaps 434: return(1);
1.1 kristaps 435: }
436:
1.23 kristaps 437: int
1.213 schwarze 438: mdoc_block_alloc(struct mdoc *mdoc, int line, int pos,
1.117 kristaps 439: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 440: {
441: struct mdoc_node *p;
442:
1.203 schwarze 443: p = node_alloc(mdoc, line, pos, tok, MDOC_BLOCK);
1.77 kristaps 444: p->args = args;
445: if (p->args)
1.53 kristaps 446: (args->refcnt)++;
1.172 kristaps 447:
448: switch (tok) {
1.213 schwarze 449: case MDOC_Bd:
1.172 kristaps 450: /* FALLTHROUGH */
1.213 schwarze 451: case MDOC_Bf:
1.172 kristaps 452: /* FALLTHROUGH */
1.213 schwarze 453: case MDOC_Bl:
1.217 schwarze 454: /* FALLTHROUGH */
455: case MDOC_En:
1.173 kristaps 456: /* FALLTHROUGH */
1.213 schwarze 457: case MDOC_Rs:
1.172 kristaps 458: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
459: break;
460: default:
461: break;
462: }
1.230 ! schwarze 463: node_append(mdoc, p);
1.203 schwarze 464: mdoc->next = MDOC_NEXT_CHILD;
1.102 kristaps 465: return(1);
1.1 kristaps 466: }
467:
1.23 kristaps 468: int
1.213 schwarze 469: mdoc_elem_alloc(struct mdoc *mdoc, int line, int pos,
1.117 kristaps 470: enum mdoct tok, struct mdoc_arg *args)
1.1 kristaps 471: {
472: struct mdoc_node *p;
473:
1.203 schwarze 474: p = node_alloc(mdoc, line, pos, tok, MDOC_ELEM);
1.77 kristaps 475: p->args = args;
476: if (p->args)
1.53 kristaps 477: (args->refcnt)++;
1.172 kristaps 478:
479: switch (tok) {
1.213 schwarze 480: case MDOC_An:
1.172 kristaps 481: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
482: break;
483: default:
484: break;
485: }
1.230 ! schwarze 486: node_append(mdoc, p);
1.203 schwarze 487: mdoc->next = MDOC_NEXT_CHILD;
1.175 kristaps 488: return(1);
489: }
1.1 kristaps 490:
1.124 kristaps 491: int
1.203 schwarze 492: mdoc_word_alloc(struct mdoc *mdoc, int line, int pos, const char *p)
1.1 kristaps 493: {
1.91 kristaps 494: struct mdoc_node *n;
1.1 kristaps 495:
1.203 schwarze 496: n = node_alloc(mdoc, line, pos, MDOC_MAX, MDOC_TEXT);
497: n->string = roff_strdup(mdoc->roff, p);
1.230 ! schwarze 498: node_append(mdoc, n);
1.203 schwarze 499: mdoc->next = MDOC_NEXT_SIBLING;
1.101 kristaps 500: return(1);
1.91 kristaps 501: }
502:
1.205 schwarze 503: void
504: mdoc_word_append(struct mdoc *mdoc, const char *p)
505: {
506: struct mdoc_node *n;
507: char *addstr, *newstr;
508:
509: n = mdoc->last;
510: addstr = roff_strdup(mdoc->roff, p);
1.210 schwarze 511: mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1.205 schwarze 512: free(addstr);
513: free(n->string);
514: n->string = newstr;
515: mdoc->next = MDOC_NEXT_SIBLING;
516: }
1.91 kristaps 517:
1.155 kristaps 518: static void
1.53 kristaps 519: mdoc_node_free(struct mdoc_node *p)
1.1 kristaps 520: {
521:
1.172 kristaps 522: if (MDOC_BLOCK == p->type || MDOC_ELEM == p->type)
1.171 kristaps 523: free(p->norm);
1.53 kristaps 524: if (p->string)
525: free(p->string);
526: if (p->args)
527: mdoc_argv_free(p->args);
1.1 kristaps 528: free(p);
529: }
530:
1.121 kristaps 531: static void
1.203 schwarze 532: mdoc_node_unlink(struct mdoc *mdoc, struct mdoc_node *n)
1.121 kristaps 533: {
534:
535: /* Adjust siblings. */
536:
537: if (n->prev)
538: n->prev->next = n->next;
539: if (n->next)
540: n->next->prev = n->prev;
541:
542: /* Adjust parent. */
543:
544: if (n->parent) {
545: n->parent->nchild--;
546: if (n->parent->child == n)
547: n->parent->child = n->prev ? n->prev : n->next;
1.169 kristaps 548: if (n->parent->last == n)
549: n->parent->last = n->prev ? n->prev : NULL;
1.121 kristaps 550: }
551:
552: /* Adjust parse point, if applicable. */
553:
1.203 schwarze 554: if (mdoc && mdoc->last == n) {
1.121 kristaps 555: if (n->prev) {
1.203 schwarze 556: mdoc->last = n->prev;
557: mdoc->next = MDOC_NEXT_SIBLING;
1.121 kristaps 558: } else {
1.203 schwarze 559: mdoc->last = n->parent;
560: mdoc->next = MDOC_NEXT_CHILD;
1.121 kristaps 561: }
562: }
563:
1.203 schwarze 564: if (mdoc && mdoc->first == n)
565: mdoc->first = NULL;
1.121 kristaps 566: }
567:
1.53 kristaps 568: void
1.203 schwarze 569: mdoc_node_delete(struct mdoc *mdoc, struct mdoc_node *p)
1.1 kristaps 570: {
571:
1.121 kristaps 572: while (p->child) {
573: assert(p->nchild);
1.203 schwarze 574: mdoc_node_delete(mdoc, p->child);
1.121 kristaps 575: }
576: assert(0 == p->nchild);
1.1 kristaps 577:
1.203 schwarze 578: mdoc_node_unlink(mdoc, p);
1.53 kristaps 579: mdoc_node_free(p);
1.201 schwarze 580: }
581:
582: int
1.203 schwarze 583: mdoc_node_relink(struct mdoc *mdoc, struct mdoc_node *p)
1.201 schwarze 584: {
585:
1.203 schwarze 586: mdoc_node_unlink(mdoc, p);
1.230 ! schwarze 587: node_append(mdoc, p);
! 588: return(1);
1.1 kristaps 589: }
590:
1.53 kristaps 591: /*
592: * Parse free-form text, that is, a line that does not begin with the
593: * control character.
594: */
595: static int
1.203 schwarze 596: mdoc_ptext(struct mdoc *mdoc, int line, char *buf, int offs)
1.1 kristaps 597: {
1.142 kristaps 598: char *c, *ws, *end;
599: struct mdoc_node *n;
600:
1.203 schwarze 601: assert(mdoc->last);
602: n = mdoc->last;
1.142 kristaps 603:
604: /*
1.144 kristaps 605: * Divert directly to list processing if we're encountering a
1.142 kristaps 606: * columnar MDOC_BLOCK with or without a prior MDOC_BLOCK entry
1.144 kristaps 607: * (a MDOC_BODY means it's already open, in which case we should
608: * process within its context in the normal way).
1.142 kristaps 609: */
610:
1.143 kristaps 611: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.213 schwarze 612: LIST_column == n->norm->Bl.type) {
1.144 kristaps 613: /* `Bl' is open without any children. */
1.203 schwarze 614: mdoc->flags |= MDOC_FREECOL;
615: return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
1.142 kristaps 616: }
617:
618: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
1.213 schwarze 619: NULL != n->parent &&
620: MDOC_Bl == n->parent->tok &&
621: LIST_column == n->parent->norm->Bl.type) {
1.144 kristaps 622: /* `Bl' has block-level `It' children. */
1.203 schwarze 623: mdoc->flags |= MDOC_FREECOL;
624: return(mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf));
1.142 kristaps 625: }
1.124 kristaps 626:
1.137 schwarze 627: /*
628: * Search for the beginning of unescaped trailing whitespace (ws)
629: * and for the first character not to be output (end).
630: */
1.139 kristaps 631:
632: /* FIXME: replace with strcspn(). */
1.137 schwarze 633: ws = NULL;
634: for (c = end = buf + offs; *c; c++) {
635: switch (*c) {
636: case ' ':
637: if (NULL == ws)
638: ws = c;
639: continue;
640: case '\t':
641: /*
642: * Always warn about trailing tabs,
643: * even outside literal context,
644: * where they should be put on the next line.
645: */
646: if (NULL == ws)
647: ws = c;
648: /*
649: * Strip trailing tabs in literal context only;
650: * outside, they affect the next line.
651: */
1.203 schwarze 652: if (MDOC_LITERAL & mdoc->flags)
1.137 schwarze 653: continue;
654: break;
655: case '\\':
656: /* Skip the escaped character, too, if any. */
657: if (c[1])
658: c++;
659: /* FALLTHROUGH */
660: default:
661: ws = NULL;
662: break;
663: }
664: end = c + 1;
665: }
666: *end = '\0';
1.91 kristaps 667:
1.137 schwarze 668: if (ws)
1.218 schwarze 669: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
670: line, (int)(ws-buf), NULL);
1.115 kristaps 671:
1.203 schwarze 672: if ('\0' == buf[offs] && ! (MDOC_LITERAL & mdoc->flags)) {
1.218 schwarze 673: mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
674: line, (int)(c - buf), NULL);
1.124 kristaps 675:
1.119 kristaps 676: /*
1.165 schwarze 677: * Insert a `sp' in the case of a blank line. Technically,
1.124 kristaps 678: * blank lines aren't allowed, but enough manuals assume this
679: * behaviour that we want to work around it.
1.119 kristaps 680: */
1.203 schwarze 681: if ( ! mdoc_elem_alloc(mdoc, line, offs, MDOC_sp, NULL))
1.119 kristaps 682: return(0);
1.124 kristaps 683:
1.203 schwarze 684: mdoc->next = MDOC_NEXT_SIBLING;
1.199 schwarze 685:
1.230 ! schwarze 686: mdoc_valid_post(mdoc);
! 687: return(1);
1.119 kristaps 688: }
1.68 kristaps 689:
1.203 schwarze 690: if ( ! mdoc_word_alloc(mdoc, line, offs, buf+offs))
1.137 schwarze 691: return(0);
1.91 kristaps 692:
1.203 schwarze 693: if (MDOC_LITERAL & mdoc->flags)
1.137 schwarze 694: return(1);
1.128 kristaps 695:
696: /*
697: * End-of-sentence check. If the last character is an unescaped
698: * EOS character, then flag the node as being the end of a
699: * sentence. The front-end will know how to interpret this.
700: */
1.132 kristaps 701:
1.137 schwarze 702: assert(buf < end);
703:
1.207 schwarze 704: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203 schwarze 705: mdoc->last->flags |= MDOC_EOS;
1.128 kristaps 706:
707: return(1);
1.1 kristaps 708: }
709:
1.53 kristaps 710: /*
711: * Parse a macro line, that is, a line beginning with the control
712: * character.
713: */
1.155 kristaps 714: static int
1.203 schwarze 715: mdoc_pmacro(struct mdoc *mdoc, int ln, char *buf, int offs)
1.1 kristaps 716: {
1.229 schwarze 717: struct mdoc_node *n;
718: const char *cp;
1.144 kristaps 719: enum mdoct tok;
1.188 kristaps 720: int i, sv;
1.144 kristaps 721: char mac[5];
1.63 kristaps 722:
1.188 kristaps 723: sv = offs;
1.130 kristaps 724:
1.213 schwarze 725: /*
1.162 schwarze 726: * Copy the first word into a nil-terminated buffer.
1.229 schwarze 727: * Stop when a space, tab, escape, or eoln is encountered.
1.160 kristaps 728: */
1.1 kristaps 729:
1.188 kristaps 730: i = 0;
1.229 schwarze 731: while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.188 kristaps 732: mac[i++] = buf[offs++];
733:
734: mac[i] = '\0';
735:
1.214 schwarze 736: tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
1.1 kristaps 737:
1.229 schwarze 738: if (tok == MDOC_MAX) {
1.222 schwarze 739: mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
740: ln, sv, buf + sv - 1);
1.58 kristaps 741: return(1);
1.53 kristaps 742: }
1.1 kristaps 743:
1.229 schwarze 744: /* Skip a leading escape sequence or tab. */
1.160 kristaps 745:
1.229 schwarze 746: switch (buf[offs]) {
747: case '\\':
748: cp = buf + offs + 1;
749: mandoc_escape(&cp, NULL, NULL);
750: offs = cp - buf;
751: break;
752: case '\t':
1.188 kristaps 753: offs++;
1.229 schwarze 754: break;
755: default:
756: break;
757: }
1.160 kristaps 758:
759: /* Jump to the next non-whitespace word. */
1.1 kristaps 760:
1.188 kristaps 761: while (buf[offs] && ' ' == buf[offs])
762: offs++;
1.1 kristaps 763:
1.213 schwarze 764: /*
1.125 kristaps 765: * Trailing whitespace. Note that tabs are allowed to be passed
766: * into the parser as "text", so we only warn about spaces here.
767: */
1.115 kristaps 768:
1.188 kristaps 769: if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218 schwarze 770: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
771: ln, offs - 1, NULL);
1.115 kristaps 772:
1.144 kristaps 773: /*
774: * If an initial macro or a list invocation, divert directly
775: * into macro processing.
776: */
777:
1.221 schwarze 778: if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok)
779: return(mdoc_macro(mdoc, tok, ln, sv, &offs, buf));
1.144 kristaps 780:
1.203 schwarze 781: n = mdoc->last;
782: assert(mdoc->last);
1.144 kristaps 783:
784: /*
785: * If the first macro of a `Bl -column', open an `It' block
786: * context around the parsed macro.
787: */
788:
789: if (MDOC_Bl == n->tok && MDOC_BODY == n->type &&
1.213 schwarze 790: LIST_column == n->norm->Bl.type) {
1.203 schwarze 791: mdoc->flags |= MDOC_FREECOL;
1.221 schwarze 792: return(mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf));
1.144 kristaps 793: }
794:
795: /*
796: * If we're following a block-level `It' within a `Bl -column'
797: * context (perhaps opened in the above block or in ptext()),
798: * then open an `It' block context around the parsed macro.
1.98 kristaps 799: */
1.144 kristaps 800:
801: if (MDOC_It == n->tok && MDOC_BLOCK == n->type &&
1.213 schwarze 802: NULL != n->parent &&
803: MDOC_Bl == n->parent->tok &&
804: LIST_column == n->parent->norm->Bl.type) {
1.203 schwarze 805: mdoc->flags |= MDOC_FREECOL;
1.221 schwarze 806: return(mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf));
1.144 kristaps 807: }
808:
809: /* Normal processing of a macro. */
810:
1.213 schwarze 811: if ( ! mdoc_macro(mdoc, tok, ln, sv, &offs, buf))
1.221 schwarze 812: return(0);
1.208 schwarze 813:
814: /* In quick mode (for mandocdb), abort after the NAME section. */
815:
816: if (mdoc->quick && MDOC_Sh == tok &&
817: SEC_NAME != mdoc->last->sec)
818: return(2);
1.1 kristaps 819:
1.53 kristaps 820: return(1);
1.1 kristaps 821: }
1.100 kristaps 822:
1.186 kristaps 823: enum mdelim
824: mdoc_isdelim(const char *p)
825: {
826:
827: if ('\0' == p[0])
828: return(DELIM_NONE);
829:
830: if ('\0' == p[1])
831: switch (p[0]) {
1.213 schwarze 832: case '(':
1.186 kristaps 833: /* FALLTHROUGH */
1.213 schwarze 834: case '[':
1.186 kristaps 835: return(DELIM_OPEN);
1.213 schwarze 836: case '|':
1.186 kristaps 837: return(DELIM_MIDDLE);
1.213 schwarze 838: case '.':
1.186 kristaps 839: /* FALLTHROUGH */
1.213 schwarze 840: case ',':
1.186 kristaps 841: /* FALLTHROUGH */
1.213 schwarze 842: case ';':
1.186 kristaps 843: /* FALLTHROUGH */
1.213 schwarze 844: case ':':
1.186 kristaps 845: /* FALLTHROUGH */
1.213 schwarze 846: case '?':
1.186 kristaps 847: /* FALLTHROUGH */
1.213 schwarze 848: case '!':
1.186 kristaps 849: /* FALLTHROUGH */
1.213 schwarze 850: case ')':
1.186 kristaps 851: /* FALLTHROUGH */
1.213 schwarze 852: case ']':
1.186 kristaps 853: return(DELIM_CLOSE);
854: default:
855: return(DELIM_NONE);
856: }
857:
858: if ('\\' != p[0])
859: return(DELIM_NONE);
1.100 kristaps 860:
1.186 kristaps 861: if (0 == strcmp(p + 1, "."))
862: return(DELIM_CLOSE);
1.200 schwarze 863: if (0 == strcmp(p + 1, "fR|\\fP"))
1.186 kristaps 864: return(DELIM_MIDDLE);
865:
866: return(DELIM_NONE);
1.211 schwarze 867: }
868:
869: void
870: mdoc_deroff(char **dest, const struct mdoc_node *n)
871: {
872: char *cp;
873: size_t sz;
874:
875: if (MDOC_TEXT != n->type) {
876: for (n = n->child; n; n = n->next)
877: mdoc_deroff(dest, n);
878: return;
879: }
880:
881: /* Skip leading whitespace. */
882:
883: for (cp = n->string; '\0' != *cp; cp++)
884: if (0 == isspace((unsigned char)*cp))
885: break;
886:
887: /* Skip trailing whitespace. */
888:
889: for (sz = strlen(cp); sz; sz--)
890: if (0 == isspace((unsigned char)cp[sz-1]))
891: break;
892:
893: /* Skip empty strings. */
894:
895: if (0 == sz)
896: return;
897:
898: if (NULL == *dest) {
899: *dest = mandoc_strndup(cp, sz);
900: return;
901: }
902:
903: mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
904: free(*dest);
905: *dest = cp;
1.186 kristaps 906: }
CVSweb