xref: /csrg-svn/usr.bin/whatis/whatis.c (revision 42741)
135535Sbostic /*
235535Sbostic  * Copyright (c) 1987 Regents of the University of California.
335535Sbostic  * All rights reserved.
435535Sbostic  *
5*42741Sbostic  * %sccs.include.redist.c%
635535Sbostic  */
735535Sbostic 
835535Sbostic #ifndef lint
935535Sbostic char copyright[] =
1035535Sbostic "@(#) Copyright (c) 1987 Regents of the University of California.\n\
1135535Sbostic  All rights reserved.\n";
1235535Sbostic #endif /* not lint */
1335535Sbostic 
1435535Sbostic #ifndef lint
15*42741Sbostic static char sccsid[] = "@(#)whatis.c	5.6 (Berkeley) 06/01/90";
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";
4240387Sbostic 	while ((ch = getopt(argc, argv, "M:m:P:")) != EOF)
4335535Sbostic 		switch((char)ch) {
4435535Sbostic 		case 'M':
4535535Sbostic 		case 'P':		/* backward compatible */
4640387Sbostic 			p_path = optarg;
4735535Sbostic 			break;
4840387Sbostic 		case 'm':
4940387Sbostic 			p_augment = optarg;
5040387Sbostic 			break;
5135535Sbostic 		case '?':
5235535Sbostic 		default:
5335535Sbostic 			usage();
5435535Sbostic 		}
5535535Sbostic 	argv += optind;
5635535Sbostic 	argc -= optind;
5740387Sbostic 
5835535Sbostic 	if (argc < 1)
5935535Sbostic 		usage();
6035535Sbostic 
6135535Sbostic 	/*NOSTRICT*/
6240387Sbostic 	if (!(found = (int *)malloc((u_int)argc)))
6340387Sbostic 		enomem();
6435535Sbostic 	bzero((char *)found, argc * sizeof(int));
6535535Sbostic 
6635535Sbostic 	for (p = argv; *p; ++p)			/* trim full paths */
6735535Sbostic 		if (beg = rindex(*p, '/'))
6835535Sbostic 			*p = beg + 1;
6935535Sbostic 
7040387Sbostic 	if (p_augment)
7142404Sbostic 		whatis(argv, p_augment, 1);
7242404Sbostic 	if (p_path || (p_path = getenv("MANPATH")))
7342404Sbostic 		whatis(argv, p_path, 1);
7442404Sbostic 	else
7542404Sbostic 		for (p = getdb(); *p; ++p)
7642404Sbostic 			whatis(argv, *p, 0);
7740387Sbostic 
7840387Sbostic 	if (!foundman) {
7940387Sbostic 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
8040387Sbostic 		exit(1);
8140387Sbostic 	}
8240387Sbostic 	for (p = argv; *p; ++p)
8340387Sbostic 		if (!found[p - argv])
8440387Sbostic 			printf("%s: not found\n", *p);
8540387Sbostic }
8640387Sbostic 
8742404Sbostic whatis(argv, path, buildpath)
8840387Sbostic 	char **argv, *path;
8942404Sbostic 	int buildpath;
9040387Sbostic {
9142404Sbostic 	register char *end, *name, **p;
9240387Sbostic 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
9340387Sbostic 
9442404Sbostic 	for (name = path; name; name = end) {	/* through name list */
9542404Sbostic 		if (end = index(name, ':'))
9642404Sbostic 			*end++ = '\0';
9742404Sbostic 
9842404Sbostic 		if (buildpath) {
9942404Sbostic 			char hold[MAXPATHLEN + 1];
10042404Sbostic 
10142404Sbostic 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
10242404Sbostic 			name = hold;
10335535Sbostic 		}
10442404Sbostic 
10542404Sbostic 		if (!freopen(name, "r", stdin))
10635535Sbostic 			continue;
10735535Sbostic 
10842404Sbostic 		foundman = 1;
10942404Sbostic 
11035535Sbostic 		/* for each file found */
11142404Sbostic 		while (fgets(buf, sizeof(buf), stdin)) {
11235535Sbostic 			dashtrunc(buf, wbuf);
11335535Sbostic 			for (p = argv; *p; ++p)
11435535Sbostic 				if (match(wbuf, *p)) {
11535535Sbostic 					printf("%s", buf);
11635535Sbostic 					found[p - argv] = 1;
11735535Sbostic 
11835535Sbostic 					/* only print line once */
11935535Sbostic 					while (*++p)
12035535Sbostic 						if (match(wbuf, *p))
12135535Sbostic 							found[p - argv] = 1;
12235535Sbostic 					break;
12335535Sbostic 				}
12435535Sbostic 		}
12535535Sbostic 	}
12635535Sbostic }
12735535Sbostic 
12835535Sbostic /*
12935535Sbostic  * match --
13035535Sbostic  *	match a full word
13135535Sbostic  */
13235535Sbostic match(bp, str)
13335535Sbostic 	register char *bp, *str;
13435535Sbostic {
13535535Sbostic 	register int len;
13635535Sbostic 	register char *start;
13735535Sbostic 
13835535Sbostic 	if (!*str || !*bp)
13935535Sbostic 		return(0);
14035535Sbostic 	for (len = strlen(str);;) {
14135535Sbostic 		for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
14235535Sbostic 		if (!*bp)
14335535Sbostic 			break;
14435536Sbostic 		for (start = bp++;
14535536Sbostic 		    *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
14635535Sbostic 		if (bp - start == len && !strncasecmp(start, str, len))
14735535Sbostic 			return(1);
14835535Sbostic 	}
14935535Sbostic 	return(0);
15035535Sbostic }
15135535Sbostic 
15235535Sbostic /*
15335535Sbostic  * dashtrunc --
15435535Sbostic  *	truncate a string at " - "
15535535Sbostic  */
15635535Sbostic dashtrunc(from, to)
15735535Sbostic 	register char *from, *to;
15835535Sbostic {
15935535Sbostic 	register int ch;
16035535Sbostic 
16135535Sbostic 	for (; (ch = *from) && ch != '\n' &&
16235535Sbostic 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
16335535Sbostic 		*to++ = ch;
16435535Sbostic 	*to = '\0';
16535535Sbostic }
16635535Sbostic 
16735535Sbostic /*
16835535Sbostic  * usage --
16935535Sbostic  *	print usage message and die
17035535Sbostic  */
17135535Sbostic usage()
17235535Sbostic {
17340387Sbostic 	(void)fprintf(stderr,
17440387Sbostic 	    "usage: whatis [-M path] [-m path] command ...\n");
17535535Sbostic 	exit(1);
17635535Sbostic }
177