xref: /csrg-svn/usr.bin/whatis/whatis.c (revision 65272)
1 /*
2  * Copyright (c) 1987, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1987, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)whatis.c	8.2 (Berkeley) 01/02/94";
16 #endif /* not lint */
17 
18 #include <sys/param.h>
19 #include <sys/queue.h>
20 
21 #include <ctype.h>
22 #include <err.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 
27 #include "../man/config.h"
28 #include "../man/pathnames.h"
29 
30 #define	MAXLINELEN	256			/* max line handled */
31 
32 char *progname;
33 
34 static int *found, foundman;
35 
36 main(argc, argv)
37 	int argc;
38 	char **argv;
39 {
40 	extern char *optarg;
41 	extern int optind;
42 	ENTRY *ep;
43 	int ch;
44 	char *beg, **p, *p_augment, *p_path, **getdb();
45 
46 	progname = "whatis";
47 	p_augment = p_path = NULL;
48 	while ((ch = getopt(argc, argv, "M:m:P:")) != EOF)
49 		switch((char)ch) {
50 		case 'M':
51 		case 'P':		/* backward compatible */
52 			p_path = optarg;
53 			break;
54 		case 'm':
55 			p_augment = optarg;
56 			break;
57 		case '?':
58 		default:
59 			usage();
60 		}
61 	argv += optind;
62 	argc -= optind;
63 
64 	if (argc < 1)
65 		usage();
66 
67 	if ((found = malloc((u_int)argc)) == NULL)
68 		err(1, NULL);
69 	memset(found, 0, argc * sizeof(int));
70 
71 	for (p = argv; *p; ++p)			/* trim full paths */
72 		if (beg = rindex(*p, '/'))
73 			*p = beg + 1;
74 
75 	if (p_augment)
76 		whatis(argv, p_augment, 1);
77 	if (p_path || (p_path = getenv("MANPATH")))
78 		whatis(argv, p_path, 1);
79 	else {
80 		config();
81 		ep = getlist("_whatdb");
82 		if (ep != NULL)
83 			ep = ep->list.qe_next;
84 		for (; ep != NULL; ep = ep->list.qe_next)
85 			whatis(argv, ep->s, 0);
86 	}
87 
88 	if (!foundman) {
89 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
90 		exit(1);
91 	}
92 	for (p = argv; *p; ++p)
93 		if (!found[p - argv])
94 			printf("%s: not found\n", *p);
95 }
96 
97 whatis(argv, path, buildpath)
98 	char **argv, *path;
99 	int buildpath;
100 {
101 	register char *end, *name, **p;
102 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
103 
104 	for (name = path; name; name = end) {	/* through name list */
105 		if (end = index(name, ':'))
106 			*end++ = '\0';
107 
108 		if (buildpath) {
109 			char hold[MAXPATHLEN + 1];
110 
111 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
112 			name = hold;
113 		}
114 
115 		if (!freopen(name, "r", stdin))
116 			continue;
117 
118 		foundman = 1;
119 
120 		/* for each file found */
121 		while (fgets(buf, sizeof(buf), stdin)) {
122 			dashtrunc(buf, wbuf);
123 			for (p = argv; *p; ++p)
124 				if (match(wbuf, *p)) {
125 					printf("%s", buf);
126 					found[p - argv] = 1;
127 
128 					/* only print line once */
129 					while (*++p)
130 						if (match(wbuf, *p))
131 							found[p - argv] = 1;
132 					break;
133 				}
134 		}
135 	}
136 }
137 
138 /*
139  * match --
140  *	match a full word
141  */
142 match(bp, str)
143 	register char *bp, *str;
144 {
145 	register int len;
146 	register char *start;
147 
148 	if (!*str || !*bp)
149 		return(0);
150 	for (len = strlen(str);;) {
151 		for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
152 		if (!*bp)
153 			break;
154 		for (start = bp++;
155 		    *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
156 		if (bp - start == len && !strncasecmp(start, str, len))
157 			return(1);
158 	}
159 	return(0);
160 }
161 
162 /*
163  * dashtrunc --
164  *	truncate a string at " - "
165  */
166 dashtrunc(from, to)
167 	register char *from, *to;
168 {
169 	register int ch;
170 
171 	for (; (ch = *from) && ch != '\n' &&
172 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
173 		*to++ = ch;
174 	*to = '\0';
175 }
176 
177 /*
178  * usage --
179  *	print usage message and die
180  */
181 usage()
182 {
183 	(void)fprintf(stderr,
184 	    "usage: whatis [-M path] [-m path] command ...\n");
185 	exit(1);
186 }
187