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