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

Annotation of mandoc/main.c, Revision 1.179

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

CVSweb