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