- sec = sqlite3_column_text(s, 0);
- arch = sqlite3_column_text(s, 1);
- name = sqlite3_column_text(s, 2);
- 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);
+
+ /* 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);