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