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

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