Branch data Line data Source code
1 : : /*
2 : : * psql - the PostgreSQL interactive terminal
3 : : *
4 : : * Copyright (c) 2000-2026, PostgreSQL Global Development Group
5 : : *
6 : : * src/bin/psql/variables.c
7 : : */
8 : : #include "postgres_fe.h"
9 : :
10 : : #include <math.h>
11 : :
12 : : #include "common.h"
13 : : #include "common/logging.h"
14 : : #include "fe_utils/cancel.h"
15 : : #include "variables.h"
16 : :
17 : : /*
18 : : * Check whether a variable's name is allowed.
19 : : *
20 : : * We allow any non-ASCII character, as well as ASCII letters, digits, and
21 : : * underscore. Keep this in sync with the definition of variable_char in
22 : : * psqlscan.l and psqlscanslash.l.
23 : : */
24 : : static bool
25 : 2305902 : valid_variable_name(const char *name)
26 : : {
27 : 2305902 : const unsigned char *ptr = (const unsigned char *) name;
28 : :
29 : : /* Mustn't be zero-length */
30 [ - + ]: 2305902 : if (*ptr == '\0')
31 : 0 : return false;
32 : :
33 [ + + ]: 32451704 : while (*ptr)
34 : : {
35 [ + - ]: 30145810 : if (IS_HIGHBIT_SET(*ptr) ||
36 : 30145810 : strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
37 [ + + ]: 30145810 : "_0123456789", *ptr) != NULL)
38 : 30145802 : ptr++;
39 : : else
40 : 8 : return false;
41 : : }
42 : :
43 : 2305894 : return true;
44 : : }
45 : :
46 : : /*
47 : : * A "variable space" is represented by an otherwise-unused struct _variable
48 : : * that serves as list header.
49 : : *
50 : : * The list entries are kept in name order (according to strcmp). This
51 : : * is mainly to make the output of PrintVariables() more pleasing.
52 : : */
53 : : VariableSpace
54 : 11633 : CreateVariableSpace(void)
55 : : {
56 : : struct _variable *ptr;
57 : :
58 : 11633 : ptr = pg_malloc_object(struct _variable);
59 : 11633 : ptr->name = NULL;
60 : 11633 : ptr->value = NULL;
61 : 11633 : ptr->substitute_hook = NULL;
62 : 11633 : ptr->assign_hook = NULL;
63 : 11633 : ptr->next = NULL;
64 : :
65 : 11633 : return ptr;
66 : : }
67 : :
68 : : /*
69 : : * Get string value of variable, or NULL if it's not defined.
70 : : *
71 : : * Note: result is valid until variable is next assigned to.
72 : : */
73 : : const char *
74 : 2938 : GetVariable(VariableSpace space, const char *name)
75 : : {
76 : : struct _variable *current;
77 : :
78 [ - + ]: 2938 : if (!space)
79 : 0 : return NULL;
80 : :
81 [ + + ]: 114841 : for (current = space->next; current; current = current->next)
82 : : {
83 : 114840 : int cmp = strcmp(current->name, name);
84 : :
85 [ + + ]: 114840 : if (cmp == 0)
86 : : {
87 : : /* this is correct answer when value is NULL, too */
88 : 2605 : return current->value;
89 : : }
90 [ + + ]: 112235 : if (cmp > 0)
91 : 332 : break; /* it's not there */
92 : : }
93 : :
94 : 333 : return NULL;
95 : : }
96 : :
97 : : /*
98 : : * Try to interpret "value" as a boolean value, and if successful,
99 : : * store it in *result. Otherwise don't clobber *result.
100 : : *
101 : : * Valid values are: true, false, yes, no, on, off, 1, 0; as well as unique
102 : : * prefixes thereof.
103 : : *
104 : : * "name" is the name of the variable we're assigning to, to use in error
105 : : * report if any. Pass name == NULL to suppress the error report.
106 : : *
107 : : * Return true when "value" is syntactically valid, false otherwise.
108 : : */
109 : : bool
110 : 161269 : ParseVariableBool(const char *value, const char *name, bool *result)
111 : : {
112 : : size_t len;
113 : 161269 : bool valid = true;
114 : :
115 : : /* Treat "unset" as an empty string, which will lead to error below */
116 [ - + ]: 161269 : if (value == NULL)
117 : 0 : value = "";
118 : :
119 : 161269 : len = strlen(value);
120 : :
121 [ + - + + ]: 161269 : if (len > 0 && pg_strncasecmp(value, "true", len) == 0)
122 : 155 : *result = true;
123 [ + - + + ]: 161114 : else if (len > 0 && pg_strncasecmp(value, "false", len) == 0)
124 : 201 : *result = false;
125 [ + - + + ]: 160913 : else if (len > 0 && pg_strncasecmp(value, "yes", len) == 0)
126 : 4 : *result = true;
127 [ + - + + ]: 160909 : else if (len > 0 && pg_strncasecmp(value, "no", len) == 0)
128 : 4 : *result = false;
129 : : /* 'o' is not unique enough */
130 [ + + ]: 160905 : else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
131 : 36236 : *result = true;
132 [ + + ]: 124669 : else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
133 : 116467 : *result = false;
134 [ + + ]: 8202 : else if (pg_strcasecmp(value, "1") == 0)
135 : 8185 : *result = true;
136 [ + + ]: 17 : else if (pg_strcasecmp(value, "0") == 0)
137 : 5 : *result = false;
138 : : else
139 : : {
140 : : /* string is not recognized; don't clobber *result */
141 [ + + ]: 12 : if (name)
142 : 8 : pg_log_error("unrecognized value \"%s\" for \"%s\": Boolean expected",
143 : : value, name);
144 : 12 : valid = false;
145 : : }
146 : 161269 : return valid;
147 : : }
148 : :
149 : : /*
150 : : * Try to interpret "value" as an integer value, and if successful,
151 : : * store it in *result. Otherwise don't clobber *result.
152 : : *
153 : : * "name" is the name of the variable we're assigning to, to use in error
154 : : * report if any. Pass name == NULL to suppress the error report.
155 : : *
156 : : * Return true when "value" is syntactically valid, false otherwise.
157 : : */
158 : : bool
159 : 34952 : ParseVariableNum(const char *value, const char *name, int *result)
160 : : {
161 : : char *end;
162 : : long numval;
163 : :
164 : : /* Treat "unset" as an empty string, which will lead to error below */
165 [ - + ]: 34952 : if (value == NULL)
166 : 0 : value = "";
167 : :
168 : 34952 : errno = 0;
169 : 34952 : numval = strtol(value, &end, 0);
170 [ + - + + : 34952 : if (errno == 0 && *end == '\0' && end != value && numval == (int) numval)
+ - + - ]
171 : : {
172 : 34948 : *result = (int) numval;
173 : 34948 : return true;
174 : : }
175 : : else
176 : : {
177 : : /* string is not recognized; don't clobber *result */
178 [ + - ]: 4 : if (name)
179 : 4 : pg_log_error("invalid value \"%s\" for \"%s\": integer expected",
180 : : value, name);
181 : 4 : return false;
182 : : }
183 : : }
184 : :
185 : : /*
186 : : * Try to interpret "value" as a double value, and if successful store it in
187 : : * *result. If unsuccessful, *result isn't clobbered. "name" is the variable
188 : : * which is being assigned, the value of which is only used to produce a good
189 : : * error message. Pass NULL as the name to suppress the error message. The
190 : : * value must be within the range [min,max] in order to be considered valid.
191 : : *
192 : : * Returns true, with *result containing the interpreted value, if "value" is
193 : : * syntactically valid, else false (with *result unchanged).
194 : : */
195 : : bool
196 : 11637 : ParseVariableDouble(const char *value, const char *name, double *result, double min, double max)
197 : : {
198 : : char *end;
199 : : double dblval;
200 : :
201 : : /*
202 : : * Empty-string input has historically been treated differently by strtod
203 : : * on various platforms, so handle that by specifically checking for it.
204 : : */
205 [ + - - + ]: 11637 : if ((value == NULL) || (*value == '\0'))
206 : : {
207 [ # # ]: 0 : if (name)
208 : 0 : pg_log_error("invalid input syntax for variable \"%s\"", name);
209 : 0 : return false;
210 : : }
211 : :
212 : 11637 : errno = 0;
213 : 11637 : dblval = strtod(value, &end);
214 [ + + + - : 11637 : if (errno == 0 && *end == '\0' && end != value)
+ - ]
215 : : {
216 [ - + ]: 11636 : if (dblval < min)
217 : : {
218 [ # # ]: 0 : if (name)
219 : 0 : pg_log_error("invalid value \"%s\" for variable \"%s\": must be greater than %.2f",
220 : : value, name, min);
221 : 0 : return false;
222 : : }
223 [ - + ]: 11636 : else if (dblval > max)
224 : : {
225 [ # # ]: 0 : if (name)
226 : 0 : pg_log_error("invalid value \"%s\" for variable \"%s\": must be less than %.2f",
227 : : value, name, max);
228 : 0 : return false;
229 : : }
230 : 11636 : *result = dblval;
231 : 11636 : return true;
232 : : }
233 : :
234 : : /*
235 : : * Cater for platforms which treat values which aren't zero, but that are
236 : : * too close to zero to have full precision, by checking for zero or real
237 : : * out-of-range values.
238 : : */
239 [ + - + - ]: 1 : else if ((errno == ERANGE) &&
240 [ - + - - ]: 1 : (dblval == 0.0 || dblval >= HUGE_VAL || dblval <= -HUGE_VAL))
241 : : {
242 [ + - ]: 1 : if (name)
243 : 1 : pg_log_error("value \"%s\" is out of range for variable \"%s\"", value, name);
244 : 1 : return false;
245 : : }
246 : : else
247 : : {
248 [ # # ]: 0 : if (name)
249 : 0 : pg_log_error("invalid value \"%s\" for variable \"%s\"", value, name);
250 : 0 : return false;
251 : : }
252 : : }
253 : :
254 : : /*
255 : : * Print values of all variables.
256 : : */
257 : : void
258 : 0 : PrintVariables(VariableSpace space)
259 : : {
260 : : struct _variable *ptr;
261 : :
262 [ # # ]: 0 : if (!space)
263 : 0 : return;
264 : :
265 [ # # ]: 0 : for (ptr = space->next; ptr; ptr = ptr->next)
266 : : {
267 [ # # ]: 0 : if (ptr->value)
268 : 0 : printf("%s = '%s'\n", ptr->name, ptr->value);
269 [ # # ]: 0 : if (CancelRequested)
270 : 0 : break;
271 : : }
272 : : }
273 : :
274 : : /*
275 : : * Set the variable named "name" to value "value",
276 : : * or delete it if "value" is NULL.
277 : : *
278 : : * Returns true if successful, false if not; in the latter case a suitable
279 : : * error message has been printed, except for the unexpected case of
280 : : * space or name being NULL.
281 : : */
282 : : bool
283 : 2038343 : SetVariable(VariableSpace space, const char *name, const char *value)
284 : : {
285 : : struct _variable *current,
286 : : *previous;
287 : :
288 [ + - - + ]: 2038343 : if (!space || !name)
289 : 0 : return false;
290 : :
291 [ + + ]: 2038343 : if (!valid_variable_name(name))
292 : : {
293 : : /* Deletion of non-existent variable is not an error */
294 [ - + ]: 8 : if (!value)
295 : 0 : return true;
296 : 8 : pg_log_error("invalid variable name: \"%s\"", name);
297 : 8 : return false;
298 : : }
299 : :
300 : 2038335 : for (previous = space, current = space->next;
301 [ + + ]: 44071768 : current;
302 : 42033433 : previous = current, current = current->next)
303 : : {
304 : 44071258 : int cmp = strcmp(current->name, name);
305 : :
306 [ + + ]: 44071258 : if (cmp == 0)
307 : : {
308 : : /*
309 : : * Found entry, so update, unless assign hook returns false.
310 : : *
311 : : * We must duplicate the passed value to start with. This
312 : : * simplifies the API for substitute hooks. Moreover, some assign
313 : : * hooks assume that the passed value has the same lifespan as the
314 : : * variable. Having to free the string again on failure is a
315 : : * small price to pay for keeping these APIs simple.
316 : : */
317 [ + + ]: 1813502 : char *new_value = value ? pg_strdup(value) : NULL;
318 : : bool confirmed;
319 : :
320 [ + + ]: 1813502 : if (current->substitute_hook)
321 : 46008 : new_value = current->substitute_hook(new_value);
322 : :
323 [ + + ]: 1813502 : if (current->assign_hook)
324 : 80907 : confirmed = current->assign_hook(new_value);
325 : : else
326 : 1732595 : confirmed = true;
327 : :
328 [ + + ]: 1813502 : if (confirmed)
329 : : {
330 : 1813489 : pg_free(current->value);
331 : 1813489 : current->value = new_value;
332 : :
333 : : /*
334 : : * If we deleted the value, and there are no hooks to
335 : : * remember, we can discard the variable altogether.
336 : : */
337 [ + + ]: 1813489 : if (new_value == NULL &&
338 [ + - ]: 4 : current->substitute_hook == NULL &&
339 [ + - ]: 4 : current->assign_hook == NULL)
340 : : {
341 : 4 : previous->next = current->next;
342 : 4 : free(current->name);
343 : 4 : free(current);
344 : : }
345 : : }
346 : : else
347 : 13 : pg_free(new_value); /* current->value is left unchanged */
348 : :
349 : 1813502 : return confirmed;
350 : : }
351 [ + + ]: 42257756 : if (cmp > 0)
352 : 224323 : break; /* it's not there */
353 : : }
354 : :
355 : : /* not present, make new entry ... unless we were asked to delete */
356 [ + + ]: 224833 : if (value)
357 : : {
358 : 213172 : current = pg_malloc_object(struct _variable);
359 : 213172 : current->name = pg_strdup(name);
360 : 213172 : current->value = pg_strdup(value);
361 : 213172 : current->substitute_hook = NULL;
362 : 213172 : current->assign_hook = NULL;
363 : 213172 : current->next = previous->next;
364 : 213172 : previous->next = current;
365 : : }
366 : 224833 : return true;
367 : : }
368 : :
369 : : /*
370 : : * Attach substitute and/or assign hook functions to the named variable.
371 : : * If you need only one hook, pass NULL for the other.
372 : : *
373 : : * If the variable doesn't already exist, create it with value NULL, just so
374 : : * we have a place to store the hook function(s). (The substitute hook might
375 : : * immediately change the NULL to something else; if not, this state is
376 : : * externally the same as the variable not being defined.)
377 : : *
378 : : * The substitute hook, if given, is immediately called on the variable's
379 : : * value. Then the assign hook, if given, is called on the variable's value.
380 : : * This is meant to let it update any derived psql state. If the assign hook
381 : : * doesn't like the current value, it will print a message to that effect,
382 : : * but we'll ignore it. Generally we do not expect any such failure here,
383 : : * because this should get called before any user-supplied value is assigned.
384 : : */
385 : : void
386 : 267559 : SetVariableHooks(VariableSpace space, const char *name,
387 : : VariableSubstituteHook shook,
388 : : VariableAssignHook ahook)
389 : : {
390 : : struct _variable *current,
391 : : *previous;
392 : :
393 [ + - - + ]: 267559 : if (!space || !name)
394 : 0 : return;
395 : :
396 [ - + ]: 267559 : if (!valid_variable_name(name))
397 : 0 : return;
398 : :
399 : 267559 : for (previous = space, current = space->next;
400 [ + + ]: 2035775 : current;
401 : 1768216 : previous = current, current = current->next)
402 : : {
403 : 1954344 : int cmp = strcmp(current->name, name);
404 : :
405 [ - + ]: 1954344 : if (cmp == 0)
406 : : {
407 : : /* found entry, so update */
408 : 0 : current->substitute_hook = shook;
409 : 0 : current->assign_hook = ahook;
410 [ # # ]: 0 : if (shook)
411 : 0 : current->value = (*shook) (current->value);
412 [ # # ]: 0 : if (ahook)
413 : 0 : (void) (*ahook) (current->value);
414 : 0 : return;
415 : : }
416 [ + + ]: 1954344 : if (cmp > 0)
417 : 186128 : break; /* it's not there */
418 : : }
419 : :
420 : : /* not present, make new entry */
421 : 267559 : current = pg_malloc_object(struct _variable);
422 : 267559 : current->name = pg_strdup(name);
423 : 267559 : current->value = NULL;
424 : 267559 : current->substitute_hook = shook;
425 : 267559 : current->assign_hook = ahook;
426 : 267559 : current->next = previous->next;
427 : 267559 : previous->next = current;
428 [ + + ]: 267559 : if (shook)
429 : 221027 : current->value = (*shook) (current->value);
430 [ + - ]: 267559 : if (ahook)
431 : 267559 : (void) (*ahook) (current->value);
432 : : }
433 : :
434 : : /*
435 : : * Return true iff the named variable has substitute and/or assign hook
436 : : * functions.
437 : : */
438 : : bool
439 : 712 : VariableHasHook(VariableSpace space, const char *name)
440 : : {
441 : : struct _variable *current;
442 : :
443 : : Assert(space);
444 : : Assert(name);
445 : :
446 [ + + ]: 35222 : for (current = space->next; current; current = current->next)
447 : : {
448 : 34973 : int cmp = strcmp(current->name, name);
449 : :
450 [ + + ]: 34973 : if (cmp == 0)
451 [ + + ]: 200 : return (current->substitute_hook != NULL ||
452 [ - + ]: 98 : current->assign_hook != NULL);
453 [ + + ]: 34871 : if (cmp > 0)
454 : 361 : break; /* it's not there */
455 : : }
456 : :
457 : 610 : return false;
458 : : }
459 : :
460 : : /*
461 : : * Convenience function to set a variable's value to "on".
462 : : */
463 : : bool
464 : 33448 : SetVariableBool(VariableSpace space, const char *name)
465 : : {
466 : 33448 : return SetVariable(space, name, "on");
467 : : }
468 : :
469 : : /*
470 : : * Attempt to delete variable.
471 : : *
472 : : * If unsuccessful, print a message and return "false".
473 : : * Deleting a nonexistent variable is not an error.
474 : : */
475 : : bool
476 : 0 : DeleteVariable(VariableSpace space, const char *name)
477 : : {
478 : 0 : return SetVariable(space, name, NULL);
479 : : }
480 : :
481 : : /*
482 : : * Emit error with suggestions for variables or commands
483 : : * accepting enum-style arguments.
484 : : * This function just exists to standardize the wording.
485 : : * suggestions should follow the format "fee, fi, fo, fum".
486 : : */
487 : : void
488 : 4 : PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
489 : : {
490 : 4 : pg_log_error("unrecognized value \"%s\" for \"%s\"\n"
491 : : "Available values are: %s.",
492 : : value, name, suggestions);
493 : 4 : }
|