Annotation of src/usr.bin/mandoc/read.c, Revision 1.27
1.27 ! schwarze 1: /* $Id: read.c,v 1.26 2014/04/20 22:03:40 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.27 ! schwarze 88: "no TH macro in document",
1.1 schwarze 89: "document title should be all caps",
90: "unknown manual section",
1.11 schwarze 91: "unknown manual volume or arch",
1.1 schwarze 92: "date missing, using today's date",
93: "cannot parse date, using it verbatim",
94: "prologue macros out of order",
95: "duplicate prologue macro",
96: "macro not allowed in prologue",
97: "macro not allowed in body",
98:
99: /* related to document structure */
100: ".so is fragile, better use ln(1)",
101: "NAME section must come first",
102: "bad NAME section contents",
103: "sections out of conventional order",
104: "duplicate section name",
1.17 schwarze 105: "section header suited to sections 2, 3, and 9 only",
1.1 schwarze 106:
107: /* related to macros and nesting */
108: "skipping obsolete macro",
109: "skipping paragraph macro",
1.10 schwarze 110: "moving paragraph macro out of list",
1.1 schwarze 111: "skipping no-space macro",
112: "blocks badly nested",
113: "child violates parent syntax",
114: "nested displays are not portable",
115: "already in literal mode",
116: "line scope broken",
117:
118: /* related to missing macro arguments */
119: "skipping empty macro",
120: "argument count wrong",
121: "missing display type",
122: "list type must come first",
123: "tag lists require a width argument",
124: "missing font type",
125: "skipping end of block that is not open",
126:
127: /* related to bad macro arguments */
128: "skipping argument",
129: "duplicate argument",
130: "duplicate display type",
131: "duplicate list type",
132: "unknown AT&T UNIX version",
133: "bad Boolean value",
134: "unknown font",
135: "unknown standard specifier",
136: "bad width argument",
137:
138: /* related to plain text */
139: "blank line in non-literal context",
140: "tab in non-literal context",
141: "end of line whitespace",
142: "bad comment style",
1.2 schwarze 143: "bad escape sequence",
1.1 schwarze 144: "unterminated quoted string",
1.3 schwarze 145:
146: /* related to equations */
147: "unexpected literal in equation",
1.25 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.1 schwarze 172: "skipping text before the first section header",
173: "skipping unknown macro",
174: "NOT IMPLEMENTED, please use groff: skipping request",
175: "argument count wrong",
1.12 schwarze 176: "skipping column outside column list",
1.1 schwarze 177: "skipping end of block that is not open",
178: "missing end of block",
179: "scope open on exit",
180: "uname(3) system call failed",
181: "macro requires line argument(s)",
182: "macro requires body argument(s)",
183: "macro requires argument(s)",
1.16 schwarze 184: "request requires a numeric argument",
1.1 schwarze 185: "missing list type",
186: "line argument(s) will be lost",
187: "body 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 \"..\"",
199: "no document body",
200: "no document prologue",
201: "static buffer exhausted",
1.18 schwarze 202:
203: /* system errors */
204: "cannot open file",
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.1 schwarze 500: if (MANDOCLEVEL_FATAL <= curp->file_status)
501: break;
502: pos = 0;
503: continue;
504: default:
505: break;
506: }
507:
508: /*
509: * If we encounter errors in the recursive parse, make
510: * sure we don't continue parsing.
511: */
512:
513: if (MANDOCLEVEL_FATAL <= curp->file_status)
514: break;
515:
516: /*
517: * If input parsers have not been allocated, do so now.
1.2 schwarze 518: * We keep these instanced between parsers, but set them
1.1 schwarze 519: * locally per parse routine since we can use different
520: * parsers with each one.
521: */
522:
523: if ( ! (curp->man || curp->mdoc))
524: pset(ln.buf + of, pos - of, curp);
525:
1.25 schwarze 526: /*
1.1 schwarze 527: * Lastly, push down into the parsers themselves. One
528: * of these will have already been set in the pset()
529: * routine.
530: * If libroff returns ROFF_TBL, then add it to the
531: * currently open parse. Since we only get here if
532: * there does exist data (see tbl_data.c), we're
533: * guaranteed that something's been allocated.
534: * Do the same for ROFF_EQN.
535: */
536:
537: rc = -1;
538:
539: if (ROFF_TBL == rr)
540: while (NULL != (span = roff_span(curp->roff))) {
541: rc = curp->man ?
1.25 schwarze 542: man_addspan(curp->man, span) :
543: mdoc_addspan(curp->mdoc, span);
1.1 schwarze 544: if (0 == rc)
545: break;
546: }
547: else if (ROFF_EQN == rr)
1.25 schwarze 548: rc = curp->mdoc ?
549: mdoc_addeqn(curp->mdoc,
550: roff_eqn(curp->roff)) :
551: man_addeqn(curp->man,
552: roff_eqn(curp->roff));
1.1 schwarze 553: else if (curp->man || curp->mdoc)
554: rc = curp->man ?
1.25 schwarze 555: man_parseln(curp->man,
556: curp->line, ln.buf, of) :
557: mdoc_parseln(curp->mdoc,
558: curp->line, ln.buf, of);
1.1 schwarze 559:
560: if (0 == rc) {
561: assert(MANDOCLEVEL_FATAL <= curp->file_status);
562: break;
1.19 schwarze 563: } else if (2 == rc)
564: break;
1.1 schwarze 565:
566: /* Temporary buffers typically are not full. */
567:
568: if (0 == start && '\0' == blk.buf[i])
569: break;
570:
571: /* Start the next input line. */
572:
573: pos = 0;
574: }
575:
576: free(ln.buf);
577: }
578:
579: static int
1.18 schwarze 580: read_whole_file(struct mparse *curp, const char *file, int fd,
581: struct buf *fb, int *with_mmap)
1.1 schwarze 582: {
583: struct stat st;
584: size_t off;
585: ssize_t ssz;
586:
587: if (-1 == fstat(fd, &st)) {
1.18 schwarze 588: curp->file_status = MANDOCLEVEL_SYSERR;
589: if (curp->mmsg)
590: (*curp->mmsg)(MANDOCERR_SYSSTAT, curp->file_status,
591: file, 0, 0, strerror(errno));
1.1 schwarze 592: return(0);
593: }
594:
595: /*
596: * If we're a regular file, try just reading in the whole entry
597: * via mmap(). This is faster than reading it into blocks, and
598: * since each file is only a few bytes to begin with, I'm not
599: * concerned that this is going to tank any machines.
600: */
601:
602: if (S_ISREG(st.st_mode)) {
603: if (st.st_size >= (1U << 31)) {
1.18 schwarze 604: curp->file_status = MANDOCLEVEL_FATAL;
605: if (curp->mmsg)
606: (*curp->mmsg)(MANDOCERR_TOOLARGE,
607: curp->file_status, file, 0, 0, NULL);
1.1 schwarze 608: return(0);
609: }
610: *with_mmap = 1;
611: fb->sz = (size_t)st.st_size;
1.15 schwarze 612: fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1 schwarze 613: if (fb->buf != MAP_FAILED)
614: return(1);
615: }
616:
617: /*
618: * If this isn't a regular file (like, say, stdin), then we must
619: * go the old way and just read things in bit by bit.
620: */
621:
622: *with_mmap = 0;
623: off = 0;
624: fb->sz = 0;
625: fb->buf = NULL;
626: for (;;) {
627: if (off == fb->sz) {
628: if (fb->sz == (1U << 31)) {
1.18 schwarze 629: curp->file_status = MANDOCLEVEL_FATAL;
630: if (curp->mmsg)
631: (*curp->mmsg)(MANDOCERR_TOOLARGE,
632: curp->file_status,
633: file, 0, 0, NULL);
1.1 schwarze 634: break;
635: }
636: resize_buf(fb, 65536);
637: }
638: ssz = read(fd, fb->buf + (int)off, fb->sz - off);
639: if (ssz == 0) {
640: fb->sz = off;
641: return(1);
642: }
643: if (ssz == -1) {
1.18 schwarze 644: curp->file_status = MANDOCLEVEL_SYSERR;
645: if (curp->mmsg)
646: (*curp->mmsg)(MANDOCERR_SYSREAD,
647: curp->file_status, file, 0, 0,
648: strerror(errno));
1.1 schwarze 649: break;
650: }
651: off += (size_t)ssz;
652: }
653:
654: free(fb->buf);
655: fb->buf = NULL;
656: return(0);
657: }
658:
659: static void
660: mparse_end(struct mparse *curp)
661: {
662:
663: if (MANDOCLEVEL_FATAL <= curp->file_status)
664: return;
665:
666: if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
667: assert(MANDOCLEVEL_FATAL <= curp->file_status);
668: return;
669: }
670:
671: if (curp->man && ! man_endparse(curp->man)) {
672: assert(MANDOCLEVEL_FATAL <= curp->file_status);
673: return;
674: }
675:
1.23 schwarze 676: if ( ! (curp->mdoc || curp->man || curp->sodest)) {
1.1 schwarze 677: mandoc_msg(MANDOCERR_NOTMANUAL, curp, 1, 0, NULL);
678: curp->file_status = MANDOCLEVEL_FATAL;
679: return;
680: }
681:
682: roff_endparse(curp->roff);
683: }
684:
1.15 schwarze 685: static void
686: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.1 schwarze 687: {
688: const char *svfile;
1.14 schwarze 689: static int recursion_depth;
690:
691: if (64 < recursion_depth) {
692: mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
1.15 schwarze 693: return;
1.14 schwarze 694: }
1.1 schwarze 695:
1.15 schwarze 696: /* Line number is per-file. */
697: svfile = curp->file;
698: curp->file = file;
699: curp->line = 1;
700: recursion_depth++;
701:
702: mparse_buf_r(curp, blk, 1);
703:
704: if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status)
705: mparse_end(curp);
706:
707: curp->file = svfile;
708: }
709:
710: enum mandoclevel
711: mparse_readfd(struct mparse *curp, int fd, const char *file)
712: {
713: struct buf blk;
714: int with_mmap;
715:
1.18 schwarze 716: if (-1 == fd && -1 == (fd = open(file, O_RDONLY, 0))) {
717: curp->file_status = MANDOCLEVEL_SYSERR;
718: if (curp->mmsg)
719: (*curp->mmsg)(MANDOCERR_SYSOPEN,
720: curp->file_status,
721: file, 0, 0, strerror(errno));
722: goto out;
723: }
724:
1.15 schwarze 725: /*
726: * Run for each opened file; may be called more than once for
727: * each full parse sequence if the opened file is nested (i.e.,
728: * from `so'). Simply sucks in the whole file and moves into
729: * the parse phase for the file.
730: */
1.1 schwarze 731:
1.18 schwarze 732: if ( ! read_whole_file(curp, file, fd, &blk, &with_mmap))
1.15 schwarze 733: goto out;
1.1 schwarze 734:
1.15 schwarze 735: mparse_parse_buffer(curp, blk, file);
1.1 schwarze 736:
1.15 schwarze 737: if (with_mmap)
738: munmap(blk.buf, blk.sz);
739: else
740: free(blk.buf);
1.1 schwarze 741:
742: if (STDIN_FILENO != fd && -1 == close(fd))
743: perror(file);
1.14 schwarze 744: out:
1.1 schwarze 745: return(curp->file_status);
746: }
747:
748: struct mparse *
1.22 schwarze 749: mparse_alloc(int options, enum mandoclevel wlevel,
750: mandocmsg mmsg, char *defos)
1.1 schwarze 751: {
752: struct mparse *curp;
753:
754: assert(wlevel <= MANDOCLEVEL_FATAL);
755:
756: curp = mandoc_calloc(1, sizeof(struct mparse));
757:
1.22 schwarze 758: curp->options = options;
1.1 schwarze 759: curp->wlevel = wlevel;
760: curp->mmsg = mmsg;
1.7 schwarze 761: curp->defos = defos;
1.1 schwarze 762:
1.22 schwarze 763: curp->roff = roff_alloc(curp, options);
1.1 schwarze 764: return(curp);
765: }
766:
767: void
768: mparse_reset(struct mparse *curp)
769: {
770:
771: roff_reset(curp->roff);
772:
773: if (curp->mdoc)
774: mdoc_reset(curp->mdoc);
775: if (curp->man)
776: man_reset(curp->man);
1.4 schwarze 777: if (curp->secondary)
778: curp->secondary->sz = 0;
1.1 schwarze 779:
780: curp->file_status = MANDOCLEVEL_OK;
781: curp->mdoc = NULL;
782: curp->man = NULL;
1.23 schwarze 783:
784: free(curp->sodest);
785: curp->sodest = NULL;
1.1 schwarze 786: }
787:
788: void
789: mparse_free(struct mparse *curp)
790: {
791:
792: if (curp->pmdoc)
793: mdoc_free(curp->pmdoc);
794: if (curp->pman)
795: man_free(curp->pman);
796: if (curp->roff)
797: roff_free(curp->roff);
1.4 schwarze 798: if (curp->secondary)
799: free(curp->secondary->buf);
1.1 schwarze 800:
1.4 schwarze 801: free(curp->secondary);
1.23 schwarze 802: free(curp->sodest);
1.1 schwarze 803: free(curp);
804: }
805:
806: void
1.23 schwarze 807: mparse_result(struct mparse *curp,
808: struct mdoc **mdoc, struct man **man, char **sodest)
1.1 schwarze 809: {
810:
1.23 schwarze 811: if (sodest && NULL != (*sodest = curp->sodest)) {
812: *mdoc = NULL;
813: *man = NULL;
814: return;
815: }
1.1 schwarze 816: if (mdoc)
817: *mdoc = curp->mdoc;
818: if (man)
819: *man = curp->man;
820: }
821:
822: void
823: mandoc_vmsg(enum mandocerr t, struct mparse *m,
824: int ln, int pos, const char *fmt, ...)
825: {
826: char buf[256];
827: va_list ap;
828:
829: va_start(ap, fmt);
1.26 schwarze 830: (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.1 schwarze 831: va_end(ap);
832:
833: mandoc_msg(t, m, ln, pos, buf);
834: }
835:
836: void
1.25 schwarze 837: mandoc_msg(enum mandocerr er, struct mparse *m,
1.1 schwarze 838: int ln, int col, const char *msg)
839: {
840: enum mandoclevel level;
841:
842: level = MANDOCLEVEL_FATAL;
843: while (er < mandoclimits[level])
844: level--;
845:
846: if (level < m->wlevel)
847: return;
848:
849: if (m->mmsg)
850: (*m->mmsg)(er, level, m->file, ln, col, msg);
851:
852: if (m->file_status < level)
853: m->file_status = level;
854: }
855:
856: const char *
857: mparse_strerror(enum mandocerr er)
858: {
859:
860: return(mandocerrs[er]);
861: }
862:
863: const char *
864: mparse_strlevel(enum mandoclevel lvl)
865: {
866: return(mandoclevels[lvl]);
1.4 schwarze 867: }
868:
869: void
870: mparse_keep(struct mparse *p)
871: {
872:
873: assert(NULL == p->secondary);
874: p->secondary = mandoc_calloc(1, sizeof(struct buf));
875: }
876:
877: const char *
878: mparse_getkeep(const struct mparse *p)
879: {
880:
881: assert(p->secondary);
882: return(p->secondary->sz ? p->secondary->buf : NULL);
1.1 schwarze 883: }