1 /* $Id: main.c,v 1.59 2010/01/29 14:39:38 kristaps Exp $ */
3 * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 #define UNCONST(a) ((void *)(uintptr_t)(const void *)(a))
33 /* FIXME: Intel's compiler? LLVM? pcc? */
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 *);
65 const char *file; /* Current parse. */
66 int fd; /* Current parse. */
68 #define WARN_WALL (1 << 0) /* All-warnings mask. */
69 #define WARN_WERR (1 << 2) /* Warnings->errors. */
71 #define IGN_SCOPE (1 << 0) /* Ignore scope errors. */
72 #define NO_IGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
73 #define NO_IGN_MACRO (1 << 2) /* Don't ignore bad macros. */
74 #define NO_IGN_CHARS (1 << 3) /* Don't ignore bad chars. */
75 #define IGN_ERRORS (1 << 4) /* Ignore failed parse. */
76 enum intt inttype; /* Input parsers... */
80 struct mdoc *lastmdoc;
81 enum outt outtype; /* Output devices... */
89 static int foptions(int *, char *);
90 static int toptions(enum outt *, char *);
91 static int moptions(enum intt *, char *);
92 static int woptions(int *, char *);
93 static int merr(void *, int, int, const char *);
94 static int mwarn(void *, int, int, const char *);
95 static int ffile(struct buf *, struct buf *,
96 const char *, struct curparse *);
97 static int fdesc(struct buf *, struct buf *,
99 static int pset(const char *, int, struct curparse *,
100 struct man **, struct mdoc **);
101 static struct man *man_init(struct curparse *);
102 static struct mdoc *mdoc_init(struct curparse *);
103 static void version(void) __attribute__((noreturn));
104 static void usage(void) __attribute__((noreturn));
106 static const char *progname;
110 main(int argc, char *argv[])
114 struct curparse curp;
116 progname = strrchr(argv[0], '/');
117 if (progname == NULL)
122 memset(&curp, 0, sizeof(struct curparse));
124 curp.inttype = INTT_AUTO;
125 curp.outtype = OUTT_ASCII;
128 while (-1 != (c = getopt(argc, argv, "f:m:O:T:VW:")))
131 if ( ! foptions(&curp.fflags, optarg))
132 return(EXIT_FAILURE);
135 if ( ! moptions(&curp.inttype, optarg))
136 return(EXIT_FAILURE);
139 (void)strlcat(curp.outopts, optarg, BUFSIZ);
140 (void)strlcat(curp.outopts, ",", BUFSIZ);
143 if ( ! toptions(&curp.outtype, optarg))
144 return(EXIT_FAILURE);
147 if ( ! woptions(&curp.wflags, optarg))
148 return(EXIT_FAILURE);
161 memset(&ln, 0, sizeof(struct buf));
162 memset(&blk, 0, sizeof(struct buf));
167 curp.file = "<stdin>";
168 curp.fd = STDIN_FILENO;
170 c = fdesc(&blk, &ln, &curp);
171 if ( ! (IGN_ERRORS & curp.fflags))
174 rc = -1 == c ? 0 : 1;
177 while (rc && *argv) {
178 c = ffile(&blk, &ln, *argv, &curp);
179 if ( ! (IGN_ERRORS & curp.fflags))
182 rc = -1 == c ? 0 : 1;
187 man_reset(curp.lastman);
189 mdoc_reset(curp.lastmdoc);
191 curp.lastmdoc = NULL;
200 (*curp.outfree)(curp.outdata);
202 mdoc_free(curp.mdoc);
206 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
214 (void)printf("%s %s\n", progname, VERSION);
223 (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
224 "[-mformat] [-Ooption] [-Toutput] "
225 "[-Werr...]\n", progname);
231 man_init(struct curparse *curp)
236 mancb.man_err = merr;
237 mancb.man_warn = mwarn;
239 /* Defaults from mandoc.1. */
241 pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE | MAN_IGN_CHARS;
243 if (curp->fflags & NO_IGN_MACRO)
244 pflags &= ~MAN_IGN_MACRO;
245 if (curp->fflags & NO_IGN_CHARS)
246 pflags &= ~MAN_IGN_CHARS;
247 if (curp->fflags & NO_IGN_ESCAPE)
248 pflags &= ~MAN_IGN_ESCAPE;
250 return(man_alloc(curp, pflags, &mancb));
255 mdoc_init(struct curparse *curp)
258 struct mdoc_cb mdoccb;
260 mdoccb.mdoc_err = merr;
261 mdoccb.mdoc_warn = mwarn;
263 /* Defaults from mandoc.1. */
265 pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
267 if (curp->fflags & IGN_SCOPE)
268 pflags |= MDOC_IGN_SCOPE;
269 if (curp->fflags & NO_IGN_ESCAPE)
270 pflags &= ~MDOC_IGN_ESCAPE;
271 if (curp->fflags & NO_IGN_MACRO)
272 pflags &= ~MDOC_IGN_MACRO;
273 if (curp->fflags & NO_IGN_CHARS)
274 pflags &= ~MDOC_IGN_CHARS;
276 return(mdoc_alloc(curp, pflags, &mdoccb));
281 ffile(struct buf *blk, struct buf *ln,
282 const char *file, struct curparse *curp)
287 if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
292 c = fdesc(blk, ln, curp);
294 if (-1 == close(curp->fd))
302 fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
307 int j, i, pos, lnn, comment;
316 * Two buffers: ln and buf. buf is the input buffer optimised
317 * here for each file's block size. ln is a line buffer. Both
318 * growable, hence passed in by ptr-ptr.
321 if (-1 == fstat(curp->fd, &st))
323 else if ((size_t)st.st_blksize > sz)
327 blk->buf = realloc(blk->buf, sz);
328 if (NULL == blk->buf) {
335 /* Fill buf with file blocksize. */
337 for (lnn = pos = comment = 0; ; ) {
338 if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
344 /* Parse the read block into partial or full lines. */
346 for (i = 0; i < (int)ssz; i++) {
347 if (pos >= (int)ln->sz) {
348 ln->sz += 256; /* Step-size. */
349 ln->buf = realloc(ln->buf, ln->sz);
350 if (NULL == ln->buf) {
352 return(EXIT_FAILURE);
356 if ('\n' != blk->buf[i]) {
359 ln->buf[pos++] = blk->buf[i];
361 /* Handle in-line `\"' comments. */
363 if (1 == pos || '\"' != ln->buf[pos - 1])
366 for (j = pos - 2; j >= 0; j--)
367 if ('\\' != ln->buf[j])
370 if ( ! ((pos - 2 - j) % 2))
378 /* Handle escaped `\\n' newlines. */
380 if (pos > 0 && 0 == comment &&
381 '\\' == ln->buf[pos - 1]) {
382 for (j = pos - 1; j >= 0; j--)
383 if ('\\' != ln->buf[j])
385 if ( ! ((pos - j) % 2)) {
395 /* If unset, assign parser in pset(). */
397 if ( ! (man || mdoc) && ! pset(ln->buf,
398 pos, curp, &man, &mdoc))
403 /* Pass down into parsers. */
405 if (man && ! man_parseln(man, lnn, ln->buf))
407 if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
412 /* NOTE a parser may not have been assigned, yet. */
414 if ( ! (man || mdoc)) {
415 fprintf(stderr, "%s: Not a manual\n", curp->file);
419 if (mdoc && ! mdoc_endparse(mdoc))
421 if (man && ! man_endparse(man))
424 /* If unset, allocate output dev now (if applicable). */
426 if ( ! (curp->outman && curp->outmdoc)) {
427 switch (curp->outtype) {
429 curp->outdata = xhtml_alloc(curp->outopts);
430 curp->outman = html_man;
431 curp->outmdoc = html_mdoc;
432 curp->outfree = html_free;
435 curp->outdata = html_alloc(curp->outopts);
436 curp->outman = html_man;
437 curp->outmdoc = html_mdoc;
438 curp->outfree = html_free;
441 curp->outman = tree_man;
442 curp->outmdoc = tree_mdoc;
447 curp->outdata = ascii_alloc();
448 curp->outman = terminal_man;
449 curp->outmdoc = terminal_mdoc;
450 curp->outfree = terminal_free;
455 /* Execute the out device, if it exists. */
457 if (man && curp->outman)
458 (*curp->outman)(curp->outdata, man);
459 if (mdoc && curp->outmdoc)
460 (*curp->outmdoc)(curp->outdata, mdoc);
467 pset(const char *buf, int pos, struct curparse *curp,
468 struct man **man, struct mdoc **mdoc)
473 * Try to intuit which kind of manual parser should be used. If
474 * passed in by command-line (-man, -mdoc), then use that
475 * explicitly. If passed as -mandoc, then try to guess from the
476 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
477 * default to -man, which is more lenient.
481 for (i = 1; buf[i]; i++)
482 if (' ' != buf[i] && '\t' != buf[i])
488 switch (curp->inttype) {
490 if (NULL == curp->mdoc)
491 curp->mdoc = mdoc_init(curp);
492 if (NULL == (*mdoc = curp->mdoc))
494 curp->lastmdoc = *mdoc;
497 if (NULL == curp->man)
498 curp->man = man_init(curp);
499 if (NULL == (*man = curp->man))
501 curp->lastman = *man;
507 if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
508 if (NULL == curp->mdoc)
509 curp->mdoc = mdoc_init(curp);
510 if (NULL == (*mdoc = curp->mdoc))
512 curp->lastmdoc = *mdoc;
516 if (NULL == curp->man)
517 curp->man = man_init(curp);
518 if (NULL == (*man = curp->man))
520 curp->lastman = *man;
526 moptions(enum intt *tflags, char *arg)
529 if (0 == strcmp(arg, "doc"))
531 else if (0 == strcmp(arg, "andoc"))
533 else if (0 == strcmp(arg, "an"))
536 fprintf(stderr, "%s: Bad argument\n", arg);
545 toptions(enum outt *tflags, char *arg)
548 if (0 == strcmp(arg, "ascii"))
549 *tflags = OUTT_ASCII;
550 else if (0 == strcmp(arg, "lint"))
552 else if (0 == strcmp(arg, "tree"))
554 else if (0 == strcmp(arg, "html"))
556 else if (0 == strcmp(arg, "xhtml"))
557 *tflags = OUTT_XHTML;
559 fprintf(stderr, "%s: Bad argument\n", arg);
568 foptions(int *fflags, char *arg)
573 toks[0] = "ign-scope";
574 toks[1] = "no-ign-escape";
575 toks[2] = "no-ign-macro";
576 toks[3] = "no-ign-chars";
577 toks[4] = "ign-errors";
579 toks[6] = "ign-escape";
584 switch (getsubopt(&arg, UNCONST(toks), &v)) {
586 *fflags |= IGN_SCOPE;
589 *fflags |= NO_IGN_ESCAPE;
592 *fflags |= NO_IGN_MACRO;
595 *fflags |= NO_IGN_CHARS;
598 *fflags |= IGN_ERRORS;
601 *fflags |= NO_IGN_ESCAPE |
602 NO_IGN_MACRO | NO_IGN_CHARS;
605 *fflags &= ~NO_IGN_ESCAPE;
608 fprintf(stderr, "%s: Bad argument\n", o);
618 woptions(int *wflags, char *arg)
629 switch (getsubopt(&arg, UNCONST(toks), &v)) {
631 *wflags |= WARN_WALL;
634 *wflags |= WARN_WERR;
637 fprintf(stderr, "%s: Bad argument\n", o);
648 merr(void *arg, int line, int col, const char *msg)
650 struct curparse *curp;
652 curp = (struct curparse *)arg;
654 (void)fprintf(stderr, "%s:%d:%d: error: %s\n",
655 curp->file, line, col + 1, msg);
662 mwarn(void *arg, int line, int col, const char *msg)
664 struct curparse *curp;
666 curp = (struct curparse *)arg;
668 if ( ! (curp->wflags & WARN_WALL))
671 (void)fprintf(stderr, "%s:%d:%d: warning: %s\n",
672 curp->file, line, col + 1, msg);
674 if ( ! (curp->wflags & WARN_WERR))