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