Annotation of mandoc/main.c, Revision 1.169
1.169 ! schwarze 1: /* $Id: main.c,v 1.168 2014/01/05 20:26:36 schwarze Exp $ */
1.1 kristaps 2: /*
1.133 schwarze 3: * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.166 schwarze 4: * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.169 ! schwarze 5: * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
1.1 kristaps 6: *
7: * Permission to use, copy, modify, and distribute this software for any
1.25 kristaps 8: * purpose with or without fee is hereby granted, provided that the above
9: * copyright notice and this permission notice appear in all copies.
1.1 kristaps 10: *
1.25 kristaps 11: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1 kristaps 18: */
1.58 kristaps 19: #ifdef HAVE_CONFIG_H
20: #include "config.h"
21: #endif
22:
1.1 kristaps 23: #include <assert.h>
24: #include <stdio.h>
1.45 kristaps 25: #include <stdint.h>
1.1 kristaps 26: #include <stdlib.h>
27: #include <string.h>
28: #include <unistd.h>
29:
1.71 kristaps 30: #include "mandoc.h"
1.90 kristaps 31: #include "main.h"
1.1 kristaps 32: #include "mdoc.h"
1.10 kristaps 33: #include "man.h"
1.101 joerg 34:
1.55 kristaps 35: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56 kristaps 36: # if !defined(lint)
37: # define __attribute__(x)
38: # endif
1.55 kristaps 39: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16 kristaps 40:
1.42 kristaps 41: typedef void (*out_mdoc)(void *, const struct mdoc *);
42: typedef void (*out_man)(void *, const struct man *);
1.22 kristaps 43: typedef void (*out_free)(void *);
44:
1.19 kristaps 45: enum outt {
1.154 kristaps 46: OUTT_ASCII = 0, /* -Tascii */
1.162 kristaps 47: OUTT_LOCALE, /* -Tlocale */
1.163 kristaps 48: OUTT_UTF8, /* -Tutf8 */
1.154 kristaps 49: OUTT_TREE, /* -Ttree */
1.164 schwarze 50: OUTT_MAN, /* -Tman */
1.154 kristaps 51: OUTT_HTML, /* -Thtml */
52: OUTT_XHTML, /* -Txhtml */
53: OUTT_LINT, /* -Tlint */
54: OUTT_PS, /* -Tps */
55: OUTT_PDF /* -Tpdf */
1.19 kristaps 56: };
57:
1.5 kristaps 58: struct curparse {
1.154 kristaps 59: struct mparse *mp;
1.148 kristaps 60: enum mandoclevel wlevel; /* ignore messages below this */
61: int wstop; /* stop after a file with a warning */
1.79 kristaps 62: enum outt outtype; /* which output to use */
63: out_mdoc outmdoc; /* mdoc output ptr */
64: out_man outman; /* man output ptr */
65: out_free outfree; /* free output ptr */
66: void *outdata; /* data for output */
67: char outopts[BUFSIZ]; /* buf of output opts */
68: };
69:
1.154 kristaps 70: static int moptions(enum mparset *, char *);
1.155 kristaps 71: static void mmsg(enum mandocerr, enum mandoclevel,
72: const char *, int, int, const char *);
1.154 kristaps 73: static void parse(struct curparse *, int,
74: const char *, enum mandoclevel *);
1.66 kristaps 75: static int toptions(struct curparse *, char *);
76: static void usage(void) __attribute__((noreturn));
1.55 kristaps 77: static void version(void) __attribute__((noreturn));
1.103 schwarze 78: static int woptions(struct curparse *, char *);
1.1 kristaps 79:
1.54 kristaps 80: static const char *progname;
1.1 kristaps 81:
82: int
83: main(int argc, char *argv[])
84: {
1.64 kristaps 85: int c;
1.5 kristaps 86: struct curparse curp;
1.154 kristaps 87: enum mparset type;
88: enum mandoclevel rc;
1.166 schwarze 89: char *defos;
1.1 kristaps 90:
1.54 kristaps 91: progname = strrchr(argv[0], '/');
92: if (progname == NULL)
93: progname = argv[0];
94: else
95: ++progname;
96:
1.51 kristaps 97: memset(&curp, 0, sizeof(struct curparse));
1.5 kristaps 98:
1.154 kristaps 99: type = MPARSE_AUTO;
1.22 kristaps 100: curp.outtype = OUTT_ASCII;
1.103 schwarze 101: curp.wlevel = MANDOCLEVEL_FATAL;
1.166 schwarze 102: defos = NULL;
1.19 kristaps 103:
1.1 kristaps 104: /* LINTED */
1.166 schwarze 105: while (-1 != (c = getopt(argc, argv, "I:m:O:T:VW:")))
1.1 kristaps 106: switch (c) {
1.166 schwarze 107: case ('I'):
108: if (strncmp(optarg, "os=", 3)) {
109: fprintf(stderr, "-I%s: Bad argument\n",
110: optarg);
111: return((int)MANDOCLEVEL_BADARG);
112: }
113: if (defos) {
114: fprintf(stderr, "-I%s: Duplicate argument\n",
115: optarg);
116: return((int)MANDOCLEVEL_BADARG);
117: }
118: defos = mandoc_strdup(optarg + 3);
119: break;
1.10 kristaps 120: case ('m'):
1.154 kristaps 121: if ( ! moptions(&type, optarg))
1.105 kristaps 122: return((int)MANDOCLEVEL_BADARG);
1.10 kristaps 123: break;
1.48 kristaps 124: case ('O'):
1.49 kristaps 125: (void)strlcat(curp.outopts, optarg, BUFSIZ);
126: (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44 kristaps 127: break;
1.1 kristaps 128: case ('T'):
1.60 kristaps 129: if ( ! toptions(&curp, optarg))
1.105 kristaps 130: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 131: break;
132: case ('W'):
1.103 schwarze 133: if ( ! woptions(&curp, optarg))
1.105 kristaps 134: return((int)MANDOCLEVEL_BADARG);
1.1 kristaps 135: break;
136: case ('V'):
137: version();
138: /* NOTREACHED */
139: default:
140: usage();
141: /* NOTREACHED */
142: }
143:
1.168 schwarze 144: curp.mp = mparse_alloc(type, curp.wlevel, mmsg, defos, 0);
1.154 kristaps 145:
1.165 kristaps 146: /*
147: * Conditionally start up the lookaside buffer before parsing.
148: */
149: if (OUTT_MAN == curp.outtype)
150: mparse_keep(curp.mp);
151:
1.1 kristaps 152: argc -= optind;
153: argv += optind;
154:
1.154 kristaps 155: rc = MANDOCLEVEL_OK;
1.39 kristaps 156:
1.154 kristaps 157: if (NULL == *argv)
158: parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64 kristaps 159:
160: while (*argv) {
1.154 kristaps 161: parse(&curp, -1, *argv, &rc);
162: if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64 kristaps 163: break;
164: ++argv;
1.1 kristaps 165: }
166:
1.22 kristaps 167: if (curp.outfree)
168: (*curp.outfree)(curp.outdata);
1.154 kristaps 169: if (curp.mp)
170: mparse_free(curp.mp);
1.166 schwarze 171: free(defos);
1.1 kristaps 172:
1.154 kristaps 173: return((int)rc);
1.1 kristaps 174: }
175:
1.55 kristaps 176: static void
1.1 kristaps 177: version(void)
178: {
179:
1.154 kristaps 180: printf("%s %s\n", progname, VERSION);
1.105 kristaps 181: exit((int)MANDOCLEVEL_OK);
1.1 kristaps 182: }
183:
1.55 kristaps 184: static void
1.1 kristaps 185: usage(void)
186: {
187:
1.154 kristaps 188: fprintf(stderr, "usage: %s "
1.112 kristaps 189: "[-V] "
1.167 schwarze 190: "[-Ios=name] "
1.112 kristaps 191: "[-mformat] "
192: "[-Ooption] "
193: "[-Toutput] "
1.167 schwarze 194: "[-Wlevel]\n"
195: "\t [file ...]\n",
1.112 kristaps 196: progname);
197:
1.105 kristaps 198: exit((int)MANDOCLEVEL_BADARG);
1.68 joerg 199: }
200:
1.64 kristaps 201: static void
1.154 kristaps 202: parse(struct curparse *curp, int fd,
203: const char *file, enum mandoclevel *level)
1.1 kristaps 204: {
1.154 kristaps 205: enum mandoclevel rc;
206: struct mdoc *mdoc;
207: struct man *man;
1.112 kristaps 208:
1.154 kristaps 209: /* Begin by parsing the file itself. */
1.112 kristaps 210:
1.154 kristaps 211: assert(file);
212: assert(fd >= -1);
1.112 kristaps 213:
1.154 kristaps 214: rc = mparse_readfd(curp->mp, fd, file);
1.112 kristaps 215:
1.154 kristaps 216: /* Stop immediately if the parse has failed. */
1.92 kristaps 217:
1.154 kristaps 218: if (MANDOCLEVEL_FATAL <= rc)
1.111 kristaps 219: goto cleanup;
220:
1.1 kristaps 221: /*
1.154 kristaps 222: * With -Wstop and warnings or errors of at least the requested
223: * level, do not produce output.
1.1 kristaps 224: */
225:
1.154 kristaps 226: if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111 kristaps 227: goto cleanup;
228:
229: /* If unset, allocate output dev now (if applicable). */
230:
231: if ( ! (curp->outman && curp->outmdoc)) {
232: switch (curp->outtype) {
233: case (OUTT_XHTML):
234: curp->outdata = xhtml_alloc(curp->outopts);
1.162 kristaps 235: curp->outfree = html_free;
1.111 kristaps 236: break;
237: case (OUTT_HTML):
238: curp->outdata = html_alloc(curp->outopts);
1.162 kristaps 239: curp->outfree = html_free;
240: break;
1.163 kristaps 241: case (OUTT_UTF8):
242: curp->outdata = utf8_alloc(curp->outopts);
243: curp->outfree = ascii_free;
244: break;
1.162 kristaps 245: case (OUTT_LOCALE):
246: curp->outdata = locale_alloc(curp->outopts);
247: curp->outfree = ascii_free;
1.111 kristaps 248: break;
249: case (OUTT_ASCII):
250: curp->outdata = ascii_alloc(curp->outopts);
251: curp->outfree = ascii_free;
252: break;
253: case (OUTT_PDF):
254: curp->outdata = pdf_alloc(curp->outopts);
255: curp->outfree = pspdf_free;
256: break;
257: case (OUTT_PS):
258: curp->outdata = ps_alloc(curp->outopts);
259: curp->outfree = pspdf_free;
260: break;
261: default:
262: break;
263: }
264:
265: switch (curp->outtype) {
266: case (OUTT_HTML):
267: /* FALLTHROUGH */
268: case (OUTT_XHTML):
269: curp->outman = html_man;
270: curp->outmdoc = html_mdoc;
271: break;
272: case (OUTT_TREE):
273: curp->outman = tree_man;
274: curp->outmdoc = tree_mdoc;
275: break;
1.164 schwarze 276: case (OUTT_MAN):
277: curp->outmdoc = man_mdoc;
1.165 kristaps 278: curp->outman = man_man;
1.164 schwarze 279: break;
1.111 kristaps 280: case (OUTT_PDF):
281: /* FALLTHROUGH */
282: case (OUTT_ASCII):
283: /* FALLTHROUGH */
1.163 kristaps 284: case (OUTT_UTF8):
285: /* FALLTHROUGH */
1.162 kristaps 286: case (OUTT_LOCALE):
287: /* FALLTHROUGH */
1.111 kristaps 288: case (OUTT_PS):
289: curp->outman = terminal_man;
290: curp->outmdoc = terminal_mdoc;
291: break;
292: default:
293: break;
294: }
295: }
296:
1.154 kristaps 297: mparse_result(curp->mp, &mdoc, &man);
298:
1.111 kristaps 299: /* Execute the out device, if it exists. */
300:
1.154 kristaps 301: if (man && curp->outman)
302: (*curp->outman)(curp->outdata, man);
303: if (mdoc && curp->outmdoc)
304: (*curp->outmdoc)(curp->outdata, mdoc);
1.111 kristaps 305:
306: cleanup:
1.112 kristaps 307:
1.154 kristaps 308: mparse_reset(curp->mp);
1.111 kristaps 309:
1.154 kristaps 310: if (*level < rc)
311: *level = rc;
1.10 kristaps 312: }
313:
314: static int
1.154 kristaps 315: moptions(enum mparset *tflags, char *arg)
1.10 kristaps 316: {
317:
1.17 kristaps 318: if (0 == strcmp(arg, "doc"))
1.154 kristaps 319: *tflags = MPARSE_MDOC;
1.19 kristaps 320: else if (0 == strcmp(arg, "andoc"))
1.154 kristaps 321: *tflags = MPARSE_AUTO;
1.17 kristaps 322: else if (0 == strcmp(arg, "an"))
1.154 kristaps 323: *tflags = MPARSE_MAN;
1.10 kristaps 324: else {
1.57 kristaps 325: fprintf(stderr, "%s: Bad argument\n", arg);
1.10 kristaps 326: return(0);
327: }
328:
329: return(1);
1.1 kristaps 330: }
331:
332: static int
1.60 kristaps 333: toptions(struct curparse *curp, char *arg)
1.1 kristaps 334: {
335:
336: if (0 == strcmp(arg, "ascii"))
1.60 kristaps 337: curp->outtype = OUTT_ASCII;
338: else if (0 == strcmp(arg, "lint")) {
339: curp->outtype = OUTT_LINT;
1.103 schwarze 340: curp->wlevel = MANDOCLEVEL_WARNING;
1.154 kristaps 341: } else if (0 == strcmp(arg, "tree"))
1.60 kristaps 342: curp->outtype = OUTT_TREE;
1.164 schwarze 343: else if (0 == strcmp(arg, "man"))
344: curp->outtype = OUTT_MAN;
1.43 kristaps 345: else if (0 == strcmp(arg, "html"))
1.60 kristaps 346: curp->outtype = OUTT_HTML;
1.163 kristaps 347: else if (0 == strcmp(arg, "utf8"))
348: curp->outtype = OUTT_UTF8;
1.162 kristaps 349: else if (0 == strcmp(arg, "locale"))
350: curp->outtype = OUTT_LOCALE;
1.59 kristaps 351: else if (0 == strcmp(arg, "xhtml"))
1.60 kristaps 352: curp->outtype = OUTT_XHTML;
1.85 kristaps 353: else if (0 == strcmp(arg, "ps"))
354: curp->outtype = OUTT_PS;
1.100 kristaps 355: else if (0 == strcmp(arg, "pdf"))
356: curp->outtype = OUTT_PDF;
1.1 kristaps 357: else {
1.57 kristaps 358: fprintf(stderr, "%s: Bad argument\n", arg);
1.1 kristaps 359: return(0);
360: }
361:
362: return(1);
363: }
364:
365: static int
1.103 schwarze 366: woptions(struct curparse *curp, char *arg)
1.1 kristaps 367: {
1.34 kristaps 368: char *v, *o;
1.103 schwarze 369: const char *toks[6];
1.1 kristaps 370:
1.103 schwarze 371: toks[0] = "stop";
372: toks[1] = "all";
373: toks[2] = "warning";
374: toks[3] = "error";
375: toks[4] = "fatal";
376: toks[5] = NULL;
1.1 kristaps 377:
1.34 kristaps 378: while (*arg) {
379: o = arg;
1.45 kristaps 380: switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1 kristaps 381: case (0):
1.103 schwarze 382: curp->wstop = 1;
1.1 kristaps 383: break;
384: case (1):
1.103 schwarze 385: /* FALLTHROUGH */
1.1 kristaps 386: case (2):
1.103 schwarze 387: curp->wlevel = MANDOCLEVEL_WARNING;
1.1 kristaps 388: break;
1.20 kristaps 389: case (3):
1.103 schwarze 390: curp->wlevel = MANDOCLEVEL_ERROR;
1.24 kristaps 391: break;
392: case (4):
1.103 schwarze 393: curp->wlevel = MANDOCLEVEL_FATAL;
1.50 kristaps 394: break;
1.1 kristaps 395: default:
1.103 schwarze 396: fprintf(stderr, "-W%s: Bad argument\n", o);
1.1 kristaps 397: return(0);
398: }
1.34 kristaps 399: }
1.1 kristaps 400:
401: return(1);
402: }
403:
1.153 kristaps 404: static void
1.155 kristaps 405: mmsg(enum mandocerr t, enum mandoclevel lvl,
406: const char *file, int line, int col, const char *msg)
1.71 kristaps 407: {
1.103 schwarze 408:
1.155 kristaps 409: fprintf(stderr, "%s:%d:%d: %s: %s",
410: file, line, col + 1,
1.160 kristaps 411: mparse_strlevel(lvl),
412: mparse_strerror(t));
1.154 kristaps 413:
1.73 kristaps 414: if (msg)
415: fprintf(stderr, ": %s", msg);
1.154 kristaps 416:
1.73 kristaps 417: fputc('\n', stderr);
1.71 kristaps 418: }
CVSweb