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