xref: /csrg-svn/usr.bin/whatis/whatis.c (revision 62442)
135535Sbostic /*
2*62442Sbostic  * Copyright (c) 1987, 1993
3*62442Sbostic  *	The Regents of the University of California.  All rights reserved.
435535Sbostic  *
542741Sbostic  * %sccs.include.redist.c%
635535Sbostic  */
735535Sbostic 
835535Sbostic #ifndef lint
9*62442Sbostic static char copyright[] =
10*62442Sbostic "@(#) Copyright (c) 1987, 1993\n\
11*62442Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1235535Sbostic #endif /* not lint */
1335535Sbostic 
1435535Sbostic #ifndef lint
15*62442Sbostic static char sccsid[] = "@(#)whatis.c	8.1 (Berkeley) 06/06/93";
1635535Sbostic #endif /* not lint */
1735535Sbostic 
1835535Sbostic #include <sys/param.h>
1935535Sbostic #include <stdio.h>
2035535Sbostic #include <ctype.h>
2140387Sbostic #include <string.h>
2242404Sbostic #include <stdlib.h>
2340387Sbostic #include "../man/pathnames.h"
2435535Sbostic 
2540387Sbostic #define	MAXLINELEN	256			/* max line handled */
2635535Sbostic 
2740387Sbostic char *progname;
2840387Sbostic 
2942404Sbostic static int *found, foundman;
3042404Sbostic 
3135535Sbostic main(argc, argv)
3235535Sbostic 	int argc;
3335535Sbostic 	char **argv;
3435535Sbostic {
3535535Sbostic 	extern char *optarg;
3635535Sbostic 	extern int optind;
3740387Sbostic 	register char *beg, **p;
3840387Sbostic 	int ch;
3942404Sbostic 	char *p_augment, *p_path, **getdb();
4035535Sbostic 
4140387Sbostic 	progname = "whatis";
4252147Sbostic 	p_augment = p_path = NULL;
4340387Sbostic 	while ((ch = getopt(argc, argv, "M:m:P:")) != EOF)
4435535Sbostic 		switch((char)ch) {
4535535Sbostic 		case 'M':
4635535Sbostic 		case 'P':		/* backward compatible */
4740387Sbostic 			p_path = optarg;
4835535Sbostic 			break;
4940387Sbostic 		case 'm':
5040387Sbostic 			p_augment = optarg;
5140387Sbostic 			break;
5235535Sbostic 		case '?':
5335535Sbostic 		default:
5435535Sbostic 			usage();
5535535Sbostic 		}
5635535Sbostic 	argv += optind;
5735535Sbostic 	argc -= optind;
5840387Sbostic 
5935535Sbostic 	if (argc < 1)
6035535Sbostic 		usage();
6135535Sbostic 
6235535Sbostic 	/*NOSTRICT*/
6340387Sbostic 	if (!(found = (int *)malloc((u_int)argc)))
6440387Sbostic 		enomem();
6535535Sbostic 	bzero((char *)found, argc * sizeof(int));
6635535Sbostic 
6735535Sbostic 	for (p = argv; *p; ++p)			/* trim full paths */
6835535Sbostic 		if (beg = rindex(*p, '/'))
6935535Sbostic 			*p = beg + 1;
7035535Sbostic 
7140387Sbostic 	if (p_augment)
7242404Sbostic 		whatis(argv, p_augment, 1);
7342404Sbostic 	if (p_path || (p_path = getenv("MANPATH")))
7442404Sbostic 		whatis(argv, p_path, 1);
7542404Sbostic 	else
7642404Sbostic 		for (p = getdb(); *p; ++p)
7742404Sbostic 			whatis(argv, *p, 0);
7840387Sbostic 
7940387Sbostic 	if (!foundman) {
8040387Sbostic 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
8140387Sbostic 		exit(1);
8240387Sbostic 	}
8340387Sbostic 	for (p = argv; *p; ++p)
8440387Sbostic 		if (!found[p - argv])
8540387Sbostic 			printf("%s: not found\n", *p);
8640387Sbostic }
8740387Sbostic 
8842404Sbostic whatis(argv, path, buildpath)
8940387Sbostic 	char **argv, *path;
9042404Sbostic 	int buildpath;
9140387Sbostic {
9242404Sbostic 	register char *end, *name, **p;
9340387Sbostic 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
9440387Sbostic 
9542404Sbostic 	for (name = path; name; name = end) {	/* through name list */
9642404Sbostic 		if (end = index(name, ':'))
9742404Sbostic 			*end++ = '\0';
9842404Sbostic 
9942404Sbostic 		if (buildpath) {
10042404Sbostic 			char hold[MAXPATHLEN + 1];
10142404Sbostic 
10242404Sbostic 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
10342404Sbostic 			name = hold;
10435535Sbostic 		}
10542404Sbostic 
10642404Sbostic 		if (!freopen(name, "r", stdin))
10735535Sbostic 			continue;
10835535Sbostic 
10942404Sbostic 		foundman = 1;
11042404Sbostic 
11135535Sbostic 		/* for each file found */
11242404Sbostic 		while (fgets(buf, sizeof(buf), stdin)) {
11335535Sbostic 			dashtrunc(buf, wbuf);
11435535Sbostic 			for (p = argv; *p; ++p)
11535535Sbostic 				if (match(wbuf, *p)) {
11635535Sbostic 					printf("%s", buf);
11735535Sbostic 					found[p - argv] = 1;
11835535Sbostic 
11935535Sbostic 					/* only print line once */
12035535Sbostic 					while (*++p)
12135535Sbostic 						if (match(wbuf, *p))
12235535Sbostic 							found[p - argv] = 1;
12335535Sbostic 					break;
12435535Sbostic 				}
12535535Sbostic 		}
12635535Sbostic 	}
12735535Sbostic }
12835535Sbostic 
12935535Sbostic /*
13035535Sbostic  * match --
13135535Sbostic  *	match a full word
13235535Sbostic  */
13335535Sbostic match(bp, str)
13435535Sbostic 	register char *bp, *str;
13535535Sbostic {
13635535Sbostic 	register int len;
13735535Sbostic 	register char *start;
13835535Sbostic 
13935535Sbostic 	if (!*str || !*bp)
14035535Sbostic 		return(0);
14135535Sbostic 	for (len = strlen(str);;) {
14235535Sbostic 		for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
14335535Sbostic 		if (!*bp)
14435535Sbostic 			break;
14535536Sbostic 		for (start = bp++;
14635536Sbostic 		    *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
14735535Sbostic 		if (bp - start == len && !strncasecmp(start, str, len))
14835535Sbostic 			return(1);
14935535Sbostic 	}
15035535Sbostic 	return(0);
15135535Sbostic }
15235535Sbostic 
15335535Sbostic /*
15435535Sbostic  * dashtrunc --
15535535Sbostic  *	truncate a string at " - "
15635535Sbostic  */
15735535Sbostic dashtrunc(from, to)
15835535Sbostic 	register char *from, *to;
15935535Sbostic {
16035535Sbostic 	register int ch;
16135535Sbostic 
16235535Sbostic 	for (; (ch = *from) && ch != '\n' &&
16335535Sbostic 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
16435535Sbostic 		*to++ = ch;
16535535Sbostic 	*to = '\0';
16635535Sbostic }
16735535Sbostic 
16835535Sbostic /*
16935535Sbostic  * usage --
17035535Sbostic  *	print usage message and die
17135535Sbostic  */
17235535Sbostic usage()
17335535Sbostic {
17440387Sbostic 	(void)fprintf(stderr,
17540387Sbostic 	    "usage: whatis [-M path] [-m path] command ...\n");
17635535Sbostic 	exit(1);
17735535Sbostic }
178