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

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