Annotation of mandoc/main.c, Revision 1.160
1.160 ! kristaps 1: /* $Id: main.c,v 1.159 2011/03/23 09:47:13 kristaps Exp $ */
1.1 kristaps 2: /*
1.133 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.140 schwarze 4: * Copyright (c) 2010, 2011 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.1 kristaps 22: #include <assert.h>
23: #include <stdio.h>
1.45 kristaps 24: #include <stdint.h>
1.1 kristaps 25: #include <stdlib.h>
26: #include <string.h>
27: #include <unistd.h>
28:
1.71 kristaps 29: #include "mandoc.h"
1.90 kristaps 30: #include "main.h"
1.1 kristaps 31: #include "mdoc.h"
1.10 kristaps 32: #include "man.h"
1.101 joerg 33:
1.55 kristaps 34: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 35: # if !defined(lint)
36: # define __attribute__(x)
37: # endif
1.55 kristaps 38: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 39:
1.42 kristaps 40: typedef void (*out_mdoc)(void *, const struct mdoc *);
41: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 42: typedef void (*out_free)(void *);
43:
1.19 kristaps 44: enum outt {
1.154 kristaps 45: OUTT_ASCII = 0, /* -Tascii */
46: OUTT_TREE, /* -Ttree */
47: OUTT_HTML, /* -Thtml */
48: OUTT_XHTML, /* -Txhtml */
49: OUTT_LINT, /* -Tlint */
50: OUTT_PS, /* -Tps */
51: OUTT_PDF /* -Tpdf */
1.19 kristaps 52: };
53:
1.5 kristaps 54: struct curparse {
1.154 kristaps 55: struct mparse *mp;
1.148 kristaps 56: enum mandoclevel wlevel; /* ignore messages below this */
57: int wstop; /* stop after a file with a warning */
1.79 kristaps 58: enum outt outtype; /* which output to use */
59: out_mdoc outmdoc; /* mdoc output ptr */
60: out_man outman; /* man output ptr */
61: out_free outfree; /* free output ptr */
62: void *outdata; /* data for output */
63: char outopts[BUFSIZ]; /* buf of output opts */
64: };
65:
1.154 kristaps 66: static int moptions(enum mparset *, char *);
1.155 kristaps 67: static void mmsg(enum mandocerr, enum mandoclevel,
68: const char *, int, int, const char *);
1.154 kristaps 69: static void parse(struct curparse *, int,
70: const char *, enum mandoclevel *);
1.66 kristaps 71: static int toptions(struct curparse *, char *);
72: static void usage(void) __attribute__((noreturn));
1.55 kristaps 73: static void version(void) __attribute__((noreturn));
1.103 schwarze 74: static int woptions(struct curparse *, char *);
1.1 kristaps 75:
1.54 kristaps 76: static const char *progname;
1.1 kristaps 77:
78: int
79: main(int argc, char *argv[])
80: {
1.64 kristaps 81: int c;
1.5 kristaps 82: struct curparse curp;
1.154 kristaps 83: enum mparset type;
84: enum mandoclevel rc;
1.1 kristaps 85:
1.54 kristaps 86: progname = strrchr(argv[0], '/');
87: if (progname == NULL)
88: progname = argv[0];
89: else
90: ++progname;
91:
1.51 kristaps 92: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 93:
1.154 kristaps 94: type = MPARSE_AUTO;
1.22 kristaps 95: curp.outtype = OUTT_ASCII;
1.103 schwarze 96: curp.wlevel = MANDOCLEVEL_FATAL;
1.19 kristaps 97:
1.1 kristaps 98: /* LINTED */
1.103 schwarze 99: while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1 kristaps 100: switch (c) {
1.10 kristaps 101: case ('m'):
1.154 kristaps 102: if ( ! moptions(&type, optarg))
1.105 kristaps 103: return((int)MANDOCLEVEL_BADARG);
1.10 kristaps 104: break;
1.48 kristaps 105: case ('O'):
1.49 kristaps 106: (void)strlcat(curp.outopts, optarg, BUFSIZ);
107: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 108: break;
1.1 kristaps 109: case ('T'):
1.60 kristaps 110: if ( ! toptions(&curp, optarg))
1.105 kristaps 111: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 112: break;
113: case ('W'):
1.103 schwarze 114: if ( ! woptions(&curp, optarg))
1.105 kristaps 115: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 116: break;
117: case ('V'):
118: version();
119: /* NOTREACHED */
120: default:
121: usage();
122: /* NOTREACHED */
123: }
124:
1.156 kristaps 125: curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
1.154 kristaps 126:
1.1 kristaps 127: argc -= optind;
128: argv += optind;
129:
1.154 kristaps 130: rc = MANDOCLEVEL_OK;
1.39 kristaps 131:
1.154 kristaps 132: if (NULL == *argv)
133: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64 kristaps 134:
135: while (*argv) {
1.154 kristaps 136: parse(&curp, -1, *argv, &rc);
137: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 138: break;
139: ++argv;
1.1 kristaps 140: }
141:
1.22 kristaps 142: if (curp.outfree)
143: (*curp.outfree)(curp.outdata);
1.154 kristaps 144: if (curp.mp)
145: mparse_free(curp.mp);
1.1 kristaps 146:
1.154 kristaps 147: return((int)rc);
1.1 kristaps 148: }
149:
1.55 kristaps 150: static void
1.1 kristaps 151: version(void)
152: {
153:
1.154 kristaps 154: printf("%s %s\n", progname, VERSION);
1.105 kristaps 155: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 156: }
157:
1.55 kristaps 158: static void
1.1 kristaps 159: usage(void)
160: {
161:
1.154 kristaps 162: fprintf(stderr, "usage: %s "
1.112 kristaps 163: "[-V] "
164: "[-foption] "
165: "[-mformat] "
166: "[-Ooption] "
167: "[-Toutput] "
168: "[-Werr] "
169: "[file...]\n",
170: progname);
171:
1.105 kristaps 172: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 173: }
174:
1.64 kristaps 175: static void
1.154 kristaps 176: parse(struct curparse *curp, int fd,
177: const char *file, enum mandoclevel *level)
1.1 kristaps 178: {
1.154 kristaps 179: enum mandoclevel rc;
180: struct mdoc *mdoc;
181: struct man *man;
1.112 kristaps 182:
1.154 kristaps 183: /* Begin by parsing the file itself. */
1.112 kristaps 184:
1.154 kristaps 185: assert(file);
186: assert(fd >= -1);
1.112 kristaps 187:
1.154 kristaps 188: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 189:
1.154 kristaps 190: /* Stop immediately if the parse has failed. */
1.92 kristaps 191:
1.154 kristaps 192: if (MANDOCLEVEL_FATAL <= rc)
1.111 kristaps 193: goto cleanup;
194:
1.1 kristaps 195: /*
1.154 kristaps 196: * With -Wstop and warnings or errors of at least the requested
197: * level, do not produce output.
1.1 kristaps 198: */
199:
1.154 kristaps 200: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 201: goto cleanup;
202:
203: /* If unset, allocate output dev now (if applicable). */
204:
205: if ( ! (curp->outman && curp->outmdoc)) {
206: switch (curp->outtype) {
207: case (OUTT_XHTML):
208: curp->outdata = xhtml_alloc(curp->outopts);
209: break;
210: case (OUTT_HTML):
211: curp->outdata = html_alloc(curp->outopts);
212: break;
213: case (OUTT_ASCII):
214: curp->outdata = ascii_alloc(curp->outopts);
215: curp->outfree = ascii_free;
216: break;
217: case (OUTT_PDF):
218: curp->outdata = pdf_alloc(curp->outopts);
219: curp->outfree = pspdf_free;
220: break;
221: case (OUTT_PS):
222: curp->outdata = ps_alloc(curp->outopts);
223: curp->outfree = pspdf_free;
224: break;
225: default:
226: break;
227: }
228:
229: switch (curp->outtype) {
230: case (OUTT_HTML):
231: /* FALLTHROUGH */
232: case (OUTT_XHTML):
233: curp->outman = html_man;
234: curp->outmdoc = html_mdoc;
235: curp->outfree = html_free;
236: break;
237: case (OUTT_TREE):
238: curp->outman = tree_man;
239: curp->outmdoc = tree_mdoc;
240: break;
241: case (OUTT_PDF):
242: /* FALLTHROUGH */
243: case (OUTT_ASCII):
244: /* FALLTHROUGH */
245: case (OUTT_PS):
246: curp->outman = terminal_man;
247: curp->outmdoc = terminal_mdoc;
248: break;
249: default:
250: break;
251: }
252: }
253:
1.154 kristaps 254: mparse_result(curp->mp, &mdoc, &man);
255:
1.111 kristaps 256: /* Execute the out device, if it exists. */
257:
1.154 kristaps 258: if (man && curp->outman)
259: (*curp->outman)(curp->outdata, man);
260: if (mdoc && curp->outmdoc)
261: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 262:
263: cleanup:
1.112 kristaps 264:
1.154 kristaps 265: mparse_reset(curp->mp);
1.111 kristaps 266:
1.154 kristaps 267: if (*level < rc)
268: *level = rc;
1.10 kristaps 269: }
270:
271: static int
1.154 kristaps 272: moptions(enum mparset *tflags, char *arg)
1.10 kristaps 273: {
274:
1.17 kristaps 275: if (0 == strcmp(arg, "doc"))
1.154 kristaps 276: *tflags = MPARSE_MDOC;
1.19 kristaps 277: else if (0 == strcmp(arg, "andoc"))
1.154 kristaps 278: *tflags = MPARSE_AUTO;
1.17 kristaps 279: else if (0 == strcmp(arg, "an"))
1.154 kristaps 280: *tflags = MPARSE_MAN;
1.10 kristaps 281: else {
1.57 kristaps 282: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 283: return(0);
284: }
285:
286: return(1);
1.1 kristaps 287: }
288:
289: static int
1.60 kristaps 290: toptions(struct curparse *curp, char *arg)
1.1 kristaps 291: {
292:
293: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 294: curp->outtype = OUTT_ASCII;
295: else if (0 == strcmp(arg, "lint")) {
296: curp->outtype = OUTT_LINT;
1.103 schwarze 297: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 298: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 299: curp->outtype = OUTT_TREE;
1.43 kristaps 300: else if (0 == strcmp(arg, "html"))
1.60 kristaps 301: curp->outtype = OUTT_HTML;
1.59 kristaps 302: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 303: curp->outtype = OUTT_XHTML;
1.85 kristaps 304: else if (0 == strcmp(arg, "ps"))
305: curp->outtype = OUTT_PS;
1.100 kristaps 306: else if (0 == strcmp(arg, "pdf"))
307: curp->outtype = OUTT_PDF;
1.1 kristaps 308: else {
1.57 kristaps 309: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 310: return(0);
311: }
312:
313: return(1);
314: }
315:
316: static int
1.103 schwarze 317: woptions(struct curparse *curp, char *arg)
1.1 kristaps 318: {
1.34 kristaps 319: char *v, *o;
1.103 schwarze 320: const char *toks[6];
1.1 kristaps 321:
1.103 schwarze 322: toks[0] = "stop";
323: toks[1] = "all";
324: toks[2] = "warning";
325: toks[3] = "error";
326: toks[4] = "fatal";
327: toks[5] = NULL;
1.1 kristaps 328:
1.34 kristaps 329: while (*arg) {
330: o = arg;
1.45 kristaps 331: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 332: case (0):
1.103 schwarze 333: curp->wstop = 1;
1.1 kristaps 334: break;
335: case (1):
1.103 schwarze 336: /* FALLTHROUGH */
1.1 kristaps 337: case (2):
1.103 schwarze 338: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 339: break;
1.20 kristaps 340: case (3):
1.103 schwarze 341: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 342: break;
343: case (4):
1.103 schwarze 344: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 345: break;
1.1 kristaps 346: default:
1.103 schwarze 347: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 348: return(0);
349: }
1.34 kristaps 350: }
1.1 kristaps 351:
352: return(1);
353: }
354:
1.153 kristaps 355: static void
1.155 kristaps 356: mmsg(enum mandocerr t, enum mandoclevel lvl,
357: const char *file, int line, int col, const char *msg)
1.71 kristaps 358: {
1.103 schwarze 359:
1.155 kristaps 360: fprintf(stderr, "%s:%d:%d: %s: %s",
361: file, line, col + 1,
1.160 ! kristaps 362: mparse_strlevel(lvl),
! 363: mparse_strerror(t));
1.154 kristaps 364:
1.73 kristaps 365: if (msg)
366: fprintf(stderr, ": %s", msg);
1.154 kristaps 367:
1.73 kristaps 368: fputc('\n', stderr);
1.71 kristaps 369: }
CVSweb