Annotation of src/usr.bin/mandoc/read.c, Revision 1.82
1.82 ! schwarze 1: /* $OpenBSD: read.c,v 1.81 2014/12/28 14:39:08 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: *
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 mparse {
43: struct man *pman; /* persistent man parser */
44: struct mdoc *pmdoc; /* persistent mdoc parser */
45: struct man *man; /* man parser */
46: struct mdoc *mdoc; /* mdoc parser */
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.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.73 schwarze 61: pid_t child; /* the gunzip(1) process */
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,
78: MANDOCERR_FATAL,
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.1 schwarze 107: "bad NAME section contents",
108: "sections out of conventional order",
1.32 schwarze 109: "duplicate section title",
110: "unexpected section",
1.63 schwarze 111: "unusual Xr order",
112: "unusual Xr punctuation",
1.62 schwarze 113: "AUTHORS section without An macro",
1.1 schwarze 114:
115: /* related to macros and nesting */
1.33 schwarze 116: "obsolete macro",
1.78 schwarze 117: "macro neither callable nor escaped",
1.1 schwarze 118: "skipping paragraph macro",
1.10 schwarze 119: "moving paragraph macro out of list",
1.1 schwarze 120: "skipping no-space macro",
121: "blocks badly nested",
122: "nested displays are not portable",
1.35 schwarze 123: "moving content out of list",
124: ".Vt block has child macro",
1.56 schwarze 125: "fill mode already enabled, skipping",
126: "fill mode already disabled, skipping",
1.1 schwarze 127: "line scope broken",
128:
129: /* related to missing macro arguments */
1.36 schwarze 130: "skipping empty request",
131: "conditional request controls empty scope",
1.1 schwarze 132: "skipping empty macro",
1.40 schwarze 133: "empty argument, using 0n",
1.1 schwarze 134: "argument count wrong",
1.38 schwarze 135: "missing display type, using -ragged",
136: "list type is not the first argument",
137: "missing -width in -tag list, using 8n",
1.56 schwarze 138: "missing utility name, using \"\"",
1.38 schwarze 139: "empty head in list item",
140: "empty list item",
1.39 schwarze 141: "missing font type, using \\fR",
142: "unknown font type, using \\fR",
1.79 schwarze 143: "nothing follows prefix",
1.38 schwarze 144: "missing -std argument, adding it",
1.66 schwarze 145: "missing eqn box, using \"\"",
1.1 schwarze 146:
147: /* related to bad macro arguments */
1.42 schwarze 148: "unterminated quoted argument",
1.1 schwarze 149: "duplicate argument",
1.54 schwarze 150: "skipping duplicate argument",
1.41 schwarze 151: "skipping duplicate display type",
152: "skipping duplicate list type",
1.54 schwarze 153: "skipping -width argument",
1.1 schwarze 154: "unknown AT&T UNIX version",
1.64 schwarze 155: "comma in function argument",
1.65 schwarze 156: "parenthesis in function name",
1.45 schwarze 157: "invalid content in Rs block",
1.41 schwarze 158: "invalid Boolean argument",
159: "unknown font, skipping request",
1.1 schwarze 160:
161: /* related to plain text */
1.42 schwarze 162: "blank line in fill mode, using .sp",
163: "tab in filled text",
164: "whitespace at end of input line",
1.1 schwarze 165: "bad comment style",
1.42 schwarze 166: "invalid escape sequence",
167: "undefined string, using \"\"",
1.3 schwarze 168:
1.1 schwarze 169: "generic error",
170:
1.3 schwarze 171: /* related to equations */
172: "unexpected equation scope closure",
173: "equation scope open on exit",
174: "overlapping equation scopes",
175: "unexpected end of equation",
176:
1.1 schwarze 177: /* related to tables */
178: "bad table syntax",
179: "bad table option",
180: "bad table layout",
181: "no table layout cells specified",
182: "no table data cells specified",
183: "ignore data in cell",
184: "data block still open",
185: "ignoring extra data cells",
1.80 schwarze 186: "ignoring macro in table",
1.1 schwarze 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.75 schwarze 201: "NOT IMPLEMENTED: Bd -file",
1.49 schwarze 202: "missing list type, using -item",
1.48 schwarze 203: "missing manual name, using \"\"",
1.49 schwarze 204: "uname(3) system call failed, using UNKNOWN",
1.41 schwarze 205: "unknown standard specifier",
1.49 schwarze 206: "skipping request without numeric argument",
1.39 schwarze 207: "skipping all arguments",
208: "skipping excess arguments",
1.68 schwarze 209: "divide by zero",
1.1 schwarze 210:
211: "generic fatal error",
212:
1.18 schwarze 213: "input too large",
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: "gunzip failed with code",
1.29 schwarze 219: NULL,
1.58 schwarze 220: "gunzip died from signal",
1.1 schwarze 221: };
222:
223: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
224: "SUCCESS",
225: "RESERVED",
226: "WARNING",
227: "ERROR",
228: "FATAL",
229: "BADARG",
230: "SYSERR"
231: };
232:
1.25 schwarze 233:
1.1 schwarze 234: static void
235: resize_buf(struct buf *buf, size_t initial)
236: {
237:
238: buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
239: buf->buf = mandoc_realloc(buf->buf, buf->sz);
240: }
241:
242: static void
1.60 schwarze 243: choose_parser(struct mparse *curp)
1.1 schwarze 244: {
1.59 schwarze 245: char *cp, *ep;
246: int format;
1.1 schwarze 247:
1.59 schwarze 248: /*
249: * If neither command line arguments -mdoc or -man select
250: * a parser nor the roff parser found a .Dd or .TH macro
251: * yet, look ahead in the main input buffer.
252: */
253:
254: if ((format = roff_getformat(curp->roff)) == 0) {
255: cp = curp->primary->buf;
256: ep = cp + curp->primary->sz;
257: while (cp < ep) {
1.61 schwarze 258: if (*cp == '.' || *cp == '\'') {
1.59 schwarze 259: cp++;
260: if (cp[0] == 'D' && cp[1] == 'd') {
261: format = MPARSE_MDOC;
262: break;
263: }
264: if (cp[0] == 'T' && cp[1] == 'H') {
265: format = MPARSE_MAN;
266: break;
267: }
268: }
269: cp = memchr(cp, '\n', ep - cp);
270: if (cp == NULL)
271: break;
272: cp++;
273: }
1.1 schwarze 274: }
275:
1.59 schwarze 276: if (format == MPARSE_MDOC) {
1.25 schwarze 277: if (NULL == curp->pmdoc)
1.22 schwarze 278: curp->pmdoc = mdoc_alloc(
279: curp->roff, curp, curp->defos,
280: MPARSE_QUICK & curp->options ? 1 : 0);
1.1 schwarze 281: assert(curp->pmdoc);
282: curp->mdoc = curp->pmdoc;
283: return;
1.25 schwarze 284: }
1.1 schwarze 285:
1.59 schwarze 286: /* Fall back to man(7) as a last resort. */
287:
1.25 schwarze 288: if (NULL == curp->pman)
1.81 schwarze 289: curp->pman = man_alloc(
290: curp->roff, curp, curp->defos,
1.22 schwarze 291: MPARSE_QUICK & curp->options ? 1 : 0);
1.1 schwarze 292: assert(curp->pman);
293: curp->man = curp->pman;
294: }
295:
296: /*
1.71 schwarze 297: * Main parse routine for a buffer.
298: * It assumes encoding and line numbering are already set up.
299: * It can recurse directly (for invocations of user-defined
300: * macros, inline equations, and input line traps)
301: * and indirectly (for .so file inclusion).
1.1 schwarze 302: */
303: static void
1.71 schwarze 304: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1 schwarze 305: {
306: const struct tbl_span *span;
307: struct buf ln;
1.71 schwarze 308: size_t pos; /* byte number in the ln buffer */
1.1 schwarze 309: enum rofferr rr;
1.76 schwarze 310: int of;
1.1 schwarze 311: int lnn; /* line number in the real file */
312: unsigned char c;
313:
1.71 schwarze 314: memset(&ln, 0, sizeof(ln));
1.1 schwarze 315:
1.25 schwarze 316: lnn = curp->line;
317: pos = 0;
1.1 schwarze 318:
1.71 schwarze 319: while (i < blk.sz) {
1.1 schwarze 320: if (0 == pos && '\0' == blk.buf[i])
321: break;
322:
323: if (start) {
324: curp->line = lnn;
325: curp->reparse_count = 0;
1.70 schwarze 326:
327: if (lnn < 3 &&
328: curp->filenc & MPARSE_UTF8 &&
1.71 schwarze 329: curp->filenc & MPARSE_LATIN1)
330: curp->filenc = preconv_cue(&blk, i);
1.1 schwarze 331: }
332:
1.71 schwarze 333: while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1 schwarze 334:
335: /*
336: * When finding an unescaped newline character,
337: * leave the character loop to process the line.
338: * Skip a preceding carriage return, if any.
339: */
340:
1.71 schwarze 341: if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1 schwarze 342: '\n' == blk.buf[i + 1])
343: ++i;
344: if ('\n' == blk.buf[i]) {
345: ++i;
346: ++lnn;
347: break;
348: }
349:
1.13 schwarze 350: /*
1.70 schwarze 351: * Make sure we have space for the worst
352: * case of 11 bytes: "\\[u10ffff]\0"
1.13 schwarze 353: */
354:
1.71 schwarze 355: if (pos + 11 > ln.sz)
1.13 schwarze 356: resize_buf(&ln, 256);
357:
1.25 schwarze 358: /*
1.70 schwarze 359: * Encode 8-bit input.
1.1 schwarze 360: */
361:
1.70 schwarze 362: c = blk.buf[i];
363: if (c & 0x80) {
1.71 schwarze 364: if ( ! (curp->filenc && preconv_encode(
365: &blk, &i, &ln, &pos, &curp->filenc))) {
1.70 schwarze 366: mandoc_vmsg(MANDOCERR_BADCHAR,
367: curp, curp->line, pos,
368: "0x%x", c);
369: ln.buf[pos++] = '?';
370: i++;
371: }
372: continue;
373: }
374:
375: /*
376: * Exclude control characters.
377: */
1.1 schwarze 378:
1.70 schwarze 379: if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.56 schwarze 380: mandoc_vmsg(MANDOCERR_BADCHAR, curp,
381: curp->line, pos, "0x%x", c);
1.1 schwarze 382: i++;
1.6 schwarze 383: ln.buf[pos++] = '?';
1.1 schwarze 384: continue;
385: }
386:
387: /* Trailing backslash = a plain char. */
388:
1.71 schwarze 389: if (blk.buf[i] != '\\' || i + 1 == blk.sz) {
1.1 schwarze 390: ln.buf[pos++] = blk.buf[i++];
391: continue;
392: }
393:
394: /*
395: * Found escape and at least one other character.
396: * When it's a newline character, skip it.
397: * When there is a carriage return in between,
398: * skip that one as well.
399: */
400:
1.71 schwarze 401: if ('\r' == blk.buf[i + 1] && i + 2 < blk.sz &&
1.1 schwarze 402: '\n' == blk.buf[i + 2])
403: ++i;
404: if ('\n' == blk.buf[i + 1]) {
405: i += 2;
406: ++lnn;
407: continue;
408: }
409:
410: if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
411: i += 2;
412: /* Comment, skip to end of line */
1.71 schwarze 413: for (; i < blk.sz; ++i) {
1.1 schwarze 414: if ('\n' == blk.buf[i]) {
415: ++i;
416: ++lnn;
417: break;
418: }
419: }
420:
421: /* Backout trailing whitespaces */
422: for (; pos > 0; --pos) {
423: if (ln.buf[pos - 1] != ' ')
424: break;
425: if (pos > 2 && ln.buf[pos - 2] == '\\')
426: break;
427: }
428: break;
429: }
430:
1.13 schwarze 431: /* Catch escaped bogus characters. */
432:
433: c = (unsigned char) blk.buf[i+1];
434:
1.25 schwarze 435: if ( ! (isascii(c) &&
436: (isgraph(c) || isblank(c)))) {
1.56 schwarze 437: mandoc_vmsg(MANDOCERR_BADCHAR, curp,
438: curp->line, pos, "0x%x", c);
1.13 schwarze 439: i += 2;
440: ln.buf[pos++] = '?';
441: continue;
442: }
443:
1.1 schwarze 444: /* Some other escape sequence, copy & cont. */
445:
446: ln.buf[pos++] = blk.buf[i++];
447: ln.buf[pos++] = blk.buf[i++];
448: }
449:
1.71 schwarze 450: if (pos >= ln.sz)
1.1 schwarze 451: resize_buf(&ln, 256);
452:
453: ln.buf[pos] = '\0';
454:
455: /*
456: * A significant amount of complexity is contained by
457: * the roff preprocessor. It's line-oriented but can be
458: * expressed on one line, so we need at times to
459: * readjust our starting point and re-run it. The roff
460: * preprocessor can also readjust the buffers with new
461: * data, so we pass them in wholesale.
462: */
463:
464: of = 0;
465:
1.4 schwarze 466: /*
467: * Maintain a lookaside buffer of all parsed lines. We
468: * only do this if mparse_keep() has been invoked (the
469: * buffer may be accessed with mparse_getkeep()).
470: */
471:
472: if (curp->secondary) {
1.25 schwarze 473: curp->secondary->buf = mandoc_realloc(
474: curp->secondary->buf,
475: curp->secondary->sz + pos + 2);
476: memcpy(curp->secondary->buf +
477: curp->secondary->sz,
478: ln.buf, pos);
1.4 schwarze 479: curp->secondary->sz += pos;
480: curp->secondary->buf
481: [curp->secondary->sz] = '\n';
482: curp->secondary->sz++;
483: curp->secondary->buf
484: [curp->secondary->sz] = '\0';
485: }
1.1 schwarze 486: rerun:
1.72 schwarze 487: rr = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1 schwarze 488:
489: switch (rr) {
1.25 schwarze 490: case ROFF_REPARSE:
1.1 schwarze 491: if (REPARSE_LIMIT >= ++curp->reparse_count)
1.71 schwarze 492: mparse_buf_r(curp, ln, of, 0);
1.1 schwarze 493: else
494: mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.25 schwarze 495: curp->line, pos, NULL);
1.1 schwarze 496: pos = 0;
497: continue;
1.25 schwarze 498: case ROFF_APPEND:
1.71 schwarze 499: pos = strlen(ln.buf);
1.1 schwarze 500: continue;
1.25 schwarze 501: case ROFF_RERUN:
1.1 schwarze 502: goto rerun;
1.25 schwarze 503: case ROFF_IGN:
1.1 schwarze 504: pos = 0;
505: continue;
1.25 schwarze 506: case ROFF_ERR:
1.1 schwarze 507: assert(MANDOCLEVEL_FATAL <= curp->file_status);
508: break;
1.25 schwarze 509: case ROFF_SO:
1.71 schwarze 510: if ( ! (curp->options & MPARSE_SO) &&
511: (i >= blk.sz || blk.buf[i] == '\0')) {
1.23 schwarze 512: curp->sodest = mandoc_strdup(ln.buf + of);
513: free(ln.buf);
514: return;
515: }
1.4 schwarze 516: /*
517: * We remove `so' clauses from our lookaside
518: * buffer because we're going to descend into
519: * the file recursively.
520: */
1.25 schwarze 521: if (curp->secondary)
1.4 schwarze 522: curp->secondary->sz -= pos + 1;
1.14 schwarze 523: mparse_readfd(curp, -1, ln.buf + of);
1.30 schwarze 524: if (MANDOCLEVEL_FATAL <= curp->file_status) {
525: mandoc_vmsg(MANDOCERR_SO_FAIL,
526: curp, curp->line, pos,
527: ".so %s", ln.buf + of);
1.1 schwarze 528: break;
1.30 schwarze 529: }
1.1 schwarze 530: pos = 0;
531: continue;
532: default:
533: break;
534: }
535:
536: /*
537: * If we encounter errors in the recursive parse, make
538: * sure we don't continue parsing.
539: */
540:
541: if (MANDOCLEVEL_FATAL <= curp->file_status)
542: break;
543:
544: /*
545: * If input parsers have not been allocated, do so now.
1.2 schwarze 546: * We keep these instanced between parsers, but set them
1.1 schwarze 547: * locally per parse routine since we can use different
548: * parsers with each one.
549: */
550:
551: if ( ! (curp->man || curp->mdoc))
1.60 schwarze 552: choose_parser(curp);
1.1 schwarze 553:
1.25 schwarze 554: /*
1.60 schwarze 555: * Lastly, push down into the parsers themselves.
1.1 schwarze 556: * If libroff returns ROFF_TBL, then add it to the
557: * currently open parse. Since we only get here if
558: * there does exist data (see tbl_data.c), we're
559: * guaranteed that something's been allocated.
560: * Do the same for ROFF_EQN.
561: */
562:
1.76 schwarze 563: if (rr == ROFF_TBL) {
564: while ((span = roff_span(curp->roff)) != NULL)
565: if (curp->man == NULL)
566: mdoc_addspan(curp->mdoc, span);
567: else
568: man_addspan(curp->man, span);
569: } else if (rr == ROFF_EQN) {
570: if (curp->man == NULL)
571: mdoc_addeqn(curp->mdoc, roff_eqn(curp->roff));
572: else
573: man_addeqn(curp->man, roff_eqn(curp->roff));
574: } else if ((curp->man == NULL ?
575: mdoc_parseln(curp->mdoc, curp->line, ln.buf, of) :
576: man_parseln(curp->man, curp->line, ln.buf, of)) == 2)
577: break;
1.1 schwarze 578:
579: /* Temporary buffers typically are not full. */
580:
581: if (0 == start && '\0' == blk.buf[i])
582: break;
583:
584: /* Start the next input line. */
585:
586: pos = 0;
587: }
588:
589: free(ln.buf);
590: }
591:
592: static int
1.18 schwarze 593: read_whole_file(struct mparse *curp, const char *file, int fd,
594: struct buf *fb, int *with_mmap)
1.1 schwarze 595: {
596: struct stat st;
597: size_t off;
598: ssize_t ssz;
599:
600: if (-1 == fstat(fd, &st)) {
1.82 ! schwarze 601: perror(file);
! 602: exit((int)MANDOCLEVEL_SYSERR);
1.1 schwarze 603: }
604:
605: /*
606: * If we're a regular file, try just reading in the whole entry
607: * via mmap(). This is faster than reading it into blocks, and
608: * since each file is only a few bytes to begin with, I'm not
609: * concerned that this is going to tank any machines.
610: */
611:
612: if (S_ISREG(st.st_mode)) {
613: if (st.st_size >= (1U << 31)) {
1.18 schwarze 614: curp->file_status = MANDOCLEVEL_FATAL;
615: if (curp->mmsg)
616: (*curp->mmsg)(MANDOCERR_TOOLARGE,
617: curp->file_status, file, 0, 0, NULL);
1.1 schwarze 618: return(0);
619: }
620: *with_mmap = 1;
621: fb->sz = (size_t)st.st_size;
1.15 schwarze 622: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 schwarze 623: if (fb->buf != MAP_FAILED)
624: return(1);
625: }
626:
627: /*
628: * If this isn't a regular file (like, say, stdin), then we must
629: * go the old way and just read things in bit by bit.
630: */
631:
632: *with_mmap = 0;
633: off = 0;
634: fb->sz = 0;
635: fb->buf = NULL;
636: for (;;) {
637: if (off == fb->sz) {
638: if (fb->sz == (1U << 31)) {
1.18 schwarze 639: curp->file_status = MANDOCLEVEL_FATAL;
640: if (curp->mmsg)
641: (*curp->mmsg)(MANDOCERR_TOOLARGE,
642: curp->file_status,
643: file, 0, 0, NULL);
1.1 schwarze 644: break;
645: }
646: resize_buf(fb, 65536);
647: }
648: ssz = read(fd, fb->buf + (int)off, fb->sz - off);
649: if (ssz == 0) {
650: fb->sz = off;
651: return(1);
652: }
653: if (ssz == -1) {
1.82 ! schwarze 654: perror(file);
! 655: exit((int)MANDOCLEVEL_SYSERR);
1.1 schwarze 656: }
657: off += (size_t)ssz;
658: }
659:
660: free(fb->buf);
661: fb->buf = NULL;
662: return(0);
663: }
664:
665: static void
666: mparse_end(struct mparse *curp)
667: {
668:
669: if (MANDOCLEVEL_FATAL <= curp->file_status)
670: return;
671:
1.50 schwarze 672: if (curp->mdoc == NULL &&
673: curp->man == NULL &&
674: curp->sodest == NULL) {
675: if (curp->options & MPARSE_MDOC)
676: curp->mdoc = curp->pmdoc;
677: else {
678: if (curp->pman == NULL)
1.81 schwarze 679: curp->pman = man_alloc(
680: curp->roff, curp, curp->defos,
1.50 schwarze 681: curp->options & MPARSE_QUICK ? 1 : 0);
682: curp->man = curp->pman;
683: }
684: }
685:
1.1 schwarze 686: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
687: assert(MANDOCLEVEL_FATAL <= curp->file_status);
688: return;
689: }
690:
691: if (curp->man && ! man_endparse(curp->man)) {
692: assert(MANDOCLEVEL_FATAL <= curp->file_status);
693: return;
694: }
695:
696: roff_endparse(curp->roff);
697: }
698:
1.15 schwarze 699: static void
700: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.1 schwarze 701: {
1.61 schwarze 702: struct buf *svprimary;
1.1 schwarze 703: const char *svfile;
1.71 schwarze 704: size_t offset;
1.14 schwarze 705: static int recursion_depth;
706:
707: if (64 < recursion_depth) {
708: mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
1.15 schwarze 709: return;
1.14 schwarze 710: }
1.1 schwarze 711:
1.15 schwarze 712: /* Line number is per-file. */
713: svfile = curp->file;
714: curp->file = file;
1.61 schwarze 715: svprimary = curp->primary;
1.59 schwarze 716: curp->primary = &blk;
1.15 schwarze 717: curp->line = 1;
718: recursion_depth++;
719:
1.70 schwarze 720: /* Skip an UTF-8 byte order mark. */
721: if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
722: (unsigned char)blk.buf[0] == 0xef &&
723: (unsigned char)blk.buf[1] == 0xbb &&
724: (unsigned char)blk.buf[2] == 0xbf) {
1.71 schwarze 725: offset = 3;
1.70 schwarze 726: curp->filenc &= ~MPARSE_LATIN1;
1.71 schwarze 727: } else
728: offset = 0;
1.70 schwarze 729:
1.71 schwarze 730: mparse_buf_r(curp, blk, offset, 1);
1.15 schwarze 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:
1.74 schwarze 739: /*
740: * If a file descriptor is given, use it and assume it points
741: * to the named file. Otherwise, open the named file.
742: * Read the whole file into memory and call the parsers.
743: * Called recursively when an .so request is encountered.
744: */
1.15 schwarze 745: enum mandoclevel
746: mparse_readfd(struct mparse *curp, int fd, const char *file)
747: {
748: struct buf blk;
749: int with_mmap;
1.70 schwarze 750: int save_filenc;
1.74 schwarze 751: pid_t save_child;
1.15 schwarze 752:
1.74 schwarze 753: save_child = curp->child;
754: if (fd != -1)
755: curp->child = 0;
756: else if (mparse_open(curp, &fd, file) >= MANDOCLEVEL_SYSERR)
757: goto out;
1.1 schwarze 758:
1.67 schwarze 759: if (read_whole_file(curp, file, fd, &blk, &with_mmap)) {
1.70 schwarze 760: save_filenc = curp->filenc;
761: curp->filenc = curp->options &
762: (MPARSE_UTF8 | MPARSE_LATIN1);
1.67 schwarze 763: mparse_parse_buffer(curp, blk, file);
1.70 schwarze 764: curp->filenc = save_filenc;
1.67 schwarze 765: if (with_mmap)
766: munmap(blk.buf, blk.sz);
767: else
768: free(blk.buf);
769: }
1.1 schwarze 770:
1.74 schwarze 771: if (fd != STDIN_FILENO && close(fd) == -1)
1.1 schwarze 772: perror(file);
1.67 schwarze 773:
1.74 schwarze 774: mparse_wait(curp);
775: out:
776: curp->child = save_child;
1.1 schwarze 777: return(curp->file_status);
1.58 schwarze 778: }
779:
780: enum mandoclevel
1.73 schwarze 781: mparse_open(struct mparse *curp, int *fd, const char *file)
1.58 schwarze 782: {
783: int pfd[2];
1.74 schwarze 784: int save_errno;
1.58 schwarze 785: char *cp;
786: enum mandocerr err;
787:
788: pfd[1] = -1;
789: curp->file = file;
1.74 schwarze 790:
791: /* Unless zipped, try to just open the file. */
792:
1.58 schwarze 793: if ((cp = strrchr(file, '.')) == NULL ||
794: strcmp(cp + 1, "gz")) {
1.73 schwarze 795: curp->child = 0;
1.74 schwarze 796: if ((*fd = open(file, O_RDONLY)) != -1)
797: return(MANDOCLEVEL_OK);
798:
799: /* Open failed; try to append ".gz". */
800:
801: mandoc_asprintf(&cp, "%s.gz", file);
802: file = cp;
803: } else
804: cp = NULL;
805:
806: /* Before forking, make sure the file can be read. */
807:
808: save_errno = errno;
809: if (access(file, R_OK) == -1) {
810: if (cp != NULL)
811: errno = save_errno;
812: err = MANDOCERR_SYSOPEN;
813: goto out;
1.58 schwarze 814: }
815:
1.74 schwarze 816: /* Run gunzip(1). */
817:
1.58 schwarze 818: if (pipe(pfd) == -1) {
1.82 ! schwarze 819: perror("pipe");
! 820: exit((int)MANDOCLEVEL_SYSERR);
1.58 schwarze 821: }
822:
1.73 schwarze 823: switch (curp->child = fork()) {
1.58 schwarze 824: case -1:
1.82 ! schwarze 825: perror("fork");
! 826: exit((int)MANDOCLEVEL_SYSERR);
1.58 schwarze 827: case 0:
828: close(pfd[0]);
829: if (dup2(pfd[1], STDOUT_FILENO) == -1) {
1.82 ! schwarze 830: perror("dup");
! 831: exit((int)MANDOCLEVEL_SYSERR);
1.58 schwarze 832: }
833: execlp("gunzip", "gunzip", "-c", file, NULL);
1.82 ! schwarze 834: perror("exec");
! 835: exit((int)MANDOCLEVEL_SYSERR);
1.58 schwarze 836: default:
837: close(pfd[1]);
838: *fd = pfd[0];
839: return(MANDOCLEVEL_OK);
840: }
841:
842: out:
1.74 schwarze 843: free(cp);
1.58 schwarze 844: *fd = -1;
1.73 schwarze 845: curp->child = 0;
1.58 schwarze 846: curp->file_status = MANDOCLEVEL_SYSERR;
847: if (curp->mmsg)
1.74 schwarze 848: (*curp->mmsg)(err, curp->file_status, curp->file,
1.58 schwarze 849: 0, 0, strerror(errno));
850: if (pfd[1] != -1)
851: exit(1);
852: return(curp->file_status);
853: }
854:
855: enum mandoclevel
1.73 schwarze 856: mparse_wait(struct mparse *curp)
1.58 schwarze 857: {
858: int status;
859:
1.73 schwarze 860: if (curp->child == 0)
861: return(MANDOCLEVEL_OK);
862:
863: if (waitpid(curp->child, &status, 0) == -1) {
1.82 ! schwarze 864: perror("wait");
! 865: exit((int)MANDOCLEVEL_SYSERR);
1.58 schwarze 866: }
867: if (WIFSIGNALED(status)) {
868: mandoc_vmsg(MANDOCERR_SYSSIG, curp, 0, 0,
869: "%d", WTERMSIG(status));
870: curp->file_status = MANDOCLEVEL_SYSERR;
871: return(curp->file_status);
872: }
873: if (WEXITSTATUS(status)) {
874: mandoc_vmsg(MANDOCERR_SYSEXIT, curp, 0, 0,
875: "%d", WEXITSTATUS(status));
876: curp->file_status = MANDOCLEVEL_SYSERR;
877: return(curp->file_status);
878: }
879: return(MANDOCLEVEL_OK);
1.1 schwarze 880: }
881:
882: struct mparse *
1.69 schwarze 883: mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg,
884: const struct mchars *mchars, const char *defos)
1.1 schwarze 885: {
886: struct mparse *curp;
887:
888: assert(wlevel <= MANDOCLEVEL_FATAL);
889:
890: curp = mandoc_calloc(1, sizeof(struct mparse));
891:
1.22 schwarze 892: curp->options = options;
1.1 schwarze 893: curp->wlevel = wlevel;
894: curp->mmsg = mmsg;
1.7 schwarze 895: curp->defos = defos;
1.1 schwarze 896:
1.69 schwarze 897: curp->mchars = mchars;
898: curp->roff = roff_alloc(curp, curp->mchars, options);
1.50 schwarze 899: if (curp->options & MPARSE_MDOC)
900: curp->pmdoc = mdoc_alloc(
901: curp->roff, curp, curp->defos,
902: curp->options & MPARSE_QUICK ? 1 : 0);
903: if (curp->options & MPARSE_MAN)
1.81 schwarze 904: curp->pman = man_alloc(
905: curp->roff, curp, curp->defos,
1.50 schwarze 906: curp->options & MPARSE_QUICK ? 1 : 0);
907:
1.1 schwarze 908: return(curp);
909: }
910:
911: void
912: mparse_reset(struct mparse *curp)
913: {
914:
915: roff_reset(curp->roff);
916:
917: if (curp->mdoc)
918: mdoc_reset(curp->mdoc);
919: if (curp->man)
920: man_reset(curp->man);
1.4 schwarze 921: if (curp->secondary)
922: curp->secondary->sz = 0;
1.1 schwarze 923:
924: curp->file_status = MANDOCLEVEL_OK;
925: curp->mdoc = NULL;
926: curp->man = NULL;
1.23 schwarze 927:
928: free(curp->sodest);
929: curp->sodest = NULL;
1.1 schwarze 930: }
931:
932: void
933: mparse_free(struct mparse *curp)
934: {
935:
936: if (curp->pmdoc)
937: mdoc_free(curp->pmdoc);
938: if (curp->pman)
939: man_free(curp->pman);
940: if (curp->roff)
941: roff_free(curp->roff);
1.4 schwarze 942: if (curp->secondary)
943: free(curp->secondary->buf);
1.1 schwarze 944:
1.4 schwarze 945: free(curp->secondary);
1.23 schwarze 946: free(curp->sodest);
1.1 schwarze 947: free(curp);
948: }
949:
950: void
1.23 schwarze 951: mparse_result(struct mparse *curp,
952: struct mdoc **mdoc, struct man **man, char **sodest)
1.1 schwarze 953: {
954:
1.23 schwarze 955: if (sodest && NULL != (*sodest = curp->sodest)) {
956: *mdoc = NULL;
957: *man = NULL;
958: return;
959: }
1.1 schwarze 960: if (mdoc)
961: *mdoc = curp->mdoc;
962: if (man)
963: *man = curp->man;
964: }
965:
966: void
967: mandoc_vmsg(enum mandocerr t, struct mparse *m,
968: int ln, int pos, const char *fmt, ...)
969: {
970: char buf[256];
971: va_list ap;
972:
973: va_start(ap, fmt);
1.26 schwarze 974: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.1 schwarze 975: va_end(ap);
976:
977: mandoc_msg(t, m, ln, pos, buf);
978: }
979:
980: void
1.25 schwarze 981: mandoc_msg(enum mandocerr er, struct mparse *m,
1.1 schwarze 982: int ln, int col, const char *msg)
983: {
984: enum mandoclevel level;
985:
986: level = MANDOCLEVEL_FATAL;
987: while (er < mandoclimits[level])
988: level--;
989:
990: if (level < m->wlevel)
991: return;
992:
993: if (m->mmsg)
994: (*m->mmsg)(er, level, m->file, ln, col, msg);
995:
996: if (m->file_status < level)
997: m->file_status = level;
998: }
999:
1000: const char *
1001: mparse_strerror(enum mandocerr er)
1002: {
1003:
1004: return(mandocerrs[er]);
1005: }
1006:
1007: const char *
1008: mparse_strlevel(enum mandoclevel lvl)
1009: {
1010: return(mandoclevels[lvl]);
1.4 schwarze 1011: }
1012:
1013: void
1014: mparse_keep(struct mparse *p)
1015: {
1016:
1017: assert(NULL == p->secondary);
1018: p->secondary = mandoc_calloc(1, sizeof(struct buf));
1019: }
1020:
1021: const char *
1022: mparse_getkeep(const struct mparse *p)
1023: {
1024:
1025: assert(p->secondary);
1026: return(p->secondary->sz ? p->secondary->buf : NULL);
1.1 schwarze 1027: }