apropos.c revision 1.7 1 1.7 mikel /* $NetBSD: apropos.c,v 1.7 1997/10/16 08:44:23 mikel 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.1 cgd static char copyright[] =
38 1.2 glass "@(#) Copyright (c) 1987, 1993, 1994\n\
39 1.1 cgd The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.3 glass #if 0
44 1.5 tls static char sccsid[] = "@(#)apropos.c 8.8 (Berkeley) 5/4/95";
45 1.3 glass #else
46 1.7 mikel static char rcsid[] = "$NetBSD: apropos.c,v 1.7 1997/10/16 08:44:23 mikel Exp $";
47 1.3 glass #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include <sys/param.h>
51 1.1 cgd #include <sys/queue.h>
52 1.1 cgd
53 1.1 cgd #include <ctype.h>
54 1.1 cgd #include <err.h>
55 1.7 mikel #include <glob.h>
56 1.2 glass #include <limits.h>
57 1.1 cgd #include <stdio.h>
58 1.1 cgd #include <stdlib.h>
59 1.1 cgd #include <string.h>
60 1.5 tls #include <unistd.h>
61 1.1 cgd
62 1.1 cgd #include "../man/config.h"
63 1.1 cgd #include "../man/pathnames.h"
64 1.1 cgd
65 1.2 glass static int *found, foundman;
66 1.1 cgd
67 1.2 glass void apropos __P((char **, char *, int));
68 1.2 glass void lowstr __P((char *, char *));
69 1.2 glass int match __P((char *, char *));
70 1.2 glass void usage __P((void));
71 1.1 cgd
72 1.1 cgd int
73 1.1 cgd main(argc, argv)
74 1.1 cgd int argc;
75 1.1 cgd char *argv[];
76 1.1 cgd {
77 1.1 cgd ENTRY *ep;
78 1.1 cgd TAG *tp;
79 1.1 cgd int ch, rv;
80 1.1 cgd char *conffile, **p, *p_augment, *p_path;
81 1.7 mikel glob_t pg;
82 1.1 cgd
83 1.1 cgd conffile = NULL;
84 1.1 cgd p_augment = p_path = NULL;
85 1.1 cgd while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF)
86 1.1 cgd switch (ch) {
87 1.1 cgd case 'C':
88 1.1 cgd conffile = optarg;
89 1.1 cgd break;
90 1.1 cgd case 'M':
91 1.1 cgd case 'P': /* backward compatible */
92 1.1 cgd p_path = optarg;
93 1.1 cgd break;
94 1.1 cgd case 'm':
95 1.1 cgd p_augment = optarg;
96 1.1 cgd break;
97 1.1 cgd case '?':
98 1.1 cgd default:
99 1.1 cgd usage();
100 1.1 cgd }
101 1.1 cgd argv += optind;
102 1.1 cgd argc -= optind;
103 1.1 cgd
104 1.1 cgd if (argc < 1)
105 1.1 cgd usage();
106 1.1 cgd
107 1.1 cgd if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
108 1.1 cgd err(1, NULL);
109 1.1 cgd memset(found, 0, argc * sizeof(int));
110 1.1 cgd
111 1.1 cgd for (p = argv; *p; ++p) /* convert to lower-case */
112 1.1 cgd lowstr(*p, *p);
113 1.1 cgd
114 1.1 cgd if (p_augment)
115 1.1 cgd apropos(argv, p_augment, 1);
116 1.1 cgd if (p_path || (p_path = getenv("MANPATH")))
117 1.1 cgd apropos(argv, p_path, 1);
118 1.1 cgd else {
119 1.1 cgd config(conffile);
120 1.1 cgd ep = (tp = getlist("_whatdb")) == NULL ?
121 1.1 cgd NULL : tp->list.tqh_first;
122 1.7 mikel for (; ep != NULL; ep = ep->q.tqe_next) {
123 1.7 mikel if (glob(ep->s, GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
124 1.7 mikel NULL, &pg) != 0)
125 1.7 mikel err(1, "glob");
126 1.7 mikel for (p = pg.gl_pathv; *p; p++)
127 1.7 mikel apropos(argv, *p, 0);
128 1.7 mikel globfree(&pg);
129 1.7 mikel }
130 1.1 cgd }
131 1.1 cgd
132 1.2 glass if (!foundman)
133 1.2 glass errx(1, "no %s file found", _PATH_WHATIS);
134 1.2 glass
135 1.1 cgd rv = 1;
136 1.1 cgd for (p = argv; *p; ++p)
137 1.1 cgd if (found[p - argv])
138 1.1 cgd rv = 0;
139 1.1 cgd else
140 1.1 cgd (void)printf("%s: nothing appropriate\n", *p);
141 1.1 cgd exit(rv);
142 1.1 cgd }
143 1.1 cgd
144 1.2 glass void
145 1.1 cgd apropos(argv, path, buildpath)
146 1.1 cgd char **argv, *path;
147 1.1 cgd int buildpath;
148 1.1 cgd {
149 1.2 glass char *end, *name, **p;
150 1.2 glass char buf[LINE_MAX + 1], wbuf[LINE_MAX + 1];
151 1.1 cgd
152 1.1 cgd for (name = path; name; name = end) { /* through name list */
153 1.2 glass if (end = strchr(name, ':'))
154 1.1 cgd *end++ = '\0';
155 1.1 cgd
156 1.1 cgd if (buildpath) {
157 1.1 cgd char hold[MAXPATHLEN + 1];
158 1.1 cgd
159 1.1 cgd (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
160 1.1 cgd name = hold;
161 1.1 cgd }
162 1.1 cgd
163 1.1 cgd if (!freopen(name, "r", stdin))
164 1.1 cgd continue;
165 1.1 cgd
166 1.1 cgd foundman = 1;
167 1.1 cgd
168 1.1 cgd /* for each file found */
169 1.1 cgd while (fgets(buf, sizeof(buf), stdin)) {
170 1.2 glass if (!strchr(buf, '\n')) {
171 1.2 glass warnx("%s: line too long", name);
172 1.1 cgd continue;
173 1.1 cgd }
174 1.1 cgd lowstr(buf, wbuf);
175 1.1 cgd for (p = argv; *p; ++p)
176 1.1 cgd if (match(wbuf, *p)) {
177 1.1 cgd (void)printf("%s", buf);
178 1.1 cgd found[p - argv] = 1;
179 1.1 cgd
180 1.1 cgd /* only print line once */
181 1.1 cgd while (*++p)
182 1.1 cgd if (match(wbuf, *p))
183 1.1 cgd found[p - argv] = 1;
184 1.1 cgd break;
185 1.1 cgd }
186 1.1 cgd }
187 1.1 cgd }
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * match --
192 1.1 cgd * match anywhere the string appears
193 1.1 cgd */
194 1.2 glass int
195 1.1 cgd match(bp, str)
196 1.2 glass char *bp, *str;
197 1.1 cgd {
198 1.2 glass int len;
199 1.2 glass char test;
200 1.1 cgd
201 1.1 cgd if (!*bp)
202 1.2 glass return (0);
203 1.1 cgd /* backward compatible: everything matches empty string */
204 1.1 cgd if (!*str)
205 1.2 glass return (1);
206 1.1 cgd for (test = *str++, len = strlen(str); *bp;)
207 1.1 cgd if (test == *bp++ && !strncmp(bp, str, len))
208 1.2 glass return (1);
209 1.2 glass return (0);
210 1.1 cgd }
211 1.1 cgd
212 1.1 cgd /*
213 1.1 cgd * lowstr --
214 1.1 cgd * convert a string to lower case
215 1.1 cgd */
216 1.2 glass void
217 1.1 cgd lowstr(from, to)
218 1.2 glass char *from, *to;
219 1.1 cgd {
220 1.2 glass char ch;
221 1.1 cgd
222 1.1 cgd while ((ch = *from++) && ch != '\n')
223 1.1 cgd *to++ = isupper(ch) ? tolower(ch) : ch;
224 1.1 cgd *to = '\0';
225 1.1 cgd }
226 1.1 cgd
227 1.1 cgd /*
228 1.1 cgd * usage --
229 1.1 cgd * print usage message and die
230 1.1 cgd */
231 1.2 glass void
232 1.1 cgd usage()
233 1.1 cgd {
234 1.2 glass
235 1.1 cgd (void)fprintf(stderr,
236 1.1 cgd "usage: apropos [-C file] [-M path] [-m path] keyword ...\n");
237 1.1 cgd exit(1);
238 1.1 cgd }
239