1 /* $Id: main.c,v 1.163 2011/05/20 15:51:18 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_LOCALE, /* -Tlocale */
47 OUTT_UTF8, /* -Tutf8 */
48 OUTT_TREE, /* -Ttree */
49 OUTT_HTML, /* -Thtml */
50 OUTT_XHTML, /* -Txhtml */
51 OUTT_LINT, /* -Tlint */
58 enum mandoclevel wlevel; /* ignore messages below this */
59 int wstop; /* stop after a file with a warning */
60 enum outt outtype; /* which output to use */
61 out_mdoc outmdoc; /* mdoc output ptr */
62 out_man outman; /* man output ptr */
63 out_free outfree; /* free output ptr */
64 void *outdata; /* data for output */
65 char outopts[BUFSIZ]; /* buf of output opts */
68 static int moptions(enum mparset *, char *);
69 static void mmsg(enum mandocerr, enum mandoclevel,
70 const char *, int, int, const char *);
71 static void parse(struct curparse *, int,
72 const char *, enum mandoclevel *);
73 static int toptions(struct curparse *, char *);
74 static void usage(void) __attribute__((noreturn));
75 static void version(void) __attribute__((noreturn));
76 static int woptions(struct curparse *, char *);
78 static const char *progname;
81 main(int argc, char *argv[])
88 progname = strrchr(argv[0], '/');
94 memset(&curp, 0, sizeof(struct curparse));
97 curp.outtype = OUTT_ASCII;
98 curp.wlevel = MANDOCLEVEL_FATAL;
101 while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
104 if ( ! moptions(&type, optarg))
105 return((int)MANDOCLEVEL_BADARG);
108 (void)strlcat(curp.outopts, optarg, BUFSIZ);
109 (void)strlcat(curp.outopts, ",", BUFSIZ);
112 if ( ! toptions(&curp, optarg))
113 return((int)MANDOCLEVEL_BADARG);
116 if ( ! woptions(&curp, optarg))
117 return((int)MANDOCLEVEL_BADARG);
127 curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
135 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
138 parse(&curp, -1, *argv, &rc);
139 if (MANDOCLEVEL_OK != rc && curp.wstop)
145 (*curp.outfree)(curp.outdata);
147 mparse_free(curp.mp);
156 printf("%s %s\n", progname, VERSION);
157 exit((int)MANDOCLEVEL_OK);
164 fprintf(stderr, "usage: %s "
174 exit((int)MANDOCLEVEL_BADARG);
178 parse(struct curparse *curp, int fd,
179 const char *file, enum mandoclevel *level)
185 /* Begin by parsing the file itself. */
190 rc = mparse_readfd(curp->mp, fd, file);
192 /* Stop immediately if the parse has failed. */
194 if (MANDOCLEVEL_FATAL <= rc)
198 * With -Wstop and warnings or errors of at least the requested
199 * level, do not produce output.
202 if (MANDOCLEVEL_OK != rc && curp->wstop)
205 /* If unset, allocate output dev now (if applicable). */
207 if ( ! (curp->outman && curp->outmdoc)) {
208 switch (curp->outtype) {
210 curp->outdata = xhtml_alloc(curp->outopts);
211 curp->outfree = html_free;
214 curp->outdata = html_alloc(curp->outopts);
215 curp->outfree = html_free;
218 curp->outdata = utf8_alloc(curp->outopts);
219 curp->outfree = ascii_free;
222 curp->outdata = locale_alloc(curp->outopts);
223 curp->outfree = ascii_free;
226 curp->outdata = ascii_alloc(curp->outopts);
227 curp->outfree = ascii_free;
230 curp->outdata = pdf_alloc(curp->outopts);
231 curp->outfree = pspdf_free;
234 curp->outdata = ps_alloc(curp->outopts);
235 curp->outfree = pspdf_free;
241 switch (curp->outtype) {
245 curp->outman = html_man;
246 curp->outmdoc = html_mdoc;
249 curp->outman = tree_man;
250 curp->outmdoc = tree_mdoc;
261 curp->outman = terminal_man;
262 curp->outmdoc = terminal_mdoc;
269 mparse_result(curp->mp, &mdoc, &man);
271 /* Execute the out device, if it exists. */
273 if (man && curp->outman)
274 (*curp->outman)(curp->outdata, man);
275 if (mdoc && curp->outmdoc)
276 (*curp->outmdoc)(curp->outdata, mdoc);
280 mparse_reset(curp->mp);
287 moptions(enum mparset *tflags, char *arg)
290 if (0 == strcmp(arg, "doc"))
291 *tflags = MPARSE_MDOC;
292 else if (0 == strcmp(arg, "andoc"))
293 *tflags = MPARSE_AUTO;
294 else if (0 == strcmp(arg, "an"))
295 *tflags = MPARSE_MAN;
297 fprintf(stderr, "%s: Bad argument\n", arg);
305 toptions(struct curparse *curp, char *arg)
308 if (0 == strcmp(arg, "ascii"))
309 curp->outtype = OUTT_ASCII;
310 else if (0 == strcmp(arg, "lint")) {
311 curp->outtype = OUTT_LINT;
312 curp->wlevel = MANDOCLEVEL_WARNING;
313 } else if (0 == strcmp(arg, "tree"))
314 curp->outtype = OUTT_TREE;
315 else if (0 == strcmp(arg, "html"))
316 curp->outtype = OUTT_HTML;
317 else if (0 == strcmp(arg, "utf8"))
318 curp->outtype = OUTT_UTF8;
319 else if (0 == strcmp(arg, "locale"))
320 curp->outtype = OUTT_LOCALE;
321 else if (0 == strcmp(arg, "xhtml"))
322 curp->outtype = OUTT_XHTML;
323 else if (0 == strcmp(arg, "ps"))
324 curp->outtype = OUTT_PS;
325 else if (0 == strcmp(arg, "pdf"))
326 curp->outtype = OUTT_PDF;
328 fprintf(stderr, "%s: Bad argument\n", arg);
336 woptions(struct curparse *curp, char *arg)
350 switch (getsubopt(&arg, UNCONST(toks), &v)) {
357 curp->wlevel = MANDOCLEVEL_WARNING;
360 curp->wlevel = MANDOCLEVEL_ERROR;
363 curp->wlevel = MANDOCLEVEL_FATAL;
366 fprintf(stderr, "-W%s: Bad argument\n", o);
375 mmsg(enum mandocerr t, enum mandoclevel lvl,
376 const char *file, int line, int col, const char *msg)
379 fprintf(stderr, "%s:%d:%d: %s: %s",
381 mparse_strlevel(lvl),
385 fprintf(stderr, ": %s", msg);