tech@mandoc.bsd.lv
 help / color / mirror / Atom feed
* minimal use of man.conf for mandocdb and apropos
@ 2011-11-15 13:56 Ingo Schwarze
  2011-11-16 16:26 ` Ingo Schwarze
  0 siblings, 1 reply; 2+ messages in thread
From: Ingo Schwarze @ 2011-11-15 13:56 UTC (permalink / raw)
  To: tech

Hi,

here is a first draft to use the _whatdb entry in man.conf(5)
for processing multiple manpage hierarchies during a single
call to mandocdb(8) or apropos(1).

Obviously, this is critical for getting apropos(1) usable in
practice; to replace makewhatis(8) with mandocdb, it's needed
there, too.

A few remarks:
 * For now, i have not copied the full man.conf parser from
   /usr/src/usr.bin/man/config.c, but written a simpler one.
 * Because apropos_search wants to sort the complete results -
   usually spanning multiple databases - we have to pass the
   pathes in and iterate inside apropos_search.
 * Changing the directory into the manpage hierarchies
   is required to handle .so files.  Bleah.
 * When processing multiple databases, mandocdb(8) needs to
   close each database before opening the next one; there's
   a leak right now, fixed here.
 * Adjusting cgi.c is expected to be trivial.
 * Simple adjustments are needed to the mandocdb(8) and apropos(1)
   manuals.
 * The handling of the apropos(1) -C, -M and -m options is still
   missing, but not difficult to implement.
 * Better error handling can be added later.

Thoughts?

Yours,
  Ingo


--- Makefile.orig
+++ Makefile
@@ -18,7 +18,7 @@
 SRCS+=	mdoc_man.c
 SRCS+=	html.c mdoc_html.c man_html.c out.c eqn_html.c
 SRCS+=	term_ps.c term_ascii.c tbl_term.c tbl_html.c
-SRCS+=	mandocdb.c apropos_db.c apropos.c
+SRCS+=	man_conf.c mandocdb.c apropos_db.c apropos.c
 
 PROG=	mandoc
 
--- apropos.c.orig
+++ apropos.c
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "apropos_db.h"
+#include "man_conf.h"
 #include "mandoc.h"
 
 static	int	 cmp(const void *, const void *);
@@ -33,12 +34,14 @@ static	char	*progname;
 int
 apropos(int argc, char *argv[])
 {
+	struct man_conf	 dirs;
 	int		 ch;
 	struct opts	 opts;
 	struct expr	*e;
 	extern int	 optind;
 	extern char	*optarg;
 
+	memset(&dirs, 0, sizeof(struct man_conf));
 	memset(&opts, 0, sizeof(struct opts));
 
 	progname = strrchr(argv[0], '/');
@@ -78,7 +81,10 @@ apropos(int argc, char *argv[])
 	 * The index database is a recno.
 	 */
 
-	apropos_search(&opts, e, NULL, list);
+	man_conf_parse(&dirs);
+	apropos_search(dirs.argc, dirs.argv, &opts, e, NULL, list);
+
+	man_conf_free(&dirs);
 	exprfree(e);
 	return(EXIT_SUCCESS);
 }
--- apropos_db.c.orig
+++ apropos_db.c
@@ -21,6 +21,7 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef __linux__
 # include <db_185.h>
@@ -32,6 +33,11 @@
 #include "apropos_db.h"
 #include "mandoc.h"
 
+struct	rectree {
+	struct rec	*node;
+	int		 len;
+};
+
 struct	expr {
 	int		 regex;
 	int	 	 mask;
@@ -72,6 +78,8 @@ static	int	 index_read(const DBT *, const DBT *,
 static	void	 norm_string(const char *,
 			const struct mchars *, char **);
 static	size_t	 norm_utf8(unsigned int, char[7]);
+static	void	 single_search(struct rectree *, const struct opts *,
+			const struct expr *, struct mchars *);
 
 /*
  * Open the keyword mandoc-db database.
@@ -316,13 +324,47 @@ index_read(const DBT *key, const DBT *val,
  * Call "res" with the results, which may be zero.
  */
 void
-apropos_search(const struct opts *opts, const struct expr *expr,
+apropos_search(int argc, char *argv[],
+		const struct opts *opts, const struct expr *expr,
 		void *arg, void (*res)(struct rec *, size_t, void *))
 {
-	int		 i, len, root, leaf;
+	struct rectree	 tree;
+	struct mchars	*mc;
+	int		 i;
+
+	memset(&tree, 0, sizeof(struct rectree));
+
+	/* XXX: error out with bad regexp? */
+
+	mc = mchars_alloc();
+
+	for (i = 0; i < argc; i++) {
+		if (chdir(argv[i]))
+			continue;
+		single_search(&tree, opts, expr, mc);
+	}
+
+	(*res)(tree.node, tree.len, arg);
+
+	for (i = 0; i < tree.len; i++) {
+		free(tree.node[i].file);
+		free(tree.node[i].cat);
+		free(tree.node[i].title);
+		free(tree.node[i].arch);
+		free(tree.node[i].desc);
+	}
+
+	if (mc)
+		mchars_free(mc);
+}
+
+static void
+single_search(struct rectree *tree, const struct opts *opts,
+		const struct expr *expr, struct mchars *mc)
+{
+	int		 root, leaf;
 	DBT		 key, val;
 	DB		*btree, *idx;
-	struct mchars	*mc;
 	int		 ch;
 	char		*buf;
 	recno_t		 rec;
@@ -333,17 +375,11 @@ apropos_search(const struct opts *opts, const struct expr *expr,
 	leaf	= -1;
 	btree	= NULL;
 	idx	= NULL;
-	mc	= NULL;
 	buf	= NULL;
-	recs	= NULL;
-	len	= 0;
+	recs	= tree->node;
 
 	memset(&srec, 0, sizeof(struct rec));
 
-	/* XXX: error out with bad regexp? */
-
-	mc = mchars_alloc();
-
 	/* XXX: return fact that we've errored? */
 
 	if (NULL == (btree = btree_open())) 
@@ -404,53 +440,40 @@ apropos_search(const struct opts *opts, const struct expr *expr,
 		if (opts->arch && strcasecmp(opts->arch, srec.arch))
 			continue;
 
-		recs = mandoc_realloc
-			(recs, (len + 1) * sizeof(struct rec));
+		tree->node = recs = mandoc_realloc
+			(recs, (tree->len + 1) * sizeof(struct rec));
 
-		memcpy(&recs[len], &srec, sizeof(struct rec));
+		memcpy(&recs[tree->len], &srec, sizeof(struct rec));
 
 		/* Append to our tree. */
 
 		if (leaf >= 0) {
 			if (rec > recs[leaf].rec)
-				recs[leaf].rhs = len;
+				recs[leaf].rhs = tree->len;
 			else
-				recs[leaf].lhs = len;
+				recs[leaf].lhs = tree->len;
 		} else
-			root = len;
+			root = tree->len;
 		
 		memset(&srec, 0, sizeof(struct rec));
-		len++;
+		tree->len++;
 	}
 
-	if (1 == ch)
-		(*res)(recs, len, arg);
+	/* XXX handle database errors? */
 
-	/* XXX: else?  corrupt database error? */
 out:
-	for (i = 0; i < len; i++) {
-		free(recs[i].file);
-		free(recs[i].cat);
-		free(recs[i].title);
-		free(recs[i].arch);
-		free(recs[i].desc);
-	}
-
 	free(srec.file);
 	free(srec.cat);
 	free(srec.title);
 	free(srec.arch);
 	free(srec.desc);
 
-	if (mc)
-		mchars_free(mc);
 	if (btree)
 		(*btree->close)(btree);
 	if (idx)
 		(*idx->close)(idx);
 
 	free(buf);
-	free(recs);
 }
 
 struct expr *
--- apropos_db.h.orig
+++ apropos_db.h
@@ -45,7 +45,7 @@ __BEGIN_DECLS
 
 struct	expr;
 
-void	 	 apropos_search(const struct opts *, 
+void	 	 apropos_search(int, char **, const struct opts *, 
 			const struct expr *, void *, 
 			void (*)(struct rec *, size_t, void *));
 
--- /dev/null
+++ man_conf.c
@@ -0,0 +1,73 @@
+/*      $Id$ */
+/*
+ * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/types.h>
+#include <assert.h>
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mandoc.h"
+#include "man_conf.h"
+
+#define	MAN_CONF_FILE	"/etc/man.conf"
+
+void
+man_conf_parse(struct man_conf *dirs) {
+	FILE	*stream;
+	char	*p, *q;
+	size_t	 len;
+	int	 maxarg;
+
+	if (NULL == (stream = fopen(MAN_CONF_FILE, "r")))
+		return;
+
+	maxarg = 0;
+
+	while (NULL != (p = fgetln(stream, &len)) && '\n' == p[--len]) {
+		p[len] = '\0';
+		while (isspace(*p))
+			p++;
+		if (strncmp("_whatdb", p, 7))
+			continue;
+		p += 7;
+		while (isspace(*p))
+			p++;
+		if ('\0' == *p)
+			continue;
+		if (NULL == (q = strrchr(p, '/')))
+			continue;
+		*q = '\0';
+		if (dirs->argc == maxarg) {
+			maxarg += 8;
+			assert(0 < maxarg);
+			dirs->argv = realloc(dirs->argv, (size_t)maxarg);
+		}
+		dirs->argv[dirs->argc++] = strdup(p);
+	}
+
+	fclose(stream);
+}
+
+void
+man_conf_free(struct man_conf *dirs) {
+
+	while (dirs->argc--)
+		free(dirs->argv[dirs->argc]);
+	free(dirs->argv);
+}
--- /dev/null
+++ man_conf.h
@@ -0,0 +1,24 @@
+/*      $Id$ */
+/*
+ * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+struct	man_conf {
+	int	   argc;
+	char	 **argv;
+};
+
+void	 man_conf_parse(struct man_conf *);
+void	 man_conf_free(struct man_conf *);
--- mandocdb.c.orig
+++ mandocdb.c
@@ -24,12 +24,14 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <db.h>
 
 #include "man.h"
 #include "mdoc.h"
 #include "mandoc.h"
 #include "mandocdb.h"
+#include "man_conf.h"
 
 #define	MANDOC_BUFSZ	  BUFSIZ
 #define	MANDOC_SLOP	  1024
@@ -242,6 +244,7 @@ static	const char	 *progname;
 int
 mandocdb(int argc, char *argv[])
 {
+	struct man_conf	 dirs;
 	struct mparse	*mp; /* parse sequence */
 	enum op		 op; /* current operation */
 	const char	*dir;
@@ -307,6 +310,7 @@ mandocdb(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	memset(&dirs, 0, sizeof(struct man_conf));
 	memset(&info, 0, sizeof(BTREEINFO));
 	info.flags = R_DUP;
 
@@ -363,14 +367,22 @@ mandocdb(int argc, char *argv[])
 		index_prune(of, db, fbuf, idx, ibuf, verb,
 				&maxrec, &recs, &recsz);
 
-		if (OP_UPDATE == op)
+		if (OP_UPDATE == op) {
+			chdir(dir);
 			index_merge(of, mp, &dbuf, &buf, hash, 
 					db, fbuf, idx, ibuf, use_all,
 					verb, maxrec, recs, reccur);
+		}
 
 		goto out;
 	}
 
+	if (0 == argc) {
+		man_conf_parse(&dirs);
+		argc = dirs.argc;
+		argv = dirs.argv;
+	}
+
 	for (i = 0; i < argc; i++) {
 		ibuf[0] = fbuf[0] = '\0';
 
@@ -387,6 +399,11 @@ mandocdb(int argc, char *argv[])
 			exit((int)MANDOCLEVEL_BADARG);
 		}
 
+		if (db)
+			(*db->close)(db);
+		if (idx)
+			(*idx->close)(idx);
+
 		db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
 		idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
 
@@ -415,6 +432,7 @@ mandocdb(int argc, char *argv[])
 
 		of = of->first;
 
+		chdir(argv[i]);
 		index_merge(of, mp, &dbuf, &buf, hash, db, fbuf, 
 				idx, ibuf, use_all, verb,
 				maxrec, recs, reccur);
@@ -429,6 +447,8 @@ out:
 		(*hash->close)(hash);
 	if (mp)
 		mparse_free(mp);
+	if (dirs.argc)
+		man_conf_free(&dirs);
 
 	ofile_free(of);
 	free(buf.cp);
--
 To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv

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

* Re: minimal use of man.conf for mandocdb and apropos
  2011-11-15 13:56 minimal use of man.conf for mandocdb and apropos Ingo Schwarze
@ 2011-11-16 16:26 ` Ingo Schwarze
  0 siblings, 0 replies; 2+ messages in thread
From: Ingo Schwarze @ 2011-11-16 16:26 UTC (permalink / raw)
  To: tech

Hi,

> here is a first draft to use the _whatdb entry in man.conf(5)
> for processing multiple manpage hierarchies during a single
> call to mandocdb(8) or apropos(1).

Updated after resolving the conflicts with kristaps@' recent
work on logical operators.

> Obviously, this is critical for getting apropos(1) usable in
> practice; to replace makewhatis(8) with mandocdb, it's needed
> there, too.
> 
> A few remarks:
>  * For now, i have not copied the full man.conf parser from
>    /usr/src/usr.bin/man/config.c, but written a simpler one.
>  * Because apropos_search wants to sort the complete results -
>    usually spanning multiple databases - we have to pass the
>    pathes in and iterate inside apropos_search.
>  * Changing the directory into the manpage hierarchies
>    is required to handle .so files.  Bleah.
>  * When processing multiple databases, mandocdb(8) needs to
>    close each database before opening the next one; there's
>    a leak right now, fixed here.
>  * Adjusting cgi.c is expected to be trivial.
>  * Simple adjustments are needed to the mandocdb(8) and apropos(1)
>    manuals.
>  * The handling of the apropos(1) -C, -M and -m options is still
>    missing, but not difficult to implement.

An implementation of apropos -M and -m is now contained as well.

OK?
  Ingo


Index: Makefile
===================================================================
RCS file: /cvs/src/usr.bin/mandoc/Makefile,v
retrieving revision 1.63
diff -u -p -r1.63 Makefile
--- Makefile	13 Nov 2011 09:58:21 -0000	1.63
+++ Makefile	16 Nov 2011 16:23:46 -0000
@@ -18,7 +18,7 @@ SRCS+=	main.c mdoc_term.c chars.c term.c
 SRCS+=	mdoc_man.c
 SRCS+=	html.c mdoc_html.c man_html.c out.c eqn_html.c
 SRCS+=	term_ps.c term_ascii.c tbl_term.c tbl_html.c
-SRCS+=	mandocdb.c apropos_db.c apropos.c
+SRCS+=	man_conf.c mandocdb.c apropos_db.c apropos.c
 
 PROG=	mandoc
 
Index: apropos.c
===================================================================
RCS file: /cvs/src/usr.bin/mandoc/apropos.c,v
retrieving revision 1.5
diff -u -p -r1.5 apropos.c
--- apropos.c	16 Nov 2011 13:23:27 -0000	1.5
+++ apropos.c	16 Nov 2011 16:23:46 -0000
@@ -22,6 +22,7 @@
 #include <string.h>
 
 #include "apropos_db.h"
+#include "man_conf.h"
 #include "mandoc.h"
 
 static	int	 cmp(const void *, const void *);
@@ -33,14 +34,17 @@ static	char	*progname;
 int
 apropos(int argc, char *argv[])
 {
-	int		 ch;
+	struct man_conf	 dirs;
+	int		 ch, use_man_conf;
 	size_t		 terms;
 	struct opts	 opts;
 	struct expr	*e;
 	extern int	 optind;
 	extern char	*optarg;
 
+	memset(&dirs, 0, sizeof(struct man_conf));
 	memset(&opts, 0, sizeof(struct opts));
+	use_man_conf = 1;
 
 	progname = strrchr(argv[0], '/');
 	if (progname == NULL)
@@ -48,8 +52,14 @@ apropos(int argc, char *argv[])
 	else
 		++progname;
 
-	while (-1 != (ch = getopt(argc, argv, "S:s:"))) 
+	while (-1 != (ch = getopt(argc, argv, "M:m:S:s:"))) 
 		switch (ch) {
+		case ('M'):
+			use_man_conf = 0;
+			/* FALLTHROUGH */
+		case ('m'):
+			manpath_parse(&dirs, optarg);
+			break;
 		case ('S'):
 			opts.arch = optarg;
 			break;
@@ -79,7 +89,12 @@ apropos(int argc, char *argv[])
 	 * The index database is a recno.
 	 */
 
-	apropos_search(&opts, e, terms, NULL, list);
+	if (use_man_conf)
+		man_conf_parse(&dirs);
+	apropos_search(dirs.argc, dirs.argv, &opts,
+			e, terms, NULL, list);
+
+	man_conf_free(&dirs);
 	exprfree(e);
 	return(EXIT_SUCCESS);
 }
@@ -113,7 +128,8 @@ usage(void)
 {
 
 	fprintf(stderr, "usage: %s "
-			"[-I] "
+			"[-M path] "
+			"[-m path] "
 			"[-S arch] "
 			"[-s section] "
 			"EXPR\n", 
Index: apropos_db.c
===================================================================
RCS file: /cvs/src/usr.bin/mandoc/apropos_db.c,v
retrieving revision 1.4
diff -u -p -r1.4 apropos_db.c
--- apropos_db.c	16 Nov 2011 13:23:27 -0000	1.4
+++ apropos_db.c	16 Nov 2011 16:23:46 -0000
@@ -21,6 +21,7 @@
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef __linux__
 # include <db_185.h>
@@ -32,6 +33,11 @@
 #include "apropos_db.h"
 #include "mandoc.h"
 
+struct	rectree {
+	struct rec	*node;
+	int		 len;
+};
+
 struct	expr {
 	int		 regex;
 	int		 index;
@@ -79,6 +85,9 @@ static	void	 norm_string(const char *,
 			const struct mchars *, char **);
 static	size_t	 norm_utf8(unsigned int, char[7]);
 static	void	 recfree(struct rec *);
+static	void	 single_search(struct rectree *, const struct opts *,
+			const struct expr *, size_t terms,
+			struct mchars *);
 
 /*
  * Open the keyword mandoc-db database.
@@ -323,35 +332,73 @@ index_read(const DBT *key, const DBT *va
  * Call "res" with the results, which may be zero.
  */
 void
-apropos_search(const struct opts *opts, const struct expr *expr,
-		size_t terms, void *arg, 
+apropos_search(int argc, char *argv[], const struct opts *opts,
+		const struct expr *expr, size_t terms, void *arg, 
 		void (*res)(struct rec *, size_t, void *))
 {
-	int		 i, len, root, leaf, mask, mlen;
+	struct rectree	 tree;
+	struct mchars	*mc;
+	struct rec	*recs;
+	int		 i, mlen;
+
+	memset(&tree, 0, sizeof(struct rectree));
+
+	/* XXX: error out with bad regexp? */
+
+	mc = mchars_alloc();
+
+	for (i = 0; i < argc; i++) {
+		if (chdir(argv[i]))
+			continue;
+		single_search(&tree, opts, expr, terms, mc);
+	}
+
+	/*
+	 * Count the matching files
+	 * and feed them to the output handler.
+	 */
+
+	for (mlen = i = 0; i < tree.len; i++)
+		if (tree.node[i].matches[0])
+			mlen++;
+	recs = mandoc_malloc(mlen * sizeof(struct rec));
+	for (mlen = i = 0; i < tree.len; i++)
+		if (tree.node[i].matches[0])
+			memcpy(&recs[mlen++], &tree.node[i], 
+					sizeof(struct rec));
+	(*res)(recs, mlen, arg);
+	free(recs);
+
+	for (i = 0; i < tree.len; i++)
+		recfree(&tree.node[i]);
+
+	if (mc)
+		mchars_free(mc);
+}
+
+static void
+single_search(struct rectree *tree, const struct opts *opts,
+		const struct expr *expr, size_t terms,
+		struct mchars *mc)
+{
+	int		 root, leaf, mask;
 	DBT		 key, val;
 	DB		*btree, *idx;
-	struct mchars	*mc;
 	int		 ch;
 	char		*buf;
 	recno_t		 rec;
-	struct rec	*recs, *rrecs;
+	struct rec	*recs;
 	struct rec	 srec;
 
 	root	= -1;
 	leaf	= -1;
 	btree	= NULL;
 	idx	= NULL;
-	mc	= NULL;
 	buf	= NULL;
-	recs	= NULL;
-	len	= 0;
+	recs	= tree->node;
 
 	memset(&srec, 0, sizeof(struct rec));
 
-	/* XXX: error out with bad regexp? */
-
-	mc = mchars_alloc();
-
 	/* XXX: return fact that we've errored? */
 
 	if (NULL == (btree = btree_open())) 
@@ -423,60 +470,42 @@ apropos_search(const struct opts *opts, 
 		if (opts->arch && strcasecmp(opts->arch, srec.arch))
 			continue;
 
-		recs = mandoc_realloc
-			(recs, (len + 1) * sizeof(struct rec));
+		tree->node = recs = mandoc_realloc
+			(recs, (tree->len + 1) * sizeof(struct rec));
 
-		memcpy(&recs[len], &srec, sizeof(struct rec));
-		recs[len].matches = 
+		memcpy(&recs[tree->len], &srec, sizeof(struct rec));
+		recs[tree->len].matches = 
 			mandoc_calloc(terms + 1, sizeof(int));
 
 		exprexecpost
 			(expr, buf, mask, 
-			 recs[len].matches, terms);
+			 recs[tree->len].matches, terms);
 
 		/* Append to our tree. */
 
 		if (leaf >= 0) {
 			if (rec > recs[leaf].rec)
-				recs[leaf].rhs = len;
+				recs[leaf].rhs = tree->len;
 			else
-				recs[leaf].lhs = len;
+				recs[leaf].lhs = tree->len;
 		} else
-			root = len;
+			root = tree->len;
 		
 		memset(&srec, 0, sizeof(struct rec));
-		len++;
+		tree->len++;
 	}
 
-	if (1 == ch) {
-		for (mlen = i = 0; i < len; i++)
-			if (recs[i].matches[0])
-				mlen++;
-		rrecs = mandoc_malloc(mlen * sizeof(struct rec));
-		for (mlen = i = 0; i < len; i++)
-			if (recs[i].matches[0])
-				memcpy(&rrecs[mlen++], &recs[i], 
-						sizeof(struct rec));
-		(*res)(rrecs, mlen, arg);
-		free(rrecs);
-	}
+	/* XXX handle database errors? */
 
-	/* XXX: else?  corrupt database error? */
 out:
-	for (i = 0; i < len; i++)
-		recfree(&recs[i]);
-
 	recfree(&srec);
 
-	if (mc)
-		mchars_free(mc);
 	if (btree)
 		(*btree->close)(btree);
 	if (idx)
 		(*idx->close)(idx);
 
 	free(buf);
-	free(recs);
 }
 
 static void
Index: apropos_db.h
===================================================================
RCS file: /cvs/src/usr.bin/mandoc/apropos_db.h,v
retrieving revision 1.4
diff -u -p -r1.4 apropos_db.h
--- apropos_db.h	16 Nov 2011 13:23:27 -0000	1.4
+++ apropos_db.h	16 Nov 2011 16:23:46 -0000
@@ -46,7 +46,7 @@ __BEGIN_DECLS
 
 struct	expr;
 
-void	 	 apropos_search(const struct opts *, 
+void	 	 apropos_search(int, char **, const struct opts *, 
 			const struct expr *, size_t, void *, 
 			void (*)(struct rec *, size_t, void *));
 struct	expr	*exprcomp(int, char *[], size_t *);
Index: mandocdb.c
===================================================================
RCS file: /cvs/src/usr.bin/mandoc/mandocdb.c,v
retrieving revision 1.6
diff -u -p -r1.6 mandocdb.c
--- mandocdb.c	14 Nov 2011 18:52:05 -0000	1.6
+++ mandocdb.c	16 Nov 2011 16:23:46 -0000
@@ -24,12 +24,14 @@
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <db.h>
 
 #include "man.h"
 #include "mdoc.h"
 #include "mandoc.h"
 #include "mandocdb.h"
+#include "man_conf.h"
 
 #define	MANDOC_BUFSZ	  BUFSIZ
 #define	MANDOC_SLOP	  1024
@@ -242,6 +244,7 @@ static	const char	 *progname;
 int
 mandocdb(int argc, char *argv[])
 {
+	struct man_conf	 dirs;
 	struct mparse	*mp; /* parse sequence */
 	enum op		 op; /* current operation */
 	const char	*dir;
@@ -307,6 +310,7 @@ mandocdb(int argc, char *argv[])
 	argc -= optind;
 	argv += optind;
 
+	memset(&dirs, 0, sizeof(struct man_conf));
 	memset(&info, 0, sizeof(BTREEINFO));
 	info.flags = R_DUP;
 
@@ -363,14 +367,22 @@ mandocdb(int argc, char *argv[])
 		index_prune(of, db, fbuf, idx, ibuf, verb,
 				&maxrec, &recs, &recsz);
 
-		if (OP_UPDATE == op)
+		if (OP_UPDATE == op) {
+			chdir(dir);
 			index_merge(of, mp, &dbuf, &buf, hash, 
 					db, fbuf, idx, ibuf, use_all,
 					verb, maxrec, recs, reccur);
+		}
 
 		goto out;
 	}
 
+	if (0 == argc) {
+		man_conf_parse(&dirs);
+		argc = dirs.argc;
+		argv = dirs.argv;
+	}
+
 	for (i = 0; i < argc; i++) {
 		ibuf[0] = fbuf[0] = '\0';
 
@@ -387,6 +399,11 @@ mandocdb(int argc, char *argv[])
 			exit((int)MANDOCLEVEL_BADARG);
 		}
 
+		if (db)
+			(*db->close)(db);
+		if (idx)
+			(*idx->close)(idx);
+
 		db = dbopen(fbuf, flags, 0644, DB_BTREE, &info);
 		idx = dbopen(ibuf, flags, 0644, DB_RECNO, NULL);
 
@@ -415,6 +432,7 @@ mandocdb(int argc, char *argv[])
 
 		of = of->first;
 
+		chdir(argv[i]);
 		index_merge(of, mp, &dbuf, &buf, hash, db, fbuf, 
 				idx, ibuf, use_all, verb,
 				maxrec, recs, reccur);
@@ -429,6 +447,8 @@ out:
 		(*hash->close)(hash);
 	if (mp)
 		mparse_free(mp);
+	if (dirs.argc)
+		man_conf_free(&dirs);
 
 	ofile_free(of);
 	free(buf.cp);
--
 To unsubscribe send an email to tech+unsubscribe@mdocml.bsd.lv

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

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

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-15 13:56 minimal use of man.conf for mandocdb and apropos Ingo Schwarze
2011-11-16 16:26 ` Ingo Schwarze

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