summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org>2021-08-12 08:58:38 +0300
committerSergey Poznyakoff <gray@gnu.org>2021-08-12 09:10:10 +0300
commit0455af7cfa6dc8a285a9d4140e8cca0af9c41acd (patch)
treebb4d7aee6997e31e347e7ef2a519681d012ca9d2
parentcc7051ae2ea384863937083a3a60a5a008d511a5 (diff)
downloadgdbm-0455af7cfa6dc8a285a9d4140e8cca0af9c41acd.tar.gz
gdbmtool: setting database option affects the current database
* src/gdbmshell.c (gdbmshell_setopt): New function. * src/gdbmtool.h (gdbmshell_setopt): New proto. * src/var.c: Provide set hooks for: cachesize, coalesce, and centfree.
-rw-r--r--src/gdbmshell.c23
-rw-r--r--src/gdbmtool.h2
-rw-r--r--src/var.c36
3 files changed, 50 insertions, 11 deletions
diff --git a/src/gdbmshell.c b/src/gdbmshell.c
index e117cc2..8a2b07b 100644
--- a/src/gdbmshell.c
+++ b/src/gdbmshell.c
@@ -41,6 +41,21 @@ datum_free (datum *dp)
dp->dptr = NULL;
}
+
+int
+gdbmshell_setopt (char *name, int opt, int val)
+{
+ if (gdbm_file)
+ {
+ if (gdbm_setopt (gdbm_file, opt, &val, sizeof (val)) == -1)
+ {
+ terror (_("%s failed: %s"), name, gdbm_strerror (gdbm_errno));
+ return 1;
+ }
+ }
+ return 0;
+}
+
static void
closedb (void)
{
@@ -131,15 +146,11 @@ opendb (char *dbname, int fd)
if (variable_is_true ("coalesce"))
{
- int t = 1;
- if (gdbm_setopt (db, GDBM_SETCOALESCEBLKS, &t, sizeof (t)) == -1)
- terror (_("gdbm_setopt failed: %s"), gdbm_strerror (gdbm_errno));
+ gdbmshell_setopt ("GDBM_SETCOALESCEBLKS", GDBM_SETCOALESCEBLKS, 1);
}
if (variable_is_true ("centfree"))
{
- int t = 1;
- if (gdbm_setopt (db, GDBM_SETCENTFREE, &t, sizeof (t)) == -1)
- terror (_("gdbm_setopt failed: %s"), gdbm_strerror (gdbm_errno));
+ gdbmshell_setopt ("GDBM_SETCENTFREE", GDBM_SETCENTFREE, 1);
}
if (gdbm_file)
diff --git a/src/gdbmtool.h b/src/gdbmtool.h
index 1f20b5a..4185bf9 100644
--- a/src/gdbmtool.h
+++ b/src/gdbmtool.h
@@ -372,6 +372,8 @@ int getnum (int *pnum, char *arg, char **endp);
int gdbmshell (instream_t input);
int gdbmshell_run (int (*init) (void *, instream_t *), void *data);
+int gdbmshell_setopt (char *name, int opt, int val);
+
void variables_init (void);
void variables_free (void);
diff --git a/src/var.c b/src/var.c
index 6038d13..2772eb1 100644
--- a/src/var.c
+++ b/src/var.c
@@ -47,6 +47,9 @@ static int open_typeconv (struct variable *var, int type, void **retptr);
static int format_sethook (struct variable *, union value *);
static int format_typeconv (struct variable *var, int type, void **retptr);
static int fd_sethook (struct variable *, union value *);
+static int centfree_sethook (struct variable *var, union value *v);
+static int coalesce_sethook (struct variable *var, union value *v);
+static int cachesize_sethook (struct variable *var, union value *v);
static struct variable vartab[] = {
/* Top-level prompt */
@@ -86,7 +89,8 @@ static struct variable vartab[] = {
{
.name = "cachesize",
.type = VART_INT,
- .flags = VARF_DFL
+ .flags = VARF_DFL,
+ .sethook = cachesize_sethook
},
{
.name = "blocksize",
@@ -122,13 +126,15 @@ static struct variable vartab[] = {
.name = "coalesce",
.type = VART_BOOL,
.flags = VARF_INIT,
- .init = { .bool = 0 }
+ .init = { .bool = 0 },
+ .sethook = coalesce_sethook
},
{
.name = "centfree",
.type = VART_BOOL,
.flags = VARF_INIT,
- .init = { .bool = 0 }
+ .init = { .bool = 0 },
+ .sethook = centfree_sethook
},
{
.name = "filemode",
@@ -486,7 +492,7 @@ variables_init (void)
for (vp = vartab; vp->name; vp++)
{
- if (vp->flags & VARF_INIT)
+ if (!(vp->flags & VARF_SET) && (vp->flags & VARF_INIT))
{
if (vp->type == VART_STRING)
variable_set (vp->name, vp->type, vp->init.string);
@@ -583,6 +589,26 @@ fd_sethook (struct variable *var, union value *v)
return VAR_OK;
}
-
+static int
+cachesize_sethook (struct variable *var, union value *v)
+{
+ if (v->num < 0)
+ return VAR_ERR_BADVALUE;
+ return gdbmshell_setopt ("GDBM_SETCACHESIZE", GDBM_SETCACHESIZE, v->num) == 0
+ ? VAR_OK : VAR_ERR_BADVALUE;
+}
+
+static int
+centfree_sethook (struct variable *var, union value *v)
+{
+ return gdbmshell_setopt ("GDBM_SETCENTFREE", GDBM_SETCENTFREE, v->bool) == 0
+ ? VAR_OK : VAR_ERR_BADVALUE;
+}
+static int
+coalesce_sethook (struct variable *var, union value *v)
+{
+ return gdbmshell_setopt ("GDBM_SETCOALESCEBLKS", GDBM_SETCOALESCEBLKS, v->bool) == 0
+ ? VAR_OK : VAR_ERR_BADVALUE;
+}