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