]> granicus.if.org Git - postgresql/blob - src/bin/psql/variables.c
I'm continuing to work on cleaning up code in psql. As things appear
[postgresql] / src / bin / psql / variables.c
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright 2000 by PostgreSQL Global Development Group
5  *
6  * $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.10 2003/03/20 06:43:35 momjian Exp $
7  */
8 #include "postgres_fe.h"
9 #include "variables.h"
10
11 #include <assert.h>
12
13
14 VariableSpace
15 CreateVariableSpace(void)
16 {
17         struct _variable *ptr;
18
19         ptr = calloc(1, sizeof *ptr);
20         if (!ptr)
21                 return NULL;
22
23         ptr->name = strdup("@");
24         ptr->value = strdup("");
25         if (!ptr->name || !ptr->value)
26         {
27                 free(ptr->name);
28                 free(ptr->value);
29                 free(ptr);
30                 return NULL;
31         }
32
33         return ptr;
34 }
35
36
37
38 const char *
39 GetVariable(VariableSpace space, const char *name)
40 {
41         struct _variable *current;
42
43         if (!space)
44                 return NULL;
45
46         if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
47                 return NULL;
48
49         for (current = space; current; current = current->next)
50         {
51 #ifdef USE_ASSERT_CHECKING
52                 assert(current->name);
53                 assert(current->value);
54 #endif
55                 if (strcmp(current->name, name) == 0)
56                         return current->value;
57         }
58
59         return NULL;
60 }
61
62
63
64 bool
65 GetVariableBool(VariableSpace space, const char *name)
66 {
67         return GetVariable(space, name) != NULL ? true : false;
68 }
69
70
71 bool
72 VariableEquals(VariableSpace space, const char name[], const char value[])
73 {
74         const char *var;
75         var = GetVariable(space, name);
76         return var && (strcmp(var, value) == 0);
77 }
78
79
80 int 
81 GetVariableNum(VariableSpace space, 
82                                         const char name[], 
83                                         int defaultval,
84                                         int faultval,
85                                         bool allowtrail)
86 {
87         const char *var;
88         int result;
89
90         var = GetVariable(space, name);
91         if (!var)
92           result = defaultval;
93         else if (!var[0])
94           result = faultval;
95         else
96         {
97                 char *end;
98                 result = strtol(var, &end, 0);
99                 if (!allowtrail && *end)
100                   result = faultval;
101         }
102
103         return result;
104 }
105
106
107 int
108 SwitchVariable(VariableSpace space, const char name[], const char *opt, ...)
109 {
110         int result;
111         const char *var;
112
113         var = GetVariable(space, name);
114         if (var) 
115         {
116                 va_list args;
117                 va_start(args, opt);
118                 for (result=1; opt && (strcmp(var, opt) != 0); result++)
119                         opt = va_arg(args,const char *);
120
121                 if (!opt) result = var_notfound;
122                 va_end(args);
123         }
124         else
125           result = var_notset;
126
127         return result;
128 }
129
130
131 void 
132 PrintVariables(VariableSpace space)
133 {
134   struct _variable *ptr;
135   for (ptr = space->next; ptr; ptr = ptr->next)
136          printf("%s = '%s'\n", ptr->name, ptr->value);
137 }
138
139
140 bool
141 SetVariable(VariableSpace space, const char *name, const char *value)
142 {
143         struct _variable *current,
144                            *previous;
145
146         if (!space)
147                 return false;
148
149         if (!value)
150                 return DeleteVariable(space, name);
151
152         if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
153                 return false;
154
155         for (current = space, previous = NULL; current; previous = current, current = current->next)
156         {
157 #ifdef USE_ASSERT_CHECKING
158                 assert(current->name);
159                 assert(current->value);
160 #endif
161                 if (strcmp(current->name, name) == 0)
162                 {
163                         free(current->value);
164                         current->value = strdup(value);
165                         return current->value ? true : false;
166                 }
167         }
168
169         previous->next = calloc(1, sizeof *(previous->next));
170         if (!previous->next)
171                 return false;
172         previous->next->name = strdup(name);
173         if (!previous->next->name)
174                 return false;
175         previous->next->value = strdup(value);
176         return previous->next->value ? true : false;
177 }
178
179
180
181 bool
182 SetVariableBool(VariableSpace space, const char *name)
183 {
184         return SetVariable(space, name, "");
185 }
186
187
188
189 bool
190 DeleteVariable(VariableSpace space, const char *name)
191 {
192         struct _variable *current,
193                            *previous;
194
195         if (!space)
196                 return false;
197
198         if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
199                 return false;
200
201         for (current = space, previous = NULL; current; previous = current, current = current->next)
202         {
203 #ifdef USE_ASSERT_CHECKING
204                 assert(current->name);
205                 assert(current->value);
206 #endif
207                 if (strcmp(current->name, name) == 0)
208                 {
209                         free(current->name);
210                         free(current->value);
211                         if (previous)
212                                 previous->next = current->next;
213                         free(current);
214                         return true;
215                 }
216         }
217
218         return true;
219 }
220
221
222
223 void
224 DestroyVariableSpace(VariableSpace space)
225 {
226         if (!space)
227                 return;
228
229         DestroyVariableSpace(space->next);
230         free(space);
231 }