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

Annotation of mandoc/main.c, Revision 1.201

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

CVSweb