diff options
author | Bruce Momjian | 2008-05-07 02:33:52 +0000 |
---|---|---|
committer | Bruce Momjian | 2008-05-07 02:33:52 +0000 |
commit | d6984b20503ddf6d6a295fa5619a0976e791d366 (patch) | |
tree | 01b843aaf833d743f9a677776e345ad9859ca164 | |
parent | 2266c90a59259c4b63d60d7ef3a50fdeabeb7581 (diff) |
Have boolean pset values checked against typical boolean values, rather
than only 'off'.
-rw-r--r-- | src/bin/psql/variables.c | 43 |
1 files changed, 35 insertions, 8 deletions
diff --git a/src/bin/psql/variables.c b/src/bin/psql/variables.c index 16fd334d5e..e333a226fd 100644 --- a/src/bin/psql/variables.c +++ b/src/bin/psql/variables.c @@ -48,21 +48,48 @@ GetVariable(VariableSpace space, const char *name) return NULL; } +/* + * Try to interpret value as boolean value. Valid values are: true, + * false, yes, no, on, off, 1, 0; as well as unique prefixes thereof. + */ bool -ParseVariableBool(const char *val) +ParseVariableBool(const char *value) { - if (val == NULL) + size_t len; + + if (value == NULL) return false; /* not set -> assume "off" */ - if (pg_strcasecmp(val, "off") == 0) - return false; /* accept "off" or "OFF" as true */ - /* - * for backwards compatibility, anything except "off" or "OFF" is taken as - * "true" - */ + len = strlen(value); + + if (pg_strncasecmp(value, "true", len) == 0) + return true; + else if (pg_strncasecmp(value, "false", len) == 0) + return false; + else if (pg_strncasecmp(value, "yes", len) == 0) + return true; + else if (pg_strncasecmp(value, "no", len) == 0) + return false; + /* 'o' is not unique enough */ + else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0) + return true; + else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0) + return false; + else if (pg_strcasecmp(value, "1") == 0) + return true; + else if (pg_strcasecmp(value, "0") == 0) + return false; + else + { + /* NULL is treated as false, so a non-matching value is 'true' */ + psql_error("unrecognized boolean value; assuming \"on\".\n"); + return true; + } + /* suppress compiler warning */ return true; } + /* * Read numeric variable, or defaultval if it is not set, or faultval if its * value is not a valid numeric string. If allowtrail is false, this will |