BSD 4_4_Lite1 release
[unix-history] / usr / src / usr.bin / apropos / apropos.c
index a6c5e80..a0e0525 100644 (file)
@@ -1,5 +1,5 @@
 /*
 /*
- * Copyright (c) 1987, 1993
+ * Copyright (c) 1987, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  *     The Regents of the University of California.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 #ifndef lint
 static char copyright[] =
 
 #ifndef lint
 static char copyright[] =
-"@(#) Copyright (c) 1987, 1993\n\
+"@(#) Copyright (c) 1987, 1993, 1994\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)apropos.c  8.1 (Berkeley) 6/6/93";
+static char sccsid[] = "@(#)apropos.c  8.7 (Berkeley) 4/2/94";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
-#include <stdio.h>
+#include <sys/queue.h>
+
 #include <ctype.h>
 #include <ctype.h>
-#include <string.h>
+#include <err.h>
+#include <limits.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
-#include "../man/pathnames.h"
-
-#define        MAXLINELEN      1024                    /* max line handled */
+#include <string.h>
 
 
-char *progname;
+#include "../man/config.h"
+#include "../man/pathnames.h"
 
 static int *found, foundman;
 
 
 static int *found, foundman;
 
+void apropos __P((char **, char *, int));
+void lowstr __P((char *, char *));
+int match __P((char *, char *));
+void usage __P((void));
+
+int
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
 {
-       extern char *optarg;
-       extern int optind;
-       register char **p;
-       int ch;
-       char *p_augment, *p_path, **getdb();
+       ENTRY *ep;
+       TAG *tp;
+       int ch, rv;
+       char *conffile, **p, *p_augment, *p_path;
 
 
-       progname = "apropos";
+       conffile = NULL;
        p_augment = p_path = NULL;
        p_augment = p_path = NULL;
-       while ((ch = getopt(argc, argv, "M:m:P:")) != EOF)
-               switch((char)ch) {
+       while ((ch = getopt(argc, argv, "C:M:m:P:")) != EOF)
+               switch (ch) {
+               case 'C':
+                       conffile = optarg;
+                       break;
                case 'M':
                case 'P':               /* backward compatible */
                        p_path = optarg;
                case 'M':
                case 'P':               /* backward compatible */
                        p_path = optarg;
@@ -85,10 +95,9 @@ main(argc, argv)
        if (argc < 1)
                usage();
 
        if (argc < 1)
                usage();
 
-       /*NOSTRICT*/
-       if (!(found = (int *)malloc((u_int)argc)))
-               enomem();
-       bzero((void *)found, argc * sizeof(int));
+       if ((found = malloc((u_int)argc * sizeof(int))) == NULL)
+               err(1, NULL);
+       memset(found, 0, argc * sizeof(int));
 
        for (p = argv; *p; ++p)                 /* convert to lower-case */
                lowstr(*p, *p);
 
        for (p = argv; *p; ++p)                 /* convert to lower-case */
                lowstr(*p, *p);
@@ -97,29 +106,36 @@ main(argc, argv)
                apropos(argv, p_augment, 1);
        if (p_path || (p_path = getenv("MANPATH")))
                apropos(argv, p_path, 1);
                apropos(argv, p_augment, 1);
        if (p_path || (p_path = getenv("MANPATH")))
                apropos(argv, p_path, 1);
-       else
-               for (p = getdb(); *p; ++p)
-                       apropos(argv, *p, 0);
-
-       if (!foundman) {
-               (void)fprintf(stderr,
-                   "apropos: : no %s file found.\n", _PATH_WHATIS);
-               exit(1);
+       else {
+               config(conffile);
+               ep = (tp = getlist("_whatdb")) == NULL ?
+                   NULL : tp->list.tqh_first;
+               for (; ep != NULL; ep = ep->q.tqe_next)
+                       apropos(argv, ep->s, 0);
        }
        }
+
+       if (!foundman)
+               errx(1, "no %s file found", _PATH_WHATIS);
+
+       rv = 1;
        for (p = argv; *p; ++p)
        for (p = argv; *p; ++p)
-               if (!found[p - argv])
+               if (found[p - argv])
+                       rv = 0;
+               else
                        (void)printf("%s: nothing appropriate\n", *p);
                        (void)printf("%s: nothing appropriate\n", *p);
+       exit(rv);
 }
 
 }
 
+void
 apropos(argv, path, buildpath)
        char **argv, *path;
        int buildpath;
 {
 apropos(argv, path, buildpath)
        char **argv, *path;
        int buildpath;
 {
-       register char *end, *name, **p;
-       char buf[MAXLINELEN + 1], wbuf[MAXLINELEN + 1];
+       char *end, *name, **p;
+       char buf[LINE_MAX + 1], wbuf[LINE_MAX + 1];
 
        for (name = path; name; name = end) {   /* through name list */
 
        for (name = path; name; name = end) {   /* through name list */
-               if (end = index(name, ':'))
+               if (end = strchr(name, ':'))
                        *end++ = '\0';
 
                if (buildpath) {
                        *end++ = '\0';
 
                if (buildpath) {
@@ -136,9 +152,8 @@ apropos(argv, path, buildpath)
 
                /* for each file found */
                while (fgets(buf, sizeof(buf), stdin)) {
 
                /* for each file found */
                while (fgets(buf, sizeof(buf), stdin)) {
-                       if (!index(buf, '\n')) {
-                               (void)fprintf(stderr,
-                                   "apropos: %s line too long.\n", name);
+                       if (!strchr(buf, '\n')) {
+                               warnx("%s: line too long", name);
                                continue;
                        }
                        lowstr(buf, wbuf);
                                continue;
                        }
                        lowstr(buf, wbuf);
@@ -161,31 +176,33 @@ apropos(argv, path, buildpath)
  * match --
  *     match anywhere the string appears
  */
  * match --
  *     match anywhere the string appears
  */
+int
 match(bp, str)
 match(bp, str)
-       register char *bp, *str;
+       char *bp, *str;
 {
 {
-       register int len;
-       register char test;
+       int len;
+       char test;
 
        if (!*bp)
 
        if (!*bp)
-               return(0);
+               return (0);
        /* backward compatible: everything matches empty string */
        if (!*str)
        /* backward compatible: everything matches empty string */
        if (!*str)
-               return(1);
+               return (1);
        for (test = *str++, len = strlen(str); *bp;)
                if (test == *bp++ && !strncmp(bp, str, len))
        for (test = *str++, len = strlen(str); *bp;)
                if (test == *bp++ && !strncmp(bp, str, len))
-                       return(1);
-       return(0);
+                       return (1);
+       return (0);
 }
 
 /*
  * lowstr --
  *     convert a string to lower case
  */
 }
 
 /*
  * lowstr --
  *     convert a string to lower case
  */
+void
 lowstr(from, to)
 lowstr(from, to)
-       register char *from, *to;
+       char *from, *to;
 {
 {
-       register char ch;
+       char ch;
 
        while ((ch = *from++) && ch != '\n')
                *to++ = isupper(ch) ? tolower(ch) : ch;
 
        while ((ch = *from++) && ch != '\n')
                *to++ = isupper(ch) ? tolower(ch) : ch;
@@ -196,9 +213,11 @@ lowstr(from, to)
  * usage --
  *     print usage message and die
  */
  * usage --
  *     print usage message and die
  */
+void
 usage()
 {
 usage()
 {
+
        (void)fprintf(stderr,
        (void)fprintf(stderr,
-           "usage: apropos [-M path] [-m path] keyword ...\n");
+           "usage: apropos [-C file] [-M path] [-m path] keyword ...\n");
        exit(1);
 }
        exit(1);
 }