Annotation of mandoc/mdoc.c, Revision 1.254
1.254 ! schwarze 1: /* $Id: mdoc.c,v 1.253 2015/10/06 18:32:19 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.247 schwarze 35: #include "roff_int.h"
1.70 kristaps 36: #include "libmdoc.h"
1.1 kristaps 37:
1.216 schwarze 38: const char *const __mdoc_macronames[MDOC_MAX + 1] = {
1.248 schwarze 39: "text",
1.82 kristaps 40: "Ap", "Dd", "Dt", "Os",
1.1 kristaps 41: "Sh", "Ss", "Pp", "D1",
42: "Dl", "Bd", "Ed", "Bl",
43: "El", "It", "Ad", "An",
44: "Ar", "Cd", "Cm", "Dv",
45: "Er", "Ev", "Ex", "Fa",
46: "Fd", "Fl", "Fn", "Ft",
47: "Ic", "In", "Li", "Nd",
48: "Nm", "Op", "Ot", "Pa",
49: "Rv", "St", "Va", "Vt",
1.114 kristaps 50: "Xr", "%A", "%B", "%D",
51: "%I", "%J", "%N", "%O",
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.114 kristaps 68: "Brc", "%C", "Es", "En",
69: "Dx", "%Q", "br", "sp",
1.248 schwarze 70: "%U", "Ta", "ll",
71: };
1.1 kristaps 72:
1.213 schwarze 73: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
1.1 kristaps 74: "split", "nosplit", "ragged",
1.213 schwarze 75: "unfilled", "literal", "file",
76: "offset", "bullet", "dash",
77: "hyphen", "item", "enum",
78: "tag", "diag", "hang",
79: "ohang", "inset", "column",
80: "width", "compact", "std",
1.52 kristaps 81: "filled", "words", "emphasis",
1.108 kristaps 82: "symbolic", "nested", "centered"
1.1 kristaps 83: };
84:
1.248 schwarze 85: const char * const *mdoc_macronames = __mdoc_macronames + 1;
1.1 kristaps 86: const char * const *mdoc_argnames = __mdoc_argnames;
87:
1.242 schwarze 88: static int mdoc_ptext(struct roff_man *, int, char *, int);
89: static int mdoc_pmacro(struct roff_man *, int, char *, int);
1.88 kristaps 90:
1.181 kristaps 91:
1.50 kristaps 92: /*
1.53 kristaps 93: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 94: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 95: */
1.20 kristaps 96: int
1.242 schwarze 97: mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 98: {
99:
1.239 schwarze 100: if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
1.228 schwarze 101: mdoc->flags |= MDOC_NEWLINE;
1.153 schwarze 102:
103: /*
104: * Let the roff nS register switch SYNOPSIS mode early,
105: * such that the parser knows at all times
106: * whether this mode is on or off.
107: * Note that this mode is also switched by the Sh macro.
108: */
1.204 schwarze 109: if (roff_getreg(mdoc->roff, "nS"))
110: mdoc->flags |= MDOC_SYNOPSIS;
111: else
112: mdoc->flags &= ~MDOC_SYNOPSIS;
1.153 schwarze 113:
1.253 schwarze 114: return roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213 schwarze 115: mdoc_pmacro(mdoc, ln, buf, offs) :
1.253 schwarze 116: mdoc_ptext(mdoc, ln, buf, offs);
1.1 kristaps 117: }
118:
1.232 schwarze 119: void
1.148 kristaps 120: mdoc_macro(MACRO_PROT_ARGS)
1.88 kristaps 121: {
1.248 schwarze 122: assert(tok > TOKEN_NONE && tok < MDOC_MAX);
1.122 kristaps 123:
1.223 schwarze 124: if (mdoc->flags & MDOC_PBODY) {
125: if (tok == MDOC_Dt) {
126: mandoc_vmsg(MANDOCERR_DT_LATE,
127: mdoc->parse, line, ppos,
128: "Dt %s", buf + *pos);
1.232 schwarze 129: return;
1.223 schwarze 130: }
131: } else if ( ! (mdoc_macros[tok].flags & MDOC_PROLOGUE)) {
132: if (mdoc->meta.title == NULL) {
133: mandoc_vmsg(MANDOCERR_DT_NOTITLE,
134: mdoc->parse, line, ppos, "%s %s",
135: mdoc_macronames[tok], buf + *pos);
136: mdoc->meta.title = mandoc_strdup("UNTITLED");
137: }
1.203 schwarze 138: if (NULL == mdoc->meta.vol)
139: mdoc->meta.vol = mandoc_strdup("LOCAL");
140: mdoc->flags |= MDOC_PBODY;
1.120 kristaps 141: }
1.232 schwarze 142: (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
1.73 kristaps 143: }
144:
1.231 schwarze 145: void
1.242 schwarze 146: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.17 kristaps 147: {
1.240 schwarze 148: struct roff_node *p;
1.17 kristaps 149:
1.247 schwarze 150: p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
151: roff_node_append(mdoc, p);
1.242 schwarze 152: mdoc->next = ROFF_NEXT_CHILD;
1.152 schwarze 153: }
154:
1.240 schwarze 155: struct roff_node *
1.242 schwarze 156: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
1.240 schwarze 157: struct roff_node *body, enum mdoc_endbody end)
1.152 schwarze 158: {
1.240 schwarze 159: struct roff_node *p;
1.152 schwarze 160:
1.237 schwarze 161: body->flags |= MDOC_ENDED;
162: body->parent->flags |= MDOC_ENDED;
1.247 schwarze 163: p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
1.237 schwarze 164: p->body = body;
1.202 schwarze 165: p->norm = body->norm;
1.152 schwarze 166: p->end = end;
1.247 schwarze 167: roff_node_append(mdoc, p);
1.242 schwarze 168: mdoc->next = ROFF_NEXT_SIBLING;
1.253 schwarze 169: return p;
1.1 kristaps 170: }
171:
1.240 schwarze 172: struct roff_node *
1.242 schwarze 173: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 174: int tok, struct mdoc_arg *args)
1.1 kristaps 175: {
1.240 schwarze 176: struct roff_node *p;
1.1 kristaps 177:
1.247 schwarze 178: p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
1.77 kristaps 179: p->args = args;
180: if (p->args)
1.53 kristaps 181: (args->refcnt)++;
1.172 kristaps 182:
183: switch (tok) {
1.213 schwarze 184: case MDOC_Bd:
185: case MDOC_Bf:
186: case MDOC_Bl:
1.217 schwarze 187: case MDOC_En:
1.213 schwarze 188: case MDOC_Rs:
1.172 kristaps 189: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
190: break;
191: default:
192: break;
193: }
1.247 schwarze 194: roff_node_append(mdoc, p);
1.242 schwarze 195: mdoc->next = ROFF_NEXT_CHILD;
1.253 schwarze 196: return p;
1.1 kristaps 197: }
198:
1.231 schwarze 199: void
1.242 schwarze 200: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 201: int tok, struct mdoc_arg *args)
1.1 kristaps 202: {
1.240 schwarze 203: struct roff_node *p;
1.1 kristaps 204:
1.247 schwarze 205: p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
1.77 kristaps 206: p->args = args;
207: if (p->args)
1.53 kristaps 208: (args->refcnt)++;
1.172 kristaps 209:
210: switch (tok) {
1.213 schwarze 211: case MDOC_An:
1.172 kristaps 212: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
213: break;
214: default:
215: break;
216: }
1.247 schwarze 217: roff_node_append(mdoc, p);
1.242 schwarze 218: mdoc->next = ROFF_NEXT_CHILD;
1.175 kristaps 219: }
1.1 kristaps 220:
1.231 schwarze 221: void
1.242 schwarze 222: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201 schwarze 223: {
224:
1.247 schwarze 225: roff_node_unlink(mdoc, p);
226: roff_node_append(mdoc, p);
1.1 kristaps 227: }
228:
1.53 kristaps 229: /*
230: * Parse free-form text, that is, a line that does not begin with the
231: * control character.
232: */
233: static int
1.242 schwarze 234: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1 kristaps 235: {
1.240 schwarze 236: struct roff_node *n;
1.142 kristaps 237: char *c, *ws, *end;
238:
1.203 schwarze 239: assert(mdoc->last);
240: n = mdoc->last;
1.142 kristaps 241:
242: /*
1.144 kristaps 243: * Divert directly to list processing if we're encountering a
1.239 schwarze 244: * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
245: * (a ROFFT_BODY means it's already open, in which case we should
1.144 kristaps 246: * process within its context in the normal way).
1.142 kristaps 247: */
248:
1.239 schwarze 249: if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238 schwarze 250: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.144 kristaps 251: /* `Bl' is open without any children. */
1.203 schwarze 252: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 253: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
1.253 schwarze 254: return 1;
1.142 kristaps 255: }
256:
1.239 schwarze 257: if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213 schwarze 258: NULL != n->parent &&
259: MDOC_Bl == n->parent->tok &&
260: LIST_column == n->parent->norm->Bl.type) {
1.144 kristaps 261: /* `Bl' has block-level `It' children. */
1.203 schwarze 262: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 263: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
1.253 schwarze 264: return 1;
1.142 kristaps 265: }
1.124 kristaps 266:
1.137 schwarze 267: /*
268: * Search for the beginning of unescaped trailing whitespace (ws)
269: * and for the first character not to be output (end).
270: */
1.139 kristaps 271:
272: /* FIXME: replace with strcspn(). */
1.137 schwarze 273: ws = NULL;
274: for (c = end = buf + offs; *c; c++) {
275: switch (*c) {
276: case ' ':
277: if (NULL == ws)
278: ws = c;
279: continue;
280: case '\t':
281: /*
282: * Always warn about trailing tabs,
283: * even outside literal context,
284: * where they should be put on the next line.
285: */
286: if (NULL == ws)
287: ws = c;
288: /*
289: * Strip trailing tabs in literal context only;
290: * outside, they affect the next line.
291: */
1.203 schwarze 292: if (MDOC_LITERAL & mdoc->flags)
1.137 schwarze 293: continue;
294: break;
295: case '\\':
296: /* Skip the escaped character, too, if any. */
297: if (c[1])
298: c++;
299: /* FALLTHROUGH */
300: default:
301: ws = NULL;
302: break;
303: }
304: end = c + 1;
305: }
306: *end = '\0';
1.91 kristaps 307:
1.137 schwarze 308: if (ws)
1.218 schwarze 309: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
310: line, (int)(ws-buf), NULL);
1.115 kristaps 311:
1.231 schwarze 312: if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.218 schwarze 313: mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
314: line, (int)(c - buf), NULL);
1.124 kristaps 315:
1.119 kristaps 316: /*
1.165 schwarze 317: * Insert a `sp' in the case of a blank line. Technically,
1.124 kristaps 318: * blank lines aren't allowed, but enough manuals assume this
319: * behaviour that we want to work around it.
1.119 kristaps 320: */
1.250 schwarze 321: roff_elem_alloc(mdoc, line, offs, MDOC_sp);
1.242 schwarze 322: mdoc->next = ROFF_NEXT_SIBLING;
1.230 schwarze 323: mdoc_valid_post(mdoc);
1.253 schwarze 324: return 1;
1.119 kristaps 325: }
1.68 kristaps 326:
1.249 schwarze 327: roff_word_alloc(mdoc, line, offs, buf+offs);
1.91 kristaps 328:
1.231 schwarze 329: if (mdoc->flags & MDOC_LITERAL)
1.253 schwarze 330: return 1;
1.128 kristaps 331:
332: /*
333: * End-of-sentence check. If the last character is an unescaped
334: * EOS character, then flag the node as being the end of a
335: * sentence. The front-end will know how to interpret this.
336: */
1.132 kristaps 337:
1.137 schwarze 338: assert(buf < end);
339:
1.207 schwarze 340: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203 schwarze 341: mdoc->last->flags |= MDOC_EOS;
1.253 schwarze 342: return 1;
1.1 kristaps 343: }
344:
1.53 kristaps 345: /*
346: * Parse a macro line, that is, a line beginning with the control
347: * character.
348: */
1.155 kristaps 349: static int
1.242 schwarze 350: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 351: {
1.240 schwarze 352: struct roff_node *n;
1.229 schwarze 353: const char *cp;
1.240 schwarze 354: int tok;
1.188 kristaps 355: int i, sv;
1.144 kristaps 356: char mac[5];
1.63 kristaps 357:
1.188 kristaps 358: sv = offs;
1.130 kristaps 359:
1.213 schwarze 360: /*
1.162 schwarze 361: * Copy the first word into a nil-terminated buffer.
1.229 schwarze 362: * Stop when a space, tab, escape, or eoln is encountered.
1.160 kristaps 363: */
1.1 kristaps 364:
1.188 kristaps 365: i = 0;
1.229 schwarze 366: while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.188 kristaps 367: mac[i++] = buf[offs++];
368:
369: mac[i] = '\0';
370:
1.248 schwarze 371: tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
1.1 kristaps 372:
1.248 schwarze 373: if (tok == TOKEN_NONE) {
1.222 schwarze 374: mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
375: ln, sv, buf + sv - 1);
1.253 schwarze 376: return 1;
1.53 kristaps 377: }
1.1 kristaps 378:
1.229 schwarze 379: /* Skip a leading escape sequence or tab. */
1.160 kristaps 380:
1.229 schwarze 381: switch (buf[offs]) {
382: case '\\':
383: cp = buf + offs + 1;
384: mandoc_escape(&cp, NULL, NULL);
385: offs = cp - buf;
386: break;
387: case '\t':
1.188 kristaps 388: offs++;
1.229 schwarze 389: break;
390: default:
391: break;
392: }
1.160 kristaps 393:
394: /* Jump to the next non-whitespace word. */
1.1 kristaps 395:
1.188 kristaps 396: while (buf[offs] && ' ' == buf[offs])
397: offs++;
1.1 kristaps 398:
1.213 schwarze 399: /*
1.125 kristaps 400: * Trailing whitespace. Note that tabs are allowed to be passed
401: * into the parser as "text", so we only warn about spaces here.
402: */
1.115 kristaps 403:
1.188 kristaps 404: if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218 schwarze 405: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
406: ln, offs - 1, NULL);
1.115 kristaps 407:
1.144 kristaps 408: /*
409: * If an initial macro or a list invocation, divert directly
410: * into macro processing.
411: */
412:
1.232 schwarze 413: if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
414: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.253 schwarze 415: return 1;
1.232 schwarze 416: }
1.144 kristaps 417:
1.203 schwarze 418: n = mdoc->last;
419: assert(mdoc->last);
1.144 kristaps 420:
421: /*
422: * If the first macro of a `Bl -column', open an `It' block
423: * context around the parsed macro.
424: */
425:
1.239 schwarze 426: if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238 schwarze 427: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.203 schwarze 428: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 429: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
1.253 schwarze 430: return 1;
1.144 kristaps 431: }
432:
433: /*
434: * If we're following a block-level `It' within a `Bl -column'
435: * context (perhaps opened in the above block or in ptext()),
436: * then open an `It' block context around the parsed macro.
1.98 kristaps 437: */
1.144 kristaps 438:
1.239 schwarze 439: if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213 schwarze 440: NULL != n->parent &&
441: MDOC_Bl == n->parent->tok &&
442: LIST_column == n->parent->norm->Bl.type) {
1.203 schwarze 443: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 444: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
1.253 schwarze 445: return 1;
1.144 kristaps 446: }
447:
448: /* Normal processing of a macro. */
449:
1.232 schwarze 450: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.208 schwarze 451:
452: /* In quick mode (for mandocdb), abort after the NAME section. */
453:
454: if (mdoc->quick && MDOC_Sh == tok &&
455: SEC_NAME != mdoc->last->sec)
1.253 schwarze 456: return 2;
1.1 kristaps 457:
1.253 schwarze 458: return 1;
1.1 kristaps 459: }
1.100 kristaps 460:
1.186 kristaps 461: enum mdelim
462: mdoc_isdelim(const char *p)
463: {
464:
465: if ('\0' == p[0])
1.253 schwarze 466: return DELIM_NONE;
1.186 kristaps 467:
468: if ('\0' == p[1])
469: switch (p[0]) {
1.213 schwarze 470: case '(':
471: case '[':
1.253 schwarze 472: return DELIM_OPEN;
1.213 schwarze 473: case '|':
1.253 schwarze 474: return DELIM_MIDDLE;
1.213 schwarze 475: case '.':
476: case ',':
477: case ';':
478: case ':':
479: case '?':
480: case '!':
481: case ')':
482: case ']':
1.253 schwarze 483: return DELIM_CLOSE;
1.186 kristaps 484: default:
1.253 schwarze 485: return DELIM_NONE;
1.186 kristaps 486: }
487:
488: if ('\\' != p[0])
1.253 schwarze 489: return DELIM_NONE;
1.100 kristaps 490:
1.186 kristaps 491: if (0 == strcmp(p + 1, "."))
1.253 schwarze 492: return DELIM_CLOSE;
1.200 schwarze 493: if (0 == strcmp(p + 1, "fR|\\fP"))
1.253 schwarze 494: return DELIM_MIDDLE;
1.186 kristaps 495:
1.253 schwarze 496: return DELIM_NONE;
1.186 kristaps 497: }
CVSweb