version 1.6, 2011/10/09 10:37:52 |
version 1.7, 2011/10/09 10:46:38 |
Line 152 static int sort_title(const void *, const void *); |
|
Line 152 static int sort_title(const void *, const void *); |
|
static int state_getrecord(struct state *, |
static int state_getrecord(struct state *, |
recno_t, struct rec *); |
recno_t, struct rec *); |
static void state_output(const struct res *, int); |
static void state_output(const struct res *, int); |
static void state_search(struct state *, |
static int state_search(struct state *, |
const struct opts *, char *); |
const struct opts *, char *); |
static void usage(void); |
static void usage(void); |
|
|
Line 275 main(int argc, char *argv[]) |
|
Line 275 main(int argc, char *argv[]) |
|
|
|
/* Main search function. */ |
/* Main search function. */ |
|
|
state_search(&state, &opts, q); |
rc = state_search(&state, &opts, q) ? |
|
EXIT_SUCCESS : EXIT_FAILURE; |
rc = EXIT_SUCCESS; |
|
out: |
out: |
if (state.db) |
if (state.db) |
(*state.db->close)(state.db); |
(*state.db->close)(state.db); |
|
|
return(rc); |
return(rc); |
} |
} |
|
|
static void |
static int |
state_search(struct state *p, const struct opts *opts, char *q) |
state_search(struct state *p, const struct opts *opts, char *q) |
{ |
{ |
int leaf, root, len, ch, dflag; |
int leaf, root, len, ch, dflag, rc; |
struct mchars *mc; |
struct mchars *mc; |
char *buf; |
char *buf; |
size_t bufsz; |
size_t bufsz; |
Line 303 state_search(struct state *p, const struct opts *opts, |
|
Line 302 state_search(struct state *p, const struct opts *opts, |
|
char filebuf[10]; |
char filebuf[10]; |
struct rec record; |
struct rec record; |
|
|
|
rc = 0; |
root = leaf = -1; |
root = leaf = -1; |
res = NULL; |
res = NULL; |
len = 0; |
len = 0; |
Line 322 state_search(struct state *p, const struct opts *opts, |
|
Line 322 state_search(struct state *p, const struct opts *opts, |
|
|
|
if (0 != regcomp(®, q, ch)) { |
if (0 != regcomp(®, q, ch)) { |
fprintf(stderr, "%s: Bad pattern\n", q); |
fprintf(stderr, "%s: Bad pattern\n", q); |
return; |
return(0); |
} |
} |
|
|
regp = ® |
regp = ® |
Line 358 state_search(struct state *p, const struct opts *opts, |
|
Line 358 state_search(struct state *p, const struct opts *opts, |
|
*/ |
*/ |
|
|
if (key.size < 2 || 8 != val.size) { |
if (key.size < 2 || 8 != val.size) { |
fprintf(stderr, "%s: Corrupt database\n", p->dbf); |
fprintf(stderr, "%s: Bad database\n", p->dbf); |
exit(EXIT_FAILURE); |
goto out; |
} |
} |
|
|
buf_redup(mc, &buf, &bufsz, (char *)key.data); |
buf_redup(mc, &buf, &bufsz, (char *)key.data); |
Line 396 state_search(struct state *p, const struct opts *opts, |
|
Line 396 state_search(struct state *p, const struct opts *opts, |
|
memcpy(&rec, val.data + 4, sizeof(recno_t)); |
memcpy(&rec, val.data + 4, sizeof(recno_t)); |
|
|
if ( ! state_getrecord(p, rec, &record)) |
if ( ! state_getrecord(p, rec, &record)) |
exit(EXIT_FAILURE); |
goto out; |
|
|
/* If we're in a different section, skip... */ |
/* If we're in a different section, skip... */ |
|
|
Line 456 state_search(struct state *p, const struct opts *opts, |
|
Line 456 state_search(struct state *p, const struct opts *opts, |
|
len++; |
len++; |
} |
} |
|
|
send: |
|
if (ch < 0) { |
if (ch < 0) { |
perror(p->dbf); |
perror(p->dbf); |
exit(EXIT_FAILURE); |
goto out; |
} |
} |
|
send: |
/* Sort our results. */ |
/* Sort our results. */ |
|
|
if (SORT_CAT == opts->sort) |
if (SORT_CAT == opts->sort) |
|
|
qsort(res, len, sizeof(struct res), sort_title); |
qsort(res, len, sizeof(struct res), sort_title); |
|
|
state_output(res, len); |
state_output(res, len); |
|
rc = 1; |
|
out: |
for (len-- ; len >= 0; len--) { |
for (len-- ; len >= 0; len--) { |
free(res[len].keyword); |
free(res[len].keyword); |
free(res[len].title); |
free(res[len].title); |
|
|
|
|
if (regp) |
if (regp) |
regfree(regp); |
regfree(regp); |
|
|
|
return(rc); |
} |
} |
|
|
/* |
/* |