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

Annotation of src/usr.bin/whatis/whatis.c, Revision 1.13

1.13    ! deraadt     1: /*     $OpenBSD: whatis.c,v 1.12 2007/03/20 03:56:13 tedu Exp $        */
1.2       deraadt     2:
1.1       deraadt     3: /*
                      4:  * Copyright (c) 1987, 1993
                      5:  *     The Regents of the University of California.  All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
1.8       millert    15:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    16:  *    may be used to endorse or promote products derived from this software
                     17:  *    without specific prior written permission.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     20:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     21:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     22:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     23:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     24:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     25:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     26:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     27:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     28:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     29:  * SUCH DAMAGE.
                     30:  */
                     31:
                     32: #include <sys/param.h>
                     33: #include <sys/queue.h>
                     34:
                     35: #include <ctype.h>
                     36: #include <err.h>
                     37: #include <stdio.h>
                     38: #include <stdlib.h>
                     39: #include <string.h>
                     40:
                     41: #include "../man/config.h"
                     42: #include "../man/pathnames.h"
                     43:
1.4       deraadt    44: #define        MAXLINELEN      8192                    /* max line handled */
1.1       deraadt    45:
                     46: static int *found, foundman;
1.5       millert    47: extern char *__progname;
1.1       deraadt    48:
1.7       millert    49: void dashtrunc(char *, char *);
                     50: int match(char *, char *);
                     51: void usage(void);
                     52: void whatis(char **, char *, int);
1.1       deraadt    53:
                     54: int
1.9       deraadt    55: main(int argc, char *argv[])
1.1       deraadt    56: {
                     57:        ENTRY *ep;
                     58:        TAG *tp;
                     59:        int ch, rv;
                     60:        char *beg, *conffile, **p, *p_augment, *p_path;
                     61:
                     62:        conffile = NULL;
                     63:        p_augment = p_path = NULL;
1.3       millert    64:        while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
1.1       deraadt    65:                switch (ch) {
                     66:                case 'C':
                     67:                        conffile = optarg;
                     68:                        break;
                     69:                case 'M':
                     70:                case 'P':               /* backward compatible */
                     71:                        p_path = optarg;
                     72:                        break;
                     73:                case 'm':
                     74:                        p_augment = optarg;
                     75:                        break;
                     76:                case '?':
                     77:                default:
                     78:                        usage();
                     79:                }
                     80:        argv += optind;
                     81:        argc -= optind;
                     82:
                     83:        if (argc < 1)
                     84:                usage();
                     85:
1.11      djm        86:        if ((found = calloc(argc, sizeof(int))) == NULL)
1.1       deraadt    87:                err(1, NULL);
                     88:
                     89:        for (p = argv; *p; ++p)                 /* trim full paths */
1.5       millert    90:                if ((beg = strrchr(*p, '/')))
1.1       deraadt    91:                        *p = beg + 1;
                     92:
                     93:        if (p_augment)
                     94:                whatis(argv, p_augment, 1);
                     95:        if (p_path || (p_path = getenv("MANPATH")))
                     96:                whatis(argv, p_path, 1);
                     97:        else {
                     98:                config(conffile);
                     99:                ep = (tp = getlist("_whatdb")) == NULL ?
1.10      otto      100:                   NULL : TAILQ_FIRST(&tp->list);
                    101:                for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1       deraadt   102:                        whatis(argv, ep->s, 0);
                    103:        }
                    104:
                    105:        if (!foundman) {
                    106:                fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
                    107:                exit(1);
                    108:        }
                    109:        rv = 1;
                    110:        for (p = argv; *p; ++p)
                    111:                if (found[p - argv])
                    112:                        rv = 0;
                    113:                else
                    114:                        printf("%s: not found\n", *p);
                    115:        exit(rv);
                    116: }
                    117:
                    118: void
1.9       deraadt   119: whatis(char **argv, char *path, int buildpath)
1.1       deraadt   120: {
1.6       mpech     121:        char *end, *name, **p;
1.1       deraadt   122:        char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
1.4       deraadt   123:        char hold[MAXPATHLEN];
1.1       deraadt   124:
                    125:        for (name = path; name; name = end) {   /* through name list */
1.5       millert   126:                if ((end = strchr(name, ':')))
1.1       deraadt   127:                        *end++ = '\0';
                    128:
                    129:                if (buildpath) {
1.4       deraadt   130:                        (void)snprintf(hold, sizeof hold, "%s/%s",
                    131:                            name, _PATH_WHATIS);
1.1       deraadt   132:                        name = hold;
                    133:                }
                    134:
                    135:                if (!freopen(name, "r", stdin))
                    136:                        continue;
                    137:
                    138:                foundman = 1;
                    139:
                    140:                /* for each file found */
                    141:                while (fgets(buf, sizeof(buf), stdin)) {
                    142:                        dashtrunc(buf, wbuf);
                    143:                        for (p = argv; *p; ++p)
                    144:                                if (match(wbuf, *p)) {
                    145:                                        printf("%s", buf);
                    146:                                        found[p - argv] = 1;
                    147:
                    148:                                        /* only print line once */
                    149:                                        while (*++p)
                    150:                                                if (match(wbuf, *p))
                    151:                                                        found[p - argv] = 1;
                    152:                                        break;
                    153:                                }
                    154:                }
                    155:        }
                    156: }
                    157:
                    158: /*
                    159:  * match --
1.5       millert   160:  *     match a full word or a full string
1.1       deraadt   161:  */
                    162: int
1.9       deraadt   163: match(char *bp, char *str)
1.1       deraadt   164: {
1.6       mpech     165:        int len;
                    166:        char *start;
1.1       deraadt   167:
                    168:        if (!*str || !*bp)
                    169:                return(0);
                    170:        for (len = strlen(str);;) {
1.5       millert   171:                /* skip leading crud */
                    172:                for (; *bp && !isalnum(*bp); ++bp)
                    173:                        ;
1.1       deraadt   174:                if (!*bp)
                    175:                        break;
1.5       millert   176:
                    177:                /* check for word match first */
1.12      tedu      178:                for (start = bp++; *bp == '_' || isalnum(*bp); ++bp)
1.5       millert   179:                        ;
                    180:                if (bp - start == len) {
                    181:                    if (strncasecmp(start, str, len) == 0)
                    182:                            return(1);
                    183:                } else if (*bp && *bp != ',') {
                    184:                    /* check for full string match */
                    185:                    for (bp = start;
                    186:                        *bp && *bp != ',' && *bp != '(' && !isspace(*bp); ++bp)
                    187:                            ;
                    188:                    if (bp - start == len && strncasecmp(start, str, len) == 0)
                    189:                            return(1);
                    190:                }
1.1       deraadt   191:        }
                    192:        return(0);
                    193: }
                    194:
                    195: /*
                    196:  * dashtrunc --
                    197:  *     truncate a string at " - "
                    198:  */
                    199: void
1.9       deraadt   200: dashtrunc(char *from, char *to)
1.1       deraadt   201: {
1.6       mpech     202:        int ch;
1.1       deraadt   203:
                    204:        for (; (ch = *from) && ch != '\n' &&
                    205:            (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
                    206:                *to++ = ch;
                    207:        *to = '\0';
                    208: }
                    209:
                    210: /*
                    211:  * usage --
                    212:  *     print usage message and die
                    213:  */
                    214: void
1.9       deraadt   215: usage(void)
1.1       deraadt   216: {
1.5       millert   217:
1.1       deraadt   218:        (void)fprintf(stderr,
1.5       millert   219:            "usage: %s [-C file] [-M path] [-m path] command ...\n",
                    220:            __progname);
1.1       deraadt   221:        exit(1);
                    222: }