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