135535Sbostic /*
262442Sbostic * Copyright (c) 1987, 1993
362442Sbostic * The Regents of the University of California. All rights reserved.
435535Sbostic *
542741Sbostic * %sccs.include.redist.c%
635535Sbostic */
735535Sbostic
835535Sbostic #ifndef lint
962442Sbostic static char copyright[] =
1062442Sbostic "@(#) Copyright (c) 1987, 1993\n\
1162442Sbostic The Regents of the University of California. All rights reserved.\n";
1235535Sbostic #endif /* not lint */
1335535Sbostic
1435535Sbostic #ifndef lint
15*65275Sbostic static char sccsid[] = "@(#)whatis.c 8.5 (Berkeley) 01/02/94";
1635535Sbostic #endif /* not lint */
1735535Sbostic
1835535Sbostic #include <sys/param.h>
1965272Sbostic #include <sys/queue.h>
2065272Sbostic
2165272Sbostic #include <ctype.h>
2265272Sbostic #include <err.h>
2335535Sbostic #include <stdio.h>
2465272Sbostic #include <stdlib.h>
2540387Sbostic #include <string.h>
2665272Sbostic
2765272Sbostic #include "../man/config.h"
2840387Sbostic #include "../man/pathnames.h"
2935535Sbostic
3040387Sbostic #define MAXLINELEN 256 /* max line handled */
3135535Sbostic
3242404Sbostic static int *found, foundman;
3342404Sbostic
3465273Sbostic int
main(argc,argv)3535535Sbostic main(argc, argv)
3635535Sbostic int argc;
3765273Sbostic char *argv[];
3835535Sbostic {
3935535Sbostic extern char *optarg;
4035535Sbostic extern int optind;
4165272Sbostic ENTRY *ep;
42*65275Sbostic TAG *tp;
4365274Sbostic int ch, rv;
4465273Sbostic char *beg, *conffile, **p, *p_augment, *p_path;
4535535Sbostic
4665273Sbostic conffile = NULL;
4752147Sbostic p_augment = p_path = NULL;
4865273Sbostic while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF)
4965273Sbostic switch (ch) {
5065273Sbostic case 'C':
5165273Sbostic conffile = optarg;
5265273Sbostic break;
5335535Sbostic case 'M':
5435535Sbostic case 'P': /* backward compatible */
5540387Sbostic p_path = optarg;
5635535Sbostic break;
5740387Sbostic case 'm':
5840387Sbostic p_augment = optarg;
5940387Sbostic break;
6035535Sbostic case '?':
6135535Sbostic default:
6235535Sbostic usage();
6335535Sbostic }
6435535Sbostic argv += optind;
6535535Sbostic argc -= optind;
6640387Sbostic
6735535Sbostic if (argc < 1)
6835535Sbostic usage();
6935535Sbostic
7065274Sbostic if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
7165272Sbostic err(1, NULL);
7265272Sbostic memset(found, 0, argc * sizeof(int));
7335535Sbostic
7435535Sbostic for (p = argv; *p; ++p) /* trim full paths */
7535535Sbostic if (beg = rindex(*p, '/'))
7635535Sbostic *p = beg + 1;
7735535Sbostic
7840387Sbostic if (p_augment)
7942404Sbostic whatis(argv, p_augment, 1);
8042404Sbostic if (p_path || (p_path = getenv("MANPATH")))
8142404Sbostic whatis(argv, p_path, 1);
8265272Sbostic else {
8365273Sbostic config(conffile);
84*65275Sbostic ep = (tp = getlist("_whatdb")) == NULL ?
85*65275Sbostic NULL : tp->list.tqh_first;
86*65275Sbostic for (; ep != NULL; ep = ep->q.tqe_next)
8765272Sbostic whatis(argv, ep->s, 0);
8865272Sbostic }
8940387Sbostic
9040387Sbostic if (!foundman) {
9140387Sbostic fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
9240387Sbostic exit(1);
9340387Sbostic }
9465274Sbostic rv = 1;
9540387Sbostic for (p = argv; *p; ++p)
9665274Sbostic if (found[p - argv])
9765274Sbostic rv = 0;
9865274Sbostic else
9940387Sbostic printf("%s: not found\n", *p);
10065274Sbostic exit(rv);
10140387Sbostic }
10240387Sbostic
whatis(argv,path,buildpath)10342404Sbostic whatis(argv, path, buildpath)
10440387Sbostic char **argv, *path;
10542404Sbostic int buildpath;
10640387Sbostic {
10742404Sbostic register char *end, *name, **p;
10840387Sbostic char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
10940387Sbostic
11042404Sbostic for (name = path; name; name = end) { /* through name list */
11142404Sbostic if (end = index(name, ':'))
11242404Sbostic *end++ = '\0';
11342404Sbostic
11442404Sbostic if (buildpath) {
11542404Sbostic char hold[MAXPATHLEN + 1];
11642404Sbostic
11742404Sbostic (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
11842404Sbostic name = hold;
11935535Sbostic }
12042404Sbostic
12142404Sbostic if (!freopen(name, "r", stdin))
12235535Sbostic continue;
12335535Sbostic
12442404Sbostic foundman = 1;
12542404Sbostic
12635535Sbostic /* for each file found */
12742404Sbostic while (fgets(buf, sizeof(buf), stdin)) {
12835535Sbostic dashtrunc(buf, wbuf);
12935535Sbostic for (p = argv; *p; ++p)
13035535Sbostic if (match(wbuf, *p)) {
13135535Sbostic printf("%s", buf);
13235535Sbostic found[p - argv] = 1;
13335535Sbostic
13435535Sbostic /* only print line once */
13535535Sbostic while (*++p)
13635535Sbostic if (match(wbuf, *p))
13735535Sbostic found[p - argv] = 1;
13835535Sbostic break;
13935535Sbostic }
14035535Sbostic }
14135535Sbostic }
14235535Sbostic }
14335535Sbostic
14435535Sbostic /*
14535535Sbostic * match --
14635535Sbostic * match a full word
14735535Sbostic */
match(bp,str)14835535Sbostic match(bp, str)
14935535Sbostic register char *bp, *str;
15035535Sbostic {
15135535Sbostic register int len;
15235535Sbostic register char *start;
15335535Sbostic
15435535Sbostic if (!*str || !*bp)
15535535Sbostic return(0);
15635535Sbostic for (len = strlen(str);;) {
15735535Sbostic for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
15835535Sbostic if (!*bp)
15935535Sbostic break;
16035536Sbostic for (start = bp++;
16135536Sbostic *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
16235535Sbostic if (bp - start == len && !strncasecmp(start, str, len))
16335535Sbostic return(1);
16435535Sbostic }
16535535Sbostic return(0);
16635535Sbostic }
16735535Sbostic
16835535Sbostic /*
16935535Sbostic * dashtrunc --
17035535Sbostic * truncate a string at " - "
17135535Sbostic */
dashtrunc(from,to)17235535Sbostic dashtrunc(from, to)
17335535Sbostic register char *from, *to;
17435535Sbostic {
17535535Sbostic register int ch;
17635535Sbostic
17735535Sbostic for (; (ch = *from) && ch != '\n' &&
17835535Sbostic (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
17935535Sbostic *to++ = ch;
18035535Sbostic *to = '\0';
18135535Sbostic }
18235535Sbostic
18335535Sbostic /*
18435535Sbostic * usage --
18535535Sbostic * print usage message and die
18635535Sbostic */
usage()18735535Sbostic usage()
18835535Sbostic {
18940387Sbostic (void)fprintf(stderr,
19065273Sbostic "usage: whatis [-C file] [-M path] [-m path] command ...\n");
19135535Sbostic exit(1);
19235535Sbostic }
193