]> git.cameronkatri.com Git - mandoc.git/blob - main.c
Add mode for -Tlocale. This mode, with this commit, behaves exactly
[mandoc.git] / main.c
1 /* $Id: main.c,v 1.162 2011/05/17 14:38:34 kristaps 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_TREE, /* -Ttree */
48 OUTT_HTML, /* -Thtml */
49 OUTT_XHTML, /* -Txhtml */
50 OUTT_LINT, /* -Tlint */
51 OUTT_PS, /* -Tps */
52 OUTT_PDF /* -Tpdf */
53 };
54
55 struct curparse {
56 struct mparse *mp;
57 enum mandoclevel wlevel; /* ignore messages below this */
58 int wstop; /* stop after a file with a warning */
59 enum outt outtype; /* which output to use */
60 out_mdoc outmdoc; /* mdoc output ptr */
61 out_man outman; /* man output ptr */
62 out_free outfree; /* free output ptr */
63 void *outdata; /* data for output */
64 char outopts[BUFSIZ]; /* buf of output opts */
65 };
66
67 static int moptions(enum mparset *, char *);
68 static void mmsg(enum mandocerr, enum mandoclevel,
69 const char *, int, int, const char *);
70 static void parse(struct curparse *, int,
71 const char *, enum mandoclevel *);
72 static int toptions(struct curparse *, char *);
73 static void usage(void) __attribute__((noreturn));
74 static void version(void) __attribute__((noreturn));
75 static int woptions(struct curparse *, char *);
76
77 static const char *progname;
78
79 int
80 main(int argc, char *argv[])
81 {
82 int c;
83 struct curparse curp;
84 enum mparset type;
85 enum mandoclevel rc;
86
87 progname = strrchr(argv[0], '/');
88 if (progname == NULL)
89 progname = argv[0];
90 else
91 ++progname;
92
93 memset(&curp, 0, sizeof(struct curparse));
94
95 type = MPARSE_AUTO;
96 curp.outtype = OUTT_ASCII;
97 curp.wlevel = MANDOCLEVEL_FATAL;
98
99 /* LINTED */
100 while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
101 switch (c) {
102 case ('m'):
103 if ( ! moptions(&type, optarg))
104 return((int)MANDOCLEVEL_BADARG);
105 break;
106 case ('O'):
107 (void)strlcat(curp.outopts, optarg, BUFSIZ);
108 (void)strlcat(curp.outopts, ",", BUFSIZ);
109 break;
110 case ('T'):
111 if ( ! toptions(&curp, optarg))
112 return((int)MANDOCLEVEL_BADARG);
113 break;
114 case ('W'):
115 if ( ! woptions(&curp, optarg))
116 return((int)MANDOCLEVEL_BADARG);
117 break;
118 case ('V'):
119 version();
120 /* NOTREACHED */
121 default:
122 usage();
123 /* NOTREACHED */
124 }
125
126 curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
127
128 argc -= optind;
129 argv += optind;
130
131 rc = MANDOCLEVEL_OK;
132
133 if (NULL == *argv)
134 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
135
136 while (*argv) {
137 parse(&curp, -1, *argv, &rc);
138 if (MANDOCLEVEL_OK != rc && curp.wstop)
139 break;
140 ++argv;
141 }
142
143 if (curp.outfree)
144 (*curp.outfree)(curp.outdata);
145 if (curp.mp)
146 mparse_free(curp.mp);
147
148 return((int)rc);
149 }
150
151 static void
152 version(void)
153 {
154
155 printf("%s %s\n", progname, VERSION);
156 exit((int)MANDOCLEVEL_OK);
157 }
158
159 static void
160 usage(void)
161 {
162
163 fprintf(stderr, "usage: %s "
164 "[-V] "
165 "[-foption] "
166 "[-mformat] "
167 "[-Ooption] "
168 "[-Toutput] "
169 "[-Wlevel] "
170 "[file...]\n",
171 progname);
172
173 exit((int)MANDOCLEVEL_BADARG);
174 }
175
176 static void
177 parse(struct curparse *curp, int fd,
178 const char *file, enum mandoclevel *level)
179 {
180 enum mandoclevel rc;
181 struct mdoc *mdoc;
182 struct man *man;
183
184 /* Begin by parsing the file itself. */
185
186 assert(file);
187 assert(fd >= -1);
188
189 rc = mparse_readfd(curp->mp, fd, file);
190
191 /* Stop immediately if the parse has failed. */
192
193 if (MANDOCLEVEL_FATAL <= rc)
194 goto cleanup;
195
196 /*
197 * With -Wstop and warnings or errors of at least the requested
198 * level, do not produce output.
199 */
200
201 if (MANDOCLEVEL_OK != rc && curp->wstop)
202 goto cleanup;
203
204 /* If unset, allocate output dev now (if applicable). */
205
206 if ( ! (curp->outman && curp->outmdoc)) {
207 switch (curp->outtype) {
208 case (OUTT_XHTML):
209 curp->outdata = xhtml_alloc(curp->outopts);
210 curp->outfree = html_free;
211 break;
212 case (OUTT_HTML):
213 curp->outdata = html_alloc(curp->outopts);
214 curp->outfree = html_free;
215 break;
216 case (OUTT_LOCALE):
217 curp->outdata = locale_alloc(curp->outopts);
218 curp->outfree = ascii_free;
219 break;
220 case (OUTT_ASCII):
221 curp->outdata = ascii_alloc(curp->outopts);
222 curp->outfree = ascii_free;
223 break;
224 case (OUTT_PDF):
225 curp->outdata = pdf_alloc(curp->outopts);
226 curp->outfree = pspdf_free;
227 break;
228 case (OUTT_PS):
229 curp->outdata = ps_alloc(curp->outopts);
230 curp->outfree = pspdf_free;
231 break;
232 default:
233 break;
234 }
235
236 switch (curp->outtype) {
237 case (OUTT_HTML):
238 /* FALLTHROUGH */
239 case (OUTT_XHTML):
240 curp->outman = html_man;
241 curp->outmdoc = html_mdoc;
242 break;
243 case (OUTT_TREE):
244 curp->outman = tree_man;
245 curp->outmdoc = tree_mdoc;
246 break;
247 case (OUTT_PDF):
248 /* FALLTHROUGH */
249 case (OUTT_ASCII):
250 /* FALLTHROUGH */
251 case (OUTT_LOCALE):
252 /* FALLTHROUGH */
253 case (OUTT_PS):
254 curp->outman = terminal_man;
255 curp->outmdoc = terminal_mdoc;
256 break;
257 default:
258 break;
259 }
260 }
261
262 mparse_result(curp->mp, &mdoc, &man);
263
264 /* Execute the out device, if it exists. */
265
266 if (man && curp->outman)
267 (*curp->outman)(curp->outdata, man);
268 if (mdoc && curp->outmdoc)
269 (*curp->outmdoc)(curp->outdata, mdoc);
270
271 cleanup:
272
273 mparse_reset(curp->mp);
274
275 if (*level < rc)
276 *level = rc;
277 }
278
279 static int
280 moptions(enum mparset *tflags, char *arg)
281 {
282
283 if (0 == strcmp(arg, "doc"))
284 *tflags = MPARSE_MDOC;
285 else if (0 == strcmp(arg, "andoc"))
286 *tflags = MPARSE_AUTO;
287 else if (0 == strcmp(arg, "an"))
288 *tflags = MPARSE_MAN;
289 else {
290 fprintf(stderr, "%s: Bad argument\n", arg);
291 return(0);
292 }
293
294 return(1);
295 }
296
297 static int
298 toptions(struct curparse *curp, char *arg)
299 {
300
301 if (0 == strcmp(arg, "ascii"))
302 curp->outtype = OUTT_ASCII;
303 else if (0 == strcmp(arg, "lint")) {
304 curp->outtype = OUTT_LINT;
305 curp->wlevel = MANDOCLEVEL_WARNING;
306 } else if (0 == strcmp(arg, "tree"))
307 curp->outtype = OUTT_TREE;
308 else if (0 == strcmp(arg, "html"))
309 curp->outtype = OUTT_HTML;
310 else if (0 == strcmp(arg, "locale"))
311 curp->outtype = OUTT_LOCALE;
312 else if (0 == strcmp(arg, "xhtml"))
313 curp->outtype = OUTT_XHTML;
314 else if (0 == strcmp(arg, "ps"))
315 curp->outtype = OUTT_PS;
316 else if (0 == strcmp(arg, "pdf"))
317 curp->outtype = OUTT_PDF;
318 else {
319 fprintf(stderr, "%s: Bad argument\n", arg);
320 return(0);
321 }
322
323 return(1);
324 }
325
326 static int
327 woptions(struct curparse *curp, char *arg)
328 {
329 char *v, *o;
330 const char *toks[6];
331
332 toks[0] = "stop";
333 toks[1] = "all";
334 toks[2] = "warning";
335 toks[3] = "error";
336 toks[4] = "fatal";
337 toks[5] = NULL;
338
339 while (*arg) {
340 o = arg;
341 switch (getsubopt(&arg, UNCONST(toks), &v)) {
342 case (0):
343 curp->wstop = 1;
344 break;
345 case (1):
346 /* FALLTHROUGH */
347 case (2):
348 curp->wlevel = MANDOCLEVEL_WARNING;
349 break;
350 case (3):
351 curp->wlevel = MANDOCLEVEL_ERROR;
352 break;
353 case (4):
354 curp->wlevel = MANDOCLEVEL_FATAL;
355 break;
356 default:
357 fprintf(stderr, "-W%s: Bad argument\n", o);
358 return(0);
359 }
360 }
361
362 return(1);
363 }
364
365 static void
366 mmsg(enum mandocerr t, enum mandoclevel lvl,
367 const char *file, int line, int col, const char *msg)
368 {
369
370 fprintf(stderr, "%s:%d:%d: %s: %s",
371 file, line, col + 1,
372 mparse_strlevel(lvl),
373 mparse_strerror(t));
374
375 if (msg)
376 fprintf(stderr, ": %s", msg);
377
378 fputc('\n', stderr);
379 }