2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2014, PostgreSQL Global Development Group
6 * src/bin/psql/variables.c
8 #include "postgres_fe.h"
11 #include "variables.h"
15 * Check whether a variable's name is allowed.
17 * We allow any non-ASCII character, as well as ASCII letters, digits, and
18 * underscore. Keep this in sync with the definition of variable_char in
22 valid_variable_name(const char *name)
24 const unsigned char *ptr = (const unsigned char *) name;
26 /* Mustn't be zero-length */
32 if (IS_HIGHBIT_SET(*ptr) ||
33 strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
34 "_0123456789", *ptr) != NULL)
44 * A "variable space" is represented by an otherwise-unused struct _variable
45 * that serves as list header.
48 CreateVariableSpace(void)
50 struct _variable *ptr;
52 ptr = pg_malloc(sizeof *ptr);
55 ptr->assign_hook = NULL;
62 GetVariable(VariableSpace space, const char *name)
64 struct _variable *current;
69 for (current = space->next; current; current = current->next)
71 if (strcmp(current->name, name) == 0)
73 /* this is correct answer when value is NULL, too */
74 return current->value;
82 * Try to interpret value as boolean value. Valid values are: true,
83 * false, yes, no, on, off, 1, 0; as well as unique prefixes thereof.
86 ParseVariableBool(const char *value)
91 return false; /* not set -> assume "off" */
95 if (pg_strncasecmp(value, "true", len) == 0)
97 else if (pg_strncasecmp(value, "false", len) == 0)
99 else if (pg_strncasecmp(value, "yes", len) == 0)
101 else if (pg_strncasecmp(value, "no", len) == 0)
103 /* 'o' is not unique enough */
104 else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
106 else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
108 else if (pg_strcasecmp(value, "1") == 0)
110 else if (pg_strcasecmp(value, "0") == 0)
114 /* NULL is treated as false, so a non-matching value is 'true' */
115 psql_error("unrecognized Boolean value; assuming \"on\"\n");
122 * Read numeric variable, or defaultval if it is not set, or faultval if its
123 * value is not a valid numeric string. If allowtrail is false, this will
124 * include the case where there are trailing characters after the number.
127 ParseVariableNum(const char *val,
142 result = strtol(val, &end, 0);
143 if (!allowtrail && *end)
151 GetVariableNum(VariableSpace space,
159 val = GetVariable(space, name);
160 return ParseVariableNum(val, defaultval, faultval, allowtrail);
164 PrintVariables(VariableSpace space)
166 struct _variable *ptr;
171 for (ptr = space->next; ptr; ptr = ptr->next)
174 printf("%s = '%s'\n", ptr->name, ptr->value);
181 SetVariable(VariableSpace space, const char *name, const char *value)
183 struct _variable *current,
189 if (!valid_variable_name(name))
193 return DeleteVariable(space, name);
195 for (previous = space, current = space->next;
197 previous = current, current = current->next)
199 if (strcmp(current->name, name) == 0)
201 /* found entry, so update */
203 free(current->value);
204 current->value = pg_strdup(value);
205 if (current->assign_hook)
206 (*current->assign_hook) (current->value);
211 /* not present, make new entry */
212 current = pg_malloc(sizeof *current);
213 current->name = pg_strdup(name);
214 current->value = pg_strdup(value);
215 current->assign_hook = NULL;
216 current->next = NULL;
217 previous->next = current;
222 * This both sets a hook function, and calls it on the current value (if any)
225 SetVariableAssignHook(VariableSpace space, const char *name, VariableAssignHook hook)
227 struct _variable *current,
233 if (!valid_variable_name(name))
236 for (previous = space, current = space->next;
238 previous = current, current = current->next)
240 if (strcmp(current->name, name) == 0)
242 /* found entry, so update */
243 current->assign_hook = hook;
244 (*hook) (current->value);
249 /* not present, make new entry */
250 current = pg_malloc(sizeof *current);
251 current->name = pg_strdup(name);
252 current->value = NULL;
253 current->assign_hook = hook;
254 current->next = NULL;
255 previous->next = current;
261 SetVariableBool(VariableSpace space, const char *name)
263 return SetVariable(space, name, "on");
267 DeleteVariable(VariableSpace space, const char *name)
269 struct _variable *current,
275 for (previous = space, current = space->next;
277 previous = current, current = current->next)
279 if (strcmp(current->name, name) == 0)
282 free(current->value);
283 current->value = NULL;
284 /* Physically delete only if no hook function to remember */
285 if (current->assign_hook)
286 (*current->assign_hook) (NULL);
289 previous->next = current->next;