summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--apropos.130
-rw-r--r--apropos.c43
-rw-r--r--apropos_db.c30
3 files changed, 50 insertions, 53 deletions
diff --git a/apropos.1 b/apropos.1
index 839f22bb..3466549f 100644
--- a/apropos.1
+++ b/apropos.1
@@ -37,10 +37,11 @@ for each file in each database.
Its arguments are as follows:
.Bl -tag -width Ds
.It Fl m Ar manpath
-A colon-separated list of paths containing
+Append the colon-separated paths to the default list of paths searched
+for
.Xr mandocdb 8
databases.
-Paths may be relative or absolute.
+Invalid paths, or paths without manual databases, are ignored.
.It Fl S Ar arch
Search only for a particular architecture.
.It Fl s Ar cat
@@ -111,7 +112,7 @@ is evaluated case-insensitively.
.Pp
By default,
.Nm
-searches for
+searches for a
.Xr mandocdb 8
database in the current working directory and
parses terms as case-sensitive regular expressions
@@ -143,12 +144,13 @@ Resulting manuals may be accessed as
If an architecture is specified in the output, use
.Pp
.Dl $ man \-s sec \-S arch title
-.\" .Sh IMPLEMENTATION NOTES
-.\" Not used in OpenBSD.
-.\" .Sh RETURN VALUES
-.\" For sections 2, 3, & 9 only.
-.\" .Sh ENVIRONMENT
-.\" For sections 1, 6, 7, & 8 only.
+.Sh ENVIRONMENT
+.Bl -tag -width Ds
+.It Ev MANPATH
+Comma-separated paths overriding the default list of paths searched for
+manual databases.
+Invalid paths, or paths without manual databases, are ignored.
+.El
.\" .Sh FILES
.Sh EXIT STATUS
.Ex -std
@@ -182,23 +184,13 @@ Search for all manuals referencing
in any letter case:
.Pp
.Dl $ apropos \-\- \-i posix
-.\" .Sh DIAGNOSTICS
-.\" For sections 1, 4, 6, 7, & 8 only.
-.\" .Sh ERRORS
-.\" For sections 2, 3, & 9 only.
.Sh SEE ALSO
.Xr man 1 ,
.Xr mandoc 1 ,
.Xr re_format 7
-.\" .Sh STANDARDS
-.\" .Sh HISTORY
.Sh AUTHORS
The
.Nm
utility was written by
.An Kristaps Dzonsons ,
.Mt kristaps@bsd.lv .
-.\" .Sh CAVEATS
-.\" .Sh BUGS
-.\" .Sh SECURITY CONSIDERATIONS
-.\" Not used in OpenBSD.
diff --git a/apropos.c b/apropos.c
index e9d3ac97..70861fa5 100644
--- a/apropos.c
+++ b/apropos.c
@@ -39,8 +39,8 @@ struct manpaths {
static int cmp(const void *, const void *);
static void list(struct res *, size_t, void *);
-static int manpath_add(struct manpaths *, const char *);
-static int manpath_parse(struct manpaths *, char *);
+static void manpath_add(struct manpaths *, const char *);
+static void manpath_parse(struct manpaths *, char *);
static void usage(void);
static char *progname;
@@ -71,8 +71,7 @@ main(int argc, char *argv[])
while (-1 != (ch = getopt(argc, argv, "m:S:s:")))
switch (ch) {
case ('m'):
- if ( ! manpath_parse(&paths, optarg))
- goto out;
+ manpath_parse(&paths, optarg);
break;
case ('S'):
opts.arch = optarg;
@@ -93,12 +92,17 @@ main(int argc, char *argv[])
goto out;
}
- if (0 == paths.sz && ! manpath_add(&paths, "."))
- goto out;
+ /*
+ * Let MANPATH override our default paths.
+ */
+
+ if (NULL != getenv("MANPATH"))
+ manpath_add(&paths, getenv("MANPATH"));
+ else
+ manpath_add(&paths, ".");
if (NULL == (e = exprcomp(argc, argv, &terms))) {
- /* FIXME: be more specific about this. */
- fprintf(stderr, "Bad expression\n");
+ fprintf(stderr, "%s: Bad expression\n", progname);
goto out;
}
@@ -106,7 +110,9 @@ main(int argc, char *argv[])
(paths.sz, paths.paths,
&opts, e, terms, NULL, list);
- /* FIXME: report an error based on ch. */
+ if (0 == rc)
+ fprintf(stderr, "%s: Error reading "
+ "manual database\n", progname);
out:
for (i = 0; i < paths.sz; i++)
@@ -156,38 +162,31 @@ usage(void)
/*
* Parse a FULL pathname from a colon-separated list of arrays.
*/
-static int
+static void
manpath_parse(struct manpaths *dirs, char *path)
{
char *dir;
for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
- if ( ! manpath_add(dirs, dir))
- return(0);
-
- return(1);
+ manpath_add(dirs, dir);
}
/*
- * Add a directory to the array.
+ * Add a directory to the array, ignoring bad directories.
* Grow the array one-by-one for simplicity's sake.
- * Return 0 if the directory is not a real path.
*/
-static int
+static void
manpath_add(struct manpaths *dirs, const char *dir)
{
char buf[PATH_MAX];
char *cp;
- if (NULL == (cp = realpath(dir, buf))) {
- fprintf(stderr, "%s: Invalid path\n", dir);
- return(0);
- }
+ if (NULL == (cp = realpath(dir, buf)))
+ return;
dirs->paths = mandoc_realloc
(dirs->paths,
((size_t)dirs->sz + 1) * sizeof(char *));
dirs->paths[dirs->sz++] = mandoc_strdup(cp);
- return(1);
}
diff --git a/apropos_db.c b/apropos_db.c
index a0e8215a..dcf52453 100644
--- a/apropos_db.c
+++ b/apropos_db.c
@@ -374,14 +374,13 @@ index_read(const DBT *key, const DBT *val,
}
/*
- * Search mandocdb databases in argv (size argc) for the expression
- * "expr".
+ * Search mandocdb databases in paths for expression "expr".
* Filter out by "opts".
* Call "res" with the results, which may be zero.
* Return 0 if there was a database error, else return 1.
*/
int
-apropos_search(int argc, char *argv[], const struct opts *opts,
+apropos_search(int pathsz, char **paths, const struct opts *opts,
const struct expr *expr, size_t terms, void *arg,
void (*res)(struct res *, size_t, void *))
{
@@ -392,19 +391,24 @@ apropos_search(int argc, char *argv[], const struct opts *opts,
memset(&tree, 0, sizeof(struct rectree));
+ rc = 0;
mc = mchars_alloc();
- for (rc = 1, i = 0; rc && i < argc; i++) {
- /* FIXME: ugly warning: we shouldn't get here! */
- if (chdir(argv[i]))
+ /*
+ * Main loop. Change into the directory containing manpage
+ * databases. Run our expession over each database in the set.
+ */
+
+ for (i = 0; i < pathsz; i++) {
+ if (chdir(paths[i]))
continue;
- rc = single_search(&tree, opts, expr, terms, mc);
- /* FIXME: warn and continue... ? */
+ if ( ! single_search(&tree, opts, expr, terms, mc))
+ goto out;
}
/*
- * Count the matching files
- * and feed them to the output handler.
+ * Count matching files, transfer to a "clean" array, then feed
+ * them to the output handler.
*/
for (mlen = i = 0; i < tree.len; i++)
@@ -421,6 +425,8 @@ apropos_search(int argc, char *argv[], const struct opts *opts,
(*res)(ress, mlen, arg);
free(ress);
+ rc = 1;
+out:
for (i = 0; i < tree.len; i++)
recfree(&tree.node[i]);
@@ -454,11 +460,11 @@ single_search(struct rectree *tree, const struct opts *opts,
memset(&r, 0, sizeof(struct rec));
if (NULL == (btree = btree_open()))
- return(0);
+ return(1);
if (NULL == (idx = index_open())) {
(*btree->close)(btree);
- return(0);
+ return(1);
}
while (0 == (ch = (*btree->seq)(btree, &key, &val, R_NEXT))) {