]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
b76a6047b2185b2dd0468e38f58ae4089fda1460
1 /* $Id: main.c,v 1.164 2011/09/17 15:00:51 schwarze 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
);
136 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
139 parse(&curp
, -1, *argv
, &rc
);
140 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
146 (*curp
.outfree
)(curp
.outdata
);
148 mparse_free(curp
.mp
);
157 printf("%s %s\n", progname
, VERSION
);
158 exit((int)MANDOCLEVEL_OK
);
165 fprintf(stderr
, "usage: %s "
175 exit((int)MANDOCLEVEL_BADARG
);
179 parse(struct curparse
*curp
, int fd
,
180 const char *file
, enum mandoclevel
*level
)
186 /* Begin by parsing the file itself. */
191 rc
= mparse_readfd(curp
->mp
, fd
, file
);
193 /* Stop immediately if the parse has failed. */
195 if (MANDOCLEVEL_FATAL
<= rc
)
199 * With -Wstop and warnings or errors of at least the requested
200 * level, do not produce output.
203 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
206 /* If unset, allocate output dev now (if applicable). */
208 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
209 switch (curp
->outtype
) {
211 curp
->outdata
= xhtml_alloc(curp
->outopts
);
212 curp
->outfree
= html_free
;
215 curp
->outdata
= html_alloc(curp
->outopts
);
216 curp
->outfree
= html_free
;
219 curp
->outdata
= utf8_alloc(curp
->outopts
);
220 curp
->outfree
= ascii_free
;
223 curp
->outdata
= locale_alloc(curp
->outopts
);
224 curp
->outfree
= ascii_free
;
227 curp
->outdata
= ascii_alloc(curp
->outopts
);
228 curp
->outfree
= ascii_free
;
231 curp
->outdata
= pdf_alloc(curp
->outopts
);
232 curp
->outfree
= pspdf_free
;
235 curp
->outdata
= ps_alloc(curp
->outopts
);
236 curp
->outfree
= pspdf_free
;
242 switch (curp
->outtype
) {
246 curp
->outman
= html_man
;
247 curp
->outmdoc
= html_mdoc
;
250 curp
->outman
= tree_man
;
251 curp
->outmdoc
= tree_mdoc
;
254 curp
->outmdoc
= man_mdoc
;
265 curp
->outman
= terminal_man
;
266 curp
->outmdoc
= terminal_mdoc
;
273 mparse_result(curp
->mp
, &mdoc
, &man
);
275 /* Execute the out device, if it exists. */
277 if (man
&& curp
->outman
)
278 (*curp
->outman
)(curp
->outdata
, man
);
279 if (mdoc
&& curp
->outmdoc
)
280 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
284 mparse_reset(curp
->mp
);
291 moptions(enum mparset
*tflags
, char *arg
)
294 if (0 == strcmp(arg
, "doc"))
295 *tflags
= MPARSE_MDOC
;
296 else if (0 == strcmp(arg
, "andoc"))
297 *tflags
= MPARSE_AUTO
;
298 else if (0 == strcmp(arg
, "an"))
299 *tflags
= MPARSE_MAN
;
301 fprintf(stderr
, "%s: Bad argument\n", arg
);
309 toptions(struct curparse
*curp
, char *arg
)
312 if (0 == strcmp(arg
, "ascii"))
313 curp
->outtype
= OUTT_ASCII
;
314 else if (0 == strcmp(arg
, "lint")) {
315 curp
->outtype
= OUTT_LINT
;
316 curp
->wlevel
= MANDOCLEVEL_WARNING
;
317 } else if (0 == strcmp(arg
, "tree"))
318 curp
->outtype
= OUTT_TREE
;
319 else if (0 == strcmp(arg
, "man"))
320 curp
->outtype
= OUTT_MAN
;
321 else if (0 == strcmp(arg
, "html"))
322 curp
->outtype
= OUTT_HTML
;
323 else if (0 == strcmp(arg
, "utf8"))
324 curp
->outtype
= OUTT_UTF8
;
325 else if (0 == strcmp(arg
, "locale"))
326 curp
->outtype
= OUTT_LOCALE
;
327 else if (0 == strcmp(arg
, "xhtml"))
328 curp
->outtype
= OUTT_XHTML
;
329 else if (0 == strcmp(arg
, "ps"))
330 curp
->outtype
= OUTT_PS
;
331 else if (0 == strcmp(arg
, "pdf"))
332 curp
->outtype
= OUTT_PDF
;
334 fprintf(stderr
, "%s: Bad argument\n", arg
);
342 woptions(struct curparse
*curp
, char *arg
)
356 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
363 curp
->wlevel
= MANDOCLEVEL_WARNING
;
366 curp
->wlevel
= MANDOCLEVEL_ERROR
;
369 curp
->wlevel
= MANDOCLEVEL_FATAL
;
372 fprintf(stderr
, "-W%s: Bad argument\n", o
);
381 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
382 const char *file
, int line
, int col
, const char *msg
)
385 fprintf(stderr
, "%s:%d:%d: %s: %s",
387 mparse_strlevel(lvl
),
391 fprintf(stderr
, ": %s", msg
);