Skip to content

Commit 0f5651a

Browse files
committed
Have boolean pset values checked against typical boolean values, rather
than only 'off'.
1 parent 053948a commit 0f5651a

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/bin/psql/variables.c

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2008, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.28 2008/01/01 19:45:56 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.29 2008/05/07 02:33:52 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -48,21 +48,48 @@ GetVariable(VariableSpace space, const char *name)
4848
return NULL;
4949
}
5050

51+
/*
52+
* Try to interpret value as boolean value. Valid values are: true,
53+
* false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
54+
*/
5155
bool
52-
ParseVariableBool(const char *val)
56+
ParseVariableBool(const char *value)
5357
{
54-
if (val == NULL)
58+
size_t len;
59+
60+
if (value == NULL)
5561
return false; /* not set -> assume "off" */
56-
if (pg_strcasecmp(val, "off") == 0)
57-
return false; /* accept "off" or "OFF" as true */
5862

59-
/*
60-
* for backwards compatibility, anything except "off" or "OFF" is taken as
61-
* "true"
62-
*/
63+
len = strlen(value);
64+
65+
if (pg_strncasecmp(value, "true", len) == 0)
66+
return true;
67+
else if (pg_strncasecmp(value, "false", len) == 0)
68+
return false;
69+
else if (pg_strncasecmp(value, "yes", len) == 0)
70+
return true;
71+
else if (pg_strncasecmp(value, "no", len) == 0)
72+
return false;
73+
/* 'o' is not unique enough */
74+
else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
75+
return true;
76+
else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
77+
return false;
78+
else if (pg_strcasecmp(value, "1") == 0)
79+
return true;
80+
else if (pg_strcasecmp(value, "0") == 0)
81+
return false;
82+
else
83+
{
84+
/* NULL is treated as false, so a non-matching value is 'true' */
85+
psql_error("unrecognized boolean value; assuming \"on\".\n");
86+
return true;
87+
}
88+
/* suppress compiler warning */
6389
return true;
6490
}
6591

92+
6693
/*
6794
* Read numeric variable, or defaultval if it is not set, or faultval if its
6895
* value is not a valid numeric string. If allowtrail is false, this will

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy