]> git.cameronkatri.com Git - mandoc.git/blob - main.c
b76a6047b2185b2dd0468e38f58ae4089fda1460
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.164 2011/09/17 15:00:51 schwarze Exp $ */
2 /*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
5 *
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.
9 *
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.
17 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <assert.h>
23 #include <stdio.h>
24 #include <stdint.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28
29 #include "mandoc.h"
30 #include "main.h"
31 #include "mdoc.h"
32 #include "man.h"
33
34 #if !defined(__GNUC__) || (__GNUC__ < 2)
35 # if !defined(lint)
36 # define __attribute__(x)
37 # endif
38 #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
39
40 typedef void (*out_mdoc)(void *, const struct mdoc *);
41 typedef void (*out_man)(void *, const struct man *);
42 typedef void (*out_free)(void *);
43
44 enum outt {
45 OUTT_ASCII = 0, /* -Tascii */
46 OUTT_LOCALE, /* -Tlocale */
47 OUTT_UTF8, /* -Tutf8 */
48 OUTT_TREE, /* -Ttree */
49 OUTT_MAN, /* -Tman */
50 OUTT_HTML, /* -Thtml */
51 OUTT_XHTML, /* -Txhtml */
52 OUTT_LINT, /* -Tlint */
53 OUTT_PS, /* -Tps */
54 OUTT_PDF /* -Tpdf */
55 };
56
57 struct curparse {
58 struct mparse *mp;
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 */
67 };
68
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 *);
78
79 static const char *progname;
80
81 int
82 main(int argc, char *argv[])
83 {
84 int c;
85 struct curparse curp;
86 enum mparset type;
87 enum mandoclevel rc;
88
89 progname = strrchr(argv[0], '/');
90 if (progname == NULL)
91 progname = argv[0];
92 else
93 ++progname;
94
95 memset(&curp, 0, sizeof(struct curparse));
96
97 type = MPARSE_AUTO;
98 curp.outtype = OUTT_ASCII;
99 curp.wlevel = MANDOCLEVEL_FATAL;
100
101 /* LINTED */
102 while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
103 switch (c) {
104 case ('m'):
105 if ( ! moptions(&type, optarg))
106 return((int)MANDOCLEVEL_BADARG);
107 break;
108 case ('O'):
109 (void)strlcat(curp.outopts, optarg, BUFSIZ);
110 (void)strlcat(curp.outopts, ",", BUFSIZ);
111 break;
112 case ('T'):
113 if ( ! toptions(&curp, optarg))
114 return((int)MANDOCLEVEL_BADARG);
115 break;
116 case ('W'):
117 if ( ! woptions(&curp, optarg))
118 return((int)MANDOCLEVEL_BADARG);
119 break;
120 case ('V'):
121 version();
122 /* NOTREACHED */
123 default:
124 usage();
125 /* NOTREACHED */
126 }
127
128 curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
129
130 argc -= optind;
131 argv += optind;
132
133 rc = MANDOCLEVEL_OK;
134
135 if (NULL == *argv)
136 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
137
138 while (*argv) {
139 parse(&curp, -1, *argv, &rc);
140 if (MANDOCLEVEL_OK != rc && curp.wstop)
141 break;
142 ++argv;
143 }
144
145 if (curp.outfree)
146 (*curp.outfree)(curp.outdata);
147 if (curp.mp)
148 mparse_free(curp.mp);
149
150 return((int)rc);
151 }
152
153 static void
154 version(void)
155 {
156
157 printf("%s %s\n", progname, VERSION);
158 exit((int)MANDOCLEVEL_OK);
159 }
160
161 static void
162 usage(void)
163 {
164
165 fprintf(stderr, "usage: %s "
166 "[-V] "
167 "[-foption] "
168 "[-mformat] "
169 "[-Ooption] "
170 "[-Toutput] "
171 "[-Wlevel] "
172 "[file...]\n",
173 progname);
174
175 exit((int)MANDOCLEVEL_BADARG);
176 }
177
178 static void
179 parse(struct curparse *curp, int fd,
180 const char *file, enum mandoclevel *level)
181 {
182 enum mandoclevel rc;
183 struct mdoc *mdoc;
184 struct man *man;
185
186 /* Begin by parsing the file itself. */
187
188 assert(file);
189 assert(fd >= -1);
190
191 rc = mparse_readfd(curp->mp, fd, file);
192
193 /* Stop immediately if the parse has failed. */
194
195 if (MANDOCLEVEL_FATAL <= rc)
196 goto cleanup;
197
198 /*
199 * With -Wstop and warnings or errors of at least the requested
200 * level, do not produce output.
201 */
202
203 if (MANDOCLEVEL_OK != rc && curp->wstop)
204 goto cleanup;
205
206 /* If unset, allocate output dev now (if applicable). */
207
208 if ( ! (curp->outman && curp->outmdoc)) {
209 switch (curp->outtype) {
210 case (OUTT_XHTML):
211 curp->outdata = xhtml_alloc(curp->outopts);
212 curp->outfree = html_free;
213 break;
214 case (OUTT_HTML):
215 curp->outdata = html_alloc(curp->outopts);
216 curp->outfree = html_free;
217 break;
218 case (OUTT_UTF8):
219 curp->outdata = utf8_alloc(curp->outopts);
220 curp->outfree = ascii_free;
221 break;
222 case (OUTT_LOCALE):
223 curp->outdata = locale_alloc(curp->outopts);
224 curp->outfree = ascii_free;
225 break;
226 case (OUTT_ASCII):
227 curp->outdata = ascii_alloc(curp->outopts);
228 curp->outfree = ascii_free;
229 break;
230 case (OUTT_PDF):
231 curp->outdata = pdf_alloc(curp->outopts);
232 curp->outfree = pspdf_free;
233 break;
234 case (OUTT_PS):
235 curp->outdata = ps_alloc(curp->outopts);
236 curp->outfree = pspdf_free;
237 break;
238 default:
239 break;
240 }
241
242 switch (curp->outtype) {
243 case (OUTT_HTML):
244 /* FALLTHROUGH */
245 case (OUTT_XHTML):
246 curp->outman = html_man;
247 curp->outmdoc = html_mdoc;
248 break;
249 case (OUTT_TREE):
250 curp->outman = tree_man;
251 curp->outmdoc = tree_mdoc;
252 break;
253 case (OUTT_MAN):
254 curp->outmdoc = man_mdoc;
255 break;
256 case (OUTT_PDF):
257 /* FALLTHROUGH */
258 case (OUTT_ASCII):
259 /* FALLTHROUGH */
260 case (OUTT_UTF8):
261 /* FALLTHROUGH */
262 case (OUTT_LOCALE):
263 /* FALLTHROUGH */
264 case (OUTT_PS):
265 curp->outman = terminal_man;
266 curp->outmdoc = terminal_mdoc;
267 break;
268 default:
269 break;
270 }
271 }
272
273 mparse_result(curp->mp, &mdoc, &man);
274
275 /* Execute the out device, if it exists. */
276
277 if (man && curp->outman)
278 (*curp->outman)(curp->outdata, man);
279 if (mdoc && curp->outmdoc)
280 (*curp->outmdoc)(curp->outdata, mdoc);
281
282 cleanup:
283
284 mparse_reset(curp->mp);
285
286 if (*level < rc)
287 *level = rc;
288 }
289
290 static int
291 moptions(enum mparset *tflags, char *arg)
292 {
293
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;
300 else {
301 fprintf(stderr, "%s: Bad argument\n", arg);
302 return(0);
303 }
304
305 return(1);
306 }
307
308 static int
309 toptions(struct curparse *curp, char *arg)
310 {
311
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;
333 else {
334 fprintf(stderr, "%s: Bad argument\n", arg);
335 return(0);
336 }
337
338 return(1);
339 }
340
341 static int
342 woptions(struct curparse *curp, char *arg)
343 {
344 char *v, *o;
345 const char *toks[6];
346
347 toks[0] = "stop";
348 toks[1] = "all";
349 toks[2] = "warning";
350 toks[3] = "error";
351 toks[4] = "fatal";
352 toks[5] = NULL;
353
354 while (*arg) {
355 o = arg;
356 switch (getsubopt(&arg, UNCONST(toks), &v)) {
357 case (0):
358 curp->wstop = 1;
359 break;
360 case (1):
361 /* FALLTHROUGH */
362 case (2):
363 curp->wlevel = MANDOCLEVEL_WARNING;
364 break;
365 case (3):
366 curp->wlevel = MANDOCLEVEL_ERROR;
367 break;
368 case (4):
369 curp->wlevel = MANDOCLEVEL_FATAL;
370 break;
371 default:
372 fprintf(stderr, "-W%s: Bad argument\n", o);
373 return(0);
374 }
375 }
376
377 return(1);
378 }
379
380 static void
381 mmsg(enum mandocerr t, enum mandoclevel lvl,
382 const char *file, int line, int col, const char *msg)
383 {
384
385 fprintf(stderr, "%s:%d:%d: %s: %s",
386 file, line, col + 1,
387 mparse_strlevel(lvl),
388 mparse_strerror(t));
389
390 if (msg)
391 fprintf(stderr, ": %s", msg);
392
393 fputc('\n', stderr);
394 }