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

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