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

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