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