summaryrefslogtreecommitdiffabout
path: root/src/var.c
authorSergey Poznyakoff <gray@gnu.org.ua>2013-05-13 16:46:30 (GMT)
committer Sergey Poznyakoff <gray@gnu.org.ua>2013-05-13 16:46:30 (GMT)
commitaa0eaa5bc642167db8cfb5778cf9a0091401a4f3 (patch) (side-by-side diff)
treea5a6cbdc9ca61b6867bd902fc880efd2eaa0f29c /src/var.c
parent1638ed6202b77b521214128189dc4aacdb5fe098 (diff)
downloadgdbm-aa0eaa5bc642167db8cfb5778cf9a0091401a4f3.tar.gz
gdbm-aa0eaa5bc642167db8cfb5778cf9a0091401a4f3.tar.bz2
Implement shell variables in gdbmtool.
* src/var.c: New file. * src/datconv.c (datum_format): Remove the "delim" parameter, use "delim1" and "delim2" shell variables instead. * src/gdbmtool.c: Remove "prompt" command. Use "set ps[12]" instead. (print_prompt): Move to lex.l * src/gdbmtool.h (variable_set) (variable_get, variable_print_all): New protos. * src/gram.y: Handle the "set" command. * src/lex.l: Update.
Diffstat (limited to 'src/var.c') (more/less context) (ignore whitespace changes)
-rw-r--r--src/var.c148
1 files changed, 148 insertions, 0 deletions
diff --git a/src/var.c b/src/var.c
new file mode 100644
index 0000000..4a0ea60
--- a/dev/null
+++ b/src/var.c
@@ -0,0 +1,148 @@
+/* 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"
+
+#define VARF_DFL 0x00
+#define VARF_SET 0x01
+
+struct variable
+{
+ char *name;
+ int type;
+ int flags;
+ union
+ {
+ char *string;
+ int bool;
+ } v;
+};
+
+static struct variable vartab[] = {
+ /* Top-level prompt */
+ { "ps1", VART_STRING, VARF_DFL, { "%p>%_" } },
+ /* Second-level prompt (used within "def" block) */
+ { "ps2", VART_STRING, VARF_DFL, { "%_>%_" } },
+ /* This delimits array members */
+ { "delim1", VART_STRING, VARF_DFL, { "," } },
+ /* This delimits structure members */
+ { "delim2", VART_STRING, VARF_DFL, { "," } },
+ { NULL }
+};
+
+static struct variable *
+varfind (const char *name)
+{
+ struct variable *vp;
+
+ for (vp = vartab; vp->name; vp++)
+ if (strcmp (vp->name, name) == 0)
+ return vp;
+
+ return NULL;
+}
+
+int
+variable_set (const char *name, int type, void *val)
+{
+ struct variable *vp = varfind (name);
+
+ if (!vp)
+ return VAR_ERR_NOTDEF;
+
+ if (type != vp->type)
+ return VAR_ERR_BADTYPE;
+
+ switch (vp->type)
+ {
+ case VART_STRING:
+ if (vp->flags && VARF_SET)
+ free (vp->v.string);
+ vp->v.string = estrdup (val);
+ break;
+
+ case VART_BOOL:
+ if (val == NULL)
+ vp->v.bool = 0;
+ else
+ vp->v.bool = *(int*)val;
+ break;
+ }
+
+ vp->flags |= VARF_SET;
+
+ return VAR_OK;
+}
+
+int
+variable_get (const char *name, int type, void **val)
+{
+ struct variable *vp = varfind (name);
+
+ if (!vp)
+ return VAR_ERR_NOTDEF;
+
+ if (type != vp->type)
+ return VAR_ERR_BADTYPE;
+
+ switch (vp->type)
+ {
+ case VART_STRING:
+ *val = vp->v.string;
+ break;
+
+ case VART_BOOL:
+ *(int*)val = vp->v.bool;
+ break;
+ }
+
+ return VAR_OK;
+}
+
+void
+variable_print_all (FILE *fp)
+{
+ struct variable *vp;
+ char *s;
+
+ for (vp = vartab; vp->name; vp++)
+ {
+ switch (vp->type)
+ {
+ case VART_BOOL:
+ fprintf (fp, "%s%s", vp->v.bool ? "no" : "", vp->name);
+ break;
+
+ case VART_STRING:
+ fprintf (fp, "%s=\"", vp->name);
+ for (s = vp->v.string; *s; s++)
+ {
+ int c;
+
+ if (isprint (*s))
+ fputc (*s, fp);
+ else if ((c = escape (*s)))
+ fprintf (fp, "\\%c", c);
+ else
+ fprintf (fp, "\\%03o", *s);
+ }
+ fprintf (fp, "\"");
+ }
+ fputc ('\n', fp);
+ }
+}
+

Return to:

Send suggestions and report system problems to the System administrator.