]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
85ad99ef081e83504665367b4d815e4b2cedfbbe
1 /* $Id: main.c,v 1.171 2014/03/19 22:20:43 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.
35 #if !defined(__GNUC__) || (__GNUC__ < 2)
37 # define __attribute__(x)
39 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
41 typedef void (*out_mdoc
)(void *, const struct mdoc
*);
42 typedef void (*out_man
)(void *, const struct man
*);
43 typedef void (*out_free
)(void *);
46 OUTT_ASCII
= 0, /* -Tascii */
47 OUTT_LOCALE
, /* -Tlocale */
48 OUTT_UTF8
, /* -Tutf8 */
49 OUTT_TREE
, /* -Ttree */
51 OUTT_HTML
, /* -Thtml */
52 OUTT_XHTML
, /* -Txhtml */
53 OUTT_LINT
, /* -Tlint */
60 enum mandoclevel wlevel
; /* ignore messages below this */
61 int wstop
; /* stop after a file with a warning */
62 enum outt outtype
; /* which output to use */
63 out_mdoc outmdoc
; /* mdoc output ptr */
64 out_man outman
; /* man output ptr */
65 out_free outfree
; /* free output ptr */
66 void *outdata
; /* data for output */
67 char outopts
[BUFSIZ
]; /* buf of output opts */
70 static int moptions(int *, char *);
71 static void mmsg(enum mandocerr
, enum mandoclevel
,
72 const char *, int, int, const char *);
73 static void parse(struct curparse
*, int,
74 const char *, enum mandoclevel
*);
75 static int toptions(struct curparse
*, char *);
76 static void usage(void) __attribute__((noreturn
));
77 static void version(void) __attribute__((noreturn
));
78 static int woptions(struct curparse
*, char *);
80 static const char *progname
;
83 main(int argc
, char *argv
[])
91 progname
= strrchr(argv
[0], '/');
97 memset(&curp
, 0, sizeof(struct curparse
));
100 curp
.outtype
= OUTT_ASCII
;
101 curp
.wlevel
= MANDOCLEVEL_FATAL
;
105 while (-1 != (c
= getopt(argc
, argv
, "I:m:O:T:VW:")))
108 if (strncmp(optarg
, "os=", 3)) {
109 fprintf(stderr
, "-I%s: Bad argument\n",
111 return((int)MANDOCLEVEL_BADARG
);
114 fprintf(stderr
, "-I%s: Duplicate argument\n",
116 return((int)MANDOCLEVEL_BADARG
);
118 defos
= mandoc_strdup(optarg
+ 3);
121 if ( ! moptions(&options
, optarg
))
122 return((int)MANDOCLEVEL_BADARG
);
125 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
126 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
129 if ( ! toptions(&curp
, optarg
))
130 return((int)MANDOCLEVEL_BADARG
);
133 if ( ! woptions(&curp
, optarg
))
134 return((int)MANDOCLEVEL_BADARG
);
144 curp
.mp
= mparse_alloc(options
, curp
.wlevel
, mmsg
, defos
);
147 * Conditionally start up the lookaside buffer before parsing.
149 if (OUTT_MAN
== curp
.outtype
)
150 mparse_keep(curp
.mp
);
158 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
161 parse(&curp
, -1, *argv
, &rc
);
162 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
168 (*curp
.outfree
)(curp
.outdata
);
170 mparse_free(curp
.mp
);
180 printf("%s %s\n", progname
, VERSION
);
181 exit((int)MANDOCLEVEL_OK
);
188 fprintf(stderr
, "usage: %s "
198 exit((int)MANDOCLEVEL_BADARG
);
202 parse(struct curparse
*curp
, int fd
,
203 const char *file
, enum mandoclevel
*level
)
209 /* Begin by parsing the file itself. */
214 rc
= mparse_readfd(curp
->mp
, fd
, file
);
216 /* Stop immediately if the parse has failed. */
218 if (MANDOCLEVEL_FATAL
<= rc
)
222 * With -Wstop and warnings or errors of at least the requested
223 * level, do not produce output.
226 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
229 /* If unset, allocate output dev now (if applicable). */
231 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
232 switch (curp
->outtype
) {
234 curp
->outdata
= xhtml_alloc(curp
->outopts
);
235 curp
->outfree
= html_free
;
238 curp
->outdata
= html_alloc(curp
->outopts
);
239 curp
->outfree
= html_free
;
242 curp
->outdata
= utf8_alloc(curp
->outopts
);
243 curp
->outfree
= ascii_free
;
246 curp
->outdata
= locale_alloc(curp
->outopts
);
247 curp
->outfree
= ascii_free
;
250 curp
->outdata
= ascii_alloc(curp
->outopts
);
251 curp
->outfree
= ascii_free
;
254 curp
->outdata
= pdf_alloc(curp
->outopts
);
255 curp
->outfree
= pspdf_free
;
258 curp
->outdata
= ps_alloc(curp
->outopts
);
259 curp
->outfree
= pspdf_free
;
265 switch (curp
->outtype
) {
269 curp
->outman
= html_man
;
270 curp
->outmdoc
= html_mdoc
;
273 curp
->outman
= tree_man
;
274 curp
->outmdoc
= tree_mdoc
;
277 curp
->outmdoc
= man_mdoc
;
278 curp
->outman
= man_man
;
289 curp
->outman
= terminal_man
;
290 curp
->outmdoc
= terminal_mdoc
;
297 mparse_result(curp
->mp
, &mdoc
, &man
, NULL
);
299 /* Execute the out device, if it exists. */
301 if (man
&& curp
->outman
)
302 (*curp
->outman
)(curp
->outdata
, man
);
303 if (mdoc
&& curp
->outmdoc
)
304 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
308 mparse_reset(curp
->mp
);
315 moptions(int *options
, char *arg
)
318 if (0 == strcmp(arg
, "doc"))
319 *options
|= MPARSE_MDOC
;
320 else if (0 == strcmp(arg
, "andoc"))
322 else if (0 == strcmp(arg
, "an"))
323 *options
|= MPARSE_MAN
;
325 fprintf(stderr
, "%s: Bad argument\n", arg
);
333 toptions(struct curparse
*curp
, char *arg
)
336 if (0 == strcmp(arg
, "ascii"))
337 curp
->outtype
= OUTT_ASCII
;
338 else if (0 == strcmp(arg
, "lint")) {
339 curp
->outtype
= OUTT_LINT
;
340 curp
->wlevel
= MANDOCLEVEL_WARNING
;
341 } else if (0 == strcmp(arg
, "tree"))
342 curp
->outtype
= OUTT_TREE
;
343 else if (0 == strcmp(arg
, "man"))
344 curp
->outtype
= OUTT_MAN
;
345 else if (0 == strcmp(arg
, "html"))
346 curp
->outtype
= OUTT_HTML
;
347 else if (0 == strcmp(arg
, "utf8"))
348 curp
->outtype
= OUTT_UTF8
;
349 else if (0 == strcmp(arg
, "locale"))
350 curp
->outtype
= OUTT_LOCALE
;
351 else if (0 == strcmp(arg
, "xhtml"))
352 curp
->outtype
= OUTT_XHTML
;
353 else if (0 == strcmp(arg
, "ps"))
354 curp
->outtype
= OUTT_PS
;
355 else if (0 == strcmp(arg
, "pdf"))
356 curp
->outtype
= OUTT_PDF
;
358 fprintf(stderr
, "%s: Bad argument\n", arg
);
366 woptions(struct curparse
*curp
, char *arg
)
380 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
387 curp
->wlevel
= MANDOCLEVEL_WARNING
;
390 curp
->wlevel
= MANDOCLEVEL_ERROR
;
393 curp
->wlevel
= MANDOCLEVEL_FATAL
;
396 fprintf(stderr
, "-W%s: Bad argument\n", o
);
405 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
406 const char *file
, int line
, int col
, const char *msg
)
409 fprintf(stderr
, "%s:%d:%d: %s: %s",
411 mparse_strlevel(lvl
),
415 fprintf(stderr
, ": %s", msg
);