Annotation of src/usr.bin/mandoc/read.c, Revision 1.68
1.68 ! schwarze 1: /* $OpenBSD: read.c,v 1.67 2014/10/18 15:46:16 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.18 schwarze 4: * Copyright (c) 2010-2014 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: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
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>
1.58 schwarze 22: #include <sys/wait.h>
1.1 schwarze 23:
24: #include <assert.h>
25: #include <ctype.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>
33:
34: #include "mandoc.h"
1.24 schwarze 35: #include "mandoc_aux.h"
1.1 schwarze 36: #include "libmandoc.h"
37: #include "mdoc.h"
38: #include "man.h"
39:
40: #define REPARSE_LIMIT 1000
41:
42: struct buf {
1.25 schwarze 43: char *buf; /* binary input buffer */
1.1 schwarze 44: size_t sz; /* size of binary buffer */
45: };
46:
47: struct mparse {
48: struct man *pman; /* persistent man parser */
49: struct mdoc *pmdoc; /* persistent mdoc parser */
50: struct man *man; /* man parser */
51: struct mdoc *mdoc; /* mdoc parser */
52: struct roff *roff; /* roff parser (!NULL) */
1.23 schwarze 53: char *sodest; /* filename pointed to by .so */
1.59 schwarze 54: const char *file; /* filename of current input file */
55: struct buf *primary; /* buffer currently being parsed */
56: struct buf *secondary; /* preprocessed copy of input */
57: const char *defos; /* default operating system */
58: mandocmsg mmsg; /* warning/error message handler */
59: enum mandoclevel file_status; /* status of current parse */
60: enum mandoclevel wlevel; /* ignore messages below this */
61: int options; /* parser options */
1.1 schwarze 62: int reparse_count; /* finite interp. stack */
1.59 schwarze 63: int line; /* line number in the file */
1.1 schwarze 64: };
65:
1.60 schwarze 66: static void choose_parser(struct mparse *);
1.1 schwarze 67: static void resize_buf(struct buf *, size_t);
68: static void mparse_buf_r(struct mparse *, struct buf, int);
1.18 schwarze 69: static int read_whole_file(struct mparse *, const char *, int,
70: struct buf *, int *);
1.1 schwarze 71: static void mparse_end(struct mparse *);
1.15 schwarze 72: static void mparse_parse_buffer(struct mparse *, struct buf,
73: const char *);
1.1 schwarze 74:
75: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
76: MANDOCERR_OK,
77: MANDOCERR_WARNING,
78: MANDOCERR_WARNING,
79: MANDOCERR_ERROR,
80: MANDOCERR_FATAL,
81: MANDOCERR_MAX,
82: MANDOCERR_MAX
83: };
84:
85: static const char * const mandocerrs[MANDOCERR_MAX] = {
86: "ok",
87:
88: "generic warning",
89:
90: /* related to the prologue */
1.57 schwarze 91: "missing manual title, using UNTITLED",
92: "missing manual title, using \"\"",
1.32 schwarze 93: "lower case character in document title",
1.57 schwarze 94: "missing manual section, using \"\"",
1.1 schwarze 95: "unknown manual section",
1.11 schwarze 96: "unknown manual volume or arch",
1.32 schwarze 97: "missing date, using today's date",
1.1 schwarze 98: "cannot parse date, using it verbatim",
1.57 schwarze 99: "missing Os macro, using \"\"",
100: "duplicate prologue macro",
101: "late prologue macro",
102: "skipping late title macro",
1.1 schwarze 103: "prologue macros out of order",
104:
105: /* related to document structure */
106: ".so is fragile, better use ln(1)",
1.28 schwarze 107: "no document body",
1.32 schwarze 108: "content before first section header",
109: "first section is not \"NAME\"",
1.1 schwarze 110: "bad NAME section contents",
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.1 schwarze 120: "skipping paragraph macro",
1.10 schwarze 121: "moving paragraph macro out of list",
1.1 schwarze 122: "skipping no-space macro",
123: "blocks badly nested",
124: "nested displays are not portable",
1.35 schwarze 125: "moving content out of list",
126: ".Vt block has child macro",
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.40 schwarze 135: "empty argument, using 0n",
1.1 schwarze 136: "argument count wrong",
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.38 schwarze 141: "empty head in list item",
142: "empty list item",
1.39 schwarze 143: "missing font type, using \\fR",
144: "unknown font type, using \\fR",
1.38 schwarze 145: "missing -std argument, adding it",
1.66 schwarze 146: "missing eqn box, using \"\"",
1.1 schwarze 147:
148: /* related to bad macro arguments */
1.42 schwarze 149: "unterminated quoted argument",
1.1 schwarze 150: "duplicate argument",
1.54 schwarze 151: "skipping duplicate argument",
1.41 schwarze 152: "skipping duplicate display type",
153: "skipping duplicate list type",
1.54 schwarze 154: "skipping -width argument",
1.1 schwarze 155: "unknown AT&T UNIX version",
1.64 schwarze 156: "comma in function argument",
1.65 schwarze 157: "parenthesis in function name",
1.45 schwarze 158: "invalid content in Rs block",
1.41 schwarze 159: "invalid Boolean argument",
160: "unknown font, skipping request",
1.1 schwarze 161:
162: /* related to plain text */
1.42 schwarze 163: "blank line in fill mode, using .sp",
164: "tab in filled text",
165: "whitespace at end of input line",
1.1 schwarze 166: "bad comment style",
1.42 schwarze 167: "invalid escape sequence",
168: "undefined string, using \"\"",
1.3 schwarze 169:
1.1 schwarze 170: "generic error",
171:
1.3 schwarze 172: /* related to equations */
173: "unexpected equation scope closure",
174: "equation scope open on exit",
175: "overlapping equation scopes",
176: "unexpected end of equation",
177:
1.1 schwarze 178: /* related to tables */
179: "bad table syntax",
180: "bad table option",
181: "bad table layout",
182: "no table layout cells specified",
183: "no table data cells specified",
184: "ignore data in cell",
185: "data block still open",
186: "ignoring extra data cells",
187:
1.46 schwarze 188: /* related to document structure and macros */
1.1 schwarze 189: "input stack limit exceeded, infinite loop?",
190: "skipping bad character",
1.46 schwarze 191: "skipping unknown macro",
1.51 schwarze 192: "skipping item outside list",
1.46 schwarze 193: "skipping column outside column list",
194: "skipping end of block that is not open",
195: "inserting missing end of block",
196: "appending missing end of block",
197:
198: /* related to request and macro arguments */
1.1 schwarze 199: "escaped character not allowed in a name",
200: "argument count wrong",
1.49 schwarze 201: "missing list type, using -item",
1.48 schwarze 202: "missing manual name, using \"\"",
1.49 schwarze 203: "uname(3) system call failed, using UNKNOWN",
1.41 schwarze 204: "unknown standard specifier",
1.49 schwarze 205: "skipping request without numeric argument",
1.39 schwarze 206: "skipping all arguments",
207: "skipping excess arguments",
1.68 ! schwarze 208: "divide by zero",
1.1 schwarze 209:
210: "generic fatal error",
211:
1.18 schwarze 212: "input too large",
1.56 schwarze 213: "NOT IMPLEMENTED: Bd -file",
1.1 schwarze 214: "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.30 schwarze 215: ".so request failed",
1.18 schwarze 216:
217: /* system errors */
1.58 schwarze 218: "cannot dup file descriptor",
219: "cannot exec",
220: "gunzip failed with code",
221: "cannot fork",
1.29 schwarze 222: NULL,
1.58 schwarze 223: "cannot open pipe",
224: "cannot read file",
225: "gunzip died from signal",
1.18 schwarze 226: "cannot stat file",
1.58 schwarze 227: "wait failed",
1.1 schwarze 228: };
229:
230: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
231: "SUCCESS",
232: "RESERVED",
233: "WARNING",
234: "ERROR",
235: "FATAL",
236: "BADARG",
237: "SYSERR"
238: };
239:
1.25 schwarze 240:
1.1 schwarze 241: static void
242: resize_buf(struct buf *buf, size_t initial)
243: {
244:
245: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
246: buf->buf = mandoc_realloc(buf->buf, buf->sz);
247: }
248:
249: static void
1.60 schwarze 250: choose_parser(struct mparse *curp)
1.1 schwarze 251: {
1.59 schwarze 252: char *cp, *ep;
253: int format;
1.1 schwarze 254:
1.59 schwarze 255: /*
256: * If neither command line arguments -mdoc or -man select
257: * a parser nor the roff parser found a .Dd or .TH macro
258: * yet, look ahead in the main input buffer.
259: */
260:
261: if ((format = roff_getformat(curp->roff)) == 0) {
262: cp = curp->primary->buf;
263: ep = cp + curp->primary->sz;
264: while (cp < ep) {
1.61 schwarze 265: if (*cp == '.' || *cp == '\'') {
1.59 schwarze 266: cp++;
267: if (cp[0] == 'D' && cp[1] == 'd') {
268: format = MPARSE_MDOC;
269: break;
270: }
271: if (cp[0] == 'T' && cp[1] == 'H') {
272: format = MPARSE_MAN;
273: break;
274: }
275: }
276: cp = memchr(cp, '\n', ep - cp);
277: if (cp == NULL)
278: break;
279: cp++;
280: }
1.1 schwarze 281: }
282:
1.59 schwarze 283: if (format == MPARSE_MDOC) {
1.25 schwarze 284: if (NULL == curp->pmdoc)
1.22 schwarze 285: curp->pmdoc = mdoc_alloc(
286: curp->roff, curp, curp->defos,
287: MPARSE_QUICK & curp->options ? 1 : 0);
1.1 schwarze 288: assert(curp->pmdoc);
289: curp->mdoc = curp->pmdoc;
290: return;
1.25 schwarze 291: }
1.1 schwarze 292:
1.59 schwarze 293: /* Fall back to man(7) as a last resort. */
294:
1.25 schwarze 295: if (NULL == curp->pman)
1.22 schwarze 296: curp->pman = man_alloc(curp->roff, curp,
297: MPARSE_QUICK & curp->options ? 1 : 0);
1.1 schwarze 298: assert(curp->pman);
299: curp->man = curp->pman;
300: }
301:
302: /*
303: * Main parse routine for an opened file. This is called for each
304: * opened file and simply loops around the full input file, possibly
305: * nesting (i.e., with `so').
306: */
307: static void
308: mparse_buf_r(struct mparse *curp, struct buf blk, int start)
309: {
310: const struct tbl_span *span;
311: struct buf ln;
312: enum rofferr rr;
313: int i, of, rc;
314: int pos; /* byte number in the ln buffer */
315: int lnn; /* line number in the real file */
316: unsigned char c;
317:
318: memset(&ln, 0, sizeof(struct buf));
319:
1.25 schwarze 320: lnn = curp->line;
321: pos = 0;
1.1 schwarze 322:
323: for (i = 0; i < (int)blk.sz; ) {
324: if (0 == pos && '\0' == blk.buf[i])
325: break;
326:
327: if (start) {
328: curp->line = lnn;
329: curp->reparse_count = 0;
330: }
331:
332: while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
333:
334: /*
335: * When finding an unescaped newline character,
336: * leave the character loop to process the line.
337: * Skip a preceding carriage return, if any.
338: */
339:
340: if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
341: '\n' == blk.buf[i + 1])
342: ++i;
343: if ('\n' == blk.buf[i]) {
344: ++i;
345: ++lnn;
346: break;
347: }
348:
1.13 schwarze 349: /*
350: * Make sure we have space for at least
351: * one backslash and one other character
352: * and the trailing NUL byte.
353: */
354:
355: if (pos + 2 >= (int)ln.sz)
356: resize_buf(&ln, 256);
357:
1.25 schwarze 358: /*
1.1 schwarze 359: * Warn about bogus characters. If you're using
360: * non-ASCII encoding, you're screwing your
361: * readers. Since I'd rather this not happen,
1.6 schwarze 362: * I'll be helpful and replace these characters
363: * with "?", so we don't display gibberish.
364: * Note to manual writers: use special characters.
1.1 schwarze 365: */
366:
367: c = (unsigned char) blk.buf[i];
368:
1.25 schwarze 369: if ( ! (isascii(c) &&
370: (isgraph(c) || isblank(c)))) {
1.56 schwarze 371: mandoc_vmsg(MANDOCERR_BADCHAR, curp,
372: curp->line, pos, "0x%x", c);
1.1 schwarze 373: i++;
1.6 schwarze 374: ln.buf[pos++] = '?';
1.1 schwarze 375: continue;
376: }
377:
378: /* Trailing backslash = a plain char. */
379:
380: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
381: ln.buf[pos++] = blk.buf[i++];
382: continue;
383: }
384:
385: /*
386: * Found escape and at least one other character.
387: * When it's a newline character, skip it.
388: * When there is a carriage return in between,
389: * skip that one as well.
390: */
391:
392: if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
393: '\n' == blk.buf[i + 2])
394: ++i;
395: if ('\n' == blk.buf[i + 1]) {
396: i += 2;
397: ++lnn;
398: continue;
399: }
400:
401: if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
402: i += 2;
403: /* Comment, skip to end of line */
404: for (; i < (int)blk.sz; ++i) {
405: if ('\n' == blk.buf[i]) {
406: ++i;
407: ++lnn;
408: break;
409: }
410: }
411:
412: /* Backout trailing whitespaces */
413: for (; pos > 0; --pos) {
414: if (ln.buf[pos - 1] != ' ')
415: break;
416: if (pos > 2 && ln.buf[pos - 2] == '\\')
417: break;
418: }
419: break;
420: }
421:
1.13 schwarze 422: /* Catch escaped bogus characters. */
423:
424: c = (unsigned char) blk.buf[i+1];
425:
1.25 schwarze 426: if ( ! (isascii(c) &&
427: (isgraph(c) || isblank(c)))) {
1.56 schwarze 428: mandoc_vmsg(MANDOCERR_BADCHAR, curp,
429: curp->line, pos, "0x%x", c);
1.13 schwarze 430: i += 2;
431: ln.buf[pos++] = '?';
432: continue;
433: }
434:
1.1 schwarze 435: /* Some other escape sequence, copy & cont. */
436:
437: ln.buf[pos++] = blk.buf[i++];
438: ln.buf[pos++] = blk.buf[i++];
439: }
440:
1.25 schwarze 441: if (pos >= (int)ln.sz)
1.1 schwarze 442: resize_buf(&ln, 256);
443:
444: ln.buf[pos] = '\0';
445:
446: /*
447: * A significant amount of complexity is contained by
448: * the roff preprocessor. It's line-oriented but can be
449: * expressed on one line, so we need at times to
450: * readjust our starting point and re-run it. The roff
451: * preprocessor can also readjust the buffers with new
452: * data, so we pass them in wholesale.
453: */
454:
455: of = 0;
456:
1.4 schwarze 457: /*
458: * Maintain a lookaside buffer of all parsed lines. We
459: * only do this if mparse_keep() has been invoked (the
460: * buffer may be accessed with mparse_getkeep()).
461: */
462:
463: if (curp->secondary) {
1.25 schwarze 464: curp->secondary->buf = mandoc_realloc(
465: curp->secondary->buf,
466: curp->secondary->sz + pos + 2);
467: memcpy(curp->secondary->buf +
468: curp->secondary->sz,
469: ln.buf, pos);
1.4 schwarze 470: curp->secondary->sz += pos;
471: curp->secondary->buf
472: [curp->secondary->sz] = '\n';
473: curp->secondary->sz++;
474: curp->secondary->buf
475: [curp->secondary->sz] = '\0';
476: }
1.1 schwarze 477: rerun:
1.25 schwarze 478: rr = roff_parseln(curp->roff, curp->line,
479: &ln.buf, &ln.sz, of, &of);
1.1 schwarze 480:
481: switch (rr) {
1.25 schwarze 482: case ROFF_REPARSE:
1.1 schwarze 483: if (REPARSE_LIMIT >= ++curp->reparse_count)
484: mparse_buf_r(curp, ln, 0);
485: else
486: mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.25 schwarze 487: curp->line, pos, NULL);
1.1 schwarze 488: pos = 0;
489: continue;
1.25 schwarze 490: case ROFF_APPEND:
1.1 schwarze 491: pos = (int)strlen(ln.buf);
492: continue;
1.25 schwarze 493: case ROFF_RERUN:
1.1 schwarze 494: goto rerun;
1.25 schwarze 495: case ROFF_IGN:
1.1 schwarze 496: pos = 0;
497: continue;
1.25 schwarze 498: case ROFF_ERR:
1.1 schwarze 499: assert(MANDOCLEVEL_FATAL <= curp->file_status);
500: break;
1.25 schwarze 501: case ROFF_SO:
1.23 schwarze 502: if (0 == (MPARSE_SO & curp->options) &&
503: (i >= (int)blk.sz || '\0' == blk.buf[i])) {
504: curp->sodest = mandoc_strdup(ln.buf + of);
505: free(ln.buf);
506: return;
507: }
1.4 schwarze 508: /*
509: * We remove `so' clauses from our lookaside
510: * buffer because we're going to descend into
511: * the file recursively.
512: */
1.25 schwarze 513: if (curp->secondary)
1.4 schwarze 514: curp->secondary->sz -= pos + 1;
1.14 schwarze 515: mparse_readfd(curp, -1, ln.buf + of);
1.30 schwarze 516: if (MANDOCLEVEL_FATAL <= curp->file_status) {
517: mandoc_vmsg(MANDOCERR_SO_FAIL,
518: curp, curp->line, pos,
519: ".so %s", ln.buf + of);
1.1 schwarze 520: break;
1.30 schwarze 521: }
1.1 schwarze 522: pos = 0;
523: continue;
524: default:
525: break;
526: }
527:
528: /*
529: * If we encounter errors in the recursive parse, make
530: * sure we don't continue parsing.
531: */
532:
533: if (MANDOCLEVEL_FATAL <= curp->file_status)
534: break;
535:
536: /*
537: * If input parsers have not been allocated, do so now.
1.2 schwarze 538: * We keep these instanced between parsers, but set them
1.1 schwarze 539: * locally per parse routine since we can use different
540: * parsers with each one.
541: */
542:
543: if ( ! (curp->man || curp->mdoc))
1.60 schwarze 544: choose_parser(curp);
1.1 schwarze 545:
1.25 schwarze 546: /*
1.60 schwarze 547: * Lastly, push down into the parsers themselves.
1.1 schwarze 548: * If libroff returns ROFF_TBL, then add it to the
549: * currently open parse. Since we only get here if
550: * there does exist data (see tbl_data.c), we're
551: * guaranteed that something's been allocated.
552: * Do the same for ROFF_EQN.
553: */
554:
555: rc = -1;
556:
557: if (ROFF_TBL == rr)
558: while (NULL != (span = roff_span(curp->roff))) {
559: rc = curp->man ?
1.25 schwarze 560: man_addspan(curp->man, span) :
561: mdoc_addspan(curp->mdoc, span);
1.1 schwarze 562: if (0 == rc)
563: break;
564: }
565: else if (ROFF_EQN == rr)
1.25 schwarze 566: rc = curp->mdoc ?
567: mdoc_addeqn(curp->mdoc,
568: roff_eqn(curp->roff)) :
569: man_addeqn(curp->man,
570: roff_eqn(curp->roff));
1.1 schwarze 571: else if (curp->man || curp->mdoc)
572: rc = curp->man ?
1.25 schwarze 573: man_parseln(curp->man,
574: curp->line, ln.buf, of) :
575: mdoc_parseln(curp->mdoc,
576: curp->line, ln.buf, of);
1.1 schwarze 577:
578: if (0 == rc) {
579: assert(MANDOCLEVEL_FATAL <= curp->file_status);
580: break;
1.19 schwarze 581: } else if (2 == rc)
582: break;
1.1 schwarze 583:
584: /* Temporary buffers typically are not full. */
585:
586: if (0 == start && '\0' == blk.buf[i])
587: break;
588:
589: /* Start the next input line. */
590:
591: pos = 0;
592: }
593:
594: free(ln.buf);
595: }
596:
597: static int
1.18 schwarze 598: read_whole_file(struct mparse *curp, const char *file, int fd,
599: struct buf *fb, int *with_mmap)
1.1 schwarze 600: {
601: struct stat st;
602: size_t off;
603: ssize_t ssz;
604:
605: if (-1 == fstat(fd, &st)) {
1.18 schwarze 606: curp->file_status = MANDOCLEVEL_SYSERR;
607: if (curp->mmsg)
608: (*curp->mmsg)(MANDOCERR_SYSSTAT, curp->file_status,
609: file, 0, 0, strerror(errno));
1.1 schwarze 610: return(0);
611: }
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:
620: if (S_ISREG(st.st_mode)) {
621: if (st.st_size >= (1U << 31)) {
1.18 schwarze 622: curp->file_status = MANDOCLEVEL_FATAL;
623: if (curp->mmsg)
624: (*curp->mmsg)(MANDOCERR_TOOLARGE,
625: curp->file_status, file, 0, 0, NULL);
1.1 schwarze 626: return(0);
627: }
628: *with_mmap = 1;
629: fb->sz = (size_t)st.st_size;
1.15 schwarze 630: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 schwarze 631: if (fb->buf != MAP_FAILED)
632: return(1);
633: }
634:
635: /*
636: * If this isn't a regular file (like, say, stdin), then we must
637: * go the old way and just read things in bit by bit.
638: */
639:
640: *with_mmap = 0;
641: off = 0;
642: fb->sz = 0;
643: fb->buf = NULL;
644: for (;;) {
645: if (off == fb->sz) {
646: if (fb->sz == (1U << 31)) {
1.18 schwarze 647: curp->file_status = MANDOCLEVEL_FATAL;
648: if (curp->mmsg)
649: (*curp->mmsg)(MANDOCERR_TOOLARGE,
650: curp->file_status,
651: file, 0, 0, NULL);
1.1 schwarze 652: break;
653: }
654: resize_buf(fb, 65536);
655: }
656: ssz = read(fd, fb->buf + (int)off, fb->sz - off);
657: if (ssz == 0) {
658: fb->sz = off;
659: return(1);
660: }
661: if (ssz == -1) {
1.18 schwarze 662: curp->file_status = MANDOCLEVEL_SYSERR;
663: if (curp->mmsg)
664: (*curp->mmsg)(MANDOCERR_SYSREAD,
665: curp->file_status, file, 0, 0,
666: strerror(errno));
1.1 schwarze 667: break;
668: }
669: off += (size_t)ssz;
670: }
671:
672: free(fb->buf);
673: fb->buf = NULL;
674: return(0);
675: }
676:
677: static void
678: mparse_end(struct mparse *curp)
679: {
680:
681: if (MANDOCLEVEL_FATAL <= curp->file_status)
682: return;
683:
1.50 schwarze 684: if (curp->mdoc == NULL &&
685: curp->man == NULL &&
686: curp->sodest == NULL) {
687: if (curp->options & MPARSE_MDOC)
688: curp->mdoc = curp->pmdoc;
689: else {
690: if (curp->pman == NULL)
691: curp->pman = man_alloc(curp->roff, curp,
692: curp->options & MPARSE_QUICK ? 1 : 0);
693: curp->man = curp->pman;
694: }
695: }
696:
1.1 schwarze 697: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
698: assert(MANDOCLEVEL_FATAL <= curp->file_status);
699: return;
700: }
701:
702: if (curp->man && ! man_endparse(curp->man)) {
703: assert(MANDOCLEVEL_FATAL <= curp->file_status);
704: return;
705: }
706:
707: roff_endparse(curp->roff);
708: }
709:
1.15 schwarze 710: static void
711: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.1 schwarze 712: {
1.61 schwarze 713: struct buf *svprimary;
1.1 schwarze 714: const char *svfile;
1.14 schwarze 715: static int recursion_depth;
716:
717: if (64 < recursion_depth) {
718: mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
1.15 schwarze 719: return;
1.14 schwarze 720: }
1.1 schwarze 721:
1.15 schwarze 722: /* Line number is per-file. */
723: svfile = curp->file;
724: curp->file = file;
1.61 schwarze 725: svprimary = curp->primary;
1.59 schwarze 726: curp->primary = &blk;
1.15 schwarze 727: curp->line = 1;
728: recursion_depth++;
729:
730: mparse_buf_r(curp, blk, 1);
731:
732: if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status)
733: mparse_end(curp);
734:
1.61 schwarze 735: curp->primary = svprimary;
1.15 schwarze 736: curp->file = svfile;
737: }
738:
739: enum mandoclevel
740: mparse_readfd(struct mparse *curp, int fd, const char *file)
741: {
742: struct buf blk;
743: int with_mmap;
744:
1.18 schwarze 745: if (-1 == fd && -1 == (fd = open(file, O_RDONLY, 0))) {
746: curp->file_status = MANDOCLEVEL_SYSERR;
747: if (curp->mmsg)
748: (*curp->mmsg)(MANDOCERR_SYSOPEN,
749: curp->file_status,
750: file, 0, 0, strerror(errno));
1.67 schwarze 751: return(curp->file_status);
1.18 schwarze 752: }
753:
1.15 schwarze 754: /*
755: * Run for each opened file; may be called more than once for
756: * each full parse sequence if the opened file is nested (i.e.,
757: * from `so'). Simply sucks in the whole file and moves into
758: * the parse phase for the file.
759: */
1.1 schwarze 760:
1.67 schwarze 761: if (read_whole_file(curp, file, fd, &blk, &with_mmap)) {
762: mparse_parse_buffer(curp, blk, file);
763: if (with_mmap)
764: munmap(blk.buf, blk.sz);
765: else
766: free(blk.buf);
767: }
1.1 schwarze 768:
769: if (STDIN_FILENO != fd && -1 == close(fd))
770: perror(file);
1.67 schwarze 771:
1.1 schwarze 772: return(curp->file_status);
1.58 schwarze 773: }
774:
775: enum mandoclevel
776: mparse_open(struct mparse *curp, int *fd, const char *file,
777: pid_t *child_pid)
778: {
779: int pfd[2];
780: char *cp;
781: enum mandocerr err;
782:
783: pfd[1] = -1;
784: curp->file = file;
785: if ((cp = strrchr(file, '.')) == NULL ||
786: strcmp(cp + 1, "gz")) {
787: *child_pid = 0;
788: if ((*fd = open(file, O_RDONLY)) == -1) {
789: err = MANDOCERR_SYSOPEN;
790: goto out;
791: }
792: return(MANDOCLEVEL_OK);
793: }
794:
795: if (pipe(pfd) == -1) {
796: err = MANDOCERR_SYSPIPE;
797: goto out;
798: }
799:
800: switch (*child_pid = fork()) {
801: case -1:
802: err = MANDOCERR_SYSFORK;
803: close(pfd[0]);
804: close(pfd[1]);
805: pfd[1] = -1;
806: break;
807: case 0:
808: close(pfd[0]);
809: if (dup2(pfd[1], STDOUT_FILENO) == -1) {
810: err = MANDOCERR_SYSDUP;
811: break;
812: }
813: execlp("gunzip", "gunzip", "-c", file, NULL);
814: err = MANDOCERR_SYSEXEC;
815: break;
816: default:
817: close(pfd[1]);
818: *fd = pfd[0];
819: return(MANDOCLEVEL_OK);
820: }
821:
822: out:
823: *fd = -1;
824: *child_pid = 0;
825: curp->file_status = MANDOCLEVEL_SYSERR;
826: if (curp->mmsg)
827: (*curp->mmsg)(err, curp->file_status, file,
828: 0, 0, strerror(errno));
829: if (pfd[1] != -1)
830: exit(1);
831: return(curp->file_status);
832: }
833:
834: enum mandoclevel
835: mparse_wait(struct mparse *curp, pid_t child_pid)
836: {
837: int status;
838:
839: if (waitpid(child_pid, &status, 0) == -1) {
840: mandoc_msg(MANDOCERR_SYSWAIT, curp, 0, 0,
841: strerror(errno));
842: curp->file_status = MANDOCLEVEL_SYSERR;
843: return(curp->file_status);
844: }
845: if (WIFSIGNALED(status)) {
846: mandoc_vmsg(MANDOCERR_SYSSIG, curp, 0, 0,
847: "%d", WTERMSIG(status));
848: curp->file_status = MANDOCLEVEL_SYSERR;
849: return(curp->file_status);
850: }
851: if (WEXITSTATUS(status)) {
852: mandoc_vmsg(MANDOCERR_SYSEXIT, curp, 0, 0,
853: "%d", WEXITSTATUS(status));
854: curp->file_status = MANDOCLEVEL_SYSERR;
855: return(curp->file_status);
856: }
857: return(MANDOCLEVEL_OK);
1.1 schwarze 858: }
859:
860: struct mparse *
1.22 schwarze 861: mparse_alloc(int options, enum mandoclevel wlevel,
1.47 schwarze 862: mandocmsg mmsg, const char *defos)
1.1 schwarze 863: {
864: struct mparse *curp;
865:
866: assert(wlevel <= MANDOCLEVEL_FATAL);
867:
868: curp = mandoc_calloc(1, sizeof(struct mparse));
869:
1.22 schwarze 870: curp->options = options;
1.1 schwarze 871: curp->wlevel = wlevel;
872: curp->mmsg = mmsg;
1.7 schwarze 873: curp->defos = defos;
1.1 schwarze 874:
1.22 schwarze 875: curp->roff = roff_alloc(curp, options);
1.50 schwarze 876: if (curp->options & MPARSE_MDOC)
877: curp->pmdoc = mdoc_alloc(
878: curp->roff, curp, curp->defos,
879: curp->options & MPARSE_QUICK ? 1 : 0);
880: if (curp->options & MPARSE_MAN)
881: curp->pman = man_alloc(curp->roff, curp,
882: curp->options & MPARSE_QUICK ? 1 : 0);
883:
1.1 schwarze 884: return(curp);
885: }
886:
887: void
888: mparse_reset(struct mparse *curp)
889: {
890:
891: roff_reset(curp->roff);
892:
893: if (curp->mdoc)
894: mdoc_reset(curp->mdoc);
895: if (curp->man)
896: man_reset(curp->man);
1.4 schwarze 897: if (curp->secondary)
898: curp->secondary->sz = 0;
1.1 schwarze 899:
900: curp->file_status = MANDOCLEVEL_OK;
901: curp->mdoc = NULL;
902: curp->man = NULL;
1.23 schwarze 903:
904: free(curp->sodest);
905: curp->sodest = NULL;
1.1 schwarze 906: }
907:
908: void
909: mparse_free(struct mparse *curp)
910: {
911:
912: if (curp->pmdoc)
913: mdoc_free(curp->pmdoc);
914: if (curp->pman)
915: man_free(curp->pman);
916: if (curp->roff)
917: roff_free(curp->roff);
1.4 schwarze 918: if (curp->secondary)
919: free(curp->secondary->buf);
1.1 schwarze 920:
1.4 schwarze 921: free(curp->secondary);
1.23 schwarze 922: free(curp->sodest);
1.1 schwarze 923: free(curp);
924: }
925:
926: void
1.23 schwarze 927: mparse_result(struct mparse *curp,
928: struct mdoc **mdoc, struct man **man, char **sodest)
1.1 schwarze 929: {
930:
1.23 schwarze 931: if (sodest && NULL != (*sodest = curp->sodest)) {
932: *mdoc = NULL;
933: *man = NULL;
934: return;
935: }
1.1 schwarze 936: if (mdoc)
937: *mdoc = curp->mdoc;
938: if (man)
939: *man = curp->man;
940: }
941:
942: void
943: mandoc_vmsg(enum mandocerr t, struct mparse *m,
944: int ln, int pos, const char *fmt, ...)
945: {
946: char buf[256];
947: va_list ap;
948:
949: va_start(ap, fmt);
1.26 schwarze 950: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.1 schwarze 951: va_end(ap);
952:
953: mandoc_msg(t, m, ln, pos, buf);
954: }
955:
956: void
1.25 schwarze 957: mandoc_msg(enum mandocerr er, struct mparse *m,
1.1 schwarze 958: int ln, int col, const char *msg)
959: {
960: enum mandoclevel level;
961:
962: level = MANDOCLEVEL_FATAL;
963: while (er < mandoclimits[level])
964: level--;
965:
966: if (level < m->wlevel)
967: return;
968:
969: if (m->mmsg)
970: (*m->mmsg)(er, level, m->file, ln, col, msg);
971:
972: if (m->file_status < level)
973: m->file_status = level;
974: }
975:
976: const char *
977: mparse_strerror(enum mandocerr er)
978: {
979:
980: return(mandocerrs[er]);
981: }
982:
983: const char *
984: mparse_strlevel(enum mandoclevel lvl)
985: {
986: return(mandoclevels[lvl]);
1.4 schwarze 987: }
988:
989: void
990: mparse_keep(struct mparse *p)
991: {
992:
993: assert(NULL == p->secondary);
994: p->secondary = mandoc_calloc(1, sizeof(struct buf));
995: }
996:
997: const char *
998: mparse_getkeep(const struct mparse *p)
999: {
1000:
1001: assert(p->secondary);
1002: return(p->secondary->sz ? p->secondary->buf : NULL);
1.1 schwarze 1003: }