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