Annotation of src/usr.bin/apropos/apropos.c, Revision 1.13
1.13 ! sobrado 1: /* $OpenBSD: apropos.c,v 1.12 2006/04/02 21:38:56 djm 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.13 ! sobrado 43: static char rcsid[] = "$OpenBSD: apropos.c,v 1.12 2006/04/02 21:38:56 djm 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
1.10 deraadt 71: main(int argc, char *argv[])
1.1 deraadt 72: {
73: ENTRY *ep;
74: TAG *tp;
75: int ch, rv;
76: char *conffile, **p, *p_augment, *p_path;
77:
78: conffile = NULL;
79: p_augment = p_path = NULL;
1.4 millert 80: while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
1.1 deraadt 81: switch (ch) {
82: case 'C':
83: conffile = optarg;
84: break;
85: case 'M':
86: case 'P': /* backward compatible */
87: p_path = optarg;
88: break;
89: case 'm':
90: p_augment = optarg;
91: break;
92: case '?':
93: default:
94: usage();
95: }
96: argv += optind;
97: argc -= optind;
98:
99: if (argc < 1)
100: usage();
101:
1.12 djm 102: if ((found = calloc(argc, sizeof(int))) == NULL)
1.1 deraadt 103: err(1, NULL);
104:
105: for (p = argv; *p; ++p) /* convert to lower-case */
106: lowstr(*p, *p);
107:
108: if (p_augment)
109: apropos(argv, p_augment, 1);
110: if (p_path || (p_path = getenv("MANPATH")))
111: apropos(argv, p_path, 1);
112: else {
113: config(conffile);
114: ep = (tp = getlist("_whatdb")) == NULL ?
1.11 otto 115: NULL : TAILQ_FIRST(&tp->list);
116: for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1 deraadt 117: apropos(argv, ep->s, 0);
118: }
119:
120: if (!foundman)
121: errx(1, "no %s file found", _PATH_WHATIS);
122:
123: rv = 1;
124: for (p = argv; *p; ++p)
125: if (found[p - argv])
126: rv = 0;
127: else
128: (void)printf("%s: nothing appropriate\n", *p);
129: exit(rv);
130: }
131:
132: void
1.10 deraadt 133: apropos(char **argv, char *path, int buildpath)
1.1 deraadt 134: {
135: char *end, *name, **p;
1.6 deraadt 136: char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
137: char hold[MAXPATHLEN];
1.1 deraadt 138:
139: for (name = path; name; name = end) { /* through name list */
1.5 deraadt 140: if ((end = strchr(name, ':')))
1.1 deraadt 141: *end++ = '\0';
142:
143: if (buildpath) {
1.3 downsj 144: (void)snprintf(hold, sizeof(hold), "%s/%s", name,
145: _PATH_WHATIS);
1.1 deraadt 146: name = hold;
147: }
148:
149: if (!freopen(name, "r", stdin))
150: continue;
151:
152: foundman = 1;
153:
154: /* for each file found */
155: while (fgets(buf, sizeof(buf), stdin)) {
156: if (!strchr(buf, '\n')) {
157: warnx("%s: line too long", name);
158: continue;
159: }
160: lowstr(buf, wbuf);
161: for (p = argv; *p; ++p)
162: if (match(wbuf, *p)) {
163: (void)printf("%s", buf);
164: found[p - argv] = 1;
165:
166: /* only print line once */
167: while (*++p)
168: if (match(wbuf, *p))
169: found[p - argv] = 1;
170: break;
171: }
172: }
173: }
174: }
175:
176: /*
177: * match --
178: * match anywhere the string appears
179: */
180: int
1.10 deraadt 181: match(char *bp, char *str)
1.1 deraadt 182: {
183: int len;
184: char test;
185:
186: if (!*bp)
187: return (0);
188: /* backward compatible: everything matches empty string */
189: if (!*str)
190: return (1);
191: for (test = *str++, len = strlen(str); *bp;)
192: if (test == *bp++ && !strncmp(bp, str, len))
193: return (1);
194: return (0);
195: }
196:
197: /*
198: * lowstr --
199: * convert a string to lower case
200: */
201: void
1.10 deraadt 202: lowstr(char *from, char *to)
1.1 deraadt 203: {
204: char ch;
205:
206: while ((ch = *from++) && ch != '\n')
207: *to++ = isupper(ch) ? tolower(ch) : ch;
208: *to = '\0';
209: }
210:
211: /*
212: * usage --
213: * print usage message and die
214: */
215: void
1.10 deraadt 216: usage(void)
1.1 deraadt 217: {
218:
219: (void)fprintf(stderr,
1.13 ! sobrado 220: "usage: apropos [-C file] [-M path] [-m path] keyword ...\n");
1.1 deraadt 221: exit(1);
222: }