Annotation of src/usr.bin/apropos/apropos.c, Revision 1.2
1.2 ! deraadt 1: /* $OpenBSD: apropos.c,v 1.5 1995/09/04 20:46:20 tls 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.2 ! deraadt 47: static char rcsid[] = "$OpenBSD: apropos.c,v 1.5 1995/09/04 20:46:20 tls 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;
84: while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF)
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 */
146: if (end = strchr(name, ':'))
147: *end++ = '\0';
148:
149: if (buildpath) {
150: char hold[MAXPATHLEN + 1];
151:
152: (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
153: name = hold;
154: }
155:
156: if (!freopen(name, "r", stdin))
157: continue;
158:
159: foundman = 1;
160:
161: /* for each file found */
162: while (fgets(buf, sizeof(buf), stdin)) {
163: if (!strchr(buf, '\n')) {
164: warnx("%s: line too long", name);
165: continue;
166: }
167: lowstr(buf, wbuf);
168: for (p = argv; *p; ++p)
169: if (match(wbuf, *p)) {
170: (void)printf("%s", buf);
171: found[p - argv] = 1;
172:
173: /* only print line once */
174: while (*++p)
175: if (match(wbuf, *p))
176: found[p - argv] = 1;
177: break;
178: }
179: }
180: }
181: }
182:
183: /*
184: * match --
185: * match anywhere the string appears
186: */
187: int
188: match(bp, str)
189: char *bp, *str;
190: {
191: int len;
192: char test;
193:
194: if (!*bp)
195: return (0);
196: /* backward compatible: everything matches empty string */
197: if (!*str)
198: return (1);
199: for (test = *str++, len = strlen(str); *bp;)
200: if (test == *bp++ && !strncmp(bp, str, len))
201: return (1);
202: return (0);
203: }
204:
205: /*
206: * lowstr --
207: * convert a string to lower case
208: */
209: void
210: lowstr(from, to)
211: char *from, *to;
212: {
213: char ch;
214:
215: while ((ch = *from++) && ch != '\n')
216: *to++ = isupper(ch) ? tolower(ch) : ch;
217: *to = '\0';
218: }
219:
220: /*
221: * usage --
222: * print usage message and die
223: */
224: void
225: usage()
226: {
227:
228: (void)fprintf(stderr,
229: "usage: apropos [-C file] [-M path] [-m path] keyword ...\n");
230: exit(1);
231: }