]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.162 2011/05/17 14:38:34 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_TREE
, /* -Ttree */
48 OUTT_HTML
, /* -Thtml */
49 OUTT_XHTML
, /* -Txhtml */
50 OUTT_LINT
, /* -Tlint */
57 enum mandoclevel wlevel
; /* ignore messages below this */
58 int wstop
; /* stop after a file with a warning */
59 enum outt outtype
; /* which output to use */
60 out_mdoc outmdoc
; /* mdoc output ptr */
61 out_man outman
; /* man output ptr */
62 out_free outfree
; /* free output ptr */
63 void *outdata
; /* data for output */
64 char outopts
[BUFSIZ
]; /* buf of output opts */
67 static int moptions(enum mparset
*, char *);
68 static void mmsg(enum mandocerr
, enum mandoclevel
,
69 const char *, int, int, const char *);
70 static void parse(struct curparse
*, int,
71 const char *, enum mandoclevel
*);
72 static int toptions(struct curparse
*, char *);
73 static void usage(void) __attribute__((noreturn
));
74 static void version(void) __attribute__((noreturn
));
75 static int woptions(struct curparse
*, char *);
77 static const char *progname
;
80 main(int argc
, char *argv
[])
87 progname
= strrchr(argv
[0], '/');
93 memset(&curp
, 0, sizeof(struct curparse
));
96 curp
.outtype
= OUTT_ASCII
;
97 curp
.wlevel
= MANDOCLEVEL_FATAL
;
100 while (-1 != (c
= getopt(argc
, argv
, "m:O:T:VW:")))
103 if ( ! moptions(&type
, optarg
))
104 return((int)MANDOCLEVEL_BADARG
);
107 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
108 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
111 if ( ! toptions(&curp
, optarg
))
112 return((int)MANDOCLEVEL_BADARG
);
115 if ( ! woptions(&curp
, optarg
))
116 return((int)MANDOCLEVEL_BADARG
);
126 curp
.mp
= mparse_alloc(type
, curp
.wlevel
, mmsg
, &curp
);
134 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
137 parse(&curp
, -1, *argv
, &rc
);
138 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
144 (*curp
.outfree
)(curp
.outdata
);
146 mparse_free(curp
.mp
);
155 printf("%s %s\n", progname
, VERSION
);
156 exit((int)MANDOCLEVEL_OK
);
163 fprintf(stderr
, "usage: %s "
173 exit((int)MANDOCLEVEL_BADARG
);
177 parse(struct curparse
*curp
, int fd
,
178 const char *file
, enum mandoclevel
*level
)
184 /* Begin by parsing the file itself. */
189 rc
= mparse_readfd(curp
->mp
, fd
, file
);
191 /* Stop immediately if the parse has failed. */
193 if (MANDOCLEVEL_FATAL
<= rc
)
197 * With -Wstop and warnings or errors of at least the requested
198 * level, do not produce output.
201 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
204 /* If unset, allocate output dev now (if applicable). */
206 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
207 switch (curp
->outtype
) {
209 curp
->outdata
= xhtml_alloc(curp
->outopts
);
210 curp
->outfree
= html_free
;
213 curp
->outdata
= html_alloc(curp
->outopts
);
214 curp
->outfree
= html_free
;
217 curp
->outdata
= locale_alloc(curp
->outopts
);
218 curp
->outfree
= ascii_free
;
221 curp
->outdata
= ascii_alloc(curp
->outopts
);
222 curp
->outfree
= ascii_free
;
225 curp
->outdata
= pdf_alloc(curp
->outopts
);
226 curp
->outfree
= pspdf_free
;
229 curp
->outdata
= ps_alloc(curp
->outopts
);
230 curp
->outfree
= pspdf_free
;
236 switch (curp
->outtype
) {
240 curp
->outman
= html_man
;
241 curp
->outmdoc
= html_mdoc
;
244 curp
->outman
= tree_man
;
245 curp
->outmdoc
= tree_mdoc
;
254 curp
->outman
= terminal_man
;
255 curp
->outmdoc
= terminal_mdoc
;
262 mparse_result(curp
->mp
, &mdoc
, &man
);
264 /* Execute the out device, if it exists. */
266 if (man
&& curp
->outman
)
267 (*curp
->outman
)(curp
->outdata
, man
);
268 if (mdoc
&& curp
->outmdoc
)
269 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
273 mparse_reset(curp
->mp
);
280 moptions(enum mparset
*tflags
, char *arg
)
283 if (0 == strcmp(arg
, "doc"))
284 *tflags
= MPARSE_MDOC
;
285 else if (0 == strcmp(arg
, "andoc"))
286 *tflags
= MPARSE_AUTO
;
287 else if (0 == strcmp(arg
, "an"))
288 *tflags
= MPARSE_MAN
;
290 fprintf(stderr
, "%s: Bad argument\n", arg
);
298 toptions(struct curparse
*curp
, char *arg
)
301 if (0 == strcmp(arg
, "ascii"))
302 curp
->outtype
= OUTT_ASCII
;
303 else if (0 == strcmp(arg
, "lint")) {
304 curp
->outtype
= OUTT_LINT
;
305 curp
->wlevel
= MANDOCLEVEL_WARNING
;
306 } else if (0 == strcmp(arg
, "tree"))
307 curp
->outtype
= OUTT_TREE
;
308 else if (0 == strcmp(arg
, "html"))
309 curp
->outtype
= OUTT_HTML
;
310 else if (0 == strcmp(arg
, "locale"))
311 curp
->outtype
= OUTT_LOCALE
;
312 else if (0 == strcmp(arg
, "xhtml"))
313 curp
->outtype
= OUTT_XHTML
;
314 else if (0 == strcmp(arg
, "ps"))
315 curp
->outtype
= OUTT_PS
;
316 else if (0 == strcmp(arg
, "pdf"))
317 curp
->outtype
= OUTT_PDF
;
319 fprintf(stderr
, "%s: Bad argument\n", arg
);
327 woptions(struct curparse
*curp
, char *arg
)
341 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
348 curp
->wlevel
= MANDOCLEVEL_WARNING
;
351 curp
->wlevel
= MANDOCLEVEL_ERROR
;
354 curp
->wlevel
= MANDOCLEVEL_FATAL
;
357 fprintf(stderr
, "-W%s: Bad argument\n", o
);
366 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
367 const char *file
, int line
, int col
, const char *msg
)
370 fprintf(stderr
, "%s:%d:%d: %s: %s",
372 mparse_strlevel(lvl
),
376 fprintf(stderr
, ": %s", msg
);