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