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