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