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