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

Annotation of mandoc/main.c, Revision 1.167

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

CVSweb