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