tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
* mandocdb: do not use bogus files
@ 2011-11-13 18:42 Ingo Schwarze
  2011-11-13 20:05 ` Kristaps Dzonsons
  0 siblings, 1 reply; 7+ messages in thread
From: Ingo Schwarze @ 2011-11-13 18:42 UTC (permalink / raw)
  To: tech

Hi,

here is the first step towards usable titles in apropos(1) output.

Currently, if you have a page /usr/share/man/man3p/FooBar::cAmEl.3p,
you are quite lost, because apropos(1) will tell you

  FOOBAR::CAMEL(3p) - my fancy Perl module

but of course, this will not find the page:

  man FOOBAR::CAMEL

Also, imagine /usr/share/man/foobar/bogus.mdoc contained .Dt fancy 6,
then apropos fancy would happily return

  FANCY - my well hidden manual

but of course man(1) will search for it in vain.

Thus, only look for files in reasonable places, and save the filename
stem (title), suffix (section), and architecture in the struct of,
for later use by the indexer.

OK?
  Ingo


--- mandocdb.c.orig
+++ mandocdb.c
@@ -38,6 +38,9 @@
 
 struct	of {
 	char		 *fname; /* heap-allocated */
+	char		 *sec;
+	char		 *arch;
+	char		 *title;
 	struct of	 *next; /* NULL for last one */
 	struct of	 *first; /* first in list */
 };
@@ -85,7 +88,8 @@ static	void		  index_prune(const struct of *, DB *,
 				const char *, DB *, const char *, 
 				int, recno_t *, recno_t **, size_t *);
 static	void		  ofile_argbuild(char *[], int, int, struct of **);
-static	int		  ofile_dirbuild(const char *, int, struct of **);
+static	int		  ofile_dirbuild(const char *, const char *,
+				const char *, int, struct of **);
 static	void		  ofile_free(struct of *);
 static	int		  pman_node(MAN_ARGS);
 static	void		  pmdoc_node(MDOC_ARGS);
@@ -396,7 +400,7 @@ mandocdb(int argc, char *argv[])
 		ofile_free(of);
 		of = NULL;
 
-		if ( ! ofile_dirbuild(argv[i], verb, &of)) 
+		if ( ! ofile_dirbuild(argv[i], NULL, NULL, verb, &of)) 
 			exit((int)MANDOCLEVEL_SYSERR);
 
 		if (NULL == of)
@@ -1180,12 +1184,14 @@ ofile_argbuild(char *argv[], int argc, int verb, struct of **of)
  * Pass in a pointer to a NULL structure for the first invocation.
  */
 static int
-ofile_dirbuild(const char *dir, int verb, struct of **of)
+ofile_dirbuild(const char *dir, const char* psec, const char *parch,
+		int verb, struct of **of)
 {
 	char		 buf[MAXPATHLEN];
 	size_t		 sz;
 	DIR		*d;
-	const char	*fn;
+	const char	*fn, *sec, *arch;
+	char		*suffix;
 	struct of	*nof;
 	struct dirent	*dp;
 
@@ -1194,12 +1200,27 @@ ofile_dirbuild(const char *dir, int verb, struct of **of)
 		return(0);
 	}
 
+	sec = psec;
+	arch = parch;
+
 	while (NULL != (dp = readdir(d))) {
 		fn = dp->d_name;
 		if (DT_DIR == dp->d_type) {
-			if (0 == strcmp(".", fn))
-				continue;
-			if (0 == strcmp("..", fn))
+
+			/*
+	 		 * Don't bother parsing directories
+			 * that man(1) won't find.
+			 */
+
+			if (NULL == psec) {
+				if(strncmp("man", fn, 3))
+					continue;
+				sec = fn + 3;
+				arch = NULL;
+			} else if (NULL == parch &&
+					NULL == strchr(fn, '.'))
+				arch = fn;
+			else
 				continue;
 
 			buf[0] = '\0';
@@ -1207,21 +1228,27 @@ ofile_dirbuild(const char *dir, int verb, struct of **of)
 			strlcat(buf, "/", MAXPATHLEN);
 			sz = strlcat(buf, fn, MAXPATHLEN);
 
-			if (sz < MAXPATHLEN) {
-				if ( ! ofile_dirbuild(buf, verb, of))
-					return(0);
-				continue;
-			} else if (sz < MAXPATHLEN)
-				continue;
+			if (MAXPATHLEN <= sz) {
+				fprintf(stderr, "%s: Path too long\n", dir);
+				return(0);
+			}
+ 
+			if (verb > 2)
+				printf("%s: Scanning\n", buf);
 
-			fprintf(stderr, "%s: Path too long\n", dir);
-			return(0);
+			if ( ! ofile_dirbuild(buf, sec, arch, verb, of))
+				return(0);
 		}
-		if (DT_REG != dp->d_type)
+		if (NULL == sec || DT_REG != dp->d_type)
 			continue;
 
-		if (0 == strcmp(MANDOC_DB, fn) ||
-				0 == strcmp(MANDOC_IDX, fn))
+		/*
+		 * Don't bother parsing files that man(1) won't find.
+		 */
+
+		if (NULL == (suffix = strrchr(fn, '.')))
+			continue;
+		if (strcmp(suffix + 1, sec))
 			continue;
 
 		buf[0] = '\0';
@@ -1235,6 +1262,11 @@ ofile_dirbuild(const char *dir, int verb, struct of **of)
 
 		nof = mandoc_calloc(1, sizeof(struct of));
 		nof->fname = mandoc_strdup(buf);
+		nof->sec = mandoc_strdup(sec);
+		if (NULL != arch)
+			nof->arch = mandoc_strdup(arch);
+		*suffix = '\0';
+		nof->title = mandoc_strdup(fn);
 
 		if (verb > 2)
 			printf("%s: Scheduling\n", buf);
--
 To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2011-11-24 10:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-13 18:42 mandocdb: do not use bogus files Ingo Schwarze
2011-11-13 20:05 ` Kristaps Dzonsons
2011-11-14  0:06   ` Ingo Schwarze
2011-11-14 19:13     ` Ingo Schwarze
2011-11-14 20:07       ` Kristaps Dzonsons
2011-11-14 23:34         ` Ingo Schwarze
2011-11-24 10:10           ` Kristaps Dzonsons

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).