=================================================================== RCS file: /cvs/mandoc/main.c,v retrieving revision 1.11 retrieving revision 1.17 diff -u -p -r1.11 -r1.17 --- mandoc/main.c 2009/03/23 15:20:51 1.11 +++ mandoc/main.c 2009/03/26 16:23:22 1.17 @@ -1,4 +1,4 @@ -/* $Id: main.c,v 1.11 2009/03/23 15:20:51 kristaps Exp $ */ +/* $Id: main.c,v 1.17 2009/03/26 16:23:22 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons * @@ -29,12 +29,14 @@ #include "mdoc.h" #include "man.h" +/* Account for FreeBSD and Linux in our declarations. */ + #ifdef __linux__ extern int getsubopt(char **, char * const *, char **); # ifndef __dead # define __dead __attribute__((__noreturn__)) # endif -#elif defined(__FreeBSD__) +#elif defined(__dead2) # ifndef __dead # define __dead __dead2 # endif @@ -54,6 +56,10 @@ struct curparse { #define WARN_WERR (1 << 2) /* Warnings->errors. */ }; +#define IGN_SCOPE (1 << 0) /* Ignore scope errors. */ +#define IGN_ESCAPE (1 << 1) /* Ignore bad escapes. */ +#define IGN_MACRO (1 << 2) /* Ignore unknown macros. */ + enum intt { INTT_MDOC = 0, INTT_MAN @@ -61,8 +67,6 @@ enum intt { enum outt { OUTT_ASCII = 0, - OUTT_LATIN1, - OUTT_UTF8, OUTT_TREE, OUTT_LINT }; @@ -74,8 +78,6 @@ typedef void (*out_free)(void *); extern char *__progname; extern void *ascii_alloc(void); -extern void *latin1_alloc(void); -extern void *utf8_alloc(void); extern int terminal_run(void *, const struct man *, const struct mdoc *); extern int tree_run(void *, const struct man *, @@ -87,7 +89,8 @@ static int toptions(enum outt *, char *); static int moptions(enum intt *, char *); static int woptions(int *, char *); static int merr(void *, int, int, const char *); -static int mwarn(void *, int, int, +static int manwarn(void *, int, int, const char *); +static int mdocwarn(void *, int, int, enum mdoc_warn, const char *); static int file(struct buf *, struct buf *, const char *, @@ -95,7 +98,6 @@ static int file(struct buf *, struct buf *, static int fdesc(struct buf *, struct buf *, const char *, int, struct man *, struct mdoc *); - __dead static void version(void); __dead static void usage(void); @@ -103,8 +105,9 @@ __dead static void usage(void); int main(int argc, char *argv[]) { - int c, rc, fflags; - struct mdoc_cb cb; + int c, rc, fflags, pflags; + struct mdoc_cb mdoccb; + struct man_cb mancb; struct man *man; struct mdoc *mdoc; void *outdata; @@ -152,23 +155,13 @@ main(int argc, char *argv[]) argv += optind; /* - * Allocate the appropriate front-end. Note that utf8, ascii - * and latin1 all resolve to the terminal front-end with - * different encodings (see terminal.c). Not all frontends have - * cleanup or alloc routines. + * Allocate the appropriate front-end. Note that utf8, latin1 + * (both not yet implemented) and ascii all resolve to the + * terminal front-end with different encodings (see terminal.c). + * Not all frontends have cleanup or alloc routines. */ switch (outtype) { - case (OUTT_LATIN1): - outdata = latin1_alloc(); - outrun = terminal_run; - outfree = terminal_free; - break; - case (OUTT_UTF8): - outdata = utf8_alloc(); - outrun = terminal_run; - outfree = terminal_free; - break; case (OUTT_TREE): outdata = NULL; outrun = tree_run; @@ -191,34 +184,53 @@ main(int argc, char *argv[]) * screen. XXX - for now, no path for debugging messages. */ - cb.mdoc_msg = NULL; - cb.mdoc_err = merr; - cb.mdoc_warn = mwarn; + mdoccb.mdoc_msg = NULL; + mdoccb.mdoc_err = merr; + mdoccb.mdoc_warn = mdocwarn; + mancb.man_err = merr; + mancb.man_warn = manwarn; + + /* Configure buffers. */ + bzero(&ln, sizeof(struct buf)); bzero(&blk, sizeof(struct buf)); man = NULL; mdoc = NULL; + pflags = 0; + /* + * Allocate the parser. There are two kinds of parser: libman + * and libmdoc. We must separately copy over the flags that + * we'll use internally. + */ + switch (inttype) { case (INTT_MAN): - man = man_alloc(); + if (fflags & IGN_MACRO) + pflags |= MAN_IGN_MACRO; + man = man_alloc(&curp, pflags, &mancb); break; default: - mdoc = mdoc_alloc(&curp, fflags, &cb); + if (fflags & IGN_SCOPE) + pflags |= MDOC_IGN_SCOPE; + if (fflags & IGN_ESCAPE) + pflags |= MDOC_IGN_ESCAPE; + if (fflags & IGN_MACRO) + pflags |= MDOC_IGN_MACRO; + mdoc = mdoc_alloc(&curp, pflags, &mdoccb); break; } /* - * Loop around available files. + * Main loop around available files. */ if (NULL == *argv) { curp.file = ""; rc = 0; - c = fdesc(&blk, &ln, "stdin", - STDIN_FILENO, man, mdoc); + c = fdesc(&blk, &ln, "stdin", STDIN_FILENO, man, mdoc); if (c && NULL == outrun) rc = 1; @@ -236,7 +248,6 @@ main(int argc, char *argv[]) man_reset(man); if (mdoc) mdoc_reset(mdoc); - argv++; } rc = NULL == *argv; @@ -271,7 +282,9 @@ __dead static void usage(void) { - (void)fprintf(stderr, "usage: %s\n", __progname); + (void)fprintf(stderr, "usage: %s [-V] [-foption...] " + "[-mformat] [-Toutput] [-Werr...]\n", + __progname); exit(1); /* NOTREACHED */ } @@ -388,9 +401,9 @@ static int moptions(enum intt *tflags, char *arg) { - if (0 == strcmp(arg, "mdoc")) + if (0 == strcmp(arg, "doc")) *tflags = INTT_MDOC; - else if (0 == strcmp(arg, "man")) + else if (0 == strcmp(arg, "an")) *tflags = INTT_MAN; else { warnx("bad argument: -m%s", arg); @@ -407,10 +420,6 @@ toptions(enum outt *tflags, char *arg) if (0 == strcmp(arg, "ascii")) *tflags = OUTT_ASCII; - else if (0 == strcmp(arg, "latin1")) - *tflags = OUTT_LATIN1; - else if (0 == strcmp(arg, "utf8")) - *tflags = OUTT_UTF8; else if (0 == strcmp(arg, "lint")) *tflags = OUTT_LINT; else if (0 == strcmp(arg, "tree")) @@ -442,13 +451,13 @@ foptions(int *fflags, char *arg) while (*arg) switch (getsubopt(&arg, toks, &v)) { case (0): - *fflags |= MDOC_IGN_SCOPE; + *fflags |= IGN_SCOPE; break; case (1): - *fflags |= MDOC_IGN_ESCAPE; + *fflags |= IGN_ESCAPE; break; case (2): - *fflags |= MDOC_IGN_MACRO; + *fflags |= IGN_MACRO; break; default: warnx("bad argument: -f%s", arg); @@ -513,7 +522,7 @@ merr(void *arg, int line, int col, const char *msg) static int -mwarn(void *arg, int line, int col, +mdocwarn(void *arg, int line, int col, enum mdoc_warn type, const char *msg) { struct curparse *curp; @@ -548,3 +557,23 @@ mwarn(void *arg, int line, int col, } +static int +manwarn(void *arg, int line, int col, const char *msg) +{ + struct curparse *curp; + + curp = (struct curparse *)arg; + + if ( ! (curp->wflags & WARN_WSYNTAX)) + return(1); + + warnx("%s:%d: syntax warning: %s (column %d)", + curp->file, line, msg, col); + + if ( ! (curp->wflags & WARN_WERR)) + return(1); + + warnx("%s: considering warnings as errors", + __progname); + return(0); +}