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