1 /* $NetBSD: apropos.c,v 1.30 2009/05/08 12:48:43 wiz 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 34 #ifndef lint 35 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\ 36 The Regents of the University of California. All rights reserved."); 37 #endif /* not lint */ 38 39 #ifndef lint 40 #if 0 41 static char sccsid[] = "@(#)apropos.c 8.8 (Berkeley) 5/4/95"; 42 #else 43 __RCSID("$NetBSD: apropos.c,v 1.30 2009/05/08 12:48:43 wiz Exp $"); 44 #endif 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/queue.h> 49 50 #include <ctype.h> 51 #include <err.h> 52 #include <glob.h> 53 #include <limits.h> 54 #include <stdbool.h> 55 #include <stdio.h> 56 #include <stdlib.h> 57 #include <string.h> 58 #include <unistd.h> 59 60 #include "manconf.h" /* from ../man/ */ 61 #include "pathnames.h" /* from ../man/ */ 62 63 static bool *found; 64 static bool foundman = false; 65 66 #define MAXLINELEN 8192 /* max line handled */ 67 68 static void apropos(char **, const char *, bool, const char *, const char *); 69 static void lowstr(const char *, char *); 70 static bool match(const char *, const char *); 71 static void usage(void) __dead; 72 73 int 74 main(int argc, char *argv[]) 75 { 76 ENTRY *ep; 77 TAG *tp; 78 int ch, rv; 79 char *conffile, *machine, **p, *p_augment, *p_path, *sflag; 80 glob_t pg; 81 82 conffile = NULL; 83 p_augment = p_path = NULL; 84 machine = sflag = NULL; 85 while ((ch = getopt(argc, argv, "C:M:m:P:S:s:")) != -1) { 86 switch (ch) { 87 case 'C': 88 conffile = optarg; 89 break; 90 case 'M': 91 case 'P': /* backward compatible */ 92 p_path = optarg; 93 break; 94 case 'm': 95 p_augment = optarg; 96 break; 97 case 'S': 98 machine = optarg; 99 lowstr(machine, machine); 100 break; 101 case 's': 102 sflag = optarg; 103 lowstr(sflag, sflag); 104 break; 105 case '?': 106 default: 107 usage(); 108 } 109 } 110 argv += optind; 111 argc -= optind; 112 113 if (argc < 1) 114 usage(); 115 116 if ((found = malloc(argc * sizeof(*found))) == NULL) 117 err(EXIT_FAILURE, "malloc"); 118 (void)memset(found, 0, argc * sizeof(*found)); 119 120 for (p = argv; *p; ++p) /* convert to lower-case */ 121 lowstr(*p, *p); 122 123 if (p_augment) 124 apropos(argv, p_augment, true, sflag, machine); 125 if (p_path || (p_path = getenv("MANPATH"))) 126 apropos(argv, p_path, true, sflag, machine); 127 else { 128 config(conffile); 129 tp = gettag("_whatdb", 1); 130 if (!tp) 131 errx(EXIT_FAILURE, "malloc"); 132 TAILQ_FOREACH(ep, &tp->entrylist, q) { 133 if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL, 134 &pg)) != 0) { 135 if (rv == GLOB_NOMATCH) 136 continue; 137 else 138 err(EXIT_FAILURE, "glob"); 139 } 140 if (pg.gl_pathc) 141 for (p = pg.gl_pathv; *p; p++) 142 apropos(argv, *p, false, sflag, 143 machine); 144 globfree(&pg); 145 } 146 } 147 148 if (!foundman) 149 errx(EXIT_FAILURE, "no %s file found", _PATH_WHATIS); 150 151 rv = 1; 152 for (p = argv; *p; ++p) 153 if (found[p - argv]) 154 rv = 0; 155 else 156 (void)printf("%s: nothing appropriate\n", *p); 157 return rv; 158 } 159 160 static void 161 apropos(char **argv, const char *path, bool buildpath, 162 const char *sflag, const char *machine) 163 { 164 char *end, **p; 165 const char *name; 166 char buf[MAXLINELEN + 1]; 167 char hold[MAXPATHLEN + 1]; 168 char wbuf[MAXLINELEN + 1]; 169 size_t slen = 0, mlen = 0; 170 171 if (sflag) 172 slen = strlen(sflag); 173 if (machine) 174 mlen = strlen(machine); 175 176 for (name = path; name; name = end) { /* through name list */ 177 if ((end = strchr(name, ':')) != NULL) 178 *end++ = '\0'; 179 180 if (buildpath) { 181 (void)snprintf(hold, sizeof(hold), "%s/%s", name, 182 _PATH_WHATIS); 183 name = hold; 184 } 185 186 if (!freopen(name, "r", stdin)) 187 continue; 188 189 foundman = true; 190 191 /* for each file found */ 192 while (fgets(buf, (int)sizeof(buf), stdin)) { 193 if (!strchr(buf, '\n')) { 194 warnx("%s: line too long", name); 195 continue; 196 } 197 lowstr(buf, wbuf); 198 if (machine) { 199 if ((strncmp(wbuf, machine, mlen) != 0) || 200 strlen(wbuf) <= mlen || wbuf[mlen] != '/') 201 continue; 202 } 203 if (sflag) { 204 char *s = strchr(wbuf, '('); 205 206 if (!s) 207 continue; 208 if (strncmp(s+1, sflag, slen) != 0) 209 continue; 210 } 211 for (p = argv; *p; ++p) { 212 if (match(wbuf, *p)) { 213 (void)printf("%s", buf); 214 found[p - argv] = true; 215 216 /* only print line once */ 217 while (*++p) 218 if (match(wbuf, *p)) 219 found[p - argv] = true; 220 break; 221 } 222 } 223 } 224 } 225 } 226 227 /* 228 * match -- 229 * match anywhere the string appears 230 */ 231 static bool 232 match(const char *bp, const char *str) 233 { 234 size_t len; 235 char test; 236 237 if (!*bp) 238 return false; 239 /* backward compatible: everything matches empty string */ 240 if (!*str) 241 return true; 242 for (test = *str++, len = strlen(str); *bp;) 243 if (test == *bp++ && !strncmp(bp, str, len)) 244 return true; 245 return false; 246 } 247 248 /* 249 * lowstr -- 250 * convert a string to lower case 251 */ 252 static void 253 lowstr(const char *from, char *to) 254 { 255 char ch; 256 257 while ((ch = *from++) && ch != '\n') 258 *to++ = tolower((unsigned char)ch); 259 *to = '\0'; 260 } 261 262 /* 263 * usage -- 264 * print usage message and die 265 */ 266 __dead 267 static void 268 usage(void) 269 { 270 271 (void)fprintf(stderr, 272 "usage: %s [-C file] [-M path] [-m path] " 273 "[-S subsection] [-s section]\n" 274 " keyword ...\n", 275 getprogname()); 276 exit(1); 277 } 278