aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/mansearch.c
diff options
context:
space:
mode:
authorIngo Schwarze <schwarze@openbsd.org>2013-12-31 02:42:29 +0000
committerIngo Schwarze <schwarze@openbsd.org>2013-12-31 02:42:29 +0000
commit05e81e9e4124ecfe3c1b096c9edebf065f703d8d (patch)
tree7e316ada7e5f346c68a4ef7c22bff56e8f633db2 /mansearch.c
parente7167875512bf0e3a78731060319b1d9e15a4ad3 (diff)
downloadmandoc-05e81e9e4124ecfe3c1b096c9edebf065f703d8d.tar.gz
mandoc-05e81e9e4124ecfe3c1b096c9edebf065f703d8d.tar.zst
mandoc-05e81e9e4124ecfe3c1b096c9edebf065f703d8d.zip
Split buildnames() out of mansearch(); the latter function is getting
too long and unwieldy, but will grow more code soon. No functional change.
Diffstat (limited to 'mansearch.c')
-rw-r--r--mansearch.c72
1 files changed, 41 insertions, 31 deletions
diff --git a/mansearch.c b/mansearch.c
index 829152e4..57c187b6 100644
--- a/mansearch.c
+++ b/mansearch.c
@@ -1,4 +1,4 @@
-/* $Id: mansearch.c,v 1.10 2013/12/27 18:51:25 schwarze Exp $ */
+/* $Id: mansearch.c,v 1.11 2013/12/31 02:42:29 schwarze Exp $ */
/*
* Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
@@ -121,6 +121,7 @@ static const struct type types[] = {
{ 0ULL, NULL }
};
+static char *buildnames(sqlite3 *, sqlite3_stmt *, uint64_t);
static void *hash_alloc(size_t, void *);
static void hash_free(void *, size_t, void *);
static void *hash_halloc(size_t, void *);
@@ -144,8 +145,7 @@ mansearch(const struct mansearch *search,
int fd, rc, c;
int64_t id;
char buf[PATH_MAX];
- char *sql, *newnames;
- const char *oldnames, *sep1, *name, *sec, *sep2, *arch;
+ char *sql;
struct manpage *mpage;
struct expr *e, *ep;
sqlite3 *db;
@@ -305,36 +305,9 @@ mansearch(const struct mansearch *search,
perror(0);
exit((int)MANDOCLEVEL_SYSERR);
}
- mpage->names = NULL;
mpage->desc = mp->desc;
mpage->form = mp->form;
-
- j = 1;
- SQL_BIND_INT64(db, s, j, mp->id);
- while (SQLITE_ROW == (c = sqlite3_step(s))) {
- if (NULL == mpage->names) {
- oldnames = "";
- sep1 = "";
- } else {
- oldnames = mpage->names;
- sep1 = ", ";
- }
- sec = sqlite3_column_text(s, 1);
- arch = sqlite3_column_text(s, 2);
- name = sqlite3_column_text(s, 3);
- sep2 = '\0' == *arch ? "" : "/";
- if (-1 == asprintf(&newnames,
- "%s%s%s(%s%s%s)", oldnames, sep1,
- name, sec, sep2, arch)) {
- perror(0);
- exit((int)MANDOCLEVEL_SYSERR);
- }
- free(mpage->names);
- mpage->names = newnames;
- }
- if (SQLITE_DONE != c)
- fprintf(stderr, "%s\n", sqlite3_errmsg(db));
- sqlite3_reset(s);
+ mpage->names = buildnames(db, s, mp->id);
free(mp->file);
free(mp);
@@ -355,6 +328,43 @@ out:
return(rc);
}
+static char *
+buildnames(sqlite3 *db, sqlite3_stmt *s, uint64_t id)
+{
+ char *names, *newnames;
+ const char *oldnames, *sep1, *name, *sec, *sep2, *arch;
+ size_t i;
+ int c;
+
+ names = NULL;
+ i = 1;
+ SQL_BIND_INT64(db, s, i, id);
+ while (SQLITE_ROW == (c = sqlite3_step(s))) {
+ if (NULL == names) {
+ oldnames = "";
+ sep1 = "";
+ } else {
+ oldnames = names;
+ sep1 = ", ";
+ }
+ sec = sqlite3_column_text(s, 1);
+ arch = sqlite3_column_text(s, 2);
+ name = sqlite3_column_text(s, 3);
+ sep2 = '\0' == *arch ? "" : "/";
+ if (-1 == asprintf(&newnames, "%s%s%s(%s%s%s)",
+ oldnames, sep1, name, sec, sep2, arch)) {
+ perror(0);
+ exit((int)MANDOCLEVEL_SYSERR);
+ }
+ free(names);
+ names = newnames;
+ }
+ if (SQLITE_DONE != c)
+ fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ sqlite3_reset(s);
+ return(names);
+}
+
/*
* Implement substring match as an application-defined SQL function.
* Using the SQL LIKE or GLOB operators instead would be a bad idea