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