Annotation of mandoc/mdoc.c, Revision 1.265
1.265 ! schwarze 1: /* $Id: mdoc.c,v 1.264 2017/05/05 15:17:32 schwarze Exp $ */
1.1 kristaps 2: /*
1.182 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.259 schwarze 4: * Copyright (c) 2010, 2012-2017 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.213 schwarze 38: const char *const __mdoc_argnames[MDOC_ARG_MAX] = {
1.1 kristaps 39: "split", "nosplit", "ragged",
1.213 schwarze 40: "unfilled", "literal", "file",
41: "offset", "bullet", "dash",
42: "hyphen", "item", "enum",
43: "tag", "diag", "hang",
44: "ohang", "inset", "column",
45: "width", "compact", "std",
1.52 kristaps 46: "filled", "words", "emphasis",
1.108 kristaps 47: "symbolic", "nested", "centered"
1.262 schwarze 48: };
1.1 kristaps 49: const char * const *mdoc_argnames = __mdoc_argnames;
50:
1.242 schwarze 51: static int mdoc_ptext(struct roff_man *, int, char *, int);
52: static int mdoc_pmacro(struct roff_man *, int, char *, int);
1.88 kristaps 53:
1.181 kristaps 54:
1.50 kristaps 55: /*
1.53 kristaps 56: * Main parse routine. Parses a single line -- really just hands off to
1.123 kristaps 57: * the macro (mdoc_pmacro()) or text parser (mdoc_ptext()).
1.50 kristaps 58: */
1.20 kristaps 59: int
1.242 schwarze 60: mdoc_parseln(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 61: {
62:
1.239 schwarze 63: if (mdoc->last->type != ROFFT_EQN || ln > mdoc->last->line)
1.228 schwarze 64: mdoc->flags |= MDOC_NEWLINE;
1.153 schwarze 65:
66: /*
67: * Let the roff nS register switch SYNOPSIS mode early,
68: * such that the parser knows at all times
69: * whether this mode is on or off.
70: * Note that this mode is also switched by the Sh macro.
71: */
1.204 schwarze 72: if (roff_getreg(mdoc->roff, "nS"))
73: mdoc->flags |= MDOC_SYNOPSIS;
74: else
75: mdoc->flags &= ~MDOC_SYNOPSIS;
1.153 schwarze 76:
1.253 schwarze 77: return roff_getcontrol(mdoc->roff, buf, &offs) ?
1.213 schwarze 78: mdoc_pmacro(mdoc, ln, buf, offs) :
1.253 schwarze 79: mdoc_ptext(mdoc, ln, buf, offs);
1.1 kristaps 80: }
81:
1.232 schwarze 82: void
1.148 kristaps 83: mdoc_macro(MACRO_PROT_ARGS)
1.88 kristaps 84: {
1.262 schwarze 85: assert(tok >= MDOC_Dd && tok < MDOC_MAX);
1.232 schwarze 86: (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
1.73 kristaps 87: }
88:
1.231 schwarze 89: void
1.262 schwarze 90: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, enum roff_tok tok)
1.17 kristaps 91: {
1.240 schwarze 92: struct roff_node *p;
1.17 kristaps 93:
1.247 schwarze 94: p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
95: roff_node_append(mdoc, p);
1.242 schwarze 96: mdoc->next = ROFF_NEXT_CHILD;
1.152 schwarze 97: }
98:
1.240 schwarze 99: struct roff_node *
1.262 schwarze 100: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos,
101: enum roff_tok tok, struct roff_node *body)
1.152 schwarze 102: {
1.240 schwarze 103: struct roff_node *p;
1.152 schwarze 104:
1.258 schwarze 105: body->flags |= NODE_ENDED;
106: body->parent->flags |= NODE_ENDED;
1.247 schwarze 107: p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
1.237 schwarze 108: p->body = body;
1.202 schwarze 109: p->norm = body->norm;
1.260 schwarze 110: p->end = ENDBODY_SPACE;
1.247 schwarze 111: roff_node_append(mdoc, p);
1.242 schwarze 112: mdoc->next = ROFF_NEXT_SIBLING;
1.253 schwarze 113: return p;
1.1 kristaps 114: }
115:
1.240 schwarze 116: struct roff_node *
1.242 schwarze 117: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.262 schwarze 118: enum roff_tok tok, struct mdoc_arg *args)
1.1 kristaps 119: {
1.240 schwarze 120: struct roff_node *p;
1.1 kristaps 121:
1.247 schwarze 122: p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
1.77 kristaps 123: p->args = args;
124: if (p->args)
1.53 kristaps 125: (args->refcnt)++;
1.172 kristaps 126:
127: switch (tok) {
1.213 schwarze 128: case MDOC_Bd:
129: case MDOC_Bf:
130: case MDOC_Bl:
1.217 schwarze 131: case MDOC_En:
1.213 schwarze 132: case MDOC_Rs:
1.172 kristaps 133: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
134: break;
135: default:
136: break;
137: }
1.247 schwarze 138: roff_node_append(mdoc, p);
1.242 schwarze 139: mdoc->next = ROFF_NEXT_CHILD;
1.253 schwarze 140: return p;
1.1 kristaps 141: }
142:
1.231 schwarze 143: void
1.242 schwarze 144: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.262 schwarze 145: enum roff_tok tok, struct mdoc_arg *args)
1.1 kristaps 146: {
1.240 schwarze 147: struct roff_node *p;
1.1 kristaps 148:
1.247 schwarze 149: p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
1.77 kristaps 150: p->args = args;
151: if (p->args)
1.53 kristaps 152: (args->refcnt)++;
1.172 kristaps 153:
154: switch (tok) {
1.213 schwarze 155: case MDOC_An:
1.172 kristaps 156: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
157: break;
158: default:
159: break;
160: }
1.247 schwarze 161: roff_node_append(mdoc, p);
1.242 schwarze 162: mdoc->next = ROFF_NEXT_CHILD;
1.175 kristaps 163: }
1.1 kristaps 164:
1.231 schwarze 165: void
1.242 schwarze 166: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201 schwarze 167: {
168:
1.247 schwarze 169: roff_node_unlink(mdoc, p);
1.256 schwarze 170: p->prev = p->next = NULL;
1.247 schwarze 171: roff_node_append(mdoc, p);
1.1 kristaps 172: }
173:
1.53 kristaps 174: /*
175: * Parse free-form text, that is, a line that does not begin with the
176: * control character.
177: */
178: static int
1.242 schwarze 179: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1 kristaps 180: {
1.240 schwarze 181: struct roff_node *n;
1.142 kristaps 182: char *c, *ws, *end;
183:
1.203 schwarze 184: n = mdoc->last;
1.142 kristaps 185:
186: /*
1.257 schwarze 187: * If a column list contains plain text, assume an implicit item
188: * macro. This can happen one or more times at the beginning
189: * of such a list, intermixed with non-It mdoc macros and with
190: * nodes generated on the roff level, for example by tbl.
1.142 kristaps 191: */
192:
1.257 schwarze 193: if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
194: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
195: (n->parent != NULL && n->parent->tok == MDOC_Bl &&
196: n->parent->norm->Bl.type == LIST_column)) {
1.203 schwarze 197: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 198: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
1.253 schwarze 199: return 1;
1.142 kristaps 200: }
1.124 kristaps 201:
1.137 schwarze 202: /*
203: * Search for the beginning of unescaped trailing whitespace (ws)
204: * and for the first character not to be output (end).
205: */
1.139 kristaps 206:
207: /* FIXME: replace with strcspn(). */
1.137 schwarze 208: ws = NULL;
209: for (c = end = buf + offs; *c; c++) {
210: switch (*c) {
211: case ' ':
212: if (NULL == ws)
213: ws = c;
214: continue;
215: case '\t':
216: /*
217: * Always warn about trailing tabs,
218: * even outside literal context,
219: * where they should be put on the next line.
220: */
221: if (NULL == ws)
222: ws = c;
223: /*
224: * Strip trailing tabs in literal context only;
225: * outside, they affect the next line.
226: */
1.203 schwarze 227: if (MDOC_LITERAL & mdoc->flags)
1.137 schwarze 228: continue;
229: break;
230: case '\\':
231: /* Skip the escaped character, too, if any. */
232: if (c[1])
233: c++;
234: /* FALLTHROUGH */
235: default:
236: ws = NULL;
237: break;
238: }
239: end = c + 1;
240: }
241: *end = '\0';
1.91 kristaps 242:
1.137 schwarze 243: if (ws)
1.218 schwarze 244: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
245: line, (int)(ws-buf), NULL);
1.115 kristaps 246:
1.231 schwarze 247: if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.218 schwarze 248: mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
249: line, (int)(c - buf), NULL);
1.124 kristaps 250:
1.119 kristaps 251: /*
1.165 schwarze 252: * Insert a `sp' in the case of a blank line. Technically,
1.124 kristaps 253: * blank lines aren't allowed, but enough manuals assume this
254: * behaviour that we want to work around it.
1.119 kristaps 255: */
1.264 schwarze 256: roff_elem_alloc(mdoc, line, offs, ROFF_sp);
1.258 schwarze 257: mdoc->last->flags |= NODE_VALID | NODE_ENDED;
1.242 schwarze 258: mdoc->next = ROFF_NEXT_SIBLING;
1.253 schwarze 259: return 1;
1.119 kristaps 260: }
1.68 kristaps 261:
1.249 schwarze 262: roff_word_alloc(mdoc, line, offs, buf+offs);
1.91 kristaps 263:
1.231 schwarze 264: if (mdoc->flags & MDOC_LITERAL)
1.253 schwarze 265: return 1;
1.128 kristaps 266:
267: /*
268: * End-of-sentence check. If the last character is an unescaped
269: * EOS character, then flag the node as being the end of a
270: * sentence. The front-end will know how to interpret this.
271: */
1.132 kristaps 272:
1.137 schwarze 273: assert(buf < end);
274:
1.207 schwarze 275: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.258 schwarze 276: mdoc->last->flags |= NODE_EOS;
1.259 schwarze 277:
278: for (c = buf + offs; c != NULL; c = strchr(c + 1, '.')) {
279: if (c - buf < offs + 2)
280: continue;
1.265 ! schwarze 281: if (end - c < 3)
1.259 schwarze 282: break;
283: if (isalpha((unsigned char)c[-2]) &&
284: isalpha((unsigned char)c[-1]) &&
285: c[1] == ' ' &&
286: isupper((unsigned char)(c[2] == ' ' ? c[3] : c[2])) &&
287: (c[-2] != 'n' || c[-1] != 'c') &&
288: (c[-2] != 'v' || c[-1] != 's'))
289: mandoc_msg(MANDOCERR_EOS, mdoc->parse,
290: line, (int)(c - buf), NULL);
291: }
292:
1.253 schwarze 293: return 1;
1.1 kristaps 294: }
295:
1.53 kristaps 296: /*
297: * Parse a macro line, that is, a line beginning with the control
298: * character.
299: */
1.155 kristaps 300: static int
1.242 schwarze 301: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 302: {
1.240 schwarze 303: struct roff_node *n;
1.229 schwarze 304: const char *cp;
1.263 schwarze 305: size_t sz;
1.262 schwarze 306: enum roff_tok tok;
1.263 schwarze 307: int sv;
308:
309: /* Determine the line macro. */
1.63 kristaps 310:
1.188 kristaps 311: sv = offs;
1.263 schwarze 312: tok = TOKEN_NONE;
313: for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
314: offs++;
315: if (sz == 2 || sz == 3)
316: tok = roffhash_find(mdoc->mdocmac, buf + sv, sz);
1.248 schwarze 317: if (tok == TOKEN_NONE) {
1.222 schwarze 318: mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
319: ln, sv, buf + sv - 1);
1.253 schwarze 320: return 1;
1.53 kristaps 321: }
1.1 kristaps 322:
1.229 schwarze 323: /* Skip a leading escape sequence or tab. */
1.160 kristaps 324:
1.229 schwarze 325: switch (buf[offs]) {
326: case '\\':
327: cp = buf + offs + 1;
328: mandoc_escape(&cp, NULL, NULL);
329: offs = cp - buf;
330: break;
331: case '\t':
1.188 kristaps 332: offs++;
1.229 schwarze 333: break;
334: default:
335: break;
336: }
1.160 kristaps 337:
338: /* Jump to the next non-whitespace word. */
1.1 kristaps 339:
1.261 schwarze 340: while (buf[offs] == ' ')
1.188 kristaps 341: offs++;
1.1 kristaps 342:
1.213 schwarze 343: /*
1.125 kristaps 344: * Trailing whitespace. Note that tabs are allowed to be passed
345: * into the parser as "text", so we only warn about spaces here.
346: */
1.115 kristaps 347:
1.188 kristaps 348: if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218 schwarze 349: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
350: ln, offs - 1, NULL);
1.115 kristaps 351:
1.144 kristaps 352: /*
353: * If an initial macro or a list invocation, divert directly
354: * into macro processing.
355: */
356:
1.257 schwarze 357: n = mdoc->last;
358: if (n == NULL || tok == MDOC_It || tok == MDOC_El) {
1.232 schwarze 359: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.253 schwarze 360: return 1;
1.232 schwarze 361: }
1.144 kristaps 362:
363: /*
1.257 schwarze 364: * If a column list contains a non-It macro, assume an implicit
365: * item macro. This can happen one or more times at the
366: * beginning of such a list, intermixed with text lines and
367: * with nodes generated on the roff level, for example by tbl.
1.98 kristaps 368: */
1.144 kristaps 369:
1.257 schwarze 370: if ((n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
371: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) ||
372: (n->parent != NULL && n->parent->tok == MDOC_Bl &&
373: n->parent->norm->Bl.type == LIST_column)) {
1.203 schwarze 374: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 375: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
1.253 schwarze 376: return 1;
1.144 kristaps 377: }
378:
379: /* Normal processing of a macro. */
380:
1.232 schwarze 381: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.208 schwarze 382:
383: /* In quick mode (for mandocdb), abort after the NAME section. */
384:
385: if (mdoc->quick && MDOC_Sh == tok &&
386: SEC_NAME != mdoc->last->sec)
1.253 schwarze 387: return 2;
1.1 kristaps 388:
1.253 schwarze 389: return 1;
1.1 kristaps 390: }
1.100 kristaps 391:
1.186 kristaps 392: enum mdelim
393: mdoc_isdelim(const char *p)
394: {
395:
396: if ('\0' == p[0])
1.253 schwarze 397: return DELIM_NONE;
1.186 kristaps 398:
399: if ('\0' == p[1])
400: switch (p[0]) {
1.213 schwarze 401: case '(':
402: case '[':
1.253 schwarze 403: return DELIM_OPEN;
1.213 schwarze 404: case '|':
1.253 schwarze 405: return DELIM_MIDDLE;
1.213 schwarze 406: case '.':
407: case ',':
408: case ';':
409: case ':':
410: case '?':
411: case '!':
412: case ')':
413: case ']':
1.253 schwarze 414: return DELIM_CLOSE;
1.186 kristaps 415: default:
1.253 schwarze 416: return DELIM_NONE;
1.186 kristaps 417: }
418:
419: if ('\\' != p[0])
1.253 schwarze 420: return DELIM_NONE;
1.100 kristaps 421:
1.186 kristaps 422: if (0 == strcmp(p + 1, "."))
1.253 schwarze 423: return DELIM_CLOSE;
1.200 schwarze 424: if (0 == strcmp(p + 1, "fR|\\fP"))
1.253 schwarze 425: return DELIM_MIDDLE;
1.186 kristaps 426:
1.253 schwarze 427: return DELIM_NONE;
1.255 schwarze 428: }
429:
430: void
431: mdoc_validate(struct roff_man *mdoc)
432: {
433:
434: mdoc->last = mdoc->first;
435: mdoc_node_validate(mdoc);
436: mdoc_state_reset(mdoc);
1.186 kristaps 437: }
CVSweb