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