]> granicus.if.org Git - postgresql/blob - src/bin/psql/variables.c
More fallout from the recent psql patch: rename xmalloc and friends to
[postgresql] / src / bin / psql / variables.c
1 /*
2  * psql - the PostgreSQL interactive terminal
3  *
4  * Copyright (c) 2000-2003, PostgreSQL Global Development Group
5  *
6  * $PostgreSQL: pgsql/src/bin/psql/variables.c,v 1.17 2004/01/25 03:07:22 neilc Exp $
7  */
8 #include "postgres_fe.h"
9 #include "common.h"
10 #include "variables.h"
11
12 VariableSpace
13 CreateVariableSpace(void)
14 {
15         struct _variable *ptr;
16
17         ptr = pg_calloc(1, sizeof *ptr);
18         ptr->name = pg_strdup("@");
19         ptr->value = pg_strdup("");
20
21         return ptr;
22 }
23
24 const char *
25 GetVariable(VariableSpace space, const char *name)
26 {
27         struct _variable *current;
28
29         if (!space)
30                 return NULL;
31
32         if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
33                 return NULL;
34
35         for (current = space; current; current = current->next)
36         {
37                 psql_assert(current->name);
38                 psql_assert(current->value);
39                 if (strcmp(current->name, name) == 0)
40                         return current->value;
41         }
42
43         return NULL;
44 }
45
46 bool
47 GetVariableBool(VariableSpace space, const char *name)
48 {
49         const char *val;
50
51         val = GetVariable(space, name);
52         if (val == NULL)
53                 return false;                   /* not set -> assume "off" */
54         if (strcmp(val, "off") == 0)
55                 return false;
56
57         /*
58          * for backwards compatibility, anything except "off" is taken as
59          * "true"
60          */
61         return true;
62 }
63
64 bool
65 VariableEquals(VariableSpace space, const char name[], const char value[])
66 {
67         const char *var;
68
69         var = GetVariable(space, name);
70         return var && (strcmp(var, value) == 0);
71 }
72
73 int
74 GetVariableNum(VariableSpace space,
75                            const char name[],
76                            int defaultval,
77                            int faultval,
78                            bool allowtrail)
79 {
80         const char *var;
81         int                     result;
82
83         var = GetVariable(space, name);
84         if (!var)
85                 result = defaultval;
86         else if (!var[0])
87                 result = faultval;
88         else
89         {
90                 char       *end;
91
92                 result = strtol(var, &end, 0);
93                 if (!allowtrail && *end)
94                         result = faultval;
95         }
96
97         return result;
98 }
99
100 int
101 SwitchVariable(VariableSpace space, const char name[], const char *opt,...)
102 {
103         int                     result;
104         const char *var;
105
106         var = GetVariable(space, name);
107         if (var)
108         {
109                 va_list         args;
110
111                 va_start(args, opt);
112                 for (result = 1; opt && (strcmp(var, opt) != 0); result++)
113                         opt = va_arg(args, const char *);
114                 if (!opt)
115                         result = VAR_NOTFOUND;
116                 va_end(args);
117         }
118         else
119                 result = VAR_NOTSET;
120
121         return result;
122 }
123
124 void
125 PrintVariables(VariableSpace space)
126 {
127         struct _variable *ptr;
128
129         for (ptr = space->next; ptr; ptr = ptr->next)
130                 printf("%s = '%s'\n", ptr->name, ptr->value);
131 }
132
133 bool
134 SetVariable(VariableSpace space, const char *name, const char *value)
135 {
136         struct _variable *current,
137                            *previous;
138
139         if (!space)
140                 return false;
141
142         if (!value)
143                 return DeleteVariable(space, name);
144
145         if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
146                 return false;
147
148         for (current = space, previous = NULL; current; previous = current, current = current->next)
149         {
150                 psql_assert(current->name);
151                 psql_assert(current->value);
152                 if (strcmp(current->name, name) == 0)
153                 {
154                         free(current->value);
155                         current->value = pg_strdup(value);
156                         return true;
157                 }
158         }
159
160         previous->next = pg_calloc(1, sizeof *(previous->next));
161         previous->next->name = pg_strdup(name);
162         previous->next->value = pg_strdup(value);
163         return true;
164 }
165
166 bool
167 SetVariableBool(VariableSpace space, const char *name)
168 {
169         return SetVariable(space, name, "on");
170 }
171
172 bool
173 DeleteVariable(VariableSpace space, const char *name)
174 {
175         struct _variable *current,
176                            *previous;
177
178         if (!space)
179                 return false;
180
181         if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
182                 return false;
183
184         for (current = space, previous = NULL; current; previous = current, current = current->next)
185         {
186                 psql_assert(current->name);
187                 psql_assert(current->value);
188                 if (strcmp(current->name, name) == 0)
189                 {
190                         free(current->name);
191                         free(current->value);
192                         if (previous)
193                                 previous->next = current->next;
194                         free(current);
195                         return true;
196                 }
197         }
198
199         return true;
200 }