Annotation of mandoc/mdoc.c, Revision 1.245
1.245 ! schwarze 1: /* $Id: mdoc.c,v 1.244 2015/04/18 17:01:58 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.242 schwarze 87: static void mdoc_node_unlink(struct roff_man *,
1.240 schwarze 88: struct roff_node *);
1.242 schwarze 89: static struct roff_node *node_alloc(struct roff_man *, int, int,
1.240 schwarze 90: int, enum roff_type);
1.242 schwarze 91: static void node_append(struct roff_man *, struct roff_node *);
92: static int mdoc_ptext(struct roff_man *, int, char *, int);
93: static int mdoc_pmacro(struct roff_man *, int, char *, int);
1.88 kristaps 94:
1.213 schwarze 95:
1.240 schwarze 96: const struct roff_node *
1.242 schwarze 97: mdoc_node(const struct roff_man *mdoc)
1.1 kristaps 98: {
99:
1.203 schwarze 100: return(mdoc->first);
1.1 kristaps 101: }
102:
1.241 schwarze 103: const struct roff_meta *
1.242 schwarze 104: mdoc_meta(const struct roff_man *mdoc)
1.37 kristaps 105: {
106:
1.203 schwarze 107: return(&mdoc->meta);
1.1 kristaps 108: }
109:
1.234 schwarze 110: void
1.242 schwarze 111: mdoc_endparse(struct roff_man *mdoc)
1.20 kristaps 112: {
113:
1.232 schwarze 114: mdoc_macroend(mdoc);
1.181 kristaps 115: }
116:
1.233 schwarze 117: void
1.242 schwarze 118: mdoc_addeqn(struct roff_man *mdoc, const struct eqn *ep)
1.181 kristaps 119: {
1.240 schwarze 120: struct roff_node *n;
1.181 kristaps 121:
1.239 schwarze 122: n = node_alloc(mdoc, ep->ln, ep->pos, MDOC_MAX, ROFFT_EQN);
1.181 kristaps 123: n->eqn = ep;
1.227 schwarze 124: if (ep->ln > mdoc->last->line)
125: n->flags |= MDOC_LINE;
1.230 schwarze 126: node_append(mdoc, n);
1.242 schwarze 127: mdoc->next = ROFF_NEXT_SIBLING;
1.20 kristaps 128: }
129:
1.233 schwarze 130: void
1.242 schwarze 131: mdoc_addspan(struct roff_man *mdoc, const struct tbl_span *sp)
1.175 kristaps 132: {
1.240 schwarze 133: struct roff_node *n;
1.175 kristaps 134:
1.239 schwarze 135: n = node_alloc(mdoc, sp->line, 0, MDOC_MAX, ROFFT_TBL);
1.180 kristaps 136: n->span = sp;
1.230 schwarze 137: node_append(mdoc, n);
1.242 schwarze 138: mdoc->next = ROFF_NEXT_SIBLING;
1.175 kristaps 139: }
140:
1.50 kristaps 141: /*
1.53 kristaps 142: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 143: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 144: */
1.20 kristaps 145: int
1.242 schwarze 146: mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 147: {
148:
1.239 schwarze 149: if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
1.228 schwarze 150: mdoc->flags |= MDOC_NEWLINE;
1.153 schwarze 151:
152: /*
153: * Let the roff nS register switch SYNOPSIS mode early,
154: * such that the parser knows at all times
155: * whether this mode is on or off.
156: * Note that this mode is also switched by the Sh macro.
157: */
1.204 schwarze 158: if (roff_getreg(mdoc->roff, "nS"))
159: mdoc->flags |= MDOC_SYNOPSIS;
160: else
161: mdoc->flags &= ~MDOC_SYNOPSIS;
1.153 schwarze 162:
1.203 schwarze 163: return(roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213 schwarze 164: mdoc_pmacro(mdoc, ln, buf, offs) :
165: mdoc_ptext(mdoc, ln, buf, offs));
1.1 kristaps 166: }
167:
1.232 schwarze 168: void
1.148 kristaps 169: mdoc_macro(MACRO_PROT_ARGS)
1.88 kristaps 170: {
1.122 kristaps 171: assert(tok < MDOC_MAX);
172:
1.223 schwarze 173: if (mdoc->flags & MDOC_PBODY) {
174: if (tok == MDOC_Dt) {
175: mandoc_vmsg(MANDOCERR_DT_LATE,
176: mdoc->parse, line, ppos,
177: "Dt %s", buf + *pos);
1.232 schwarze 178: return;
1.223 schwarze 179: }
180: } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
181: if (mdoc->meta.title == NULL) {
182: mandoc_vmsg(MANDOCERR_DT_NOTITLE,
183: mdoc->parse, line, ppos, "%s %s",
184: mdoc_macronames[tok], buf + *pos);
185: mdoc->meta.title = mandoc_strdup("UNTITLED");
186: }
1.203 schwarze 187: if (NULL == mdoc->meta.vol)
188: mdoc->meta.vol = mandoc_strdup("LOCAL");
189: mdoc->flags |= MDOC_PBODY;
1.120 kristaps 190: }
1.232 schwarze 191: (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
1.73 kristaps 192: }
193:
194:
1.230 schwarze 195: static void
1.242 schwarze 196: node_append(struct roff_man *mdoc, struct roff_node *p)
1.1 kristaps 197: {
198:
1.25 kristaps 199: assert(mdoc->last);
200: assert(mdoc->first);
1.239 schwarze 201: assert(p->type != ROFFT_ROOT);
1.1 kristaps 202:
1.13 kristaps 203: switch (mdoc->next) {
1.242 schwarze 204: case ROFF_NEXT_SIBLING:
1.6 kristaps 205: mdoc->last->next = p;
206: p->prev = mdoc->last;
1.13 kristaps 207: p->parent = mdoc->last->parent;
1.1 kristaps 208: break;
1.242 schwarze 209: case ROFF_NEXT_CHILD:
1.13 kristaps 210: mdoc->last->child = p;
1.1 kristaps 211: p->parent = mdoc->last;
212: break;
213: default:
1.13 kristaps 214: abort();
215: /* NOTREACHED */
1.1 kristaps 216: }
217:
1.86 kristaps 218: p->parent->nchild++;
219:
1.172 kristaps 220: /*
221: * Copy over the normalised-data pointer of our parent. Not
222: * everybody has one, but copying a null pointer is fine.
223: */
224:
225: switch (p->type) {
1.239 schwarze 226: case ROFFT_BODY:
1.202 schwarze 227: if (ENDBODY_NOT != p->end)
228: break;
1.172 kristaps 229: /* FALLTHROUGH */
1.239 schwarze 230: case ROFFT_TAIL:
1.172 kristaps 231: /* FALLTHROUGH */
1.239 schwarze 232: case ROFFT_HEAD:
1.172 kristaps 233: p->norm = p->parent->norm;
234: break;
235: default:
236: break;
237: }
238:
1.230 schwarze 239: mdoc_valid_pre(mdoc, p);
1.27 kristaps 240:
241: switch (p->type) {
1.239 schwarze 242: case ROFFT_HEAD:
243: assert(p->parent->type == ROFFT_BLOCK);
1.53 kristaps 244: p->parent->head = p;
1.27 kristaps 245: break;
1.239 schwarze 246: case ROFFT_TAIL:
247: assert(p->parent->type == ROFFT_BLOCK);
1.53 kristaps 248: p->parent->tail = p;
1.27 kristaps 249: break;
1.239 schwarze 250: case ROFFT_BODY:
1.152 schwarze 251: if (p->end)
252: break;
1.239 schwarze 253: assert(p->parent->type == ROFFT_BLOCK);
1.53 kristaps 254: p->parent->body = p;
1.27 kristaps 255: break;
256: default:
257: break;
258: }
259:
1.1 kristaps 260: mdoc->last = p;
1.71 kristaps 261:
262: switch (p->type) {
1.239 schwarze 263: case ROFFT_TBL:
1.176 kristaps 264: /* FALLTHROUGH */
1.239 schwarze 265: case ROFFT_TEXT:
1.230 schwarze 266: mdoc_valid_post(mdoc);
1.71 kristaps 267: break;
268: default:
269: break;
270: }
1.1 kristaps 271: }
272:
1.240 schwarze 273: static struct roff_node *
1.242 schwarze 274: node_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 275: int tok, enum roff_type type)
1.45 kristaps 276: {
1.240 schwarze 277: struct roff_node *p;
1.46 kristaps 278:
1.240 schwarze 279: p = mandoc_calloc(1, sizeof(*p));
1.203 schwarze 280: p->sec = mdoc->lastsec;
1.73 kristaps 281: p->line = line;
282: p->pos = pos;
283: p->tok = tok;
1.118 kristaps 284: p->type = type;
1.150 kristaps 285:
286: /* Flag analysis. */
287:
1.203 schwarze 288: if (MDOC_SYNOPSIS & mdoc->flags)
1.153 schwarze 289: p->flags |= MDOC_SYNPRETTY;
290: else
291: p->flags &= ~MDOC_SYNPRETTY;
1.203 schwarze 292: if (MDOC_NEWLINE & mdoc->flags)
1.130 kristaps 293: p->flags |= MDOC_LINE;
1.203 schwarze 294: mdoc->flags &= ~MDOC_NEWLINE;
1.150 kristaps 295:
1.46 kristaps 296: return(p);
1.45 kristaps 297: }
298:
1.231 schwarze 299: void
1.242 schwarze 300: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.17 kristaps 301: {
1.240 schwarze 302: struct roff_node *p;
1.17 kristaps 303:
1.239 schwarze 304: p = node_alloc(mdoc, line, pos, tok, ROFFT_TAIL);
1.230 schwarze 305: node_append(mdoc, p);
1.242 schwarze 306: mdoc->next = ROFF_NEXT_CHILD;
1.17 kristaps 307: }
308:
1.240 schwarze 309: struct roff_node *
1.242 schwarze 310: mdoc_head_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.1 kristaps 311: {
1.240 schwarze 312: struct roff_node *p;
1.1 kristaps 313:
1.203 schwarze 314: assert(mdoc->first);
315: assert(mdoc->last);
1.239 schwarze 316: p = node_alloc(mdoc, line, pos, tok, ROFFT_HEAD);
1.230 schwarze 317: node_append(mdoc, p);
1.242 schwarze 318: mdoc->next = ROFF_NEXT_CHILD;
1.231 schwarze 319: return(p);
1.1 kristaps 320: }
321:
1.240 schwarze 322: struct roff_node *
1.242 schwarze 323: mdoc_body_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.1 kristaps 324: {
1.240 schwarze 325: struct roff_node *p;
1.1 kristaps 326:
1.239 schwarze 327: p = node_alloc(mdoc, line, pos, tok, ROFFT_BODY);
1.230 schwarze 328: node_append(mdoc, p);
1.242 schwarze 329: mdoc->next = ROFF_NEXT_CHILD;
1.231 schwarze 330: return(p);
1.152 schwarze 331: }
332:
1.240 schwarze 333: struct roff_node *
1.242 schwarze 334: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
1.240 schwarze 335: struct roff_node *body, enum mdoc_endbody end)
1.152 schwarze 336: {
1.240 schwarze 337: struct roff_node *p;
1.152 schwarze 338:
1.237 schwarze 339: body->flags |= MDOC_ENDED;
340: body->parent->flags |= MDOC_ENDED;
1.239 schwarze 341: p = node_alloc(mdoc, line, pos, tok, ROFFT_BODY);
1.237 schwarze 342: p->body = body;
1.202 schwarze 343: p->norm = body->norm;
1.152 schwarze 344: p->end = end;
1.230 schwarze 345: node_append(mdoc, p);
1.242 schwarze 346: mdoc->next = ROFF_NEXT_SIBLING;
1.235 schwarze 347: return(p);
1.1 kristaps 348: }
349:
1.240 schwarze 350: struct roff_node *
1.242 schwarze 351: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 352: int tok, struct mdoc_arg *args)
1.1 kristaps 353: {
1.240 schwarze 354: struct roff_node *p;
1.1 kristaps 355:
1.239 schwarze 356: p = node_alloc(mdoc, line, pos, tok, ROFFT_BLOCK);
1.77 kristaps 357: p->args = args;
358: if (p->args)
1.53 kristaps 359: (args->refcnt)++;
1.172 kristaps 360:
361: switch (tok) {
1.213 schwarze 362: case MDOC_Bd:
1.172 kristaps 363: /* FALLTHROUGH */
1.213 schwarze 364: case MDOC_Bf:
1.172 kristaps 365: /* FALLTHROUGH */
1.213 schwarze 366: case MDOC_Bl:
1.217 schwarze 367: /* FALLTHROUGH */
368: case MDOC_En:
1.173 kristaps 369: /* FALLTHROUGH */
1.213 schwarze 370: case MDOC_Rs:
1.172 kristaps 371: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
372: break;
373: default:
374: break;
375: }
1.230 schwarze 376: node_append(mdoc, p);
1.242 schwarze 377: mdoc->next = ROFF_NEXT_CHILD;
1.231 schwarze 378: return(p);
1.1 kristaps 379: }
380:
1.231 schwarze 381: void
1.242 schwarze 382: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 383: int tok, struct mdoc_arg *args)
1.1 kristaps 384: {
1.240 schwarze 385: struct roff_node *p;
1.1 kristaps 386:
1.239 schwarze 387: p = node_alloc(mdoc, line, pos, tok, ROFFT_ELEM);
1.77 kristaps 388: p->args = args;
389: if (p->args)
1.53 kristaps 390: (args->refcnt)++;
1.172 kristaps 391:
392: switch (tok) {
1.213 schwarze 393: case MDOC_An:
1.172 kristaps 394: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
395: break;
396: default:
397: break;
398: }
1.230 schwarze 399: node_append(mdoc, p);
1.242 schwarze 400: mdoc->next = ROFF_NEXT_CHILD;
1.175 kristaps 401: }
1.1 kristaps 402:
1.231 schwarze 403: void
1.242 schwarze 404: mdoc_word_alloc(struct roff_man *mdoc, int line, int pos, const char *p)
1.1 kristaps 405: {
1.240 schwarze 406: struct roff_node *n;
1.1 kristaps 407:
1.239 schwarze 408: n = node_alloc(mdoc, line, pos, MDOC_MAX, ROFFT_TEXT);
1.203 schwarze 409: n->string = roff_strdup(mdoc->roff, p);
1.230 schwarze 410: node_append(mdoc, n);
1.242 schwarze 411: mdoc->next = ROFF_NEXT_SIBLING;
1.91 kristaps 412: }
413:
1.205 schwarze 414: void
1.242 schwarze 415: mdoc_word_append(struct roff_man *mdoc, const char *p)
1.205 schwarze 416: {
1.240 schwarze 417: struct roff_node *n;
1.205 schwarze 418: char *addstr, *newstr;
419:
420: n = mdoc->last;
421: addstr = roff_strdup(mdoc->roff, p);
1.210 schwarze 422: mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
1.205 schwarze 423: free(addstr);
424: free(n->string);
425: n->string = newstr;
1.242 schwarze 426: mdoc->next = ROFF_NEXT_SIBLING;
1.205 schwarze 427: }
1.91 kristaps 428:
1.155 kristaps 429: static void
1.240 schwarze 430: mdoc_node_free(struct roff_node *p)
1.1 kristaps 431: {
432:
1.239 schwarze 433: if (p->type == ROFFT_BLOCK || p->type == ROFFT_ELEM)
1.171 kristaps 434: free(p->norm);
1.53 kristaps 435: if (p->string)
436: free(p->string);
437: if (p->args)
438: mdoc_argv_free(p->args);
1.1 kristaps 439: free(p);
440: }
441:
1.121 kristaps 442: static void
1.242 schwarze 443: mdoc_node_unlink(struct roff_man *mdoc, struct roff_node *n)
1.121 kristaps 444: {
445:
446: /* Adjust siblings. */
447:
448: if (n->prev)
449: n->prev->next = n->next;
450: if (n->next)
451: n->next->prev = n->prev;
452:
453: /* Adjust parent. */
454:
455: if (n->parent) {
456: n->parent->nchild--;
457: if (n->parent->child == n)
458: n->parent->child = n->prev ? n->prev : n->next;
1.169 kristaps 459: if (n->parent->last == n)
460: n->parent->last = n->prev ? n->prev : NULL;
1.121 kristaps 461: }
462:
463: /* Adjust parse point, if applicable. */
464:
1.203 schwarze 465: if (mdoc && mdoc->last == n) {
1.121 kristaps 466: if (n->prev) {
1.203 schwarze 467: mdoc->last = n->prev;
1.242 schwarze 468: mdoc->next = ROFF_NEXT_SIBLING;
1.121 kristaps 469: } else {
1.203 schwarze 470: mdoc->last = n->parent;
1.242 schwarze 471: mdoc->next = ROFF_NEXT_CHILD;
1.121 kristaps 472: }
473: }
474:
1.203 schwarze 475: if (mdoc && mdoc->first == n)
476: mdoc->first = NULL;
1.121 kristaps 477: }
478:
1.53 kristaps 479: void
1.242 schwarze 480: mdoc_node_delete(struct roff_man *mdoc, struct roff_node *p)
1.1 kristaps 481: {
482:
1.121 kristaps 483: while (p->child) {
484: assert(p->nchild);
1.203 schwarze 485: mdoc_node_delete(mdoc, p->child);
1.121 kristaps 486: }
487: assert(0 == p->nchild);
1.1 kristaps 488:
1.203 schwarze 489: mdoc_node_unlink(mdoc, p);
1.53 kristaps 490: mdoc_node_free(p);
1.201 schwarze 491: }
492:
1.231 schwarze 493: void
1.242 schwarze 494: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201 schwarze 495: {
496:
1.203 schwarze 497: mdoc_node_unlink(mdoc, p);
1.230 schwarze 498: node_append(mdoc, p);
1.1 kristaps 499: }
500:
1.53 kristaps 501: /*
502: * Parse free-form text, that is, a line that does not begin with the
503: * control character.
504: */
505: static int
1.242 schwarze 506: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1 kristaps 507: {
1.240 schwarze 508: struct roff_node *n;
1.142 kristaps 509: char *c, *ws, *end;
510:
1.203 schwarze 511: assert(mdoc->last);
512: n = mdoc->last;
1.142 kristaps 513:
514: /*
1.144 kristaps 515: * Divert directly to list processing if we're encountering a
1.239 schwarze 516: * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
517: * (a ROFFT_BODY means it's already open, in which case we should
1.144 kristaps 518: * process within its context in the normal way).
1.142 kristaps 519: */
520:
1.239 schwarze 521: if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238 schwarze 522: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.144 kristaps 523: /* `Bl' is open without any children. */
1.203 schwarze 524: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 525: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
526: return(1);
1.142 kristaps 527: }
528:
1.239 schwarze 529: if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213 schwarze 530: NULL != n->parent &&
531: MDOC_Bl == n->parent->tok &&
532: LIST_column == n->parent->norm->Bl.type) {
1.144 kristaps 533: /* `Bl' has block-level `It' children. */
1.203 schwarze 534: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 535: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
536: return(1);
1.142 kristaps 537: }
1.124 kristaps 538:
1.137 schwarze 539: /*
540: * Search for the beginning of unescaped trailing whitespace (ws)
541: * and for the first character not to be output (end).
542: */
1.139 kristaps 543:
544: /* FIXME: replace with strcspn(). */
1.137 schwarze 545: ws = NULL;
546: for (c = end = buf + offs; *c; c++) {
547: switch (*c) {
548: case ' ':
549: if (NULL == ws)
550: ws = c;
551: continue;
552: case '\t':
553: /*
554: * Always warn about trailing tabs,
555: * even outside literal context,
556: * where they should be put on the next line.
557: */
558: if (NULL == ws)
559: ws = c;
560: /*
561: * Strip trailing tabs in literal context only;
562: * outside, they affect the next line.
563: */
1.203 schwarze 564: if (MDOC_LITERAL & mdoc->flags)
1.137 schwarze 565: continue;
566: break;
567: case '\\':
568: /* Skip the escaped character, too, if any. */
569: if (c[1])
570: c++;
571: /* FALLTHROUGH */
572: default:
573: ws = NULL;
574: break;
575: }
576: end = c + 1;
577: }
578: *end = '\0';
1.91 kristaps 579:
1.137 schwarze 580: if (ws)
1.218 schwarze 581: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
582: line, (int)(ws-buf), NULL);
1.115 kristaps 583:
1.231 schwarze 584: if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.218 schwarze 585: mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
586: line, (int)(c - buf), NULL);
1.124 kristaps 587:
1.119 kristaps 588: /*
1.165 schwarze 589: * Insert a `sp' in the case of a blank line. Technically,
1.124 kristaps 590: * blank lines aren't allowed, but enough manuals assume this
591: * behaviour that we want to work around it.
1.119 kristaps 592: */
1.231 schwarze 593: mdoc_elem_alloc(mdoc, line, offs, MDOC_sp, NULL);
1.242 schwarze 594: mdoc->next = ROFF_NEXT_SIBLING;
1.230 schwarze 595: mdoc_valid_post(mdoc);
596: return(1);
1.119 kristaps 597: }
1.68 kristaps 598:
1.231 schwarze 599: mdoc_word_alloc(mdoc, line, offs, buf+offs);
1.91 kristaps 600:
1.231 schwarze 601: if (mdoc->flags & MDOC_LITERAL)
1.137 schwarze 602: return(1);
1.128 kristaps 603:
604: /*
605: * End-of-sentence check. If the last character is an unescaped
606: * EOS character, then flag the node as being the end of a
607: * sentence. The front-end will know how to interpret this.
608: */
1.132 kristaps 609:
1.137 schwarze 610: assert(buf < end);
611:
1.207 schwarze 612: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203 schwarze 613: mdoc->last->flags |= MDOC_EOS;
1.128 kristaps 614: return(1);
1.1 kristaps 615: }
616:
1.53 kristaps 617: /*
618: * Parse a macro line, that is, a line beginning with the control
619: * character.
620: */
1.155 kristaps 621: static int
1.242 schwarze 622: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 623: {
1.240 schwarze 624: struct roff_node *n;
1.229 schwarze 625: const char *cp;
1.240 schwarze 626: int tok;
1.188 kristaps 627: int i, sv;
1.144 kristaps 628: char mac[5];
1.63 kristaps 629:
1.188 kristaps 630: sv = offs;
1.130 kristaps 631:
1.213 schwarze 632: /*
1.162 schwarze 633: * Copy the first word into a nil-terminated buffer.
1.229 schwarze 634: * Stop when a space, tab, escape, or eoln is encountered.
1.160 kristaps 635: */
1.1 kristaps 636:
1.188 kristaps 637: i = 0;
1.229 schwarze 638: while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.188 kristaps 639: mac[i++] = buf[offs++];
640:
641: mac[i] = '\0';
642:
1.214 schwarze 643: tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : MDOC_MAX;
1.1 kristaps 644:
1.229 schwarze 645: if (tok == MDOC_MAX) {
1.222 schwarze 646: mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
647: ln, sv, buf + sv - 1);
1.58 kristaps 648: return(1);
1.53 kristaps 649: }
1.1 kristaps 650:
1.229 schwarze 651: /* Skip a leading escape sequence or tab. */
1.160 kristaps 652:
1.229 schwarze 653: switch (buf[offs]) {
654: case '\\':
655: cp = buf + offs + 1;
656: mandoc_escape(&cp, NULL, NULL);
657: offs = cp - buf;
658: break;
659: case '\t':
1.188 kristaps 660: offs++;
1.229 schwarze 661: break;
662: default:
663: break;
664: }
1.160 kristaps 665:
666: /* Jump to the next non-whitespace word. */
1.1 kristaps 667:
1.188 kristaps 668: while (buf[offs] && ' ' == buf[offs])
669: offs++;
1.1 kristaps 670:
1.213 schwarze 671: /*
1.125 kristaps 672: * Trailing whitespace. Note that tabs are allowed to be passed
673: * into the parser as "text", so we only warn about spaces here.
674: */
1.115 kristaps 675:
1.188 kristaps 676: if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218 schwarze 677: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
678: ln, offs - 1, NULL);
1.115 kristaps 679:
1.144 kristaps 680: /*
681: * If an initial macro or a list invocation, divert directly
682: * into macro processing.
683: */
684:
1.232 schwarze 685: if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
686: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
687: return(1);
688: }
1.144 kristaps 689:
1.203 schwarze 690: n = mdoc->last;
691: assert(mdoc->last);
1.144 kristaps 692:
693: /*
694: * If the first macro of a `Bl -column', open an `It' block
695: * context around the parsed macro.
696: */
697:
1.239 schwarze 698: if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238 schwarze 699: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.203 schwarze 700: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 701: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
702: return(1);
1.144 kristaps 703: }
704:
705: /*
706: * If we're following a block-level `It' within a `Bl -column'
707: * context (perhaps opened in the above block or in ptext()),
708: * then open an `It' block context around the parsed macro.
1.98 kristaps 709: */
1.144 kristaps 710:
1.239 schwarze 711: if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213 schwarze 712: NULL != n->parent &&
713: MDOC_Bl == n->parent->tok &&
714: LIST_column == n->parent->norm->Bl.type) {
1.203 schwarze 715: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 716: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
717: return(1);
1.144 kristaps 718: }
719:
720: /* Normal processing of a macro. */
721:
1.232 schwarze 722: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.208 schwarze 723:
724: /* In quick mode (for mandocdb), abort after the NAME section. */
725:
726: if (mdoc->quick && MDOC_Sh == tok &&
727: SEC_NAME != mdoc->last->sec)
728: return(2);
1.1 kristaps 729:
1.53 kristaps 730: return(1);
1.1 kristaps 731: }
1.100 kristaps 732:
1.186 kristaps 733: enum mdelim
734: mdoc_isdelim(const char *p)
735: {
736:
737: if ('\0' == p[0])
738: return(DELIM_NONE);
739:
740: if ('\0' == p[1])
741: switch (p[0]) {
1.213 schwarze 742: case '(':
1.186 kristaps 743: /* FALLTHROUGH */
1.213 schwarze 744: case '[':
1.186 kristaps 745: return(DELIM_OPEN);
1.213 schwarze 746: case '|':
1.186 kristaps 747: return(DELIM_MIDDLE);
1.213 schwarze 748: case '.':
1.186 kristaps 749: /* FALLTHROUGH */
1.213 schwarze 750: case ',':
1.186 kristaps 751: /* FALLTHROUGH */
1.213 schwarze 752: case ';':
1.186 kristaps 753: /* FALLTHROUGH */
1.213 schwarze 754: case ':':
1.186 kristaps 755: /* FALLTHROUGH */
1.213 schwarze 756: case '?':
1.186 kristaps 757: /* FALLTHROUGH */
1.213 schwarze 758: case '!':
1.186 kristaps 759: /* FALLTHROUGH */
1.213 schwarze 760: case ')':
1.186 kristaps 761: /* FALLTHROUGH */
1.213 schwarze 762: case ']':
1.186 kristaps 763: return(DELIM_CLOSE);
764: default:
765: return(DELIM_NONE);
766: }
767:
768: if ('\\' != p[0])
769: return(DELIM_NONE);
1.100 kristaps 770:
1.186 kristaps 771: if (0 == strcmp(p + 1, "."))
772: return(DELIM_CLOSE);
1.200 schwarze 773: if (0 == strcmp(p + 1, "fR|\\fP"))
1.186 kristaps 774: return(DELIM_MIDDLE);
775:
776: return(DELIM_NONE);
1.211 schwarze 777: }
778:
779: void
1.240 schwarze 780: mdoc_deroff(char **dest, const struct roff_node *n)
1.211 schwarze 781: {
782: char *cp;
783: size_t sz;
784:
1.239 schwarze 785: if (n->type != ROFFT_TEXT) {
1.211 schwarze 786: for (n = n->child; n; n = n->next)
787: mdoc_deroff(dest, n);
788: return;
789: }
790:
791: /* Skip leading whitespace. */
792:
793: for (cp = n->string; '\0' != *cp; cp++)
794: if (0 == isspace((unsigned char)*cp))
795: break;
796:
797: /* Skip trailing whitespace. */
798:
799: for (sz = strlen(cp); sz; sz--)
800: if (0 == isspace((unsigned char)cp[sz-1]))
801: break;
802:
803: /* Skip empty strings. */
804:
805: if (0 == sz)
806: return;
807:
808: if (NULL == *dest) {
809: *dest = mandoc_strndup(cp, sz);
810: return;
811: }
812:
813: mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
814: free(*dest);
815: *dest = cp;
1.186 kristaps 816: }
CVSweb