xref: /netbsd/usr.bin/whatis/whatis.c (revision 6550d01e)
1 /*	$NetBSD: whatis.c,v 1.23 2008/07/21 14:19:28 lukem Exp $	*/
2 
3 /*
4  * Copyright (c) 1987, 1993
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\
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[] = "@(#)whatis.c	8.5 (Berkeley) 1/2/94";
42 #else
43 __RCSID("$NetBSD: whatis.c,v 1.23 2008/07/21 14:19:28 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 <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <unistd.h>
57 
58 #include "manconf.h"		/* from ../man/ */
59 #include "pathnames.h"		/* from ../man/ */
60 
61 #define	MAXLINELEN	8192			/* max line handled */
62 
63 static int *found, foundman;
64 
65 int	main(int, char **);
66 void	dashtrunc(char *, char *);
67 int	match(char *, char *);
68 void	usage(void);
69 void	whatis(char **, char *, int);
70 
71 int
72 main(int argc, char **argv)
73 {
74 	ENTRY *ep;
75 	TAG *tp;
76 	int ch, rv;
77 	char *beg, *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)			/* trim full paths */
109 		if ((beg = strrchr(*p, '/')))
110 			*p = beg + 1;
111 
112 	if (p_augment)
113 		whatis(argv, p_augment, 1);
114 	if (p_path || (p_path = getenv("MANPATH")))
115 		whatis(argv, p_path, 1);
116 	else {
117 		config(conffile);
118 		tp = gettag("_whatdb", 0);
119 		if (!tp)
120 			errx(EXIT_FAILURE,
121 			    "no database dirs (_whatdb) in config file");
122 		TAILQ_FOREACH(ep, &tp->entrylist, q) {
123 			if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL,
124 			    &pg)) != 0) {
125 				if (rv == GLOB_NOMATCH)
126 					continue;
127 				else
128 					err(EXIT_FAILURE, "glob");
129 			}
130 			if (pg.gl_pathc)
131 				for (p = pg.gl_pathv; *p; p++)
132 					whatis(argv, *p, 0);
133 			globfree(&pg);
134 		}
135 	}
136 
137 	if (!foundman) {
138 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
139 		exit(1);
140 	}
141 	rv = 1;
142 	for (p = argv; *p; ++p)
143 		if (found[p - argv])
144 			rv = 0;
145 		else
146 			printf("%s: not found\n", *p);
147 	exit(rv);
148 }
149 
150 void
151 whatis(char **argv, char *path, 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 			dashtrunc(buf, wbuf);
174 			for (p = argv; *p; ++p)
175 				if (match(wbuf, *p)) {
176 					printf("%s", buf);
177 					found[p - argv] = 1;
178 
179 					/* only print line once */
180 					while (*++p)
181 						if (match(wbuf, *p))
182 							found[p - argv] = 1;
183 					break;
184 				}
185 		}
186 	}
187 }
188 
189 /*
190  * match --
191  *	match a full word
192  */
193 int
194 match(char *bp, char *str)
195 {
196 	int len;
197 	char *start;
198 
199 	if (!*str || !*bp)
200 		return(0);
201 	for (len = strlen(str);;) {
202 		/*
203 		 * /bin/[ is a special case.
204 		 */
205 		for (; *bp && *bp != '[' && !isalnum((unsigned char)*bp); ++bp);
206 		if (!*bp)
207 			break;
208 
209 		/* check for word match first */
210 		for (start = bp++; *bp && (*bp == '_' ||
211 			isalnum((unsigned char) *bp)); ++bp);
212 		if (bp - start == len && strncasecmp(start, str, len) == 0) {
213 			return(1);
214 		} else if (*bp && *bp != ',') {
215 			/* check for full string match */
216 			for (bp = start; *bp && *bp != ',' && *bp != '(' &&
217 				!isspace((unsigned char) *bp); ++bp);
218 			if (bp - start == len &&
219 				strncasecmp(start, str, len) == 0)
220 				return(1);
221 		}
222 	}
223 	return(0);
224 }
225 
226 /*
227  * dashtrunc --
228  *	truncate a string at " - "
229  */
230 void
231 dashtrunc(char *from, char *to)
232 {
233 	int ch;
234 
235 	for (; (ch = *from) && ch != '\n' &&
236 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
237 		*to++ = ch;
238 	*to = '\0';
239 }
240 
241 /*
242  * usage --
243  *	print usage message and die
244  */
245 void
246 usage(void)
247 {
248 	(void)fprintf(stderr,
249 	    "usage: whatis [-C file] [-M path] [-m path] command ...\n");
250 	exit(1);
251 }
252