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

1.16    ! deraadt     1: /*      $OpenBSD: apropos.c,v 1.15 2009/10/27 23:59:35 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.
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: #include <sys/param.h>
                     34: #include <sys/queue.h>
                     35:
                     36: #include <ctype.h>
                     37: #include <err.h>
                     38: #include <limits.h>
                     39: #include <stdio.h>
                     40: #include <stdlib.h>
                     41: #include <string.h>
                     42: #include <unistd.h>
                     43:
                     44: #include "../man/config.h"
                     45: #include "../man/pathnames.h"
                     46:
                     47: static int *found, foundman;
                     48:
1.6       deraadt    49: #define        MAXLINELEN      8192            /* max line handled */
                     50:
1.14      jmc        51: void apropos(char **, char *, int, char *, char *);
1.8       millert    52: void lowstr(char *, char *);
                     53: int match(char *, char *);
                     54: void usage(void);
1.1       deraadt    55:
                     56: int
1.10      deraadt    57: main(int argc, char *argv[])
1.1       deraadt    58: {
                     59:        ENTRY *ep;
                     60:        TAG *tp;
                     61:        int ch, rv;
1.14      jmc        62:        char *conffile, *machine, **p, *p_augment, *p_path, *sflag;
1.1       deraadt    63:
                     64:        conffile = NULL;
                     65:        p_augment = p_path = NULL;
1.14      jmc        66:        machine = sflag = NULL;
                     67:        while ((ch = getopt(argc, argv, "C:M:m:P:S:s:")) != -1)
1.1       deraadt    68:                switch (ch) {
                     69:                case 'C':
                     70:                        conffile = optarg;
                     71:                        break;
                     72:                case 'M':
                     73:                case 'P':               /* backward compatible */
                     74:                        p_path = optarg;
                     75:                        break;
                     76:                case 'm':
                     77:                        p_augment = optarg;
                     78:                        break;
1.14      jmc        79:                case 'S':
                     80:                        machine = optarg;
                     81:                        lowstr(machine, machine);
                     82:                        break;
                     83:                case 's':
                     84:                        sflag = optarg;
                     85:                        lowstr(sflag, sflag);
                     86:                        break;
1.1       deraadt    87:                case '?':
                     88:                default:
                     89:                        usage();
                     90:                }
                     91:        argv += optind;
                     92:        argc -= optind;
                     93:
                     94:        if (argc < 1)
                     95:                usage();
                     96:
1.12      djm        97:        if ((found = calloc(argc, sizeof(int))) == NULL)
1.1       deraadt    98:                err(1, NULL);
                     99:
                    100:        for (p = argv; *p; ++p)                 /* convert to lower-case */
                    101:                lowstr(*p, *p);
                    102:
                    103:        if (p_augment)
1.14      jmc       104:                apropos(argv, p_augment, 1, sflag, machine);
1.1       deraadt   105:        if (p_path || (p_path = getenv("MANPATH")))
1.14      jmc       106:                apropos(argv, p_path, 1, sflag, machine);
1.1       deraadt   107:        else {
                    108:                config(conffile);
                    109:                ep = (tp = getlist("_whatdb")) == NULL ?
1.11      otto      110:                    NULL : TAILQ_FIRST(&tp->list);
                    111:                for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.14      jmc       112:                        apropos(argv, ep->s, 0, sflag, machine);
1.1       deraadt   113:        }
                    114:
                    115:        if (!foundman)
                    116:                errx(1, "no %s file found", _PATH_WHATIS);
                    117:
                    118:        rv = 1;
                    119:        for (p = argv; *p; ++p)
                    120:                if (found[p - argv])
                    121:                        rv = 0;
                    122:                else
                    123:                        (void)printf("%s: nothing appropriate\n", *p);
                    124:        exit(rv);
                    125: }
                    126:
                    127: void
1.14      jmc       128: apropos(char **argv, char *path, int buildpath, char *sflag, char *machine)
1.1       deraadt   129: {
                    130:        char *end, *name, **p;
1.6       deraadt   131:        char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
                    132:        char hold[MAXPATHLEN];
1.14      jmc       133:        size_t slen = 0, mlen = 0;
                    134:
                    135:        if (sflag)
                    136:                slen = strlen(sflag);
                    137:        if (machine)
                    138:                mlen = strlen(machine);
1.1       deraadt   139:
                    140:        for (name = path; name; name = end) {   /* through name list */
1.5       deraadt   141:                if ((end = strchr(name, ':')))
1.1       deraadt   142:                        *end++ = '\0';
                    143:
                    144:                if (buildpath) {
1.3       downsj    145:                        (void)snprintf(hold, sizeof(hold), "%s/%s", name,
                    146:                            _PATH_WHATIS);
1.1       deraadt   147:                        name = hold;
                    148:                }
                    149:
                    150:                if (!freopen(name, "r", stdin))
                    151:                        continue;
                    152:
                    153:                foundman = 1;
                    154:
                    155:                /* for each file found */
                    156:                while (fgets(buf, sizeof(buf), stdin)) {
                    157:                        if (!strchr(buf, '\n')) {
                    158:                                warnx("%s: line too long", name);
                    159:                                continue;
                    160:                        }
                    161:                        lowstr(buf, wbuf);
1.14      jmc       162:                        if (sflag || machine) {
                    163:                                char *s = strstr(wbuf, ") - ");
                    164:                                if (!s)
                    165:                                        continue;
                    166:                                while (s > wbuf && *s != '/' && *s != '(')
                    167:                                        s--;
                    168:                                if (machine && *s == '/' &&
                    169:                                    strncmp(s+1, machine, mlen))
                    170:                                        continue;
                    171:                                while (s > wbuf && *s != '(')
                    172:                                        s--;
                    173:                                if (sflag && *s == '(' &&
                    174:                                    strncmp(s+1, sflag, slen))
                    175:                                        continue;
                    176:                        }
1.1       deraadt   177:                        for (p = argv; *p; ++p)
                    178:                                if (match(wbuf, *p)) {
                    179:                                        (void)printf("%s", buf);
                    180:                                        found[p - argv] = 1;
                    181:
                    182:                                        /* only print line once */
                    183:                                        while (*++p)
                    184:                                                if (match(wbuf, *p))
                    185:                                                        found[p - argv] = 1;
                    186:                                        break;
                    187:                                }
                    188:                }
                    189:        }
                    190: }
                    191:
                    192: /*
                    193:  * match --
                    194:  *     match anywhere the string appears
                    195:  */
                    196: int
1.10      deraadt   197: match(char *bp, char *str)
1.1       deraadt   198: {
                    199:        int len;
                    200:        char test;
                    201:
                    202:        if (!*bp)
                    203:                return (0);
                    204:        /* backward compatible: everything matches empty string */
                    205:        if (!*str)
                    206:                return (1);
                    207:        for (test = *str++, len = strlen(str); *bp;)
                    208:                if (test == *bp++ && !strncmp(bp, str, len))
                    209:                        return (1);
                    210:        return (0);
                    211: }
                    212:
                    213: /*
                    214:  * lowstr --
                    215:  *     convert a string to lower case
                    216:  */
                    217: void
1.10      deraadt   218: lowstr(char *from, char *to)
1.1       deraadt   219: {
1.16    ! deraadt   220:        unsigned char ch;
1.1       deraadt   221:
                    222:        while ((ch = *from++) && ch != '\n')
                    223:                *to++ = isupper(ch) ? tolower(ch) : ch;
                    224:        *to = '\0';
                    225: }
                    226:
                    227: /*
                    228:  * usage --
                    229:  *     print usage message and die
                    230:  */
                    231: void
1.10      deraadt   232: usage(void)
1.1       deraadt   233: {
                    234:
                    235:        (void)fprintf(stderr,
1.14      jmc       236:            "usage: apropos [-C file] [-M path] [-m path] "
                    237:            "[-S subsection] [-s section]\n"
                    238:            "       keyword ...\n");
1.1       deraadt   239:        exit(1);
                    240: }