Annotation of mandoc/main.c, Revision 1.85
1.85 ! kristaps 1: /* $Id: main.c,v 1.84 2010/06/07 10:52:44 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);
! 594: break;
! 595: case (OUTT_PS):
! 596: curp->outdata = ps_alloc();
! 597: break;
! 598: default:
! 599: break;
! 600: }
! 601:
! 602: switch (curp->outtype) {
! 603: case (OUTT_HTML):
! 604: /* FALLTHROUGH */
! 605: case (OUTT_XHTML):
1.43 kristaps 606: curp->outman = html_man;
607: curp->outmdoc = html_mdoc;
608: curp->outfree = html_free;
609: break;
1.22 kristaps 610: case (OUTT_TREE):
611: curp->outman = tree_man;
612: curp->outmdoc = tree_mdoc;
613: break;
1.85 ! kristaps 614: case (OUTT_ASCII):
! 615: /* FALLTHROUGH */
! 616: case (OUTT_PS):
1.22 kristaps 617: curp->outman = terminal_man;
618: curp->outmdoc = terminal_mdoc;
619: curp->outfree = terminal_free;
620: break;
1.85 ! kristaps 621: default:
! 622: break;
1.22 kristaps 623: }
624: }
625:
626: /* Execute the out device, if it exists. */
627:
628: if (man && curp->outman)
1.42 kristaps 629: (*curp->outman)(curp->outdata, man);
1.22 kristaps 630: if (mdoc && curp->outmdoc)
1.42 kristaps 631: (*curp->outmdoc)(curp->outdata, mdoc);
1.22 kristaps 632:
1.64 kristaps 633: cleanup:
1.71 kristaps 634: if (mdoc)
635: mdoc_reset(mdoc);
636: if (man)
637: man_reset(man);
638: if (roff)
639: roff_reset(roff);
1.66 kristaps 640: if (ln.buf)
641: free(ln.buf);
642: if (with_mmap)
643: munmap(blk.buf, blk.sz);
644: else
645: free(blk.buf);
1.71 kristaps 646:
1.64 kristaps 647: return;
648:
649: bailout:
650: with_error = 1;
651: goto cleanup;
1.19 kristaps 652: }
653:
654:
655: static int
1.22 kristaps 656: pset(const char *buf, int pos, struct curparse *curp,
1.19 kristaps 657: struct man **man, struct mdoc **mdoc)
658: {
1.29 kristaps 659: int i;
1.19 kristaps 660:
661: /*
662: * Try to intuit which kind of manual parser should be used. If
663: * passed in by command-line (-man, -mdoc), then use that
664: * explicitly. If passed as -mandoc, then try to guess from the
1.30 kristaps 665: * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19 kristaps 666: * default to -man, which is more lenient.
667: */
668:
1.75 kristaps 669: if ('.' == buf[0] || '\'' == buf[0]) {
1.29 kristaps 670: for (i = 1; buf[i]; i++)
671: if (' ' != buf[i] && '\t' != buf[i])
672: break;
673: if (0 == buf[i])
674: return(1);
675: }
1.10 kristaps 676:
1.19 kristaps 677: switch (curp->inttype) {
678: case (INTT_MDOC):
679: if (NULL == curp->mdoc)
680: curp->mdoc = mdoc_init(curp);
681: if (NULL == (*mdoc = curp->mdoc))
682: return(0);
683: return(1);
684: case (INTT_MAN):
685: if (NULL == curp->man)
686: curp->man = man_init(curp);
687: if (NULL == (*man = curp->man))
688: return(0);
689: return(1);
690: default:
691: break;
692: }
693:
694: if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
695: if (NULL == curp->mdoc)
696: curp->mdoc = mdoc_init(curp);
697: if (NULL == (*mdoc = curp->mdoc))
698: return(0);
699: return(1);
700: }
701:
702: if (NULL == curp->man)
703: curp->man = man_init(curp);
704: if (NULL == (*man = curp->man))
705: return(0);
706: return(1);
1.10 kristaps 707: }
708:
709:
710: static int
711: moptions(enum intt *tflags, char *arg)
712: {
713:
1.17 kristaps 714: if (0 == strcmp(arg, "doc"))
1.10 kristaps 715: *tflags = INTT_MDOC;
1.19 kristaps 716: else if (0 == strcmp(arg, "andoc"))
717: *tflags = INTT_AUTO;
1.17 kristaps 718: else if (0 == strcmp(arg, "an"))
1.10 kristaps 719: *tflags = INTT_MAN;
720: else {
1.57 kristaps 721: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 722: return(0);
723: }
724:
725: return(1);
1.1 kristaps 726: }
727:
728:
729: static int
1.60 kristaps 730: toptions(struct curparse *curp, char *arg)
1.1 kristaps 731: {
732:
733: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 734: curp->outtype = OUTT_ASCII;
735: else if (0 == strcmp(arg, "lint")) {
736: curp->outtype = OUTT_LINT;
737: curp->wflags |= WARN_WALL;
738: curp->fflags |= FL_STRICT;
739: }
1.1 kristaps 740: else if (0 == strcmp(arg, "tree"))
1.60 kristaps 741: curp->outtype = OUTT_TREE;
1.43 kristaps 742: else if (0 == strcmp(arg, "html"))
1.60 kristaps 743: curp->outtype = OUTT_HTML;
1.59 kristaps 744: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 745: curp->outtype = OUTT_XHTML;
1.85 ! kristaps 746: else if (0 == strcmp(arg, "ps"))
! 747: curp->outtype = OUTT_PS;
1.1 kristaps 748: else {
1.57 kristaps 749: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 750: return(0);
751: }
752:
753: return(1);
754: }
755:
756:
757: static int
758: foptions(int *fflags, char *arg)
759: {
1.34 kristaps 760: char *v, *o;
1.50 kristaps 761: const char *toks[8];
1.1 kristaps 762:
763: toks[0] = "ign-scope";
1.24 kristaps 764: toks[1] = "no-ign-escape";
765: toks[2] = "no-ign-macro";
1.62 kristaps 766: toks[3] = "ign-errors";
767: toks[4] = "strict";
768: toks[5] = "ign-escape";
769: toks[6] = NULL;
1.1 kristaps 770:
1.34 kristaps 771: while (*arg) {
772: o = arg;
1.45 kristaps 773: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 774: case (0):
1.60 kristaps 775: *fflags |= FL_IGN_SCOPE;
1.1 kristaps 776: break;
777: case (1):
1.60 kristaps 778: *fflags |= FL_NIGN_ESCAPE;
1.1 kristaps 779: break;
780: case (2):
1.60 kristaps 781: *fflags |= FL_NIGN_MACRO;
1.1 kristaps 782: break;
1.20 kristaps 783: case (3):
1.62 kristaps 784: *fflags |= FL_IGN_ERRORS;
1.24 kristaps 785: break;
786: case (4):
1.62 kristaps 787: *fflags |= FL_STRICT;
1.39 kristaps 788: break;
789: case (5):
1.60 kristaps 790: *fflags &= ~FL_NIGN_ESCAPE;
1.50 kristaps 791: break;
1.1 kristaps 792: default:
1.57 kristaps 793: fprintf(stderr, "%s: Bad argument\n", o);
1.1 kristaps 794: return(0);
795: }
1.34 kristaps 796: }
1.1 kristaps 797:
798: return(1);
799: }
800:
801:
802: static int
803: woptions(int *wflags, char *arg)
804: {
1.34 kristaps 805: char *v, *o;
1.45 kristaps 806: const char *toks[3];
1.1 kristaps 807:
808: toks[0] = "all";
1.36 kristaps 809: toks[1] = "error";
810: toks[2] = NULL;
1.1 kristaps 811:
1.34 kristaps 812: while (*arg) {
813: o = arg;
1.45 kristaps 814: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 815: case (0):
816: *wflags |= WARN_WALL;
817: break;
818: case (1):
819: *wflags |= WARN_WERR;
820: break;
821: default:
1.57 kristaps 822: fprintf(stderr, "%s: Bad argument\n", o);
1.1 kristaps 823: return(0);
824: }
1.34 kristaps 825: }
1.1 kristaps 826:
827: return(1);
828: }
829:
830:
1.71 kristaps 831: static int
832: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
833: {
834: struct curparse *cp;
835:
836: cp = (struct curparse *)arg;
837:
1.79 kristaps 838: if (t <= MANDOCERR_ERROR) {
839: if ( ! (cp->wflags & WARN_WALL))
840: return(1);
841: with_warning = 1;
842: } else
843: with_error = 1;
844:
1.73 kristaps 845: fprintf(stderr, "%s:%d:%d: %s", cp->file,
846: ln, col + 1, mandocerrs[t]);
847:
848: if (msg)
849: fprintf(stderr, ": %s", msg);
1.71 kristaps 850:
1.73 kristaps 851: fputc('\n', stderr);
1.79 kristaps 852:
853: /* This is superfluous, but whatever. */
854: if (t > MANDOCERR_ERROR)
855: return(0);
856: if (cp->wflags & WARN_WERR) {
857: with_error = 1;
858: return(0);
859: }
1.71 kristaps 860: return(1);
861: }
CVSweb