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