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