xref: /original-bsd/usr.bin/apropos/apropos.c (revision 2301fdfb)
1 /*
2  * Copyright (c) 1987 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1987 Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)apropos.c	5.7 (Berkeley) 09/18/88";
26 #endif /* not lint */
27 
28 #include <sys/param.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <strings.h>
32 
33 #define	DEF_PATH	"/usr/man:/usr/new/man:/usr/local/man"
34 #define	MAXLINELEN	1000			/* max line handled */
35 #define	WHATIS		"whatis"		/* database name */
36 
37 main(argc, argv)
38 	int argc;
39 	char **argv;
40 {
41 	extern char *optarg;
42 	extern int optind;
43 	register char *beg, *end, **p;
44 	int ch, foundman = 0, *found;
45 	char *manpath, buf[MAXLINELEN + 1], fname[MAXPATHLEN + 1];
46 	char wbuf[MAXLINELEN + 1], *getenv(), *malloc();
47 
48 	while ((ch = getopt(argc, argv, "M:P:")) != EOF)
49 		switch((char)ch) {
50 		case 'M':
51 		case 'P':		/* backward compatible */
52 			manpath = optarg;
53 			break;
54 		case '?':
55 		default:
56 			usage();
57 		}
58 	argv += optind;
59 	argc -= optind;
60 	if (argc < 1)
61 		usage();
62 
63 	if (!(manpath = getenv("MANPATH")))
64 		manpath = DEF_PATH;
65 
66 	/*NOSTRICT*/
67 	if (!(found = (int *)malloc((u_int)argc))) {
68 		fprintf(stderr, "apropos: out of space.\n");
69 		exit(1);
70 	}
71 	bzero((char *)found, argc * sizeof(int));
72 
73 	for (p = argv; *p; ++p)			/* convert to lower-case */
74 		lowstr(*p, *p);
75 	for (beg = manpath; beg; beg = end) {	/* through path list */
76 		end = index(beg, ':');
77 		if (!end)
78 			(void)sprintf(fname, "%s/%s", beg, WHATIS);
79 		else {
80 			(void)sprintf(fname, "%.*s/%s", end - beg, beg, WHATIS);
81 			++end;
82 		}
83 		if (!freopen(fname, "r", stdin))
84 			continue;
85 
86 		/* for each file found */
87 		for (foundman = 1; fgets(buf, sizeof(buf), stdin);) {
88 			lowstr(buf, wbuf);
89 			for (p = argv; *p; ++p)
90 				if (match(wbuf, *p)) {
91 					printf("%s", buf);
92 					found[p - argv] = 1;
93 
94 					/* only print line once */
95 					while (*++p)
96 						if (match(wbuf, *p))
97 							found[p - argv] = 1;
98 					break;
99 				}
100 		}
101 	}
102 	if (!foundman) {
103 		fprintf(stderr, "apropos: no %s file found in %s.\n",
104 		    WHATIS, manpath);
105 		exit(1);
106 	}
107 	for (p = argv; *p; ++p)
108 		if (!found[p - argv])
109 			printf("%s: nothing appropriate\n", *p);
110 }
111 
112 /*
113  * match --
114  *	match anywhere the string appears
115  */
116 match(bp, str)
117 	register char *bp, *str;
118 {
119 	register int len;
120 	register char test;
121 
122 	if (!*bp)
123 		return(0);
124 	/* backward compatible: everything matches empty string */
125 	if (!*str)
126 		return(1);
127 	for (test = *str++, len = strlen(str); *bp;)
128 		if (test == *bp++ && !strncmp(bp, str, len))
129 			return(1);
130 	return(0);
131 }
132 
133 /*
134  * lowstr --
135  *	convert a string to lower case
136  */
137 lowstr(from, to)
138 	register char *from, *to;
139 {
140 	register char ch;
141 
142 	while ((ch = *from++) && ch != '\n')
143 		*to++ = isupper(ch) ? tolower(ch) : ch;
144 	*to = '\0';
145 }
146 
147 /*
148  * usage --
149  *	print usage message and die
150  */
151 usage()
152 {
153 	fprintf(stderr, "usage: apropos [-M path] string ...\n");
154 	exit(1);
155 }
156