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