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