1 /* $Id: main.c,v 1.167 2012/11/19 17:22:26 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
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.
34 #if !defined(__GNUC__) || (__GNUC__ < 2)
36 # define __attribute__(x)
38 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
40 typedef void (*out_mdoc)(void *, const struct mdoc *);
41 typedef void (*out_man)(void *, const struct man *);
42 typedef void (*out_free)(void *);
45 OUTT_ASCII = 0, /* -Tascii */
46 OUTT_LOCALE, /* -Tlocale */
47 OUTT_UTF8, /* -Tutf8 */
48 OUTT_TREE, /* -Ttree */
50 OUTT_HTML, /* -Thtml */
51 OUTT_XHTML, /* -Txhtml */
52 OUTT_LINT, /* -Tlint */
59 enum mandoclevel wlevel; /* ignore messages below this */
60 int wstop; /* stop after a file with a warning */
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 */
69 static int moptions(enum mparset *, char *);
70 static void mmsg(enum mandocerr, enum mandoclevel,
71 const char *, int, int, const char *);
72 static void parse(struct curparse *, int,
73 const char *, enum mandoclevel *);
74 static int toptions(struct curparse *, char *);
75 static void usage(void) __attribute__((noreturn));
76 static void version(void) __attribute__((noreturn));
77 static int woptions(struct curparse *, char *);
79 static const char *progname;
82 main(int argc, char *argv[])
90 progname = strrchr(argv[0], '/');
96 memset(&curp, 0, sizeof(struct curparse));
99 curp.outtype = OUTT_ASCII;
100 curp.wlevel = MANDOCLEVEL_FATAL;
104 while (-1 != (c = getopt(argc, argv, "I:m:O:T:VW:")))
107 if (strncmp(optarg, "os=", 3)) {
108 fprintf(stderr, "-I%s: Bad argument\n",
110 return((int)MANDOCLEVEL_BADARG);
113 fprintf(stderr, "-I%s: Duplicate argument\n",
115 return((int)MANDOCLEVEL_BADARG);
117 defos = mandoc_strdup(optarg + 3);
120 if ( ! moptions(&type, optarg))
121 return((int)MANDOCLEVEL_BADARG);
124 (void)strlcat(curp.outopts, optarg, BUFSIZ);
125 (void)strlcat(curp.outopts, ",", BUFSIZ);
128 if ( ! toptions(&curp, optarg))
129 return((int)MANDOCLEVEL_BADARG);
132 if ( ! woptions(&curp, optarg))
133 return((int)MANDOCLEVEL_BADARG);
143 curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp, defos);
146 * Conditionally start up the lookaside buffer before parsing.
148 if (OUTT_MAN == curp.outtype)
149 mparse_keep(curp.mp);
157 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
160 parse(&curp, -1, *argv, &rc);
161 if (MANDOCLEVEL_OK != rc && curp.wstop)
167 (*curp.outfree)(curp.outdata);
169 mparse_free(curp.mp);
179 printf("%s %s\n", progname, VERSION);
180 exit((int)MANDOCLEVEL_OK);
187 fprintf(stderr, "usage: %s "
197 exit((int)MANDOCLEVEL_BADARG);
201 parse(struct curparse *curp, int fd,
202 const char *file, enum mandoclevel *level)
208 /* Begin by parsing the file itself. */
213 rc = mparse_readfd(curp->mp, fd, file);
215 /* Stop immediately if the parse has failed. */
217 if (MANDOCLEVEL_FATAL <= rc)
221 * With -Wstop and warnings or errors of at least the requested
222 * level, do not produce output.
225 if (MANDOCLEVEL_OK != rc && curp->wstop)
228 /* If unset, allocate output dev now (if applicable). */
230 if ( ! (curp->outman && curp->outmdoc)) {
231 switch (curp->outtype) {
233 curp->outdata = xhtml_alloc(curp->outopts);
234 curp->outfree = html_free;
237 curp->outdata = html_alloc(curp->outopts);
238 curp->outfree = html_free;
241 curp->outdata = utf8_alloc(curp->outopts);
242 curp->outfree = ascii_free;
245 curp->outdata = locale_alloc(curp->outopts);
246 curp->outfree = ascii_free;
249 curp->outdata = ascii_alloc(curp->outopts);
250 curp->outfree = ascii_free;
253 curp->outdata = pdf_alloc(curp->outopts);
254 curp->outfree = pspdf_free;
257 curp->outdata = ps_alloc(curp->outopts);
258 curp->outfree = pspdf_free;
264 switch (curp->outtype) {
268 curp->outman = html_man;
269 curp->outmdoc = html_mdoc;
272 curp->outman = tree_man;
273 curp->outmdoc = tree_mdoc;
276 curp->outmdoc = man_mdoc;
277 curp->outman = man_man;
288 curp->outman = terminal_man;
289 curp->outmdoc = terminal_mdoc;
296 mparse_result(curp->mp, &mdoc, &man);
298 /* Execute the out device, if it exists. */
300 if (man && curp->outman)
301 (*curp->outman)(curp->outdata, man);
302 if (mdoc && curp->outmdoc)
303 (*curp->outmdoc)(curp->outdata, mdoc);
307 mparse_reset(curp->mp);
314 moptions(enum mparset *tflags, char *arg)
317 if (0 == strcmp(arg, "doc"))
318 *tflags = MPARSE_MDOC;
319 else if (0 == strcmp(arg, "andoc"))
320 *tflags = MPARSE_AUTO;
321 else if (0 == strcmp(arg, "an"))
322 *tflags = MPARSE_MAN;
324 fprintf(stderr, "%s: Bad argument\n", arg);
332 toptions(struct curparse *curp, char *arg)
335 if (0 == strcmp(arg, "ascii"))
336 curp->outtype = OUTT_ASCII;
337 else if (0 == strcmp(arg, "lint")) {
338 curp->outtype = OUTT_LINT;
339 curp->wlevel = MANDOCLEVEL_WARNING;
340 } else if (0 == strcmp(arg, "tree"))
341 curp->outtype = OUTT_TREE;
342 else if (0 == strcmp(arg, "man"))
343 curp->outtype = OUTT_MAN;
344 else if (0 == strcmp(arg, "html"))
345 curp->outtype = OUTT_HTML;
346 else if (0 == strcmp(arg, "utf8"))
347 curp->outtype = OUTT_UTF8;
348 else if (0 == strcmp(arg, "locale"))
349 curp->outtype = OUTT_LOCALE;
350 else if (0 == strcmp(arg, "xhtml"))
351 curp->outtype = OUTT_XHTML;
352 else if (0 == strcmp(arg, "ps"))
353 curp->outtype = OUTT_PS;
354 else if (0 == strcmp(arg, "pdf"))
355 curp->outtype = OUTT_PDF;
357 fprintf(stderr, "%s: Bad argument\n", arg);
365 woptions(struct curparse *curp, char *arg)
379 switch (getsubopt(&arg, UNCONST(toks), &v)) {
386 curp->wlevel = MANDOCLEVEL_WARNING;
389 curp->wlevel = MANDOCLEVEL_ERROR;
392 curp->wlevel = MANDOCLEVEL_FATAL;
395 fprintf(stderr, "-W%s: Bad argument\n", o);
404 mmsg(enum mandocerr t, enum mandoclevel lvl,
405 const char *file, int line, int col, const char *msg)
408 fprintf(stderr, "%s:%d:%d: %s: %s",
410 mparse_strlevel(lvl),
414 fprintf(stderr, ": %s", msg);