apropos.c revision 1.25 1 1.25 xtraeme /* $NetBSD: apropos.c,v 1.25 2005/02/17 16:53:45 xtraeme Exp $ */
2 1.3 glass
3 1.1 cgd /*
4 1.2 glass * Copyright (c) 1987, 1993, 1994
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.22 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.8 mikel #include <sys/cdefs.h>
33 1.8 mikel
34 1.1 cgd #ifndef lint
35 1.8 mikel __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
36 1.8 mikel The Regents of the University of California. All rights reserved.\n");
37 1.1 cgd #endif /* not lint */
38 1.1 cgd
39 1.1 cgd #ifndef lint
40 1.3 glass #if 0
41 1.5 tls static char sccsid[] = "@(#)apropos.c 8.8 (Berkeley) 5/4/95";
42 1.3 glass #else
43 1.25 xtraeme __RCSID("$NetBSD: apropos.c,v 1.25 2005/02/17 16:53:45 xtraeme Exp $");
44 1.3 glass #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.7 mikel #include <glob.h>
53 1.2 glass #include <limits.h>
54 1.1 cgd #include <stdio.h>
55 1.1 cgd #include <stdlib.h>
56 1.1 cgd #include <string.h>
57 1.5 tls #include <unistd.h>
58 1.1 cgd
59 1.21 lukem #include "manconf.h" /* from ../man/ */
60 1.21 lukem #include "pathnames.h" /* from ../man/ */
61 1.1 cgd
62 1.2 glass static int *found, foundman;
63 1.1 cgd
64 1.11 mrg #define MAXLINELEN 8192 /* max line handled */
65 1.11 mrg
66 1.25 xtraeme void apropos(char **, char *, int);
67 1.25 xtraeme void lowstr(char *, char *);
68 1.25 xtraeme int match(char *, char *);
69 1.25 xtraeme void usage(void);
70 1.1 cgd
71 1.1 cgd int
72 1.25 xtraeme 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 *conffile, **p, *p_augment, *p_path;
78 1.7 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 mikel 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.8 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) /* convert to lower-case */
109 1.1 cgd lowstr(*p, *p);
110 1.1 cgd
111 1.1 cgd if (p_augment)
112 1.1 cgd apropos(argv, p_augment, 1);
113 1.1 cgd if (p_path || (p_path = getenv("MANPATH")))
114 1.1 cgd apropos(argv, p_path, 1);
115 1.1 cgd else {
116 1.1 cgd config(conffile);
117 1.19 lukem tp = getlist("_whatdb", 1);
118 1.19 lukem TAILQ_FOREACH(ep, &tp->list, q) {
119 1.13 kleink if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL,
120 1.13 kleink &pg)) != 0) {
121 1.13 kleink if (rv == GLOB_NOMATCH)
122 1.13 kleink continue;
123 1.13 kleink else
124 1.17 jdolecek err(EXIT_FAILURE, "glob");
125 1.13 kleink }
126 1.10 mikel if (pg.gl_pathc)
127 1.10 mikel for (p = pg.gl_pathv; *p; p++)
128 1.10 mikel apropos(argv, *p, 0);
129 1.7 mikel globfree(&pg);
130 1.7 mikel }
131 1.1 cgd }
132 1.1 cgd
133 1.2 glass if (!foundman)
134 1.2 glass errx(1, "no %s file found", _PATH_WHATIS);
135 1.2 glass
136 1.1 cgd rv = 1;
137 1.1 cgd for (p = argv; *p; ++p)
138 1.1 cgd if (found[p - argv])
139 1.1 cgd rv = 0;
140 1.1 cgd else
141 1.1 cgd (void)printf("%s: nothing appropriate\n", *p);
142 1.1 cgd exit(rv);
143 1.1 cgd }
144 1.1 cgd
145 1.2 glass void
146 1.25 xtraeme apropos(char **argv, char *path, int buildpath)
147 1.1 cgd {
148 1.2 glass char *end, *name, **p;
149 1.11 mrg char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
150 1.8 mikel char hold[MAXPATHLEN + 1];
151 1.1 cgd
152 1.1 cgd for (name = path; name; name = end) { /* through name list */
153 1.8 mikel if ((end = strchr(name, ':')))
154 1.1 cgd *end++ = '\0';
155 1.1 cgd
156 1.1 cgd if (buildpath) {
157 1.1 cgd (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
158 1.1 cgd name = hold;
159 1.1 cgd }
160 1.1 cgd
161 1.1 cgd if (!freopen(name, "r", stdin))
162 1.1 cgd continue;
163 1.1 cgd
164 1.1 cgd foundman = 1;
165 1.1 cgd
166 1.1 cgd /* for each file found */
167 1.1 cgd while (fgets(buf, sizeof(buf), stdin)) {
168 1.2 glass if (!strchr(buf, '\n')) {
169 1.2 glass warnx("%s: line too long", name);
170 1.1 cgd continue;
171 1.1 cgd }
172 1.1 cgd lowstr(buf, wbuf);
173 1.1 cgd for (p = argv; *p; ++p)
174 1.1 cgd if (match(wbuf, *p)) {
175 1.1 cgd (void)printf("%s", buf);
176 1.1 cgd found[p - argv] = 1;
177 1.1 cgd
178 1.1 cgd /* only print line once */
179 1.1 cgd while (*++p)
180 1.1 cgd if (match(wbuf, *p))
181 1.1 cgd found[p - argv] = 1;
182 1.1 cgd break;
183 1.1 cgd }
184 1.1 cgd }
185 1.1 cgd }
186 1.1 cgd }
187 1.1 cgd
188 1.1 cgd /*
189 1.1 cgd * match --
190 1.1 cgd * match anywhere the string appears
191 1.1 cgd */
192 1.2 glass int
193 1.25 xtraeme match(char *bp, char *str)
194 1.1 cgd {
195 1.2 glass int len;
196 1.2 glass char test;
197 1.1 cgd
198 1.1 cgd if (!*bp)
199 1.2 glass return (0);
200 1.1 cgd /* backward compatible: everything matches empty string */
201 1.1 cgd if (!*str)
202 1.2 glass return (1);
203 1.1 cgd for (test = *str++, len = strlen(str); *bp;)
204 1.1 cgd if (test == *bp++ && !strncmp(bp, str, len))
205 1.2 glass return (1);
206 1.2 glass return (0);
207 1.1 cgd }
208 1.1 cgd
209 1.1 cgd /*
210 1.1 cgd * lowstr --
211 1.1 cgd * convert a string to lower case
212 1.1 cgd */
213 1.2 glass void
214 1.25 xtraeme lowstr(char *from, char *to)
215 1.1 cgd {
216 1.2 glass char ch;
217 1.1 cgd
218 1.1 cgd while ((ch = *from++) && ch != '\n')
219 1.24 dsl *to++ = tolower((unsigned char)ch);
220 1.1 cgd *to = '\0';
221 1.1 cgd }
222 1.1 cgd
223 1.1 cgd /*
224 1.1 cgd * usage --
225 1.1 cgd * print usage message and die
226 1.1 cgd */
227 1.2 glass void
228 1.25 xtraeme usage(void)
229 1.1 cgd {
230 1.16 cgd
231 1.1 cgd (void)fprintf(stderr,
232 1.23 jmmv "usage: %s [-C file] [-M path] [-m path] keyword ...\n",
233 1.16 cgd getprogname());
234 1.1 cgd exit(1);
235 1.1 cgd }
236