whatis.c revision 1.4 1 /* $NetBSD: whatis.c,v 1.4 1997/10/17 06:58:52 mikel Exp $ */
2
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 #if 0
44 static char sccsid[] = "@(#)whatis.c 8.5 (Berkeley) 1/2/94";
45 #else
46 static char rcsid[] = "$NetBSD: whatis.c,v 1.4 1997/10/17 06:58:52 mikel Exp $";
47 #endif
48 #endif /* not lint */
49
50 #include <sys/param.h>
51 #include <sys/queue.h>
52
53 #include <ctype.h>
54 #include <err.h>
55 #include <glob.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59
60 #include "../man/config.h"
61 #include "../man/pathnames.h"
62
63 #define MAXLINELEN 256 /* max line handled */
64
65 static int *found, foundman;
66
67 void dashtrunc __P((char *, char *));
68 int match __P((char *, char *));
69 void usage __P((void));
70 void whatis __P((char **, char *, int));
71
72 int
73 main(argc, argv)
74 int argc;
75 char *argv[];
76 {
77 ENTRY *ep;
78 TAG *tp;
79 int ch, rv;
80 char *beg, *conffile, **p, *p_augment, *p_path;
81 glob_t pg;
82
83 conffile = NULL;
84 p_augment = p_path = NULL;
85 while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF)
86 switch (ch) {
87 case 'C':
88 conffile = optarg;
89 break;
90 case 'M':
91 case 'P': /* backward compatible */
92 p_path = optarg;
93 break;
94 case 'm':
95 p_augment = optarg;
96 break;
97 case '?':
98 default:
99 usage();
100 }
101 argv += optind;
102 argc -= optind;
103
104 if (argc < 1)
105 usage();
106
107 if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
108 err(1, NULL);
109 memset(found, 0, argc * sizeof(int));
110
111 for (p = argv; *p; ++p) /* trim full paths */
112 if (beg = strrchr(*p, '/'))
113 *p = beg + 1;
114
115 if (p_augment)
116 whatis(argv, p_augment, 1);
117 if (p_path || (p_path = getenv("MANPATH")))
118 whatis(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 if (glob(ep->s, GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
125 NULL, &pg) != 0)
126 err(1, "glob");
127 for (p = pg.gl_pathv; *p; p++)
128 whatis(argv, *p, 0);
129 globfree(&pg);
130 }
131 }
132
133 if (!foundman) {
134 fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
135 exit(1);
136 }
137 rv = 1;
138 for (p = argv; *p; ++p)
139 if (found[p - argv])
140 rv = 0;
141 else
142 printf("%s: not found\n", *p);
143 exit(rv);
144 }
145
146 void
147 whatis(argv, path, buildpath)
148 char **argv, *path;
149 int buildpath;
150 {
151 register char *end, *name, **p;
152 char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
153
154 for (name = path; name; name = end) { /* through name list */
155 if (end = strchr(name, ':'))
156 *end++ = '\0';
157
158 if (buildpath) {
159 char hold[MAXPATHLEN + 1];
160
161 (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
162 name = hold;
163 }
164
165 if (!freopen(name, "r", stdin))
166 continue;
167
168 foundman = 1;
169
170 /* for each file found */
171 while (fgets(buf, sizeof(buf), stdin)) {
172 dashtrunc(buf, wbuf);
173 for (p = argv; *p; ++p)
174 if (match(wbuf, *p)) {
175 printf("%s", buf);
176 found[p - argv] = 1;
177
178 /* only print line once */
179 while (*++p)
180 if (match(wbuf, *p))
181 found[p - argv] = 1;
182 break;
183 }
184 }
185 }
186 }
187
188 /*
189 * match --
190 * match a full word
191 */
192 int
193 match(bp, str)
194 register char *bp, *str;
195 {
196 register int len;
197 register char *start;
198
199 if (!*str || !*bp)
200 return(0);
201 for (len = strlen(str);;) {
202 for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
203 if (!*bp)
204 break;
205 for (start = bp++;
206 *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
207 if (bp - start == len && !strncasecmp(start, str, len))
208 return(1);
209 }
210 return(0);
211 }
212
213 /*
214 * dashtrunc --
215 * truncate a string at " - "
216 */
217 void
218 dashtrunc(from, to)
219 register char *from, *to;
220 {
221 register int ch;
222
223 for (; (ch = *from) && ch != '\n' &&
224 (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
225 *to++ = ch;
226 *to = '\0';
227 }
228
229 /*
230 * usage --
231 * print usage message and die
232 */
233 void
234 usage()
235 {
236 (void)fprintf(stderr,
237 "usage: whatis [-C file] [-M path] [-m path] command ...\n");
238 exit(1);
239 }
240