source@mandoc.bsd.lv
 help / color / mirror / Atom feed
From: schwarze@mdocml.bsd.lv
To: source@mdocml.bsd.lv
Subject: mdocml: When man(1) and apropos(1) look for a file man1/foo.1 but it's
Date: Fri, 27 Feb 2015 11:02:40 -0500 (EST)	[thread overview]
Message-ID: <3201952353371120498.enqueue@fantadrom.bsd.lv> (raw)

Log Message:
-----------
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>.

Modified Files:
--------------
    mdocml:
        main.c
        mansearch.c

Revision Data
-------------
Index: mansearch.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/mansearch.c,v
retrieving revision 1.53
retrieving revision 1.54
diff -Lmansearch.c -Lmansearch.c -u -p -r1.53 -r1.54
--- mansearch.c
+++ 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 *searc
 		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,10 +496,33 @@ buildnames(const struct mansearch *searc
 		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. */
 
Index: main.c
===================================================================
RCS file: /home/cvs/mdocml/mdocml/main.c,v
retrieving revision 1.221
retrieving revision 1.222
diff -Lmain.c -Lmain.c -u -p -r1.221 -r1.222
--- main.c
+++ main.c
@@ -25,6 +25,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <glob.h>
 #include <stdio.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -517,16 +518,16 @@ fs_lookup(const struct manpaths *paths, 
 	const char *sec, const char *arch, const char *name,
 	struct manpage **res, size_t *ressz)
 {
+	glob_t		 globinfo;
 	struct manpage	*page;
 	char		*file;
-	int		 form;
+	int		 form, globres;
 
+	form = FORM_SRC;
 	mandoc_asprintf(&file, "%s/man%s/%s.%s",
 	    paths->paths[ipath], sec, name, sec);
-	if (access(file, R_OK) != -1) {
-		form = FORM_SRC;
+	if (access(file, R_OK) != -1)
 		goto found;
-	}
 	free(file);
 
 	mandoc_asprintf(&file, "%s/cat%s/%s.0",
@@ -540,13 +541,23 @@ fs_lookup(const struct manpaths *paths, 
 	if (arch != NULL) {
 		mandoc_asprintf(&file, "%s/man%s/%s/%s.%s",
 		    paths->paths[ipath], sec, arch, name, sec);
-		if (access(file, R_OK) != -1) {
-			form = FORM_SRC;
+		if (access(file, R_OK) != -1)
 			goto found;
-		}
 		free(file);
 	}
-	return(0);
+
+	mandoc_asprintf(&file, "%s/man%s/%s.*",
+	    paths->paths[ipath], sec, name);
+	globres = glob(file, 0, NULL, &globinfo);
+	if (globres != 0 && globres != GLOB_NOMATCH)
+		fprintf(stderr, "%s: %s: glob: %s\n",
+		    progname, file, strerror(errno));
+	free(file);
+	if (globres == 0)
+		file = mandoc_strdup(*globinfo.gl_pathv);
+	globfree(&globinfo);
+	if (globres != 0)
+		return(0);
 
 found:
 #if HAVE_SQLITE3
--
 To unsubscribe send an email to source+unsubscribe@mdocml.bsd.lv

                 reply	other threads:[~2015-02-27 16:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3201952353371120498.enqueue@fantadrom.bsd.lv \
    --to=schwarze@mdocml.bsd.lv \
    --cc=source@mdocml.bsd.lv \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).