2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2011, 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");
118 /* suppress compiler warning */
124 * Read numeric variable, or defaultval if it is not set, or faultval if its
125 * value is not a valid numeric string. If allowtrail is false, this will
126 * include the case where there are trailing characters after the number.
129 ParseVariableNum(const char *val,
144 result = strtol(val, &end, 0);
145 if (!allowtrail && *end)
153 GetVariableNum(VariableSpace space,
161 val = GetVariable(space, name);
162 return ParseVariableNum(val, defaultval, faultval, allowtrail);
166 PrintVariables(VariableSpace space)
168 struct _variable *ptr;
173 for (ptr = space->next; ptr; ptr = ptr->next)
176 printf("%s = '%s'\n", ptr->name, ptr->value);
183 SetVariable(VariableSpace space, const char *name, const char *value)
185 struct _variable *current,
191 if (!valid_variable_name(name))
195 return DeleteVariable(space, name);
197 for (previous = space, current = space->next;
199 previous = current, current = current->next)
201 if (strcmp(current->name, name) == 0)
203 /* found entry, so update */
205 free(current->value);
206 current->value = pg_strdup(value);
207 if (current->assign_hook)
208 (*current->assign_hook) (current->value);
213 /* not present, make new entry */
214 current = pg_malloc(sizeof *current);
215 current->name = pg_strdup(name);
216 current->value = pg_strdup(value);
217 current->assign_hook = NULL;
218 current->next = NULL;
219 previous->next = current;
224 * This both sets a hook function, and calls it on the current value (if any)
227 SetVariableAssignHook(VariableSpace space, const char *name, VariableAssignHook hook)
229 struct _variable *current,
235 if (!valid_variable_name(name))
238 for (previous = space, current = space->next;
240 previous = current, current = current->next)
242 if (strcmp(current->name, name) == 0)
244 /* found entry, so update */
245 current->assign_hook = hook;
246 (*hook) (current->value);
251 /* not present, make new entry */
252 current = pg_malloc(sizeof *current);
253 current->name = pg_strdup(name);
254 current->value = NULL;
255 current->assign_hook = hook;
256 current->next = NULL;
257 previous->next = current;
263 SetVariableBool(VariableSpace space, const char *name)
265 return SetVariable(space, name, "on");
269 DeleteVariable(VariableSpace space, const char *name)
271 struct _variable *current,
277 for (previous = space, current = space->next;
279 previous = current, current = current->next)
281 if (strcmp(current->name, name) == 0)
284 free(current->value);
285 current->value = NULL;
286 /* Physically delete only if no hook function to remember */
287 if (current->assign_hook)
288 (*current->assign_hook) (NULL);
291 previous->next = current->next;