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