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