Annotation of src/usr.bin/whatis/whatis.c, Revision 1.12
1.12 ! tedu 1: /* $OpenBSD: whatis.c,v 1.11 2006/04/02 21:38:56 djm 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:
1.11 djm 96: if ((found = calloc(argc, sizeof(int))) == NULL)
1.1 deraadt 97: err(1, NULL);
98:
99: for (p = argv; *p; ++p) /* trim full paths */
1.5 millert 100: if ((beg = strrchr(*p, '/')))
1.1 deraadt 101: *p = beg + 1;
102:
103: if (p_augment)
104: whatis(argv, p_augment, 1);
105: if (p_path || (p_path = getenv("MANPATH")))
106: whatis(argv, p_path, 1);
107: else {
108: config(conffile);
109: ep = (tp = getlist("_whatdb")) == NULL ?
1.10 otto 110: NULL : TAILQ_FIRST(&tp->list);
111: for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1 deraadt 112: whatis(argv, ep->s, 0);
113: }
114:
115: if (!foundman) {
116: fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
117: exit(1);
118: }
119: rv = 1;
120: for (p = argv; *p; ++p)
121: if (found[p - argv])
122: rv = 0;
123: else
124: printf("%s: not found\n", *p);
125: exit(rv);
126: }
127:
128: void
1.9 deraadt 129: whatis(char **argv, char *path, int buildpath)
1.1 deraadt 130: {
1.6 mpech 131: char *end, *name, **p;
1.1 deraadt 132: char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
1.4 deraadt 133: char hold[MAXPATHLEN];
1.1 deraadt 134:
135: for (name = path; name; name = end) { /* through name list */
1.5 millert 136: if ((end = strchr(name, ':')))
1.1 deraadt 137: *end++ = '\0';
138:
139: if (buildpath) {
1.4 deraadt 140: (void)snprintf(hold, sizeof hold, "%s/%s",
141: name, _PATH_WHATIS);
1.1 deraadt 142: name = hold;
143: }
144:
145: if (!freopen(name, "r", stdin))
146: continue;
147:
148: foundman = 1;
149:
150: /* for each file found */
151: while (fgets(buf, sizeof(buf), stdin)) {
152: dashtrunc(buf, wbuf);
153: for (p = argv; *p; ++p)
154: if (match(wbuf, *p)) {
155: printf("%s", buf);
156: found[p - argv] = 1;
157:
158: /* only print line once */
159: while (*++p)
160: if (match(wbuf, *p))
161: found[p - argv] = 1;
162: break;
163: }
164: }
165: }
166: }
167:
168: /*
169: * match --
1.5 millert 170: * match a full word or a full string
1.1 deraadt 171: */
172: int
1.9 deraadt 173: match(char *bp, char *str)
1.1 deraadt 174: {
1.6 mpech 175: int len;
176: char *start;
1.1 deraadt 177:
178: if (!*str || !*bp)
179: return(0);
180: for (len = strlen(str);;) {
1.5 millert 181: /* skip leading crud */
182: for (; *bp && !isalnum(*bp); ++bp)
183: ;
1.1 deraadt 184: if (!*bp)
185: break;
1.5 millert 186:
187: /* check for word match first */
1.12 ! tedu 188: for (start = bp++; *bp == '_' || isalnum(*bp); ++bp)
1.5 millert 189: ;
190: if (bp - start == len) {
191: if (strncasecmp(start, str, len) == 0)
192: return(1);
193: } else if (*bp && *bp != ',') {
194: /* check for full string match */
195: for (bp = start;
196: *bp && *bp != ',' && *bp != '(' && !isspace(*bp); ++bp)
197: ;
198: if (bp - start == len && strncasecmp(start, str, len) == 0)
199: return(1);
200: }
1.1 deraadt 201: }
202: return(0);
203: }
204:
205: /*
206: * dashtrunc --
207: * truncate a string at " - "
208: */
209: void
1.9 deraadt 210: dashtrunc(char *from, char *to)
1.1 deraadt 211: {
1.6 mpech 212: int ch;
1.1 deraadt 213:
214: for (; (ch = *from) && ch != '\n' &&
215: (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
216: *to++ = ch;
217: *to = '\0';
218: }
219:
220: /*
221: * usage --
222: * print usage message and die
223: */
224: void
1.9 deraadt 225: usage(void)
1.1 deraadt 226: {
1.5 millert 227:
1.1 deraadt 228: (void)fprintf(stderr,
1.5 millert 229: "usage: %s [-C file] [-M path] [-m path] command ...\n",
230: __progname);
1.1 deraadt 231: exit(1);
232: }