Skip to content

Commit

Permalink
Merge branch 'master' into merge_master_from_cgi
Browse files Browse the repository at this point in the history
Conflicts:
	Makefile
	apropos-utils.c
	apropos.c
	makemandb.c

Coflicts resolved.
  • Loading branch information
abhinav-upadhyay committed Dec 25, 2012
2 parents 342675d + 18e10d3 commit ed462c6
Show file tree
Hide file tree
Showing 11 changed files with 271 additions and 129 deletions.
1 change: 1 addition & 0 deletions DBSCHEMA
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,4 @@ There are three tables in the database at present:
3. section The section number
4. machine The machine architecture (if any) for which
the page is relevant
5. md5_hash MD5 Hash of the target man page.
13 changes: 8 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# $NetBSD: Makefile,v 1.2 2012/02/16 20:58:55 joerg Exp $
# $NetBSD: Makefile,v 1.3 2012/10/06 15:33:59 wiz Exp $

.include <bsd.own.mk>

MDIST= ${NETBSDSRCDIR}/external/bsd/mdocml/dist
MDOCDIR=${NETBSDSRCDIR}/external/bsd/mdocml
MANCONFDIR=${NETBSDSRCDIR}/usr.bin/man

PROGS= makemandb apropos whatis apropos.cgi suggest.cgi
SRCS.makemandb= makemandb.c apropos-utils.c
SRCS.apropos= apropos.c apropos-utils.c
SRCS.whatis= whatis.c apropos-utils.c
SRCS.makemandb= makemandb.c apropos-utils.c manconf.c
SRCS.apropos= apropos.c apropos-utils.c manconf.c
SRCS.whatis= whatis.c apropos-utils.c manconf.c
SRCS.apropos.cgi= apropos_cgi.c apropos-utils.c cgi-utils.c
SRCS.suggest.cgi= suggest_cgi.c cgi-utils.c apropos-utils.c
MAN.makemandb= makemandb.8
Expand All @@ -19,7 +20,9 @@ BINDIR.apropos= /usr/bin
BINDIR.makemandb= /usr/sbin
BINDIR.whatis= /usr/bin

CPPFLAGS+=-I${MDIST} -I${.OBJDIR}
.PATH: ${MANCONFDIR}

CPPFLAGS+=-I${MDIST} -I${MANCONFDIR} -I${.OBJDIR}

MDOCMLOBJDIR!= cd ${MDOCDIR}/lib/libmandoc && ${PRINTOBJDIR}
MDOCMLLIB= ${MDOCMLOBJDIR}/libmandoc.a
Expand Down
67 changes: 54 additions & 13 deletions apropos-utils.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: apropos-utils.c,v 1.2 2012/02/07 19:17:16 joerg Exp $ */
/* $NetBSD: apropos-utils.c,v 1.7 2012/10/06 15:33:59 wiz Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
Expand Down Expand Up @@ -31,8 +31,9 @@
*/

#include <sys/cdefs.h>
__RCSID("$NetBSD: apropos-utils.c,v 1.2 2012/02/07 19:17:16 joerg Exp $");
__RCSID("$NetBSD: apropos-utils.c,v 1.7 2012/10/06 15:33:59 wiz Exp $");

#include <sys/queue.h>
#include <sys/stat.h>

#include <assert.h>
Expand All @@ -46,6 +47,7 @@ __RCSID("$NetBSD: apropos-utils.c,v 1.2 2012/02/07 19:17:16 joerg Exp $");
#include <zlib.h>

#include "apropos-utils.h"
#include "manconf.h"
#include "mandoc.h"
#include "sqlite3.h"

Expand Down Expand Up @@ -223,7 +225,7 @@ create_db(sqlite3 *db)
"file UNIQUE, md5_hash UNIQUE, id INTEGER PRIMARY KEY); "
//mandb_meta
"CREATE TABLE IF NOT EXISTS mandb_links(link, target, section, "
"machine); " //mandb_links
"machine, md5_hash); " //mandb_links
"CREATE TABLE mandb_dict(word UNIQUE, frequency);"; //mandb_dict;


Expand All @@ -234,7 +236,9 @@ create_db(sqlite3 *db)
sqlstr = "CREATE INDEX IF NOT EXISTS index_mandb_links ON mandb_links "
"(link); "
"CREATE INDEX IF NOT EXISTS index_mandb_meta_dev ON mandb_meta "
"(device, inode)";
"(device, inode); "
"CREATE INDEX IF NOT EXISTS index_mandb_links_md5 ON mandb_links "
"(md5_hash);";
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL)
goto out;
Expand Down Expand Up @@ -311,6 +315,28 @@ unzip(sqlite3_context *pctx, int nval, sqlite3_value **apval)
sqlite3_result_text(pctx, (const char *) outbuf, stream.total_out, free);
}

/*
* get_dbpath --
* Read the path of the database from man.conf and return.
*/
char *
get_dbpath(const char *manconf)
{
TAG *tp;
char *dbpath;

config(manconf);
tp = gettag("_mandb", 1);
if (!tp)
return NULL;

if (TAILQ_EMPTY(&tp->entrylist))
return NULL;

dbpath = TAILQ_LAST(&tp->entrylist, tqh)->s;
return dbpath;
}

/* init_db --
* Prepare the database. Register the compress/uncompress functions and the
* stopword tokenizer.
Expand All @@ -327,16 +353,19 @@ unzip(sqlite3_context *pctx, int nval, sqlite3_value **apval)
* In normal cases the function should return a handle to the db.
*/
sqlite3 *
init_db(int db_flag)
init_db(int db_flag, const char *manconf)
{
sqlite3 *db = NULL;
sqlite3_stmt *stmt;
struct stat sb;
int rc;
int create_db_flag = 0;

char *dbpath = get_dbpath(manconf);
if (dbpath == NULL)
errx(EXIT_FAILURE, "_mandb entry not found in man.conf");
/* Check if the database exists or not */
if (!(stat(DBPATH, &sb) == 0 && S_ISREG(sb.st_mode))) {
if (!(stat(dbpath, &sb) == 0 && S_ISREG(sb.st_mode))) {
/* Database does not exist, check if DB_CREATE was specified, and set
* flag to create the database schema
*/
Expand All @@ -350,7 +379,7 @@ init_db(int db_flag)

/* Now initialize the database connection */
sqlite3_initialize();
rc = sqlite3_open_v2(DBPATH, &db, db_flag, NULL);
rc = sqlite3_open_v2(dbpath, &db, db_flag, NULL);

if (rc != SQLITE_OK) {
warnx("%s", sqlite3_errmsg(db));
Expand All @@ -365,12 +394,14 @@ init_db(int db_flag)

rc = sqlite3_prepare_v2(db, "PRAGMA user_version", -1, &stmt, NULL);
if (rc != SQLITE_OK) {
warnx("Unable to query schema version");
warnx("Unable to query schema version: %s",
sqlite3_errmsg(db));
goto error;
}
if (sqlite3_step(stmt) != SQLITE_ROW) {
sqlite3_finalize(stmt);
warnx("Unable to query schema version");
warnx("Unable to query schema version: %s",
sqlite3_errmsg(db));
goto error;
}
if (sqlite3_column_int(stmt, 0) != APROPOS_SCHEMA_VERSION) {
Expand All @@ -386,17 +417,20 @@ init_db(int db_flag)
/* Register the zip and unzip functions for FTS compression */
rc = sqlite3_create_function(db, "zip", 1, SQLITE_ANY, NULL, zip, NULL, NULL);
if (rc != SQLITE_OK) {
warnx("Unable to register function: compress");
warnx("Unable to register function: compress: %s",
sqlite3_errmsg(db));
goto error;
}

rc = sqlite3_create_function(db, "unzip", 1, SQLITE_ANY, NULL,
unzip, NULL, NULL);
if (rc != SQLITE_OK) {
warnx("Unable to register function: uncompress");
warnx("Unable to register function: uncompress: %s",
sqlite3_errmsg(db));
goto error;
}
return db;

error:
sqlite3_close(db);
sqlite3_shutdown();
Expand Down Expand Up @@ -794,6 +828,8 @@ run_query(sqlite3 *db, const char *snippet_args[3], query_args *args)
const char *name_desc;
const char *machine;
const char *snippet;
const char *name_temp;
char *slash_ptr;
char *m = NULL;
int rc;
inverse_document_frequency idf = {0, 0};
Expand All @@ -806,9 +842,11 @@ run_query(sqlite3 *db, const char *snippet_args[3], query_args *args)
rc = sqlite3_create_function(db, "rank_func", 1, SQLITE_ANY, (void *)&idf,
rank_func, NULL, NULL);
if (rc != SQLITE_OK) {
warnx("Unable to register the ranking function: %s",
sqlite3_errmsg(db));
sqlite3_close(db);
sqlite3_shutdown();
errx(EXIT_FAILURE, "Unable to register the ranking function");
exit(EXIT_FAILURE);
}

/* We want to build a query of the form: "select x,y,z from mandb where
Expand Down Expand Up @@ -892,13 +930,16 @@ run_query(sqlite3 *db, const char *snippet_args[3], query_args *args)

while (sqlite3_step(stmt) == SQLITE_ROW) {
section = (const char *) sqlite3_column_text(stmt, 0);
name_temp = (const char *) sqlite3_column_text(stmt, 1);
name_desc = (const char *) sqlite3_column_text(stmt, 2);
machine = (const char *) sqlite3_column_text(stmt, 3);
snippet = (const char *) sqlite3_column_text(stmt, 4);
if ((slash_ptr = strrchr(name_temp, '/')) != NULL)
name_temp = slash_ptr + 1;
if (machine && machine[0]) {
m = estrdup(machine);
easprintf(&name, "%s/%s", lower(m),
sqlite3_column_text(stmt, 1));
name_temp);
free(m);
} else {
name = estrdup((const char *) sqlite3_column_text(stmt, 1));
Expand Down
9 changes: 5 additions & 4 deletions apropos-utils.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: apropos-utils.h,v 1.2 2012/02/07 19:17:16 joerg Exp $ */
/* $NetBSD: apropos-utils.h,v 1.4 2012/10/06 15:33:59 wiz Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
Expand Down Expand Up @@ -35,15 +35,15 @@

#include "sqlite3.h"

#define DBPATH "/var/db/man.db"
#define MANCONF "/etc/man.conf"
#define SECMAX 9

/* Flags for opening the database */
#define MANDB_READONLY SQLITE_OPEN_READONLY
#define MANDB_WRITE SQLITE_OPEN_READWRITE
#define MANDB_CREATE SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE

#define APROPOS_SCHEMA_VERSION 20120130
#define APROPOS_SCHEMA_VERSION 20120507

/*
* Used to identify the section of a man(7) page.
Expand Down Expand Up @@ -84,8 +84,9 @@ typedef struct query_args {
char *lower(char *);
void concat(char **, const char *);
void concat2(char **, const char *, size_t);
sqlite3 *init_db(int);
sqlite3 *init_db(int, const char *);
void close_db(sqlite3 *);
char *get_dbpath(const char *);
int run_query(sqlite3 *, const char *[3], query_args *);
int run_query_html(sqlite3 *, query_args *);
int run_query_pager(sqlite3 *, query_args *);
Expand Down
20 changes: 15 additions & 5 deletions apropos.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" $NetBSD: apropos.1,v 1.3 2012/02/15 23:53:13 joerg Exp $
.\" $NetBSD: apropos.1,v 1.6 2012/10/06 15:33:59 wiz Exp $
.\"
.\" Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
.\" All rights reserved.
Expand Down Expand Up @@ -29,7 +29,7 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd February 15, 2012
.Dd October 5, 2012
.Dt APROPOS 1
.Os
.Sh NAME
Expand Down Expand Up @@ -106,12 +106,22 @@ using it is equivalent to using the
options directly.
.El
.Sh FILES
.Bl -hang -width -compact
.It Pa /var/db/man.db
The Sqlite FTS database which contains an index of the manual pages.
.Bl -hang -width /etc/man.conf -compact
.It Pa /etc/man.conf
The location of the Sqlite FTS database can be configured using the
.Cd _mandb
tag.
.El
.Sh SEE ALSO
.Xr man 1 ,
.Xr whatis 1 ,
.Xr makemandb 8
.Sh HISTORY
The
.Nm
command appeared in 3.0BSD.
It was rewritten in
.Nx 6.0
to support full text search using Sqlite.
.Sh AUTHORS
.An Abhinav Upadhyay
10 changes: 5 additions & 5 deletions apropos.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: apropos.c,v 1.5 2012/02/15 23:53:13 joerg Exp $ */
/* $NetBSD: apropos.c,v 1.8 2012/10/06 15:33:59 wiz Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
Expand Down Expand Up @@ -31,7 +31,7 @@
*/

#include <sys/cdefs.h>
__RCSID("$NetBSD: apropos.c,v 1.5 2012/02/15 23:53:13 joerg Exp $");
__RCSID("$NetBSD: apropos.c,v 1.8 2012/10/06 15:33:59 wiz Exp $");

#include <err.h>
#include <search.h>
Expand Down Expand Up @@ -153,7 +153,7 @@ main(int argc, char *argv[])
errx(EXIT_FAILURE, "Try using more relevant keywords");

build_boolean_query(query);
if ((db = init_db(MANDB_WRITE)) == NULL)
if ((db = init_db(MANDB_READONLY, MANCONF)) == NULL)
exit(EXIT_FAILURE);

/* If user wants to page the output, then set some settings */
Expand Down Expand Up @@ -227,7 +227,7 @@ query_callback(void *data, const char *section, const char *name,
callback_data *cbdata = (callback_data *) data;
FILE *out = cbdata->out;
cbdata->count++;
fprintf(out, "%s(%s)\t%s\n", name, section, name_desc);
fprintf(out, "%s (%s)\t%s\n", name, section, name_desc);

if (cbdata->aflags->no_context == 0)
fprintf(out, "%s\n\n", snippet);
Expand All @@ -243,7 +243,7 @@ static void
usage(void)
{
fprintf(stderr,
"Usage: %s [-n Number of records] [-p] [-123456789] [-S machine] query\n",
"Usage: %s [-n Number of records] [-123456789Ccp] [-S machine] query\n",
getprogname());
exit(1);
}
Loading

0 comments on commit ed462c6

Please sign in to comment.