1 /* $Id: main.c,v 1.161 2011/03/31 10:53:43 kristaps Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 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_TREE, /* -Ttree */
47 OUTT_HTML, /* -Thtml */
48 OUTT_XHTML, /* -Txhtml */
49 OUTT_LINT, /* -Tlint */
56 enum mandoclevel wlevel; /* ignore messages below this */
57 int wstop; /* stop after a file with a warning */
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 */
66 static int moptions(enum mparset *, char *);
67 static void mmsg(enum mandocerr, enum mandoclevel,
68 const char *, int, int, const char *);
69 static void parse(struct curparse *, int,
70 const char *, enum mandoclevel *);
71 static int toptions(struct curparse *, char *);
72 static void usage(void) __attribute__((noreturn));
73 static void version(void) __attribute__((noreturn));
74 static int woptions(struct curparse *, char *);
76 static const char *progname;
79 main(int argc, char *argv[])
86 progname = strrchr(argv[0], '/');
92 memset(&curp, 0, sizeof(struct curparse));
95 curp.outtype = OUTT_ASCII;
96 curp.wlevel = MANDOCLEVEL_FATAL;
99 while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
102 if ( ! moptions(&type, optarg))
103 return((int)MANDOCLEVEL_BADARG);
106 (void)strlcat(curp.outopts, optarg, BUFSIZ);
107 (void)strlcat(curp.outopts, ",", BUFSIZ);
110 if ( ! toptions(&curp, optarg))
111 return((int)MANDOCLEVEL_BADARG);
114 if ( ! woptions(&curp, optarg))
115 return((int)MANDOCLEVEL_BADARG);
125 curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
133 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
136 parse(&curp, -1, *argv, &rc);
137 if (MANDOCLEVEL_OK != rc && curp.wstop)
143 (*curp.outfree)(curp.outdata);
145 mparse_free(curp.mp);
154 printf("%s %s\n", progname, VERSION);
155 exit((int)MANDOCLEVEL_OK);
162 fprintf(stderr, "usage: %s "
172 exit((int)MANDOCLEVEL_BADARG);
176 parse(struct curparse *curp, int fd,
177 const char *file, enum mandoclevel *level)
183 /* Begin by parsing the file itself. */
188 rc = mparse_readfd(curp->mp, fd, file);
190 /* Stop immediately if the parse has failed. */
192 if (MANDOCLEVEL_FATAL <= rc)
196 * With -Wstop and warnings or errors of at least the requested
197 * level, do not produce output.
200 if (MANDOCLEVEL_OK != rc && curp->wstop)
203 /* If unset, allocate output dev now (if applicable). */
205 if ( ! (curp->outman && curp->outmdoc)) {
206 switch (curp->outtype) {
208 curp->outdata = xhtml_alloc(curp->outopts);
211 curp->outdata = html_alloc(curp->outopts);
214 curp->outdata = ascii_alloc(curp->outopts);
215 curp->outfree = ascii_free;
218 curp->outdata = pdf_alloc(curp->outopts);
219 curp->outfree = pspdf_free;
222 curp->outdata = ps_alloc(curp->outopts);
223 curp->outfree = pspdf_free;
229 switch (curp->outtype) {
233 curp->outman = html_man;
234 curp->outmdoc = html_mdoc;
235 curp->outfree = html_free;
238 curp->outman = tree_man;
239 curp->outmdoc = tree_mdoc;
246 curp->outman = terminal_man;
247 curp->outmdoc = terminal_mdoc;
254 mparse_result(curp->mp, &mdoc, &man);
256 /* Execute the out device, if it exists. */
258 if (man && curp->outman)
259 (*curp->outman)(curp->outdata, man);
260 if (mdoc && curp->outmdoc)
261 (*curp->outmdoc)(curp->outdata, mdoc);
265 mparse_reset(curp->mp);
272 moptions(enum mparset *tflags, char *arg)
275 if (0 == strcmp(arg, "doc"))
276 *tflags = MPARSE_MDOC;
277 else if (0 == strcmp(arg, "andoc"))
278 *tflags = MPARSE_AUTO;
279 else if (0 == strcmp(arg, "an"))
280 *tflags = MPARSE_MAN;
282 fprintf(stderr, "%s: Bad argument\n", arg);
290 toptions(struct curparse *curp, char *arg)
293 if (0 == strcmp(arg, "ascii"))
294 curp->outtype = OUTT_ASCII;
295 else if (0 == strcmp(arg, "lint")) {
296 curp->outtype = OUTT_LINT;
297 curp->wlevel = MANDOCLEVEL_WARNING;
298 } else if (0 == strcmp(arg, "tree"))
299 curp->outtype = OUTT_TREE;
300 else if (0 == strcmp(arg, "html"))
301 curp->outtype = OUTT_HTML;
302 else if (0 == strcmp(arg, "xhtml"))
303 curp->outtype = OUTT_XHTML;
304 else if (0 == strcmp(arg, "ps"))
305 curp->outtype = OUTT_PS;
306 else if (0 == strcmp(arg, "pdf"))
307 curp->outtype = OUTT_PDF;
309 fprintf(stderr, "%s: Bad argument\n", arg);
317 woptions(struct curparse *curp, char *arg)
331 switch (getsubopt(&arg, UNCONST(toks), &v)) {
338 curp->wlevel = MANDOCLEVEL_WARNING;
341 curp->wlevel = MANDOCLEVEL_ERROR;
344 curp->wlevel = MANDOCLEVEL_FATAL;
347 fprintf(stderr, "-W%s: Bad argument\n", o);
356 mmsg(enum mandocerr t, enum mandoclevel lvl,
357 const char *file, int line, int col, const char *msg)
360 fprintf(stderr, "%s:%d:%d: %s: %s",
362 mparse_strlevel(lvl),
366 fprintf(stderr, ": %s", msg);