xref: /csrg-svn/usr.bin/whatis/whatis.c (revision 42404)
135535Sbostic /*
235535Sbostic  * Copyright (c) 1987 Regents of the University of California.
335535Sbostic  * All rights reserved.
435535Sbostic  *
535535Sbostic  * Redistribution and use in source and binary forms are permitted
635535Sbostic  * provided that the above copyright notice and this paragraph are
735535Sbostic  * duplicated in all such forms and that any documentation,
835535Sbostic  * advertising materials, and other materials related to such
935535Sbostic  * distribution and use acknowledge that the software was developed
1035535Sbostic  * by the University of California, Berkeley.  The name of the
1135535Sbostic  * University may not be used to endorse or promote products derived
1235535Sbostic  * from this software without specific prior written permission.
1335535Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1435535Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1535535Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1635535Sbostic  */
1735535Sbostic 
1835535Sbostic #ifndef lint
1935535Sbostic char copyright[] =
2035535Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\
2135535Sbostic  All rights reserved.\n";
2235535Sbostic #endif /* not lint */
2335535Sbostic 
2435535Sbostic #ifndef lint
25*42404Sbostic static char sccsid[] = "@(#)whatis.c	5.5 (Berkeley) 05/27/90";
2635535Sbostic #endif /* not lint */
2735535Sbostic 
2835535Sbostic #include <sys/param.h>
2935535Sbostic #include <stdio.h>
3035535Sbostic #include <ctype.h>
3140387Sbostic #include <string.h>
32*42404Sbostic #include <stdlib.h>
3340387Sbostic #include "../man/pathnames.h"
3435535Sbostic 
3540387Sbostic #define	MAXLINELEN	256			/* max line handled */
3635535Sbostic 
3740387Sbostic char *progname;
3840387Sbostic 
39*42404Sbostic static int *found, foundman;
40*42404Sbostic 
4135535Sbostic main(argc, argv)
4235535Sbostic 	int argc;
4335535Sbostic 	char **argv;
4435535Sbostic {
4535535Sbostic 	extern char *optarg;
4635535Sbostic 	extern int optind;
4740387Sbostic 	register char *beg, **p;
4840387Sbostic 	int ch;
49*42404Sbostic 	char *p_augment, *p_path, **getdb();
5035535Sbostic 
5140387Sbostic 	progname = "whatis";
5240387Sbostic 	while ((ch = getopt(argc, argv, "M:m:P:")) != EOF)
5335535Sbostic 		switch((char)ch) {
5435535Sbostic 		case 'M':
5535535Sbostic 		case 'P':		/* backward compatible */
5640387Sbostic 			p_path = optarg;
5735535Sbostic 			break;
5840387Sbostic 		case 'm':
5940387Sbostic 			p_augment = optarg;
6040387Sbostic 			break;
6135535Sbostic 		case '?':
6235535Sbostic 		default:
6335535Sbostic 			usage();
6435535Sbostic 		}
6535535Sbostic 	argv += optind;
6635535Sbostic 	argc -= optind;
6740387Sbostic 
6835535Sbostic 	if (argc < 1)
6935535Sbostic 		usage();
7035535Sbostic 
7135535Sbostic 	/*NOSTRICT*/
7240387Sbostic 	if (!(found = (int *)malloc((u_int)argc)))
7340387Sbostic 		enomem();
7435535Sbostic 	bzero((char *)found, argc * sizeof(int));
7535535Sbostic 
7635535Sbostic 	for (p = argv; *p; ++p)			/* trim full paths */
7735535Sbostic 		if (beg = rindex(*p, '/'))
7835535Sbostic 			*p = beg + 1;
7935535Sbostic 
8040387Sbostic 	if (p_augment)
81*42404Sbostic 		whatis(argv, p_augment, 1);
82*42404Sbostic 	if (p_path || (p_path = getenv("MANPATH")))
83*42404Sbostic 		whatis(argv, p_path, 1);
84*42404Sbostic 	else
85*42404Sbostic 		for (p = getdb(); *p; ++p)
86*42404Sbostic 			whatis(argv, *p, 0);
8740387Sbostic 
8840387Sbostic 	if (!foundman) {
8940387Sbostic 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
9040387Sbostic 		exit(1);
9140387Sbostic 	}
9240387Sbostic 	for (p = argv; *p; ++p)
9340387Sbostic 		if (!found[p - argv])
9440387Sbostic 			printf("%s: not found\n", *p);
9540387Sbostic }
9640387Sbostic 
97*42404Sbostic whatis(argv, path, buildpath)
9840387Sbostic 	char **argv, *path;
99*42404Sbostic 	int buildpath;
10040387Sbostic {
101*42404Sbostic 	register char *end, *name, **p;
10240387Sbostic 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
10340387Sbostic 
104*42404Sbostic 	for (name = path; name; name = end) {	/* through name list */
105*42404Sbostic 		if (end = index(name, ':'))
106*42404Sbostic 			*end++ = '\0';
107*42404Sbostic 
108*42404Sbostic 		if (buildpath) {
109*42404Sbostic 			char hold[MAXPATHLEN + 1];
110*42404Sbostic 
111*42404Sbostic 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
112*42404Sbostic 			name = hold;
11335535Sbostic 		}
114*42404Sbostic 
115*42404Sbostic 		if (!freopen(name, "r", stdin))
11635535Sbostic 			continue;
11735535Sbostic 
118*42404Sbostic 		foundman = 1;
119*42404Sbostic 
12035535Sbostic 		/* for each file found */
121*42404Sbostic 		while (fgets(buf, sizeof(buf), stdin)) {
12235535Sbostic 			dashtrunc(buf, wbuf);
12335535Sbostic 			for (p = argv; *p; ++p)
12435535Sbostic 				if (match(wbuf, *p)) {
12535535Sbostic 					printf("%s", buf);
12635535Sbostic 					found[p - argv] = 1;
12735535Sbostic 
12835535Sbostic 					/* only print line once */
12935535Sbostic 					while (*++p)
13035535Sbostic 						if (match(wbuf, *p))
13135535Sbostic 							found[p - argv] = 1;
13235535Sbostic 					break;
13335535Sbostic 				}
13435535Sbostic 		}
13535535Sbostic 	}
13635535Sbostic }
13735535Sbostic 
13835535Sbostic /*
13935535Sbostic  * match --
14035535Sbostic  *	match a full word
14135535Sbostic  */
14235535Sbostic match(bp, str)
14335535Sbostic 	register char *bp, *str;
14435535Sbostic {
14535535Sbostic 	register int len;
14635535Sbostic 	register char *start;
14735535Sbostic 
14835535Sbostic 	if (!*str || !*bp)
14935535Sbostic 		return(0);
15035535Sbostic 	for (len = strlen(str);;) {
15135535Sbostic 		for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
15235535Sbostic 		if (!*bp)
15335535Sbostic 			break;
15435536Sbostic 		for (start = bp++;
15535536Sbostic 		    *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
15635535Sbostic 		if (bp - start == len && !strncasecmp(start, str, len))
15735535Sbostic 			return(1);
15835535Sbostic 	}
15935535Sbostic 	return(0);
16035535Sbostic }
16135535Sbostic 
16235535Sbostic /*
16335535Sbostic  * dashtrunc --
16435535Sbostic  *	truncate a string at " - "
16535535Sbostic  */
16635535Sbostic dashtrunc(from, to)
16735535Sbostic 	register char *from, *to;
16835535Sbostic {
16935535Sbostic 	register int ch;
17035535Sbostic 
17135535Sbostic 	for (; (ch = *from) && ch != '\n' &&
17235535Sbostic 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
17335535Sbostic 		*to++ = ch;
17435535Sbostic 	*to = '\0';
17535535Sbostic }
17635535Sbostic 
17735535Sbostic /*
17835535Sbostic  * usage --
17935535Sbostic  *	print usage message and die
18035535Sbostic  */
18135535Sbostic usage()
18235535Sbostic {
18340387Sbostic 	(void)fprintf(stderr,
18440387Sbostic 	    "usage: whatis [-M path] [-m path] command ...\n");
18535535Sbostic 	exit(1);
18635535Sbostic }
187