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