Annotation of src/usr.bin/mandoc/read.c, Revision 1.63
1.63 ! schwarze 1: /* $OpenBSD: read.c,v 1.62 2014/09/07 23:24:33 schwarze Exp $ */
1.1 schwarze 2: /*
3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.18 schwarze 4: * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
1.20 schwarze 5: * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
1.1 schwarze 6: *
7: * Permission to use, copy, modify, and distribute this software for any
8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
10: *
11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18: */
1.58 schwarze 19: #include <sys/types.h>
20: #include <sys/mman.h>
1.1 schwarze 21: #include <sys/stat.h>
1.58 schwarze 22: #include <sys/wait.h>
1.1 schwarze 23:
24: #include <assert.h>
25: #include <ctype.h>
1.18 schwarze 26: #include <errno.h>
1.1 schwarze 27: #include <fcntl.h>
28: #include <stdarg.h>
29: #include <stdio.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <unistd.h>
33:
34: #include "mandoc.h"
1.24 schwarze 35: #include "mandoc_aux.h"
1.1 schwarze 36: #include "libmandoc.h"
37: #include "mdoc.h"
38: #include "man.h"
39:
40: #define REPARSE_LIMIT 1000
41:
42: struct buf {
1.25 schwarze 43: char *buf; /* binary input buffer */
1.1 schwarze 44: size_t sz; /* size of binary buffer */
45: };
46:
47: struct mparse {
48: struct man *pman; /* persistent man parser */
49: struct mdoc *pmdoc; /* persistent mdoc parser */
50: struct man *man; /* man parser */
51: struct mdoc *mdoc; /* mdoc parser */
52: struct roff *roff; /* roff parser (!NULL) */
1.23 schwarze 53: char *sodest; /* filename pointed to by .so */
1.59 schwarze 54: const char *file; /* filename of current input file */
55: struct buf *primary; /* buffer currently being parsed */
56: struct buf *secondary; /* preprocessed copy of input */
57: const char *defos; /* default operating system */
58: mandocmsg mmsg; /* warning/error message handler */
59: enum mandoclevel file_status; /* status of current parse */
60: enum mandoclevel wlevel; /* ignore messages below this */
61: int options; /* parser options */
1.1 schwarze 62: int reparse_count; /* finite interp. stack */
1.59 schwarze 63: int line; /* line number in the file */
1.1 schwarze 64: };
65:
1.60 schwarze 66: static void choose_parser(struct mparse *);
1.1 schwarze 67: static void resize_buf(struct buf *, size_t);
68: static void mparse_buf_r(struct mparse *, struct buf, int);
1.18 schwarze 69: static int read_whole_file(struct mparse *, const char *, int,
70: struct buf *, int *);
1.1 schwarze 71: static void mparse_end(struct mparse *);
1.15 schwarze 72: static void mparse_parse_buffer(struct mparse *, struct buf,
73: const char *);
1.1 schwarze 74:
75: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
76: MANDOCERR_OK,
77: MANDOCERR_WARNING,
78: MANDOCERR_WARNING,
79: MANDOCERR_ERROR,
80: MANDOCERR_FATAL,
81: MANDOCERR_MAX,
82: MANDOCERR_MAX
83: };
84:
85: static const char * const mandocerrs[MANDOCERR_MAX] = {
86: "ok",
87:
88: "generic warning",
89:
90: /* related to the prologue */
1.57 schwarze 91: "missing manual title, using UNTITLED",
92: "missing manual title, using \"\"",
1.32 schwarze 93: "lower case character in document title",
1.57 schwarze 94: "missing manual section, using \"\"",
1.1 schwarze 95: "unknown manual section",
1.11 schwarze 96: "unknown manual volume or arch",
1.32 schwarze 97: "missing date, using today's date",
1.1 schwarze 98: "cannot parse date, using it verbatim",
1.57 schwarze 99: "missing Os macro, using \"\"",
100: "duplicate prologue macro",
101: "late prologue macro",
102: "skipping late title macro",
1.1 schwarze 103: "prologue macros out of order",
104:
105: /* related to document structure */
106: ".so is fragile, better use ln(1)",
1.28 schwarze 107: "no document body",
1.32 schwarze 108: "content before first section header",
109: "first section is not \"NAME\"",
1.1 schwarze 110: "bad NAME section contents",
111: "sections out of conventional order",
1.32 schwarze 112: "duplicate section title",
113: "unexpected section",
1.63 ! schwarze 114: "unusual Xr order",
! 115: "unusual Xr punctuation",
1.62 schwarze 116: "AUTHORS section without An macro",
1.1 schwarze 117:
118: /* related to macros and nesting */
1.33 schwarze 119: "obsolete macro",
1.1 schwarze 120: "skipping paragraph macro",
1.10 schwarze 121: "moving paragraph macro out of list",
1.1 schwarze 122: "skipping no-space macro",
123: "blocks badly nested",
124: "nested displays are not portable",
1.35 schwarze 125: "moving content out of list",
126: ".Vt block has child macro",
1.56 schwarze 127: "fill mode already enabled, skipping",
128: "fill mode already disabled, skipping",
1.1 schwarze 129: "line scope broken",
130:
131: /* related to missing macro arguments */
1.36 schwarze 132: "skipping empty request",
133: "conditional request controls empty scope",
1.1 schwarze 134: "skipping empty macro",
1.40 schwarze 135: "empty argument, using 0n",
1.1 schwarze 136: "argument count wrong",
1.38 schwarze 137: "missing display type, using -ragged",
138: "list type is not the first argument",
139: "missing -width in -tag list, using 8n",
1.56 schwarze 140: "missing utility name, using \"\"",
1.38 schwarze 141: "empty head in list item",
142: "empty list item",
1.39 schwarze 143: "missing font type, using \\fR",
144: "unknown font type, using \\fR",
1.38 schwarze 145: "missing -std argument, adding it",
1.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.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: "equation syntax error",
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.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: /*
300: * Main parse routine for an opened file. This is called for each
301: * opened file and simply loops around the full input file, possibly
302: * nesting (i.e., with `so').
303: */
304: static void
305: mparse_buf_r(struct mparse *curp, struct buf blk, int start)
306: {
307: const struct tbl_span *span;
308: struct buf ln;
309: enum rofferr rr;
310: int i, of, rc;
311: int pos; /* byte number in the ln buffer */
312: int lnn; /* line number in the real file */
313: unsigned char c;
314:
315: memset(&ln, 0, sizeof(struct buf));
316:
1.25 schwarze 317: lnn = curp->line;
318: pos = 0;
1.1 schwarze 319:
320: for (i = 0; i < (int)blk.sz; ) {
321: if (0 == pos && '\0' == blk.buf[i])
322: break;
323:
324: if (start) {
325: curp->line = lnn;
326: curp->reparse_count = 0;
327: }
328:
329: while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
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:
337: if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
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: /*
347: * Make sure we have space for at least
348: * one backslash and one other character
349: * and the trailing NUL byte.
350: */
351:
352: if (pos + 2 >= (int)ln.sz)
353: resize_buf(&ln, 256);
354:
1.25 schwarze 355: /*
1.1 schwarze 356: * Warn about bogus characters. If you're using
357: * non-ASCII encoding, you're screwing your
358: * readers. Since I'd rather this not happen,
1.6 schwarze 359: * I'll be helpful and replace these characters
360: * with "?", so we don't display gibberish.
361: * Note to manual writers: use special characters.
1.1 schwarze 362: */
363:
364: c = (unsigned char) blk.buf[i];
365:
1.25 schwarze 366: if ( ! (isascii(c) &&
367: (isgraph(c) || isblank(c)))) {
1.56 schwarze 368: mandoc_vmsg(MANDOCERR_BADCHAR, curp,
369: curp->line, pos, "0x%x", c);
1.1 schwarze 370: i++;
1.6 schwarze 371: ln.buf[pos++] = '?';
1.1 schwarze 372: continue;
373: }
374:
375: /* Trailing backslash = a plain char. */
376:
377: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
378: ln.buf[pos++] = blk.buf[i++];
379: continue;
380: }
381:
382: /*
383: * Found escape and at least one other character.
384: * When it's a newline character, skip it.
385: * When there is a carriage return in between,
386: * skip that one as well.
387: */
388:
389: if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
390: '\n' == blk.buf[i + 2])
391: ++i;
392: if ('\n' == blk.buf[i + 1]) {
393: i += 2;
394: ++lnn;
395: continue;
396: }
397:
398: if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
399: i += 2;
400: /* Comment, skip to end of line */
401: for (; i < (int)blk.sz; ++i) {
402: if ('\n' == blk.buf[i]) {
403: ++i;
404: ++lnn;
405: break;
406: }
407: }
408:
409: /* Backout trailing whitespaces */
410: for (; pos > 0; --pos) {
411: if (ln.buf[pos - 1] != ' ')
412: break;
413: if (pos > 2 && ln.buf[pos - 2] == '\\')
414: break;
415: }
416: break;
417: }
418:
1.13 schwarze 419: /* Catch escaped bogus characters. */
420:
421: c = (unsigned char) blk.buf[i+1];
422:
1.25 schwarze 423: if ( ! (isascii(c) &&
424: (isgraph(c) || isblank(c)))) {
1.56 schwarze 425: mandoc_vmsg(MANDOCERR_BADCHAR, curp,
426: curp->line, pos, "0x%x", c);
1.13 schwarze 427: i += 2;
428: ln.buf[pos++] = '?';
429: continue;
430: }
431:
1.1 schwarze 432: /* Some other escape sequence, copy & cont. */
433:
434: ln.buf[pos++] = blk.buf[i++];
435: ln.buf[pos++] = blk.buf[i++];
436: }
437:
1.25 schwarze 438: if (pos >= (int)ln.sz)
1.1 schwarze 439: resize_buf(&ln, 256);
440:
441: ln.buf[pos] = '\0';
442:
443: /*
444: * A significant amount of complexity is contained by
445: * the roff preprocessor. It's line-oriented but can be
446: * expressed on one line, so we need at times to
447: * readjust our starting point and re-run it. The roff
448: * preprocessor can also readjust the buffers with new
449: * data, so we pass them in wholesale.
450: */
451:
452: of = 0;
453:
1.4 schwarze 454: /*
455: * Maintain a lookaside buffer of all parsed lines. We
456: * only do this if mparse_keep() has been invoked (the
457: * buffer may be accessed with mparse_getkeep()).
458: */
459:
460: if (curp->secondary) {
1.25 schwarze 461: curp->secondary->buf = mandoc_realloc(
462: curp->secondary->buf,
463: curp->secondary->sz + pos + 2);
464: memcpy(curp->secondary->buf +
465: curp->secondary->sz,
466: ln.buf, pos);
1.4 schwarze 467: curp->secondary->sz += pos;
468: curp->secondary->buf
469: [curp->secondary->sz] = '\n';
470: curp->secondary->sz++;
471: curp->secondary->buf
472: [curp->secondary->sz] = '\0';
473: }
1.1 schwarze 474: rerun:
1.25 schwarze 475: rr = roff_parseln(curp->roff, curp->line,
476: &ln.buf, &ln.sz, of, &of);
1.1 schwarze 477:
478: switch (rr) {
1.25 schwarze 479: case ROFF_REPARSE:
1.1 schwarze 480: if (REPARSE_LIMIT >= ++curp->reparse_count)
481: mparse_buf_r(curp, ln, 0);
482: else
483: mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.25 schwarze 484: curp->line, pos, NULL);
1.1 schwarze 485: pos = 0;
486: continue;
1.25 schwarze 487: case ROFF_APPEND:
1.1 schwarze 488: pos = (int)strlen(ln.buf);
489: continue;
1.25 schwarze 490: case ROFF_RERUN:
1.1 schwarze 491: goto rerun;
1.25 schwarze 492: case ROFF_IGN:
1.1 schwarze 493: pos = 0;
494: continue;
1.25 schwarze 495: case ROFF_ERR:
1.1 schwarze 496: assert(MANDOCLEVEL_FATAL <= curp->file_status);
497: break;
1.25 schwarze 498: case ROFF_SO:
1.23 schwarze 499: if (0 == (MPARSE_SO & curp->options) &&
500: (i >= (int)blk.sz || '\0' == blk.buf[i])) {
501: curp->sodest = mandoc_strdup(ln.buf + of);
502: free(ln.buf);
503: return;
504: }
1.4 schwarze 505: /*
506: * We remove `so' clauses from our lookaside
507: * buffer because we're going to descend into
508: * the file recursively.
509: */
1.25 schwarze 510: if (curp->secondary)
1.4 schwarze 511: curp->secondary->sz -= pos + 1;
1.14 schwarze 512: mparse_readfd(curp, -1, ln.buf + of);
1.30 schwarze 513: if (MANDOCLEVEL_FATAL <= curp->file_status) {
514: mandoc_vmsg(MANDOCERR_SO_FAIL,
515: curp, curp->line, pos,
516: ".so %s", ln.buf + of);
1.1 schwarze 517: break;
1.30 schwarze 518: }
1.1 schwarze 519: pos = 0;
520: continue;
521: default:
522: break;
523: }
524:
525: /*
526: * If we encounter errors in the recursive parse, make
527: * sure we don't continue parsing.
528: */
529:
530: if (MANDOCLEVEL_FATAL <= curp->file_status)
531: break;
532:
533: /*
534: * If input parsers have not been allocated, do so now.
1.2 schwarze 535: * We keep these instanced between parsers, but set them
1.1 schwarze 536: * locally per parse routine since we can use different
537: * parsers with each one.
538: */
539:
540: if ( ! (curp->man || curp->mdoc))
1.60 schwarze 541: choose_parser(curp);
1.1 schwarze 542:
1.25 schwarze 543: /*
1.60 schwarze 544: * Lastly, push down into the parsers themselves.
1.1 schwarze 545: * If libroff returns ROFF_TBL, then add it to the
546: * currently open parse. Since we only get here if
547: * there does exist data (see tbl_data.c), we're
548: * guaranteed that something's been allocated.
549: * Do the same for ROFF_EQN.
550: */
551:
552: rc = -1;
553:
554: if (ROFF_TBL == rr)
555: while (NULL != (span = roff_span(curp->roff))) {
556: rc = curp->man ?
1.25 schwarze 557: man_addspan(curp->man, span) :
558: mdoc_addspan(curp->mdoc, span);
1.1 schwarze 559: if (0 == rc)
560: break;
561: }
562: else if (ROFF_EQN == rr)
1.25 schwarze 563: rc = curp->mdoc ?
564: mdoc_addeqn(curp->mdoc,
565: roff_eqn(curp->roff)) :
566: man_addeqn(curp->man,
567: roff_eqn(curp->roff));
1.1 schwarze 568: else if (curp->man || curp->mdoc)
569: rc = curp->man ?
1.25 schwarze 570: man_parseln(curp->man,
571: curp->line, ln.buf, of) :
572: mdoc_parseln(curp->mdoc,
573: curp->line, ln.buf, of);
1.1 schwarze 574:
575: if (0 == rc) {
576: assert(MANDOCLEVEL_FATAL <= curp->file_status);
577: break;
1.19 schwarze 578: } else if (2 == rc)
579: break;
1.1 schwarze 580:
581: /* Temporary buffers typically are not full. */
582:
583: if (0 == start && '\0' == blk.buf[i])
584: break;
585:
586: /* Start the next input line. */
587:
588: pos = 0;
589: }
590:
591: free(ln.buf);
592: }
593:
594: static int
1.18 schwarze 595: read_whole_file(struct mparse *curp, const char *file, int fd,
596: struct buf *fb, int *with_mmap)
1.1 schwarze 597: {
598: struct stat st;
599: size_t off;
600: ssize_t ssz;
601:
602: if (-1 == fstat(fd, &st)) {
1.18 schwarze 603: curp->file_status = MANDOCLEVEL_SYSERR;
604: if (curp->mmsg)
605: (*curp->mmsg)(MANDOCERR_SYSSTAT, curp->file_status,
606: file, 0, 0, strerror(errno));
1.1 schwarze 607: return(0);
608: }
609:
610: /*
611: * If we're a regular file, try just reading in the whole entry
612: * via mmap(). This is faster than reading it into blocks, and
613: * since each file is only a few bytes to begin with, I'm not
614: * concerned that this is going to tank any machines.
615: */
616:
617: if (S_ISREG(st.st_mode)) {
618: if (st.st_size >= (1U << 31)) {
1.18 schwarze 619: curp->file_status = MANDOCLEVEL_FATAL;
620: if (curp->mmsg)
621: (*curp->mmsg)(MANDOCERR_TOOLARGE,
622: curp->file_status, file, 0, 0, NULL);
1.1 schwarze 623: return(0);
624: }
625: *with_mmap = 1;
626: fb->sz = (size_t)st.st_size;
1.15 schwarze 627: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 schwarze 628: if (fb->buf != MAP_FAILED)
629: return(1);
630: }
631:
632: /*
633: * If this isn't a regular file (like, say, stdin), then we must
634: * go the old way and just read things in bit by bit.
635: */
636:
637: *with_mmap = 0;
638: off = 0;
639: fb->sz = 0;
640: fb->buf = NULL;
641: for (;;) {
642: if (off == fb->sz) {
643: if (fb->sz == (1U << 31)) {
1.18 schwarze 644: curp->file_status = MANDOCLEVEL_FATAL;
645: if (curp->mmsg)
646: (*curp->mmsg)(MANDOCERR_TOOLARGE,
647: curp->file_status,
648: file, 0, 0, NULL);
1.1 schwarze 649: break;
650: }
651: resize_buf(fb, 65536);
652: }
653: ssz = read(fd, fb->buf + (int)off, fb->sz - off);
654: if (ssz == 0) {
655: fb->sz = off;
656: return(1);
657: }
658: if (ssz == -1) {
1.18 schwarze 659: curp->file_status = MANDOCLEVEL_SYSERR;
660: if (curp->mmsg)
661: (*curp->mmsg)(MANDOCERR_SYSREAD,
662: curp->file_status, file, 0, 0,
663: strerror(errno));
1.1 schwarze 664: break;
665: }
666: off += (size_t)ssz;
667: }
668:
669: free(fb->buf);
670: fb->buf = NULL;
671: return(0);
672: }
673:
674: static void
675: mparse_end(struct mparse *curp)
676: {
677:
678: if (MANDOCLEVEL_FATAL <= curp->file_status)
679: return;
680:
1.50 schwarze 681: if (curp->mdoc == NULL &&
682: curp->man == NULL &&
683: curp->sodest == NULL) {
684: if (curp->options & MPARSE_MDOC)
685: curp->mdoc = curp->pmdoc;
686: else {
687: if (curp->pman == NULL)
688: curp->pman = man_alloc(curp->roff, curp,
689: curp->options & MPARSE_QUICK ? 1 : 0);
690: curp->man = curp->pman;
691: }
692: }
693:
1.1 schwarze 694: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
695: assert(MANDOCLEVEL_FATAL <= curp->file_status);
696: return;
697: }
698:
699: if (curp->man && ! man_endparse(curp->man)) {
700: assert(MANDOCLEVEL_FATAL <= curp->file_status);
701: return;
702: }
703:
704: roff_endparse(curp->roff);
705: }
706:
1.15 schwarze 707: static void
708: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.1 schwarze 709: {
1.61 schwarze 710: struct buf *svprimary;
1.1 schwarze 711: const char *svfile;
1.14 schwarze 712: static int recursion_depth;
713:
714: if (64 < recursion_depth) {
715: mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
1.15 schwarze 716: return;
1.14 schwarze 717: }
1.1 schwarze 718:
1.15 schwarze 719: /* Line number is per-file. */
720: svfile = curp->file;
721: curp->file = file;
1.61 schwarze 722: svprimary = curp->primary;
1.59 schwarze 723: curp->primary = &blk;
1.15 schwarze 724: curp->line = 1;
725: recursion_depth++;
726:
727: mparse_buf_r(curp, blk, 1);
728:
729: if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status)
730: mparse_end(curp);
731:
1.61 schwarze 732: curp->primary = svprimary;
1.15 schwarze 733: curp->file = svfile;
734: }
735:
736: enum mandoclevel
737: mparse_readfd(struct mparse *curp, int fd, const char *file)
738: {
739: struct buf blk;
740: int with_mmap;
741:
1.18 schwarze 742: if (-1 == fd && -1 == (fd = open(file, O_RDONLY, 0))) {
743: curp->file_status = MANDOCLEVEL_SYSERR;
744: if (curp->mmsg)
745: (*curp->mmsg)(MANDOCERR_SYSOPEN,
746: curp->file_status,
747: file, 0, 0, strerror(errno));
748: goto out;
749: }
750:
1.15 schwarze 751: /*
752: * Run for each opened file; may be called more than once for
753: * each full parse sequence if the opened file is nested (i.e.,
754: * from `so'). Simply sucks in the whole file and moves into
755: * the parse phase for the file.
756: */
1.1 schwarze 757:
1.18 schwarze 758: if ( ! read_whole_file(curp, file, fd, &blk, &with_mmap))
1.15 schwarze 759: goto out;
1.1 schwarze 760:
1.15 schwarze 761: mparse_parse_buffer(curp, blk, file);
1.1 schwarze 762:
1.15 schwarze 763: if (with_mmap)
764: munmap(blk.buf, blk.sz);
765: else
766: free(blk.buf);
1.1 schwarze 767:
768: if (STDIN_FILENO != fd && -1 == close(fd))
769: perror(file);
1.14 schwarze 770: out:
1.1 schwarze 771: return(curp->file_status);
1.58 schwarze 772: }
773:
774: enum mandoclevel
775: mparse_open(struct mparse *curp, int *fd, const char *file,
776: pid_t *child_pid)
777: {
778: int pfd[2];
779: char *cp;
780: enum mandocerr err;
781:
782: pfd[1] = -1;
783: curp->file = file;
784: if ((cp = strrchr(file, '.')) == NULL ||
785: strcmp(cp + 1, "gz")) {
786: *child_pid = 0;
787: if ((*fd = open(file, O_RDONLY)) == -1) {
788: err = MANDOCERR_SYSOPEN;
789: goto out;
790: }
791: return(MANDOCLEVEL_OK);
792: }
793:
794: if (pipe(pfd) == -1) {
795: err = MANDOCERR_SYSPIPE;
796: goto out;
797: }
798:
799: switch (*child_pid = fork()) {
800: case -1:
801: err = MANDOCERR_SYSFORK;
802: close(pfd[0]);
803: close(pfd[1]);
804: pfd[1] = -1;
805: break;
806: case 0:
807: close(pfd[0]);
808: if (dup2(pfd[1], STDOUT_FILENO) == -1) {
809: err = MANDOCERR_SYSDUP;
810: break;
811: }
812: execlp("gunzip", "gunzip", "-c", file, NULL);
813: err = MANDOCERR_SYSEXEC;
814: break;
815: default:
816: close(pfd[1]);
817: *fd = pfd[0];
818: return(MANDOCLEVEL_OK);
819: }
820:
821: out:
822: *fd = -1;
823: *child_pid = 0;
824: curp->file_status = MANDOCLEVEL_SYSERR;
825: if (curp->mmsg)
826: (*curp->mmsg)(err, curp->file_status, file,
827: 0, 0, strerror(errno));
828: if (pfd[1] != -1)
829: exit(1);
830: return(curp->file_status);
831: }
832:
833: enum mandoclevel
834: mparse_wait(struct mparse *curp, pid_t child_pid)
835: {
836: int status;
837:
838: if (waitpid(child_pid, &status, 0) == -1) {
839: mandoc_msg(MANDOCERR_SYSWAIT, curp, 0, 0,
840: strerror(errno));
841: curp->file_status = MANDOCLEVEL_SYSERR;
842: return(curp->file_status);
843: }
844: if (WIFSIGNALED(status)) {
845: mandoc_vmsg(MANDOCERR_SYSSIG, curp, 0, 0,
846: "%d", WTERMSIG(status));
847: curp->file_status = MANDOCLEVEL_SYSERR;
848: return(curp->file_status);
849: }
850: if (WEXITSTATUS(status)) {
851: mandoc_vmsg(MANDOCERR_SYSEXIT, curp, 0, 0,
852: "%d", WEXITSTATUS(status));
853: curp->file_status = MANDOCLEVEL_SYSERR;
854: return(curp->file_status);
855: }
856: return(MANDOCLEVEL_OK);
1.1 schwarze 857: }
858:
859: struct mparse *
1.22 schwarze 860: mparse_alloc(int options, enum mandoclevel wlevel,
1.47 schwarze 861: mandocmsg mmsg, const char *defos)
1.1 schwarze 862: {
863: struct mparse *curp;
864:
865: assert(wlevel <= MANDOCLEVEL_FATAL);
866:
867: curp = mandoc_calloc(1, sizeof(struct mparse));
868:
1.22 schwarze 869: curp->options = options;
1.1 schwarze 870: curp->wlevel = wlevel;
871: curp->mmsg = mmsg;
1.7 schwarze 872: curp->defos = defos;
1.1 schwarze 873:
1.22 schwarze 874: curp->roff = roff_alloc(curp, options);
1.50 schwarze 875: if (curp->options & MPARSE_MDOC)
876: curp->pmdoc = mdoc_alloc(
877: curp->roff, curp, curp->defos,
878: curp->options & MPARSE_QUICK ? 1 : 0);
879: if (curp->options & MPARSE_MAN)
880: curp->pman = man_alloc(curp->roff, curp,
881: curp->options & MPARSE_QUICK ? 1 : 0);
882:
1.1 schwarze 883: return(curp);
884: }
885:
886: void
887: mparse_reset(struct mparse *curp)
888: {
889:
890: roff_reset(curp->roff);
891:
892: if (curp->mdoc)
893: mdoc_reset(curp->mdoc);
894: if (curp->man)
895: man_reset(curp->man);
1.4 schwarze 896: if (curp->secondary)
897: curp->secondary->sz = 0;
1.1 schwarze 898:
899: curp->file_status = MANDOCLEVEL_OK;
900: curp->mdoc = NULL;
901: curp->man = NULL;
1.23 schwarze 902:
903: free(curp->sodest);
904: curp->sodest = NULL;
1.1 schwarze 905: }
906:
907: void
908: mparse_free(struct mparse *curp)
909: {
910:
911: if (curp->pmdoc)
912: mdoc_free(curp->pmdoc);
913: if (curp->pman)
914: man_free(curp->pman);
915: if (curp->roff)
916: roff_free(curp->roff);
1.4 schwarze 917: if (curp->secondary)
918: free(curp->secondary->buf);
1.1 schwarze 919:
1.4 schwarze 920: free(curp->secondary);
1.23 schwarze 921: free(curp->sodest);
1.1 schwarze 922: free(curp);
923: }
924:
925: void
1.23 schwarze 926: mparse_result(struct mparse *curp,
927: struct mdoc **mdoc, struct man **man, char **sodest)
1.1 schwarze 928: {
929:
1.23 schwarze 930: if (sodest && NULL != (*sodest = curp->sodest)) {
931: *mdoc = NULL;
932: *man = NULL;
933: return;
934: }
1.1 schwarze 935: if (mdoc)
936: *mdoc = curp->mdoc;
937: if (man)
938: *man = curp->man;
939: }
940:
941: void
942: mandoc_vmsg(enum mandocerr t, struct mparse *m,
943: int ln, int pos, const char *fmt, ...)
944: {
945: char buf[256];
946: va_list ap;
947:
948: va_start(ap, fmt);
1.26 schwarze 949: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.1 schwarze 950: va_end(ap);
951:
952: mandoc_msg(t, m, ln, pos, buf);
953: }
954:
955: void
1.25 schwarze 956: mandoc_msg(enum mandocerr er, struct mparse *m,
1.1 schwarze 957: int ln, int col, const char *msg)
958: {
959: enum mandoclevel level;
960:
961: level = MANDOCLEVEL_FATAL;
962: while (er < mandoclimits[level])
963: level--;
964:
965: if (level < m->wlevel)
966: return;
967:
968: if (m->mmsg)
969: (*m->mmsg)(er, level, m->file, ln, col, msg);
970:
971: if (m->file_status < level)
972: m->file_status = level;
973: }
974:
975: const char *
976: mparse_strerror(enum mandocerr er)
977: {
978:
979: return(mandocerrs[er]);
980: }
981:
982: const char *
983: mparse_strlevel(enum mandoclevel lvl)
984: {
985: return(mandoclevels[lvl]);
1.4 schwarze 986: }
987:
988: void
989: mparse_keep(struct mparse *p)
990: {
991:
992: assert(NULL == p->secondary);
993: p->secondary = mandoc_calloc(1, sizeof(struct buf));
994: }
995:
996: const char *
997: mparse_getkeep(const struct mparse *p)
998: {
999:
1000: assert(p->secondary);
1001: return(p->secondary->sz ? p->secondary->buf : NULL);
1.1 schwarze 1002: }