[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.108

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