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

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