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

Annotation of mandoc/main.c, Revision 1.162

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

CVSweb