diff options
author | Bruce Momjian | 2003-03-20 06:43:35 +0000 |
---|---|---|
committer | Bruce Momjian | 2003-03-20 06:43:35 +0000 |
commit | add932ee91b9ca78c1a0647487cd6d2a680c4f12 (patch) | |
tree | abb208bec97099a408ead6d6c5b0f0929c459a2d /src/bin/psql/variables.c | |
parent | 1b3d4cefe8e025a40e3a795f311d4711178366e9 (diff) |
I'm continuing to work on cleaning up code in psql. As things appear
now, my changes seem to work. Some possible minor bugs got squished
on the way but I can't be sure without more feedback from people who
really put the code to the test.
The new patch mostly simplifies variable handling and reduces code
duplication. Changes in the command parser eliminate some redundant
variables (boolean state + depth counter), replaces some
"else if" constructs with switches, and so on. It is meant to be
applied together with my previous patch, although I hope they don't
conflict; I went back to the CVS version for this one.
One more thing I thought should perhaps be changed: an IGNOREEOF
value of n will ignore only n-1 EOFs. I didn't want to touch this
for fear of breaking existing applications, but it does seem a tad
illogical.
Jeroen T. Vermeulen
Diffstat (limited to 'src/bin/psql/variables.c')
-rw-r--r-- | src/bin/psql/variables.c | 70 |
1 files changed, 69 insertions, 1 deletions
diff --git a/src/bin/psql/variables.c b/src/bin/psql/variables.c index b65ce38bd68..35f84aefdcc 100644 --- a/src/bin/psql/variables.c +++ b/src/bin/psql/variables.c @@ -3,7 +3,7 @@ * * Copyright 2000 by PostgreSQL Global Development Group * - * $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.9 2001/02/10 02:31:28 tgl Exp $ + * $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.10 2003/03/20 06:43:35 momjian Exp $ */ #include "postgres_fe.h" #include "variables.h" @@ -68,6 +68,74 @@ GetVariableBool(VariableSpace space, const char *name) } +bool +VariableEquals(VariableSpace space, const char name[], const char value[]) +{ + const char *var; + var = GetVariable(space, name); + return var && (strcmp(var, value) == 0); +} + + +int +GetVariableNum(VariableSpace space, + const char name[], + int defaultval, + int faultval, + bool allowtrail) +{ + const char *var; + int result; + + var = GetVariable(space, name); + if (!var) + result = defaultval; + else if (!var[0]) + result = faultval; + else + { + char *end; + result = strtol(var, &end, 0); + if (!allowtrail && *end) + result = faultval; + } + + return result; +} + + +int +SwitchVariable(VariableSpace space, const char name[], const char *opt, ...) +{ + int result; + const char *var; + + var = GetVariable(space, name); + if (var) + { + va_list args; + va_start(args, opt); + for (result=1; opt && (strcmp(var, opt) != 0); result++) + opt = va_arg(args,const char *); + + if (!opt) result = var_notfound; + va_end(args); + } + else + result = var_notset; + + return result; +} + + +void +PrintVariables(VariableSpace space) +{ + struct _variable *ptr; + for (ptr = space->next; ptr; ptr = ptr->next) + printf("%s = '%s'\n", ptr->name, ptr->value); +} + bool SetVariable(VariableSpace space, const char *name, const char *value) |