xref: /netbsd-src/usr.bin/whatis/whatis.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char copyright[] =
36 "@(#) Copyright (c) 1987, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 static char sccsid[] = "@(#)whatis.c	8.5 (Berkeley) 11/26/93";
42 #endif /* not lint */
43 
44 #include <sys/param.h>
45 #include <sys/queue.h>
46 
47 #include <ctype.h>
48 #include <err.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include "../man/config.h"
54 #include "../man/pathnames.h"
55 
56 #define	MAXLINELEN	256			/* max line handled */
57 
58 static int *found, foundman;
59 
60 int
61 main(argc, argv)
62 	int argc;
63 	char *argv[];
64 {
65 	extern char *optarg;
66 	extern int optind;
67 	ENTRY *ep;
68 	TAG *tp;
69 	int ch, rv;
70 	char *beg, *conffile, **p, *p_augment, *p_path;
71 
72 	conffile = NULL;
73 	p_augment = p_path = NULL;
74 	while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF)
75 		switch (ch) {
76 		case 'C':
77 			conffile = optarg;
78 			break;
79 		case 'M':
80 		case 'P':		/* backward compatible */
81 			p_path = optarg;
82 			break;
83 		case 'm':
84 			p_augment = optarg;
85 			break;
86 		case '?':
87 		default:
88 			usage();
89 		}
90 	argv += optind;
91 	argc -= optind;
92 
93 	if (argc < 1)
94 		usage();
95 
96 	if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
97 		err(1, NULL);
98 	memset(found, 0, argc * sizeof(int));
99 
100 	for (p = argv; *p; ++p)			/* trim full paths */
101 		if (beg = rindex(*p, '/'))
102 			*p = beg + 1;
103 
104 	if (p_augment)
105 		whatis(argv, p_augment, 1);
106 	if (p_path || (p_path = getenv("MANPATH")))
107 		whatis(argv, p_path, 1);
108 	else {
109 		config(conffile);
110 		ep = (tp = getlist("_whatdb")) == NULL ?
111 		   NULL : tp->list.tqh_first;
112 		for (; ep != NULL; ep = ep->q.tqe_next)
113 			whatis(argv, ep->s, 0);
114 	}
115 
116 	if (!foundman) {
117 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
118 		exit(1);
119 	}
120 	rv = 1;
121 	for (p = argv; *p; ++p)
122 		if (found[p - argv])
123 			rv = 0;
124 		else
125 			printf("%s: not found\n", *p);
126 	exit(rv);
127 }
128 
129 whatis(argv, path, buildpath)
130 	char **argv, *path;
131 	int buildpath;
132 {
133 	register char *end, *name, **p;
134 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
135 
136 	for (name = path; name; name = end) {	/* through name list */
137 		if (end = index(name, ':'))
138 			*end++ = '\0';
139 
140 		if (buildpath) {
141 			char hold[MAXPATHLEN + 1];
142 
143 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
144 			name = hold;
145 		}
146 
147 		if (!freopen(name, "r", stdin))
148 			continue;
149 
150 		foundman = 1;
151 
152 		/* for each file found */
153 		while (fgets(buf, sizeof(buf), stdin)) {
154 			dashtrunc(buf, wbuf);
155 			for (p = argv; *p; ++p)
156 				if (match(wbuf, *p)) {
157 					printf("%s", buf);
158 					found[p - argv] = 1;
159 
160 					/* only print line once */
161 					while (*++p)
162 						if (match(wbuf, *p))
163 							found[p - argv] = 1;
164 					break;
165 				}
166 		}
167 	}
168 }
169 
170 /*
171  * match --
172  *	match a full word
173  */
174 match(bp, str)
175 	register char *bp, *str;
176 {
177 	register int len;
178 	register char *start;
179 
180 	if (!*str || !*bp)
181 		return(0);
182 	for (len = strlen(str);;) {
183 		for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
184 		if (!*bp)
185 			break;
186 		for (start = bp++;
187 		    *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
188 		if (bp - start == len && !strncasecmp(start, str, len))
189 			return(1);
190 	}
191 	return(0);
192 }
193 
194 /*
195  * dashtrunc --
196  *	truncate a string at " - "
197  */
198 dashtrunc(from, to)
199 	register char *from, *to;
200 {
201 	register int ch;
202 
203 	for (; (ch = *from) && ch != '\n' &&
204 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
205 		*to++ = ch;
206 	*to = '\0';
207 }
208 
209 /*
210  * usage --
211  *	print usage message and die
212  */
213 usage()
214 {
215 	(void)fprintf(stderr,
216 	    "usage: whatis [-C file] [-M path] [-m path] command ...\n");
217 	exit(1);
218 }
219