Annotation of src/usr.bin/whatis/whatis.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: whatis.c,v 1.7 2002/02/16 21:27:59 millert 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: #ifndef lint
33: static char copyright[] =
34: "@(#) Copyright (c) 1987, 1993\n\
35: The Regents of the University of California. All rights reserved.\n";
36: #endif /* not lint */
37:
38: #ifndef lint
39: static char sccsid[] = "@(#)whatis.c 8.5 (Berkeley) 11/26/93";
40: #endif /* not lint */
41:
42: #include <sys/param.h>
43: #include <sys/queue.h>
44:
45: #include <ctype.h>
46: #include <err.h>
47: #include <stdio.h>
48: #include <stdlib.h>
49: #include <string.h>
50:
51: #include "../man/config.h"
52: #include "../man/pathnames.h"
53:
1.4 deraadt 54: #define MAXLINELEN 8192 /* max line handled */
1.1 deraadt 55:
56: static int *found, foundman;
1.5 millert 57: extern char *__progname;
1.1 deraadt 58:
1.7 millert 59: void dashtrunc(char *, char *);
60: int match(char *, char *);
61: void usage(void);
62: void whatis(char **, char *, int);
1.1 deraadt 63:
64: int
65: main(argc, argv)
66: int argc;
67: char *argv[];
68: {
69: ENTRY *ep;
70: TAG *tp;
71: int ch, rv;
72: char *beg, *conffile, **p, *p_augment, *p_path;
73:
74: conffile = NULL;
75: p_augment = p_path = NULL;
1.3 millert 76: while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
1.1 deraadt 77: switch (ch) {
78: case 'C':
79: conffile = optarg;
80: break;
81: case 'M':
82: case 'P': /* backward compatible */
83: p_path = optarg;
84: break;
85: case 'm':
86: p_augment = optarg;
87: break;
88: case '?':
89: default:
90: usage();
91: }
92: argv += optind;
93: argc -= optind;
94:
95: if (argc < 1)
96: usage();
97:
98: if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
99: err(1, NULL);
100: memset(found, 0, argc * sizeof(int));
101:
102: for (p = argv; *p; ++p) /* trim full paths */
1.5 millert 103: if ((beg = strrchr(*p, '/')))
1.1 deraadt 104: *p = beg + 1;
105:
106: if (p_augment)
107: whatis(argv, p_augment, 1);
108: if (p_path || (p_path = getenv("MANPATH")))
109: whatis(argv, p_path, 1);
110: else {
111: config(conffile);
112: ep = (tp = getlist("_whatdb")) == NULL ?
113: NULL : tp->list.tqh_first;
114: for (; ep != NULL; ep = ep->q.tqe_next)
115: whatis(argv, ep->s, 0);
116: }
117:
118: if (!foundman) {
119: fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
120: exit(1);
121: }
122: rv = 1;
123: for (p = argv; *p; ++p)
124: if (found[p - argv])
125: rv = 0;
126: else
127: printf("%s: not found\n", *p);
128: exit(rv);
129: }
130:
131: void
132: whatis(argv, path, buildpath)
133: char **argv, *path;
134: int buildpath;
135: {
1.6 mpech 136: char *end, *name, **p;
1.1 deraadt 137: char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
1.4 deraadt 138: char hold[MAXPATHLEN];
1.1 deraadt 139:
140: for (name = path; name; name = end) { /* through name list */
1.5 millert 141: if ((end = strchr(name, ':')))
1.1 deraadt 142: *end++ = '\0';
143:
144: if (buildpath) {
1.4 deraadt 145: (void)snprintf(hold, sizeof hold, "%s/%s",
146: name, _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: dashtrunc(buf, wbuf);
158: for (p = argv; *p; ++p)
159: if (match(wbuf, *p)) {
160: printf("%s", buf);
161: found[p - argv] = 1;
162:
163: /* only print line once */
164: while (*++p)
165: if (match(wbuf, *p))
166: found[p - argv] = 1;
167: break;
168: }
169: }
170: }
171: }
172:
173: /*
174: * match --
1.5 millert 175: * match a full word or a full string
1.1 deraadt 176: */
177: int
178: match(bp, str)
1.6 mpech 179: char *bp, *str;
1.1 deraadt 180: {
1.6 mpech 181: int len;
182: char *start;
1.1 deraadt 183:
184: if (!*str || !*bp)
185: return(0);
186: for (len = strlen(str);;) {
1.5 millert 187: /* skip leading crud */
188: for (; *bp && !isalnum(*bp); ++bp)
189: ;
1.1 deraadt 190: if (!*bp)
191: break;
1.5 millert 192:
193: /* check for word match first */
194: for (start = bp++; *bp && (*bp == '_' || isalnum(*bp)); ++bp)
195: ;
196: if (bp - start == len) {
197: if (strncasecmp(start, str, len) == 0)
198: return(1);
199: } else if (*bp && *bp != ',') {
200: /* check for full string match */
201: for (bp = start;
202: *bp && *bp != ',' && *bp != '(' && !isspace(*bp); ++bp)
203: ;
204: if (bp - start == len && strncasecmp(start, str, len) == 0)
205: return(1);
206: }
1.1 deraadt 207: }
208: return(0);
209: }
210:
211: /*
212: * dashtrunc --
213: * truncate a string at " - "
214: */
215: void
216: dashtrunc(from, to)
1.6 mpech 217: char *from, *to;
1.1 deraadt 218: {
1.6 mpech 219: int ch;
1.1 deraadt 220:
221: for (; (ch = *from) && ch != '\n' &&
222: (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
223: *to++ = ch;
224: *to = '\0';
225: }
226:
227: /*
228: * usage --
229: * print usage message and die
230: */
231: void
232: usage()
233: {
1.5 millert 234:
1.1 deraadt 235: (void)fprintf(stderr,
1.5 millert 236: "usage: %s [-C file] [-M path] [-m path] command ...\n",
237: __progname);
1.1 deraadt 238: exit(1);
239: }