- if (0 == argc || '\0' == **argv) {
- usage();
- goto out;
- } else
- q = *argv;
-
- if (0 == opts.types)
- opts.types = TYPE_NAME | TYPE_DESC;
-
- /*
- * Configure databases.
- * The keyword database is a btree that allows for duplicate
- * entries.
- * The index database is a recno.
- */
-
- memset(&info, 0, sizeof(BTREEINFO));
- info.flags = R_DUP;
-
- state.db = dbopen(dbf, O_RDONLY, 0, DB_BTREE, &info);
- if (NULL == state.db) {
- perror(dbf);
- goto out;
- }
-
- state.idx = dbopen(idxf, O_RDONLY, 0, DB_RECNO, NULL);
- if (NULL == state.idx) {
- perror(idxf);
- goto out;
- }
-
- /* Main search function. */
-
- state_search(&state, &opts, q);
-
- rc = EXIT_SUCCESS;
-out:
- if (state.db)
- (*state.db->close)(state.db);
- if (state.idx)
- (*state.idx->close)(state.idx);
-
- return(rc);
-}
-
-static void
-state_search(struct state *p, const struct opts *opts, char *q)
-{
- int leaf, root, len, ch, rflags, dflag;
- struct mchars *mc;
- char *buf;
- size_t bufsz;
- recno_t rec;
- uint32_t fl;
- DBT key, val;
- struct res *res;
- regex_t reg;
- regex_t *regp;
- char filebuf[10];
- struct rec record;
-
- root = leaf = -1;
- res = NULL;
- len = 0;
- buf = NULL;
- bufsz = 0;
- ch = 0;
- regp = NULL;
-
- /*
- * Configure how we scan through results to see if we match:
- * whether by regexp or exact matches.
- */
-
- switch (opts->match) {
- case (MATCH_REGEX):
- rflags = REG_EXTENDED | REG_NOSUB |
- (opts->insens ? REG_ICASE : 0);
-
- if (0 != regcomp(®, q, rflags)) {
- fprintf(stderr, "%s: Bad pattern\n", q);
- return;
- }
-
- regp = ®
- dflag = R_FIRST;
- break;
- case (MATCH_EXACT):
- key.data = q;
- key.size = strlen(q) + 1;
- dflag = R_CURSOR;
- break;
- default:
- dflag = R_FIRST;
- break;
- }
-
- if (NULL == (mc = mchars_alloc())) {
- perror(NULL);
- exit(EXIT_FAILURE);
- }
-
- /*
- * Iterate over the entire keyword database.
- * For each record, we must first translate the key into UTF-8.
- * Following that, make sure it's acceptable.
- * Lastly, add it to the available records.
- */
-
- while (0 == (ch = (*p->db->seq)(p->db, &key, &val, dflag))) {
- dflag = R_NEXT;
-
- /*
- * Keys must be sized as such: the keyword must be
- * non-empty (nil terminator plus one character) and the
- * value must be 8 (recno_t---uint32_t---index reference
- * and a uint32_t flag field).
- */
-
- if (key.size < 2 || 8 != val.size) {
- fprintf(stderr, "%s: Corrupt database\n", p->dbf);
- exit(EXIT_FAILURE);
- }
-
- buf_redup(mc, &buf, &bufsz, (char *)key.data);
-
- fl = *(uint32_t *)val.data;
-
- if ( ! (fl & opts->types))
- continue;
-
- switch (opts->match) {
- case (MATCH_REGEX):
- if (regexec(regp, buf, 0, NULL, 0))
- continue;
- break;
- case (MATCH_EXACT):
- if (opts->insens && strcasecmp(buf, q))
- goto send;
- if ( ! opts->insens && strcmp(buf, q))
- goto send;
- break;
- default:
- if (opts->insens && NULL == strcasestr(buf, q))
- continue;
- if ( ! opts->insens && NULL == strstr(buf, q))
- continue;
- break;
- }
-
- /*
- * Now look up the file itself in our index. The file's
- * indexed by its recno for fast lookups.
- */
-
- memcpy(&rec, val.data + 4, sizeof(recno_t));
-
- if ( ! state_getrecord(p, rec, &record))
- exit(EXIT_FAILURE);
-
- /* If we're in a different section, skip... */
-
- if (opts->cat && strcasecmp(opts->cat, record.cat))
- continue;
- if (opts->arch && strcasecmp(opts->arch, record.arch))
- continue;
-
- /*
- * Do a binary search to dedupe the results tree of the
- * same record: we don't print the same file.
- */
-
- for (leaf = root; leaf >= 0; )
- if (rec > res[leaf].rec && res[leaf].rhs >= 0)
- leaf = res[leaf].rhs;
- else if (rec < res[leaf].rec && res[leaf].lhs >= 0)
- leaf = res[leaf].lhs;
- else
- break;
-
- if (leaf >= 0 && res[leaf].rec == rec)
- continue;
-
- res = mandoc_realloc
- (res, (len + 1) * sizeof(struct res));
-
- /*
- * Now we have our filename, keywords, types, and all
- * other necessary information.
- * Process it and add it to our list of results.
- */
-
- filebuf[9] = '\0';
- snprintf(filebuf, 10, "%u", record.rec);
- assert('\0' == filebuf[9]);