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