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