Annotation of src/usr.bin/mandoc/read.c, Revision 1.138
1.138 ! schwarze 1: /* $OpenBSD: read.c,v 1.137 2017/05/16 19:05:36 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.129 schwarze 4: * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
1.20 schwarze 5: * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
1.1 schwarze 6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
1.108 schwarze 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1 schwarze 12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.108 schwarze 13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1 schwarze 14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: */
1.58 schwarze 19: #include <sys/types.h>
20: #include <sys/mman.h>
1.1 schwarze 21: #include <sys/stat.h>
22:
23: #include <assert.h>
24: #include <ctype.h>
1.118 schwarze 25: #include <err.h>
1.18 schwarze 26: #include <errno.h>
1.1 schwarze 27: #include <fcntl.h>
28: #include <stdarg.h>
29: #include <stdio.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
1.115 schwarze 33: #include <zlib.h>
1.1 schwarze 34:
1.108 schwarze 35: #include "mandoc_aux.h"
1.1 schwarze 36: #include "mandoc.h"
1.108 schwarze 37: #include "roff.h"
1.1 schwarze 38: #include "mdoc.h"
39: #include "man.h"
1.108 schwarze 40: #include "libmandoc.h"
1.114 schwarze 41: #include "roff_int.h"
1.1 schwarze 42:
43: #define REPARSE_LIMIT 1000
44:
45: struct mparse {
1.133 schwarze 46: struct roff *roff; /* roff parser (!NULL) */
1.109 schwarze 47: struct roff_man *man; /* man parser */
1.23 schwarze 48: char *sodest; /* filename pointed to by .so */
1.59 schwarze 49: const char *file; /* filename of current input file */
50: struct buf *primary; /* buffer currently being parsed */
51: struct buf *secondary; /* preprocessed copy of input */
52: const char *defos; /* default operating system */
53: mandocmsg mmsg; /* warning/error message handler */
54: enum mandoclevel file_status; /* status of current parse */
55: enum mandoclevel wlevel; /* ignore messages below this */
56: int options; /* parser options */
1.115 schwarze 57: int gzip; /* current input file is gzipped */
1.70 schwarze 58: int filenc; /* encoding of the current file */
1.1 schwarze 59: int reparse_count; /* finite interp. stack */
1.59 schwarze 60: int line; /* line number in the file */
1.1 schwarze 61: };
62:
1.60 schwarze 63: static void choose_parser(struct mparse *);
1.1 schwarze 64: static void resize_buf(struct buf *, size_t);
1.135 schwarze 65: static int mparse_buf_r(struct mparse *, struct buf, size_t, int);
1.18 schwarze 66: static int read_whole_file(struct mparse *, const char *, int,
67: struct buf *, int *);
1.1 schwarze 68: static void mparse_end(struct mparse *);
1.15 schwarze 69: static void mparse_parse_buffer(struct mparse *, struct buf,
70: const char *);
1.1 schwarze 71:
72: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
73: MANDOCERR_OK,
1.137 schwarze 74: MANDOCERR_STYLE,
1.1 schwarze 75: MANDOCERR_WARNING,
76: MANDOCERR_ERROR,
1.87 schwarze 77: MANDOCERR_UNSUPP,
1.1 schwarze 78: MANDOCERR_MAX,
79: MANDOCERR_MAX
80: };
81:
82: static const char * const mandocerrs[MANDOCERR_MAX] = {
83: "ok",
84:
1.137 schwarze 85: "generic style suggestion",
1.138 ! schwarze 86:
! 87: "useless macro",
! 88:
1.1 schwarze 89: "generic warning",
90:
91: /* related to the prologue */
1.57 schwarze 92: "missing manual title, using UNTITLED",
93: "missing manual title, using \"\"",
1.32 schwarze 94: "lower case character in document title",
1.57 schwarze 95: "missing manual section, using \"\"",
1.1 schwarze 96: "unknown manual section",
1.32 schwarze 97: "missing date, using today's date",
1.1 schwarze 98: "cannot parse date, using it verbatim",
1.57 schwarze 99: "missing Os macro, using \"\"",
100: "duplicate prologue macro",
101: "late prologue macro",
102: "skipping late title macro",
1.1 schwarze 103: "prologue macros out of order",
104:
105: /* related to document structure */
106: ".so is fragile, better use ln(1)",
1.28 schwarze 107: "no document body",
1.32 schwarze 108: "content before first section header",
109: "first section is not \"NAME\"",
1.129 schwarze 110: "NAME section without Nm before Nd",
1.103 schwarze 111: "NAME section without description",
112: "description not at the end of NAME",
113: "bad NAME section content",
1.129 schwarze 114: "missing comma before name",
1.96 schwarze 115: "missing description line, using \"\"",
1.134 schwarze 116: "description line outside NAME section",
1.1 schwarze 117: "sections out of conventional order",
1.32 schwarze 118: "duplicate section title",
119: "unexpected section",
1.63 schwarze 120: "unusual Xr order",
121: "unusual Xr punctuation",
1.62 schwarze 122: "AUTHORS section without An macro",
1.1 schwarze 123:
124: /* related to macros and nesting */
1.33 schwarze 125: "obsolete macro",
1.78 schwarze 126: "macro neither callable nor escaped",
1.1 schwarze 127: "skipping paragraph macro",
1.10 schwarze 128: "moving paragraph macro out of list",
1.1 schwarze 129: "skipping no-space macro",
130: "blocks badly nested",
131: "nested displays are not portable",
1.35 schwarze 132: "moving content out of list",
1.56 schwarze 133: "fill mode already enabled, skipping",
134: "fill mode already disabled, skipping",
1.1 schwarze 135: "line scope broken",
136:
137: /* related to missing macro arguments */
1.36 schwarze 138: "skipping empty request",
139: "conditional request controls empty scope",
1.1 schwarze 140: "skipping empty macro",
1.99 schwarze 141: "empty block",
1.40 schwarze 142: "empty argument, using 0n",
1.38 schwarze 143: "missing display type, using -ragged",
144: "list type is not the first argument",
1.125 schwarze 145: "missing -width in -tag list, using 6n",
1.56 schwarze 146: "missing utility name, using \"\"",
1.98 schwarze 147: "missing function name, using \"\"",
1.38 schwarze 148: "empty head in list item",
149: "empty list item",
1.39 schwarze 150: "missing font type, using \\fR",
151: "unknown font type, using \\fR",
1.79 schwarze 152: "nothing follows prefix",
1.97 schwarze 153: "empty reference block",
1.128 schwarze 154: "missing section argument",
1.38 schwarze 155: "missing -std argument, adding it",
1.100 schwarze 156: "missing option string, using \"\"",
157: "missing resource identifier, using \"\"",
1.66 schwarze 158: "missing eqn box, using \"\"",
1.1 schwarze 159:
160: /* related to bad macro arguments */
1.42 schwarze 161: "unterminated quoted argument",
1.1 schwarze 162: "duplicate argument",
1.54 schwarze 163: "skipping duplicate argument",
1.41 schwarze 164: "skipping duplicate display type",
165: "skipping duplicate list type",
1.54 schwarze 166: "skipping -width argument",
1.101 schwarze 167: "wrong number of cells",
1.1 schwarze 168: "unknown AT&T UNIX version",
1.64 schwarze 169: "comma in function argument",
1.65 schwarze 170: "parenthesis in function name",
1.45 schwarze 171: "invalid content in Rs block",
1.41 schwarze 172: "invalid Boolean argument",
173: "unknown font, skipping request",
1.101 schwarze 174: "odd number of characters in request",
1.1 schwarze 175:
176: /* related to plain text */
1.42 schwarze 177: "blank line in fill mode, using .sp",
178: "tab in filled text",
179: "whitespace at end of input line",
1.131 schwarze 180: "new sentence, new line",
1.1 schwarze 181: "bad comment style",
1.42 schwarze 182: "invalid escape sequence",
183: "undefined string, using \"\"",
1.3 schwarze 184:
1.93 schwarze 185: /* related to tables */
186: "tbl line starts with span",
187: "tbl column starts with span",
188: "skipping vertical bar in tbl layout",
189:
1.1 schwarze 190: "generic error",
1.3 schwarze 191:
1.1 schwarze 192: /* related to tables */
1.91 schwarze 193: "non-alphabetic character in tbl options",
194: "skipping unknown tbl option",
195: "missing tbl option argument",
196: "wrong tbl option argument size",
1.93 schwarze 197: "empty tbl layout",
198: "invalid character in tbl layout",
199: "unmatched parenthesis in tbl layout",
1.94 schwarze 200: "tbl without any data cells",
201: "ignoring data in spanned tbl cell",
202: "ignoring extra tbl data cells",
203: "data block open at end of tbl",
1.1 schwarze 204:
1.46 schwarze 205: /* related to document structure and macros */
1.83 schwarze 206: NULL,
1.1 schwarze 207: "input stack limit exceeded, infinite loop?",
208: "skipping bad character",
1.46 schwarze 209: "skipping unknown macro",
1.87 schwarze 210: "skipping insecure request",
1.51 schwarze 211: "skipping item outside list",
1.46 schwarze 212: "skipping column outside column list",
213: "skipping end of block that is not open",
1.90 schwarze 214: "fewer RS blocks open, skipping",
1.46 schwarze 215: "inserting missing end of block",
216: "appending missing end of block",
217:
218: /* related to request and macro arguments */
1.1 schwarze 219: "escaped character not allowed in a name",
1.75 schwarze 220: "NOT IMPLEMENTED: Bd -file",
1.120 schwarze 221: "skipping display without arguments",
1.49 schwarze 222: "missing list type, using -item",
1.48 schwarze 223: "missing manual name, using \"\"",
1.49 schwarze 224: "uname(3) system call failed, using UNKNOWN",
1.41 schwarze 225: "unknown standard specifier",
1.49 schwarze 226: "skipping request without numeric argument",
1.84 schwarze 227: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.85 schwarze 228: ".so request failed",
1.39 schwarze 229: "skipping all arguments",
230: "skipping excess arguments",
1.68 schwarze 231: "divide by zero",
1.87 schwarze 232:
233: "unsupported feature",
234: "input too large",
1.89 schwarze 235: "unsupported control character",
1.87 schwarze 236: "unsupported roff request",
1.94 schwarze 237: "eqn delim option in tbl",
1.93 schwarze 238: "unsupported tbl layout modifier",
1.87 schwarze 239: "ignoring macro in table",
1.1 schwarze 240: };
241:
242: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
243: "SUCCESS",
1.137 schwarze 244: "STYLE",
1.1 schwarze 245: "WARNING",
246: "ERROR",
1.87 schwarze 247: "UNSUPP",
1.1 schwarze 248: "BADARG",
249: "SYSERR"
250: };
251:
1.25 schwarze 252:
1.1 schwarze 253: static void
254: resize_buf(struct buf *buf, size_t initial)
255: {
256:
257: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
258: buf->buf = mandoc_realloc(buf->buf, buf->sz);
259: }
260:
261: static void
1.60 schwarze 262: choose_parser(struct mparse *curp)
1.1 schwarze 263: {
1.59 schwarze 264: char *cp, *ep;
265: int format;
1.1 schwarze 266:
1.59 schwarze 267: /*
268: * If neither command line arguments -mdoc or -man select
269: * a parser nor the roff parser found a .Dd or .TH macro
270: * yet, look ahead in the main input buffer.
271: */
272:
273: if ((format = roff_getformat(curp->roff)) == 0) {
274: cp = curp->primary->buf;
275: ep = cp + curp->primary->sz;
276: while (cp < ep) {
1.61 schwarze 277: if (*cp == '.' || *cp == '\'') {
1.59 schwarze 278: cp++;
279: if (cp[0] == 'D' && cp[1] == 'd') {
280: format = MPARSE_MDOC;
281: break;
282: }
283: if (cp[0] == 'T' && cp[1] == 'H') {
284: format = MPARSE_MAN;
285: break;
286: }
287: }
288: cp = memchr(cp, '\n', ep - cp);
289: if (cp == NULL)
290: break;
291: cp++;
292: }
1.1 schwarze 293: }
294:
1.59 schwarze 295: if (format == MPARSE_MDOC) {
1.112 schwarze 296: curp->man->macroset = MACROSET_MDOC;
1.136 schwarze 297: if (curp->man->mdocmac == NULL)
298: curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.112 schwarze 299: } else {
300: curp->man->macroset = MACROSET_MAN;
1.136 schwarze 301: if (curp->man->manmac == NULL)
302: curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.25 schwarze 303: }
1.136 schwarze 304: curp->man->first->tok = TOKEN_NONE;
1.1 schwarze 305: }
306:
307: /*
1.71 schwarze 308: * Main parse routine for a buffer.
309: * It assumes encoding and line numbering are already set up.
310: * It can recurse directly (for invocations of user-defined
311: * macros, inline equations, and input line traps)
312: * and indirectly (for .so file inclusion).
1.1 schwarze 313: */
1.135 schwarze 314: static int
1.71 schwarze 315: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1 schwarze 316: {
317: const struct tbl_span *span;
318: struct buf ln;
1.88 schwarze 319: const char *save_file;
1.85 schwarze 320: char *cp;
1.71 schwarze 321: size_t pos; /* byte number in the ln buffer */
1.127 schwarze 322: size_t j; /* auxiliary byte number in the blk buffer */
1.1 schwarze 323: enum rofferr rr;
1.76 schwarze 324: int of;
1.1 schwarze 325: int lnn; /* line number in the real file */
1.85 schwarze 326: int fd;
1.1 schwarze 327: unsigned char c;
328:
1.71 schwarze 329: memset(&ln, 0, sizeof(ln));
1.1 schwarze 330:
1.25 schwarze 331: lnn = curp->line;
332: pos = 0;
1.1 schwarze 333:
1.71 schwarze 334: while (i < blk.sz) {
1.1 schwarze 335: if (0 == pos && '\0' == blk.buf[i])
336: break;
337:
338: if (start) {
339: curp->line = lnn;
340: curp->reparse_count = 0;
1.70 schwarze 341:
342: if (lnn < 3 &&
343: curp->filenc & MPARSE_UTF8 &&
1.71 schwarze 344: curp->filenc & MPARSE_LATIN1)
345: curp->filenc = preconv_cue(&blk, i);
1.1 schwarze 346: }
347:
1.71 schwarze 348: while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1 schwarze 349:
350: /*
351: * When finding an unescaped newline character,
352: * leave the character loop to process the line.
353: * Skip a preceding carriage return, if any.
354: */
355:
1.71 schwarze 356: if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1 schwarze 357: '\n' == blk.buf[i + 1])
358: ++i;
359: if ('\n' == blk.buf[i]) {
360: ++i;
361: ++lnn;
362: break;
363: }
364:
1.13 schwarze 365: /*
1.70 schwarze 366: * Make sure we have space for the worst
367: * case of 11 bytes: "\\[u10ffff]\0"
1.13 schwarze 368: */
369:
1.71 schwarze 370: if (pos + 11 > ln.sz)
1.13 schwarze 371: resize_buf(&ln, 256);
372:
1.25 schwarze 373: /*
1.70 schwarze 374: * Encode 8-bit input.
1.1 schwarze 375: */
376:
1.70 schwarze 377: c = blk.buf[i];
378: if (c & 0x80) {
1.71 schwarze 379: if ( ! (curp->filenc && preconv_encode(
380: &blk, &i, &ln, &pos, &curp->filenc))) {
1.89 schwarze 381: mandoc_vmsg(MANDOCERR_CHAR_BAD, curp,
382: curp->line, pos, "0x%x", c);
1.70 schwarze 383: ln.buf[pos++] = '?';
384: i++;
385: }
386: continue;
387: }
388:
389: /*
390: * Exclude control characters.
391: */
1.1 schwarze 392:
1.70 schwarze 393: if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.89 schwarze 394: mandoc_vmsg(c == 0x00 || c == 0x04 ||
395: c > 0x0a ? MANDOCERR_CHAR_BAD :
396: MANDOCERR_CHAR_UNSUPP,
397: curp, curp->line, pos, "0x%x", c);
1.1 schwarze 398: i++;
1.102 schwarze 399: if (c != '\r')
400: ln.buf[pos++] = '?';
1.1 schwarze 401: continue;
402: }
403:
404: /* Trailing backslash = a plain char. */
405:
1.71 schwarze 406: if (blk.buf[i] != '\\' || i + 1 == blk.sz) {
1.1 schwarze 407: ln.buf[pos++] = blk.buf[i++];
408: continue;
409: }
410:
411: /*
412: * Found escape and at least one other character.
413: * When it's a newline character, skip it.
414: * When there is a carriage return in between,
415: * skip that one as well.
416: */
417:
1.71 schwarze 418: if ('\r' == blk.buf[i + 1] && i + 2 < blk.sz &&
1.1 schwarze 419: '\n' == blk.buf[i + 2])
420: ++i;
421: if ('\n' == blk.buf[i + 1]) {
422: i += 2;
423: ++lnn;
424: continue;
425: }
426:
427: if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
1.127 schwarze 428: j = i;
1.1 schwarze 429: i += 2;
430: /* Comment, skip to end of line */
1.71 schwarze 431: for (; i < blk.sz; ++i) {
1.126 schwarze 432: if (blk.buf[i] != '\n')
433: continue;
434: if (blk.buf[i - 1] == ' ' ||
435: blk.buf[i - 1] == '\t')
436: mandoc_msg(
437: MANDOCERR_SPACE_EOL,
438: curp, curp->line,
1.127 schwarze 439: pos + i-1 - j, NULL);
1.126 schwarze 440: ++i;
441: ++lnn;
442: break;
1.1 schwarze 443: }
444:
445: /* Backout trailing whitespaces */
446: for (; pos > 0; --pos) {
447: if (ln.buf[pos - 1] != ' ')
448: break;
449: if (pos > 2 && ln.buf[pos - 2] == '\\')
450: break;
451: }
452: break;
453: }
454:
1.13 schwarze 455: /* Catch escaped bogus characters. */
456:
457: c = (unsigned char) blk.buf[i+1];
458:
1.25 schwarze 459: if ( ! (isascii(c) &&
460: (isgraph(c) || isblank(c)))) {
1.89 schwarze 461: mandoc_vmsg(MANDOCERR_CHAR_BAD, curp,
1.56 schwarze 462: curp->line, pos, "0x%x", c);
1.13 schwarze 463: i += 2;
464: ln.buf[pos++] = '?';
465: continue;
466: }
467:
1.1 schwarze 468: /* Some other escape sequence, copy & cont. */
469:
470: ln.buf[pos++] = blk.buf[i++];
471: ln.buf[pos++] = blk.buf[i++];
472: }
473:
1.71 schwarze 474: if (pos >= ln.sz)
1.1 schwarze 475: resize_buf(&ln, 256);
476:
477: ln.buf[pos] = '\0';
478:
479: /*
480: * A significant amount of complexity is contained by
481: * the roff preprocessor. It's line-oriented but can be
482: * expressed on one line, so we need at times to
483: * readjust our starting point and re-run it. The roff
484: * preprocessor can also readjust the buffers with new
485: * data, so we pass them in wholesale.
486: */
487:
488: of = 0;
489:
1.4 schwarze 490: /*
491: * Maintain a lookaside buffer of all parsed lines. We
492: * only do this if mparse_keep() has been invoked (the
493: * buffer may be accessed with mparse_getkeep()).
494: */
495:
496: if (curp->secondary) {
1.25 schwarze 497: curp->secondary->buf = mandoc_realloc(
498: curp->secondary->buf,
499: curp->secondary->sz + pos + 2);
500: memcpy(curp->secondary->buf +
501: curp->secondary->sz,
502: ln.buf, pos);
1.4 schwarze 503: curp->secondary->sz += pos;
504: curp->secondary->buf
505: [curp->secondary->sz] = '\n';
506: curp->secondary->sz++;
507: curp->secondary->buf
508: [curp->secondary->sz] = '\0';
509: }
1.1 schwarze 510: rerun:
1.72 schwarze 511: rr = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1 schwarze 512:
513: switch (rr) {
1.25 schwarze 514: case ROFF_REPARSE:
1.135 schwarze 515: if (++curp->reparse_count > REPARSE_LIMIT)
1.1 schwarze 516: mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.25 schwarze 517: curp->line, pos, NULL);
1.135 schwarze 518: else if (mparse_buf_r(curp, ln, of, 0) == 1 ||
519: start == 1) {
520: pos = 0;
521: continue;
522: }
523: free(ln.buf);
524: return 0;
1.25 schwarze 525: case ROFF_APPEND:
1.71 schwarze 526: pos = strlen(ln.buf);
1.1 schwarze 527: continue;
1.25 schwarze 528: case ROFF_RERUN:
1.1 schwarze 529: goto rerun;
1.25 schwarze 530: case ROFF_IGN:
1.1 schwarze 531: pos = 0;
532: continue;
1.25 schwarze 533: case ROFF_SO:
1.71 schwarze 534: if ( ! (curp->options & MPARSE_SO) &&
535: (i >= blk.sz || blk.buf[i] == '\0')) {
1.23 schwarze 536: curp->sodest = mandoc_strdup(ln.buf + of);
537: free(ln.buf);
1.135 schwarze 538: return 1;
1.23 schwarze 539: }
1.4 schwarze 540: /*
541: * We remove `so' clauses from our lookaside
542: * buffer because we're going to descend into
543: * the file recursively.
544: */
1.25 schwarze 545: if (curp->secondary)
1.4 schwarze 546: curp->secondary->sz -= pos + 1;
1.88 schwarze 547: save_file = curp->file;
1.122 schwarze 548: if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
1.85 schwarze 549: mparse_readfd(curp, fd, ln.buf + of);
1.121 schwarze 550: close(fd);
1.88 schwarze 551: curp->file = save_file;
552: } else {
553: curp->file = save_file;
1.30 schwarze 554: mandoc_vmsg(MANDOCERR_SO_FAIL,
555: curp, curp->line, pos,
556: ".so %s", ln.buf + of);
1.85 schwarze 557: ln.sz = mandoc_asprintf(&cp,
558: ".sp\nSee the file %s.\n.sp",
559: ln.buf + of);
560: free(ln.buf);
561: ln.buf = cp;
562: of = 0;
563: mparse_buf_r(curp, ln, of, 0);
1.30 schwarze 564: }
1.1 schwarze 565: pos = 0;
566: continue;
567: default:
568: break;
569: }
570:
1.124 schwarze 571: if (curp->man->macroset == MACROSET_NONE)
1.60 schwarze 572: choose_parser(curp);
1.1 schwarze 573:
1.25 schwarze 574: /*
1.60 schwarze 575: * Lastly, push down into the parsers themselves.
1.1 schwarze 576: * If libroff returns ROFF_TBL, then add it to the
577: * currently open parse. Since we only get here if
578: * there does exist data (see tbl_data.c), we're
579: * guaranteed that something's been allocated.
580: * Do the same for ROFF_EQN.
581: */
582:
1.114 schwarze 583: if (rr == ROFF_TBL)
1.76 schwarze 584: while ((span = roff_span(curp->roff)) != NULL)
1.114 schwarze 585: roff_addtbl(curp->man, span);
586: else if (rr == ROFF_EQN)
587: roff_addeqn(curp->man, roff_eqn(curp->roff));
588: else if ((curp->man->macroset == MACROSET_MDOC ?
1.110 schwarze 589: mdoc_parseln(curp->man, curp->line, ln.buf, of) :
1.76 schwarze 590: man_parseln(curp->man, curp->line, ln.buf, of)) == 2)
591: break;
1.1 schwarze 592:
593: /* Temporary buffers typically are not full. */
594:
595: if (0 == start && '\0' == blk.buf[i])
596: break;
597:
598: /* Start the next input line. */
599:
600: pos = 0;
601: }
602:
603: free(ln.buf);
1.135 schwarze 604: return 1;
1.1 schwarze 605: }
606:
607: static int
1.18 schwarze 608: read_whole_file(struct mparse *curp, const char *file, int fd,
609: struct buf *fb, int *with_mmap)
1.1 schwarze 610: {
611: struct stat st;
1.115 schwarze 612: gzFile gz;
1.1 schwarze 613: size_t off;
614: ssize_t ssz;
615:
1.118 schwarze 616: if (fstat(fd, &st) == -1)
617: err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1 schwarze 618:
619: /*
620: * If we're a regular file, try just reading in the whole entry
621: * via mmap(). This is faster than reading it into blocks, and
622: * since each file is only a few bytes to begin with, I'm not
623: * concerned that this is going to tank any machines.
624: */
625:
1.115 schwarze 626: if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
1.106 schwarze 627: if (st.st_size > 0x7fffffff) {
1.86 schwarze 628: mandoc_msg(MANDOCERR_TOOLARGE, curp, 0, 0, NULL);
1.117 schwarze 629: return 0;
1.1 schwarze 630: }
631: *with_mmap = 1;
632: fb->sz = (size_t)st.st_size;
1.15 schwarze 633: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 schwarze 634: if (fb->buf != MAP_FAILED)
1.117 schwarze 635: return 1;
1.1 schwarze 636: }
637:
1.115 schwarze 638: if (curp->gzip) {
1.118 schwarze 639: if ((gz = gzdopen(fd, "rb")) == NULL)
640: err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.115 schwarze 641: } else
642: gz = NULL;
643:
1.1 schwarze 644: /*
645: * If this isn't a regular file (like, say, stdin), then we must
646: * go the old way and just read things in bit by bit.
647: */
648:
649: *with_mmap = 0;
650: off = 0;
651: fb->sz = 0;
652: fb->buf = NULL;
653: for (;;) {
654: if (off == fb->sz) {
655: if (fb->sz == (1U << 31)) {
1.86 schwarze 656: mandoc_msg(MANDOCERR_TOOLARGE, curp,
657: 0, 0, NULL);
1.1 schwarze 658: break;
659: }
660: resize_buf(fb, 65536);
661: }
1.115 schwarze 662: ssz = curp->gzip ?
663: gzread(gz, fb->buf + (int)off, fb->sz - off) :
664: read(fd, fb->buf + (int)off, fb->sz - off);
1.1 schwarze 665: if (ssz == 0) {
666: fb->sz = off;
1.117 schwarze 667: return 1;
1.1 schwarze 668: }
1.118 schwarze 669: if (ssz == -1)
670: err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1 schwarze 671: off += (size_t)ssz;
672: }
673:
674: free(fb->buf);
675: fb->buf = NULL;
1.117 schwarze 676: return 0;
1.1 schwarze 677: }
678:
679: static void
680: mparse_end(struct mparse *curp)
681: {
1.110 schwarze 682: if (curp->man->macroset == MACROSET_NONE)
683: curp->man->macroset = MACROSET_MAN;
684: if (curp->man->macroset == MACROSET_MDOC)
685: mdoc_endparse(curp->man);
686: else
1.86 schwarze 687: man_endparse(curp->man);
1.1 schwarze 688: roff_endparse(curp->roff);
689: }
690:
1.15 schwarze 691: static void
692: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.1 schwarze 693: {
1.61 schwarze 694: struct buf *svprimary;
1.1 schwarze 695: const char *svfile;
1.71 schwarze 696: size_t offset;
1.14 schwarze 697: static int recursion_depth;
698:
699: if (64 < recursion_depth) {
700: mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
1.15 schwarze 701: return;
1.14 schwarze 702: }
1.1 schwarze 703:
1.15 schwarze 704: /* Line number is per-file. */
705: svfile = curp->file;
706: curp->file = file;
1.61 schwarze 707: svprimary = curp->primary;
1.59 schwarze 708: curp->primary = &blk;
1.15 schwarze 709: curp->line = 1;
710: recursion_depth++;
711:
1.70 schwarze 712: /* Skip an UTF-8 byte order mark. */
713: if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
714: (unsigned char)blk.buf[0] == 0xef &&
715: (unsigned char)blk.buf[1] == 0xbb &&
716: (unsigned char)blk.buf[2] == 0xbf) {
1.71 schwarze 717: offset = 3;
1.70 schwarze 718: curp->filenc &= ~MPARSE_LATIN1;
1.71 schwarze 719: } else
720: offset = 0;
1.70 schwarze 721:
1.71 schwarze 722: mparse_buf_r(curp, blk, offset, 1);
1.15 schwarze 723:
1.86 schwarze 724: if (--recursion_depth == 0)
1.15 schwarze 725: mparse_end(curp);
726:
1.61 schwarze 727: curp->primary = svprimary;
1.15 schwarze 728: curp->file = svfile;
729: }
730:
1.74 schwarze 731: /*
732: * Read the whole file into memory and call the parsers.
733: * Called recursively when an .so request is encountered.
734: */
1.15 schwarze 735: enum mandoclevel
736: mparse_readfd(struct mparse *curp, int fd, const char *file)
737: {
738: struct buf blk;
739: int with_mmap;
1.70 schwarze 740: int save_filenc;
1.1 schwarze 741:
1.67 schwarze 742: if (read_whole_file(curp, file, fd, &blk, &with_mmap)) {
1.70 schwarze 743: save_filenc = curp->filenc;
744: curp->filenc = curp->options &
745: (MPARSE_UTF8 | MPARSE_LATIN1);
1.67 schwarze 746: mparse_parse_buffer(curp, blk, file);
1.70 schwarze 747: curp->filenc = save_filenc;
1.67 schwarze 748: if (with_mmap)
749: munmap(blk.buf, blk.sz);
750: else
751: free(blk.buf);
752: }
1.117 schwarze 753: return curp->file_status;
1.58 schwarze 754: }
755:
1.122 schwarze 756: int
757: mparse_open(struct mparse *curp, const char *file)
1.58 schwarze 758: {
759: char *cp;
1.122 schwarze 760: int fd;
1.58 schwarze 761:
762: curp->file = file;
1.115 schwarze 763: cp = strrchr(file, '.');
764: curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
1.74 schwarze 765:
1.115 schwarze 766: /* First try to use the filename as it is. */
1.74 schwarze 767:
1.122 schwarze 768: if ((fd = open(file, O_RDONLY)) != -1)
769: return fd;
1.74 schwarze 770:
1.115 schwarze 771: /*
772: * If that doesn't work and the filename doesn't
773: * already end in .gz, try appending .gz.
774: */
1.74 schwarze 775:
1.115 schwarze 776: if ( ! curp->gzip) {
1.74 schwarze 777: mandoc_asprintf(&cp, "%s.gz", file);
1.123 schwarze 778: fd = open(cp, O_RDONLY);
1.83 schwarze 779: free(cp);
1.122 schwarze 780: if (fd != -1) {
1.115 schwarze 781: curp->gzip = 1;
1.122 schwarze 782: return fd;
1.58 schwarze 783: }
784: }
785:
1.115 schwarze 786: /* Neither worked, give up. */
1.73 schwarze 787:
1.115 schwarze 788: mandoc_msg(MANDOCERR_FILE, curp, 0, 0, strerror(errno));
1.122 schwarze 789: return -1;
1.1 schwarze 790: }
791:
792: struct mparse *
1.69 schwarze 793: mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg,
1.119 schwarze 794: const char *defos)
1.1 schwarze 795: {
796: struct mparse *curp;
797:
798: curp = mandoc_calloc(1, sizeof(struct mparse));
799:
1.22 schwarze 800: curp->options = options;
1.1 schwarze 801: curp->wlevel = wlevel;
802: curp->mmsg = mmsg;
1.7 schwarze 803: curp->defos = defos;
1.1 schwarze 804:
1.119 schwarze 805: curp->roff = roff_alloc(curp, options);
1.112 schwarze 806: curp->man = roff_man_alloc( curp->roff, curp, curp->defos,
807: curp->options & MPARSE_QUICK ? 1 : 0);
1.111 schwarze 808: if (curp->options & MPARSE_MDOC) {
1.112 schwarze 809: curp->man->macroset = MACROSET_MDOC;
1.136 schwarze 810: if (curp->man->mdocmac == NULL)
811: curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.112 schwarze 812: } else if (curp->options & MPARSE_MAN) {
813: curp->man->macroset = MACROSET_MAN;
1.136 schwarze 814: if (curp->man->manmac == NULL)
815: curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.111 schwarze 816: }
1.113 schwarze 817: curp->man->first->tok = TOKEN_NONE;
1.117 schwarze 818: return curp;
1.1 schwarze 819: }
820:
821: void
822: mparse_reset(struct mparse *curp)
823: {
824: roff_reset(curp->roff);
1.124 schwarze 825: roff_man_reset(curp->man);
1.133 schwarze 826:
827: free(curp->sodest);
828: curp->sodest = NULL;
829:
1.4 schwarze 830: if (curp->secondary)
831: curp->secondary->sz = 0;
1.1 schwarze 832:
833: curp->file_status = MANDOCLEVEL_OK;
1.132 schwarze 834: curp->gzip = 0;
1.1 schwarze 835: }
836:
837: void
838: mparse_free(struct mparse *curp)
839: {
840:
1.136 schwarze 841: roffhash_free(curp->man->mdocmac);
842: roffhash_free(curp->man->manmac);
1.112 schwarze 843: roff_man_free(curp->man);
1.133 schwarze 844: roff_free(curp->roff);
1.4 schwarze 845: if (curp->secondary)
846: free(curp->secondary->buf);
1.1 schwarze 847:
1.4 schwarze 848: free(curp->secondary);
1.23 schwarze 849: free(curp->sodest);
1.1 schwarze 850: free(curp);
851: }
852:
853: void
1.110 schwarze 854: mparse_result(struct mparse *curp, struct roff_man **man,
855: char **sodest)
1.1 schwarze 856: {
857:
1.23 schwarze 858: if (sodest && NULL != (*sodest = curp->sodest)) {
859: *man = NULL;
860: return;
861: }
1.1 schwarze 862: if (man)
863: *man = curp->man;
1.130 schwarze 864: }
865:
866: void
867: mparse_updaterc(struct mparse *curp, enum mandoclevel *rc)
868: {
869: if (curp->file_status > *rc)
870: *rc = curp->file_status;
1.1 schwarze 871: }
872:
873: void
874: mandoc_vmsg(enum mandocerr t, struct mparse *m,
875: int ln, int pos, const char *fmt, ...)
876: {
877: char buf[256];
878: va_list ap;
879:
880: va_start(ap, fmt);
1.26 schwarze 881: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.1 schwarze 882: va_end(ap);
883:
884: mandoc_msg(t, m, ln, pos, buf);
885: }
886:
887: void
1.25 schwarze 888: mandoc_msg(enum mandocerr er, struct mparse *m,
1.1 schwarze 889: int ln, int col, const char *msg)
890: {
891: enum mandoclevel level;
892:
1.87 schwarze 893: level = MANDOCLEVEL_UNSUPP;
1.1 schwarze 894: while (er < mandoclimits[level])
895: level--;
896:
1.83 schwarze 897: if (level < m->wlevel && er != MANDOCERR_FILE)
1.1 schwarze 898: return;
899:
900: if (m->mmsg)
901: (*m->mmsg)(er, level, m->file, ln, col, msg);
902:
903: if (m->file_status < level)
904: m->file_status = level;
905: }
906:
907: const char *
908: mparse_strerror(enum mandocerr er)
909: {
910:
1.117 schwarze 911: return mandocerrs[er];
1.1 schwarze 912: }
913:
914: const char *
915: mparse_strlevel(enum mandoclevel lvl)
916: {
1.117 schwarze 917: return mandoclevels[lvl];
1.4 schwarze 918: }
919:
920: void
921: mparse_keep(struct mparse *p)
922: {
923:
924: assert(NULL == p->secondary);
925: p->secondary = mandoc_calloc(1, sizeof(struct buf));
926: }
927:
928: const char *
929: mparse_getkeep(const struct mparse *p)
930: {
931:
932: assert(p->secondary);
1.117 schwarze 933: return p->secondary->sz ? p->secondary->buf : NULL;
1.1 schwarze 934: }