Annotation of src/usr.bin/mandoc/main.c, Revision 1.52
1.52 ! schwarze 1: /* $Id: main.c,v 1.51 2010/10/26 22:13:58 schwarze Exp $ */
1.1 kristaps 2: /*
1.42 schwarze 3: * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
4: * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1 kristaps 5: *
6: * Permission to use, copy, modify, and distribute this software for any
1.2 schwarze 7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 9: *
1.2 schwarze 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.
1.1 kristaps 17: */
1.27 schwarze 18: #include <sys/types.h>
19: #include <sys/mman.h>
1.1 kristaps 20: #include <sys/stat.h>
21:
22: #include <assert.h>
1.43 schwarze 23: #include <ctype.h>
1.1 kristaps 24: #include <fcntl.h>
25: #include <stdio.h>
1.17 schwarze 26: #include <stdint.h>
1.1 kristaps 27: #include <stdlib.h>
28: #include <string.h>
29: #include <unistd.h>
30:
1.30 schwarze 31: #include "mandoc.h"
1.38 schwarze 32: #include "main.h"
1.1 kristaps 33: #include "mdoc.h"
34: #include "man.h"
1.30 schwarze 35: #include "roff.h"
1.17 schwarze 36:
37: #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
1.1 kristaps 38:
1.16 schwarze 39: typedef void (*out_mdoc)(void *, const struct mdoc *);
40: typedef void (*out_man)(void *, const struct man *);
1.1 kristaps 41: typedef void (*out_free)(void *);
42:
43: struct buf {
44: char *buf;
45: size_t sz;
46: };
47:
48: enum intt {
49: INTT_AUTO,
50: INTT_MDOC,
51: INTT_MAN
52: };
53:
54: enum outt {
55: OUTT_ASCII = 0,
56: OUTT_TREE,
1.17 schwarze 57: OUTT_HTML,
1.21 schwarze 58: OUTT_XHTML,
1.36 schwarze 59: OUTT_LINT,
1.43 schwarze 60: OUTT_PS,
61: OUTT_PDF
1.1 kristaps 62: };
63:
64: struct curparse {
65: const char *file; /* Current parse. */
66: int fd; /* Current parse. */
1.45 schwarze 67: enum mandoclevel wlevel; /* Ignore messages below this. */
68: int wstop; /* Stop after a file with a warning. */
1.33 schwarze 69: enum intt inttype; /* which parser to use */
70: struct man *man; /* man parser */
71: struct mdoc *mdoc; /* mdoc parser */
72: struct roff *roff; /* roff parser (!NULL) */
1.38 schwarze 73: struct regset regs; /* roff registers */
1.33 schwarze 74: enum outt outtype; /* which output to use */
75: out_mdoc outmdoc; /* mdoc output ptr */
76: out_man outman; /* man output ptr */
77: out_free outfree; /* free output ptr */
78: void *outdata; /* data for output */
79: char outopts[BUFSIZ]; /* buf of output opts */
80: };
81:
1.45 schwarze 82: static const char * const mandoclevels[MANDOCLEVEL_MAX] = {
83: "SUCCESS",
84: "RESERVED",
85: "WARNING",
86: "ERROR",
87: "FATAL",
88: "BADARG",
89: "SYSERR"
90: };
91:
92: static const enum mandocerr mandoclimits[MANDOCLEVEL_MAX] = {
93: MANDOCERR_OK,
94: MANDOCERR_WARNING,
95: MANDOCERR_WARNING,
96: MANDOCERR_ERROR,
97: MANDOCERR_FATAL,
98: MANDOCERR_MAX,
99: MANDOCERR_MAX
100: };
101:
1.33 schwarze 102: static const char * const mandocerrs[MANDOCERR_MAX] = {
103: "ok",
1.40 schwarze 104:
105: "generic warning",
106:
1.33 schwarze 107: "text should be uppercase",
1.34 schwarze 108: "sections out of conventional order",
1.33 schwarze 109: "section name repeats",
110: "out of order prologue",
111: "repeated prologue entry",
112: "list type must come first",
113: "bad standard",
114: "bad library",
1.43 schwarze 115: "tab in non-literal context",
1.33 schwarze 116: "bad escape sequence",
117: "unterminated quoted string",
118: "argument requires the width argument",
119: "superfluous width argument",
1.37 schwarze 120: "ignoring argument",
1.33 schwarze 121: "bad date argument",
122: "bad width argument",
1.34 schwarze 123: "unknown manual section",
1.50 schwarze 124: "nested displays are not portable",
1.33 schwarze 125: "section not in conventional manual section",
126: "end of line whitespace",
1.41 schwarze 127: "blocks badly nested",
1.40 schwarze 128:
129: "generic error",
130:
1.33 schwarze 131: "NAME section must come first",
132: "bad Boolean value",
133: "child violates parent syntax",
134: "bad AT&T symbol",
135: "list type repeated",
136: "display type repeated",
137: "argument repeated",
138: "manual name not yet set",
139: "obsolete macro ignored",
140: "empty macro ignored",
141: "macro not allowed in body",
142: "macro not allowed in prologue",
143: "bad character",
144: "bad NAME section contents",
145: "no blank lines",
146: "no text in this context",
147: "bad comment style",
148: "unknown macro will be lost",
149: "line scope broken",
150: "argument count wrong",
151: "request scope close w/none open",
1.50 schwarze 152: "missing end of block",
1.33 schwarze 153: "scope already open",
1.47 schwarze 154: "scope open on exit",
1.50 schwarze 155: "uname(3) system call failed",
1.33 schwarze 156: "macro requires line argument(s)",
157: "macro requires body argument(s)",
158: "macro requires argument(s)",
159: "no title in document",
1.34 schwarze 160: "missing list type",
1.37 schwarze 161: "missing display type",
1.42 schwarze 162: "missing font type",
1.33 schwarze 163: "line argument(s) will be lost",
164: "body argument(s) will be lost",
1.48 schwarze 165: "paragraph macro ignored",
1.49 schwarze 166: "tbl(1) error",
1.40 schwarze 167:
168: "generic fatal error",
169:
1.34 schwarze 170: "column syntax is inconsistent",
1.37 schwarze 171: "unsupported display type",
1.33 schwarze 172: "line scope broken, syntax violated",
173: "argument count wrong, violates syntax",
174: "child violates parent syntax",
175: "argument count wrong, violates syntax",
1.52 ! schwarze 176: "invalid path in include directive",
1.33 schwarze 177: "no document body",
178: "no document prologue",
1.45 schwarze 179: "static buffer exhausted",
1.1 kristaps 180: };
181:
1.51 schwarze 182: static void pdesc(struct curparse *);
1.27 schwarze 183: static void fdesc(struct curparse *);
184: static void ffile(const char *, struct curparse *);
1.52 ! schwarze 185: static int pfile(const char *, struct curparse *, int);
1.1 kristaps 186: static int moptions(enum intt *, char *);
1.30 schwarze 187: static int mmsg(enum mandocerr, void *,
188: int, int, const char *);
1.45 schwarze 189: static void pset(const char *, int, struct curparse *,
1.1 kristaps 190: struct man **, struct mdoc **);
1.27 schwarze 191: static int toptions(struct curparse *, char *);
192: static void usage(void) __attribute__((noreturn));
1.20 schwarze 193: static void version(void) __attribute__((noreturn));
1.45 schwarze 194: static int woptions(struct curparse *, char *);
1.1 kristaps 195:
1.19 schwarze 196: static const char *progname;
1.45 schwarze 197: static enum mandoclevel exit_status = MANDOCLEVEL_OK;
1.1 kristaps 198:
199: int
200: main(int argc, char *argv[])
201: {
1.27 schwarze 202: int c;
1.1 kristaps 203: struct curparse curp;
204:
1.19 schwarze 205: progname = strrchr(argv[0], '/');
206: if (progname == NULL)
207: progname = argv[0];
208: else
209: ++progname;
210:
211: memset(&curp, 0, sizeof(struct curparse));
1.1 kristaps 212:
213: curp.inttype = INTT_AUTO;
214: curp.outtype = OUTT_ASCII;
1.45 schwarze 215: curp.wlevel = MANDOCLEVEL_FATAL;
1.1 kristaps 216:
217: /* LINTED */
1.45 schwarze 218: while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1 kristaps 219: switch (c) {
220: case ('m'):
221: if ( ! moptions(&curp.inttype, optarg))
1.47 schwarze 222: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 223: break;
1.18 schwarze 224: case ('O'):
225: (void)strlcat(curp.outopts, optarg, BUFSIZ);
226: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.17 schwarze 227: break;
1.1 kristaps 228: case ('T'):
1.22 schwarze 229: if ( ! toptions(&curp, optarg))
1.47 schwarze 230: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 231: break;
232: case ('W'):
1.45 schwarze 233: if ( ! woptions(&curp, optarg))
1.47 schwarze 234: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 235: break;
1.3 schwarze 236: case ('V'):
237: version();
238: /* NOTREACHED */
1.1 kristaps 239: default:
240: usage();
241: /* NOTREACHED */
242: }
243:
244: argc -= optind;
245: argv += optind;
246:
1.7 schwarze 247: if (NULL == *argv) {
248: curp.file = "<stdin>";
249: curp.fd = STDIN_FILENO;
1.14 schwarze 250:
1.27 schwarze 251: fdesc(&curp);
252: }
253:
254: while (*argv) {
255: ffile(*argv, &curp);
1.45 schwarze 256: if (MANDOCLEVEL_OK != exit_status && curp.wstop)
1.27 schwarze 257: break;
258: ++argv;
1.1 kristaps 259: }
260:
261: if (curp.outfree)
262: (*curp.outfree)(curp.outdata);
1.30 schwarze 263: if (curp.mdoc)
264: mdoc_free(curp.mdoc);
265: if (curp.man)
266: man_free(curp.man);
267: if (curp.roff)
268: roff_free(curp.roff);
1.1 kristaps 269:
1.47 schwarze 270: return((int)exit_status);
1.1 kristaps 271: }
272:
273:
1.20 schwarze 274: static void
1.3 schwarze 275: version(void)
276: {
277:
1.19 schwarze 278: (void)printf("%s %s\n", progname, VERSION);
1.47 schwarze 279: exit((int)MANDOCLEVEL_OK);
1.3 schwarze 280: }
281:
282:
1.20 schwarze 283: static void
1.1 kristaps 284: usage(void)
285: {
286:
1.23 jmc 287: (void)fprintf(stderr, "usage: %s [-V] [-foption] "
1.18 schwarze 288: "[-mformat] [-Ooption] [-Toutput] "
1.23 jmc 289: "[-Werr] [file...]\n", progname);
1.47 schwarze 290: exit((int)MANDOCLEVEL_BADARG);
1.1 kristaps 291: }
292:
293:
1.27 schwarze 294: static void
295: ffile(const char *file, struct curparse *curp)
1.1 kristaps 296: {
297:
298: curp->file = file;
299: if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.19 schwarze 300: perror(curp->file);
1.45 schwarze 301: exit_status = MANDOCLEVEL_SYSERR;
1.27 schwarze 302: return;
1.1 kristaps 303: }
304:
1.27 schwarze 305: fdesc(curp);
1.1 kristaps 306:
307: if (-1 == close(curp->fd))
1.19 schwarze 308: perror(curp->file);
1.27 schwarze 309: }
1.1 kristaps 310:
1.52 ! schwarze 311: static int
! 312: pfile(const char *file, struct curparse *curp, int ln)
! 313: {
! 314: const char *savefile;
! 315: int fd, savefd;
! 316:
! 317: if (-1 == (fd = open(file, O_RDONLY, 0))) {
! 318: perror(file);
! 319: exit_status = MANDOCLEVEL_SYSERR;
! 320: return(0);
! 321: }
! 322:
! 323: savefile = curp->file;
! 324: savefd = curp->fd;
! 325:
! 326: curp->file = file;
! 327: curp->fd = fd;
! 328:
! 329: pdesc(curp);
! 330:
! 331: curp->file = savefile;
! 332: curp->fd = savefd;
! 333:
! 334: if (-1 == close(fd))
! 335: perror(file);
! 336:
! 337: return(MANDOCLEVEL_FATAL > exit_status ? 1 : 0);
! 338: }
! 339:
1.27 schwarze 340:
1.45 schwarze 341: static void
1.27 schwarze 342: resize_buf(struct buf *buf, size_t initial)
343: {
344:
1.45 schwarze 345: buf->sz = buf->sz ? 2 * buf->sz : initial;
346: buf->buf = realloc(buf->buf, buf->sz);
347: if (NULL == buf->buf) {
1.27 schwarze 348: perror(NULL);
1.47 schwarze 349: exit((int)MANDOCLEVEL_SYSERR);
1.27 schwarze 350: }
1.1 kristaps 351: }
352:
353:
354: static int
1.27 schwarze 355: read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap)
1.1 kristaps 356: {
1.27 schwarze 357: struct stat st;
358: size_t off;
1.1 kristaps 359: ssize_t ssz;
1.27 schwarze 360:
361: if (-1 == fstat(curp->fd, &st)) {
362: perror(curp->file);
363: return(0);
364: }
365:
366: /*
367: * If we're a regular file, try just reading in the whole entry
368: * via mmap(). This is faster than reading it into blocks, and
369: * since each file is only a few bytes to begin with, I'm not
370: * concerned that this is going to tank any machines.
371: */
372:
373: if (S_ISREG(st.st_mode)) {
374: if (st.st_size >= (1U << 31)) {
375: fprintf(stderr, "%s: input too large\n",
376: curp->file);
377: return(0);
378: }
379: *with_mmap = 1;
1.30 schwarze 380: fb->sz = (size_t)st.st_size;
1.27 schwarze 381: fb->buf = mmap(NULL, fb->sz, PROT_READ,
1.35 schwarze 382: MAP_FILE|MAP_SHARED, curp->fd, 0);
1.27 schwarze 383: if (fb->buf != MAP_FAILED)
384: return(1);
385: }
386:
387: /*
388: * If this isn't a regular file (like, say, stdin), then we must
389: * go the old way and just read things in bit by bit.
390: */
391:
392: *with_mmap = 0;
393: off = 0;
394: fb->sz = 0;
395: fb->buf = NULL;
396: for (;;) {
397: if (off == fb->sz) {
398: if (fb->sz == (1U << 31)) {
399: fprintf(stderr, "%s: input too large\n",
400: curp->file);
401: break;
402: }
1.45 schwarze 403: resize_buf(fb, 65536);
1.27 schwarze 404: }
1.30 schwarze 405: ssz = read(curp->fd, fb->buf + (int)off, fb->sz - off);
1.27 schwarze 406: if (ssz == 0) {
407: fb->sz = off;
408: return(1);
409: }
410: if (ssz == -1) {
411: perror(curp->file);
412: break;
413: }
1.30 schwarze 414: off += (size_t)ssz;
1.27 schwarze 415: }
416:
417: free(fb->buf);
418: fb->buf = NULL;
419: return(0);
420: }
421:
422:
423: static void
424: fdesc(struct curparse *curp)
425: {
1.51 schwarze 426: struct man *man;
427: struct mdoc *mdoc;
428: struct roff *roff;
429:
430: pdesc(curp);
431:
432: man = curp->man;
433: mdoc = curp->mdoc;
434: roff = curp->roff;
435:
436: if (MANDOCLEVEL_FATAL <= exit_status)
437: goto cleanup;
438:
439: /* NOTE a parser may not have been assigned, yet. */
440:
441: if ( ! (man || mdoc)) {
442: fprintf(stderr, "%s: Not a manual\n", curp->file);
443: exit_status = MANDOCLEVEL_FATAL;
444: goto cleanup;
445: }
446:
447: /* Clean up the parse routine ASTs. */
448:
449: if (mdoc && ! mdoc_endparse(mdoc)) {
450: assert(MANDOCLEVEL_FATAL <= exit_status);
451: goto cleanup;
452: }
453: if (man && ! man_endparse(man)) {
454: assert(MANDOCLEVEL_FATAL <= exit_status);
455: goto cleanup;
456: }
457: if (roff && ! roff_endparse(roff)) {
458: assert(MANDOCLEVEL_FATAL <= exit_status);
459: goto cleanup;
460: }
461:
462: /*
463: * With -Wstop and warnings or errors of at least
464: * the requested level, do not produce output.
465: */
466:
467: if (MANDOCLEVEL_OK != exit_status && curp->wstop)
468: goto cleanup;
469:
470: /* If unset, allocate output dev now (if applicable). */
471:
472: if ( ! (curp->outman && curp->outmdoc)) {
473: switch (curp->outtype) {
474: case (OUTT_XHTML):
475: curp->outdata = xhtml_alloc(curp->outopts);
476: break;
477: case (OUTT_HTML):
478: curp->outdata = html_alloc(curp->outopts);
479: break;
480: case (OUTT_ASCII):
481: curp->outdata = ascii_alloc(curp->outopts);
482: curp->outfree = ascii_free;
483: break;
484: case (OUTT_PDF):
485: curp->outdata = pdf_alloc(curp->outopts);
486: curp->outfree = pspdf_free;
487: break;
488: case (OUTT_PS):
489: curp->outdata = ps_alloc(curp->outopts);
490: curp->outfree = pspdf_free;
491: break;
492: default:
493: break;
494: }
495:
496: switch (curp->outtype) {
497: case (OUTT_HTML):
498: /* FALLTHROUGH */
499: case (OUTT_XHTML):
500: curp->outman = html_man;
501: curp->outmdoc = html_mdoc;
502: curp->outfree = html_free;
503: break;
504: case (OUTT_TREE):
505: curp->outman = tree_man;
506: curp->outmdoc = tree_mdoc;
507: break;
508: case (OUTT_PDF):
509: /* FALLTHROUGH */
510: case (OUTT_ASCII):
511: /* FALLTHROUGH */
512: case (OUTT_PS):
513: curp->outman = terminal_man;
514: curp->outmdoc = terminal_mdoc;
515: break;
516: default:
517: break;
518: }
519: }
520:
521: /* Execute the out device, if it exists. */
522:
523: if (man && curp->outman)
524: (*curp->outman)(curp->outdata, man);
525: if (mdoc && curp->outmdoc)
526: (*curp->outmdoc)(curp->outdata, mdoc);
527:
528: cleanup:
529: memset(&curp->regs, 0, sizeof(struct regset));
530: if (mdoc)
531: mdoc_reset(mdoc);
532: if (man)
533: man_reset(man);
534: if (roff)
535: roff_reset(roff);
536:
537: return;
538: }
539:
540:
541: static void
542: pdesc(struct curparse *curp)
543: {
1.27 schwarze 544: struct buf ln, blk;
1.32 schwarze 545: int i, pos, lnn, lnn_start, with_mmap, of;
1.30 schwarze 546: enum rofferr re;
1.44 schwarze 547: unsigned char c;
1.1 kristaps 548: struct man *man;
549: struct mdoc *mdoc;
1.30 schwarze 550: struct roff *roff;
1.1 kristaps 551:
1.27 schwarze 552: memset(&ln, 0, sizeof(struct buf));
1.1 kristaps 553:
554: /*
1.29 schwarze 555: * Two buffers: ln and buf. buf is the input file and may be
556: * memory mapped. ln is a line buffer and grows on-demand.
1.1 kristaps 557: */
558:
1.45 schwarze 559: if ( ! read_whole_file(curp, &blk, &with_mmap)) {
560: exit_status = MANDOCLEVEL_SYSERR;
1.27 schwarze 561: return;
1.45 schwarze 562: }
1.27 schwarze 563:
1.30 schwarze 564: if (NULL == curp->roff)
1.45 schwarze 565: curp->roff = roff_alloc(&curp->regs, curp, mmsg);
566: assert(curp->roff);
567: roff = curp->roff;
1.51 schwarze 568: mdoc = curp->mdoc;
569: man = curp->man;
1.30 schwarze 570:
1.29 schwarze 571: for (i = 0, lnn = 1; i < (int)blk.sz;) {
572: pos = 0;
573: lnn_start = lnn;
574: while (i < (int)blk.sz) {
575: if ('\n' == blk.buf[i]) {
576: ++i;
577: ++lnn;
578: break;
579: }
1.43 schwarze 580:
581: /*
582: * Warn about bogus characters. If you're using
583: * non-ASCII encoding, you're screwing your
584: * readers. Since I'd rather this not happen,
585: * I'll be helpful and drop these characters so
586: * we don't display gibberish. Note to manual
587: * writers: use special characters.
588: */
589:
1.44 schwarze 590: c = (unsigned char) blk.buf[i];
591: if ( ! (isascii(c) && (isgraph(c) || isblank(c)))) {
1.45 schwarze 592: mmsg(MANDOCERR_BADCHAR, curp,
593: lnn_start, pos, "ignoring byte");
1.43 schwarze 594: i++;
595: continue;
596: }
597:
1.29 schwarze 598: /* Trailing backslash is like a plain character. */
599: if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
600: if (pos >= (int)ln.sz)
1.45 schwarze 601: resize_buf(&ln, 256);
1.29 schwarze 602: ln.buf[pos++] = blk.buf[i++];
1.27 schwarze 603: continue;
1.29 schwarze 604: }
605: /* Found an escape and at least one other character. */
606: if ('\n' == blk.buf[i + 1]) {
607: /* Escaped newlines are skipped over */
608: i += 2;
609: ++lnn;
1.27 schwarze 610: continue;
611: }
1.29 schwarze 612: if ('"' == blk.buf[i + 1]) {
613: i += 2;
614: /* Comment, skip to end of line */
615: for (; i < (int)blk.sz; ++i) {
616: if ('\n' == blk.buf[i]) {
617: ++i;
618: ++lnn;
619: break;
620: }
621: }
622: /* Backout trailing whitespaces */
623: for (; pos > 0; --pos) {
624: if (ln.buf[pos - 1] != ' ')
625: break;
626: if (pos > 2 && ln.buf[pos - 2] == '\\')
627: break;
628: }
629: break;
630: }
631: /* Some other escape sequence, copy and continue. */
632: if (pos + 1 >= (int)ln.sz)
1.45 schwarze 633: resize_buf(&ln, 256);
1.1 kristaps 634:
1.29 schwarze 635: ln.buf[pos++] = blk.buf[i++];
636: ln.buf[pos++] = blk.buf[i++];
1.27 schwarze 637: }
1.1 kristaps 638:
1.29 schwarze 639: if (pos >= (int)ln.sz)
1.45 schwarze 640: resize_buf(&ln, 256);
1.30 schwarze 641: ln.buf[pos] = '\0';
642:
1.32 schwarze 643: /*
644: * A significant amount of complexity is contained by
645: * the roff preprocessor. It's line-oriented but can be
646: * expressed on one line, so we need at times to
647: * readjust our starting point and re-run it. The roff
648: * preprocessor can also readjust the buffers with new
649: * data, so we pass them in wholesale.
650: */
651:
652: of = 0;
653: do {
654: re = roff_parseln(roff, lnn_start,
655: &ln.buf, &ln.sz, of, &of);
656: } while (ROFF_RERUN == re);
657:
1.45 schwarze 658: if (ROFF_IGN == re) {
1.30 schwarze 659: continue;
1.45 schwarze 660: } else if (ROFF_ERR == re) {
661: assert(MANDOCLEVEL_FATAL <= exit_status);
1.51 schwarze 662: break;
1.52 ! schwarze 663: } else if (ROFF_SO == re) {
! 664: if (pfile(ln.buf + of, curp, lnn_start))
! 665: continue;
! 666: else
! 667: break;
1.45 schwarze 668: }
1.5 schwarze 669:
1.32 schwarze 670: /*
671: * If input parsers have not been allocated, do so now.
672: * We keep these instanced betwen parsers, but set them
673: * locally per parse routine since we can use different
674: * parsers with each one.
675: */
1.1 kristaps 676:
1.32 schwarze 677: if ( ! (man || mdoc))
1.45 schwarze 678: pset(ln.buf + of, pos - of, curp, &man, &mdoc);
1.5 schwarze 679:
1.32 schwarze 680: /* Lastly, push down into the parsers themselves. */
1.1 kristaps 681:
1.45 schwarze 682: if (man && ! man_parseln(man, lnn_start, ln.buf, of)) {
683: assert(MANDOCLEVEL_FATAL <= exit_status);
1.51 schwarze 684: break;
1.45 schwarze 685: }
686: if (mdoc && ! mdoc_parseln(mdoc, lnn_start, ln.buf, of)) {
687: assert(MANDOCLEVEL_FATAL <= exit_status);
1.1 kristaps 688: break;
689: }
690: }
691:
1.51 schwarze 692: free(ln.buf);
1.27 schwarze 693: if (with_mmap)
694: munmap(blk.buf, blk.sz);
695: else
696: free(blk.buf);
1.1 kristaps 697: }
698:
699:
1.45 schwarze 700: static void
1.1 kristaps 701: pset(const char *buf, int pos, struct curparse *curp,
702: struct man **man, struct mdoc **mdoc)
703: {
1.5 schwarze 704: int i;
1.1 kristaps 705:
706: /*
707: * Try to intuit which kind of manual parser should be used. If
708: * passed in by command-line (-man, -mdoc), then use that
709: * explicitly. If passed as -mandoc, then try to guess from the
1.5 schwarze 710: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.1 kristaps 711: * default to -man, which is more lenient.
712: */
713:
1.31 schwarze 714: if ('.' == buf[0] || '\'' == buf[0]) {
1.5 schwarze 715: for (i = 1; buf[i]; i++)
716: if (' ' != buf[i] && '\t' != buf[i])
717: break;
1.45 schwarze 718: if ('\0' == buf[i])
719: return;
1.5 schwarze 720: }
1.1 kristaps 721:
722: switch (curp->inttype) {
723: case (INTT_MDOC):
724: if (NULL == curp->mdoc)
1.45 schwarze 725: curp->mdoc = mdoc_alloc(&curp->regs, curp, mmsg);
726: assert(curp->mdoc);
727: *mdoc = curp->mdoc;
728: return;
1.1 kristaps 729: case (INTT_MAN):
730: if (NULL == curp->man)
1.45 schwarze 731: curp->man = man_alloc(&curp->regs, curp, mmsg);
732: assert(curp->man);
733: *man = curp->man;
734: return;
1.1 kristaps 735: default:
736: break;
737: }
738:
739: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
740: if (NULL == curp->mdoc)
1.45 schwarze 741: curp->mdoc = mdoc_alloc(&curp->regs, curp, mmsg);
742: assert(curp->mdoc);
743: *mdoc = curp->mdoc;
744: return;
1.1 kristaps 745: }
746:
747: if (NULL == curp->man)
1.45 schwarze 748: curp->man = man_alloc(&curp->regs, curp, mmsg);
749: assert(curp->man);
750: *man = curp->man;
1.1 kristaps 751: }
752:
753:
754: static int
755: moptions(enum intt *tflags, char *arg)
756: {
757:
758: if (0 == strcmp(arg, "doc"))
759: *tflags = INTT_MDOC;
760: else if (0 == strcmp(arg, "andoc"))
761: *tflags = INTT_AUTO;
762: else if (0 == strcmp(arg, "an"))
763: *tflags = INTT_MAN;
764: else {
1.20 schwarze 765: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 766: return(0);
767: }
768:
769: return(1);
770: }
771:
772:
773: static int
1.22 schwarze 774: toptions(struct curparse *curp, char *arg)
1.1 kristaps 775: {
776:
777: if (0 == strcmp(arg, "ascii"))
1.22 schwarze 778: curp->outtype = OUTT_ASCII;
779: else if (0 == strcmp(arg, "lint")) {
780: curp->outtype = OUTT_LINT;
1.45 schwarze 781: curp->wlevel = MANDOCLEVEL_WARNING;
1.22 schwarze 782: }
1.1 kristaps 783: else if (0 == strcmp(arg, "tree"))
1.22 schwarze 784: curp->outtype = OUTT_TREE;
1.17 schwarze 785: else if (0 == strcmp(arg, "html"))
1.22 schwarze 786: curp->outtype = OUTT_HTML;
1.21 schwarze 787: else if (0 == strcmp(arg, "xhtml"))
1.22 schwarze 788: curp->outtype = OUTT_XHTML;
1.36 schwarze 789: else if (0 == strcmp(arg, "ps"))
790: curp->outtype = OUTT_PS;
1.43 schwarze 791: else if (0 == strcmp(arg, "pdf"))
792: curp->outtype = OUTT_PDF;
1.1 kristaps 793: else {
1.20 schwarze 794: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 795: return(0);
796: }
797:
798: return(1);
799: }
800:
801:
802: static int
1.45 schwarze 803: woptions(struct curparse *curp, char *arg)
1.1 kristaps 804: {
1.10 schwarze 805: char *v, *o;
1.45 schwarze 806: const char *toks[6];
1.1 kristaps 807:
1.45 schwarze 808: toks[0] = "stop";
809: toks[1] = "all";
810: toks[2] = "warning";
811: toks[3] = "error";
812: toks[4] = "fatal";
813: toks[5] = NULL;
1.1 kristaps 814:
1.10 schwarze 815: while (*arg) {
816: o = arg;
1.17 schwarze 817: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 818: case (0):
1.45 schwarze 819: curp->wstop = 1;
1.1 kristaps 820: break;
821: case (1):
1.45 schwarze 822: /* FALLTHROUGH */
1.1 kristaps 823: case (2):
1.45 schwarze 824: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 825: break;
826: case (3):
1.45 schwarze 827: curp->wlevel = MANDOCLEVEL_ERROR;
1.1 kristaps 828: break;
829: case (4):
1.45 schwarze 830: curp->wlevel = MANDOCLEVEL_FATAL;
1.19 schwarze 831: break;
1.1 kristaps 832: default:
1.45 schwarze 833: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 834: return(0);
835: }
1.10 schwarze 836: }
1.1 kristaps 837:
838: return(1);
839: }
840:
841:
1.30 schwarze 842: static int
843: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
844: {
845: struct curparse *cp;
1.45 schwarze 846: enum mandoclevel level;
847:
848: level = MANDOCLEVEL_FATAL;
849: while (t < mandoclimits[level])
1.47 schwarze 850: /* LINTED */
1.45 schwarze 851: level--;
1.30 schwarze 852:
853: cp = (struct curparse *)arg;
1.45 schwarze 854: if (level < cp->wlevel)
855: return(1);
1.30 schwarze 856:
1.45 schwarze 857: fprintf(stderr, "%s:%d:%d: %s: %s",
858: cp->file, ln, col + 1, mandoclevels[level], mandocerrs[t]);
1.30 schwarze 859: if (msg)
860: fprintf(stderr, ": %s", msg);
861: fputc('\n', stderr);
1.33 schwarze 862:
1.45 schwarze 863: if (exit_status < level)
864: exit_status = level;
865:
866: return(level < MANDOCLEVEL_FATAL);
1.30 schwarze 867: }