2 * psql - the PostgreSQL interactive terminal
4 * Copyright 2000 by PostgreSQL Global Development Group
6 * $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.10 2003/03/20 06:43:35 momjian Exp $
8 #include "postgres_fe.h"
15 CreateVariableSpace(void)
17 struct _variable *ptr;
19 ptr = calloc(1, sizeof *ptr);
23 ptr->name = strdup("@");
24 ptr->value = strdup("");
25 if (!ptr->name || !ptr->value)
39 GetVariable(VariableSpace space, const char *name)
41 struct _variable *current;
46 if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
49 for (current = space; current; current = current->next)
51 #ifdef USE_ASSERT_CHECKING
52 assert(current->name);
53 assert(current->value);
55 if (strcmp(current->name, name) == 0)
56 return current->value;
65 GetVariableBool(VariableSpace space, const char *name)
67 return GetVariable(space, name) != NULL ? true : false;
72 VariableEquals(VariableSpace space, const char name[], const char value[])
75 var = GetVariable(space, name);
76 return var && (strcmp(var, value) == 0);
81 GetVariableNum(VariableSpace space,
90 var = GetVariable(space, name);
98 result = strtol(var, &end, 0);
99 if (!allowtrail && *end)
108 SwitchVariable(VariableSpace space, const char name[], const char *opt, ...)
113 var = GetVariable(space, name);
118 for (result=1; opt && (strcmp(var, opt) != 0); result++)
119 opt = va_arg(args,const char *);
121 if (!opt) result = var_notfound;
132 PrintVariables(VariableSpace space)
134 struct _variable *ptr;
135 for (ptr = space->next; ptr; ptr = ptr->next)
136 printf("%s = '%s'\n", ptr->name, ptr->value);
141 SetVariable(VariableSpace space, const char *name, const char *value)
143 struct _variable *current,
150 return DeleteVariable(space, name);
152 if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
155 for (current = space, previous = NULL; current; previous = current, current = current->next)
157 #ifdef USE_ASSERT_CHECKING
158 assert(current->name);
159 assert(current->value);
161 if (strcmp(current->name, name) == 0)
163 free(current->value);
164 current->value = strdup(value);
165 return current->value ? true : false;
169 previous->next = calloc(1, sizeof *(previous->next));
172 previous->next->name = strdup(name);
173 if (!previous->next->name)
175 previous->next->value = strdup(value);
176 return previous->next->value ? true : false;
182 SetVariableBool(VariableSpace space, const char *name)
184 return SetVariable(space, name, "");
190 DeleteVariable(VariableSpace space, const char *name)
192 struct _variable *current,
198 if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
201 for (current = space, previous = NULL; current; previous = current, current = current->next)
203 #ifdef USE_ASSERT_CHECKING
204 assert(current->name);
205 assert(current->value);
207 if (strcmp(current->name, name) == 0)
210 free(current->value);
212 previous->next = current->next;
224 DestroyVariableSpace(VariableSpace space)
229 DestroyVariableSpace(space->next);