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