home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / man / whatis / whatis.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-18  |  4.9 KB  |  203 lines

  1. /*
  2.  * Copyright (c) 1987 Regents of the University of California.
  3.  * 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. char copyright[] =
  36. "@(#) Copyright (c) 1987 Regents of the University of California.\n\
  37.  All rights reserved.\n";
  38. #endif /* not lint */
  39.  
  40. #ifndef lint
  41. static char sccsid[] = "@(#)whatis.c    5.6 (Berkeley) 6/1/90";
  42. #endif /* not lint */
  43.  
  44. #include <sys/param.h>
  45. #include <stdio.h>
  46. #include <ctype.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49. #include "../man/pathnames.h"
  50.  
  51. #define    MAXLINELEN    256            /* max line handled */
  52.  
  53. char *progname;
  54.  
  55. static int *found, foundman;
  56.  
  57. main(argc, argv)
  58.     int argc;
  59.     char **argv;
  60. {
  61.     extern char *optarg;
  62.     extern int optind;
  63.     register char *beg, **p;
  64.     int ch;
  65.     char *p_augment, *p_path, **getdb();
  66.  
  67.     progname = "whatis";
  68.     while ((ch = getopt(argc, argv, "M:m:P:")) != EOF)
  69.         switch((char)ch) {
  70.         case 'M':
  71.         case 'P':        /* backward compatible */
  72.             p_path = optarg;
  73.             break;
  74.         case 'm':
  75.             p_augment = optarg;
  76.             break;
  77.         case '?':
  78.         default:
  79.             usage();
  80.         }
  81.     argv += optind;
  82.     argc -= optind;
  83.  
  84.     if (argc < 1)
  85.         usage();
  86.  
  87.     /*NOSTRICT*/
  88.     if (!(found = (int *)malloc((u_int)argc)))
  89.         enomem();
  90.     bzero((char *)found, argc * sizeof(int));
  91.  
  92.     for (p = argv; *p; ++p)            /* trim full paths */
  93.         if (beg = rindex(*p, '/'))
  94.             *p = beg + 1;
  95.  
  96.     if (p_augment)
  97.         whatis(argv, p_augment, 1);
  98.     if (p_path || (p_path = getenv("MANPATH")))
  99.         whatis(argv, p_path, 1);
  100.     else
  101.         for (p = getdb(); *p; ++p)
  102.             whatis(argv, *p, 0);
  103.  
  104.     if (!foundman) {
  105.         fprintf(stderr, "whatis: no %s file found.\n", _PATH_WHATIS);
  106.         exit(1);
  107.     }
  108.     for (p = argv; *p; ++p)
  109.         if (!found[p - argv])
  110.             printf("%s: not found\n", *p);
  111. }
  112.  
  113. whatis(argv, path, buildpath)
  114.     char **argv, *path;
  115.     int buildpath;
  116. {
  117.     register char *end, *name, **p;
  118.     char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
  119.  
  120.     for (name = path; name; name = end) {    /* through name list */
  121.         if (end = index(name, ':'))
  122.             *end++ = '\0';
  123.  
  124.         if (buildpath) {
  125.             char hold[MAXPATHLEN + 1];
  126.  
  127.             (void)sprintf(hold, "%s/%s", name, _PATH_WHATIS);
  128.             name = hold;
  129.         }
  130.  
  131.         if (!freopen(name, "r", stdin))
  132.             continue;
  133.  
  134.         foundman = 1;
  135.  
  136.         /* for each file found */
  137.         while (fgets(buf, sizeof(buf), stdin)) {
  138.             dashtrunc(buf, wbuf);
  139.             for (p = argv; *p; ++p)
  140.                 if (match(wbuf, *p)) {
  141.                     printf("%s", buf);
  142.                     found[p - argv] = 1;
  143.  
  144.                     /* only print line once */
  145.                     while (*++p)
  146.                         if (match(wbuf, *p))
  147.                             found[p - argv] = 1;
  148.                     break;
  149.                 }
  150.         }
  151.     }
  152. }
  153.  
  154. /*
  155.  * match --
  156.  *    match a full word
  157.  */
  158. match(bp, str)
  159.     register char *bp, *str;
  160. {
  161.     register int len;
  162.     register char *start;
  163.  
  164.     if (!*str || !*bp)
  165.         return(0);
  166.     for (len = strlen(str);;) {
  167.         for (; *bp && !isdigit(*bp) && !isalpha(*bp); ++bp);
  168.         if (!*bp)
  169.             break;
  170.         for (start = bp++;
  171.             *bp && (*bp == '_' || isdigit(*bp) || isalpha(*bp)); ++bp);
  172.         if (bp - start == len && !strncasecmp(start, str, len))
  173.             return(1);
  174.     }
  175.     return(0);
  176. }
  177.  
  178. /*
  179.  * dashtrunc --
  180.  *    truncate a string at " - "
  181.  */
  182. dashtrunc(from, to)
  183.     register char *from, *to;
  184. {
  185.     register int ch;
  186.  
  187.     for (; (ch = *from) && ch != '\n' &&
  188.         (ch != ' ' || from[1] != '-' || from[2] != ' '); ++from)
  189.         *to++ = ch;
  190.     *to = '\0';
  191. }
  192.  
  193. /*
  194.  * usage --
  195.  *    print usage message and die
  196.  */
  197. usage()
  198. {
  199.     (void)fprintf(stderr,
  200.         "usage: whatis [-M path] [-m path] command ...\n");
  201.     exit(1);
  202. }
  203.