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