Annotation of src/usr.bin/whatis/whatis.c, Revision 1.15
1.15 ! deraadt 1: /* $OpenBSD: whatis.c,v 1.14 2013/11/15 22:20:04 millert 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: #include <sys/param.h>
33: #include <sys/queue.h>
34:
35: #include <ctype.h>
36: #include <err.h>
37: #include <stdio.h>
38: #include <stdlib.h>
39: #include <string.h>
1.14 millert 40: #include <unistd.h>
1.1 deraadt 41:
42: #include "../man/config.h"
43: #include "../man/pathnames.h"
44:
1.4 deraadt 45: #define MAXLINELEN 8192 /* max line handled */
1.1 deraadt 46:
47: static int *found, foundman;
1.5 millert 48: extern char *__progname;
1.1 deraadt 49:
1.7 millert 50: void dashtrunc(char *, char *);
51: int match(char *, char *);
52: void usage(void);
53: void whatis(char **, char *, int);
1.1 deraadt 54:
55: int
1.9 deraadt 56: main(int argc, char *argv[])
1.1 deraadt 57: {
58: ENTRY *ep;
59: TAG *tp;
60: int ch, rv;
61: char *beg, *conffile, **p, *p_augment, *p_path;
62:
63: conffile = NULL;
64: p_augment = p_path = NULL;
1.3 millert 65: while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
1.1 deraadt 66: switch (ch) {
67: case 'C':
68: conffile = optarg;
69: break;
70: case 'M':
71: case 'P': /* backward compatible */
72: p_path = optarg;
73: break;
74: case 'm':
75: p_augment = optarg;
76: break;
77: case '?':
78: default:
79: usage();
80: }
81: argv += optind;
82: argc -= optind;
83:
84: if (argc < 1)
85: usage();
86:
1.11 djm 87: if ((found = calloc(argc, sizeof(int))) == NULL)
1.1 deraadt 88: err(1, NULL);
89:
90: for (p = argv; *p; ++p) /* trim full paths */
1.5 millert 91: if ((beg = strrchr(*p, '/')))
1.1 deraadt 92: *p = beg + 1;
93:
94: if (p_augment)
95: whatis(argv, p_augment, 1);
96: if (p_path || (p_path = getenv("MANPATH")))
97: whatis(argv, p_path, 1);
98: else {
99: config(conffile);
100: ep = (tp = getlist("_whatdb")) == NULL ?
1.10 otto 101: NULL : TAILQ_FIRST(&tp->list);
102: for (; ep != NULL; ep = TAILQ_NEXT(ep, q))
1.1 deraadt 103: whatis(argv, ep->s, 0);
104: }
105:
106: if (!foundman) {
107: fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
108: exit(1);
109: }
110: rv = 1;
111: for (p = argv; *p; ++p)
112: if (found[p - argv])
113: rv = 0;
114: else
115: printf("%s: not found\n", *p);
116: exit(rv);
117: }
118:
119: void
1.9 deraadt 120: whatis(char **argv, char *path, int buildpath)
1.1 deraadt 121: {
1.6 mpech 122: char *end, *name, **p;
1.1 deraadt 123: char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
1.4 deraadt 124: char hold[MAXPATHLEN];
1.1 deraadt 125:
126: for (name = path; name; name = end) { /* through name list */
1.5 millert 127: if ((end = strchr(name, ':')))
1.1 deraadt 128: *end++ = '\0';
129:
130: if (buildpath) {
1.4 deraadt 131: (void)snprintf(hold, sizeof hold, "%s/%s",
132: name, _PATH_WHATIS);
1.1 deraadt 133: name = hold;
134: }
135:
136: if (!freopen(name, "r", stdin))
137: continue;
138:
139: foundman = 1;
140:
141: /* for each file found */
142: while (fgets(buf, sizeof(buf), stdin)) {
143: dashtrunc(buf, wbuf);
144: for (p = argv; *p; ++p)
145: if (match(wbuf, *p)) {
146: printf("%s", buf);
147: found[p - argv] = 1;
148:
149: /* only print line once */
150: while (*++p)
151: if (match(wbuf, *p))
152: found[p - argv] = 1;
153: break;
154: }
155: }
156: }
157: }
158:
159: /*
160: * match --
1.5 millert 161: * match a full word or a full string
1.1 deraadt 162: */
163: int
1.9 deraadt 164: match(char *bp, char *str)
1.1 deraadt 165: {
1.6 mpech 166: int len;
167: char *start;
1.1 deraadt 168:
169: if (!*str || !*bp)
170: return(0);
171: for (len = strlen(str);;) {
1.5 millert 172: /* skip leading crud */
1.15 ! deraadt 173: for (; *bp && !isalnum((unsigned char)*bp); ++bp)
1.5 millert 174: ;
1.1 deraadt 175: if (!*bp)
176: break;
1.5 millert 177:
178: /* check for word match first */
1.15 ! deraadt 179: for (start = bp++; *bp == '_' || isalnum((unsigned char)*bp); ++bp)
1.5 millert 180: ;
181: if (bp - start == len) {
182: if (strncasecmp(start, str, len) == 0)
183: return(1);
184: } else if (*bp && *bp != ',') {
185: /* check for full string match */
186: for (bp = start;
1.15 ! deraadt 187: *bp && *bp != ',' && *bp != '(' &&
! 188: !isspace((unsigned char)*bp); ++bp)
1.5 millert 189: ;
190: if (bp - start == len && strncasecmp(start, str, len) == 0)
191: return(1);
192: }
1.1 deraadt 193: }
194: return(0);
195: }
196:
197: /*
198: * dashtrunc --
199: * truncate a string at " - "
200: */
201: void
1.9 deraadt 202: dashtrunc(char *from, char *to)
1.1 deraadt 203: {
1.6 mpech 204: int ch;
1.1 deraadt 205:
206: for (; (ch = *from) && ch != '\n' &&
207: (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
208: *to++ = ch;
209: *to = '\0';
210: }
211:
212: /*
213: * usage --
214: * print usage message and die
215: */
216: void
1.9 deraadt 217: usage(void)
1.1 deraadt 218: {
1.5 millert 219:
1.1 deraadt 220: (void)fprintf(stderr,
1.5 millert 221: "usage: %s [-C file] [-M path] [-m path] command ...\n",
222: __progname);
1.1 deraadt 223: exit(1);
224: }