Import mdocml-1.13.1
[dragonfly.git] / contrib / mdocml / main.c
CommitLineData
070c62a6 1/* $Id: main.c,v 1.177 2014/06/21 22:24:01 schwarze Exp $ */
80387638
SW
2/*
3 * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
070c62a6
FF
4 * Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
5 * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
80387638
SW
6 *
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.
10 *
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.
18 */
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
80387638 23#include <assert.h>
80387638
SW
24#include <stdio.h>
25#include <stdint.h>
26#include <stdlib.h>
27#include <string.h>
28#include <unistd.h>
29
30#include "mandoc.h"
070c62a6 31#include "mandoc_aux.h"
80387638
SW
32#include "main.h"
33#include "mdoc.h"
34#include "man.h"
80387638
SW
35
36#if !defined(__GNUC__) || (__GNUC__ < 2)
37# if !defined(lint)
38# define __attribute__(x)
39# endif
40#endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
41
42typedef void (*out_mdoc)(void *, const struct mdoc *);
43typedef void (*out_man)(void *, const struct man *);
44typedef void (*out_free)(void *);
45
80387638 46enum outt {
60e1e752 47 OUTT_ASCII = 0, /* -Tascii */
a4c7eb57
SW
48 OUTT_LOCALE, /* -Tlocale */
49 OUTT_UTF8, /* -Tutf8 */
60e1e752 50 OUTT_TREE, /* -Ttree */
36342e81 51 OUTT_MAN, /* -Tman */
60e1e752
SW
52 OUTT_HTML, /* -Thtml */
53 OUTT_XHTML, /* -Txhtml */
54 OUTT_LINT, /* -Tlint */
55 OUTT_PS, /* -Tps */
56 OUTT_PDF /* -Tpdf */
80387638
SW
57};
58
59struct curparse {
60e1e752
SW
60 struct mparse *mp;
61 enum mandoclevel wlevel; /* ignore messages below this */
62 int wstop; /* stop after a file with a warning */
070c62a6 63 enum outt outtype; /* which output to use */
80387638 64 out_mdoc outmdoc; /* mdoc output ptr */
070c62a6 65 out_man outman; /* man output ptr */
80387638
SW
66 out_free outfree; /* free output ptr */
67 void *outdata; /* data for output */
68 char outopts[BUFSIZ]; /* buf of output opts */
69};
70
070c62a6 71static int moptions(int *, char *);
60e1e752
SW
72static void mmsg(enum mandocerr, enum mandoclevel,
73 const char *, int, int, const char *);
070c62a6 74static void parse(struct curparse *, int,
60e1e752 75 const char *, enum mandoclevel *);
80387638
SW
76static int toptions(struct curparse *, char *);
77static void usage(void) __attribute__((noreturn));
78static void version(void) __attribute__((noreturn));
79static int woptions(struct curparse *, char *);
80
81static const char *progname;
80387638 82
070c62a6 83
80387638
SW
84int
85main(int argc, char *argv[])
86{
87 int c;
88 struct curparse curp;
070c62a6 89 int options;
60e1e752 90 enum mandoclevel rc;
f88b6c16 91 char *defos;
80387638
SW
92
93 progname = strrchr(argv[0], '/');
94 if (progname == NULL)
95 progname = argv[0];
96 else
97 ++progname;
98
99 memset(&curp, 0, sizeof(struct curparse));
100
070c62a6 101 options = MPARSE_SO;
80387638
SW
102 curp.outtype = OUTT_ASCII;
103 curp.wlevel = MANDOCLEVEL_FATAL;
f88b6c16 104 defos = NULL;
80387638 105
f88b6c16 106 while (-1 != (c = getopt(argc, argv, "I:m:O:T:VW:")))
80387638 107 switch (c) {
070c62a6 108 case 'I':
f88b6c16 109 if (strncmp(optarg, "os=", 3)) {
070c62a6
FF
110 fprintf(stderr,
111 "%s: -I%s: Bad argument\n",
112 progname, optarg);
f88b6c16
FF
113 return((int)MANDOCLEVEL_BADARG);
114 }
115 if (defos) {
070c62a6
FF
116 fprintf(stderr,
117 "%s: -I%s: Duplicate argument\n",
118 progname, optarg);
f88b6c16
FF
119 return((int)MANDOCLEVEL_BADARG);
120 }
121 defos = mandoc_strdup(optarg + 3);
122 break;
070c62a6
FF
123 case 'm':
124 if ( ! moptions(&options, optarg))
80387638
SW
125 return((int)MANDOCLEVEL_BADARG);
126 break;
070c62a6 127 case 'O':
80387638
SW
128 (void)strlcat(curp.outopts, optarg, BUFSIZ);
129 (void)strlcat(curp.outopts, ",", BUFSIZ);
130 break;
070c62a6 131 case 'T':
80387638
SW
132 if ( ! toptions(&curp, optarg))
133 return((int)MANDOCLEVEL_BADARG);
134 break;
070c62a6 135 case 'W':
80387638
SW
136 if ( ! woptions(&curp, optarg))
137 return((int)MANDOCLEVEL_BADARG);
138 break;
070c62a6 139 case 'V':
80387638
SW
140 version();
141 /* NOTREACHED */
142 default:
143 usage();
144 /* NOTREACHED */
145 }
146
070c62a6 147 curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
60e1e752 148
36342e81
SW
149 /*
150 * Conditionally start up the lookaside buffer before parsing.
151 */
152 if (OUTT_MAN == curp.outtype)
153 mparse_keep(curp.mp);
154
80387638
SW
155 argc -= optind;
156 argv += optind;
157
60e1e752 158 rc = MANDOCLEVEL_OK;
80387638 159
60e1e752
SW
160 if (NULL == *argv)
161 parse(&curp, STDIN_FILENO, "<stdin>", &rc);
80387638
SW
162
163 while (*argv) {
60e1e752
SW
164 parse(&curp, -1, *argv, &rc);
165 if (MANDOCLEVEL_OK != rc && curp.wstop)
80387638
SW
166 break;
167 ++argv;
168 }
169
170 if (curp.outfree)
171 (*curp.outfree)(curp.outdata);
60e1e752
SW
172 if (curp.mp)
173 mparse_free(curp.mp);
f88b6c16 174 free(defos);
80387638 175
60e1e752 176 return((int)rc);
80387638
SW
177}
178
80387638
SW
179static void
180version(void)
181{
182
60e1e752 183 printf("%s %s\n", progname, VERSION);
80387638
SW
184 exit((int)MANDOCLEVEL_OK);
185}
186
80387638
SW
187static void
188usage(void)
189{
190
60e1e752 191 fprintf(stderr, "usage: %s "
80387638 192 "[-V] "
f88b6c16 193 "[-Ios=name] "
80387638
SW
194 "[-mformat] "
195 "[-Ooption] "
196 "[-Toutput] "
f88b6c16 197 "[-Wlevel]\n"
070c62a6 198 "\t [file ...]\n",
80387638
SW
199 progname);
200
201 exit((int)MANDOCLEVEL_BADARG);
202}
203
204static void
070c62a6
FF
205parse(struct curparse *curp, int fd, const char *file,
206 enum mandoclevel *level)
80387638 207{
60e1e752
SW
208 enum mandoclevel rc;
209 struct mdoc *mdoc;
210 struct man *man;
80387638 211
60e1e752 212 /* Begin by parsing the file itself. */
80387638 213
60e1e752
SW
214 assert(file);
215 assert(fd >= -1);
80387638 216
60e1e752 217 rc = mparse_readfd(curp->mp, fd, file);
80387638 218
60e1e752 219 /* Stop immediately if the parse has failed. */
80387638 220
60e1e752 221 if (MANDOCLEVEL_FATAL <= rc)
80387638 222 goto cleanup;
80387638
SW
223
224 /*
60e1e752
SW
225 * With -Wstop and warnings or errors of at least the requested
226 * level, do not produce output.
80387638
SW
227 */
228
60e1e752 229 if (MANDOCLEVEL_OK != rc && curp->wstop)
80387638
SW
230 goto cleanup;
231
232 /* If unset, allocate output dev now (if applicable). */
233
234 if ( ! (curp->outman && curp->outmdoc)) {
235 switch (curp->outtype) {
070c62a6 236 case OUTT_XHTML:
80387638 237 curp->outdata = xhtml_alloc(curp->outopts);
a4c7eb57 238 curp->outfree = html_free;
80387638 239 break;
070c62a6 240 case OUTT_HTML:
80387638 241 curp->outdata = html_alloc(curp->outopts);
a4c7eb57
SW
242 curp->outfree = html_free;
243 break;
070c62a6 244 case OUTT_UTF8:
a4c7eb57
SW
245 curp->outdata = utf8_alloc(curp->outopts);
246 curp->outfree = ascii_free;
247 break;
070c62a6 248 case OUTT_LOCALE:
a4c7eb57
SW
249 curp->outdata = locale_alloc(curp->outopts);
250 curp->outfree = ascii_free;
80387638 251 break;
070c62a6 252 case OUTT_ASCII:
80387638
SW
253 curp->outdata = ascii_alloc(curp->outopts);
254 curp->outfree = ascii_free;
255 break;
070c62a6 256 case OUTT_PDF:
80387638
SW
257 curp->outdata = pdf_alloc(curp->outopts);
258 curp->outfree = pspdf_free;
259 break;
070c62a6 260 case OUTT_PS:
80387638
SW
261 curp->outdata = ps_alloc(curp->outopts);
262 curp->outfree = pspdf_free;
263 break;
264 default:
265 break;
266 }
267
268 switch (curp->outtype) {
070c62a6 269 case OUTT_HTML:
80387638 270 /* FALLTHROUGH */
070c62a6 271 case OUTT_XHTML:
80387638
SW
272 curp->outman = html_man;
273 curp->outmdoc = html_mdoc;
80387638 274 break;
070c62a6 275 case OUTT_TREE:
80387638
SW
276 curp->outman = tree_man;
277 curp->outmdoc = tree_mdoc;
278 break;
070c62a6 279 case OUTT_MAN:
36342e81
SW
280 curp->outmdoc = man_mdoc;
281 curp->outman = man_man;
282 break;
070c62a6 283 case OUTT_PDF:
80387638 284 /* FALLTHROUGH */
070c62a6 285 case OUTT_ASCII:
80387638 286 /* FALLTHROUGH */
070c62a6 287 case OUTT_UTF8:
a4c7eb57 288 /* FALLTHROUGH */
070c62a6 289 case OUTT_LOCALE:
a4c7eb57 290 /* FALLTHROUGH */
070c62a6 291 case OUTT_PS:
80387638
SW
292 curp->outman = terminal_man;
293 curp->outmdoc = terminal_mdoc;
294 break;
295 default:
296 break;
297 }
298 }
299
070c62a6 300 mparse_result(curp->mp, &mdoc, &man, NULL);
60e1e752 301
80387638
SW
302 /* Execute the out device, if it exists. */
303
60e1e752
SW
304 if (man && curp->outman)
305 (*curp->outman)(curp->outdata, man);
306 if (mdoc && curp->outmdoc)
307 (*curp->outmdoc)(curp->outdata, mdoc);
80387638
SW
308
309 cleanup:
310
60e1e752 311 mparse_reset(curp->mp);
80387638 312
60e1e752
SW
313 if (*level < rc)
314 *level = rc;
80387638
SW
315}
316
317static int
070c62a6 318moptions(int *options, char *arg)
80387638
SW
319{
320
321 if (0 == strcmp(arg, "doc"))
070c62a6 322 *options |= MPARSE_MDOC;
80387638 323 else if (0 == strcmp(arg, "andoc"))
070c62a6 324 /* nothing to do */;
80387638 325 else if (0 == strcmp(arg, "an"))
070c62a6 326 *options |= MPARSE_MAN;
80387638 327 else {
070c62a6
FF
328 fprintf(stderr, "%s: -m%s: Bad argument\n",
329 progname, arg);
80387638
SW
330 return(0);
331 }
332
333 return(1);
334}
335
336static int
337toptions(struct curparse *curp, char *arg)
338{
339
340 if (0 == strcmp(arg, "ascii"))
341 curp->outtype = OUTT_ASCII;
342 else if (0 == strcmp(arg, "lint")) {
343 curp->outtype = OUTT_LINT;
344 curp->wlevel = MANDOCLEVEL_WARNING;
60e1e752 345 } else if (0 == strcmp(arg, "tree"))
80387638 346 curp->outtype = OUTT_TREE;
36342e81
SW
347 else if (0 == strcmp(arg, "man"))
348 curp->outtype = OUTT_MAN;
80387638
SW
349 else if (0 == strcmp(arg, "html"))
350 curp->outtype = OUTT_HTML;
a4c7eb57
SW
351 else if (0 == strcmp(arg, "utf8"))
352 curp->outtype = OUTT_UTF8;
353 else if (0 == strcmp(arg, "locale"))
354 curp->outtype = OUTT_LOCALE;
80387638
SW
355 else if (0 == strcmp(arg, "xhtml"))
356 curp->outtype = OUTT_XHTML;
357 else if (0 == strcmp(arg, "ps"))
358 curp->outtype = OUTT_PS;
359 else if (0 == strcmp(arg, "pdf"))
360 curp->outtype = OUTT_PDF;
361 else {
070c62a6
FF
362 fprintf(stderr, "%s: -T%s: Bad argument\n",
363 progname, arg);
80387638
SW
364 return(0);
365 }
366
367 return(1);
368}
369
370static int
371woptions(struct curparse *curp, char *arg)
372{
373 char *v, *o;
070c62a6 374 const char *toks[6];
80387638
SW
375
376 toks[0] = "stop";
377 toks[1] = "all";
378 toks[2] = "warning";
379 toks[3] = "error";
380 toks[4] = "fatal";
381 toks[5] = NULL;
382
383 while (*arg) {
384 o = arg;
385 switch (getsubopt(&arg, UNCONST(toks), &v)) {
070c62a6 386 case 0:
80387638
SW
387 curp->wstop = 1;
388 break;
070c62a6 389 case 1:
80387638 390 /* FALLTHROUGH */
070c62a6 391 case 2:
80387638
SW
392 curp->wlevel = MANDOCLEVEL_WARNING;
393 break;
070c62a6 394 case 3:
80387638
SW
395 curp->wlevel = MANDOCLEVEL_ERROR;
396 break;
070c62a6 397 case 4:
80387638
SW
398 curp->wlevel = MANDOCLEVEL_FATAL;
399 break;
400 default:
070c62a6
FF
401 fprintf(stderr, "%s: -W%s: Bad argument\n",
402 progname, o);
80387638
SW
403 return(0);
404 }
405 }
406
407 return(1);
408}
409
60e1e752 410static void
070c62a6 411mmsg(enum mandocerr t, enum mandoclevel lvl,
60e1e752 412 const char *file, int line, int col, const char *msg)
80387638 413{
070c62a6
FF
414 const char *mparse_msg;
415
416 fprintf(stderr, "%s: %s:", progname, file);
417
418 if (line)
419 fprintf(stderr, "%d:%d:", line, col + 1);
420
421 fprintf(stderr, " %s", mparse_strlevel(lvl));
80387638 422
070c62a6
FF
423 if (NULL != (mparse_msg = mparse_strerror(t)))
424 fprintf(stderr, ": %s", mparse_msg);
80387638 425
80387638
SW
426 if (msg)
427 fprintf(stderr, ": %s", msg);
80387638 428
60e1e752 429 fputc('\n', stderr);
80387638 430}