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