Module Name:    src
Committed By:   riz
Date:           Sat Feb 18 18:03:26 UTC 2012

Modified Files:
        src/etc [netbsd-6]: man.conf
        src/external/bsd/mdocml [netbsd-6]: Makefile.inc
        src/external/bsd/mdocml/dist [netbsd-6]: mandoc.h read.c
        src/usr.sbin/makemandb [netbsd-6]: Makefile apropos.1 apropos.c
            makemandb.c

Log Message:
Pull up following revision(s) (requested by joerg in ticket #4):
        usr.sbin/makemandb/apropos.c: revision 1.5
        external/bsd/mdocml/dist/read.c: revision 1.6
        external/bsd/mdocml/dist/mandoc.h: revision 1.2
        usr.sbin/makemandb/makemandb.c: revision 1.3
        usr.sbin/makemandb/makemandb.c: revision 1.4
        usr.sbin/makemandb/makemandb.c: revision 1.5
        usr.sbin/makemandb/Makefile: revision 1.2
        etc/man.conf: revision 1.30
        usr.sbin/makemandb/apropos.1: revision 1.3
        external/bsd/mdocml/Makefile.inc: revision 1.15
Be a bit more friendly to man pages using the roff .so command by
changing the current directory to the parent of the man -p entry, e.g.
/usr/share/man for /usr/share/man1.
Also handle hyphen replacement if it was used as plain input and no
backslash sequence was used at all in the line.
Add support for apropos -s like in the old apropos. Fix capitalisation.
Add note about optional quotation. From Abhinav Upadhyay.
Merge mandoc.h r1.99 and read.c 1.28 from upstream:
Add mparse_readmem, which allows application to run the parser directly
from memory, e.g. after de-compressing a document.
Add support for compressed man pages in all the usual formats.
Use mmap when dealing with plain files.
Add entries for bzip2 and xz.


To generate a diff of this commit:
cvs rdiff -u -r1.29 -r1.29.2.1 src/etc/man.conf
cvs rdiff -u -r1.14 -r1.14.4.1 src/external/bsd/mdocml/Makefile.inc
cvs rdiff -u -r1.1.1.9 -r1.1.1.9.2.1 src/external/bsd/mdocml/dist/mandoc.h
cvs rdiff -u -r1.5 -r1.5.2.1 src/external/bsd/mdocml/dist/read.c
cvs rdiff -u -r1.1 -r1.1.2.1 src/usr.sbin/makemandb/Makefile
cvs rdiff -u -r1.2 -r1.2.2.1 src/usr.sbin/makemandb/apropos.1 \
    src/usr.sbin/makemandb/makemandb.c
cvs rdiff -u -r1.4 -r1.4.2.1 src/usr.sbin/makemandb/apropos.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/etc/man.conf
diff -u src/etc/man.conf:1.29 src/etc/man.conf:1.29.2.1
--- src/etc/man.conf:1.29	Tue Feb 14 14:03:35 2012
+++ src/etc/man.conf	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-#	$NetBSD: man.conf,v 1.29 2012/02/14 14:03:35 wiz Exp $
+#	$NetBSD: man.conf,v 1.29.2.1 2012/02/18 18:03:26 riz Exp $
 
 # Sheer, raging paranoia...
 _version	BSD.2
@@ -17,18 +17,26 @@ _subdir		cat1 man1 cat8 man8 cat6 man6 c
 # Note the order, .Z must come after .[1-9].Z, or it will match first.
 _suffix		.0
 _build		.0.Z		/usr/bin/zcat %s
-_build		.0.gz		/usr/bin/gunzip -c %s
+_build		.0.bz2		/usr/bin/bzcat %s
+_build		.0.gz		/usr/bin/zcat %s
+_build		.0.xz		/usr/bin/xzcat %s
 _build		.[1-9ln]	/usr/bin/mandoc %s
 _build		.[1-9ln].Z	/usr/bin/zcat %s | /usr/bin/mandoc
-_build		.[1-9ln].gz	/usr/bin/gunzip -c %s | /usr/bin/mandoc
+_build		.[1-9ln].bz2	/usr/bin/bzcat %s | /usr/bin/mandoc
+_build		.[1-9ln].gz	/usr/bin/zcat %s | /usr/bin/mandoc
+_build		.[1-9ln].xz	/usr/bin/xzcat %s | /usr/bin/mandoc
 _build		.tbl		/usr/bin/mandoc %s
 _build		.tbl.Z		/usr/bin/zcat %s | /usr/bin/mandoc
-_build		.tbl.gz		/usr/bin/gunzip -c %s | /usr/bin/mandoc
+_build		.tbl.bz2	/usr/bin/bzcat %s | /usr/bin/mandoc
+_build		.tbl.gz		/usr/bin/zcat %s | /usr/bin/mandoc
+_build		.tbl.xz		/usr/bin/xzcat %s | /usr/bin/mandoc
 
 _build		.me		/usr/bin/nroff -msafer -me %s 2>/dev/null | cat -s
 
-_crunch		.Z		/usr/bin/compress -c > %s
-_crunch		.gz		/usr/bin/gzip -c > %s
+_crunch		.Z		/usr/bin/zcat > %s
+_crunch		.bz		/usr/bin/bzcat > %s
+_crunch		.gz		/usr/bin/zcat > %s
+_crunch		.xz		/usr/bin/xzcat > %s
 
 # Sections and their directories.
 # All paths ending in '/' are the equivalent of entries specifying that

Index: src/external/bsd/mdocml/Makefile.inc
diff -u src/external/bsd/mdocml/Makefile.inc:1.14 src/external/bsd/mdocml/Makefile.inc:1.14.4.1
--- src/external/bsd/mdocml/Makefile.inc:1.14	Wed Oct 12 15:33:38 2011
+++ src/external/bsd/mdocml/Makefile.inc	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile.inc,v 1.14 2011/10/12 15:33:38 njoly Exp $
+# $NetBSD: Makefile.inc,v 1.14.4.1 2012/02/18 18:03:26 riz Exp $
 
 .include <bsd.own.mk>
 
@@ -7,7 +7,7 @@ VERSION!=	cd ${.PARSEDIR}/dist && ${MAKE
 CPPFLAGS+=	-DVERSION=\"${VERSION}\" -DUGLY
 
 .if (${HOSTPROG:U} == "")
-CPPFLAGS+=	-DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRPTIME
+CPPFLAGS+=	-DHAVE_STRLCAT -DHAVE_STRLCPY -DHAVE_STRPTIME -DHAVE_MMAP
 .endif
 
 DISTDIR:=	${.PARSEDIR}/dist

Index: src/external/bsd/mdocml/dist/mandoc.h
diff -u src/external/bsd/mdocml/dist/mandoc.h:1.1.1.9 src/external/bsd/mdocml/dist/mandoc.h:1.1.1.9.2.1
--- src/external/bsd/mdocml/dist/mandoc.h:1.1.1.9	Mon Jan 30 16:44:19 2012
+++ src/external/bsd/mdocml/dist/mandoc.h	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-/*	$Vendor-Id: mandoc.h,v 1.98 2011/11/08 00:15:23 kristaps Exp $ */
+/*	$Vendor-Id: mandoc.h,v 1.99 2012/02/16 20:51:31 joerg Exp $ */
 /*
  * Copyright (c) 2010, 2011 Kristaps Dzonsons <krist...@bsd.lv>
  *
@@ -418,6 +418,8 @@ struct mparse	 *mparse_alloc(enum mparse
 void		  mparse_free(struct mparse *);
 void		  mparse_keep(struct mparse *);
 enum mandoclevel  mparse_readfd(struct mparse *, int, const char *);
+enum mandoclevel  mparse_readmem(struct mparse *, const void *, size_t,
+			const char *);
 void		  mparse_reset(struct mparse *);
 void		  mparse_result(struct mparse *, 
 			struct mdoc **, struct man **);

Index: src/external/bsd/mdocml/dist/read.c
diff -u src/external/bsd/mdocml/dist/read.c:1.5 src/external/bsd/mdocml/dist/read.c:1.5.2.1
--- src/external/bsd/mdocml/dist/read.c:1.5	Mon Feb  6 10:42:44 2012
+++ src/external/bsd/mdocml/dist/read.c	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-/*	$Vendor-Id: read.c,v 1.27 2012/02/05 16:46:15 joerg Exp $ */
+/*	$Vendor-Id: read.c,v 1.28 2012/02/16 20:51:31 joerg Exp $ */
 /*
  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <krist...@bsd.lv>
  * Copyright (c) 2010, 2011 Ingo Schwarze <schwa...@openbsd.org>
@@ -28,6 +28,7 @@
 #include <ctype.h>
 #include <fcntl.h>
 #include <stdarg.h>
+#include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -37,6 +38,7 @@
 #include "libmandoc.h"
 #include "mdoc.h"
 #include "man.h"
+#include "main.h"
 
 #ifndef MAP_FILE
 #define	MAP_FILE	0
@@ -70,7 +72,6 @@ static	void	  resize_buf(struct buf *, s
 static	void	  mparse_buf_r(struct mparse *, struct buf, int);
 static	void	  mparse_readfd_r(struct mparse *, int, const char *, int);
 static	void	  pset(const char *, int, struct mparse *);
-static	void	  pdesc(struct mparse *, const char *, int);
 static	int	  read_whole_file(const char *, int, struct buf *, int *);
 static	void	  mparse_end(struct mparse *);
 
@@ -547,38 +548,6 @@ rerun:
 	free(ln.buf);
 }
 
-static void
-pdesc(struct mparse *curp, const char *file, int fd)
-{
-	struct buf	 blk;
-	int		 with_mmap;
-
-	/*
-	 * Run for each opened file; may be called more than once for
-	 * each full parse sequence if the opened file is nested (i.e.,
-	 * from `so').  Simply sucks in the whole file and moves into
-	 * the parse phase for the file.
-	 */
-
-	if ( ! read_whole_file(file, fd, &blk, &with_mmap)) {
-		curp->file_status = MANDOCLEVEL_SYSERR;
-		return;
-	}
-
-	/* Line number is per-file. */
-
-	curp->line = 1;
-
-	mparse_buf_r(curp, blk, 1);
-
-#ifdef	HAVE_MMAP
-	if (with_mmap)
-		munmap(blk.buf, blk.sz);
-	else
-#endif
-		free(blk.buf);
-}
-
 static int
 read_whole_file(const char *file, int fd, struct buf *fb, int *with_mmap)
 {
@@ -674,29 +643,72 @@ mparse_end(struct mparse *curp)
 }
 
 static void
-mparse_readfd_r(struct mparse *curp, int fd, const char *file, int re)
+mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file,
+		int re)
 {
 	const char	*svfile;
 
+	/* Line number is per-file. */
+	svfile = curp->file;
+	curp->file = file;
+	curp->line = 1;
+
+	mparse_buf_r(curp, blk, 1);
+
+	if (0 == re && MANDOCLEVEL_FATAL > curp->file_status)
+		mparse_end(curp);
+
+	curp->file = svfile;
+}
+
+enum mandoclevel
+mparse_readmem(struct mparse *curp, const void *buf, size_t len,
+		const char *file)
+{
+	struct buf blk;
+
+	blk.buf = UNCONST(buf);
+	blk.sz = len;
+
+	mparse_parse_buffer(curp, blk, file, 0);
+	return(curp->file_status);
+}
+
+static void
+mparse_readfd_r(struct mparse *curp, int fd, const char *file, int re)
+{
+	struct buf	 blk;
+	int		 with_mmap;
+
 	if (-1 == fd)
 		if (-1 == (fd = open(file, O_RDONLY, 0))) {
 			perror(file);
 			curp->file_status = MANDOCLEVEL_SYSERR;
 			return;
 		}
+	/*
+	 * Run for each opened file; may be called more than once for
+	 * each full parse sequence if the opened file is nested (i.e.,
+	 * from `so').  Simply sucks in the whole file and moves into
+	 * the parse phase for the file.
+	 */
 
-	svfile = curp->file;
-	curp->file = file;
+	if ( ! read_whole_file(file, fd, &blk, &with_mmap)) {
+		curp->file_status = MANDOCLEVEL_SYSERR;
+		return;
+	}
 
-	pdesc(curp, file, fd);
+	mparse_parse_buffer(curp, blk, file, re);
 
-	if (0 == re && MANDOCLEVEL_FATAL > curp->file_status)
-		mparse_end(curp);
+#ifdef	HAVE_MMAP
+	if (with_mmap)
+		munmap(blk.buf, blk.sz);
+	else
+#endif
+		free(blk.buf);
 
 	if (STDIN_FILENO != fd && -1 == close(fd))
 		perror(file);
-
-	curp->file = svfile;
 }
 
 enum mandoclevel

Index: src/usr.sbin/makemandb/Makefile
diff -u src/usr.sbin/makemandb/Makefile:1.1 src/usr.sbin/makemandb/Makefile:1.1.2.1
--- src/usr.sbin/makemandb/Makefile:1.1	Tue Feb  7 19:13:32 2012
+++ src/usr.sbin/makemandb/Makefile	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-# $NetBSD: Makefile,v 1.1 2012/02/07 19:13:32 joerg Exp $
+# $NetBSD: Makefile,v 1.1.2.1 2012/02/18 18:03:26 riz Exp $
 
 .include <bsd.own.mk>
 
@@ -22,8 +22,8 @@ CPPFLAGS+=-I${MDIST} -I${.OBJDIR}
 MDOCMLOBJDIR!=	cd ${MDOCDIR}/lib/libmandoc && ${PRINTOBJDIR}
 MDOCMLLIB=	${MDOCMLOBJDIR}/libmandoc.a
 
-DPADD.makemandb+= 	${MDOCMLLIB}
-LDADD.makemandb+= 	-L${MDOCMLOBJDIR} -lmandoc
+DPADD.makemandb+= 	${MDOCMLLIB} ${LIBARCHIVE} ${LIBBZ2} ${LIBLZMA}
+LDADD.makemandb+= 	-L${MDOCMLOBJDIR} -lmandoc -larchive -lbz2 -llzma
 DPADD+=		${LIBSQLITE3} ${LIBM} ${LIBZ} ${LIBUTIL}
 LDADD+=		-lsqlite3 -lm -lz -lutil
 

Index: src/usr.sbin/makemandb/apropos.1
diff -u src/usr.sbin/makemandb/apropos.1:1.2 src/usr.sbin/makemandb/apropos.1:1.2.2.1
--- src/usr.sbin/makemandb/apropos.1:1.2	Fri Feb 10 16:57:44 2012
+++ src/usr.sbin/makemandb/apropos.1	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-.\" $NetBSD: apropos.1,v 1.2 2012/02/10 16:57:44 njoly Exp $
+.\" $NetBSD: apropos.1,v 1.2.2.1 2012/02/18 18:03:26 riz Exp $
 .\"
 .\" Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
 .\" All rights reserved.
@@ -29,7 +29,7 @@
 .\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 .\" SUCH DAMAGE.
 .\"
-.Dd February 6, 2012
+.Dd February 15, 2012
 .Dt APROPOS 1
 .Os
 .Sh NAME
@@ -40,6 +40,7 @@
 .Op Fl 123456789Ccp
 .Op Fl n Ar Number of results
 .Op Fl S Ar machine
+.Op Fl s Ar section
 .Ar query
 .Sh DESCRIPTION
 The
@@ -61,6 +62,8 @@ By default
 .Nm
 will only display the top 10 matches in the output.
 .Pp
+Quotes are optional for specifying multiword queries.
+.Pp
 It supports the following options:
 .Bl -tag -width indent
 .It Fl 1
@@ -91,9 +94,16 @@ The default limit is 10.
 .It Fl p
 Display all matching results and pipe them through a pager (defaulting to
 .Xr more 1 ) .
-.It Fl S
+.It Fl S Ar machine
 Limit the search to the pages for the specified machine architecture.
 By default pages for all architectures are shown in the search results.
+.It Fl s Ar section
+Restrict the search to the specified section of the manual.
+By default, pages from all section are shown.
+This option is for backwards compatibility with the classic version of apropos,
+using it is equivalent to using the
+.Op 123456789
+options directly.
 .El
 .Sh FILES
 .Bl -hang -width -compact
Index: src/usr.sbin/makemandb/makemandb.c
diff -u src/usr.sbin/makemandb/makemandb.c:1.2 src/usr.sbin/makemandb/makemandb.c:1.2.2.1
--- src/usr.sbin/makemandb/makemandb.c:1.2	Tue Feb  7 19:17:16 2012
+++ src/usr.sbin/makemandb/makemandb.c	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: makemandb.c,v 1.2 2012/02/07 19:17:16 joerg Exp $	*/
+/*	$NetBSD: makemandb.c,v 1.2.2.1 2012/02/18 18:03:26 riz Exp $	*/
 /*
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * Copyright (c) 2011 Kristaps Dzonsons <krist...@bsd.lv>
@@ -17,7 +17,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: makemandb.c,v 1.2 2012/02/07 19:17:16 joerg Exp $");
+__RCSID("$NetBSD: makemandb.c,v 1.2.2.1 2012/02/18 18:03:26 riz Exp $");
 
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -26,6 +26,8 @@ __RCSID("$NetBSD: makemandb.c,v 1.2 2012
 #include <ctype.h>
 #include <dirent.h>
 #include <err.h>
+#include <archive.h>
+#include <libgen.h>
 #include <md5.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -93,12 +95,13 @@ typedef struct mandb_rec {
 static void append(secbuff *sbuff, const char *src);
 static void init_secbuffs(mandb_rec *);
 static void free_secbuffs(mandb_rec *);
-static int check_md5(const char *, sqlite3 *, const char *, char **);
+static int check_md5(const char *, sqlite3 *, const char *, char **, void *, size_t);
 static void cleanup(mandb_rec *);
 static void set_section(const struct mdoc *, const struct man *, mandb_rec *);
 static void set_machine(const struct mdoc *, mandb_rec *);
 static int insert_into_db(sqlite3 *, mandb_rec *);
-static	void begin_parse(const char *, struct mparse *, mandb_rec *);
+static	void begin_parse(const char *, struct mparse *, mandb_rec *,
+			 const void *, size_t len);
 static void pmdoc_node(const struct mdoc_node *, mandb_rec *);
 static void pmdoc_Nm(const struct mdoc_node *, mandb_rec *);
 static void pmdoc_Nd(const struct mdoc_node *, mandb_rec *);
@@ -291,7 +294,7 @@ main(int argc, char *argv[])
 {
 	FILE *file;
 	const char *sqlstr, *manconf = NULL;
-	char *line, *command;
+	char *line, *command, *parent;
 	char *errmsg;
 	int ch;
 	struct mparse *mp;
@@ -397,6 +400,9 @@ main(int argc, char *argv[])
 	while ((len = getline(&line, &linesize, file)) != -1) {
 		/* Replace the new line character at the end of string with '\0' */
 		line[len - 1] = '\0';
+		parent = estrdup(line);
+		chdir(dirname(parent));
+		free(parent);
 		/* Traverse the man page directories and parse the pages */
 		traversedir(line, db, mp);
 	}
@@ -587,6 +593,59 @@ update_existing_entry(sqlite3 *db, const
 	sqlite3_finalize(inner_stmt);
 }
 
+/* read_and_decompress --
+ *	Reads the given file into memory. If it is compressed, decompres
+ *	it before returning to the caller.
+ */
+static int
+read_and_decompress(const char *file, void **buf, size_t *len)
+{
+	size_t off;
+	ssize_t r;
+	struct archive *a;
+	struct archive_entry *ae;
+
+	if ((a = archive_read_new()) == NULL)
+		errx(EXIT_FAILURE, "memory allocation failed");
+
+	if (archive_read_support_compression_all(a) != ARCHIVE_OK ||
+	    archive_read_support_format_raw(a) != ARCHIVE_OK ||
+	    archive_read_open_filename(a, file, 65536) != ARCHIVE_OK ||
+	    archive_read_next_header(a, &ae) != ARCHIVE_OK)
+		goto archive_error;
+	*len = 65536;
+	*buf = emalloc(*len);
+	off = 0;
+	for (;;) {
+		r = archive_read_data(a, (char *)*buf + off, *len - off);
+		if (r == ARCHIVE_OK) {
+			archive_read_close(a);
+			*len = off;
+			return 0;
+		}
+		if (r <= 0) {
+			free(*buf);
+			break;
+		}
+		off += r;
+		if (off == *len) {
+			*len *= 2;
+			if (*len < off) {
+				warnx("File too large: %s", file);
+				free(*buf);
+				archive_read_close(a);
+				return -1;
+			}
+			*buf = erealloc(*buf, *len);
+		}
+	}
+
+archive_error:
+	warnx("Error while reading `%s': %s", file, archive_error_string(a));
+	archive_read_close(a);
+	return -1;
+}
+
 /* update_db --
  *	Does an incremental updation of the database by checking the file_cache.
  *	It parses and adds the pages which are present in file_cache,
@@ -601,7 +660,9 @@ update_db(sqlite3 *db, struct mparse *mp
 	sqlite3_stmt *stmt = NULL;
 	const char *file;
 	char *errmsg = NULL;
-	char *buf = NULL;
+	char *md5sum;
+	void *buf;
+	size_t buflen;
 	int new_count = 0;	/* Counter for newly indexed/updated pages */
 	int total_count = 0;	/* Counter for total number of pages */
 	int err_count = 0;	/* Counter for number of failed pages */
@@ -619,14 +680,21 @@ update_db(sqlite3 *db, struct mparse *mp
 		errx(EXIT_FAILURE, "Could not query file cache");
 	}
 
+	buf = NULL;
 	while (sqlite3_step(stmt) == SQLITE_ROW) {
+		free(buf);
 		total_count++;
 		rec->device = sqlite3_column_int64(stmt, 0);
 		rec->inode = sqlite3_column_int64(stmt, 1);
 		rec->mtime = sqlite3_column_int64(stmt, 2);
 		file = (const char *) sqlite3_column_text(stmt, 3);
-		md5_status = check_md5(file, db, "mandb_meta", &buf);
-		assert(buf != NULL);
+		if (read_and_decompress(file, &buf, &buflen)) {
+			err_count++;
+			buf = NULL;
+			continue;
+		}
+		md5_status = check_md5(file, db, "mandb_meta", &md5sum, buf, buflen);
+		assert(md5sum != NULL);
 		if (md5_status == -1) {
 			warnx("An error occurred in checking md5 value"
 			      " for file %s", file);
@@ -642,13 +710,13 @@ update_db(sqlite3 *db, struct mparse *mp
 			struct stat sb;
 			stat(file, &sb);
 			if (S_ISLNK(sb.st_mode)) {
-				free(buf);
+				free(md5sum);
 				link_count++;
 				continue;
 			}
-			update_existing_entry(db, file, buf, rec,
+			update_existing_entry(db, file, md5sum, rec,
 			    &new_count, &link_count, &err_count);
-			free(buf);
+			free(md5sum);
 			continue;
 		}
 
@@ -660,10 +728,10 @@ update_db(sqlite3 *db, struct mparse *mp
 			 */
 			if (mflags.verbosity > 1)
 				printf("Parsing: %s\n", file);
-			rec->md5_hash = buf;
+			rec->md5_hash = md5sum;
 			rec->file_path = estrdup(file);
 			// file_path is freed by insert_into_db itself.
-			begin_parse(file, mp, rec);
+			begin_parse(file, mp, rec, buf, buflen);
 			if (insert_into_db(db, rec) < 0) {
 				warnx("Error in indexing %s", file);
 				err_count++;
@@ -672,7 +740,8 @@ update_db(sqlite3 *db, struct mparse *mp
 			}
 		}
 	}
-	
+	free(buf);
+
 	sqlite3_finalize(stmt);
 	
 	if (mflags.verbosity) {
@@ -711,7 +780,8 @@ update_db(sqlite3 *db, struct mparse *mp
  *  parses the man page using libmandoc
  */
 static void
-begin_parse(const char *file, struct mparse *mp, mandb_rec *rec)
+begin_parse(const char *file, struct mparse *mp, mandb_rec *rec,
+    const void *buf, size_t len)
 {
 	struct mdoc *mdoc;
 	struct man *man;
@@ -719,7 +789,7 @@ begin_parse(const char *file, struct mpa
 
 	rec->xr_found = 0;
 
-	if (mparse_readfd(mp, -1, file) >= MANDOCLEVEL_FATAL) {
+	if (mparse_readmem(mp, buf, len, file) >= MANDOCLEVEL_FATAL) {
 		warnx("%s: Parse failure", file);
 		return;
 	}
@@ -1675,7 +1745,8 @@ insert_into_db(sqlite3 *db, mandb_rec *r
  *  1: If the hash exists in the database.
  */
 static int
-check_md5(const char *file, sqlite3 *db, const char *table, char **buf)
+check_md5(const char *file, sqlite3 *db, const char *table, char **md5sum,
+    void *buf, size_t buflen)
 {
 	int rc = 0;
 	int idx = -1;
@@ -1683,8 +1754,8 @@ check_md5(const char *file, sqlite3 *db,
 	sqlite3_stmt *stmt = NULL;
 
 	assert(file != NULL);
-	*buf = MD5File(file, NULL);
-	if (*buf == NULL) {
+	*md5sum = MD5Data(buf, buflen, NULL);
+	if (*md5sum == NULL) {
 		warn("md5 failed: %s", file);
 		return -1;
 	}
@@ -1694,19 +1765,19 @@ check_md5(const char *file, sqlite3 *db,
 	rc = sqlite3_prepare_v2(db, sqlstr, -1, &stmt, NULL);
 	if (rc != SQLITE_OK) {
 		free(sqlstr);
-		free(*buf);
-		*buf = NULL;
+		free(*md5sum);
+		*md5sum = NULL;
 		return -1;
 	}
 
 	idx = sqlite3_bind_parameter_index(stmt, ":md5_hash");
-	rc = sqlite3_bind_text(stmt, idx, *buf, -1, NULL);
+	rc = sqlite3_bind_text(stmt, idx, *md5sum, -1, NULL);
 	if (rc != SQLITE_OK) {
 		warnx("%s", sqlite3_errmsg(db));
 		sqlite3_finalize(stmt);
 		free(sqlstr);
-		free(*buf);
-		*buf = NULL;
+		free(*md5sum);
+		*md5sum = NULL;
 		return -1;
 	}
 
@@ -1842,6 +1913,14 @@ free_secbuffs(mandb_rec *rec)
 	free(rec->errors.data);
 }
 
+static void
+replace_hyph(char *str)
+{
+	char *iter = str;
+	while ((iter = strchr(iter, ASCII_HYPH)) != NULL)
+		*iter = '-';
+}
+
 static char *
 parse_escape(const char *str)
 {
@@ -1853,8 +1932,11 @@ parse_escape(const char *str)
 
 	last_backslash = str;
 	backslash = strchr(str, '\\');
-	if (backslash == NULL)
-		return estrdup(str);
+	if (backslash == NULL) {
+		result = estrdup(str);
+		replace_hyph(result);
+		return result;
+	}
 
 	result = emalloc(strlen(str) + 1);
 	iter = result;
@@ -1878,9 +1960,8 @@ parse_escape(const char *str)
 	} while (backslash != NULL);
 	if (last_backslash != NULL)
 		strcpy(iter, last_backslash);
-	iter = result;
-	while ((iter = strchr(iter, ASCII_HYPH)) != NULL)
-		*iter = '-';
+	
+	replace_hyph(result);
 	return result;
 }
 

Index: src/usr.sbin/makemandb/apropos.c
diff -u src/usr.sbin/makemandb/apropos.c:1.4 src/usr.sbin/makemandb/apropos.c:1.4.2.1
--- src/usr.sbin/makemandb/apropos.c:1.4	Tue Feb  7 23:03:34 2012
+++ src/usr.sbin/makemandb/apropos.c	Sat Feb 18 18:03:26 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: apropos.c,v 1.4 2012/02/07 23:03:34 joerg Exp $	*/
+/*	$NetBSD: apropos.c,v 1.4.2.1 2012/02/18 18:03:26 riz Exp $	*/
 /*-
  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadh...@gmail.com>
  * All rights reserved.
@@ -31,7 +31,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos.c,v 1.4 2012/02/07 23:03:34 joerg Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.4.2.1 2012/02/18 18:03:26 riz Exp $");
 
 #include <err.h>
 #include <search.h>
@@ -76,6 +76,7 @@ main(int argc, char *argv[])
 	char *errmsg = NULL;
 	char *str;
 	int ch, rc = 0;
+	int s;
 	callback_data cbdata;
 	cbdata.out = stdout;		// the default output stream
 	cbdata.count = 0;
@@ -92,7 +93,7 @@ main(int argc, char *argv[])
 	 * index element in sec_nums is set to the string representing that 
 	 * section number.
 	 */
-	while ((ch = getopt(argc, argv, "123456789Ccn:pS:")) != -1) {
+	while ((ch = getopt(argc, argv, "123456789Ccn:pS:s:")) != -1) {
 		switch (ch) {
 		case '1':
 		case '2':
@@ -121,6 +122,12 @@ main(int argc, char *argv[])
 		case 'S':
 			aflags.machine = optarg;
 			break;
+		case 's':
+			s = atoi(optarg);
+			if (s < 1 || s > 9)
+				errx(EXIT_FAILURE, "Invalid section");
+			aflags.sec_nums[s - 1] = 1;
+			break;
 		case '?':
 		default:
 			usage();
@@ -189,8 +196,8 @@ main(int argc, char *argv[])
 	
 	if (cbdata.count == 0) {
 		warnx("No relevant results obtained.\n"
-			  "Please make sure that you spelled all the terms correctly\n"
-			  "Or try using better keywords.");
+			  "Please make sure that you spelled all the terms correctly "
+			  "or try using better keywords.");
 	}
 	return 0;
 }

Reply via email to