1 /* $Id: main.c,v 1.60 2010/03/22 20:43:00 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 FL_IGN_SCOPE (1 << 0) /* Ignore scope errors. */
72 #define FL_NIGN_ESCAPE (1 << 1) /* Don't ignore bad escapes. */
73 #define FL_NIGN_MACRO (1 << 2) /* Don't ignore bad macros. */
74 #define FL_NIGN_CHARS (1 << 3) /* Don't ignore bad chars. */
75 #define FL_IGN_ERRORS (1 << 4) /* Ignore failed parse. */
76 enum intt inttype; /* Input parsers... */
80 struct mdoc *lastmdoc;
81 enum outt outtype; /* Output devices... */
89 #define FL_STRICT FL_NIGN_ESCAPE | \
93 static int foptions(int *, char *);
94 static int toptions(struct curparse *, char *);
95 static int moptions(enum intt *, char *);
96 static int woptions(int *, char *);
97 static int merr(void *, int, int, const char *);
98 static int mwarn(void *, int, int, const char *);
99 static int ffile(struct buf *, struct buf *,
100 const char *, struct curparse *);
101 static int fdesc(struct buf *, struct buf *,
103 static int pset(const char *, int, struct curparse *,
104 struct man **, struct mdoc **);
105 static struct man *man_init(struct curparse *);
106 static struct mdoc *mdoc_init(struct curparse *);
107 static void version(void) __attribute__((noreturn));
108 static void usage(void) __attribute__((noreturn));
110 static const char *progname;
114 main(int argc, char *argv[])
118 struct curparse curp;
120 progname = strrchr(argv[0], '/');
121 if (progname == NULL)
126 memset(&curp, 0, sizeof(struct curparse));
128 curp.inttype = INTT_AUTO;
129 curp.outtype = OUTT_ASCII;
132 while (-1 != (c = getopt(argc, argv, "f:m:O:T:VW:")))
135 if ( ! foptions(&curp.fflags, optarg))
136 return(EXIT_FAILURE);
139 if ( ! moptions(&curp.inttype, optarg))
140 return(EXIT_FAILURE);
143 (void)strlcat(curp.outopts, optarg, BUFSIZ);
144 (void)strlcat(curp.outopts, ",", BUFSIZ);
147 if ( ! toptions(&curp, optarg))
148 return(EXIT_FAILURE);
151 if ( ! woptions(&curp.wflags, optarg))
152 return(EXIT_FAILURE);
165 memset(&ln, 0, sizeof(struct buf));
166 memset(&blk, 0, sizeof(struct buf));
171 curp.file = "<stdin>";
172 curp.fd = STDIN_FILENO;
174 c = fdesc(&blk, &ln, &curp);
175 if ( ! (FL_IGN_ERRORS & curp.fflags))
178 rc = -1 == c ? 0 : 1;
181 while (rc && *argv) {
182 c = ffile(&blk, &ln, *argv, &curp);
183 if ( ! (FL_IGN_ERRORS & curp.fflags))
186 rc = -1 == c ? 0 : 1;
191 man_reset(curp.lastman);
193 mdoc_reset(curp.lastmdoc);
195 curp.lastmdoc = NULL;
204 (*curp.outfree)(curp.outdata);
206 mdoc_free(curp.mdoc);
210 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
218 (void)printf("%s %s\n", progname, VERSION);
227 (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
228 "[-mformat] [-Ooption] [-Toutput] "
229 "[-Werr...]\n", progname);
235 man_init(struct curparse *curp)
240 mancb.man_err = merr;
241 mancb.man_warn = mwarn;
243 /* Defaults from mandoc.1. */
245 pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE | MAN_IGN_CHARS;
247 if (curp->fflags & FL_NIGN_MACRO)
248 pflags &= ~MAN_IGN_MACRO;
249 if (curp->fflags & FL_NIGN_CHARS)
250 pflags &= ~MAN_IGN_CHARS;
251 if (curp->fflags & FL_NIGN_ESCAPE)
252 pflags &= ~MAN_IGN_ESCAPE;
254 return(man_alloc(curp, pflags, &mancb));
259 mdoc_init(struct curparse *curp)
262 struct mdoc_cb mdoccb;
264 mdoccb.mdoc_err = merr;
265 mdoccb.mdoc_warn = mwarn;
267 /* Defaults from mandoc.1. */
269 pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
271 if (curp->fflags & FL_IGN_SCOPE)
272 pflags |= MDOC_IGN_SCOPE;
273 if (curp->fflags & FL_NIGN_ESCAPE)
274 pflags &= ~MDOC_IGN_ESCAPE;
275 if (curp->fflags & FL_NIGN_MACRO)
276 pflags &= ~MDOC_IGN_MACRO;
277 if (curp->fflags & FL_NIGN_CHARS)
278 pflags &= ~MDOC_IGN_CHARS;
280 return(mdoc_alloc(curp, pflags, &mdoccb));
285 ffile(struct buf *blk, struct buf *ln,
286 const char *file, struct curparse *curp)
291 if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
296 c = fdesc(blk, ln, curp);
298 if (-1 == close(curp->fd))
306 fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
311 int j, i, pos, lnn, comment;
320 * Two buffers: ln and buf. buf is the input buffer optimised
321 * here for each file's block size. ln is a line buffer. Both
322 * growable, hence passed in by ptr-ptr.
325 if (-1 == fstat(curp->fd, &st))
327 else if ((size_t)st.st_blksize > sz)
331 blk->buf = realloc(blk->buf, sz);
332 if (NULL == blk->buf) {
339 /* Fill buf with file blocksize. */
341 for (lnn = pos = comment = 0; ; ) {
342 if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
348 /* Parse the read block into partial or full lines. */
350 for (i = 0; i < (int)ssz; i++) {
351 if (pos >= (int)ln->sz) {
352 ln->sz += 256; /* Step-size. */
353 ln->buf = realloc(ln->buf, ln->sz);
354 if (NULL == ln->buf) {
356 return(EXIT_FAILURE);
360 if ('\n' != blk->buf[i]) {
363 ln->buf[pos++] = blk->buf[i];
365 /* Handle in-line `\"' comments. */
367 if (1 == pos || '\"' != ln->buf[pos - 1])
370 for (j = pos - 2; j >= 0; j--)
371 if ('\\' != ln->buf[j])
374 if ( ! ((pos - 2 - j) % 2))
382 /* Handle escaped `\\n' newlines. */
384 if (pos > 0 && 0 == comment &&
385 '\\' == ln->buf[pos - 1]) {
386 for (j = pos - 1; j >= 0; j--)
387 if ('\\' != ln->buf[j])
389 if ( ! ((pos - j) % 2)) {
399 /* If unset, assign parser in pset(). */
401 if ( ! (man || mdoc) && ! pset(ln->buf,
402 pos, curp, &man, &mdoc))
407 /* Pass down into parsers. */
409 if (man && ! man_parseln(man, lnn, ln->buf))
411 if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
416 /* NOTE a parser may not have been assigned, yet. */
418 if ( ! (man || mdoc)) {
419 fprintf(stderr, "%s: Not a manual\n", curp->file);
423 if (mdoc && ! mdoc_endparse(mdoc))
425 if (man && ! man_endparse(man))
428 /* If unset, allocate output dev now (if applicable). */
430 if ( ! (curp->outman && curp->outmdoc)) {
431 switch (curp->outtype) {
433 curp->outdata = xhtml_alloc(curp->outopts);
434 curp->outman = html_man;
435 curp->outmdoc = html_mdoc;
436 curp->outfree = html_free;
439 curp->outdata = html_alloc(curp->outopts);
440 curp->outman = html_man;
441 curp->outmdoc = html_mdoc;
442 curp->outfree = html_free;
445 curp->outman = tree_man;
446 curp->outmdoc = tree_mdoc;
451 curp->outdata = ascii_alloc();
452 curp->outman = terminal_man;
453 curp->outmdoc = terminal_mdoc;
454 curp->outfree = terminal_free;
459 /* Execute the out device, if it exists. */
461 if (man && curp->outman)
462 (*curp->outman)(curp->outdata, man);
463 if (mdoc && curp->outmdoc)
464 (*curp->outmdoc)(curp->outdata, mdoc);
471 pset(const char *buf, int pos, struct curparse *curp,
472 struct man **man, struct mdoc **mdoc)
477 * Try to intuit which kind of manual parser should be used. If
478 * passed in by command-line (-man, -mdoc), then use that
479 * explicitly. If passed as -mandoc, then try to guess from the
480 * line: either skip dot-lines, use -mdoc when finding `.Dt', or
481 * default to -man, which is more lenient.
485 for (i = 1; buf[i]; i++)
486 if (' ' != buf[i] && '\t' != buf[i])
492 switch (curp->inttype) {
494 if (NULL == curp->mdoc)
495 curp->mdoc = mdoc_init(curp);
496 if (NULL == (*mdoc = curp->mdoc))
498 curp->lastmdoc = *mdoc;
501 if (NULL == curp->man)
502 curp->man = man_init(curp);
503 if (NULL == (*man = curp->man))
505 curp->lastman = *man;
511 if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3)) {
512 if (NULL == curp->mdoc)
513 curp->mdoc = mdoc_init(curp);
514 if (NULL == (*mdoc = curp->mdoc))
516 curp->lastmdoc = *mdoc;
520 if (NULL == curp->man)
521 curp->man = man_init(curp);
522 if (NULL == (*man = curp->man))
524 curp->lastman = *man;
530 moptions(enum intt *tflags, char *arg)
533 if (0 == strcmp(arg, "doc"))
535 else if (0 == strcmp(arg, "andoc"))
537 else if (0 == strcmp(arg, "an"))
540 fprintf(stderr, "%s: Bad argument\n", arg);
549 toptions(struct curparse *curp, char *arg)
552 if (0 == strcmp(arg, "ascii"))
553 curp->outtype = OUTT_ASCII;
554 else if (0 == strcmp(arg, "lint")) {
555 curp->outtype = OUTT_LINT;
556 curp->wflags |= WARN_WALL;
557 curp->fflags |= FL_STRICT;
559 else if (0 == strcmp(arg, "tree"))
560 curp->outtype = OUTT_TREE;
561 else if (0 == strcmp(arg, "html"))
562 curp->outtype = OUTT_HTML;
563 else if (0 == strcmp(arg, "xhtml"))
564 curp->outtype = OUTT_XHTML;
566 fprintf(stderr, "%s: Bad argument\n", arg);
575 foptions(int *fflags, char *arg)
580 toks[0] = "ign-scope";
581 toks[1] = "no-ign-escape";
582 toks[2] = "no-ign-macro";
583 toks[3] = "no-ign-chars";
584 toks[4] = "ign-errors";
586 toks[6] = "ign-escape";
591 switch (getsubopt(&arg, UNCONST(toks), &v)) {
593 *fflags |= FL_IGN_SCOPE;
596 *fflags |= FL_NIGN_ESCAPE;
599 *fflags |= FL_NIGN_MACRO;
602 *fflags |= FL_NIGN_CHARS;
605 *fflags |= FL_IGN_ERRORS;
608 *fflags |= FL_STRICT;
611 *fflags &= ~FL_NIGN_ESCAPE;
614 fprintf(stderr, "%s: Bad argument\n", o);
624 woptions(int *wflags, char *arg)
635 switch (getsubopt(&arg, UNCONST(toks), &v)) {
637 *wflags |= WARN_WALL;
640 *wflags |= WARN_WERR;
643 fprintf(stderr, "%s: Bad argument\n", o);
654 merr(void *arg, int line, int col, const char *msg)
656 struct curparse *curp;
658 curp = (struct curparse *)arg;
660 (void)fprintf(stderr, "%s:%d:%d: error: %s\n",
661 curp->file, line, col + 1, msg);
668 mwarn(void *arg, int line, int col, const char *msg)
670 struct curparse *curp;
672 curp = (struct curparse *)arg;
674 if ( ! (curp->wflags & WARN_WALL))
677 (void)fprintf(stderr, "%s:%d:%d: warning: %s\n",
678 curp->file, line, col + 1, msg);
680 if ( ! (curp->wflags & WARN_WERR))