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*65272Sbostic static char sccsid[] = "@(#)whatis.c 8.2 (Berkeley) 01/02/94"; 1635535Sbostic #endif /* not lint */ 1735535Sbostic 1835535Sbostic #include <sys/param.h> 19*65272Sbostic #include <sys/queue.h> 20*65272Sbostic 21*65272Sbostic #include <ctype.h> 22*65272Sbostic #include <err.h> 2335535Sbostic #include <stdio.h> 24*65272Sbostic #include <stdlib.h> 2540387Sbostic #include <string.h> 26*65272Sbostic 27*65272Sbostic #include "../man/config.h" 2840387Sbostic #include "../man/pathnames.h" 2935535Sbostic 3040387Sbostic #define MAXLINELEN 256 /* max line handled */ 3135535Sbostic 3240387Sbostic char *progname; 3340387Sbostic 3442404Sbostic static int *found, foundman; 3542404Sbostic 3635535Sbostic main(argc, argv) 3735535Sbostic int argc; 3835535Sbostic char **argv; 3935535Sbostic { 4035535Sbostic extern char *optarg; 4135535Sbostic extern int optind; 42*65272Sbostic ENTRY *ep; 4340387Sbostic int ch; 44*65272Sbostic char *beg, **p, *p_augment, *p_path, **getdb(); 4535535Sbostic 4640387Sbostic progname = "whatis"; 4752147Sbostic p_augment = p_path = NULL; 4840387Sbostic while ((ch = getopt(argc, argv, "M:m:P:")) != EOF) 4935535Sbostic switch((char)ch) { 5035535Sbostic case 'M': 5135535Sbostic case 'P': /* backward compatible */ 5240387Sbostic p_path = optarg; 5335535Sbostic break; 5440387Sbostic case 'm': 5540387Sbostic p_augment = optarg; 5640387Sbostic break; 5735535Sbostic case '?': 5835535Sbostic default: 5935535Sbostic usage(); 6035535Sbostic } 6135535Sbostic argv += optind; 6235535Sbostic argc -= optind; 6340387Sbostic 6435535Sbostic if (argc < 1) 6535535Sbostic usage(); 6635535Sbostic 67*65272Sbostic if ((found = malloc((u_int)argc)) == NULL) 68*65272Sbostic err(1, NULL); 69*65272Sbostic memset(found, 0, argc * sizeof(int)); 7035535Sbostic 7135535Sbostic for (p = argv; *p; ++p) /* trim full paths */ 7235535Sbostic if (beg = rindex(*p, '/')) 7335535Sbostic *p = beg + 1; 7435535Sbostic 7540387Sbostic if (p_augment) 7642404Sbostic whatis(argv, p_augment, 1); 7742404Sbostic if (p_path || (p_path = getenv("MANPATH"))) 7842404Sbostic whatis(argv, p_path, 1); 79*65272Sbostic else { 80*65272Sbostic config(); 81*65272Sbostic ep = getlist("_whatdb"); 82*65272Sbostic if (ep != NULL) 83*65272Sbostic ep = ep->list.qe_next; 84*65272Sbostic for (; ep != NULL; ep = ep->list.qe_next) 85*65272Sbostic whatis(argv, ep->s, 0); 86*65272Sbostic } 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 9742404Sbostic whatis(argv, path, buildpath) 9840387Sbostic char **argv, *path; 9942404Sbostic int buildpath; 10040387Sbostic { 10142404Sbostic register char *end, *name, **p; 10240387Sbostic char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1]; 10340387Sbostic 10442404Sbostic for (name = path; name; name = end) { /* through name list */ 10542404Sbostic if (end = index(name, ':')) 10642404Sbostic *end++ = '\0'; 10742404Sbostic 10842404Sbostic if (buildpath) { 10942404Sbostic char hold[MAXPATHLEN + 1]; 11042404Sbostic 11142404Sbostic (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS); 11242404Sbostic name = hold; 11335535Sbostic } 11442404Sbostic 11542404Sbostic if (!freopen(name, "r", stdin)) 11635535Sbostic continue; 11735535Sbostic 11842404Sbostic foundman = 1; 11942404Sbostic 12035535Sbostic /* for each file found */ 12142404Sbostic 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