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