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