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

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