1 /* $OpenBSD: apropos.c,v 1.10 2003/06/10 22:20:44 deraadt Exp $ */
2 /* $NetBSD: apropos.c,v 1.5 1995/09/04 20:46:20 tls Exp $ */
3
4 /*
5 * Copyright (c) 1987, 1993, 1994
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33 #ifndef lint
34 static char copyright[] =
35 "@(#) 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 static char rcsid[] = "$OpenBSD: apropos.c,v 1.10 2003/06/10 22:20:44 deraadt 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 <limits.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57
58 #include "../man/config.h"
59 #include "../man/pathnames.h"
60
61 static int *found, foundman;
62
63 #define MAXLINELEN 8192 /* max line handled */
64
65 void apropos(char **, char *, int);
66 void lowstr(char *, char *);
67 int match(char *, char *);
68 void usage(void);
69
70 int
main(int argc,char * argv[])71 main(int argc, char *argv[])
72 {
73 ENTRY *ep;
74 TAG *tp;
75 int ch, rv;
76 char *conffile, **p, *p_augment, *p_path;
77
78 conffile = NULL;
79 p_augment = p_path = NULL;
80 while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
81 switch (ch) {
82 case 'C':
83 conffile = optarg;
84 break;
85 case 'M':
86 case 'P': /* backward compatible */
87 p_path = optarg;
88 break;
89 case 'm':
90 p_augment = optarg;
91 break;
92 case '?':
93 default:
94 usage();
95 }
96 argv += optind;
97 argc -= optind;
98
99 if (argc < 1)
100 usage();
101
102 if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
103 err(1, NULL);
104 memset(found, 0, argc * sizeof(int));
105
106 for (p = argv; *p; ++p) /* convert to lower-case */
107 lowstr(*p, *p);
108
109 if (p_augment)
110 apropos(argv, p_augment, 1);
111 if (p_path || (p_path = getenv("MANPATH")))
112 apropos(argv, p_path, 1);
113 else {
114 config(conffile);
115 ep = (tp = getlist("_whatdb")) == NULL ?
116 NULL : tp->list.tqh_first;
117 for (; ep != NULL; ep = ep->q.tqe_next)
118 apropos(argv, ep->s, 0);
119 }
120
121 if (!foundman)
122 errx(1, "no %s file found", _PATH_WHATIS);
123
124 rv = 1;
125 for (p = argv; *p; ++p)
126 if (found[p - argv])
127 rv = 0;
128 else
129 (void)printf("%s: nothing appropriate\n", *p);
130 exit(rv);
131 }
132
133 void
apropos(char ** argv,char * path,int buildpath)134 apropos(char **argv, char *path, int buildpath)
135 {
136 char *end, *name, **p;
137 char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
138 char hold[MAXPATHLEN];
139
140 for (name = path; name; name = end) { /* through name list */
141 if ((end = strchr(name, ':')))
142 *end++ = '\0';
143
144 if (buildpath) {
145 (void)snprintf(hold, sizeof(hold), "%s/%s", name,
146 _PATH_WHATIS);
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 if (!strchr(buf, '\n')) {
158 warnx("%s: line too long", name);
159 continue;
160 }
161 lowstr(buf, wbuf);
162 for (p = argv; *p; ++p)
163 if (match(wbuf, *p)) {
164 (void)printf("%s", buf);
165 found[p - argv] = 1;
166
167 /* only print line once */
168 while (*++p)
169 if (match(wbuf, *p))
170 found[p - argv] = 1;
171 break;
172 }
173 }
174 }
175 }
176
177 /*
178 * match --
179 * match anywhere the string appears
180 */
181 int
match(char * bp,char * str)182 match(char *bp, char *str)
183 {
184 int len;
185 char test;
186
187 if (!*bp)
188 return (0);
189 /* backward compatible: everything matches empty string */
190 if (!*str)
191 return (1);
192 for (test = *str++, len = strlen(str); *bp;)
193 if (test == *bp++ && !strncmp(bp, str, len))
194 return (1);
195 return (0);
196 }
197
198 /*
199 * lowstr --
200 * convert a string to lower case
201 */
202 void
lowstr(char * from,char * to)203 lowstr(char *from, char *to)
204 {
205 char ch;
206
207 while ((ch = *from++) && ch != '\n')
208 *to++ = isupper(ch) ? tolower(ch) : ch;
209 *to = '\0';
210 }
211
212 /*
213 * usage --
214 * print usage message and die
215 */
216 void
usage(void)217 usage(void)
218 {
219
220 (void)fprintf(stderr,
221 "usage: apropos [-C file] [-M path] [-m path] keyword [...]\n");
222 exit(1);
223 }
224