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