1 /* $NetBSD: apropos.c,v 1.29 2008/07/21 14:19:20 lukem Exp $ */
4 * Copyright (c) 1987, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
35 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
36 The Regents of the University of California. All rights reserved.");
41 static char sccsid
[] = "@(#)apropos.c 8.8 (Berkeley) 5/4/95";
43 __RCSID("$NetBSD: apropos.c,v 1.29 2008/07/21 14:19:20 lukem Exp $");
47 #include <sys/param.h>
48 #include <sys/queue.h>
60 #include "manconf.h" /* from ../man/ */
61 #include "pathnames.h" /* from ../man/ */
64 static bool foundman
= false;
66 #define MAXLINELEN 8192 /* max line handled */
68 static void apropos(char **, const char *, bool, const char *, const char *);
69 static void lowstr(const char *, char *);
70 static bool match(const char *, const char *);
71 static void usage(void) __dead
;
74 main(int argc
, char *argv
[])
79 char *conffile
, *machine
, **p
, *p_augment
, *p_path
, *sflag
;
83 p_augment
= p_path
= NULL
;
84 machine
= sflag
= NULL
;
85 while ((ch
= getopt(argc
, argv
, "C:M:m:P:S:s:")) != -1) {
91 case 'P': /* backward compatible */
99 lowstr(machine
, machine
);
103 lowstr(sflag
, sflag
);
116 if ((found
= malloc(argc
* sizeof(*found
))) == NULL
)
117 err(EXIT_FAILURE
, "malloc");
118 (void)memset(found
, 0, argc
* sizeof(*found
));
120 for (p
= argv
; *p
; ++p
) /* convert to lower-case */
124 apropos(argv
, p_augment
, true, sflag
, machine
);
125 if (p_path
|| (p_path
= getenv("MANPATH")))
126 apropos(argv
, p_path
, true, sflag
, machine
);
129 tp
= gettag("_whatdb", 1);
131 errx(EXIT_FAILURE
, "malloc");
132 TAILQ_FOREACH(ep
, &tp
->entrylist
, q
) {
133 if ((rv
= glob(ep
->s
, GLOB_BRACE
| GLOB_NOSORT
, NULL
,
135 if (rv
== GLOB_NOMATCH
)
138 err(EXIT_FAILURE
, "glob");
141 for (p
= pg
.gl_pathv
; *p
; p
++)
142 apropos(argv
, *p
, false, sflag
,
149 errx(EXIT_FAILURE
, "no %s file found", _PATH_WHATIS
);
152 for (p
= argv
; *p
; ++p
)
156 (void)printf("%s: nothing appropriate\n", *p
);
161 apropos(char **argv
, const char *path
, bool buildpath
,
162 const char *sflag
, const char *machine
)
166 char buf
[MAXLINELEN
+ 1];
167 char hold
[MAXPATHLEN
+ 1];
168 char wbuf
[MAXLINELEN
+ 1];
169 size_t slen
= 0, mlen
= 0;
172 slen
= strlen(sflag
);
174 mlen
= strlen(machine
);
176 for (name
= path
; name
; name
= end
) { /* through name list */
177 if ((end
= strchr(name
, ':')) != NULL
)
181 (void)snprintf(hold
, sizeof(hold
), "%s/%s", name
,
186 if (!freopen(name
, "r", stdin
))
191 /* for each file found */
192 while (fgets(buf
, (int)sizeof(buf
), stdin
)) {
193 if (!strchr(buf
, '\n')) {
194 warnx("%s: line too long", name
);
199 if ((strncmp(wbuf
, machine
, mlen
) != 0) ||
200 strlen(wbuf
) <= mlen
|| wbuf
[mlen
] != '/')
204 char *s
= strchr(wbuf
, '(');
208 if (strncmp(s
+1, sflag
, slen
) != 0)
211 for (p
= argv
; *p
; ++p
) {
212 if (match(wbuf
, *p
)) {
213 (void)printf("%s", buf
);
214 found
[p
- argv
] = true;
216 /* only print line once */
219 found
[p
- argv
] = true;
229 * match anywhere the string appears
232 match(const char *bp
, const char *str
)
239 /* backward compatible: everything matches empty string */
242 for (test
= *str
++, len
= strlen(str
); *bp
;)
243 if (test
== *bp
++ && !strncmp(bp
, str
, len
))
250 * convert a string to lower case
253 lowstr(const char *from
, char *to
)
257 while ((ch
= *from
++) && ch
!= '\n')
258 *to
++ = tolower((unsigned char)ch
);
264 * print usage message and die
271 (void)fprintf(stderr
,
272 "usage: %s [-C file] [-M path] [-m path] "
273 "[-S subsection] [-s section]\n"