1 /* $NetBSD: whatis.c,v 1.3 1997/01/09 12:03:22 tls Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1987, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)whatis.c 8.5 (Berkeley) 1/2/94"; 45 #else 46 static char rcsid[] = "$NetBSD: whatis.c,v 1.3 1997/01/09 12:03:22 tls Exp $"; 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/queue.h> 52 53 #include <ctype.h> 54 #include <err.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 59 #include "../man/config.h" 60 #include "../man/pathnames.h" 61 62 #define MAXLINELEN 256 /* max line handled */ 63 64 static int *found, foundman; 65 66 void dashtrunc __P((char *, char *)); 67 int match __P((char *, char *)); 68 void usage __P((void)); 69 void whatis __P((char **, char *, int)); 70 71 int 72 main(argc, argv) 73 int argc; 74 char *argv[]; 75 { 76 ENTRY *ep; 77 TAG *tp; 78 int ch, rv; 79 char *beg, *conffile, **p, *p_augment, *p_path; 80 81 conffile = NULL; 82 p_augment = p_path = NULL; 83 while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF) 84 switch (ch) { 85 case 'C': 86 conffile = optarg; 87 break; 88 case 'M': 89 case 'P': /* backward compatible */ 90 p_path = optarg; 91 break; 92 case 'm': 93 p_augment = optarg; 94 break; 95 case '?': 96 default: 97 usage(); 98 } 99 argv += optind; 100 argc -= optind; 101 102 if (argc < 1) 103 usage(); 104 105 if ((found = malloc((u_int)argc * sizeof(int))) == NULL) 106 err(1, NULL); 107 memset(found, 0, argc * sizeof(int)); 108 109 for (p = argv; *p; ++p) /* trim full paths */ 110 if (beg = strrchr(*p, '/')) 111 *p = beg + 1; 112 113 if (p_augment) 114 whatis(argv, p_augment, 1); 115 if (p_path || (p_path = getenv("MANPATH"))) 116 whatis(argv, p_path, 1); 117 else { 118 config(conffile); 119 ep = (tp = getlist("_whatdb")) == NULL ? 120 NULL : tp->list.tqh_first; 121 for (; ep != NULL; ep = ep->q.tqe_next) 122 whatis(argv, ep->s, 0); 123 } 124 125 if (!foundman) { 126 fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS); 127 exit(1); 128 } 129 rv = 1; 130 for (p = argv; *p; ++p) 131 if (found[p - argv]) 132 rv = 0; 133 else 134 printf("%s: not found\n", *p); 135 exit(rv); 136 } 137 138 void 139 whatis(argv, path, buildpath) 140 char **argv, *path; 141 int buildpath; 142 { 143 register char *end, *name, **p; 144 char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1]; 145 146 for (name = path; name; name = end) { /* through name list */ 147 if (end = strchr(name, ':')) 148 *end++ = '\0'; 149 150 if (buildpath) { 151 char hold[MAXPATHLEN + 1]; 152 153 (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS); 154 name = hold; 155 } 156 157 if (!freopen(name, "r", stdin)) 158 continue; 159 160 foundman = 1; 161 162 /* for each file found */ 163 while (fgets(buf, sizeof(buf), stdin)) { 164 dashtrunc(buf, wbuf); 165 for (p = argv; *p; ++p) 166 if (match(wbuf, *p)) { 167 printf("%s", buf); 168 found[p - argv] = 1; 169 170 /* only print line once */ 171 while (*++p) 172 if (match(wbuf, *p)) 173 found[p - argv] = 1; 174 break; 175 } 176 } 177 } 178 } 179 180 /* 181 * match -- 182 * match a full word 183 */ 184 int 185 match(bp, str) 186 register char *bp, *str; 187 { 188 register int len; 189 register char *start; 190 191 if (!*str || !*bp) 192 return(0); 193 for (len = strlen(str);;) { 194 for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp); 195 if (!*bp) 196 break; 197 for (start = bp++; 198 *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp); 199 if (bp - start == len && !strncasecmp(start, str, len)) 200 return(1); 201 } 202 return(0); 203 } 204 205 /* 206 * dashtrunc -- 207 * truncate a string at " - " 208 */ 209 void 210 dashtrunc(from, to) 211 register char *from, *to; 212 { 213 register int ch; 214 215 for (; (ch = *from) && ch != '\n' && 216 (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from) 217 *to++ = ch; 218 *to = '\0'; 219 } 220 221 /* 222 * usage -- 223 * print usage message and die 224 */ 225 void 226 usage() 227 { 228 (void)fprintf(stderr, 229 "usage: whatis [-C file] [-M path] [-m path] command ...\n"); 230 exit(1); 231 } 232