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

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