apropos.c revision 1.14 1 /* $NetBSD: apropos.c,v 1.14 1998/11/06 22:29:44 christos Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1993, 1994
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 #include <sys/cdefs.h>
37
38 #ifndef lint
39 __COPYRIGHT("@(#) 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
47 __RCSID("$NetBSD: apropos.c,v 1.14 1998/11/06 22:29:44 christos Exp $");
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 <glob.h>
57 #include <limits.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <unistd.h>
62
63 #include "config.h"
64 #include "pathnames.h"
65
66 static int *found, foundman;
67
68 #define MAXLINELEN 8192 /* max line handled */
69
70 int main __P((int, char **));
71 void apropos __P((char **, char *, int));
72 void lowstr __P((char *, char *));
73 int match __P((char *, char *));
74 void usage __P((void));
75
76 int
77 main(argc, argv)
78 int argc;
79 char *argv[];
80 {
81 ENTRY *ep;
82 TAG *tp;
83 int ch, rv;
84 char *conffile, **p, *p_augment, *p_path;
85 glob_t pg;
86
87 conffile = NULL;
88 p_augment = p_path = NULL;
89 while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
90 switch (ch) {
91 case 'C':
92 conffile = optarg;
93 break;
94 case 'M':
95 case 'P': /* backward compatible */
96 p_path = optarg;
97 break;
98 case 'm':
99 p_augment = optarg;
100 break;
101 case '?':
102 default:
103 usage();
104 }
105 argv += optind;
106 argc -= optind;
107
108 if (argc < 1)
109 usage();
110
111 if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
112 err(1, "malloc");
113 memset(found, 0, argc * sizeof(int));
114
115 for (p = argv; *p; ++p) /* convert to lower-case */
116 lowstr(*p, *p);
117
118 if (p_augment)
119 apropos(argv, p_augment, 1);
120 if (p_path || (p_path = getenv("MANPATH")))
121 apropos(argv, p_path, 1);
122 else {
123 config(conffile);
124 ep = (tp = getlist("_whatdb")) == NULL ?
125 NULL : tp->list.tqh_first;
126 for (; ep != NULL; ep = ep->q.tqe_next) {
127 if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL,
128 &pg)) != 0) {
129 if (rv == GLOB_NOMATCH)
130 continue;
131 else
132 err(1, "glob");
133 }
134 if (pg.gl_pathc)
135 for (p = pg.gl_pathv; *p; p++)
136 apropos(argv, *p, 0);
137 globfree(&pg);
138 }
139 }
140
141 if (!foundman)
142 errx(1, "no %s file found", _PATH_WHATIS);
143
144 rv = 1;
145 for (p = argv; *p; ++p)
146 if (found[p - argv])
147 rv = 0;
148 else
149 (void)printf("%s: nothing appropriate\n", *p);
150 exit(rv);
151 }
152
153 void
154 apropos(argv, path, buildpath)
155 char **argv, *path;
156 int buildpath;
157 {
158 char *end, *name, **p;
159 char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
160 char hold[MAXPATHLEN + 1];
161
162 for (name = path; name; name = end) { /* through name list */
163 if ((end = strchr(name, ':')))
164 *end++ = '\0';
165
166 if (buildpath) {
167 (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
168 name = hold;
169 }
170
171 if (!freopen(name, "r", stdin))
172 continue;
173
174 foundman = 1;
175
176 /* for each file found */
177 while (fgets(buf, sizeof(buf), stdin)) {
178 if (!strchr(buf, '\n')) {
179 warnx("%s: line too long", name);
180 continue;
181 }
182 lowstr(buf, wbuf);
183 for (p = argv; *p; ++p)
184 if (match(wbuf, *p)) {
185 (void)printf("%s", buf);
186 found[p - argv] = 1;
187
188 /* only print line once */
189 while (*++p)
190 if (match(wbuf, *p))
191 found[p - argv] = 1;
192 break;
193 }
194 }
195 }
196 }
197
198 /*
199 * match --
200 * match anywhere the string appears
201 */
202 int
203 match(bp, str)
204 char *bp, *str;
205 {
206 int len;
207 char test;
208
209 if (!*bp)
210 return (0);
211 /* backward compatible: everything matches empty string */
212 if (!*str)
213 return (1);
214 for (test = *str++, len = strlen(str); *bp;)
215 if (test == *bp++ && !strncmp(bp, str, len))
216 return (1);
217 return (0);
218 }
219
220 /*
221 * lowstr --
222 * convert a string to lower case
223 */
224 void
225 lowstr(from, to)
226 char *from, *to;
227 {
228 char ch;
229
230 while ((ch = *from++) && ch != '\n')
231 *to++ = isupper((unsigned char)ch) ? tolower(ch) : ch;
232 *to = '\0';
233 }
234
235 /*
236 * usage --
237 * print usage message and die
238 */
239 void
240 usage()
241 {
242
243 extern char *__progname;
244
245 (void)fprintf(stderr,
246 "Usage: %s [-C file] [-M path] [-m path] keyword ...\n",
247 __progname);
248 exit(1);
249 }
250