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

Annotation of mandoc/main.c, Revision 1.174

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

CVSweb