1 /* $NetBSD: apropos.c,v 1.6 1997/01/09 20:18:25 tls Exp $ */ 2 3 /* 4 * Copyright (c) 1987, 1993, 1994 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, 1994\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[] = "@(#)apropos.c 8.8 (Berkeley) 5/4/95"; 45 #else 46 static char rcsid[] = "$NetBSD: apropos.c,v 1.6 1997/01/09 20:18:25 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 <limits.h> 56 #include <stdio.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include <unistd.h> 60 61 #include "../man/config.h" 62 #include "../man/pathnames.h" 63 64 static int *found, foundman; 65 66 void apropos __P((char **, char *, int)); 67 void lowstr __P((char *, char *)); 68 int match __P((char *, char *)); 69 void usage __P((void)); 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 *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) /* convert to lower-case */ 110 lowstr(*p, *p); 111 112 if (p_augment) 113 apropos(argv, p_augment, 1); 114 if (p_path || (p_path = getenv("MANPATH"))) 115 apropos(argv, p_path, 1); 116 else { 117 config(conffile); 118 ep = (tp = getlist("_whatdb")) == NULL ? 119 NULL : tp->list.tqh_first; 120 for (; ep != NULL; ep = ep->q.tqe_next) 121 apropos(argv, ep->s, 0); 122 } 123 124 if (!foundman) 125 errx(1, "no %s file found", _PATH_WHATIS); 126 127 rv = 1; 128 for (p = argv; *p; ++p) 129 if (found[p - argv]) 130 rv = 0; 131 else 132 (void)printf("%s: nothing appropriate\n", *p); 133 exit(rv); 134 } 135 136 void 137 apropos(argv, path, buildpath) 138 char **argv, *path; 139 int buildpath; 140 { 141 char *end, *name, **p; 142 char buf[LINE_MAX + 1], wbuf[LINE_MAX + 1]; 143 144 for (name = path; name; name = end) { /* through name list */ 145 if (end = strchr(name, ':')) 146 *end++ = '\0'; 147 148 if (buildpath) { 149 char hold[MAXPATHLEN + 1]; 150 151 (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS); 152 name = hold; 153 } 154 155 if (!freopen(name, "r", stdin)) 156 continue; 157 158 foundman = 1; 159 160 /* for each file found */ 161 while (fgets(buf, sizeof(buf), stdin)) { 162 if (!strchr(buf, '\n')) { 163 warnx("%s: line too long", name); 164 continue; 165 } 166 lowstr(buf, wbuf); 167 for (p = argv; *p; ++p) 168 if (match(wbuf, *p)) { 169 (void)printf("%s", buf); 170 found[p - argv] = 1; 171 172 /* only print line once */ 173 while (*++p) 174 if (match(wbuf, *p)) 175 found[p - argv] = 1; 176 break; 177 } 178 } 179 } 180 } 181 182 /* 183 * match -- 184 * match anywhere the string appears 185 */ 186 int 187 match(bp, str) 188 char *bp, *str; 189 { 190 int len; 191 char test; 192 193 if (!*bp) 194 return (0); 195 /* backward compatible: everything matches empty string */ 196 if (!*str) 197 return (1); 198 for (test = *str++, len = strlen(str); *bp;) 199 if (test == *bp++ && !strncmp(bp, str, len)) 200 return (1); 201 return (0); 202 } 203 204 /* 205 * lowstr -- 206 * convert a string to lower case 207 */ 208 void 209 lowstr(from, to) 210 char *from, *to; 211 { 212 char ch; 213 214 while ((ch = *from++) && ch != '\n') 215 *to++ = isupper(ch) ? tolower(ch) : ch; 216 *to = '\0'; 217 } 218 219 /* 220 * usage -- 221 * print usage message and die 222 */ 223 void 224 usage() 225 { 226 227 (void)fprintf(stderr, 228 "usage: apropos [-C file] [-M path] [-m path] keyword ...\n"); 229 exit(1); 230 } 231