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*65273Sbostic static char sccsid[] = "@(#)whatis.c 8.3 (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 34*65273Sbostic int 3535535Sbostic main(argc, argv) 3635535Sbostic int argc; 37*65273Sbostic char *argv[]; 3835535Sbostic { 3935535Sbostic extern char *optarg; 4035535Sbostic extern int optind; 4165272Sbostic ENTRY *ep; 4240387Sbostic int ch; 43*65273Sbostic char *beg, *conffile, **p, *p_augment, *p_path; 4435535Sbostic 45*65273Sbostic conffile = NULL; 4652147Sbostic p_augment = p_path = NULL; 47*65273Sbostic while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF) 48*65273Sbostic switch (ch) { 49*65273Sbostic case 'C': 50*65273Sbostic conffile = optarg; 51*65273Sbostic 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 6965272Sbostic if ((found = malloc((u_int)argc)) == 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 { 82*65273Sbostic 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 } 9440387Sbostic for (p = argv; *p; ++p) 9540387Sbostic if (!found[p - argv]) 9640387Sbostic printf("%s: not found\n", *p); 9740387Sbostic } 9840387Sbostic 9942404Sbostic whatis(argv, path, buildpath) 10040387Sbostic char **argv, *path; 10142404Sbostic int buildpath; 10240387Sbostic { 10342404Sbostic register char *end, *name, **p; 10440387Sbostic char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1]; 10540387Sbostic 10642404Sbostic for (name = path; name; name = end) { /* through name list */ 10742404Sbostic if (end = index(name, ':')) 10842404Sbostic *end++ = '\0'; 10942404Sbostic 11042404Sbostic if (buildpath) { 11142404Sbostic char hold[MAXPATHLEN + 1]; 11242404Sbostic 11342404Sbostic (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS); 11442404Sbostic name = hold; 11535535Sbostic } 11642404Sbostic 11742404Sbostic if (!freopen(name, "r", stdin)) 11835535Sbostic continue; 11935535Sbostic 12042404Sbostic foundman = 1; 12142404Sbostic 12235535Sbostic /* for each file found */ 12342404Sbostic while (fgets(buf, sizeof(buf), stdin)) { 12435535Sbostic dashtrunc(buf, wbuf); 12535535Sbostic for (p = argv; *p; ++p) 12635535Sbostic if (match(wbuf, *p)) { 12735535Sbostic printf("%s", buf); 12835535Sbostic found[p - argv] = 1; 12935535Sbostic 13035535Sbostic /* only print line once */ 13135535Sbostic while (*++p) 13235535Sbostic if (match(wbuf, *p)) 13335535Sbostic found[p - argv] = 1; 13435535Sbostic break; 13535535Sbostic } 13635535Sbostic } 13735535Sbostic } 13835535Sbostic } 13935535Sbostic 14035535Sbostic /* 14135535Sbostic * match -- 14235535Sbostic * match a full word 14335535Sbostic */ 14435535Sbostic match(bp, str) 14535535Sbostic register char *bp, *str; 14635535Sbostic { 14735535Sbostic register int len; 14835535Sbostic register char *start; 14935535Sbostic 15035535Sbostic if (!*str || !*bp) 15135535Sbostic return(0); 15235535Sbostic for (len = strlen(str);;) { 15335535Sbostic for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp); 15435535Sbostic if (!*bp) 15535535Sbostic break; 15635536Sbostic for (start = bp++; 15735536Sbostic *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp); 15835535Sbostic if (bp - start == len && !strncasecmp(start, str, len)) 15935535Sbostic return(1); 16035535Sbostic } 16135535Sbostic return(0); 16235535Sbostic } 16335535Sbostic 16435535Sbostic /* 16535535Sbostic * dashtrunc -- 16635535Sbostic * truncate a string at " - " 16735535Sbostic */ 16835535Sbostic dashtrunc(from, to) 16935535Sbostic register char *from, *to; 17035535Sbostic { 17135535Sbostic register int ch; 17235535Sbostic 17335535Sbostic for (; (ch = *from) && ch != '\n' && 17435535Sbostic (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from) 17535535Sbostic *to++ = ch; 17635535Sbostic *to = '\0'; 17735535Sbostic } 17835535Sbostic 17935535Sbostic /* 18035535Sbostic * usage -- 18135535Sbostic * print usage message and die 18235535Sbostic */ 18335535Sbostic usage() 18435535Sbostic { 18540387Sbostic (void)fprintf(stderr, 186*65273Sbostic "usage: whatis [-C file] [-M path] [-m path] command ...\n"); 18735535Sbostic exit(1); 18835535Sbostic } 189