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

Annotation of mandoc/main.c, Revision 1.227

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

CVSweb