]>
git.cameronkatri.com Git - mandoc.git/blob - main.c
1 /* $Id: main.c,v 1.158 2011/03/22 10:35:26 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_TREE
, /* -Ttree */
47 OUTT_HTML
, /* -Thtml */
48 OUTT_XHTML
, /* -Txhtml */
49 OUTT_LINT
, /* -Tlint */
56 enum mandoclevel wlevel
; /* ignore messages below this */
57 int wstop
; /* stop after a file with a warning */
58 enum outt outtype
; /* which output to use */
59 out_mdoc outmdoc
; /* mdoc output ptr */
60 out_man outman
; /* man output ptr */
61 out_free outfree
; /* free output ptr */
62 void *outdata
; /* data for output */
63 char outopts
[BUFSIZ
]; /* buf of output opts */
66 static const char * const mandoclevels
[MANDOCLEVEL_MAX
] = {
76 static const char * const mandocerrs
[MANDOCERR_MAX
] = {
81 /* related to the prologue */
82 "no title in document",
83 "document title should be all caps",
84 "unknown manual section",
85 "date missing, using today's date",
86 "cannot parse date, using it verbatim",
87 "prologue macros out of order",
88 "duplicate prologue macro",
89 "macro not allowed in prologue",
90 "macro not allowed in body",
92 /* related to document structure */
93 ".so is fragile, better use ln(1)",
94 "NAME section must come first",
95 "bad NAME section contents",
96 "manual name not yet set",
97 "sections out of conventional order",
98 "duplicate section name",
99 "section not in conventional manual section",
101 /* related to macros and nesting */
102 "skipping obsolete macro",
103 "skipping paragraph macro",
104 "skipping no-space macro",
105 "blocks badly nested",
106 "child violates parent syntax",
107 "nested displays are not portable",
108 "already in literal mode",
110 /* related to missing macro arguments */
111 "skipping empty macro",
112 "argument count wrong",
113 "missing display type",
114 "list type must come first",
115 "tag lists require a width argument",
117 "skipping end of block that is not open",
119 /* related to bad macro arguments */
121 "duplicate argument",
122 "duplicate display type",
123 "duplicate list type",
124 "unknown AT&T UNIX version",
127 "unknown standard specifier",
128 "bad width argument",
130 /* related to plain text */
131 "blank line in non-literal context",
132 "tab in non-literal context",
133 "end of line whitespace",
135 "unknown escape sequence",
136 "unterminated quoted string",
140 /* related to tables */
144 "no table layout cells specified",
145 "no table data cells specified",
146 "ignore data in cell",
147 "data block still open",
148 "ignoring extra data cells",
150 "input stack limit exceeded, infinite loop?",
151 "skipping bad character",
152 "escaped character not allowed in a name",
153 "skipping text before the first section header",
154 "skipping unknown macro",
155 "NOT IMPLEMENTED, please use groff: skipping request",
157 "argument count wrong",
158 "skipping end of block that is not open",
159 "missing end of block",
160 "scope open on exit",
161 "uname(3) system call failed",
162 "macro requires line argument(s)",
163 "macro requires body argument(s)",
164 "macro requires argument(s)",
166 "line argument(s) will be lost",
167 "body argument(s) will be lost",
169 "generic fatal error",
172 "column syntax is inconsistent",
173 "NOT IMPLEMENTED: .Bd -file",
174 "line scope broken, syntax violated",
175 "argument count wrong, violates syntax",
176 "child violates parent syntax",
177 "argument count wrong, violates syntax",
178 "NOT IMPLEMENTED: .so with absolute path or \"..\"",
180 "no document prologue",
181 "static buffer exhausted",
184 static int moptions(enum mparset
*, char *);
185 static void mmsg(enum mandocerr
, enum mandoclevel
,
186 const char *, int, int, const char *);
187 static void parse(struct curparse
*, int,
188 const char *, enum mandoclevel
*);
189 static int toptions(struct curparse
*, char *);
190 static void usage(void) __attribute__((noreturn
));
191 static void version(void) __attribute__((noreturn
));
192 static int woptions(struct curparse
*, char *);
194 static const char *progname
;
197 main(int argc
, char *argv
[])
200 struct curparse curp
;
204 progname
= strrchr(argv
[0], '/');
205 if (progname
== NULL
)
210 memset(&curp
, 0, sizeof(struct curparse
));
213 curp
.outtype
= OUTT_ASCII
;
214 curp
.wlevel
= MANDOCLEVEL_FATAL
;
217 while (-1 != (c
= getopt(argc
, argv
, "m:O:T:VW:")))
220 if ( ! moptions(&type
, optarg
))
221 return((int)MANDOCLEVEL_BADARG
);
224 (void)strlcat(curp
.outopts
, optarg
, BUFSIZ
);
225 (void)strlcat(curp
.outopts
, ",", BUFSIZ
);
228 if ( ! toptions(&curp
, optarg
))
229 return((int)MANDOCLEVEL_BADARG
);
232 if ( ! woptions(&curp
, optarg
))
233 return((int)MANDOCLEVEL_BADARG
);
243 curp
.mp
= mparse_alloc(type
, curp
.wlevel
, mmsg
, &curp
);
251 parse(&curp
, STDIN_FILENO
, "<stdin>", &rc
);
254 parse(&curp
, -1, *argv
, &rc
);
255 if (MANDOCLEVEL_OK
!= rc
&& curp
.wstop
)
261 (*curp
.outfree
)(curp
.outdata
);
263 mparse_free(curp
.mp
);
272 printf("%s %s\n", progname
, VERSION
);
273 exit((int)MANDOCLEVEL_OK
);
280 fprintf(stderr
, "usage: %s "
290 exit((int)MANDOCLEVEL_BADARG
);
294 parse(struct curparse
*curp
, int fd
,
295 const char *file
, enum mandoclevel
*level
)
301 /* Begin by parsing the file itself. */
306 rc
= mparse_readfd(curp
->mp
, fd
, file
);
308 /* Stop immediately if the parse has failed. */
310 if (MANDOCLEVEL_FATAL
<= rc
)
314 * With -Wstop and warnings or errors of at least the requested
315 * level, do not produce output.
318 if (MANDOCLEVEL_OK
!= rc
&& curp
->wstop
)
321 /* If unset, allocate output dev now (if applicable). */
323 if ( ! (curp
->outman
&& curp
->outmdoc
)) {
324 switch (curp
->outtype
) {
326 curp
->outdata
= xhtml_alloc(curp
->outopts
);
329 curp
->outdata
= html_alloc(curp
->outopts
);
332 curp
->outdata
= ascii_alloc(curp
->outopts
);
333 curp
->outfree
= ascii_free
;
336 curp
->outdata
= pdf_alloc(curp
->outopts
);
337 curp
->outfree
= pspdf_free
;
340 curp
->outdata
= ps_alloc(curp
->outopts
);
341 curp
->outfree
= pspdf_free
;
347 switch (curp
->outtype
) {
351 curp
->outman
= html_man
;
352 curp
->outmdoc
= html_mdoc
;
353 curp
->outfree
= html_free
;
356 curp
->outman
= tree_man
;
357 curp
->outmdoc
= tree_mdoc
;
364 curp
->outman
= terminal_man
;
365 curp
->outmdoc
= terminal_mdoc
;
372 mparse_result(curp
->mp
, &mdoc
, &man
);
374 /* Execute the out device, if it exists. */
376 if (man
&& curp
->outman
)
377 (*curp
->outman
)(curp
->outdata
, man
);
378 if (mdoc
&& curp
->outmdoc
)
379 (*curp
->outmdoc
)(curp
->outdata
, mdoc
);
383 mparse_reset(curp
->mp
);
390 moptions(enum mparset
*tflags
, char *arg
)
393 if (0 == strcmp(arg
, "doc"))
394 *tflags
= MPARSE_MDOC
;
395 else if (0 == strcmp(arg
, "andoc"))
396 *tflags
= MPARSE_AUTO
;
397 else if (0 == strcmp(arg
, "an"))
398 *tflags
= MPARSE_MAN
;
400 fprintf(stderr
, "%s: Bad argument\n", arg
);
408 toptions(struct curparse
*curp
, char *arg
)
411 if (0 == strcmp(arg
, "ascii"))
412 curp
->outtype
= OUTT_ASCII
;
413 else if (0 == strcmp(arg
, "lint")) {
414 curp
->outtype
= OUTT_LINT
;
415 curp
->wlevel
= MANDOCLEVEL_WARNING
;
416 } else if (0 == strcmp(arg
, "tree"))
417 curp
->outtype
= OUTT_TREE
;
418 else if (0 == strcmp(arg
, "html"))
419 curp
->outtype
= OUTT_HTML
;
420 else if (0 == strcmp(arg
, "xhtml"))
421 curp
->outtype
= OUTT_XHTML
;
422 else if (0 == strcmp(arg
, "ps"))
423 curp
->outtype
= OUTT_PS
;
424 else if (0 == strcmp(arg
, "pdf"))
425 curp
->outtype
= OUTT_PDF
;
427 fprintf(stderr
, "%s: Bad argument\n", arg
);
435 woptions(struct curparse
*curp
, char *arg
)
449 switch (getsubopt(&arg
, UNCONST(toks
), &v
)) {
456 curp
->wlevel
= MANDOCLEVEL_WARNING
;
459 curp
->wlevel
= MANDOCLEVEL_ERROR
;
462 curp
->wlevel
= MANDOCLEVEL_FATAL
;
465 fprintf(stderr
, "-W%s: Bad argument\n", o
);
474 mmsg(enum mandocerr t
, enum mandoclevel lvl
,
475 const char *file
, int line
, int col
, const char *msg
)
478 fprintf(stderr
, "%s:%d:%d: %s: %s",
480 mandoclevels
[lvl
], mandocerrs
[t
]);
483 fprintf(stderr
, ": %s", msg
);