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