Annotation of mandoc/mdoc.c, Revision 1.255
1.255 ! schwarze 1: /* $Id: mdoc.c,v 1.254 2015/10/12 00:08:15 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.232 schwarze 124: (*mdoc_macros[tok].fp)(mdoc, tok, line, ppos, pos, buf);
1.73 kristaps 125: }
126:
1.231 schwarze 127: void
1.242 schwarze 128: mdoc_tail_alloc(struct roff_man *mdoc, int line, int pos, int tok)
1.17 kristaps 129: {
1.240 schwarze 130: struct roff_node *p;
1.17 kristaps 131:
1.247 schwarze 132: p = roff_node_alloc(mdoc, line, pos, ROFFT_TAIL, tok);
133: roff_node_append(mdoc, p);
1.242 schwarze 134: mdoc->next = ROFF_NEXT_CHILD;
1.152 schwarze 135: }
136:
1.240 schwarze 137: struct roff_node *
1.242 schwarze 138: mdoc_endbody_alloc(struct roff_man *mdoc, int line, int pos, int tok,
1.240 schwarze 139: struct roff_node *body, enum mdoc_endbody end)
1.152 schwarze 140: {
1.240 schwarze 141: struct roff_node *p;
1.152 schwarze 142:
1.237 schwarze 143: body->flags |= MDOC_ENDED;
144: body->parent->flags |= MDOC_ENDED;
1.247 schwarze 145: p = roff_node_alloc(mdoc, line, pos, ROFFT_BODY, tok);
1.237 schwarze 146: p->body = body;
1.202 schwarze 147: p->norm = body->norm;
1.152 schwarze 148: p->end = end;
1.247 schwarze 149: roff_node_append(mdoc, p);
1.242 schwarze 150: mdoc->next = ROFF_NEXT_SIBLING;
1.253 schwarze 151: return p;
1.1 kristaps 152: }
153:
1.240 schwarze 154: struct roff_node *
1.242 schwarze 155: mdoc_block_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 156: int tok, struct mdoc_arg *args)
1.1 kristaps 157: {
1.240 schwarze 158: struct roff_node *p;
1.1 kristaps 159:
1.247 schwarze 160: p = roff_node_alloc(mdoc, line, pos, ROFFT_BLOCK, tok);
1.77 kristaps 161: p->args = args;
162: if (p->args)
1.53 kristaps 163: (args->refcnt)++;
1.172 kristaps 164:
165: switch (tok) {
1.213 schwarze 166: case MDOC_Bd:
167: case MDOC_Bf:
168: case MDOC_Bl:
1.217 schwarze 169: case MDOC_En:
1.213 schwarze 170: case MDOC_Rs:
1.172 kristaps 171: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
172: break;
173: default:
174: break;
175: }
1.247 schwarze 176: roff_node_append(mdoc, p);
1.242 schwarze 177: mdoc->next = ROFF_NEXT_CHILD;
1.253 schwarze 178: return p;
1.1 kristaps 179: }
180:
1.231 schwarze 181: void
1.242 schwarze 182: mdoc_elem_alloc(struct roff_man *mdoc, int line, int pos,
1.240 schwarze 183: int tok, struct mdoc_arg *args)
1.1 kristaps 184: {
1.240 schwarze 185: struct roff_node *p;
1.1 kristaps 186:
1.247 schwarze 187: p = roff_node_alloc(mdoc, line, pos, ROFFT_ELEM, tok);
1.77 kristaps 188: p->args = args;
189: if (p->args)
1.53 kristaps 190: (args->refcnt)++;
1.172 kristaps 191:
192: switch (tok) {
1.213 schwarze 193: case MDOC_An:
1.172 kristaps 194: p->norm = mandoc_calloc(1, sizeof(union mdoc_data));
195: break;
196: default:
197: break;
198: }
1.247 schwarze 199: roff_node_append(mdoc, p);
1.242 schwarze 200: mdoc->next = ROFF_NEXT_CHILD;
1.175 kristaps 201: }
1.1 kristaps 202:
1.231 schwarze 203: void
1.242 schwarze 204: mdoc_node_relink(struct roff_man *mdoc, struct roff_node *p)
1.201 schwarze 205: {
206:
1.247 schwarze 207: roff_node_unlink(mdoc, p);
208: roff_node_append(mdoc, p);
1.1 kristaps 209: }
210:
1.53 kristaps 211: /*
212: * Parse free-form text, that is, a line that does not begin with the
213: * control character.
214: */
215: static int
1.242 schwarze 216: mdoc_ptext(struct roff_man *mdoc, int line, char *buf, int offs)
1.1 kristaps 217: {
1.240 schwarze 218: struct roff_node *n;
1.142 kristaps 219: char *c, *ws, *end;
220:
1.203 schwarze 221: assert(mdoc->last);
222: n = mdoc->last;
1.142 kristaps 223:
224: /*
1.144 kristaps 225: * Divert directly to list processing if we're encountering a
1.239 schwarze 226: * columnar ROFFT_BLOCK with or without a prior ROFFT_BLOCK entry
227: * (a ROFFT_BODY means it's already open, in which case we should
1.144 kristaps 228: * process within its context in the normal way).
1.142 kristaps 229: */
230:
1.239 schwarze 231: if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238 schwarze 232: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.144 kristaps 233: /* `Bl' is open without any children. */
1.203 schwarze 234: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 235: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
1.253 schwarze 236: return 1;
1.142 kristaps 237: }
238:
1.239 schwarze 239: if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213 schwarze 240: NULL != n->parent &&
241: MDOC_Bl == n->parent->tok &&
242: LIST_column == n->parent->norm->Bl.type) {
1.144 kristaps 243: /* `Bl' has block-level `It' children. */
1.203 schwarze 244: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 245: mdoc_macro(mdoc, MDOC_It, line, offs, &offs, buf);
1.253 schwarze 246: return 1;
1.142 kristaps 247: }
1.124 kristaps 248:
1.137 schwarze 249: /*
250: * Search for the beginning of unescaped trailing whitespace (ws)
251: * and for the first character not to be output (end).
252: */
1.139 kristaps 253:
254: /* FIXME: replace with strcspn(). */
1.137 schwarze 255: ws = NULL;
256: for (c = end = buf + offs; *c; c++) {
257: switch (*c) {
258: case ' ':
259: if (NULL == ws)
260: ws = c;
261: continue;
262: case '\t':
263: /*
264: * Always warn about trailing tabs,
265: * even outside literal context,
266: * where they should be put on the next line.
267: */
268: if (NULL == ws)
269: ws = c;
270: /*
271: * Strip trailing tabs in literal context only;
272: * outside, they affect the next line.
273: */
1.203 schwarze 274: if (MDOC_LITERAL & mdoc->flags)
1.137 schwarze 275: continue;
276: break;
277: case '\\':
278: /* Skip the escaped character, too, if any. */
279: if (c[1])
280: c++;
281: /* FALLTHROUGH */
282: default:
283: ws = NULL;
284: break;
285: }
286: end = c + 1;
287: }
288: *end = '\0';
1.91 kristaps 289:
1.137 schwarze 290: if (ws)
1.218 schwarze 291: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
292: line, (int)(ws-buf), NULL);
1.115 kristaps 293:
1.231 schwarze 294: if (buf[offs] == '\0' && ! (mdoc->flags & MDOC_LITERAL)) {
1.218 schwarze 295: mandoc_msg(MANDOCERR_FI_BLANK, mdoc->parse,
296: line, (int)(c - buf), NULL);
1.124 kristaps 297:
1.119 kristaps 298: /*
1.165 schwarze 299: * Insert a `sp' in the case of a blank line. Technically,
1.124 kristaps 300: * blank lines aren't allowed, but enough manuals assume this
301: * behaviour that we want to work around it.
1.119 kristaps 302: */
1.250 schwarze 303: roff_elem_alloc(mdoc, line, offs, MDOC_sp);
1.255 ! schwarze 304: mdoc->last->flags |= MDOC_VALID | MDOC_ENDED;
1.242 schwarze 305: mdoc->next = ROFF_NEXT_SIBLING;
1.253 schwarze 306: return 1;
1.119 kristaps 307: }
1.68 kristaps 308:
1.249 schwarze 309: roff_word_alloc(mdoc, line, offs, buf+offs);
1.91 kristaps 310:
1.231 schwarze 311: if (mdoc->flags & MDOC_LITERAL)
1.253 schwarze 312: return 1;
1.128 kristaps 313:
314: /*
315: * End-of-sentence check. If the last character is an unescaped
316: * EOS character, then flag the node as being the end of a
317: * sentence. The front-end will know how to interpret this.
318: */
1.132 kristaps 319:
1.137 schwarze 320: assert(buf < end);
321:
1.207 schwarze 322: if (mandoc_eos(buf+offs, (size_t)(end-buf-offs)))
1.203 schwarze 323: mdoc->last->flags |= MDOC_EOS;
1.253 schwarze 324: return 1;
1.1 kristaps 325: }
326:
1.53 kristaps 327: /*
328: * Parse a macro line, that is, a line beginning with the control
329: * character.
330: */
1.155 kristaps 331: static int
1.242 schwarze 332: mdoc_pmacro(struct roff_man *mdoc, int ln, char *buf, int offs)
1.1 kristaps 333: {
1.240 schwarze 334: struct roff_node *n;
1.229 schwarze 335: const char *cp;
1.240 schwarze 336: int tok;
1.188 kristaps 337: int i, sv;
1.144 kristaps 338: char mac[5];
1.63 kristaps 339:
1.188 kristaps 340: sv = offs;
1.130 kristaps 341:
1.213 schwarze 342: /*
1.162 schwarze 343: * Copy the first word into a nil-terminated buffer.
1.229 schwarze 344: * Stop when a space, tab, escape, or eoln is encountered.
1.160 kristaps 345: */
1.1 kristaps 346:
1.188 kristaps 347: i = 0;
1.229 schwarze 348: while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.188 kristaps 349: mac[i++] = buf[offs++];
350:
351: mac[i] = '\0';
352:
1.248 schwarze 353: tok = (i > 1 && i < 4) ? mdoc_hash_find(mac) : TOKEN_NONE;
1.1 kristaps 354:
1.248 schwarze 355: if (tok == TOKEN_NONE) {
1.222 schwarze 356: mandoc_msg(MANDOCERR_MACRO, mdoc->parse,
357: ln, sv, buf + sv - 1);
1.253 schwarze 358: return 1;
1.53 kristaps 359: }
1.1 kristaps 360:
1.229 schwarze 361: /* Skip a leading escape sequence or tab. */
1.160 kristaps 362:
1.229 schwarze 363: switch (buf[offs]) {
364: case '\\':
365: cp = buf + offs + 1;
366: mandoc_escape(&cp, NULL, NULL);
367: offs = cp - buf;
368: break;
369: case '\t':
1.188 kristaps 370: offs++;
1.229 schwarze 371: break;
372: default:
373: break;
374: }
1.160 kristaps 375:
376: /* Jump to the next non-whitespace word. */
1.1 kristaps 377:
1.188 kristaps 378: while (buf[offs] && ' ' == buf[offs])
379: offs++;
1.1 kristaps 380:
1.213 schwarze 381: /*
1.125 kristaps 382: * Trailing whitespace. Note that tabs are allowed to be passed
383: * into the parser as "text", so we only warn about spaces here.
384: */
1.115 kristaps 385:
1.188 kristaps 386: if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.218 schwarze 387: mandoc_msg(MANDOCERR_SPACE_EOL, mdoc->parse,
388: ln, offs - 1, NULL);
1.115 kristaps 389:
1.144 kristaps 390: /*
391: * If an initial macro or a list invocation, divert directly
392: * into macro processing.
393: */
394:
1.232 schwarze 395: if (NULL == mdoc->last || MDOC_It == tok || MDOC_El == tok) {
396: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.253 schwarze 397: return 1;
1.232 schwarze 398: }
1.144 kristaps 399:
1.203 schwarze 400: n = mdoc->last;
401: assert(mdoc->last);
1.144 kristaps 402:
403: /*
404: * If the first macro of a `Bl -column', open an `It' block
405: * context around the parsed macro.
406: */
407:
1.239 schwarze 408: if (n->tok == MDOC_Bl && n->type == ROFFT_BODY &&
1.238 schwarze 409: n->end == ENDBODY_NOT && n->norm->Bl.type == LIST_column) {
1.203 schwarze 410: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 411: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
1.253 schwarze 412: return 1;
1.144 kristaps 413: }
414:
415: /*
416: * If we're following a block-level `It' within a `Bl -column'
417: * context (perhaps opened in the above block or in ptext()),
418: * then open an `It' block context around the parsed macro.
1.98 kristaps 419: */
1.144 kristaps 420:
1.239 schwarze 421: if (n->tok == MDOC_It && n->type == ROFFT_BLOCK &&
1.213 schwarze 422: NULL != n->parent &&
423: MDOC_Bl == n->parent->tok &&
424: LIST_column == n->parent->norm->Bl.type) {
1.203 schwarze 425: mdoc->flags |= MDOC_FREECOL;
1.232 schwarze 426: mdoc_macro(mdoc, MDOC_It, ln, sv, &sv, buf);
1.253 schwarze 427: return 1;
1.144 kristaps 428: }
429:
430: /* Normal processing of a macro. */
431:
1.232 schwarze 432: mdoc_macro(mdoc, tok, ln, sv, &offs, buf);
1.208 schwarze 433:
434: /* In quick mode (for mandocdb), abort after the NAME section. */
435:
436: if (mdoc->quick && MDOC_Sh == tok &&
437: SEC_NAME != mdoc->last->sec)
1.253 schwarze 438: return 2;
1.1 kristaps 439:
1.253 schwarze 440: return 1;
1.1 kristaps 441: }
1.100 kristaps 442:
1.186 kristaps 443: enum mdelim
444: mdoc_isdelim(const char *p)
445: {
446:
447: if ('\0' == p[0])
1.253 schwarze 448: return DELIM_NONE;
1.186 kristaps 449:
450: if ('\0' == p[1])
451: switch (p[0]) {
1.213 schwarze 452: case '(':
453: case '[':
1.253 schwarze 454: return DELIM_OPEN;
1.213 schwarze 455: case '|':
1.253 schwarze 456: return DELIM_MIDDLE;
1.213 schwarze 457: case '.':
458: case ',':
459: case ';':
460: case ':':
461: case '?':
462: case '!':
463: case ')':
464: case ']':
1.253 schwarze 465: return DELIM_CLOSE;
1.186 kristaps 466: default:
1.253 schwarze 467: return DELIM_NONE;
1.186 kristaps 468: }
469:
470: if ('\\' != p[0])
1.253 schwarze 471: return DELIM_NONE;
1.100 kristaps 472:
1.186 kristaps 473: if (0 == strcmp(p + 1, "."))
1.253 schwarze 474: return DELIM_CLOSE;
1.200 schwarze 475: if (0 == strcmp(p + 1, "fR|\\fP"))
1.253 schwarze 476: return DELIM_MIDDLE;
1.186 kristaps 477:
1.253 schwarze 478: return DELIM_NONE;
1.255 ! schwarze 479: }
! 480:
! 481: void
! 482: mdoc_validate(struct roff_man *mdoc)
! 483: {
! 484:
! 485: mdoc->last = mdoc->first;
! 486: mdoc_node_validate(mdoc);
! 487: mdoc_state_reset(mdoc);
1.186 kristaps 488: }
CVSweb