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