From 9b83ca8638cdb8c4deafcc48d8157fe0e8da1740 Mon Sep 17 00:00:00 2001 From: Sergey Poznyakoff Date: Wed, 15 May 2013 16:17:51 +0000 Subject: Add "pager" variable and "unset" command. * src/gdbmtool.c (command_tab) : New command. (run_command): Get pager value from the variable. * src/gdbmtool.h (VAR_ERR_NOTSET): New error code. (variable_is_true): New function. Replaces variable_is_set, which changed semantics. * src/gram.y: Implement the unset command. * src/var.c: Support the "unset variable" notion. (VARF_INIT): New flag. (VAR_IS_SET): New define. (vartab): Mark initialized variables with VARF_INIT. New variable "pager". (open_hook): v can be NULL. (variable_set): NULL value unsets the variable. (variable_unset): New function. (variable_get): Return VAR_ERR_NOTSET if the variable is not set. (variable_is_true): Renamed from variable_is_set. (variable_is_set): New function. * src/gdbmdefs.h: Fix some typos. --- src/var.c | 136 ++++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 97 insertions(+), 39 deletions(-) (limited to 'src/var.c') diff --git a/src/var.c b/src/var.c index a518e94..a080483 100644 --- a/src/var.c +++ b/src/var.c @@ -19,6 +19,9 @@ #define VARF_DFL 0x00 #define VARF_SET 0x01 +#define VARF_INIT 0x02 + +#define VAR_IS_SET(v) ((v)->flags & (VARF_SET|VARF_INIT)) union value { @@ -41,20 +44,21 @@ static int open_hook (struct variable *, union value *); static struct variable vartab[] = { /* Top-level prompt */ - { "ps1", VART_STRING, VARF_DFL, { "%p>%_" } }, + { "ps1", VART_STRING, VARF_INIT, { "%p>%_" } }, /* Second-level prompt (used within "def" block) */ - { "ps2", VART_STRING, VARF_DFL, { "%_>%_" } }, + { "ps2", VART_STRING, VARF_INIT, { "%_>%_" } }, /* This delimits array members */ - { "delim1", VART_STRING, VARF_DFL, { "," } }, + { "delim1", VART_STRING, VARF_INIT, { "," } }, /* This delimits structure members */ - { "delim2", VART_STRING, VARF_DFL, { "," } }, - { "confirm", VART_BOOL, VARF_DFL, { num: 1 } }, - { "cachesize", VART_INT, VARF_DFL, { num: DEFAULT_CACHESIZE } }, + { "delim2", VART_STRING, VARF_INIT, { "," } }, + { "confirm", VART_BOOL, VARF_INIT, { num: 1 } }, + { "cachesize", VART_INT, VARF_INIT, { num: DEFAULT_CACHESIZE } }, { "blocksize", VART_INT, VARF_DFL, { num: 0 } }, { "open", VART_STRING, VARF_DFL, { NULL }, open_hook }, - { "lock", VART_BOOL, VARF_DFL, { num: 1 } }, - { "mmap", VART_BOOL, VARF_DFL, { num: 1 } }, - { "sync", VART_BOOL, VARF_DFL, { num: 0 } }, + { "lock", VART_BOOL, VARF_INIT, { num: 1 } }, + { "mmap", VART_BOOL, VARF_INIT, { num: 1 } }, + { "sync", VART_BOOL, VARF_INIT, { num: 0 } }, + { "pager", VART_STRING, VARF_DFL }, { NULL } }; @@ -74,6 +78,9 @@ open_hook (struct variable *var, union value *v) }; int i; + if (!v) + return VAR_ERR_BADVALUE; + for (i = 0; trans[i].s; i++) if (strcmp (trans[i].s, v->string) == 0) { @@ -204,24 +211,55 @@ variable_set (const char *name, int type, void *val) { struct variable *vp = varfind (name); int rc; - union value v; + union value v, *valp; if (!vp) return VAR_ERR_NOTDEF; - 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) + if (val) + { + memset (&v, 0, sizeof (v)); + rc = setvar[vp->type][type] (&v, val); + if (rc) + return rc; + valp = &v; + } + else + valp = NULL; + + if (vp->hook && (rc = vp->hook (vp, valp)) != VAR_OK) return rc; if (vp->type == VART_STRING && (vp->flags && VARF_SET)) free (vp->v.string); - vp->v = v; - vp->flags |= VARF_SET; + if (!val) + { + vp->flags &= (VARF_INIT|VARF_SET); + } + else + { + vp->v = v; + vp->flags &= ~VARF_INIT; + vp->flags |= VARF_SET; + } + + return VAR_OK; +} + +int +variable_unset (const char *name) +{ + struct variable *vp = varfind (name); + int rc; + + if (!vp) + return VAR_ERR_NOTDEF; + + if (vp->hook && (rc = vp->hook (vp, NULL)) != VAR_OK) + return rc; + + vp->flags &= ~(VARF_INIT|VARF_SET); return VAR_OK; } @@ -236,6 +274,9 @@ variable_get (const char *name, int type, void **val) if (type != vp->type) return VAR_ERR_BADTYPE; + + if (!VAR_IS_SET (vp)) + return VAR_ERR_NOTSET; switch (vp->type) { @@ -263,37 +304,54 @@ variable_print_all (FILE *fp) for (vp = vartab; vp->name; vp++) { - switch (vp->type) + if (!VAR_IS_SET (vp)) + { + fprintf (fp, "# %s is unset", vp->name); + } + else { - case VART_INT: - fprintf (fp, "%s=%d", vp->name, vp->v.num); - break; - - 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++) + switch (vp->type) { - int c; + case VART_INT: + fprintf (fp, "%s=%d", vp->name, vp->v.num); + break; - if (isprint (*s)) - fputc (*s, fp); - else if ((c = escape (*s))) - fprintf (fp, "\\%c", c); - else - fprintf (fp, "\\%03o", *s); + 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, "\""); } - fprintf (fp, "\""); } fputc ('\n', fp); } } - + int variable_is_set (const char *name) +{ + struct variable *vp = varfind (name); + + if (!vp) + return 0; + return VAR_IS_SET (vp); +} + +int +variable_is_true (const char *name) { int n; -- cgit v1.2.1