[BACK]Return to main.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/main.c, Revision 1.163

1.163   ! kristaps    1: /*     $Id: main.c,v 1.162 2011/05/17 14:38:34 kristaps Exp $ */
1.1       kristaps    2: /*
1.133     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.140     schwarze    4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.25      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.25      kristaps   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.
1.1       kristaps   17:  */
1.58      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.1       kristaps   22: #include <assert.h>
                     23: #include <stdio.h>
1.45      kristaps   24: #include <stdint.h>
1.1       kristaps   25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
1.71      kristaps   29: #include "mandoc.h"
1.90      kristaps   30: #include "main.h"
1.1       kristaps   31: #include "mdoc.h"
1.10      kristaps   32: #include "man.h"
1.101     joerg      33:
1.55      kristaps   34: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56      kristaps   35: # if !defined(lint)
                     36: #  define __attribute__(x)
                     37: # endif
1.55      kristaps   38: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16      kristaps   39:
1.42      kristaps   40: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     41: typedef        void            (*out_man)(void *, const struct man *);
1.22      kristaps   42: typedef        void            (*out_free)(void *);
                     43:
1.19      kristaps   44: enum   outt {
1.154     kristaps   45:        OUTT_ASCII = 0, /* -Tascii */
1.162     kristaps   46:        OUTT_LOCALE,    /* -Tlocale */
1.163   ! kristaps   47:        OUTT_UTF8,      /* -Tutf8 */
1.154     kristaps   48:        OUTT_TREE,      /* -Ttree */
                     49:        OUTT_HTML,      /* -Thtml */
                     50:        OUTT_XHTML,     /* -Txhtml */
                     51:        OUTT_LINT,      /* -Tlint */
                     52:        OUTT_PS,        /* -Tps */
                     53:        OUTT_PDF        /* -Tpdf */
1.19      kristaps   54: };
                     55:
1.5       kristaps   56: struct curparse {
1.154     kristaps   57:        struct mparse    *mp;
1.148     kristaps   58:        enum mandoclevel  wlevel;       /* ignore messages below this */
                     59:        int               wstop;        /* stop after a file with a warning */
1.79      kristaps   60:        enum outt         outtype;      /* which output to use */
                     61:        out_mdoc          outmdoc;      /* mdoc output ptr */
                     62:        out_man           outman;       /* man output ptr */
                     63:        out_free          outfree;      /* free output ptr */
                     64:        void             *outdata;      /* data for output */
                     65:        char              outopts[BUFSIZ]; /* buf of output opts */
                     66: };
                     67:
1.154     kristaps   68: static int               moptions(enum mparset *, char *);
1.155     kristaps   69: static void              mmsg(enum mandocerr, enum mandoclevel,
                     70:                                const char *, int, int, const char *);
1.154     kristaps   71: static void              parse(struct curparse *, int,
                     72:                                const char *, enum mandoclevel *);
1.66      kristaps   73: static int               toptions(struct curparse *, char *);
                     74: static void              usage(void) __attribute__((noreturn));
1.55      kristaps   75: static void              version(void) __attribute__((noreturn));
1.103     schwarze   76: static int               woptions(struct curparse *, char *);
1.1       kristaps   77:
1.54      kristaps   78: static const char       *progname;
1.1       kristaps   79:
                     80: int
                     81: main(int argc, char *argv[])
                     82: {
1.64      kristaps   83:        int              c;
1.5       kristaps   84:        struct curparse  curp;
1.154     kristaps   85:        enum mparset     type;
                     86:        enum mandoclevel rc;
1.1       kristaps   87:
1.54      kristaps   88:        progname = strrchr(argv[0], '/');
                     89:        if (progname == NULL)
                     90:                progname = argv[0];
                     91:        else
                     92:                ++progname;
                     93:
1.51      kristaps   94:        memset(&curp, 0, sizeof(struct curparse));
1.5       kristaps   95:
1.154     kristaps   96:        type = MPARSE_AUTO;
1.22      kristaps   97:        curp.outtype = OUTT_ASCII;
1.103     schwarze   98:        curp.wlevel  = MANDOCLEVEL_FATAL;
1.19      kristaps   99:
1.1       kristaps  100:        /* LINTED */
1.103     schwarze  101:        while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1       kristaps  102:                switch (c) {
1.10      kristaps  103:                case ('m'):
1.154     kristaps  104:                        if ( ! moptions(&type, optarg))
1.105     kristaps  105:                                return((int)MANDOCLEVEL_BADARG);
1.10      kristaps  106:                        break;
1.48      kristaps  107:                case ('O'):
1.49      kristaps  108:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    109:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44      kristaps  110:                        break;
1.1       kristaps  111:                case ('T'):
1.60      kristaps  112:                        if ( ! toptions(&curp, optarg))
1.105     kristaps  113:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  114:                        break;
                    115:                case ('W'):
1.103     schwarze  116:                        if ( ! woptions(&curp, optarg))
1.105     kristaps  117:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  118:                        break;
                    119:                case ('V'):
                    120:                        version();
                    121:                        /* NOTREACHED */
                    122:                default:
                    123:                        usage();
                    124:                        /* NOTREACHED */
                    125:                }
                    126:
1.156     kristaps  127:        curp.mp = mparse_alloc(type, curp.wlevel, mmsg, &curp);
1.154     kristaps  128:
1.1       kristaps  129:        argc -= optind;
                    130:        argv += optind;
                    131:
1.154     kristaps  132:        rc = MANDOCLEVEL_OK;
1.39      kristaps  133:
1.154     kristaps  134:        if (NULL == *argv)
                    135:                parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64      kristaps  136:
                    137:        while (*argv) {
1.154     kristaps  138:                parse(&curp, -1, *argv, &rc);
                    139:                if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64      kristaps  140:                        break;
                    141:                ++argv;
1.1       kristaps  142:        }
                    143:
1.22      kristaps  144:        if (curp.outfree)
                    145:                (*curp.outfree)(curp.outdata);
1.154     kristaps  146:        if (curp.mp)
                    147:                mparse_free(curp.mp);
1.1       kristaps  148:
1.154     kristaps  149:        return((int)rc);
1.1       kristaps  150: }
                    151:
1.55      kristaps  152: static void
1.1       kristaps  153: version(void)
                    154: {
                    155:
1.154     kristaps  156:        printf("%s %s\n", progname, VERSION);
1.105     kristaps  157:        exit((int)MANDOCLEVEL_OK);
1.1       kristaps  158: }
                    159:
1.55      kristaps  160: static void
1.1       kristaps  161: usage(void)
                    162: {
                    163:
1.154     kristaps  164:        fprintf(stderr, "usage: %s "
1.112     kristaps  165:                        "[-V] "
                    166:                        "[-foption] "
                    167:                        "[-mformat] "
                    168:                        "[-Ooption] "
                    169:                        "[-Toutput] "
1.161     kristaps  170:                        "[-Wlevel] "
1.112     kristaps  171:                        "[file...]\n",
                    172:                        progname);
                    173:
1.105     kristaps  174:        exit((int)MANDOCLEVEL_BADARG);
1.68      joerg     175: }
                    176:
1.64      kristaps  177: static void
1.154     kristaps  178: parse(struct curparse *curp, int fd,
                    179:                const char *file, enum mandoclevel *level)
1.1       kristaps  180: {
1.154     kristaps  181:        enum mandoclevel  rc;
                    182:        struct mdoc      *mdoc;
                    183:        struct man       *man;
1.112     kristaps  184:
1.154     kristaps  185:        /* Begin by parsing the file itself. */
1.112     kristaps  186:
1.154     kristaps  187:        assert(file);
                    188:        assert(fd >= -1);
1.112     kristaps  189:
1.154     kristaps  190:        rc = mparse_readfd(curp->mp, fd, file);
1.112     kristaps  191:
1.154     kristaps  192:        /* Stop immediately if the parse has failed. */
1.92      kristaps  193:
1.154     kristaps  194:        if (MANDOCLEVEL_FATAL <= rc)
1.111     kristaps  195:                goto cleanup;
                    196:
1.1       kristaps  197:        /*
1.154     kristaps  198:         * With -Wstop and warnings or errors of at least the requested
                    199:         * level, do not produce output.
1.1       kristaps  200:         */
                    201:
1.154     kristaps  202:        if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111     kristaps  203:                goto cleanup;
                    204:
                    205:        /* If unset, allocate output dev now (if applicable). */
                    206:
                    207:        if ( ! (curp->outman && curp->outmdoc)) {
                    208:                switch (curp->outtype) {
                    209:                case (OUTT_XHTML):
                    210:                        curp->outdata = xhtml_alloc(curp->outopts);
1.162     kristaps  211:                        curp->outfree = html_free;
1.111     kristaps  212:                        break;
                    213:                case (OUTT_HTML):
                    214:                        curp->outdata = html_alloc(curp->outopts);
1.162     kristaps  215:                        curp->outfree = html_free;
                    216:                        break;
1.163   ! kristaps  217:                case (OUTT_UTF8):
        !           218:                        curp->outdata = utf8_alloc(curp->outopts);
        !           219:                        curp->outfree = ascii_free;
        !           220:                        break;
1.162     kristaps  221:                case (OUTT_LOCALE):
                    222:                        curp->outdata = locale_alloc(curp->outopts);
                    223:                        curp->outfree = ascii_free;
1.111     kristaps  224:                        break;
                    225:                case (OUTT_ASCII):
                    226:                        curp->outdata = ascii_alloc(curp->outopts);
                    227:                        curp->outfree = ascii_free;
                    228:                        break;
                    229:                case (OUTT_PDF):
                    230:                        curp->outdata = pdf_alloc(curp->outopts);
                    231:                        curp->outfree = pspdf_free;
                    232:                        break;
                    233:                case (OUTT_PS):
                    234:                        curp->outdata = ps_alloc(curp->outopts);
                    235:                        curp->outfree = pspdf_free;
                    236:                        break;
                    237:                default:
                    238:                        break;
                    239:                }
                    240:
                    241:                switch (curp->outtype) {
                    242:                case (OUTT_HTML):
                    243:                        /* FALLTHROUGH */
                    244:                case (OUTT_XHTML):
                    245:                        curp->outman = html_man;
                    246:                        curp->outmdoc = html_mdoc;
                    247:                        break;
                    248:                case (OUTT_TREE):
                    249:                        curp->outman = tree_man;
                    250:                        curp->outmdoc = tree_mdoc;
                    251:                        break;
                    252:                case (OUTT_PDF):
                    253:                        /* FALLTHROUGH */
                    254:                case (OUTT_ASCII):
                    255:                        /* FALLTHROUGH */
1.163   ! kristaps  256:                case (OUTT_UTF8):
        !           257:                        /* FALLTHROUGH */
1.162     kristaps  258:                case (OUTT_LOCALE):
                    259:                        /* FALLTHROUGH */
1.111     kristaps  260:                case (OUTT_PS):
                    261:                        curp->outman = terminal_man;
                    262:                        curp->outmdoc = terminal_mdoc;
                    263:                        break;
                    264:                default:
                    265:                        break;
                    266:                }
                    267:        }
                    268:
1.154     kristaps  269:        mparse_result(curp->mp, &mdoc, &man);
                    270:
1.111     kristaps  271:        /* Execute the out device, if it exists. */
                    272:
1.154     kristaps  273:        if (man && curp->outman)
                    274:                (*curp->outman)(curp->outdata, man);
                    275:        if (mdoc && curp->outmdoc)
                    276:                (*curp->outmdoc)(curp->outdata, mdoc);
1.111     kristaps  277:
                    278:  cleanup:
1.112     kristaps  279:
1.154     kristaps  280:        mparse_reset(curp->mp);
1.111     kristaps  281:
1.154     kristaps  282:        if (*level < rc)
                    283:                *level = rc;
1.10      kristaps  284: }
                    285:
                    286: static int
1.154     kristaps  287: moptions(enum mparset *tflags, char *arg)
1.10      kristaps  288: {
                    289:
1.17      kristaps  290:        if (0 == strcmp(arg, "doc"))
1.154     kristaps  291:                *tflags = MPARSE_MDOC;
1.19      kristaps  292:        else if (0 == strcmp(arg, "andoc"))
1.154     kristaps  293:                *tflags = MPARSE_AUTO;
1.17      kristaps  294:        else if (0 == strcmp(arg, "an"))
1.154     kristaps  295:                *tflags = MPARSE_MAN;
1.10      kristaps  296:        else {
1.57      kristaps  297:                fprintf(stderr, "%s: Bad argument\n", arg);
1.10      kristaps  298:                return(0);
                    299:        }
                    300:
                    301:        return(1);
1.1       kristaps  302: }
                    303:
                    304: static int
1.60      kristaps  305: toptions(struct curparse *curp, char *arg)
1.1       kristaps  306: {
                    307:
                    308:        if (0 == strcmp(arg, "ascii"))
1.60      kristaps  309:                curp->outtype = OUTT_ASCII;
                    310:        else if (0 == strcmp(arg, "lint")) {
                    311:                curp->outtype = OUTT_LINT;
1.103     schwarze  312:                curp->wlevel  = MANDOCLEVEL_WARNING;
1.154     kristaps  313:        } else if (0 == strcmp(arg, "tree"))
1.60      kristaps  314:                curp->outtype = OUTT_TREE;
1.43      kristaps  315:        else if (0 == strcmp(arg, "html"))
1.60      kristaps  316:                curp->outtype = OUTT_HTML;
1.163   ! kristaps  317:        else if (0 == strcmp(arg, "utf8"))
        !           318:                curp->outtype = OUTT_UTF8;
1.162     kristaps  319:        else if (0 == strcmp(arg, "locale"))
                    320:                curp->outtype = OUTT_LOCALE;
1.59      kristaps  321:        else if (0 == strcmp(arg, "xhtml"))
1.60      kristaps  322:                curp->outtype = OUTT_XHTML;
1.85      kristaps  323:        else if (0 == strcmp(arg, "ps"))
                    324:                curp->outtype = OUTT_PS;
1.100     kristaps  325:        else if (0 == strcmp(arg, "pdf"))
                    326:                curp->outtype = OUTT_PDF;
1.1       kristaps  327:        else {
1.57      kristaps  328:                fprintf(stderr, "%s: Bad argument\n", arg);
1.1       kristaps  329:                return(0);
                    330:        }
                    331:
                    332:        return(1);
                    333: }
                    334:
                    335: static int
1.103     schwarze  336: woptions(struct curparse *curp, char *arg)
1.1       kristaps  337: {
1.34      kristaps  338:        char            *v, *o;
1.103     schwarze  339:        const char      *toks[6];
1.1       kristaps  340:
1.103     schwarze  341:        toks[0] = "stop";
                    342:        toks[1] = "all";
                    343:        toks[2] = "warning";
                    344:        toks[3] = "error";
                    345:        toks[4] = "fatal";
                    346:        toks[5] = NULL;
1.1       kristaps  347:
1.34      kristaps  348:        while (*arg) {
                    349:                o = arg;
1.45      kristaps  350:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  351:                case (0):
1.103     schwarze  352:                        curp->wstop = 1;
1.1       kristaps  353:                        break;
                    354:                case (1):
1.103     schwarze  355:                        /* FALLTHROUGH */
1.1       kristaps  356:                case (2):
1.103     schwarze  357:                        curp->wlevel = MANDOCLEVEL_WARNING;
1.1       kristaps  358:                        break;
1.20      kristaps  359:                case (3):
1.103     schwarze  360:                        curp->wlevel = MANDOCLEVEL_ERROR;
1.24      kristaps  361:                        break;
                    362:                case (4):
1.103     schwarze  363:                        curp->wlevel = MANDOCLEVEL_FATAL;
1.50      kristaps  364:                        break;
1.1       kristaps  365:                default:
1.103     schwarze  366:                        fprintf(stderr, "-W%s: Bad argument\n", o);
1.1       kristaps  367:                        return(0);
                    368:                }
1.34      kristaps  369:        }
1.1       kristaps  370:
                    371:        return(1);
                    372: }
                    373:
1.153     kristaps  374: static void
1.155     kristaps  375: mmsg(enum mandocerr t, enum mandoclevel lvl,
                    376:                const char *file, int line, int col, const char *msg)
1.71      kristaps  377: {
1.103     schwarze  378:
1.155     kristaps  379:        fprintf(stderr, "%s:%d:%d: %s: %s",
                    380:                        file, line, col + 1,
1.160     kristaps  381:                        mparse_strlevel(lvl),
                    382:                        mparse_strerror(t));
1.154     kristaps  383:
1.73      kristaps  384:        if (msg)
                    385:                fprintf(stderr, ": %s", msg);
1.154     kristaps  386:
1.73      kristaps  387:        fputc('\n', stderr);
1.71      kristaps  388: }

CVSweb