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