[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/main.c, Revision 1.97

1.97    ! schwarze    1: /*     $OpenBSD: main.c,v 1.96 2014/08/30 18:04:52 schwarze Exp $ */
1.1       kristaps    2: /*
1.95      schwarze    3:  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.87      schwarze    4:  * Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.86      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.2       schwarze    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.2       schwarze   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:  */
                     19:
1.95      schwarze   20: #include <sys/types.h>
                     21:
1.1       kristaps   22: #include <assert.h>
1.95      schwarze   23: #include <ctype.h>
                     24: #include <errno.h>
                     25: #include <fcntl.h>
1.1       kristaps   26: #include <stdio.h>
1.17      schwarze   27: #include <stdint.h>
1.1       kristaps   28: #include <stdlib.h>
                     29: #include <string.h>
                     30: #include <unistd.h>
                     31:
1.30      schwarze   32: #include "mandoc.h"
1.89      schwarze   33: #include "mandoc_aux.h"
1.38      schwarze   34: #include "main.h"
1.1       kristaps   35: #include "mdoc.h"
                     36: #include "man.h"
1.95      schwarze   37: #include "manpath.h"
                     38: #include "mansearch.h"
                     39:
                     40: enum   outmode {
                     41:        OUTMODE_DEF = 0,
                     42:        OUTMODE_FLN,
                     43:        OUTMODE_LST,
                     44:        OUTMODE_ALL,
                     45:        OUTMODE_INT,
                     46:        OUTMODE_ONE
                     47: };
1.1       kristaps   48:
1.16      schwarze   49: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     50: typedef        void            (*out_man)(void *, const struct man *);
1.1       kristaps   51: typedef        void            (*out_free)(void *);
                     52:
                     53: enum   outt {
1.76      schwarze   54:        OUTT_ASCII = 0, /* -Tascii */
1.77      schwarze   55:        OUTT_LOCALE,    /* -Tlocale */
                     56:        OUTT_UTF8,      /* -Tutf8 */
1.76      schwarze   57:        OUTT_TREE,      /* -Ttree */
1.78      schwarze   58:        OUTT_MAN,       /* -Tman */
1.76      schwarze   59:        OUTT_HTML,      /* -Thtml */
                     60:        OUTT_XHTML,     /* -Txhtml */
                     61:        OUTT_LINT,      /* -Tlint */
                     62:        OUTT_PS,        /* -Tps */
                     63:        OUTT_PDF        /* -Tpdf */
1.1       kristaps   64: };
                     65:
                     66: struct curparse {
1.76      schwarze   67:        struct mparse    *mp;
1.75      schwarze   68:        enum mandoclevel  wlevel;       /* ignore messages below this */
                     69:        int               wstop;        /* stop after a file with a warning */
1.90      schwarze   70:        enum outt         outtype;      /* which output to use */
1.33      schwarze   71:        out_mdoc          outmdoc;      /* mdoc output ptr */
1.90      schwarze   72:        out_man           outman;       /* man output ptr */
1.33      schwarze   73:        out_free          outfree;      /* free output ptr */
                     74:        void             *outdata;      /* data for output */
                     75:        char              outopts[BUFSIZ]; /* buf of output opts */
                     76: };
                     77:
1.79      schwarze   78: int                      mandocdb(int, char**);
1.87      schwarze   79: static int               moptions(int *, char *);
1.76      schwarze   80: static void              mmsg(enum mandocerr, enum mandoclevel,
                     81:                                const char *, int, int, const char *);
1.90      schwarze   82: static void              parse(struct curparse *, int,
1.76      schwarze   83:                                const char *, enum mandoclevel *);
1.95      schwarze   84: static enum mandoclevel  passthrough(const char *);
                     85: static void              spawn_pager(void);
1.27      schwarze   86: static int               toptions(struct curparse *, char *);
1.95      schwarze   87: static void              usage(enum argmode) __attribute__((noreturn));
1.20      schwarze   88: static void              version(void) __attribute__((noreturn));
1.45      schwarze   89: static int               woptions(struct curparse *, char *);
1.1       kristaps   90:
1.95      schwarze   91: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.19      schwarze   92: static const char       *progname;
1.1       kristaps   93:
1.90      schwarze   94:
1.1       kristaps   95: int
                     96: main(int argc, char *argv[])
                     97: {
                     98:        struct curparse  curp;
1.95      schwarze   99:        struct mansearch search;
                    100:        struct manpaths  paths;
                    101:        char            *conf_file, *defpaths, *auxpaths;
                    102:        char            *defos;
                    103:        struct manpage  *res, *resp;
                    104:        size_t           isec, i, sz;
                    105:        int              prio, best_prio;
                    106:        char             sec;
                    107:        enum mandoclevel rc;
                    108:        enum outmode     outmode;
                    109:        int              show_usage;
                    110:        int              use_pager;
1.87      schwarze  111:        int              options;
1.95      schwarze  112:        int              c;
1.1       kristaps  113:
1.19      schwarze  114:        progname = strrchr(argv[0], '/');
                    115:        if (progname == NULL)
                    116:                progname = argv[0];
                    117:        else
                    118:                ++progname;
1.79      schwarze  119:
1.82      schwarze  120:        if (0 == strncmp(progname, "mandocdb", 8) ||
                    121:            0 == strncmp(progname, "makewhatis", 10))
1.79      schwarze  122:                return(mandocdb(argc, argv));
1.19      schwarze  123:
1.95      schwarze  124:        /* Search options. */
                    125:
                    126:        memset(&paths, 0, sizeof(struct manpaths));
                    127:        conf_file = defpaths = auxpaths = NULL;
                    128:
                    129:        memset(&search, 0, sizeof(struct mansearch));
                    130:        search.outkey = "Nd";
                    131:
                    132:        if (strcmp(progname, "man") == 0)
                    133:                search.argmode = ARG_NAME;
                    134:        else if (strncmp(progname, "apropos", 7) == 0)
                    135:                search.argmode = ARG_EXPR;
                    136:        else if (strncmp(progname, "whatis", 6) == 0)
                    137:                search.argmode = ARG_WORD;
                    138:        else
                    139:                search.argmode = ARG_FILE;
                    140:
                    141:        /* Parser and formatter options. */
                    142:
1.19      schwarze  143:        memset(&curp, 0, sizeof(struct curparse));
1.1       kristaps  144:        curp.outtype = OUTT_ASCII;
1.45      schwarze  145:        curp.wlevel  = MANDOCLEVEL_FATAL;
1.95      schwarze  146:        options = MPARSE_SO;
1.83      schwarze  147:        defos = NULL;
1.1       kristaps  148:
1.95      schwarze  149:        use_pager = 1;
                    150:        show_usage = 0;
                    151:        outmode = OUTMODE_DEF;
                    152:
1.96      schwarze  153:        while (-1 != (c = getopt(argc, argv, "aC:cfI:iklM:m:O:S:s:T:VW:w"))) {
1.1       kristaps  154:                switch (c) {
1.95      schwarze  155:                case 'a':
                    156:                        outmode = OUTMODE_ALL;
                    157:                        break;
                    158:                case 'C':
                    159:                        conf_file = optarg;
                    160:                        break;
                    161:                case 'c':
                    162:                        use_pager = 0;
                    163:                        break;
                    164:                case 'f':
                    165:                        search.argmode = ARG_WORD;
                    166:                        break;
1.90      schwarze  167:                case 'I':
1.83      schwarze  168:                        if (strncmp(optarg, "os=", 3)) {
1.90      schwarze  169:                                fprintf(stderr,
1.93      schwarze  170:                                    "%s: -I%s: Bad argument\n",
                    171:                                    progname, optarg);
1.83      schwarze  172:                                return((int)MANDOCLEVEL_BADARG);
                    173:                        }
                    174:                        if (defos) {
1.90      schwarze  175:                                fprintf(stderr,
1.93      schwarze  176:                                    "%s: -I%s: Duplicate argument\n",
                    177:                                    progname, optarg);
1.83      schwarze  178:                                return((int)MANDOCLEVEL_BADARG);
                    179:                        }
                    180:                        defos = mandoc_strdup(optarg + 3);
                    181:                        break;
1.95      schwarze  182:                case 'i':
                    183:                        outmode = OUTMODE_INT;
                    184:                        break;
                    185:                case 'k':
                    186:                        search.argmode = ARG_EXPR;
                    187:                        break;
1.96      schwarze  188:                case 'l':
                    189:                        search.argmode = ARG_FILE;
                    190:                        outmode = OUTMODE_ALL;
                    191:                        break;
1.95      schwarze  192:                case 'M':
                    193:                        defpaths = optarg;
                    194:                        break;
1.90      schwarze  195:                case 'm':
1.95      schwarze  196:                        auxpaths = optarg;
1.1       kristaps  197:                        break;
1.90      schwarze  198:                case 'O':
1.95      schwarze  199:                        search.outkey = optarg;
1.18      schwarze  200:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    201:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.17      schwarze  202:                        break;
1.95      schwarze  203:                case 'S':
                    204:                        search.arch = optarg;
                    205:                        break;
                    206:                case 's':
                    207:                        search.sec = optarg;
                    208:                        break;
1.90      schwarze  209:                case 'T':
1.22      schwarze  210:                        if ( ! toptions(&curp, optarg))
1.47      schwarze  211:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  212:                        break;
1.90      schwarze  213:                case 'W':
1.45      schwarze  214:                        if ( ! woptions(&curp, optarg))
1.47      schwarze  215:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  216:                        break;
1.95      schwarze  217:                case 'w':
                    218:                        outmode = OUTMODE_FLN;
                    219:                        break;
1.90      schwarze  220:                case 'V':
1.3       schwarze  221:                        version();
                    222:                        /* NOTREACHED */
1.1       kristaps  223:                default:
1.95      schwarze  224:                        show_usage = 1;
                    225:                        break;
                    226:                }
                    227:        }
                    228:
                    229:        if (show_usage)
                    230:                usage(search.argmode);
                    231:
                    232:        /* Postprocess options. */
                    233:
                    234:        if (outmode == OUTMODE_DEF) {
                    235:                switch (search.argmode) {
                    236:                case ARG_FILE:
                    237:                        outmode = OUTMODE_ALL;
                    238:                        use_pager = 0;
                    239:                        break;
                    240:                case ARG_NAME:
                    241:                        outmode = OUTMODE_ONE;
                    242:                        break;
                    243:                default:
                    244:                        outmode = OUTMODE_LST;
                    245:                        break;
                    246:                }
                    247:        }
                    248:
                    249:        /* Parse arguments. */
                    250:
                    251:        argc -= optind;
                    252:        argv += optind;
                    253:        resp = NULL;
                    254:
                    255:        /* Quirk for a man(1) section argument without -s. */
                    256:
                    257:        if (search.argmode == ARG_NAME &&
                    258:            argv[0] != NULL &&
                    259:            isdigit((unsigned char)argv[0][0]) &&
                    260:            (argv[0][1] == '\0' || !strcmp(argv[0], "3p"))) {
                    261:                search.sec = argv[0];
                    262:                argv++;
                    263:                argc--;
                    264:        }
                    265:
                    266:        rc = MANDOCLEVEL_OK;
                    267:
                    268:        /* man(1), whatis(1), apropos(1) */
                    269:
                    270:        if (search.argmode != ARG_FILE) {
                    271:                if (argc == 0)
                    272:                        usage(search.argmode);
                    273:
                    274:                /* Access the mandoc database. */
                    275:
                    276:                manpath_parse(&paths, conf_file, defpaths, auxpaths);
                    277:                mansearch_setup(1);
                    278:                if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
                    279:                        usage(search.argmode);
                    280:                resp = res;
                    281:
                    282:                if (sz == 0) {
                    283:                        if (search.argmode == ARG_NAME)
                    284:                                fprintf(stderr, "%s: No entry for %s "
                    285:                                    "in the manual.\n", progname, argv[0]);
                    286:                        rc = MANDOCLEVEL_BADARG;
                    287:                        goto out;
                    288:                }
                    289:
                    290:                /*
                    291:                 * For standard man(1) and -a output mode,
                    292:                 * prepare for copying filename pointers
                    293:                 * into the program parameter array.
                    294:                 */
                    295:
                    296:                if (outmode == OUTMODE_ONE) {
                    297:                        argc = 1;
                    298:                        best_prio = 10;
                    299:                } else if (outmode == OUTMODE_ALL)
                    300:                        argc = (int)sz;
                    301:
                    302:                /* Iterate all matching manuals. */
                    303:
                    304:                for (i = 0; i < sz; i++) {
                    305:                        if (outmode == OUTMODE_FLN)
                    306:                                puts(res[i].file);
                    307:                        else if (outmode == OUTMODE_LST)
                    308:                                printf("%s - %s\n", res[i].names,
                    309:                                    res[i].output == NULL ? "" :
                    310:                                    res[i].output);
                    311:                        else if (outmode == OUTMODE_ONE) {
                    312:                                /* Search for the best section. */
                    313:                                isec = strcspn(res[i].file, "123456789");
                    314:                                sec = res[i].file[isec];
                    315:                                if ('\0' == sec)
                    316:                                        continue;
                    317:                                prio = sec_prios[sec - '1'];
                    318:                                if (prio >= best_prio)
                    319:                                        continue;
                    320:                                best_prio = prio;
                    321:                                resp = res + i;
                    322:                        }
1.1       kristaps  323:                }
                    324:
1.95      schwarze  325:                /*
                    326:                 * For man(1), -a and -i output mode, fall through
                    327:                 * to the main mandoc(1) code iterating files
                    328:                 * and running the parsers on each of them.
                    329:                 */
                    330:
                    331:                if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
                    332:                        goto out;
                    333:        }
                    334:
                    335:        /* mandoc(1) */
                    336:
                    337:        if ( ! moptions(&options, auxpaths))
                    338:                return((int)MANDOCLEVEL_BADARG);
                    339:
                    340:        if (use_pager && isatty(STDOUT_FILENO))
                    341:                spawn_pager();
                    342:
1.87      schwarze  343:        curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
1.76      schwarze  344:
1.80      schwarze  345:        /*
                    346:         * Conditionally start up the lookaside buffer before parsing.
                    347:         */
                    348:        if (OUTT_MAN == curp.outtype)
                    349:                mparse_keep(curp.mp);
                    350:
1.95      schwarze  351:        if (argc == 0)
1.76      schwarze  352:                parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.27      schwarze  353:
1.95      schwarze  354:        while (argc) {
                    355:                if (resp != NULL) {
1.97    ! schwarze  356:                        if (resp->form) {
        !           357:                                /* For .so only; ignore failure. */
        !           358:                                chdir(paths.paths[resp->ipath]);
1.95      schwarze  359:                                parse(&curp, -1, resp->file, &rc);
1.97    ! schwarze  360:                        } else
1.95      schwarze  361:                                rc = passthrough(resp->file);
                    362:                        resp++;
                    363:                } else
                    364:                        parse(&curp, -1, *argv++, &rc);
1.76      schwarze  365:                if (MANDOCLEVEL_OK != rc && curp.wstop)
1.27      schwarze  366:                        break;
1.95      schwarze  367:                argc--;
1.1       kristaps  368:        }
                    369:
                    370:        if (curp.outfree)
                    371:                (*curp.outfree)(curp.outdata);
1.76      schwarze  372:        if (curp.mp)
                    373:                mparse_free(curp.mp);
1.95      schwarze  374:
                    375: out:
                    376:        if (search.argmode != ARG_FILE) {
1.97    ! schwarze  377:                manpath_free(&paths);
1.95      schwarze  378:                mansearch_free(res, sz);
                    379:                mansearch_setup(0);
                    380:        }
                    381:
1.83      schwarze  382:        free(defos);
1.1       kristaps  383:
1.76      schwarze  384:        return((int)rc);
1.1       kristaps  385: }
                    386:
1.20      schwarze  387: static void
1.3       schwarze  388: version(void)
                    389: {
                    390:
1.95      schwarze  391:        printf("mandoc %s\n", VERSION);
1.47      schwarze  392:        exit((int)MANDOCLEVEL_OK);
1.3       schwarze  393: }
                    394:
1.20      schwarze  395: static void
1.95      schwarze  396: usage(enum argmode argmode)
1.1       kristaps  397: {
                    398:
1.95      schwarze  399:        switch (argmode) {
                    400:        case ARG_FILE:
1.96      schwarze  401:                fputs("usage: mandoc [-acfklV] [-Ios=name] "
                    402:                    "[-mformat] [-Ooption] [-Toutput] [-Wlevel]\n"
1.95      schwarze  403:                    "\t      [file ...]\n", stderr);
                    404:                break;
                    405:        case ARG_NAME:
1.96      schwarze  406:                fputs("usage: man [-acfhklVw] [-C file] "
1.95      schwarze  407:                    "[-M path] [-m path] [-S arch] [-s section]\n"
                    408:                    "\t   [section] name ...\n", stderr);
                    409:                break;
                    410:        case ARG_WORD:
1.96      schwarze  411:                fputs("usage: whatis [-acfklVw] [-C file] "
                    412:                    "[-M path] [-m path] [-O outkey] [-S arch]\n"
                    413:                    "\t      [-s section] name ...\n", stderr);
1.95      schwarze  414:                break;
                    415:        case ARG_EXPR:
1.96      schwarze  416:                fputs("usage: apropos [-acfklVw] [-C file] "
                    417:                    "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.95      schwarze  418:                    "\t       [-s section] expression ...\n", stderr);
                    419:                break;
                    420:        }
1.47      schwarze  421:        exit((int)MANDOCLEVEL_BADARG);
1.1       kristaps  422: }
                    423:
1.27      schwarze  424: static void
1.90      schwarze  425: parse(struct curparse *curp, int fd, const char *file,
                    426:        enum mandoclevel *level)
1.52      schwarze  427: {
1.76      schwarze  428:        enum mandoclevel  rc;
                    429:        struct mdoc      *mdoc;
                    430:        struct man       *man;
1.52      schwarze  431:
1.76      schwarze  432:        /* Begin by parsing the file itself. */
1.52      schwarze  433:
1.76      schwarze  434:        assert(file);
                    435:        assert(fd >= -1);
1.52      schwarze  436:
1.76      schwarze  437:        rc = mparse_readfd(curp->mp, fd, file);
1.1       kristaps  438:
1.76      schwarze  439:        /* Stop immediately if the parse has failed. */
1.1       kristaps  440:
1.76      schwarze  441:        if (MANDOCLEVEL_FATAL <= rc)
1.51      schwarze  442:                goto cleanup;
                    443:
                    444:        /*
1.76      schwarze  445:         * With -Wstop and warnings or errors of at least the requested
                    446:         * level, do not produce output.
1.51      schwarze  447:         */
                    448:
1.76      schwarze  449:        if (MANDOCLEVEL_OK != rc && curp->wstop)
1.51      schwarze  450:                goto cleanup;
                    451:
                    452:        /* If unset, allocate output dev now (if applicable). */
                    453:
                    454:        if ( ! (curp->outman && curp->outmdoc)) {
                    455:                switch (curp->outtype) {
1.90      schwarze  456:                case OUTT_XHTML:
1.51      schwarze  457:                        curp->outdata = xhtml_alloc(curp->outopts);
1.77      schwarze  458:                        curp->outfree = html_free;
1.51      schwarze  459:                        break;
1.90      schwarze  460:                case OUTT_HTML:
1.51      schwarze  461:                        curp->outdata = html_alloc(curp->outopts);
1.77      schwarze  462:                        curp->outfree = html_free;
                    463:                        break;
1.90      schwarze  464:                case OUTT_UTF8:
1.77      schwarze  465:                        curp->outdata = utf8_alloc(curp->outopts);
                    466:                        curp->outfree = ascii_free;
                    467:                        break;
1.90      schwarze  468:                case OUTT_LOCALE:
1.77      schwarze  469:                        curp->outdata = locale_alloc(curp->outopts);
                    470:                        curp->outfree = ascii_free;
1.51      schwarze  471:                        break;
1.90      schwarze  472:                case OUTT_ASCII:
1.51      schwarze  473:                        curp->outdata = ascii_alloc(curp->outopts);
                    474:                        curp->outfree = ascii_free;
                    475:                        break;
1.90      schwarze  476:                case OUTT_PDF:
1.51      schwarze  477:                        curp->outdata = pdf_alloc(curp->outopts);
                    478:                        curp->outfree = pspdf_free;
                    479:                        break;
1.90      schwarze  480:                case OUTT_PS:
1.51      schwarze  481:                        curp->outdata = ps_alloc(curp->outopts);
                    482:                        curp->outfree = pspdf_free;
                    483:                        break;
                    484:                default:
                    485:                        break;
                    486:                }
                    487:
                    488:                switch (curp->outtype) {
1.90      schwarze  489:                case OUTT_HTML:
1.51      schwarze  490:                        /* FALLTHROUGH */
1.90      schwarze  491:                case OUTT_XHTML:
1.51      schwarze  492:                        curp->outman = html_man;
                    493:                        curp->outmdoc = html_mdoc;
                    494:                        break;
1.90      schwarze  495:                case OUTT_TREE:
1.51      schwarze  496:                        curp->outman = tree_man;
                    497:                        curp->outmdoc = tree_mdoc;
                    498:                        break;
1.90      schwarze  499:                case OUTT_MAN:
1.78      schwarze  500:                        curp->outmdoc = man_mdoc;
1.80      schwarze  501:                        curp->outman = man_man;
1.78      schwarze  502:                        break;
1.90      schwarze  503:                case OUTT_PDF:
1.51      schwarze  504:                        /* FALLTHROUGH */
1.90      schwarze  505:                case OUTT_ASCII:
1.51      schwarze  506:                        /* FALLTHROUGH */
1.90      schwarze  507:                case OUTT_UTF8:
1.77      schwarze  508:                        /* FALLTHROUGH */
1.90      schwarze  509:                case OUTT_LOCALE:
1.77      schwarze  510:                        /* FALLTHROUGH */
1.90      schwarze  511:                case OUTT_PS:
1.51      schwarze  512:                        curp->outman = terminal_man;
                    513:                        curp->outmdoc = terminal_mdoc;
                    514:                        break;
                    515:                default:
                    516:                        break;
                    517:                }
                    518:        }
                    519:
1.88      schwarze  520:        mparse_result(curp->mp, &mdoc, &man, NULL);
1.76      schwarze  521:
1.51      schwarze  522:        /* Execute the out device, if it exists. */
                    523:
1.76      schwarze  524:        if (man && curp->outman)
                    525:                (*curp->outman)(curp->outdata, man);
                    526:        if (mdoc && curp->outmdoc)
                    527:                (*curp->outmdoc)(curp->outdata, mdoc);
1.51      schwarze  528:
                    529:  cleanup:
1.59      schwarze  530:
1.76      schwarze  531:        mparse_reset(curp->mp);
1.59      schwarze  532:
1.76      schwarze  533:        if (*level < rc)
                    534:                *level = rc;
1.1       kristaps  535: }
                    536:
1.95      schwarze  537: static enum mandoclevel
                    538: passthrough(const char *file)
                    539: {
                    540:        char             buf[BUFSIZ];
                    541:        const char      *syscall;
                    542:        ssize_t          nr, nw, off;
                    543:        int              fd;
                    544:
                    545:        fd = open(file, O_RDONLY);
                    546:        if (fd == -1) {
                    547:                syscall = "open";
                    548:                goto fail;
                    549:        }
                    550:
                    551:        while ((nr = read(fd, buf, BUFSIZ)) != -1 && nr != 0)
                    552:                for (off = 0; off < nr; off += nw)
                    553:                        if ((nw = write(STDOUT_FILENO, buf + off,
                    554:                            (size_t)(nr - off))) == -1 || nw == 0) {
                    555:                                syscall = "write";
                    556:                                goto fail;
                    557:                        }
                    558:
                    559:         if (nr == 0) {
                    560:                close(fd);
                    561:                return(MANDOCLEVEL_OK);
                    562:         }
                    563:
                    564:        syscall = "read";
                    565: fail:
                    566:        fprintf(stderr, "%s: %s: SYSERR: %s: %s",
                    567:            progname, file, syscall, strerror(errno));
                    568:        return(MANDOCLEVEL_SYSERR);
                    569: }
                    570:
1.1       kristaps  571: static int
1.87      schwarze  572: moptions(int *options, char *arg)
1.1       kristaps  573: {
                    574:
1.95      schwarze  575:        if (arg == NULL)
                    576:                /* nothing to do */;
                    577:        else if (0 == strcmp(arg, "doc"))
1.87      schwarze  578:                *options |= MPARSE_MDOC;
1.1       kristaps  579:        else if (0 == strcmp(arg, "andoc"))
1.87      schwarze  580:                /* nothing to do */;
1.1       kristaps  581:        else if (0 == strcmp(arg, "an"))
1.87      schwarze  582:                *options |= MPARSE_MAN;
1.1       kristaps  583:        else {
1.93      schwarze  584:                fprintf(stderr, "%s: -m%s: Bad argument\n",
                    585:                    progname, arg);
1.1       kristaps  586:                return(0);
                    587:        }
                    588:
                    589:        return(1);
                    590: }
                    591:
                    592: static int
1.22      schwarze  593: toptions(struct curparse *curp, char *arg)
1.1       kristaps  594: {
                    595:
                    596:        if (0 == strcmp(arg, "ascii"))
1.22      schwarze  597:                curp->outtype = OUTT_ASCII;
                    598:        else if (0 == strcmp(arg, "lint")) {
                    599:                curp->outtype = OUTT_LINT;
1.45      schwarze  600:                curp->wlevel  = MANDOCLEVEL_WARNING;
1.76      schwarze  601:        } else if (0 == strcmp(arg, "tree"))
1.22      schwarze  602:                curp->outtype = OUTT_TREE;
1.78      schwarze  603:        else if (0 == strcmp(arg, "man"))
                    604:                curp->outtype = OUTT_MAN;
1.17      schwarze  605:        else if (0 == strcmp(arg, "html"))
1.22      schwarze  606:                curp->outtype = OUTT_HTML;
1.77      schwarze  607:        else if (0 == strcmp(arg, "utf8"))
                    608:                curp->outtype = OUTT_UTF8;
                    609:        else if (0 == strcmp(arg, "locale"))
                    610:                curp->outtype = OUTT_LOCALE;
1.21      schwarze  611:        else if (0 == strcmp(arg, "xhtml"))
1.22      schwarze  612:                curp->outtype = OUTT_XHTML;
1.36      schwarze  613:        else if (0 == strcmp(arg, "ps"))
                    614:                curp->outtype = OUTT_PS;
1.43      schwarze  615:        else if (0 == strcmp(arg, "pdf"))
                    616:                curp->outtype = OUTT_PDF;
1.1       kristaps  617:        else {
1.93      schwarze  618:                fprintf(stderr, "%s: -T%s: Bad argument\n",
                    619:                    progname, arg);
1.1       kristaps  620:                return(0);
                    621:        }
                    622:
                    623:        return(1);
                    624: }
                    625:
                    626: static int
1.45      schwarze  627: woptions(struct curparse *curp, char *arg)
1.1       kristaps  628: {
1.10      schwarze  629:        char            *v, *o;
1.90      schwarze  630:        const char      *toks[6];
1.1       kristaps  631:
1.45      schwarze  632:        toks[0] = "stop";
                    633:        toks[1] = "all";
                    634:        toks[2] = "warning";
                    635:        toks[3] = "error";
                    636:        toks[4] = "fatal";
                    637:        toks[5] = NULL;
1.1       kristaps  638:
1.10      schwarze  639:        while (*arg) {
                    640:                o = arg;
1.17      schwarze  641:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.90      schwarze  642:                case 0:
1.45      schwarze  643:                        curp->wstop = 1;
1.1       kristaps  644:                        break;
1.90      schwarze  645:                case 1:
1.45      schwarze  646:                        /* FALLTHROUGH */
1.90      schwarze  647:                case 2:
1.45      schwarze  648:                        curp->wlevel = MANDOCLEVEL_WARNING;
1.1       kristaps  649:                        break;
1.90      schwarze  650:                case 3:
1.45      schwarze  651:                        curp->wlevel = MANDOCLEVEL_ERROR;
1.1       kristaps  652:                        break;
1.90      schwarze  653:                case 4:
1.45      schwarze  654:                        curp->wlevel = MANDOCLEVEL_FATAL;
1.19      schwarze  655:                        break;
1.1       kristaps  656:                default:
1.93      schwarze  657:                        fprintf(stderr, "%s: -W%s: Bad argument\n",
                    658:                            progname, o);
1.1       kristaps  659:                        return(0);
                    660:                }
1.10      schwarze  661:        }
1.1       kristaps  662:
                    663:        return(1);
                    664: }
                    665:
1.75      schwarze  666: static void
1.90      schwarze  667: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.76      schwarze  668:                const char *file, int line, int col, const char *msg)
1.30      schwarze  669: {
1.94      schwarze  670:        const char      *mparse_msg;
1.45      schwarze  671:
1.92      schwarze  672:        fprintf(stderr, "%s: %s:", progname, file);
                    673:
                    674:        if (line)
                    675:                fprintf(stderr, "%d:%d:", line, col + 1);
                    676:
1.94      schwarze  677:        fprintf(stderr, " %s", mparse_strlevel(lvl));
                    678:
                    679:        if (NULL != (mparse_msg = mparse_strerror(t)))
                    680:                fprintf(stderr, ": %s", mparse_msg);
1.30      schwarze  681:
                    682:        if (msg)
                    683:                fprintf(stderr, ": %s", msg);
1.76      schwarze  684:
1.30      schwarze  685:        fputc('\n', stderr);
1.95      schwarze  686: }
                    687:
                    688: static void
                    689: spawn_pager(void)
                    690: {
                    691: #define MAX_PAGER_ARGS 16
                    692:        char            *argv[MAX_PAGER_ARGS];
                    693:        const char      *pager;
                    694:        char            *cp;
                    695:        int              fildes[2];
                    696:        int              argc;
                    697:
                    698:        if (pipe(fildes) == -1) {
                    699:                fprintf(stderr, "%s: pipe: %s\n",
                    700:                    progname, strerror(errno));
                    701:                return;
                    702:        }
                    703:
                    704:        switch (fork()) {
                    705:        case -1:
                    706:                fprintf(stderr, "%s: fork: %s\n",
                    707:                    progname, strerror(errno));
                    708:                exit((int)MANDOCLEVEL_SYSERR);
                    709:        case 0:
                    710:                close(fildes[0]);
                    711:                if (dup2(fildes[1], STDOUT_FILENO) == -1) {
                    712:                        fprintf(stderr, "%s: dup output: %s\n",
                    713:                            progname, strerror(errno));
                    714:                        exit((int)MANDOCLEVEL_SYSERR);
                    715:                }
                    716:                return;
                    717:        default:
                    718:                break;
                    719:        }
                    720:
                    721:        /* The original process becomes the pager. */
                    722:
                    723:        close(fildes[1]);
                    724:        if (dup2(fildes[0], STDIN_FILENO) == -1) {
                    725:                fprintf(stderr, "%s: dup input: %s\n",
                    726:                    progname, strerror(errno));
                    727:                exit((int)MANDOCLEVEL_SYSERR);
                    728:        }
                    729:
                    730:        pager = getenv("MANPAGER");
                    731:        if (pager == NULL || *pager == '\0')
                    732:                pager = getenv("PAGER");
                    733:        if (pager == NULL || *pager == '\0')
                    734:                pager = "/usr/bin/more -s";
                    735:        cp = mandoc_strdup(pager);
                    736:
                    737:        /*
                    738:         * Parse the pager command into words.
                    739:         * Intentionally do not do anything fancy here.
                    740:         */
                    741:
                    742:        argc = 0;
                    743:        while (argc + 1 < MAX_PAGER_ARGS) {
                    744:                argv[argc++] = cp;
                    745:                cp = strchr(cp, ' ');
                    746:                if (cp == NULL)
                    747:                        break;
                    748:                *cp++ = '\0';
                    749:                while (*cp == ' ')
                    750:                        cp++;
                    751:                if (*cp == '\0')
                    752:                        break;
                    753:        }
                    754:        argv[argc] = NULL;
                    755:
                    756:        /* Hand over to the pager. */
                    757:
                    758:        execvp(argv[0], argv);
                    759:        fprintf(stderr, "%s: exec: %s\n",
                    760:            progname, strerror(errno));
                    761:        exit((int)MANDOCLEVEL_SYSERR);
1.30      schwarze  762: }