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*65274Sbostic static char sccsid[] = "@(#)whatis.c 8.4 (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 3535535Sbostic main(argc, argv) 3635535Sbostic int argc; 3765273Sbostic char *argv[]; 3835535Sbostic { 3935535Sbostic extern char *optarg; 4035535Sbostic extern int optind; 4165272Sbostic ENTRY *ep; 42*65274Sbostic int ch, rv; 4365273Sbostic char *beg, *conffile, **p, *p_augment, *p_path; 4435535Sbostic 4565273Sbostic conffile = NULL; 4652147Sbostic p_augment = p_path = NULL; 4765273Sbostic while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF) 4865273Sbostic switch (ch) { 4965273Sbostic case 'C': 5065273Sbostic conffile = optarg; 5165273Sbostic break; 5235535Sbostic case 'M': 5335535Sbostic case 'P': /* backward compatible */ 5440387Sbostic p_path = optarg; 5535535Sbostic break; 5640387Sbostic case 'm': 5740387Sbostic p_augment = optarg; 5840387Sbostic break; 5935535Sbostic case '?': 6035535Sbostic default: 6135535Sbostic usage(); 6235535Sbostic } 6335535Sbostic argv += optind; 6435535Sbostic argc -= optind; 6540387Sbostic 6635535Sbostic if (argc < 1) 6735535Sbostic usage(); 6835535Sbostic 69*65274Sbostic if ((found = malloc((u_int)argc * sizeof(int))) == NULL) 7065272Sbostic err(1, NULL); 7165272Sbostic memset(found, 0, argc * sizeof(int)); 7235535Sbostic 7335535Sbostic for (p = argv; *p; ++p) /* trim full paths */ 7435535Sbostic if (beg = rindex(*p, '/')) 7535535Sbostic *p = beg + 1; 7635535Sbostic 7740387Sbostic if (p_augment) 7842404Sbostic whatis(argv, p_augment, 1); 7942404Sbostic if (p_path || (p_path = getenv("MANPATH"))) 8042404Sbostic whatis(argv, p_path, 1); 8165272Sbostic else { 8265273Sbostic config(conffile); 8365272Sbostic ep = getlist("_whatdb"); 8465272Sbostic if (ep != NULL) 8565272Sbostic ep = ep->list.qe_next; 8665272Sbostic for (; ep != NULL; ep = ep->list.qe_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 } 94*65274Sbostic rv = 1; 9540387Sbostic for (p = argv; *p; ++p) 96*65274Sbostic if (found[p - argv]) 97*65274Sbostic rv = 0; 98*65274Sbostic else 9940387Sbostic printf("%s: not found\n", *p); 100*65274Sbostic exit(rv); 10140387Sbostic } 10240387Sbostic 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 */ 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 */ 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 */ 18735535Sbostic usage() 18835535Sbostic { 18940387Sbostic (void)fprintf(stderr, 19065273Sbostic "usage: whatis [-C file] [-M path] [-m path] command ...\n"); 19135535Sbostic exit(1); 19235535Sbostic } 193