[BACK]Return to main.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/main.c, Revision 1.186

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

CVSweb