xref: /netbsd-src/usr.bin/apropos/apropos.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: apropos.c,v 1.24 2004/10/30 16:10:46 dsl 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.24 2004/10/30 16:10:46 dsl 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 int main __P((int, char **));
67 void apropos __P((char **, char *, int));
68 void lowstr __P((char *, char *));
69 int match __P((char *, char *));
70 void usage __P((void));
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 *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:")) != -1)
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, "malloc");
109 	memset(found, 0, argc * sizeof(int));
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, 1);
116 	if (p_path || (p_path = getenv("MANPATH")))
117 		apropos(argv, p_path, 1);
118 	else {
119 		config(conffile);
120 		tp = getlist("_whatdb", 1);
121 		TAILQ_FOREACH(ep, &tp->list, q) {
122 			if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL,
123 			    &pg)) != 0) {
124 				if (rv == GLOB_NOMATCH)
125 					continue;
126 				else
127 					err(EXIT_FAILURE, "glob");
128 			}
129 			if (pg.gl_pathc)
130 				for (p = pg.gl_pathv; *p; p++)
131 					apropos(argv, *p, 0);
132 			globfree(&pg);
133 		}
134 	}
135 
136 	if (!foundman)
137 		errx(1, "no %s file found", _PATH_WHATIS);
138 
139 	rv = 1;
140 	for (p = argv; *p; ++p)
141 		if (found[p - argv])
142 			rv = 0;
143 		else
144 			(void)printf("%s: nothing appropriate\n", *p);
145 	exit(rv);
146 }
147 
148 void
149 apropos(argv, path, buildpath)
150 	char **argv, *path;
151 	int buildpath;
152 {
153 	char *end, *name, **p;
154 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
155 	char hold[MAXPATHLEN + 1];
156 
157 	for (name = path; name; name = end) {	/* through name list */
158 		if ((end = strchr(name, ':')))
159 			*end++ = '\0';
160 
161 		if (buildpath) {
162 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
163 			name = hold;
164 		}
165 
166 		if (!freopen(name, "r", stdin))
167 			continue;
168 
169 		foundman = 1;
170 
171 		/* for each file found */
172 		while (fgets(buf, sizeof(buf), stdin)) {
173 			if (!strchr(buf, '\n')) {
174 				warnx("%s: line too long", name);
175 				continue;
176 			}
177 			lowstr(buf, wbuf);
178 			for (p = argv; *p; ++p)
179 				if (match(wbuf, *p)) {
180 					(void)printf("%s", buf);
181 					found[p - argv] = 1;
182 
183 					/* only print line once */
184 					while (*++p)
185 						if (match(wbuf, *p))
186 							found[p - argv] = 1;
187 					break;
188 				}
189 		}
190 	}
191 }
192 
193 /*
194  * match --
195  *	match anywhere the string appears
196  */
197 int
198 match(bp, str)
199 	char *bp, *str;
200 {
201 	int len;
202 	char test;
203 
204 	if (!*bp)
205 		return (0);
206 	/* backward compatible: everything matches empty string */
207 	if (!*str)
208 		return (1);
209 	for (test = *str++, len = strlen(str); *bp;)
210 		if (test == *bp++ && !strncmp(bp, str, len))
211 			return (1);
212 	return (0);
213 }
214 
215 /*
216  * lowstr --
217  *	convert a string to lower case
218  */
219 void
220 lowstr(from, to)
221 	char *from, *to;
222 {
223 	char ch;
224 
225 	while ((ch = *from++) && ch != '\n')
226 		*to++ = tolower((unsigned char)ch);
227 	*to = '\0';
228 }
229 
230 /*
231  * usage --
232  *	print usage message and die
233  */
234 void
235 usage()
236 {
237 
238 	(void)fprintf(stderr,
239 	    "usage: %s [-C file] [-M path] [-m path] keyword ...\n",
240 	    getprogname());
241 	exit(1);
242 }
243