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