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