2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2007, PostgreSQL Global Development Group
6 * $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.27 2007/01/05 22:19:50 momjian Exp $
8 #include "postgres_fe.h"
10 #include "variables.h"
14 * A "variable space" is represented by an otherwise-unused struct _variable
15 * that serves as list header.
18 CreateVariableSpace(void)
20 struct _variable *ptr;
22 ptr = pg_malloc(sizeof *ptr);
25 ptr->assign_hook = NULL;
32 GetVariable(VariableSpace space, const char *name)
34 struct _variable *current;
39 for (current = space->next; current; current = current->next)
41 if (strcmp(current->name, name) == 0)
43 /* this is correct answer when value is NULL, too */
44 return current->value;
52 ParseVariableBool(const char *val)
55 return false; /* not set -> assume "off" */
56 if (pg_strcasecmp(val, "off") == 0)
57 return false; /* accept "off" or "OFF" as true */
60 * for backwards compatibility, anything except "off" or "OFF" is taken as
67 * Read numeric variable, or defaultval if it is not set, or faultval if its
68 * value is not a valid numeric string. If allowtrail is false, this will
69 * include the case where there are trailing characters after the number.
72 ParseVariableNum(const char *val,
87 result = strtol(val, &end, 0);
88 if (!allowtrail && *end)
96 GetVariableNum(VariableSpace space,
104 val = GetVariable(space, name);
105 return ParseVariableNum(val, defaultval, faultval, allowtrail);
109 PrintVariables(VariableSpace space)
111 struct _variable *ptr;
116 for (ptr = space->next; ptr; ptr = ptr->next)
119 printf("%s = '%s'\n", ptr->name, ptr->value);
126 SetVariable(VariableSpace space, const char *name, const char *value)
128 struct _variable *current,
134 if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
138 return DeleteVariable(space, name);
140 for (previous = space, current = space->next;
142 previous = current, current = current->next)
144 if (strcmp(current->name, name) == 0)
146 /* found entry, so update */
148 free(current->value);
149 current->value = pg_strdup(value);
150 if (current->assign_hook)
151 (*current->assign_hook) (current->value);
156 /* not present, make new entry */
157 current = pg_malloc(sizeof *current);
158 current->name = pg_strdup(name);
159 current->value = pg_strdup(value);
160 current->assign_hook = NULL;
161 current->next = NULL;
162 previous->next = current;
167 * This both sets a hook function, and calls it on the current value (if any)
170 SetVariableAssignHook(VariableSpace space, const char *name, VariableAssignHook hook)
172 struct _variable *current,
178 if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
181 for (previous = space, current = space->next;
183 previous = current, current = current->next)
185 if (strcmp(current->name, name) == 0)
187 /* found entry, so update */
188 current->assign_hook = hook;
189 (*hook) (current->value);
194 /* not present, make new entry */
195 current = pg_malloc(sizeof *current);
196 current->name = pg_strdup(name);
197 current->value = NULL;
198 current->assign_hook = hook;
199 current->next = NULL;
200 previous->next = current;
206 SetVariableBool(VariableSpace space, const char *name)
208 return SetVariable(space, name, "on");
212 DeleteVariable(VariableSpace space, const char *name)
214 struct _variable *current,
220 for (previous = space, current = space->next;
222 previous = current, current = current->next)
224 if (strcmp(current->name, name) == 0)
227 free(current->value);
228 current->value = NULL;
229 /* Physically delete only if no hook function to remember */
230 if (current->assign_hook)
231 (*current->assign_hook) (NULL);
234 previous->next = current->next;