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

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