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

Annotation of mandoc/manpage.c, Revision 1.1

1.1     ! kristaps    1: /*     $Id: mandocdb.c,v 1.46 2012/03/23 06:52:17 kristaps Exp $ */
        !             2: /*
        !             3:  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
        !             4:  *
        !             5:  * Permission to use, copy, modify, and distribute this software for any
        !             6:  * purpose with or without fee is hereby granted, provided that the above
        !             7:  * copyright notice and this permission notice appear in all copies.
        !             8:  *
        !             9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
        !            10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
        !            11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
        !            12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
        !            13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
        !            14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
        !            15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
        !            16:  */
        !            17: #ifdef HAVE_CONFIG_H
        !            18: #include "config.h"
        !            19: #endif
        !            20: #include <sys/param.h>
        !            21:
        !            22: #include <assert.h>
        !            23: #include <getopt.h>
        !            24: #include <stdio.h>
        !            25: #include <stdlib.h>
        !            26: #include <string.h>
        !            27: #include <unistd.h>
        !            28:
        !            29: #include "manpath.h"
        !            30: #include "mansearch.h"
        !            31:
        !            32: static void     show(const char *, const char *);
        !            33:
        !            34: int
        !            35: main(int argc, char *argv[])
        !            36: {
        !            37:        int              ch, term;
        !            38:        size_t           i, sz, len;
        !            39:        struct manpage  *res;
        !            40:        char            *conf_file, *defpaths, *auxpaths, *cp,
        !            41:                        *arch, *sec;
        !            42:        char             buf[MAXPATHLEN];
        !            43:        const char      *cmd;
        !            44:        struct manpaths  paths;
        !            45:        char            *progname;
        !            46:        extern char     *optarg;
        !            47:        extern int       optind;
        !            48:
        !            49:        term = isatty(STDIN_FILENO) && isatty(STDOUT_FILENO);
        !            50:
        !            51:        progname = strrchr(argv[0], '/');
        !            52:        if (progname == NULL)
        !            53:                progname = argv[0];
        !            54:        else
        !            55:                ++progname;
        !            56:
        !            57:        auxpaths = defpaths = conf_file = arch = sec = NULL;
        !            58:        memset(&paths, 0, sizeof(struct manpaths));
        !            59:
        !            60:        while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
        !            61:                switch (ch) {
        !            62:                case ('C'):
        !            63:                        conf_file = optarg;
        !            64:                        break;
        !            65:                case ('M'):
        !            66:                        defpaths = optarg;
        !            67:                        break;
        !            68:                case ('m'):
        !            69:                        auxpaths = optarg;
        !            70:                        break;
        !            71:                case ('S'):
        !            72:                        arch = optarg;
        !            73:                        break;
        !            74:                case ('s'):
        !            75:                        sec = optarg;
        !            76:                        break;
        !            77:                default:
        !            78:                        goto usage;
        !            79:                }
        !            80:
        !            81:        argc -= optind;
        !            82:        argv += optind;
        !            83:
        !            84:        if (0 == argc)
        !            85:                goto usage;
        !            86:
        !            87:        manpath_parse(&paths, conf_file, defpaths, auxpaths);
        !            88:        ch = mansearch(&paths, arch, sec, argc, argv, &res, &sz);
        !            89:        manpath_free(&paths);
        !            90:
        !            91:        if (0 == ch)
        !            92:                goto usage;
        !            93:
        !            94:        if (0 == sz) {
        !            95:                free(res);
        !            96:                return(EXIT_FAILURE);
        !            97:        } else if (1 == sz && term) {
        !            98:                i = 1;
        !            99:                goto show;
        !           100:        } else if (NULL == res)
        !           101:                return(EXIT_FAILURE);
        !           102:
        !           103:        for (i = 0; i < sz; i++) {
        !           104:                printf("%6zu  %s: %s\n",
        !           105:                        i + 1, res[i].file, res[i].desc);
        !           106:                free(res[i].desc);
        !           107:        }
        !           108:
        !           109:        if (0 == term) {
        !           110:                free(res);
        !           111:                return(EXIT_SUCCESS);
        !           112:        }
        !           113:
        !           114:        i = 1;
        !           115:        printf("Enter a choice [1]: ");
        !           116:        fflush(stdout);
        !           117:
        !           118:        if (NULL != (cp = fgetln(stdin, &len)))
        !           119:                if ('\n' == cp[--len] && len > 0) {
        !           120:                        cp[len] = '\0';
        !           121:                        if ((i = atoi(cp)) < 1 || i > sz)
        !           122:                                i = 0;
        !           123:                }
        !           124:
        !           125:        if (0 == i) {
        !           126:                free(res);
        !           127:                return(EXIT_SUCCESS);
        !           128:        }
        !           129: show:
        !           130:        cmd = res[i - 1].form ? "mandoc" : "cat";
        !           131:        strlcpy(buf, res[i - 1].file, MAXPATHLEN);
        !           132:        free(res);
        !           133:
        !           134:        show(cmd, buf);
        !           135:        /* NOTREACHED */
        !           136: usage:
        !           137:        fprintf(stderr, "usage: %s [-C conf] "
        !           138:                                  "[-M paths] "
        !           139:                                  "[-m paths] "
        !           140:                                  "[-S arch] "
        !           141:                                  "[-s section] "
        !           142:                                  "expr ...\n",
        !           143:                                  progname);
        !           144:        return(EXIT_FAILURE);
        !           145: }
        !           146:
        !           147: static void
        !           148: show(const char *cmd, const char *file)
        !           149: {
        !           150:        int              fds[2];
        !           151:        pid_t            pid;
        !           152:
        !           153:        if (-1 == pipe(fds)) {
        !           154:                perror(NULL);
        !           155:                exit(EXIT_FAILURE);
        !           156:        }
        !           157:
        !           158:        if (-1 == (pid = fork())) {
        !           159:                perror(NULL);
        !           160:                exit(EXIT_FAILURE);
        !           161:        } else if (pid > 0) {
        !           162:                dup2(fds[0], STDIN_FILENO);
        !           163:                close(fds[1]);
        !           164:                cmd = NULL != getenv("MANPAGER") ?
        !           165:                        getenv("MANPAGER") :
        !           166:                        (NULL != getenv("PAGER") ?
        !           167:                         getenv("PAGER") : "more");
        !           168:                execlp(cmd, cmd, (char *)NULL);
        !           169:                perror(cmd);
        !           170:                exit(EXIT_FAILURE);
        !           171:        }
        !           172:
        !           173:        dup2(fds[1], STDOUT_FILENO);
        !           174:        close(fds[0]);
        !           175:        execlp(cmd, cmd, file, (char *)NULL);
        !           176:        perror(cmd);
        !           177:        exit(EXIT_FAILURE);
        !           178: }

CVSweb