]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.172 2014/03/23 11:25:26 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
;
84 main(int argc
, char *argv
[])
92 progname
= strrchr(argv
[0], '/');
98 memset(&curp
, 0, sizeof(struct curparse
));
101 curp
.outtype
= OUTT_ASCII
;
102 curp
.wlevel
= MANDOCLEVEL_FATAL
;
106 while (-1 != (c
= getopt(argc
, argv
, "I:m:O:T:VW:")))
109 if (strncmp(optarg
, "os=", 3)) {
110 fprintf(stderr
, "-I%s: Bad argument\n",
112 return((int)MANDOCLEVEL_BADARG
);
115 fprintf(stderr
, "-I%s: Duplicate argument\n",
117 return((int)MANDOCLEVEL_BADARG
);
119 defos
= mandoc_strdup(optarg
+ 3);
122 if ( ! moptions(&options
, optarg
))
123 return((int)MANDOCLEVEL_BADARG
);
126 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
127 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
130 if ( ! toptions(&curp
, optarg
))
131 return((int)MANDOCLEVEL_BADARG
);
134 if ( ! woptions(&curp
, optarg
))
135 return((int)MANDOCLEVEL_BADARG
);
145 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
148 * Conditionally start up the lookaside buffer before parsing.
150 if (OUTT_MAN
== curp
.outtype
)
151 mparse_keep(curp
.mp
);
159 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
162 parse(&curp
, -1, *argv
, &rc
);
163 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
169 (*curp
.outfree
)(curp
.outdata
);
171 mparse_free(curp
.mp
);
181 printf("%s %s\n", progname
, VERSION
);
182 exit((int)MANDOCLEVEL_OK
);
189 fprintf(stderr
, "usage: %s "
199 exit((int)MANDOCLEVEL_BADARG
);
203 parse(struct curparse
*curp
, int fd
,
204 const char *file
, enum mandoclevel
*level
)
210 /* Begin by parsing the file itself. */
215 rc
= mparse_readfd(curp
->mp
, fd
, file
);
217 /* Stop immediately if the parse has failed. */
219 if (MANDOCLEVEL_FATAL
<= rc
)
223 * With -Wstop and warnings or errors of at least the requested
224 * level, do not produce output.
227 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
230 /* If unset, allocate output dev now (if applicable). */
232 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
233 switch (curp
->outtype
) {
235 curp
->outdata
= xhtml_alloc(curp
->outopts
);
236 curp
->outfree
= html_free
;
239 curp
->outdata
= html_alloc(curp
->outopts
);
240 curp
->outfree
= html_free
;
243 curp
->outdata
= utf8_alloc(curp
->outopts
);
244 curp
->outfree
= ascii_free
;
247 curp
->outdata
= locale_alloc(curp
->outopts
);
248 curp
->outfree
= ascii_free
;
251 curp
->outdata
= ascii_alloc(curp
->outopts
);
252 curp
->outfree
= ascii_free
;
255 curp
->outdata
= pdf_alloc(curp
->outopts
);
256 curp
->outfree
= pspdf_free
;
259 curp
->outdata
= ps_alloc(curp
->outopts
);
260 curp
->outfree
= pspdf_free
;
266 switch (curp
->outtype
) {
270 curp
->outman
= html_man
;
271 curp
->outmdoc
= html_mdoc
;
274 curp
->outman
= tree_man
;
275 curp
->outmdoc
= tree_mdoc
;
278 curp
->outmdoc
= man_mdoc
;
279 curp
->outman
= man_man
;
290 curp
->outman
= terminal_man
;
291 curp
->outmdoc
= terminal_mdoc
;
298 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
300 /* Execute the out device, if it exists. */
302 if (man
&& curp
->outman
)
303 (*curp
->outman
)(curp
->outdata
, man
);
304 if (mdoc
&& curp
->outmdoc
)
305 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
309 mparse_reset(curp
->mp
);
316 moptions(int *options
, char *arg
)
319 if (0 == strcmp(arg
, "doc"))
320 *options
|= MPARSE_MDOC
;
321 else if (0 == strcmp(arg
, "andoc"))
323 else if (0 == strcmp(arg
, "an"))
324 *options
|= MPARSE_MAN
;
326 fprintf(stderr
, "%s: Bad argument\n", arg
);
334 toptions(struct curparse
*curp
, char *arg
)
337 if (0 == strcmp(arg
, "ascii"))
338 curp
->outtype
= OUTT_ASCII
;
339 else if (0 == strcmp(arg
, "lint")) {
340 curp
->outtype
= OUTT_LINT
;
341 curp
->wlevel
= MANDOCLEVEL_WARNING
;
342 } else if (0 == strcmp(arg
, "tree"))
343 curp
->outtype
= OUTT_TREE
;
344 else if (0 == strcmp(arg
, "man"))
345 curp
->outtype
= OUTT_MAN
;
346 else if (0 == strcmp(arg
, "html"))
347 curp
->outtype
= OUTT_HTML
;
348 else if (0 == strcmp(arg
, "utf8"))
349 curp
->outtype
= OUTT_UTF8
;
350 else if (0 == strcmp(arg
, "locale"))
351 curp
->outtype
= OUTT_LOCALE
;
352 else if (0 == strcmp(arg
, "xhtml"))
353 curp
->outtype
= OUTT_XHTML
;
354 else if (0 == strcmp(arg
, "ps"))
355 curp
->outtype
= OUTT_PS
;
356 else if (0 == strcmp(arg
, "pdf"))
357 curp
->outtype
= OUTT_PDF
;
359 fprintf(stderr
, "%s: Bad argument\n", arg
);
367 woptions(struct curparse
*curp
, char *arg
)
381 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
388 curp
->wlevel
= MANDOCLEVEL_WARNING
;
391 curp
->wlevel
= MANDOCLEVEL_ERROR
;
394 curp
->wlevel
= MANDOCLEVEL_FATAL
;
397 fprintf(stderr
, "-W%s: Bad argument\n", o
);
406 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
407 const char *file
, int line
, int col
, const char *msg
)
410 fprintf(stderr
, "%s:%d:%d: %s: %s",
412 mparse_strlevel(lvl
),
416 fprintf(stderr
, ": %s", msg
);