Home | History | Annotate | Line # | Download | only in apropos
      1  1.30       wiz /*	$NetBSD: apropos.c,v 1.30 2009/05/08 12:48:43 wiz Exp $	*/
      2   1.3     glass 
      3   1.1       cgd /*
      4   1.2     glass  * Copyright (c) 1987, 1993, 1994
      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.22       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32   1.8     mikel #include <sys/cdefs.h>
     33   1.8     mikel 
     34   1.1       cgd #ifndef lint
     35  1.29     lukem __COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
     36  1.29     lukem  The Regents of the University of California.  All rights reserved.");
     37   1.1       cgd #endif /* not lint */
     38   1.1       cgd 
     39   1.1       cgd #ifndef lint
     40   1.3     glass #if 0
     41   1.5       tls static char sccsid[] = "@(#)apropos.c	8.8 (Berkeley) 5/4/95";
     42   1.3     glass #else
     43  1.30       wiz __RCSID("$NetBSD: apropos.c,v 1.30 2009/05/08 12:48:43 wiz Exp $");
     44   1.3     glass #endif
     45   1.1       cgd #endif /* not lint */
     46   1.1       cgd 
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/queue.h>
     49   1.1       cgd 
     50   1.1       cgd #include <ctype.h>
     51   1.1       cgd #include <err.h>
     52   1.7     mikel #include <glob.h>
     53   1.2     glass #include <limits.h>
     54  1.28  christos #include <stdbool.h>
     55   1.1       cgd #include <stdio.h>
     56   1.1       cgd #include <stdlib.h>
     57   1.1       cgd #include <string.h>
     58   1.5       tls #include <unistd.h>
     59   1.1       cgd 
     60  1.21     lukem #include "manconf.h"		/* from ../man/ */
     61  1.21     lukem #include "pathnames.h"		/* from ../man/ */
     62   1.1       cgd 
     63  1.28  christos static bool *found;
     64  1.28  christos static bool foundman = false;
     65   1.1       cgd 
     66  1.11       mrg #define	MAXLINELEN	8192		/* max line handled */
     67  1.11       mrg 
     68  1.30       wiz static void apropos(char **, const char *, bool, const char *, const char *);
     69  1.30       wiz static void lowstr(const char *, char *);
     70  1.30       wiz static bool match(const char *, const char *);
     71  1.28  christos static void usage(void) __dead;
     72   1.1       cgd 
     73   1.1       cgd int
     74  1.25   xtraeme main(int argc, char *argv[])
     75   1.1       cgd {
     76   1.1       cgd 	ENTRY *ep;
     77   1.1       cgd 	TAG *tp;
     78   1.1       cgd 	int ch, rv;
     79  1.30       wiz 	char *conffile, *machine, **p, *p_augment, *p_path, *sflag;
     80   1.7     mikel 	glob_t pg;
     81   1.1       cgd 
     82   1.1       cgd 	conffile = NULL;
     83   1.1       cgd 	p_augment = p_path = NULL;
     84  1.30       wiz 	machine = sflag = NULL;
     85  1.30       wiz 	while ((ch = getopt(argc, argv, "C:M:m:P:S:s:")) != -1) {
     86   1.1       cgd 		switch (ch) {
     87   1.1       cgd 		case 'C':
     88   1.1       cgd 			conffile = optarg;
     89   1.1       cgd 			break;
     90   1.1       cgd 		case 'M':
     91   1.1       cgd 		case 'P':		/* backward compatible */
     92   1.1       cgd 			p_path = optarg;
     93   1.1       cgd 			break;
     94   1.1       cgd 		case 'm':
     95   1.1       cgd 			p_augment = optarg;
     96   1.1       cgd 			break;
     97  1.30       wiz 		case 'S':
     98  1.30       wiz 			machine = optarg;
     99  1.30       wiz 			lowstr(machine, machine);
    100  1.30       wiz 			break;
    101  1.30       wiz 		case 's':
    102  1.30       wiz 			sflag = optarg;
    103  1.30       wiz 			lowstr(sflag, sflag);
    104  1.30       wiz 			break;
    105   1.1       cgd 		case '?':
    106   1.1       cgd 		default:
    107   1.1       cgd 			usage();
    108   1.1       cgd 		}
    109  1.28  christos 	}
    110   1.1       cgd 	argv += optind;
    111   1.1       cgd 	argc -= optind;
    112   1.1       cgd 
    113   1.1       cgd 	if (argc < 1)
    114   1.1       cgd 		usage();
    115   1.1       cgd 
    116  1.28  christos 	if ((found = malloc(argc * sizeof(*found))) == NULL)
    117  1.28  christos 		err(EXIT_FAILURE, "malloc");
    118  1.28  christos 	(void)memset(found, 0, argc * sizeof(*found));
    119   1.1       cgd 
    120   1.1       cgd 	for (p = argv; *p; ++p)			/* convert to lower-case */
    121   1.1       cgd 		lowstr(*p, *p);
    122   1.1       cgd 
    123   1.1       cgd 	if (p_augment)
    124  1.30       wiz 		apropos(argv, p_augment, true, sflag, machine);
    125   1.1       cgd 	if (p_path || (p_path = getenv("MANPATH")))
    126  1.30       wiz 		apropos(argv, p_path, true, sflag, machine);
    127   1.1       cgd 	else {
    128   1.1       cgd 		config(conffile);
    129  1.27     chuck 		tp = gettag("_whatdb", 1);
    130  1.27     chuck 		if (!tp)
    131  1.27     chuck 			errx(EXIT_FAILURE, "malloc");
    132  1.27     chuck 		TAILQ_FOREACH(ep, &tp->entrylist, q) {
    133  1.13    kleink 			if ((rv = glob(ep->s, GLOB_BRACE | GLOB_NOSORT, NULL,
    134  1.13    kleink 			    &pg)) != 0) {
    135  1.13    kleink 				if (rv == GLOB_NOMATCH)
    136  1.13    kleink 					continue;
    137  1.13    kleink 				else
    138  1.28  christos 					err(EXIT_FAILURE, "glob");
    139  1.13    kleink 			}
    140  1.10     mikel 			if (pg.gl_pathc)
    141  1.10     mikel 				for (p = pg.gl_pathv; *p; p++)
    142  1.30       wiz 					apropos(argv, *p, false, sflag,
    143  1.30       wiz 						machine);
    144   1.7     mikel 			globfree(&pg);
    145   1.7     mikel 		}
    146   1.1       cgd 	}
    147   1.1       cgd 
    148   1.2     glass 	if (!foundman)
    149  1.28  christos 		errx(EXIT_FAILURE, "no %s file found", _PATH_WHATIS);
    150   1.2     glass 
    151   1.1       cgd 	rv = 1;
    152   1.1       cgd 	for (p = argv; *p; ++p)
    153   1.1       cgd 		if (found[p - argv])
    154   1.1       cgd 			rv = 0;
    155   1.1       cgd 		else
    156   1.1       cgd 			(void)printf("%s: nothing appropriate\n", *p);
    157  1.28  christos 	return rv;
    158   1.1       cgd }
    159   1.1       cgd 
    160  1.28  christos static void
    161  1.30       wiz apropos(char **argv, const char *path, bool buildpath,
    162  1.30       wiz 	const char *sflag, const char *machine)
    163   1.1       cgd {
    164  1.30       wiz 	char *end, **p;
    165  1.30       wiz 	const char *name;
    166  1.28  christos 	char buf[MAXLINELEN + 1];
    167   1.8     mikel 	char hold[MAXPATHLEN + 1];
    168  1.28  christos 	char wbuf[MAXLINELEN + 1];
    169  1.30       wiz 	size_t slen = 0, mlen = 0;
    170  1.30       wiz 
    171  1.30       wiz 	if (sflag)
    172  1.30       wiz 		slen = strlen(sflag);
    173  1.30       wiz 	if (machine)
    174  1.30       wiz 		mlen = strlen(machine);
    175   1.1       cgd 
    176   1.1       cgd 	for (name = path; name; name = end) {	/* through name list */
    177  1.28  christos 		if ((end = strchr(name, ':')) != NULL)
    178   1.1       cgd 			*end++ = '\0';
    179   1.1       cgd 
    180   1.1       cgd 		if (buildpath) {
    181  1.28  christos 			(void)snprintf(hold, sizeof(hold), "%s/%s", name,
    182  1.27     chuck 					_PATH_WHATIS);
    183   1.1       cgd 			name = hold;
    184   1.1       cgd 		}
    185   1.1       cgd 
    186   1.1       cgd 		if (!freopen(name, "r", stdin))
    187   1.1       cgd 			continue;
    188   1.1       cgd 
    189  1.28  christos 		foundman = true;
    190   1.1       cgd 
    191   1.1       cgd 		/* for each file found */
    192  1.28  christos 		while (fgets(buf, (int)sizeof(buf), stdin)) {
    193   1.2     glass 			if (!strchr(buf, '\n')) {
    194   1.2     glass 				warnx("%s: line too long", name);
    195   1.1       cgd 				continue;
    196   1.1       cgd 			}
    197   1.1       cgd 			lowstr(buf, wbuf);
    198  1.30       wiz 			if (machine) {
    199  1.30       wiz 				if ((strncmp(wbuf, machine, mlen) != 0) ||
    200  1.30       wiz 				    strlen(wbuf) <= mlen || wbuf[mlen] != '/')
    201  1.30       wiz 					continue;
    202  1.30       wiz 			}
    203  1.30       wiz 			if (sflag) {
    204  1.30       wiz 				char *s = strchr(wbuf, '(');
    205  1.30       wiz 
    206  1.30       wiz 				if (!s)
    207  1.30       wiz 					continue;
    208  1.30       wiz 				if (strncmp(s+1, sflag, slen) != 0)
    209  1.30       wiz 					continue;
    210  1.30       wiz 			}
    211  1.28  christos 			for (p = argv; *p; ++p) {
    212   1.1       cgd 				if (match(wbuf, *p)) {
    213   1.1       cgd 					(void)printf("%s", buf);
    214  1.28  christos 					found[p - argv] = true;
    215   1.1       cgd 
    216   1.1       cgd 					/* only print line once */
    217   1.1       cgd 					while (*++p)
    218   1.1       cgd 						if (match(wbuf, *p))
    219  1.28  christos 							found[p - argv] = true;
    220   1.1       cgd 					break;
    221   1.1       cgd 				}
    222  1.28  christos 			}
    223   1.1       cgd 		}
    224   1.1       cgd 	}
    225   1.1       cgd }
    226   1.1       cgd 
    227   1.1       cgd /*
    228   1.1       cgd  * match --
    229   1.1       cgd  *	match anywhere the string appears
    230   1.1       cgd  */
    231  1.28  christos static bool
    232  1.30       wiz match(const char *bp, const char *str)
    233   1.1       cgd {
    234  1.28  christos 	size_t len;
    235   1.2     glass 	char test;
    236   1.1       cgd 
    237   1.1       cgd 	if (!*bp)
    238  1.28  christos 		return false;
    239   1.1       cgd 	/* backward compatible: everything matches empty string */
    240   1.1       cgd 	if (!*str)
    241  1.28  christos 		return true;
    242   1.1       cgd 	for (test = *str++, len = strlen(str); *bp;)
    243   1.1       cgd 		if (test == *bp++ && !strncmp(bp, str, len))
    244  1.28  christos 			return true;
    245  1.28  christos 	return false;
    246   1.1       cgd }
    247   1.1       cgd 
    248   1.1       cgd /*
    249   1.1       cgd  * lowstr --
    250   1.1       cgd  *	convert a string to lower case
    251   1.1       cgd  */
    252  1.28  christos static void
    253  1.30       wiz lowstr(const char *from, char *to)
    254   1.1       cgd {
    255   1.2     glass 	char ch;
    256   1.1       cgd 
    257   1.1       cgd 	while ((ch = *from++) && ch != '\n')
    258  1.24       dsl 		*to++ = tolower((unsigned char)ch);
    259   1.1       cgd 	*to = '\0';
    260   1.1       cgd }
    261   1.1       cgd 
    262   1.1       cgd /*
    263   1.1       cgd  * usage --
    264   1.1       cgd  *	print usage message and die
    265   1.1       cgd  */
    266  1.28  christos __dead
    267  1.28  christos static void
    268  1.25   xtraeme usage(void)
    269   1.1       cgd {
    270  1.16       cgd 
    271   1.1       cgd 	(void)fprintf(stderr,
    272  1.30       wiz 	    "usage: %s [-C file] [-M path] [-m path] "
    273  1.30       wiz 	    "[-S subsection] [-s section]\n"
    274  1.30       wiz 	    "       keyword ...\n",
    275  1.16       cgd 	    getprogname());
    276   1.1       cgd 	exit(1);
    277   1.1       cgd }
    278