- /* 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 i, len, ch, rflags, dflag;
- struct mchars *mc;
- char *buf;
- size_t bufsz;
- recno_t rec;
- uint32_t fl;
- DBT key, val;
- struct res res[MAXRESULTS];
- regex_t reg;
- regex_t *regp;
- char filebuf[10];
- struct rec record;
-
- 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 (len < MAXRESULTS) {
- if ((ch = (*p->db->seq)(p->db, &key, &val, dflag)))
- break;
-
- 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;
-
- /* FIXME: this needs to be changed. Ugh. Linear. */
-
- for (i = 0; i < len; i++)
- if (res[i].rec == record.rec)
- break;
-
- if (i < len)
- continue;
-
- /*
- * 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]);
-
- res[len].rec = record.rec;
- res[len].types = fl;
-
- buf_dup(mc, &res[len].keyword, buf);
- buf_dup(mc, &res[len].uri, filebuf);
- buf_dup(mc, &res[len].cat, record.cat);
- buf_dup(mc, &res[len].arch, record.arch);
- buf_dup(mc, &res[len].title, record.title);
- buf_dup(mc, &res[len].desc, record.desc);
- len++;
- }
-
-send:
- if (ch < 0) {
- perror(p->dbf);
- exit(EXIT_FAILURE);
- }
-
- /*
- * Sort our results.
- * We do this post-scan (instead of an in-line sort) because
- * it's more or less the same in terms of run-time. Assuming we
- * sort in-line with a tree versus post:
- *
- * In-place: n * O(lg n)
- * After: n + O(n lg n)
- *
- * Whatever. This also buys us simplicity.
- */
-
- switch (opts->sort) {
- case (SORT_CAT):
- qsort(res, len, sizeof(struct res), sort_cat);
- break;
- default:
- qsort(res, len, sizeof(struct res), sort_title);
- break;
- }
-
- state_output(res, len);
-
- for (len-- ; len >= 0; len--) {
- free(res[len].keyword);
- free(res[len].title);
- free(res[len].cat);
- free(res[len].arch);
- free(res[len].desc);
- free(res[len].uri);
- }
-
- free(buf);
- mchars_free(mc);
-
- if (regp)
- regfree(regp);
-}
-
-/*
- * Track allocated buffer size for buf_redup().
- */
-static inline void
-buf_alloc(char **buf, size_t *bufsz, size_t sz)
-{
-
- if (sz < *bufsz)
- return;
-
- *bufsz = sz + 1024;
- if (NULL == (*buf = realloc(*buf, *bufsz))) {
- perror(NULL);
- exit(EXIT_FAILURE);
- }
-}
-
-/*
- * Like buf_redup() but throwing away the buffer size.
- */
-static void
-buf_dup(struct mchars *mc, char **buf, const char *val)
-{
- size_t bufsz;
-
- bufsz = 0;
- *buf = NULL;
- buf_redup(mc, buf, &bufsz, val);
-}
-
-/*
- * Normalise strings from the index and database.
- * These strings are escaped as defined by mandoc_char(7) along with
- * other goop in mandoc.h (e.g., soft hyphens).
- */
-static void
-buf_redup(struct mchars *mc, char **buf,
- size_t *bufsz, const char *val)
-{
- size_t sz;
- const char *seq, *cpp;
- int len, pos;
- enum mandoc_esc esc;
- const char rsv[] = { '\\', ASCII_NBRSP, ASCII_HYPH, '\0' };
-
- /* Pre-allocate by the length of the input */
-
- buf_alloc(buf, bufsz, strlen(val) + 1);
-
- pos = 0;
-
- while ('\0' != *val) {
- /*
- * Halt on the first escape sequence.
- * This also halts on the end of string, in which case
- * we just copy, fallthrough, and exit the loop.
- */
- if ((sz = strcspn(val, rsv)) > 0) {
- memcpy(&(*buf)[pos], val, sz);
- pos += (int)sz;
- val += (int)sz;
- }
-
- if (ASCII_HYPH == *val) {
- (*buf)[pos++] = '-';
- val++;
- continue;
- } else if (ASCII_NBRSP == *val) {
- (*buf)[pos++] = ' ';
- val++;
- continue;
- } else if ('\\' != *val)
- break;
-
- /* Read past the slash. */
-
- val++;
-
- /*
- * Parse the escape sequence and see if it's a
- * predefined character or special character.
- */
-
- esc = mandoc_escape(&val, &seq, &len);
- if (ESCAPE_ERROR == esc)
- break;
-
- cpp = ESCAPE_SPECIAL == esc ?
- mchars_spec2str(mc, seq, len, &sz) : NULL;
-
- if (NULL == cpp)
- continue;
-
- /* Copy the rendered glyph into the stream. */
-
- buf_alloc(buf, bufsz, sz);
-
- memcpy(&(*buf)[pos], cpp, sz);
- pos += (int)sz;
- }
-
- (*buf)[pos] = '\0';
-}
-
-static void
-state_output(const struct res *res, int sz)
-{
- int i;
-
- for (i = 0; i < sz; i++)
- printf("%s(%s%s%s) - %s\n", res[i].title,
- res[i].cat,
- *res[i].arch ? "/" : "",
- *res[i].arch ? res[i].arch : "",
- res[i].desc);
-}
-
-static void
-usage(void)
-{
-
- fprintf(stderr, "usage: %s "
- "[-eIr] "
- "[-a arch] "
- "[-c cat] "
- "[-s sort] "
- "[-t type[,...]] "
- "key\n", progname);
-}
-
-static int
-state_getrecord(struct state *p, recno_t rec, struct rec *rp)
-{
- DBT key, val;
- size_t sz;
- int rc;
-
- key.data = &rec;
- key.size = sizeof(recno_t);
-
- rc = (*p->idx->get)(p->idx, &key, &val, 0);
- if (rc < 0) {
- perror(p->idxf);
- return(0);
- } else if (rc > 0)
- goto err;
-
- rp->file = (char *)val.data;
- if ((sz = strlen(rp->file) + 1) >= val.size)
- goto err;
-
- rp->cat = (char *)val.data + (int)sz;
- if ((sz += strlen(rp->cat) + 1) >= val.size)
- goto err;
-
- rp->title = (char *)val.data + (int)sz;
- if ((sz += strlen(rp->title) + 1) >= val.size)
- goto err;
-
- rp->arch = (char *)val.data + (int)sz;
- if ((sz += strlen(rp->arch) + 1) >= val.size)
- goto err;
-
- rp->desc = (char *)val.data + (int)sz;
- rp->rec = rec;
- return(1);
-err:
- fprintf(stderr, "%s: Corrupt index\n", p->idxf);
- return(0);
-}
-
-static int
-sort_title(const void *p1, const void *p2)
-{
-
- return(strcmp(((const struct res *)p1)->title,
- ((const struct res *)p2)->title));
-}
-
-static int
-sort_cat(const void *p1, const void *p2)
-{
- int rc;
-
- rc = strcmp(((const struct res *)p1)->cat,
- ((const struct res *)p2)->cat);
-
- return(0 == rc ? sort_title(p1, p2) : rc);