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