[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/main.c, Revision 1.126

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