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