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

Annotation of mandoc/main.c, Revision 1.165

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

CVSweb