+ return(1);
+}
+
+void
+mansearch_free(struct manpage *res, size_t sz)
+{
+ size_t i;
+
+ for (i = 0; i < sz; i++) {
+ free(res[i].file);
+ free(res[i].names);
+ free(res[i].output);
+ }
+ free(res);
+}
+
+static int
+manpage_compare(const void *vp1, const void *vp2)
+{
+ const struct manpage *mp1, *mp2;
+ int diff;
+
+ mp1 = vp1;
+ mp2 = vp2;
+ return( (diff = mp2->bits - mp1->bits) ? diff :
+ (diff = mp1->sec - mp2->sec) ? diff :
+ strcasecmp(mp1->names, mp2->names));
+}
+
+static void
+buildnames(const struct mansearch *search, struct manpage *mpage,
+ sqlite3 *db, sqlite3_stmt *s,
+ uint64_t pageid, const char *path, int form)
+{
+ glob_t globinfo;
+ char *firstname, *newnames, *prevsec, *prevarch;
+ const char *oldnames, *sep1, *name, *sec, *sep2, *arch, *fsec;
+ size_t i;
+ int c, globres;
+
+ mpage->file = NULL;
+ mpage->names = NULL;
+ firstname = prevsec = prevarch = NULL;
+ i = 1;
+ SQL_BIND_INT64(db, s, i, pageid);
+ while (SQLITE_ROW == (c = sqlite3_step(s))) {
+
+ /* Decide whether we already have some names. */
+
+ if (NULL == mpage->names) {
+ oldnames = "";
+ sep1 = "";
+ } else {
+ oldnames = mpage->names;
+ sep1 = ", ";
+ }
+
+ /* Fetch the next name, rejecting sec/arch mismatches. */
+
+ sec = (const char *)sqlite3_column_text(s, 0);
+ if (search->sec != NULL && strcasecmp(sec, search->sec))
+ continue;
+ arch = (const char *)sqlite3_column_text(s, 1);
+ if (search->arch != NULL && *arch != '\0' &&
+ strcasecmp(arch, search->arch))
+ continue;
+ name = (const char *)sqlite3_column_text(s, 2);
+
+ /* Remember the first section found. */
+
+ if (9 < mpage->sec && '1' <= *sec && '9' >= *sec)
+ mpage->sec = (*sec - '1') + 1;
+
+ /* If the section changed, append the old one. */
+
+ if (NULL != prevsec &&
+ (strcmp(sec, prevsec) ||
+ strcmp(arch, prevarch))) {
+ sep2 = '\0' == *prevarch ? "" : "/";
+ mandoc_asprintf(&newnames, "%s(%s%s%s)",
+ oldnames, prevsec, sep2, prevarch);
+ free(mpage->names);
+ oldnames = mpage->names = newnames;
+ free(prevsec);
+ free(prevarch);
+ prevsec = prevarch = NULL;
+ }
+
+ /* Save the new section, to append it later. */
+
+ if (NULL == prevsec) {
+ prevsec = mandoc_strdup(sec);
+ prevarch = mandoc_strdup(arch);
+ }
+
+ /* Append the new name. */
+
+ mandoc_asprintf(&newnames, "%s%s%s",
+ oldnames, sep1, name);
+ free(mpage->names);
+ mpage->names = newnames;
+
+ /* Also save the first file name encountered. */
+
+ if (mpage->file != NULL)
+ continue;
+
+ if (form & FORM_SRC) {
+ sep1 = "man";
+ fsec = sec;
+ } else {
+ sep1 = "cat";
+ fsec = "0";
+ }
+ sep2 = *arch == '\0' ? "" : "/";
+ mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.%s",
+ path, sep1, sec, sep2, arch, name, fsec);
+ if (access(mpage->file, R_OK) != -1)
+ continue;
+
+ /* Handle unusual file name extensions. */
+
+ if (firstname == NULL)
+ firstname = mpage->file;
+ else
+ free(mpage->file);
+ mandoc_asprintf(&mpage->file, "%s/%s%s%s%s/%s.*",
+ path, sep1, sec, sep2, arch, name);
+ globres = glob(mpage->file, 0, NULL, &globinfo);
+ free(mpage->file);
+ mpage->file = globres ? NULL :
+ mandoc_strdup(*globinfo.gl_pathv);
+ globfree(&globinfo);
+ }
+ if (c != SQLITE_DONE)
+ fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ sqlite3_reset(s);
+
+ /* If none of the files is usable, use the first name. */
+
+ if (mpage->file == NULL)
+ mpage->file = firstname;
+ else if (mpage->file != firstname)
+ free(firstname);
+
+ /* Append one final section to the names. */
+
+ if (prevsec != NULL) {
+ sep2 = *prevarch == '\0' ? "" : "/";
+ mandoc_asprintf(&newnames, "%s(%s%s%s)",
+ mpage->names, prevsec, sep2, prevarch);
+ free(mpage->names);
+ mpage->names = newnames;
+ free(prevsec);
+ free(prevarch);
+ }
+}
+
+static char *
+buildoutput(sqlite3 *db, sqlite3_stmt *s, uint64_t pageid, uint64_t outbit)
+{
+ char *output, *newoutput;
+ const char *oldoutput, *sep1, *data;
+ size_t i;
+ int c;
+
+ output = NULL;
+ i = 1;
+ SQL_BIND_INT64(db, s, i, pageid);
+ SQL_BIND_INT64(db, s, i, outbit);
+ while (SQLITE_ROW == (c = sqlite3_step(s))) {
+ if (NULL == output) {
+ oldoutput = "";
+ sep1 = "";
+ } else {
+ oldoutput = output;
+ sep1 = " # ";
+ }
+ data = (const char *)sqlite3_column_text(s, 1);
+ mandoc_asprintf(&newoutput, "%s%s%s",
+ oldoutput, sep1, data);
+ free(output);
+ output = newoutput;
+ }
+ if (SQLITE_DONE != c)
+ fprintf(stderr, "%s\n", sqlite3_errmsg(db));
+ sqlite3_reset(s);
+ return(output);
+}
+
+/*
+ * Implement substring match as an application-defined SQL function.
+ * Using the SQL LIKE or GLOB operators instead would be a bad idea
+ * because that would require escaping metacharacters in the string
+ * being searched for.
+ */
+static void
+sql_match(sqlite3_context *context, int argc, sqlite3_value **argv)
+{
+
+ assert(2 == argc);
+ sqlite3_result_int(context, NULL != strcasestr(
+ (const char *)sqlite3_value_text(argv[1]),
+ (const char *)sqlite3_value_text(argv[0])));
+}
+
+/*
+ * Implement regular expression match
+ * as an application-defined SQL function.
+ */
+static void
+sql_regexp(sqlite3_context *context, int argc, sqlite3_value **argv)
+{
+
+ assert(2 == argc);
+ sqlite3_result_int(context, !regexec(
+ (regex_t *)sqlite3_value_blob(argv[0]),
+ (const char *)sqlite3_value_text(argv[1]),
+ 0, NULL, 0));
+}
+
+static void
+sql_append(char **sql, size_t *sz, const char *newstr, int count)
+{
+ size_t newsz;
+
+ newsz = 1 < count ? (size_t)count : strlen(newstr);
+ *sql = mandoc_realloc(*sql, *sz + newsz + 1);
+ if (1 < count)
+ memset(*sql + *sz, *newstr, (size_t)count);
+ else
+ memcpy(*sql + *sz, newstr, newsz);
+ *sz += newsz;
+ (*sql)[*sz] = '\0';