1 /* $Id: main.c,v 1.177 2014/06/21 22:24:01 schwarze Exp $ */
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
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.
31 #include "mandoc_aux.h"
36 #if !defined(__GNUC__) || (__GNUC__ < 2)
38 # define __attribute__(x)
40 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
42 typedef void (*out_mdoc)(void *, const struct mdoc *);
43 typedef void (*out_man)(void *, const struct man *);
44 typedef void (*out_free)(void *);
47 OUTT_ASCII = 0, /* -Tascii */
48 OUTT_LOCALE, /* -Tlocale */
49 OUTT_UTF8, /* -Tutf8 */
50 OUTT_TREE, /* -Ttree */
52 OUTT_HTML, /* -Thtml */
53 OUTT_XHTML, /* -Txhtml */
54 OUTT_LINT, /* -Tlint */
61 enum mandoclevel wlevel; /* ignore messages below this */
62 int wstop; /* stop after a file with a warning */
63 enum outt outtype; /* which output to use */
64 out_mdoc outmdoc; /* mdoc output ptr */
65 out_man outman; /* man output ptr */
66 out_free outfree; /* free output ptr */
67 void *outdata; /* data for output */
68 char outopts[BUFSIZ]; /* buf of output opts */
71 static int moptions(int *, char *);
72 static void mmsg(enum mandocerr, enum mandoclevel,
73 const char *, int, int, const char *);
74 static void parse(struct curparse *, int,
75 const char *, enum mandoclevel *);
76 static int toptions(struct curparse *, char *);
77 static void usage(void) __attribute__((noreturn));
78 static void version(void) __attribute__((noreturn));
79 static int woptions(struct curparse *, char *);
81 static const char *progname;
85 main(int argc, char *argv[])
93 progname = strrchr(argv[0], '/');
99 memset(&curp, 0, sizeof(struct curparse));
102 curp.outtype = OUTT_ASCII;
103 curp.wlevel = MANDOCLEVEL_FATAL;
106 while (-1 != (c = getopt(argc, argv, "I:m:O:T:VW:")))
109 if (strncmp(optarg, "os=", 3)) {
111 "%s: -I%s: Bad argument\n",
113 return((int)MANDOCLEVEL_BADARG);
117 "%s: -I%s: Duplicate argument\n",
119 return((int)MANDOCLEVEL_BADARG);
121 defos = mandoc_strdup(optarg + 3);
124 if ( ! moptions(&options, optarg))
125 return((int)MANDOCLEVEL_BADARG);
128 (void)strlcat(curp.outopts, optarg, BUFSIZ);
129 (void)strlcat(curp.outopts, ",", BUFSIZ);
132 if ( ! toptions(&curp, optarg))
133 return((int)MANDOCLEVEL_BADARG);
136 if ( ! woptions(&curp, optarg))
137 return((int)MANDOCLEVEL_BADARG);
147 curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
150 * Conditionally start up the lookaside buffer before parsing.
152 if (OUTT_MAN == curp.outtype)
153 mparse_keep(curp.mp);
161 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
164 parse(&curp, -1, *argv, &rc);
165 if (MANDOCLEVEL_OK != rc && curp.wstop)
171 (*curp.outfree)(curp.outdata);
173 mparse_free(curp.mp);
183 printf("%s %s\n", progname, VERSION);
184 exit((int)MANDOCLEVEL_OK);
191 fprintf(stderr, "usage: %s "
201 exit((int)MANDOCLEVEL_BADARG);
205 parse(struct curparse *curp, int fd, const char *file,
206 enum mandoclevel *level)
212 /* Begin by parsing the file itself. */
217 rc = mparse_readfd(curp->mp, fd, file);
219 /* Stop immediately if the parse has failed. */
221 if (MANDOCLEVEL_FATAL <= rc)
225 * With -Wstop and warnings or errors of at least the requested
226 * level, do not produce output.
229 if (MANDOCLEVEL_OK != rc && curp->wstop)
232 /* If unset, allocate output dev now (if applicable). */
234 if ( ! (curp->outman && curp->outmdoc)) {
235 switch (curp->outtype) {
237 curp->outdata = xhtml_alloc(curp->outopts);
238 curp->outfree = html_free;
241 curp->outdata = html_alloc(curp->outopts);
242 curp->outfree = html_free;
245 curp->outdata = utf8_alloc(curp->outopts);
246 curp->outfree = ascii_free;
249 curp->outdata = locale_alloc(curp->outopts);
250 curp->outfree = ascii_free;
253 curp->outdata = ascii_alloc(curp->outopts);
254 curp->outfree = ascii_free;
257 curp->outdata = pdf_alloc(curp->outopts);
258 curp->outfree = pspdf_free;
261 curp->outdata = ps_alloc(curp->outopts);
262 curp->outfree = pspdf_free;
268 switch (curp->outtype) {
272 curp->outman = html_man;
273 curp->outmdoc = html_mdoc;
276 curp->outman = tree_man;
277 curp->outmdoc = tree_mdoc;
280 curp->outmdoc = man_mdoc;
281 curp->outman = man_man;
292 curp->outman = terminal_man;
293 curp->outmdoc = terminal_mdoc;
300 mparse_result(curp->mp, &mdoc, &man, NULL);
302 /* Execute the out device, if it exists. */
304 if (man && curp->outman)
305 (*curp->outman)(curp->outdata, man);
306 if (mdoc && curp->outmdoc)
307 (*curp->outmdoc)(curp->outdata, mdoc);
311 mparse_reset(curp->mp);
318 moptions(int *options, char *arg)
321 if (0 == strcmp(arg, "doc"))
322 *options |= MPARSE_MDOC;
323 else if (0 == strcmp(arg, "andoc"))
325 else if (0 == strcmp(arg, "an"))
326 *options |= MPARSE_MAN;
328 fprintf(stderr, "%s: -m%s: Bad argument\n",
337 toptions(struct curparse *curp, char *arg)
340 if (0 == strcmp(arg, "ascii"))
341 curp->outtype = OUTT_ASCII;
342 else if (0 == strcmp(arg, "lint")) {
343 curp->outtype = OUTT_LINT;
344 curp->wlevel = MANDOCLEVEL_WARNING;
345 } else if (0 == strcmp(arg, "tree"))
346 curp->outtype = OUTT_TREE;
347 else if (0 == strcmp(arg, "man"))
348 curp->outtype = OUTT_MAN;
349 else if (0 == strcmp(arg, "html"))
350 curp->outtype = OUTT_HTML;
351 else if (0 == strcmp(arg, "utf8"))
352 curp->outtype = OUTT_UTF8;
353 else if (0 == strcmp(arg, "locale"))
354 curp->outtype = OUTT_LOCALE;
355 else if (0 == strcmp(arg, "xhtml"))
356 curp->outtype = OUTT_XHTML;
357 else if (0 == strcmp(arg, "ps"))
358 curp->outtype = OUTT_PS;
359 else if (0 == strcmp(arg, "pdf"))
360 curp->outtype = OUTT_PDF;
362 fprintf(stderr, "%s: -T%s: Bad argument\n",
371 woptions(struct curparse *curp, char *arg)
385 switch (getsubopt(&arg, UNCONST(toks), &v)) {
392 curp->wlevel = MANDOCLEVEL_WARNING;
395 curp->wlevel = MANDOCLEVEL_ERROR;
398 curp->wlevel = MANDOCLEVEL_FATAL;
401 fprintf(stderr, "%s: -W%s: Bad argument\n",
411 mmsg(enum mandocerr t, enum mandoclevel lvl,
412 const char *file, int line, int col, const char *msg)
414 const char *mparse_msg;
416 fprintf(stderr, "%s: %s:", progname, file);
419 fprintf(stderr, "%d:%d:", line, col + 1);
421 fprintf(stderr, " %s", mparse_strlevel(lvl));
423 if (NULL != (mparse_msg = mparse_strerror(t)))
424 fprintf(stderr, ": %s", mparse_msg);
427 fprintf(stderr, ": %s", msg);