Home | History | Annotate | Line # | Download | only in apropos
apropos.c revision 1.10
      1 /*	$NetBSD: apropos.c,v 1.10 1997/10/20 23:02:09 mikel 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 #include <sys/cdefs.h>
     37 
     38 #ifndef lint
     39 __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
     40 	The Regents of the University of California.  All rights reserved.\n");
     41 #endif /* not lint */
     42 
     43 #ifndef lint
     44 #if 0
     45 static char sccsid[] = "@(#)apropos.c	8.8 (Berkeley) 5/4/95";
     46 #else
     47 __RCSID("$NetBSD: apropos.c,v 1.10 1997/10/20 23:02:09 mikel Exp $");
     48 #endif
     49 #endif /* not lint */
     50 
     51 #include <sys/param.h>
     52 #include <sys/queue.h>
     53 
     54 #include <ctype.h>
     55 #include <err.h>
     56 #include <glob.h>
     57 #include <limits.h>
     58 #include <stdio.h>
     59 #include <stdlib.h>
     60 #include <string.h>
     61 #include <unistd.h>
     62 
     63 #include "config.h"
     64 #include "pathnames.h"
     65 
     66 static int *found, foundman;
     67 
     68 int main __P((int, char **));
     69 void apropos __P((char **, char *, int));
     70 void lowstr __P((char *, char *));
     71 int match __P((char *, char *));
     72 void usage __P((void));
     73 
     74 int
     75 main(argc, argv)
     76 	int argc;
     77 	char *argv[];
     78 {
     79 	ENTRY *ep;
     80 	TAG *tp;
     81 	int ch, rv;
     82 	char *conffile, **p, *p_augment, *p_path;
     83 	glob_t pg;
     84 
     85 	conffile = NULL;
     86 	p_augment = p_path = NULL;
     87 	while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
     88 		switch (ch) {
     89 		case 'C':
     90 			conffile = optarg;
     91 			break;
     92 		case 'M':
     93 		case 'P':		/* backward compatible */
     94 			p_path = optarg;
     95 			break;
     96 		case 'm':
     97 			p_augment = optarg;
     98 			break;
     99 		case '?':
    100 		default:
    101 			usage();
    102 		}
    103 	argv += optind;
    104 	argc -= optind;
    105 
    106 	if (argc < 1)
    107 		usage();
    108 
    109 	if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
    110 		err(1, "malloc");
    111 	memset(found, 0, argc * sizeof(int));
    112 
    113 	for (p = argv; *p; ++p)			/* convert to lower-case */
    114 		lowstr(*p, *p);
    115 
    116 	if (p_augment)
    117 		apropos(argv, p_augment, 1);
    118 	if (p_path || (p_path = getenv("MANPATH")))
    119 		apropos(argv, p_path, 1);
    120 	else {
    121 		config(conffile);
    122 		ep = (tp = getlist("_whatdb")) == NULL ?
    123 		    NULL : tp->list.tqh_first;
    124 		for (; ep != NULL; ep = ep->q.tqe_next) {
    125 			if (glob(ep->s, GLOB_BRACE | GLOB_NOSORT | GLOB_QUOTE,
    126 			    NULL, &pg) != 0)
    127 				err(1, "glob");
    128 			if (pg.gl_pathc)
    129 				for (p = pg.gl_pathv; *p; p++)
    130 					apropos(argv, *p, 0);
    131 			globfree(&pg);
    132 		}
    133 	}
    134 
    135 	if (!foundman)
    136 		errx(1, "no %s file found", _PATH_WHATIS);
    137 
    138 	rv = 1;
    139 	for (p = argv; *p; ++p)
    140 		if (found[p - argv])
    141 			rv = 0;
    142 		else
    143 			(void)printf("%s: nothing appropriate\n", *p);
    144 	exit(rv);
    145 }
    146 
    147 void
    148 apropos(argv, path, buildpath)
    149 	char **argv, *path;
    150 	int buildpath;
    151 {
    152 	char *end, *name, **p;
    153 	char buf[LINE_MAX + 1], wbuf[LINE_MAX + 1];
    154 	char hold[MAXPATHLEN + 1];
    155 
    156 	for (name = path; name; name = end) {	/* through name list */
    157 		if ((end = strchr(name, ':')))
    158 			*end++ = '\0';
    159 
    160 		if (buildpath) {
    161 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
    162 			name = hold;
    163 		}
    164 
    165 		if (!freopen(name, "r", stdin))
    166 			continue;
    167 
    168 		foundman = 1;
    169 
    170 		/* for each file found */
    171 		while (fgets(buf, sizeof(buf), stdin)) {
    172 			if (!strchr(buf, '\n')) {
    173 				warnx("%s: line too long", name);
    174 				continue;
    175 			}
    176 			lowstr(buf, wbuf);
    177 			for (p = argv; *p; ++p)
    178 				if (match(wbuf, *p)) {
    179 					(void)printf("%s", buf);
    180 					found[p - argv] = 1;
    181 
    182 					/* only print line once */
    183 					while (*++p)
    184 						if (match(wbuf, *p))
    185 							found[p - argv] = 1;
    186 					break;
    187 				}
    188 		}
    189 	}
    190 }
    191 
    192 /*
    193  * match --
    194  *	match anywhere the string appears
    195  */
    196 int
    197 match(bp, str)
    198 	char *bp, *str;
    199 {
    200 	int len;
    201 	char test;
    202 
    203 	if (!*bp)
    204 		return (0);
    205 	/* backward compatible: everything matches empty string */
    206 	if (!*str)
    207 		return (1);
    208 	for (test = *str++, len = strlen(str); *bp;)
    209 		if (test == *bp++ && !strncmp(bp, str, len))
    210 			return (1);
    211 	return (0);
    212 }
    213 
    214 /*
    215  * lowstr --
    216  *	convert a string to lower case
    217  */
    218 void
    219 lowstr(from, to)
    220 	char *from, *to;
    221 {
    222 	char ch;
    223 
    224 	while ((ch = *from++) && ch != '\n')
    225 		*to++ = isupper(ch) ? tolower(ch) : ch;
    226 	*to = '\0';
    227 }
    228 
    229 /*
    230  * usage --
    231  *	print usage message and die
    232  */
    233 void
    234 usage()
    235 {
    236 
    237 	(void)fprintf(stderr,
    238 	    "usage: apropos [-C file] [-M path] [-m path] keyword ...\n");
    239 	exit(1);
    240 }
    241