-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)
- warnx("%s", 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);
- }
-}
-