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

Annotation of src/usr.bin/apropos/apropos.c, Revision 1.7

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