aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-05-14 16:08:34 +0000
committerSergey Poznyakoff <gray@gnu.org.ua>2013-05-14 16:08:34 +0000
commit8cbaa44e2f1b80dd8954a0e06e0bc8a52494237f (patch)
treeacb81d52eed9a1e4f72a02b251b4bfb76691ef25
parent045ec749566683e9a4471a81901b7a3f37066376 (diff)
downloadgdbm-8cbaa44e2f1b80dd8954a0e06e0bc8a52494237f.tar.gz
gdbm-8cbaa44e2f1b80dd8954a0e06e0bc8a52494237f.tar.bz2
Change variable support.
* src/util.c: New file. * src/Makefile.am: Add new files. * src/gdbmtool.c (open_mode): New variable. (terror): Remove. (mkfilename, tildexpand): Move to util.c (opendb): Use open_mode. (import_handler): Likewise. (optab): New option -N (--norc). * src/gdbmtool.h (vparse_error): Rename to vlerror. (parse_error): Rename to lerror. (syntax_error): Rename tp terror. All uses updated. (VAR_ERR_FAILURE): Remove. (VAR_ERR_BADVALUE): New error code. (variable_mode_name): Remove. (mkfilename, tildexpand) (vgetyn, getyn): New protos. * src/lex.l (initialized): New static. (setsource): Set initialized. (vlerror): Print locus only if the lexer has been initialized. * src/var.c: Rewrite.
-rw-r--r--ChangeLog28
-rw-r--r--src/Makefile.am7
-rw-r--r--src/datconv.c8
-rw-r--r--src/gdbmtool.c186
-rw-r--r--src/gdbmtool.h21
-rw-r--r--src/gram.y24
-rw-r--r--src/lex.l23
-rw-r--r--src/util.c127
-rw-r--r--src/var.c140
9 files changed, 340 insertions, 224 deletions
diff --git a/ChangeLog b/ChangeLog
index 0ee3823..1f9d600 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -2,2 +2,30 @@
+ Change variable handling.
+
+ * src/util.c: New file.
+ * src/Makefile.am: Add new files.
+ * src/gdbmtool.c (open_mode): New variable.
+ (terror): Remove.
+ (mkfilename, tildexpand): Move to util.c
+ (opendb): Use open_mode.
+ (import_handler): Likewise.
+ (optab): New option -N (--norc).
+
+ * src/gdbmtool.h (vparse_error): Rename to vlerror.
+ (parse_error): Rename to lerror.
+ (syntax_error): Rename tp terror.
+ All uses updated.
+ (VAR_ERR_FAILURE): Remove.
+ (VAR_ERR_BADVALUE): New error code.
+ (variable_mode_name): Remove.
+ (mkfilename, tildexpand)
+ (vgetyn, getyn): New protos.
+ * src/lex.l (initialized): New static.
+ (setsource): Set initialized.
+ (vlerror): Print locus only if the lexer has been
+ initialized.
+ * src/var.c: Rewrite.
+
+2013-05-14 Sergey Poznyakoff <gray@gnu.org.ua>
+
Provide "open" and "close" commands; implement new variables.
diff --git a/src/Makefile.am b/src/Makefile.am
index bf2f21e..b8bebde 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -28,3 +28,3 @@ noinst_HEADERS = \
-EXTRA_DIST = gdbm.h.in
+EXTRA_DIST = gdbm.h.in gram.h
@@ -87,5 +87,4 @@ gdbmtool_SOURCES = \
gdbmtool.c\
- var.c
-
-EXTRA_DIST = gram.h
+ var.c\
+ util.c
diff --git a/src/datconv.c b/src/datconv.c
index 0178f41..95656db 100644
--- a/src/datconv.c
+++ b/src/datconv.c
@@ -321,3 +321,3 @@ datum_scan_notag (datum *dat, struct dsegm *ds, struct kvpair *kv)
{
- parse_error (&kv->loc,
+ lerror (&kv->loc,
_("mixing tagged and untagged values is not allowed"));
@@ -338,3 +338,3 @@ datum_scan_notag (datum *dat, struct dsegm *ds, struct kvpair *kv)
if (err)
- parse_error (&kv->loc, _("cannot convert"));
+ lerror (&kv->loc, _("cannot convert"));
break;
@@ -348,3 +348,3 @@ datum_scan_notag (datum *dat, struct dsegm *ds, struct kvpair *kv)
{
- parse_error (&kv->loc,
+ lerror (&kv->loc,
_("cannot convert value #%d: %s"),
@@ -385,3 +385,3 @@ datum_scan_tag (datum *dat, struct dsegm *ds, struct kvpair *kv)
{
- parse_error (&kv->loc, "tagged values are not yet supported");
+ lerror (&kv->loc, "tagged values are not yet supported");
return 1;
diff --git a/src/gdbmtool.c b/src/gdbmtool.c
index a561bfc..6083edc 100644
--- a/src/gdbmtool.c
+++ b/src/gdbmtool.c
@@ -39,2 +39,3 @@ datum return_data; /* Current data */
int quiet_option = 0; /* Omit the usual welcome banner at startup */
+int open_mode; /* Default open mode */
@@ -45,66 +46,2 @@ unsigned input_line;
-void
-terror (int code, const char *fmt, ...)
-{
- va_list ap;
- if (!interactive)
- fprintf (stderr, "%s: ", progname);
- va_start (ap, fmt);
- vfprintf (stderr, fmt, ap);
- va_end (ap);
- fputc ('\n', stderr);
- if (code)
- exit (code);
-}
-
-char *
-mkfilename (const char *dir, const char *file, const char *suf)
-{
- char *tmp;
- size_t dirlen = strlen (dir);
- size_t suflen = suf ? strlen (suf) : 0;
- size_t fillen = strlen (file);
- size_t len;
-
- while (dirlen > 0 && dir[dirlen-1] == '/')
- dirlen--;
-
- len = dirlen + (dir[0] ? 1 : 0) + fillen + suflen;
- tmp = emalloc (len + 1);
- memcpy (tmp, dir, dirlen);
- if (dir[0])
- tmp[dirlen++] = '/';
- memcpy (tmp + dirlen, file, fillen);
- if (suf)
- memcpy (tmp + dirlen + fillen, suf, suflen);
- tmp[len] = 0;
- return tmp;
-}
-
-char *
-tildexpand (char *s)
-{
- if (s[0] == '~')
- {
- char *p = s + 1;
- size_t len = strcspn (p, "/");
- struct passwd *pw;
-
- if (len == 0)
- pw = getpwuid (getuid ());
- else
- {
- char *user = emalloc (len + 1);
-
- memcpy (user, p, len);
- user[len] = 0;
- pw = getpwnam (user);
- free (user);
- }
- if (pw)
- return mkfilename (pw->pw_dir, p + len + 1, NULL);
- }
- return estrdup (s);
-}
-
static int
@@ -129,10 +66,13 @@ opendb (char *dbname)
- if (variable_is_set ("readonly"))
- flags = GDBM_READER;
- else if (variable_is_set ("newdb"))
- flags |= GDBM_NEWDB;
- else
- flags |= GDBM_WRCREAT;
+ if (open_mode == GDBM_NEWDB)
+ {
+ if (interactive && variable_is_set ("confirm") &&
+ access (dbname, F_OK) == 0)
+ {
+ if (!getyn (_("database %s already exists; overwrite"), dbname))
+ return 1;
+ }
+ }
- db = gdbm_open (dbname, block_size, flags, 00664, NULL);
+ db = gdbm_open (dbname, block_size, open_mode | flags, 0644, NULL);
@@ -140,4 +80,4 @@ opendb (char *dbname)
{
- syntax_error (_("cannot open database %s: %s"), dbname,
- gdbm_strerror (gdbm_errno));
+ terror (_("cannot open database %s: %s"), dbname,
+ gdbm_strerror (gdbm_errno));
return 1;
@@ -147,3 +87,3 @@ opendb (char *dbname)
-1)
- syntax_error (_("gdbm_setopt failed: %s"),
+ terror (_("gdbm_setopt failed: %s"),
gdbm_strerror (gdbm_errno));
@@ -165,3 +105,3 @@ checkdb ()
file_name = estrdup (GDBMTOOL_DEFFILE);
- syntax_error (_("warning: using default database file %s"),
+ terror (_("warning: using default database file %s"),
file_name);
@@ -230,3 +170,3 @@ _gdbm_avail_list_size (GDBM_FILE dbf, size_t min_size)
{
- terror (0, "lseek: %s", strerror (errno));
+ terror ("lseek: %s", strerror (errno));
break;
@@ -237,5 +177,6 @@ _gdbm_avail_list_size (GDBM_FILE dbf, size_t min_size)
if (rc == GDBM_FILE_EOF)
- terror (0, "read: %s", gdbm_strerror (rc));
+ terror ("read: %s", gdbm_strerror (rc));
else
- terror (0, "read: %s (%s)", gdbm_strerror (rc), strerror (errno));
+ terror ("read: %s (%s)",
+ gdbm_strerror (rc), strerror (errno));
break;
@@ -282,3 +223,3 @@ _gdbm_print_avail_list (FILE *fp, GDBM_FILE dbf)
{
- terror (0, "lseek: %s", strerror (errno));
+ terror ("lseek: %s", strerror (errno));
break;
@@ -289,5 +230,5 @@ _gdbm_print_avail_list (FILE *fp, GDBM_FILE dbf)
if (rc == GDBM_FILE_EOF)
- terror (0, "read: %s", gdbm_strerror (rc));
+ terror ("read: %s", gdbm_strerror (rc));
else
- terror (0, "read: %s (%s)", gdbm_strerror (rc), strerror (errno));
+ terror ("read: %s (%s)", gdbm_strerror (rc), strerror (errno));
break;
@@ -417,3 +358,3 @@ close_handler (struct handler_param *param)
if (!gdbm_file)
- syntax_error (_("nothing to close"));
+ terror (_("nothing to close"));
gdbm_close (gdbm_file);
@@ -739,3 +680,6 @@ list_handler (struct handler_param *param)
if (!data.dptr)
- terror (0, _("cannot fetch data (key %.*s)"), key.dsize, key.dptr);
+ {
+ terror (_("cannot fetch data; the key was:"));
+ datum_format (stderr, &key, dsdef[DS_KEY]);
+ }
else
@@ -781,3 +725,3 @@ export_handler (struct handler_param *param)
{
- syntax_error (_("unrecognized argument: %s"),
+ terror (_("unrecognized argument: %s"),
param->argv[i]->v.string);
@@ -789,3 +733,4 @@ export_handler (struct handler_param *param)
{
- terror (0, _("error dumping database: %s"), gdbm_strerror (gdbm_errno));
+ terror (_("error dumping database: %s"),
+ gdbm_strerror (gdbm_errno));
}
@@ -811,3 +756,3 @@ import_handler (struct handler_param *param)
{
- syntax_error (_("unrecognized argument: %s"),
+ terror (_("unrecognized argument: %s"),
param->argv[i]->v.string);
@@ -821,9 +766,7 @@ import_handler (struct handler_param *param)
{
- const char *varname = variable_mode_name ();
- int t = 1;
+ int t = open_mode;
- variable_set ("newdb", VART_BOOL, &t);
+ open_mode = GDBM_NEWDB;
rc = checkdb ();
- if (varname)
- variable_set (varname, VART_BOOL, &t);
+ open_mode = t;
@@ -841,4 +784,4 @@ import_handler (struct handler_param *param)
case GDBM_ERR_FILE_MODE:
- terror (0, _("error restoring metadata: %s (%s)"),
- gdbm_strerror (gdbm_errno), strerror (errno));
+ terror (_("error restoring metadata: %s (%s)"),
+ gdbm_strerror (gdbm_errno), strerror (errno));
break;
@@ -847,7 +790,7 @@ import_handler (struct handler_param *param)
if (err_line)
- terror (0, "%s:%lu: %s", param->argv[0], err_line,
- gdbm_strerror (gdbm_errno));
+ terror ("%s:%lu: %s", param->argv[0], err_line,
+ gdbm_strerror (gdbm_errno));
else
- terror (0, _("cannot load from %s: %s"), param->argv[0],
- gdbm_strerror (gdbm_errno));
+ terror (_("cannot load from %s: %s"), param->argv[0],
+ gdbm_strerror (gdbm_errno));
}
@@ -858,3 +801,3 @@ import_handler (struct handler_param *param)
if (gdbm_setopt (gdbm_file, GDBM_GETDBNAME, &file_name, sizeof (file_name)))
- syntax_error (_("gdbm_setopt failed: %s"), gdbm_strerror (gdbm_errno));
+ terror (_("gdbm_setopt failed: %s"), gdbm_strerror (gdbm_errno));
}
@@ -1106,3 +1049,3 @@ command_lookup (const char *str, struct locus *loc, struct command **pcmd)
if (state == fcom_init)
- parse_error (loc,
+ lerror (loc,
interactive ? _("Invalid command. Try ? for help.") :
@@ -1126,2 +1069,3 @@ struct gdbm_option optab[] = {
{ 'n', "newdb", NULL, N_("create database") },
+ { 'N', "norc", NULL, N_("do not read .gdbmtoolrc file") },
{ 'r', "read-only", NULL, N_("open database in read-only mode") },
@@ -1368,3 +1312,3 @@ coerce (struct gdbmarg *arg, struct argdef *def)
{
- parse_error (&arg->loc, _("cannot coerce %s to %s"),
+ lerror (&arg->loc, _("cannot coerce %s to %s"),
argtypestr[arg->type], argtypestr[def->type]);
@@ -1413,3 +1357,3 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
{
- syntax_error (_("%s: not enough arguments"), cmd->name);
+ terror (_("%s: not enough arguments"), cmd->name);
return 1;
@@ -1419,3 +1363,6 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
if (fgets (argbuf, sizeof argbuf, stdin) == NULL)
- terror (EXIT_USAGE, _("unexpected eof"));
+ {
+ terror (_("unexpected eof"));
+ exit (EXIT_USAGE);
+ }
@@ -1440,3 +1387,3 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
{
- syntax_error (_("%s: too many arguments"), cmd->name);
+ terror (_("%s: too many arguments"), cmd->name);
return 1;
@@ -1467,4 +1414,4 @@ run_command (struct command *cmd, struct gdbmarglist *arglist)
{
- terror (0, _("cannot run pager `%s': %s"), pager,
- strerror (errno));
+ terror (_("cannot run pager `%s': %s"), pager,
+ strerror (errno));
pager = NULL;
@@ -1508,3 +1455,3 @@ source_rcfile ()
{
- terror (0, _("cannot find home directory"));
+ terror (_("cannot find home directory"));
return;
@@ -1529,2 +1476,3 @@ main (int argc, char *argv[])
int bv;
+ int norc = 0;
@@ -1539,2 +1487,4 @@ main (int argc, char *argv[])
sort_commands ();
+
+ variable_set ("open", VART_STRING, "wrcreat");
@@ -1561,4 +1511,3 @@ main (int argc, char *argv[])
case 'r':
- bv = 1;
- variable_set ("readonly", VART_BOOL, &bv);
+ variable_set ("open", VART_STRING, "readonly");
break;
@@ -1566,4 +1515,7 @@ main (int argc, char *argv[])
case 'n':
- bv = 1;
- variable_set ("newdb", VART_BOOL, &bv);
+ variable_set ("open", VART_STRING, "newdb");
+ break;
+
+ case 'N':
+ norc = 1;
break;
@@ -1587,4 +1539,5 @@ main (int argc, char *argv[])
default:
- terror (EXIT_USAGE,
- _("unknown option; try `%s -h' for more info\n"), progname);
+ terror (_("unknown option; try `%s -h' for more info"),
+ progname);
+ exit (EXIT_USAGE);
}
@@ -1595,3 +1548,7 @@ main (int argc, char *argv[])
if (argc > 1)
- terror (EXIT_USAGE, _("too many arguments"));
+ {
+ terror (_("too many arguments"));
+ exit (EXIT_USAGE);
+ }
+
if (argc == 1)
@@ -1613,3 +1570,4 @@ main (int argc, char *argv[])
- source_rcfile ();
+ if (!norc)
+ source_rcfile ();
diff --git a/src/gdbmtool.h b/src/gdbmtool.h
index 8c585f0..db18669 100644
--- a/src/gdbmtool.h
+++ b/src/gdbmtool.h
@@ -91,6 +91,6 @@ typedef struct locus gdbm_yyltype_t;
-void vparse_error (struct locus *loc, const char *fmt, va_list ap);
-void parse_error (struct locus *loc, const char *fmt, ...);
+void vlerror (struct locus *loc, const char *fmt, va_list ap);
+void lerror (struct locus *loc, const char *fmt, ...);
-void syntax_error (const char *fmt, ...);
+void terror (const char *fmt, ...);
@@ -102,2 +102,3 @@ extern char *file_name;
extern int interactive;
+extern int open_mode;
@@ -229,6 +230,6 @@ extern struct dsegm *dsdef[];
-#define VAR_OK 0
-#define VAR_ERR_NOTDEF 1
-#define VAR_ERR_BADTYPE 2
-#define VAR_ERR_FAILURE 3
+#define VAR_OK 0
+#define VAR_ERR_NOTDEF 1
+#define VAR_ERR_BADTYPE 2
+#define VAR_ERR_BADVALUE 3
@@ -238,3 +239,2 @@ int variable_is_set (const char *name);
void variable_print_all (FILE *fp);
-const char *variable_mode_name ();
@@ -254,2 +254,5 @@ void dsprint (FILE *fp, int what, struct dsegm *ds);
-
+char *mkfilename (const char *dir, const char *file, const char *suf);
+char *tildexpand (char *s);
+int vgetyn (const char *prompt, va_list ap);
+int getyn (const char *prompt, ...);
diff --git a/src/gram.y b/src/gram.y
index 75c7e86..a54ef0e 100644
--- a/src/gram.y
+++ b/src/gram.y
@@ -203,4 +203,4 @@ defid : T_IDENT
{
- syntax_error (_("expected \"key\" or \"content\", "
- "but found \"%s\""), $1);
+ terror (_("expected \"key\" or \"content\", "
+ "but found \"%s\""), $1);
YYERROR;
@@ -273,3 +273,3 @@ var : T_IDENT
case VAR_ERR_NOTDEF:
- parse_error (&@1, _("no such variable: %s"), varname);
+ lerror (&@1, _("no such variable: %s"), varname);
break;
@@ -277,3 +277,3 @@ var : T_IDENT
case VAR_ERR_BADTYPE:
- parse_error (&@1, _("%s is not a boolean variable"), varname);
+ lerror (&@1, _("%s is not a boolean variable"), varname);
break;
@@ -285,3 +285,2 @@ var : T_IDENT
int rc = variable_set ($1, VART_STRING, $3);
- free ($3);
switch (rc)
@@ -292,3 +291,3 @@ var : T_IDENT
case VAR_ERR_NOTDEF:
- parse_error (&@1, _("no such variable: %s"), $1);
+ lerror (&@1, _("no such variable: %s"), $1);
break;
@@ -296,3 +295,7 @@ var : T_IDENT
case VAR_ERR_BADTYPE:
- parse_error (&@1, _("%s is not a string variable"), $1);
+ lerror (&@1, _("%s: bad variable type"), $1);
+ break;
+
+ case VAR_ERR_BADVALUE:
+ lerror (&@1, _("%s: value %s is not allowed"), $1, $3);
break;
@@ -300,2 +303,3 @@ var : T_IDENT
free($1);
+ free ($3);
}
@@ -305,3 +309,3 @@ var : T_IDENT
void
-syntax_error (const char *fmt, ...)
+terror (const char *fmt, ...)
{
@@ -310,3 +314,3 @@ syntax_error (const char *fmt, ...)
va_start (ap, fmt);
- vparse_error (&yylloc, fmt, ap);
+ vlerror (&yylloc, fmt, ap);
va_end (ap);
@@ -317,3 +321,3 @@ yyerror (char *s)
{
- syntax_error ("%s", s);
+ terror ("%s", s);
return 0;
diff --git a/src/lex.l b/src/lex.l
index dcd9af3..919bbad 100644
--- a/src/lex.l
+++ b/src/lex.l
@@ -76,3 +76,4 @@ static dev_t dev;
int interactive; /* Are we running in interactive mode? */
-
+static int initialized;
+
static void
@@ -146,3 +147,3 @@ setsource (const char *name, int intr)
{
- syntax_error (_("cannot open `%s': %s"), name, strerror (errno));
+ terror (_("cannot open `%s': %s"), name, strerror (errno));
return -1;
@@ -151,3 +152,3 @@ setsource (const char *name, int intr)
{
- syntax_error (_("%s is not a regular file"), name);
+ terror (_("%s is not a regular file"), name);
return -1;
@@ -158,5 +159,5 @@ setsource (const char *name, int intr)
{
- syntax_error (_("recursive sourcing"));
+ terror (_("recursive sourcing"));
if (cp->parent)
- parse_error (&cp->locus, _("%s already sourced here"), name);
+ lerror (&cp->locus, _("%s already sourced here"), name);
return 1;
@@ -167,3 +168,3 @@ setsource (const char *name, int intr)
{
- syntax_error (_("cannot open %s for reading: %s"), name,
+ terror (_("cannot open %s for reading: %s"), name,
strerror (errno));
@@ -187,2 +188,4 @@ setsource (const char *name, int intr)
+ initialized = 1;
+
return 0;
@@ -421,3 +424,3 @@ escape (int c)
void
-vparse_error (struct locus *loc, const char *fmt, va_list ap)
+vlerror (struct locus *loc, const char *fmt, va_list ap)
{
@@ -425,3 +428,3 @@ vparse_error (struct locus *loc, const char *fmt, va_list ap)
fprintf (stderr, "%s: ", progname);
- if (loc)
+ if (initialized && loc && loc->beg.file)
{
@@ -435,3 +438,3 @@ vparse_error (struct locus *loc, const char *fmt, va_list ap)
void
-parse_error (struct locus *loc, const char *fmt, ...)
+lerror (struct locus *loc, const char *fmt, ...)
{
@@ -440,3 +443,3 @@ parse_error (struct locus *loc, const char *fmt, ...)
va_start (ap, fmt);
- vparse_error (loc, fmt, ap);
+ vlerror (loc, fmt, ap);
va_end (ap);
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..d46325b
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,127 @@
+/* This file is part of GDBM, the GNU data base manager.
+ Copyright (C) 1990, 1991, 1993, 2007, 2011, 2013 Free Software Foundation,
+ Inc.
+
+ GDBM is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3, or (at your option)
+ any later version.
+
+ GDBM is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with GDBM. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "gdbmtool.h"
+#include <pwd.h>
+
+char *
+mkfilename (const char *dir, const char *file, const char *suf)
+{
+ char *tmp;
+ size_t dirlen = strlen (dir);
+ size_t suflen = suf ? strlen (suf) : 0;
+ size_t fillen = strlen (file);
+ size_t len;
+
+ while (dirlen > 0 && dir[dirlen-1] == '/')
+ dirlen--;
+
+ len = dirlen + (dir[0] ? 1 : 0) + fillen + suflen;
+ tmp = emalloc (len + 1);
+ memcpy (tmp, dir, dirlen);
+ if (dir[0])
+ tmp[dirlen++] = '/';
+ memcpy (tmp + dirlen, file, fillen);
+ if (suf)
+ memcpy (tmp + dirlen + fillen, suf, suflen);
+ tmp[len] = 0;
+ return tmp;
+}
+
+char *
+tildexpand (char *s)
+{
+ if (s[0] == '~')
+ {
+ char *p = s + 1;
+ size_t len = strcspn (p, "/");
+ struct passwd *pw;
+
+ if (len == 0)
+ pw = getpwuid (getuid ());
+ else
+ {
+ char *user = emalloc (len + 1);
+
+ memcpy (user, p, len);
+ user[len] = 0;
+ pw = getpwnam (user);
+ free (user);
+ }
+ if (pw)
+ return mkfilename (pw->pw_dir, p + len + 1, NULL);
+ }
+ return estrdup (s);
+}
+
+int
+vgetyn (const char *prompt, va_list ap)
+{
+ int state = 0;
+ int c, resp;
+
+ do
+ {
+ switch (state)
+ {
+ case 1:
+ if (c == ' ' || c == '\t')
+ continue;
+ resp = c;
+ state = 2;
+ /* fall through */
+ case 2:
+ if (c == '\n')
+ {
+ switch (resp)
+ {
+ case 'y':
+ case 'Y':
+ return 1;
+ case 'n':
+ case 'N':
+ return 0;
+ default:
+ fprintf (stdout, "%s\n", _("Please, reply 'y' or 'n'"));
+ }
+ state = 0;
+ } else
+ break;
+
+ case 0:
+ vfprintf (stdout, prompt, ap);
+ fprintf (stdout, " [y/n]?");
+ fflush (stdout);
+ state = 1;
+ break;
+ }
+ } while ((c = getchar ()) != EOF);
+ exit (EXIT_USAGE);
+}
+
+int
+getyn (const char *prompt, ...)
+{
+ va_list ap;
+ int rc;
+
+ va_start (ap, prompt);
+ rc = vgetyn (prompt, ap);
+ va_end (ap);
+ return rc;
+}
+
diff --git a/src/var.c b/src/var.c
index ffa7297..a518e94 100644
--- a/src/var.c
+++ b/src/var.c
@@ -22,2 +22,9 @@
+union value
+{
+ char *string;
+ int bool;
+ int num;
+};
+
struct variable
@@ -27,9 +34,4 @@ struct variable
int flags;
- union
- {
- char *string;
- int bool;
- int num;
- } v;
- int (*hook) (struct variable *, int);
+ union value v;
+ int (*hook) (struct variable *, union value *);
void *hook_data;
@@ -37,3 +39,3 @@ struct variable
-static int mode_toggle (struct variable *var, int after);
+static int open_hook (struct variable *, union value *);
@@ -48,6 +50,6 @@ static struct variable vartab[] = {
{ "delim2", VART_STRING, VARF_DFL, { "," } },
+ { "confirm", VART_BOOL, VARF_DFL, { num: 1 } },
{ "cachesize", VART_INT, VARF_DFL, { num: DEFAULT_CACHESIZE } },
{ "blocksize", VART_INT, VARF_DFL, { num: 0 } },
- { "readonly", VART_BOOL, VARF_DFL, { num: 0 }, mode_toggle },
- { "newdb", VART_BOOL, VARF_DFL, { 0 }, mode_toggle },
+ { "open", VART_STRING, VARF_DFL, { NULL }, open_hook },
{ "lock", VART_BOOL, VARF_DFL, { num: 1 } },
@@ -59,35 +61,27 @@ static struct variable vartab[] = {
static int
-mode_toggle (struct variable *var, int after)
-{
- if (after)
- {
- struct variable *vp;
- int newval = !var->v.bool;
-
- for (vp = vartab; vp->name; vp++)
- {
- if (vp == var)
- continue;
- else if (vp->hook == mode_toggle)
- {
- vp->v.bool = newval;
- newval = 0;
- }
- }
- }
- return 0;
-}
-
-const char *
-variable_mode_name ()
+open_hook (struct variable *var, union value *v)
{
- struct variable *vp;
+ static struct {
+ char *s;
+ int t;
+ } trans[] = {
+ { "newdb", GDBM_NEWDB },
+ { "wrcreat", GDBM_WRCREAT },
+ { "rw", GDBM_WRCREAT },
+ { "reader", GDBM_READER },
+ { "readonly", GDBM_READER },
+ { NULL }
+ };
+ int i;
- for (vp = vartab; vp->name; vp++)
- if (vp->hook == mode_toggle && vp->v.bool)
- return vp->name;
+ for (i = 0; trans[i].s; i++)
+ if (strcmp (trans[i].s, v->string) == 0)
+ {
+ open_mode = trans[i].t;
+ return VAR_OK;
+ }
- return NULL;
+ return VAR_ERR_BADVALUE;
}
-
+
static struct variable *
@@ -104,9 +98,9 @@ varfind (const char *name)
-typedef int (*setvar_t) (struct variable *, void *);
+typedef int (*setvar_t) (union value *, void *);
static int
-s2s (struct variable *vp, void *val)
+s2s (union value *vp, void *val)
{
- vp->v.string = estrdup (val);
- return 0;
+ vp->string = estrdup (val);
+ return VAR_OK;
}
@@ -114,6 +108,6 @@ s2s (struct variable *vp, void *val)
static int
-b2s (struct variable *vp, void *val)
+b2s (union value *vp, void *val)
{
- vp->v.string = estrdup (*(int*)val ? "true" : "false");
- return 0;
+ vp->string = estrdup (*(int*)val ? "true" : "false");
+ return VAR_OK;
}
@@ -121,3 +115,3 @@ b2s (struct variable *vp, void *val)
static int
-i2s (struct variable *vp, void *val)
+i2s (union value *vp, void *val)
{
@@ -125,4 +119,4 @@ i2s (struct variable *vp, void *val)
snprintf (buf, sizeof buf, "%d", *(int*)val);
- vp->v.string = estrdup (buf);
- return 0;
+ vp->string = estrdup (buf);
+ return VAR_OK;
}
@@ -130,3 +124,3 @@ i2s (struct variable *vp, void *val)
static int
-s2b (struct variable *vp, void *val)
+s2b (union value *vp, void *val)
{
@@ -141,3 +135,3 @@ s2b (struct variable *vp, void *val)
{
- vp->v.bool = 1;
+ vp->bool = 1;
return 0;
@@ -148,3 +142,3 @@ s2b (struct variable *vp, void *val)
{
- vp->v.bool = 0;
+ vp->bool = 0;
return 1;
@@ -155,3 +149,3 @@ s2b (struct variable *vp, void *val)
return VAR_ERR_BADTYPE;
- vp->v.bool = !!n;
+ vp->bool = !!n;
return VAR_OK;
@@ -160,3 +154,3 @@ s2b (struct variable *vp, void *val)
static int
-s2i (struct variable *vp, void *val)
+s2i (union value *vp, void *val)
{
@@ -168,3 +162,3 @@ s2i (struct variable *vp, void *val)
- vp->v.num = n;
+ vp->num = n;
return VAR_OK;
@@ -173,5 +167,5 @@ s2i (struct variable *vp, void *val)
static int
-b2b (struct variable *vp, void *val)
+b2b (union value *vp, void *val)
{
- vp->v.bool = !!*(int*)val;
+ vp->bool = !!*(int*)val;
return VAR_OK;
@@ -180,5 +174,5 @@ b2b (struct variable *vp, void *val)
static int
-b2i (struct variable *vp, void *val)
+b2i (union value *vp, void *val)
{
- vp->v.num = *(int*)val;
+ vp->num = *(int*)val;
return VAR_OK;
@@ -187,5 +181,5 @@ b2i (struct variable *vp, void *val)
static int
-i2i (struct variable *vp, void *val)
+i2i (union value *vp, void *val)
{
- vp->v.num = *(int*)val;
+ vp->num = *(int*)val;
return VAR_OK;
@@ -194,5 +188,5 @@ i2i (struct variable *vp, void *val)
static int
-i2b (struct variable *vp, void *val)
+i2b (union value *vp, void *val)
{
- vp->v.bool = *(int*)val;
+ vp->bool = *(int*)val;
return VAR_OK;
@@ -212,2 +206,3 @@ variable_set (const char *name, int type, void *val)
int rc;
+ union value v;
@@ -216,4 +211,9 @@ variable_set (const char *name, int type, void *val)
- if (vp->hook && vp->hook (vp, 0))
- return VAR_ERR_FAILURE;
+ memset (&v, 0, sizeof (v));
+ rc = setvar[vp->type][type] (&v, val);
+ if (rc)
+ return rc;
+
+ if (vp->hook && (rc = vp->hook (vp, &v)) != VAR_OK)
+ return rc;
@@ -221,6 +221,3 @@ variable_set (const char *name, int type, void *val)
free (vp->v.string);
- rc = setvar[vp->type][type] (vp, val);
-
- if (rc)
- return rc;
+ vp->v = v;
@@ -228,5 +225,2 @@ variable_set (const char *name, int type, void *val)
- if (vp->hook)
- vp->hook (vp, 1);
-
return VAR_OK;
@@ -278,3 +272,3 @@ variable_print_all (FILE *fp)
case VART_BOOL:
- fprintf (fp, "%s%s", vp->v.bool ? "no" : "", vp->name);
+ fprintf (fp, "%s%s", vp->v.bool ? "" : "no", vp->name);
break;

Return to:

Send suggestions and report system problems to the System administrator.