Home | History | Annotate | Line # | Download | only in whatis
whatis.c revision 1.17
      1 /*	$NetBSD: whatis.c,v 1.17 2002/09/19 11:29:55 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1993
      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\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[] = "@(#)whatis.c	8.5 (Berkeley) 1/2/94";
     46 #else
     47 __RCSID("$NetBSD: whatis.c,v 1.17 2002/09/19 11:29:55 lukem 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 <stdio.h>
     58 #include <stdlib.h>
     59 #include <string.h>
     60 #include <unistd.h>
     61 
     62 #include "manconf.h"		/* from ../man/ */
     63 #include "pathnames.h"		/* from ../man/ */
     64 
     65 #define	MAXLINELEN	8192			/* max line handled */
     66 
     67 static int *found, foundman;
     68 
     69 int main __P((int, char **));
     70 void dashtrunc __P((char *, char *));
     71 int match __P((char *, char *));
     72 void usage __P((void));
     73 void whatis __P((char **, char *, int));
     74 
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char *argv[];
     79 {
     80 	ENTRY *ep;
     81 	TAG *tp;
     82 	int ch, rv;
     83 	char *beg, *conffile, **p, *p_augment, *p_path;
     84 	glob_t pg;
     85 
     86 	conffile = NULL;
     87 	p_augment = p_path = NULL;
     88 	while ((ch = getopt(argc, argv, "C:M:m:P:")) != -1)
     89 		switch (ch) {
     90 		case 'C':
     91 			conffile = optarg;
     92 			break;
     93 		case 'M':
     94 		case 'P':		/* backward compatible */
     95 			p_path = optarg;
     96 			break;
     97 		case 'm':
     98 			p_augment = optarg;
     99 			break;
    100 		case '?':
    101 		default:
    102 			usage();
    103 		}
    104 	argv += optind;
    105 	argc -= optind;
    106 
    107 	if (argc < 1)
    108 		usage();
    109 
    110 	if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
    111 		err(1, "malloc");
    112 	memset(found, 0, argc * sizeof(int));
    113 
    114 	for (p = argv; *p; ++p)			/* trim full paths */
    115 		if ((beg = strrchr(*p, '/')))
    116 			*p = beg + 1;
    117 
    118 	if (p_augment)
    119 		whatis(argv, p_augment, 1);
    120 	if (p_path || (p_path = getenv("MANPATH")))
    121 		whatis(argv, p_path, 1);
    122 	else {
    123 		config(conffile);
    124 		tp = getlist("_whatdb", 0);
    125 		TAILQ_FOREACH(ep, &tp->list, q) {
    126 			if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL,
    127 			    &pg)) != 0) {
    128 				if (rv == GLOB_NOMATCH)
    129 					continue;
    130 				else
    131 					err(EXIT_FAILURE, "glob");
    132 			}
    133 			if (pg.gl_pathc)
    134 				for (p = pg.gl_pathv; *p; p++)
    135 					whatis(argv, *p, 0);
    136 			globfree(&pg);
    137 		}
    138 	}
    139 
    140 	if (!foundman) {
    141 		fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
    142 		exit(1);
    143 	}
    144 	rv = 1;
    145 	for (p = argv; *p; ++p)
    146 		if (found[p - argv])
    147 			rv = 0;
    148 		else
    149 			printf("%s: not found\n", *p);
    150 	exit(rv);
    151 }
    152 
    153 void
    154 whatis(argv, path, buildpath)
    155 	char **argv, *path;
    156 	int buildpath;
    157 {
    158 	char *end, *name, **p;
    159 	char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
    160 	char hold[MAXPATHLEN + 1];
    161 
    162 	for (name = path; name; name = end) {	/* through name list */
    163 		if ((end = strchr(name, ':')))
    164 			*end++ = '\0';
    165 
    166 		if (buildpath) {
    167 			(void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
    168 			name = hold;
    169 		}
    170 
    171 		if (!freopen(name, "r", stdin))
    172 			continue;
    173 
    174 		foundman = 1;
    175 
    176 		/* for each file found */
    177 		while (fgets(buf, sizeof(buf), stdin)) {
    178 			dashtrunc(buf, wbuf);
    179 			for (p = argv; *p; ++p)
    180 				if (match(wbuf, *p)) {
    181 					printf("%s", buf);
    182 					found[p - argv] = 1;
    183 
    184 					/* only print line once */
    185 					while (*++p)
    186 						if (match(wbuf, *p))
    187 							found[p - argv] = 1;
    188 					break;
    189 				}
    190 		}
    191 	}
    192 }
    193 
    194 /*
    195  * match --
    196  *	match a full word
    197  */
    198 int
    199 match(bp, str)
    200 	char *bp, *str;
    201 {
    202 	int len;
    203 	char *start;
    204 
    205 	if (!*str || !*bp)
    206 		return(0);
    207 	for (len = strlen(str);;) {
    208 		for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
    209 		if (!*bp)
    210 			break;
    211 		for (start = bp++;
    212 		    *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
    213 		if (bp - start == len && !strncasecmp(start, str, len))
    214 			return(1);
    215 	}
    216 	return(0);
    217 }
    218 
    219 /*
    220  * dashtrunc --
    221  *	truncate a string at " - "
    222  */
    223 void
    224 dashtrunc(from, to)
    225 	char *from, *to;
    226 {
    227 	int ch;
    228 
    229 	for (; (ch = *from) && ch != '\n' &&
    230 	    (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
    231 		*to++ = ch;
    232 	*to = '\0';
    233 }
    234 
    235 /*
    236  * usage --
    237  *	print usage message and die
    238  */
    239 void
    240 usage()
    241 {
    242 	(void)fprintf(stderr,
    243 	    "usage: whatis [-C file] [-M path] [-m path] command ...\n");
    244 	exit(1);
    245 }
    246