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