]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.165 2011/10/06 22:29:12 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 */
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
[])
89 progname
= strrchr(argv
[0], '/');
95 memset(&curp
, 0, sizeof(struct curparse
));
98 curp
.outtype
= OUTT_ASCII
;
99 curp
.wlevel
= MANDOCLEVEL_FATAL
;
102 while (-1 != (c
= getopt(argc
, argv
, "m:O:T:VW:")))
105 if ( ! moptions(&type
, optarg
))
106 return((int)MANDOCLEVEL_BADARG
);
109 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
110 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
113 if ( ! toptions(&curp
, optarg
))
114 return((int)MANDOCLEVEL_BADARG
);
117 if ( ! woptions(&curp
, optarg
))
118 return((int)MANDOCLEVEL_BADARG
);
128 curp
.mp
= mparse_alloc(type
, curp
.wlevel
, mmsg
, &curp
);
131 * Conditionally start up the lookaside buffer before parsing.
133 if (OUTT_MAN
== curp
.outtype
)
134 mparse_keep(curp
.mp
);
142 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
145 parse(&curp
, -1, *argv
, &rc
);
146 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
152 (*curp
.outfree
)(curp
.outdata
);
154 mparse_free(curp
.mp
);
163 printf("%s %s\n", progname
, VERSION
);
164 exit((int)MANDOCLEVEL_OK
);
171 fprintf(stderr
, "usage: %s "
181 exit((int)MANDOCLEVEL_BADARG
);
185 parse(struct curparse
*curp
, int fd
,
186 const char *file
, enum mandoclevel
*level
)
192 /* Begin by parsing the file itself. */
197 rc
= mparse_readfd(curp
->mp
, fd
, file
);
199 /* Stop immediately if the parse has failed. */
201 if (MANDOCLEVEL_FATAL
<= rc
)
205 * With -Wstop and warnings or errors of at least the requested
206 * level, do not produce output.
209 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
212 /* If unset, allocate output dev now (if applicable). */
214 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
215 switch (curp
->outtype
) {
217 curp
->outdata
= xhtml_alloc(curp
->outopts
);
218 curp
->outfree
= html_free
;
221 curp
->outdata
= html_alloc(curp
->outopts
);
222 curp
->outfree
= html_free
;
225 curp
->outdata
= utf8_alloc(curp
->outopts
);
226 curp
->outfree
= ascii_free
;
229 curp
->outdata
= locale_alloc(curp
->outopts
);
230 curp
->outfree
= ascii_free
;
233 curp
->outdata
= ascii_alloc(curp
->outopts
);
234 curp
->outfree
= ascii_free
;
237 curp
->outdata
= pdf_alloc(curp
->outopts
);
238 curp
->outfree
= pspdf_free
;
241 curp
->outdata
= ps_alloc(curp
->outopts
);
242 curp
->outfree
= pspdf_free
;
248 switch (curp
->outtype
) {
252 curp
->outman
= html_man
;
253 curp
->outmdoc
= html_mdoc
;
256 curp
->outman
= tree_man
;
257 curp
->outmdoc
= tree_mdoc
;
260 curp
->outmdoc
= man_mdoc
;
261 curp
->outman
= man_man
;
272 curp
->outman
= terminal_man
;
273 curp
->outmdoc
= terminal_mdoc
;
280 mparse_result(curp
->mp
, &mdoc
, &man
);
282 /* Execute the out device, if it exists. */
284 if (man
&& curp
->outman
)
285 (*curp
->outman
)(curp
->outdata
, man
);
286 if (mdoc
&& curp
->outmdoc
)
287 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
291 mparse_reset(curp
->mp
);
298 moptions(enum mparset
*tflags
, char *arg
)
301 if (0 == strcmp(arg
, "doc"))
302 *tflags
= MPARSE_MDOC
;
303 else if (0 == strcmp(arg
, "andoc"))
304 *tflags
= MPARSE_AUTO
;
305 else if (0 == strcmp(arg
, "an"))
306 *tflags
= MPARSE_MAN
;
308 fprintf(stderr
, "%s: Bad argument\n", arg
);
316 toptions(struct curparse
*curp
, char *arg
)
319 if (0 == strcmp(arg
, "ascii"))
320 curp
->outtype
= OUTT_ASCII
;
321 else if (0 == strcmp(arg
, "lint")) {
322 curp
->outtype
= OUTT_LINT
;
323 curp
->wlevel
= MANDOCLEVEL_WARNING
;
324 } else if (0 == strcmp(arg
, "tree"))
325 curp
->outtype
= OUTT_TREE
;
326 else if (0 == strcmp(arg
, "man"))
327 curp
->outtype
= OUTT_MAN
;
328 else if (0 == strcmp(arg
, "html"))
329 curp
->outtype
= OUTT_HTML
;
330 else if (0 == strcmp(arg
, "utf8"))
331 curp
->outtype
= OUTT_UTF8
;
332 else if (0 == strcmp(arg
, "locale"))
333 curp
->outtype
= OUTT_LOCALE
;
334 else if (0 == strcmp(arg
, "xhtml"))
335 curp
->outtype
= OUTT_XHTML
;
336 else if (0 == strcmp(arg
, "ps"))
337 curp
->outtype
= OUTT_PS
;
338 else if (0 == strcmp(arg
, "pdf"))
339 curp
->outtype
= OUTT_PDF
;
341 fprintf(stderr
, "%s: Bad argument\n", arg
);
349 woptions(struct curparse
*curp
, char *arg
)
363 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
370 curp
->wlevel
= MANDOCLEVEL_WARNING
;
373 curp
->wlevel
= MANDOCLEVEL_ERROR
;
376 curp
->wlevel
= MANDOCLEVEL_FATAL
;
379 fprintf(stderr
, "-W%s: Bad argument\n", o
);
388 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
389 const char *file
, int line
, int col
, const char *msg
)
392 fprintf(stderr
, "%s:%d:%d: %s: %s",
394 mparse_strlevel(lvl
),
398 fprintf(stderr
, ": %s", msg
);