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

Annotation of mandoc/main.c, Revision 1.193

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

CVSweb