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