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