Skip to content

Commit

Permalink
Add whatis(1) replacement. Change makemandb section to 8.
Browse files Browse the repository at this point in the history
Fix various bugs in it. Refactor error handling.
  • Loading branch information
jsonn authored and abhinav-upadhyay committed Feb 5, 2012
1 parent 1753a18 commit 67e6778
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 63 deletions.
10 changes: 7 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ MDIST= ${NETBSDSRCDIR}/external/bsd/mdocml/dist
MDOCDIR=${NETBSDSRCDIR}/external/bsd/mdocml

PROGS= makemandb apropos
SRCS.makemandb= makemandb.c sqlite3.c apropos-utils.c apropos-utils.h
SRCS.apropos= apropos.c sqlite3.c apropos-utils.c apropos-utils.h
MAN= makemandb.1 apropos.1 apropos-utils.3 init_db.3 close_db.3 run_query.3 run_query_html.3 run_query_pager.3
SRCS.makemandb= makemandb.c sqlite3.c apropos-utils.c
SRCS.apropos= apropos.c sqlite3.c apropos-utils.c
SRCS.whatis= whatis.c apropos-utils.c sqlite3.c
MAN= apropos-utils.3 init_db.3 close_db.3 run_query.3 run_query_html.3 run_query_pager.3
MAN.makemandb= makemandb.8
MAN.apropos= apropos.1
MAN.whatis= whatis.1

.PATH: ${MDIST}
CPPFLAGS+=-I${MDIST}
Expand Down
45 changes: 24 additions & 21 deletions apropos-utils.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* $NetBSD */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
Expand Down Expand Up @@ -29,6 +30,9 @@
* SUCH DAMAGE.
*/

#include <sys/cdefs.h>
__RCSID("$NetBSD$");

#include <sys/stat.h>

#include <assert.h>
Expand All @@ -39,6 +43,7 @@
#include <stdlib.h>
#include <string.h>
#include <util.h>
#include <zlib.h>

#include "apropos-utils.h"
#include "mandoc.h"
Expand Down Expand Up @@ -146,16 +151,17 @@ create_db(sqlite3 *db)

/*------------------------ Create the tables------------------------------*/

#if NOTYET
sqlite3_exec(db, "PRAGMA journal_mode = WAL", NULL, NULL, NULL);
#else
sqlite3_exec(db, "PRAGMA journal_mode = DELETE", NULL, NULL, NULL);
#endif

schemasql = sqlite3_mprintf("PRAGMA user_version = %d",
APROPOS_SCHEMA_VERSION);
sqlite3_exec(db, schemasql, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
sqlite3_close(db);
sqlite3_shutdown();
return -1;
}
if (errmsg != NULL)
goto out;
sqlite3_free(schemasql);

sqlstr = "CREATE VIRTUAL TABLE mandb USING fts4(section, name, "
Expand All @@ -169,27 +175,24 @@ create_db(sqlite3 *db)
"machine); "; //mandb_links

sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
sqlite3_close(db);
sqlite3_shutdown();
return -1;
}
if (errmsg != NULL)
goto out;

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)";
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("%s", errmsg);
free(errmsg);
sqlite3_close(db);
sqlite3_shutdown();
return -1;
}
if (errmsg != NULL)
goto out;
return 0;

out:
warnx("%s", errmsg);
free(errmsg);
sqlite3_close(db);
sqlite3_shutdown();
return -1;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion apropos-utils.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* $NetBSD */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
Expand Down Expand Up @@ -32,7 +33,6 @@
#ifndef APROPOS_UTILS_H
#define APROPOS_UTILS_H

#include <zlib.h>
#include "sqlite3.h"

#define DBPATH "/var/db/man.db"
Expand Down
7 changes: 5 additions & 2 deletions apropos.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* $NetBSD */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
Expand Down Expand Up @@ -29,6 +30,9 @@
* SUCH DAMAGE.
*/

#include <sys/cdefs.h>
__RCSID("$NetBSD$");

#include <err.h>
#include <search.h>
#include <stdio.h>
Expand Down Expand Up @@ -107,8 +111,7 @@ main(int argc, char *argv[])
case '?':
default:
usage();
break;
}
}
}

argc -= optind;
Expand Down
11 changes: 8 additions & 3 deletions makemandb.1 → makemandb.8
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.Dd January 31, 2012
.Dt MAKEMANDB 1
.Dd February 4, 2012
.Dt MAKEMANDB 8
.Os
.Sh NAME
.Nm makemandb
.Nd parse the manual pages and build a search index over them
.Sh SYNOPSIS
.Nm
.Op Fl C Ar path
.Op Fl flo
.Op Fl floqv
.Sh DESCRIPTION
The
.Nm
Expand Down Expand Up @@ -71,6 +71,11 @@ and also to substantially save disk space.
Use this option to optimize the index for speed and also
to significantly reduce disk space usage.
This is a somewhat expensive operation.
.It Fl q
Print only error messages and no status updates.
.It Fl v
Enable verbose output.
This prints the name of every file being parsed.
.El
.Pp
As the database file is stored under
Expand Down
88 changes: 55 additions & 33 deletions makemandb.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* $NetBSD */
/*
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* Copyright (c) 2011 Kristaps Dzonsons <[email protected]>
Expand All @@ -15,6 +16,9 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <sys/cdefs.h>
__RCSID("$NetBSD$");

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

Expand Down Expand Up @@ -52,6 +56,7 @@ typedef struct makemandb_flags {
int optimize;
int limit; // limit the indexing to only NAME section
int recreate; // Database was created from scratch
int verbosity; // 0: quiet, 1: default, 2: verbose
} makemandb_flags;

typedef struct mandb_rec {
Expand Down Expand Up @@ -115,7 +120,7 @@ static void update_db(sqlite3 *, struct mparse *, mandb_rec *);
__dead static void usage(void);
static void optimize(sqlite3 *);
static char *parse_escape(const char *);
static makemandb_flags mflags;
static makemandb_flags mflags = { .verbosity = 1 };

typedef void (*pman_nf)(const struct man_node *n, mandb_rec *);
typedef void (*pmdoc_nf)(const struct mdoc_node *n, mandb_rec *);
Expand Down Expand Up @@ -295,7 +300,7 @@ main(int argc, char *argv[])
size_t linesize;
struct mandb_rec rec;

while ((ch = getopt(argc, argv, "C:flo")) != -1) {
while ((ch = getopt(argc, argv, "C:floqv")) != -1) {
switch (ch) {
case 'C':
manconf = optarg;
Expand All @@ -310,10 +315,14 @@ main(int argc, char *argv[])
case 'o':
mflags.optimize = 1;
break;
case '?':
case 'q':
mflags.verbosity = 0;
break;
case 'v':
mflags.verbosity = 2;
break;
default:
usage();
break;
}
}

Expand Down Expand Up @@ -381,7 +390,8 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}

printf("Building temporary file cache\n");
if (mflags.verbosity)
printf("Building temporary file cache\n");
line = NULL;
linesize = 0;
while ((len = getline(&line, &linesize, file)) != -1) {
Expand Down Expand Up @@ -563,7 +573,8 @@ update_existing_entry(sqlite3 *db, const char *file, const char *hash,
if (rc == SQLITE_DONE) {
/* Check if an update has been performed. */
if (update_count != sqlite3_total_changes(db)) {
printf("Updated %s\n", file);
if (mflags.verbosity)
printf("Updated %s\n", file);
(*new_count)++;
} else {
/* Otherwise it was a hardlink. */
Expand Down Expand Up @@ -647,7 +658,8 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)
* This means is either a new file or an updated file.
* We should go ahead with parsing.
*/
printf("Parsing: %s\n", file);
if (mflags.verbosity > 1)
printf("Parsing: %s\n", file);
rec->md5_hash = buf;
rec->file_path = estrdup(file);
// file_path is freed by insert_into_db itself.
Expand All @@ -663,29 +675,34 @@ update_db(sqlite3 *db, struct mparse *mp, mandb_rec *rec)

sqlite3_finalize(stmt);

printf("Total Number of new or updated pages enountered = %d\n"
"Total number of pages that were successfully"
" indexed/updated = %d\n"
"Total number of (hard or symbolic) links found = %d\n"
"Total number of pages that could not be indexed"
" due to errors = %d\n",
total_count, new_count, link_count, err_count);

if (!mflags.recreate) {
if (mflags.verbosity) {
printf("Total Number of new or updated pages enountered = %d\n"
"Total number of pages that were successfully"
" indexed/updated = %d\n"
"Total number of (hard or symbolic) links found = %d\n"
"Total number of pages that could not be indexed"
" due to errors = %d\n",
total_count, new_count, link_count, err_count);
}

if (mflags.recreate == 0)
return;

if (mflags.verbosity)
printf("Deleting stale index entries\n");
sqlstr = "DELETE FROM mandb_meta WHERE file NOT IN"
" (SELECT file FROM metadb.file_cache);"
"DROP TABLE metadb.file_cache;"
"DELETE FROM mandb WHERE rowid NOT IN"
" (SELECT id FROM mandb_meta);";

sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("Removing old entries failed: %s", errmsg);
warnx("Please rebuild database from scratch with -f.");
free(errmsg);
return;
}
sqlstr = "DELETE FROM mandb_meta WHERE file NOT IN"
" (SELECT file FROM metadb.file_cache);"
"DROP TABLE metadb.file_cache;"
"DELETE FROM mandb WHERE rowid NOT IN"
" (SELECT id FROM mandb_meta);";

sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
if (errmsg != NULL) {
warnx("Removing old entries failed: %s", errmsg);
warnx("Please rebuild database from scratch with -f.");
free(errmsg);
return;
}
}

Expand Down Expand Up @@ -1210,9 +1227,9 @@ pman_sh(const struct man_node *n, mandb_rec *rec)

/* The RETURN VALUE section might be specified in multiple ways */
if (strcmp(head->string, "RETURN") == 0 &&
head->next->type == MAN_TEXT &&
head->next != NULL && head->next->type == MAN_TEXT &&
(strcmp(head->next->string, "VALUE") == 0 ||
strcmp(head->next->string, "VALUES") == 0)) {
strcmp(head->next->string, "VALUES") == 0)) {
man_parse_section(MANSEC_RETURN_VALUES, n, rec);
return;
}
Expand All @@ -1221,7 +1238,8 @@ pman_sh(const struct man_node *n, mandb_rec *rec)
* EXIT STATUS section can also be specified all on one line or on two
* separate lines.
*/
if (strcmp(head->string, "EXIT") == 0 && head->next->type == MAN_TEXT &&
if (strcmp(head->string, "EXIT") == 0 &&
head->next != NULL && head->next->type == MAN_TEXT &&
strcmp(head->next->string, "STATUS") == 0) {
man_parse_section(MANSEC_EXIT_STATUS, n, rec);
return;
Expand Down Expand Up @@ -1707,7 +1725,8 @@ optimize(sqlite3 *db)
const char *sqlstr;
char *errmsg = NULL;

printf("Optimizing the database index\n");
if (mflags.verbosity)
printf("Optimizing the database index\n");
sqlstr = "INSERT INTO mandb(mandb) VALUES (\'optimize\');"
"VACUUM";
sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg);
Expand Down Expand Up @@ -1849,10 +1868,13 @@ parse_escape(const char *str)
++backslash;
mandoc_escape(&backslash, NULL, NULL);
last_backslash = backslash;
if (backslash == NULL)
break;
backslash = strchr(last_backslash, '\\');
}
} while (backslash != NULL);
strcpy(iter, last_backslash);
if (last_backslash != NULL)
strcpy(iter, last_backslash);
return result;
}

Expand Down
Loading

0 comments on commit 67e6778

Please sign in to comment.