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