aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/apropos.c
diff options
context:
space:
mode:
authorKristaps Dzonsons <kristaps@bsd.lv>2011-10-09 10:46:38 +0000
committerKristaps Dzonsons <kristaps@bsd.lv>2011-10-09 10:46:38 +0000
commit2a523f572ba9f33bfa20f2cb1a806abf2947b5c5 (patch)
tree61ca2262aa6b863798b459df23c4c89cb5d77043 /apropos.c
parentd9510873b94738ebb329530847654946ad1af52b (diff)
downloadmandoc-2a523f572ba9f33bfa20f2cb1a806abf2947b5c5.tar.gz
mandoc-2a523f572ba9f33bfa20f2cb1a806abf2947b5c5.tar.zst
mandoc-2a523f572ba9f33bfa20f2cb1a806abf2947b5c5.zip
Make apropos results-gathering able to error out and clean up before
making the utility exit non-zero.
Diffstat (limited to 'apropos.c')
-rw-r--r--apropos.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/apropos.c b/apropos.c
index f13d2774..02c5d0ba 100644
--- a/apropos.c
+++ b/apropos.c
@@ -1,4 +1,4 @@
-/* $Id: apropos.c,v 1.6 2011/10/09 10:37:52 kristaps Exp $ */
+/* $Id: apropos.c,v 1.7 2011/10/09 10:46:38 kristaps Exp $ */
/*
* Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
*
@@ -152,7 +152,7 @@ static int sort_title(const void *, const void *);
static int state_getrecord(struct state *,
recno_t, struct rec *);
static void state_output(const struct res *, int);
-static void state_search(struct state *,
+static int state_search(struct state *,
const struct opts *, char *);
static void usage(void);
@@ -275,9 +275,8 @@ main(int argc, char *argv[])
/* Main search function. */
- state_search(&state, &opts, q);
-
- rc = EXIT_SUCCESS;
+ rc = state_search(&state, &opts, q) ?
+ EXIT_SUCCESS : EXIT_FAILURE;
out:
if (state.db)
(*state.db->close)(state.db);
@@ -287,10 +286,10 @@ out:
return(rc);
}
-static void
+static int
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;
char *buf;
size_t bufsz;
@@ -303,6 +302,7 @@ state_search(struct state *p, const struct opts *opts, char *q)
char filebuf[10];
struct rec record;
+ rc = 0;
root = leaf = -1;
res = NULL;
len = 0;
@@ -322,7 +322,7 @@ state_search(struct state *p, const struct opts *opts, char *q)
if (0 != regcomp(&reg, q, ch)) {
fprintf(stderr, "%s: Bad pattern\n", q);
- return;
+ return(0);
}
regp = &reg;
@@ -358,8 +358,8 @@ state_search(struct state *p, const struct opts *opts, char *q)
*/
if (key.size < 2 || 8 != val.size) {
- fprintf(stderr, "%s: Corrupt database\n", p->dbf);
- exit(EXIT_FAILURE);
+ fprintf(stderr, "%s: Bad database\n", p->dbf);
+ goto out;
}
buf_redup(mc, &buf, &bufsz, (char *)key.data);
@@ -396,7 +396,7 @@ state_search(struct state *p, const struct opts *opts, char *q)
memcpy(&rec, val.data + 4, sizeof(recno_t));
if ( ! state_getrecord(p, rec, &record))
- exit(EXIT_FAILURE);
+ goto out;
/* If we're in a different section, skip... */
@@ -456,12 +456,11 @@ state_search(struct state *p, const struct opts *opts, char *q)
len++;
}
-send:
if (ch < 0) {
perror(p->dbf);
- exit(EXIT_FAILURE);
+ goto out;
}
-
+send:
/* Sort our results. */
if (SORT_CAT == opts->sort)
@@ -470,7 +469,8 @@ send:
qsort(res, len, sizeof(struct res), sort_title);
state_output(res, len);
-
+ rc = 1;
+out:
for (len-- ; len >= 0; len--) {
free(res[len].keyword);
free(res[len].title);
@@ -486,6 +486,8 @@ send:
if (regp)
regfree(regp);
+
+ return(rc);
}
/*