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