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