summaryrefslogtreecommitdiffstats
path: root/mansearch.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2015-02-27 16:02:10 +0000
committerIngo Schwarze <schwarze@openbsd.org>2015-02-27 16:02:10 +0000
commit1a83fe0518c070d90a4989d137927b786a7732d4 (patch)
tree8ff9bf6798357bdb8a1cf2e05accfe3e574c3e3f /mansearch.c
parent970476b50a74492edd4cf5202324ba6c1edc6229 (diff)
downloadmandoc-1a83fe0518c070d90a4989d137927b786a7732d4.tar.gz
When man(1) and apropos(1) look for a file man1/foo.1 but it's unavailable,
fall back to glob(man1/foo.*), which is more like what old man(1) did. Do this both for file names from the database and for fs_lookup(). This is relevant because some ports install files like man1/xset.1x. Regression reported by patrick keshishian <pkeshish at gmail dot com>.
Diffstat (limited to 'mansearch.c')
-rw-r--r--mansearch.c31
1 files changed, 28 insertions, 3 deletions
diff --git a/mansearch.c b/mansearch.c
index 87664795..72324544 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -24,6 +24,7 @@
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
+#include <glob.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
@@ -412,14 +413,15 @@ buildnames(const struct mansearch *search, struct manpage *mpage,
sqlite3 *db, sqlite3_stmt *s,
uint64_t pageid, const char *path, int form)
{
- char *newnames, *prevsec, *prevarch;
+ glob_t globinfo;
+ char *firstname, *newnames, *prevsec, *prevarch;
const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
size_t i;
- int c;
+ int c, globres;
mpage->file = NULL;
mpage->names = NULL;
- prevsec = prevarch = NULL;
+ firstname = prevsec = prevarch = NULL;
i = 1;
SQL_BIND_INT64(db, s, i, pageid);
while (SQLITE_ROW == (c = sqlite3_step(s))) {
@@ -494,11 +496,34 @@ buildnames(const struct mansearch *search, struct manpage *mpage,
sep2 = *arch == '\0' ? "" : "/";
mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
path, sep1, sec, sep2, arch, name, fsec);
+ if (access(mpage->file, R_OK) != -1)
+ continue;
+
+ /* Handle unusual file name extensions. */
+
+ if (firstname == NULL)
+ firstname = mpage->file;
+ else
+ free(mpage->file);
+ mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.*",
+ path, sep1, sec, sep2, arch, name);
+ globres = glob(mpage->file, 0, NULL, &globinfo);
+ free(mpage->file);
+ mpage->file = globres ? NULL :
+ mandoc_strdup(*globinfo.gl_pathv);
+ globfree(&globinfo);
}
if (c != SQLITE_DONE)
fprintf(stderr, "%s\n", sqlite3_errmsg(db));
sqlite3_reset(s);
+ /* If none of the files is usable, use the first name. */
+
+ if (mpage->file == NULL)
+ mpage->file = firstname;
+ else if (mpage->file != firstname)
+ free(firstname);
+
/* Append one final section to the names. */
if (prevsec != NULL) {