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