PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
variables.c File Reference
#include "postgres_fe.h"
#include "common.h"
#include "common/logging.h"
#include "variables.h"
Include dependency graph for variables.c:

Go to the source code of this file.

Functions

static bool valid_variable_name (const char *name)
 
VariableSpace CreateVariableSpace (void)
 
const char * GetVariable (VariableSpace space, const char *name)
 
bool ParseVariableBool (const char *value, const char *name, bool *result)
 
bool ParseVariableNum (const char *value, const char *name, int *result)
 
void PrintVariables (VariableSpace space)
 
bool SetVariable (VariableSpace space, const char *name, const char *value)
 
void SetVariableHooks (VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
 
bool VariableHasHook (VariableSpace space, const char *name)
 
bool SetVariableBool (VariableSpace space, const char *name)
 
bool DeleteVariable (VariableSpace space, const char *name)
 
void PsqlVarEnumError (const char *name, const char *value, const char *suggestions)
 

Function Documentation

◆ CreateVariableSpace()

VariableSpace CreateVariableSpace ( void  )

Definition at line 51 of file variables.c.

52 {
53  struct _variable *ptr;
54 
55  ptr = pg_malloc(sizeof *ptr);
56  ptr->name = NULL;
57  ptr->value = NULL;
58  ptr->substitute_hook = NULL;
59  ptr->assign_hook = NULL;
60  ptr->next = NULL;
61 
62  return ptr;
63 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
VariableSubstituteHook substitute_hook
Definition: variables.h:66
VariableAssignHook assign_hook
Definition: variables.h:67
struct _variable * next
Definition: variables.h:68
char * name
Definition: variables.h:64
char * value
Definition: variables.h:65

References _variable::assign_hook, _variable::name, _variable::next, pg_malloc(), _variable::substitute_hook, and _variable::value.

Referenced by EstablishVariableSpace().

◆ DeleteVariable()

bool DeleteVariable ( VariableSpace  space,
const char *  name 
)

Definition at line 404 of file variables.c.

405 {
406  return SetVariable(space, name, NULL);
407 }
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
const char * name

References name, and SetVariable().

Referenced by parse_psql_options().

◆ GetVariable()

const char* GetVariable ( VariableSpace  space,
const char *  name 
)

Definition at line 71 of file variables.c.

72 {
73  struct _variable *current;
74 
75  if (!space)
76  return NULL;
77 
78  for (current = space->next; current; current = current->next)
79  {
80  int cmp = strcmp(current->name, name);
81 
82  if (cmp == 0)
83  {
84  /* this is correct answer when value is NULL, too */
85  return current->value;
86  }
87  if (cmp > 0)
88  break; /* it's not there */
89  }
90 
91  return NULL;
92 }
static int cmp(const chr *x, const chr *y, size_t len)
Definition: regc_locale.c:743

References cmp(), name, _variable::name, _variable::next, and _variable::value.

Referenced by get_prompt(), initializeInput(), and psql_get_variable().

◆ ParseVariableBool()

bool ParseVariableBool ( const char *  value,
const char *  name,
bool result 
)

Definition at line 107 of file variables.c.

108 {
109  size_t len;
110  bool valid = true;
111 
112  /* Treat "unset" as an empty string, which will lead to error below */
113  if (value == NULL)
114  value = "";
115 
116  len = strlen(value);
117 
118  if (len > 0 && pg_strncasecmp(value, "true", len) == 0)
119  *result = true;
120  else if (len > 0 && pg_strncasecmp(value, "false", len) == 0)
121  *result = false;
122  else if (len > 0 && pg_strncasecmp(value, "yes", len) == 0)
123  *result = true;
124  else if (len > 0 && pg_strncasecmp(value, "no", len) == 0)
125  *result = false;
126  /* 'o' is not unique enough */
127  else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
128  *result = true;
129  else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
130  *result = false;
131  else if (pg_strcasecmp(value, "1") == 0)
132  *result = true;
133  else if (pg_strcasecmp(value, "0") == 0)
134  *result = false;
135  else
136  {
137  /* string is not recognized; don't clobber *result */
138  if (name)
139  pg_log_error("unrecognized value \"%s\" for \"%s\": Boolean expected",
140  value, name);
141  valid = false;
142  }
143  return valid;
144 }
static struct @160 value
#define pg_log_error(...)
Definition: logging.h:106
const void size_t len
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69

References len, name, pg_log_error, pg_strcasecmp(), pg_strncasecmp(), and value.

Referenced by autocommit_hook(), do_pset(), echo_hidden_hook(), exec_command_connect(), exec_command_timing(), hide_compression_hook(), hide_tableam_hook(), is_true_boolean_expression(), on_error_rollback_hook(), on_error_stop_hook(), quiet_hook(), show_all_results_hook(), singleline_hook(), and singlestep_hook().

◆ ParseVariableNum()

bool ParseVariableNum ( const char *  value,
const char *  name,
int *  result 
)

Definition at line 156 of file variables.c.

157 {
158  char *end;
159  long numval;
160 
161  /* Treat "unset" as an empty string, which will lead to error below */
162  if (value == NULL)
163  value = "";
164 
165  errno = 0;
166  numval = strtol(value, &end, 0);
167  if (errno == 0 && *end == '\0' && end != value && numval == (int) numval)
168  {
169  *result = (int) numval;
170  return true;
171  }
172  else
173  {
174  /* string is not recognized; don't clobber *result */
175  if (name)
176  pg_log_error("invalid value \"%s\" for \"%s\": integer expected",
177  value, name);
178  return false;
179  }
180 }

References name, pg_log_error, and value.

Referenced by do_pset(), fetch_count_hook(), histsize_hook(), ignoreeof_hook(), and ignoreeof_substitute_hook().

◆ PrintVariables()

void PrintVariables ( VariableSpace  space)

Definition at line 186 of file variables.c.

187 {
188  struct _variable *ptr;
189 
190  if (!space)
191  return;
192 
193  for (ptr = space->next; ptr; ptr = ptr->next)
194  {
195  if (ptr->value)
196  printf("%s = '%s'\n", ptr->name, ptr->value);
197  if (cancel_pressed)
198  break;
199  }
200 }
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
#define printf(...)
Definition: port.h:244

References cancel_pressed, _variable::name, _variable::next, printf, and _variable::value.

Referenced by exec_command_set().

◆ PsqlVarEnumError()

void PsqlVarEnumError ( const char *  name,
const char *  value,
const char *  suggestions 
)

Definition at line 416 of file variables.c.

417 {
418  pg_log_error("unrecognized value \"%s\" for \"%s\"\n"
419  "Available values are: %s.",
420  value, name, suggestions);
421 }

References name, pg_log_error, and value.

Referenced by comp_keyword_case_hook(), do_pset(), echo_hidden_hook(), echo_hook(), histcontrol_hook(), on_error_rollback_hook(), show_context_hook(), and verbosity_hook().

◆ SetVariable()

bool SetVariable ( VariableSpace  space,
const char *  name,
const char *  value 
)

Definition at line 211 of file variables.c.

212 {
213  struct _variable *current,
214  *previous;
215 
216  if (!space || !name)
217  return false;
218 
220  {
221  /* Deletion of non-existent variable is not an error */
222  if (!value)
223  return true;
224  pg_log_error("invalid variable name: \"%s\"", name);
225  return false;
226  }
227 
228  for (previous = space, current = space->next;
229  current;
230  previous = current, current = current->next)
231  {
232  int cmp = strcmp(current->name, name);
233 
234  if (cmp == 0)
235  {
236  /*
237  * Found entry, so update, unless assign hook returns false.
238  *
239  * We must duplicate the passed value to start with. This
240  * simplifies the API for substitute hooks. Moreover, some assign
241  * hooks assume that the passed value has the same lifespan as the
242  * variable. Having to free the string again on failure is a
243  * small price to pay for keeping these APIs simple.
244  */
245  char *new_value = value ? pg_strdup(value) : NULL;
246  bool confirmed;
247 
248  if (current->substitute_hook)
249  new_value = current->substitute_hook(new_value);
250 
251  if (current->assign_hook)
252  confirmed = current->assign_hook(new_value);
253  else
254  confirmed = true;
255 
256  if (confirmed)
257  {
258  pg_free(current->value);
259  current->value = new_value;
260 
261  /*
262  * If we deleted the value, and there are no hooks to
263  * remember, we can discard the variable altogether.
264  */
265  if (new_value == NULL &&
266  current->substitute_hook == NULL &&
267  current->assign_hook == NULL)
268  {
269  previous->next = current->next;
270  free(current->name);
271  free(current);
272  }
273  }
274  else
275  pg_free(new_value); /* current->value is left unchanged */
276 
277  return confirmed;
278  }
279  if (cmp > 0)
280  break; /* it's not there */
281  }
282 
283  /* not present, make new entry ... unless we were asked to delete */
284  if (value)
285  {
286  current = pg_malloc(sizeof *current);
287  current->name = pg_strdup(name);
288  current->value = pg_strdup(value);
289  current->substitute_hook = NULL;
290  current->assign_hook = NULL;
291  current->next = previous->next;
292  previous->next = current;
293  }
294  return true;
295 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105
#define free(a)
Definition: header.h:65
static bool valid_variable_name(const char *name)
Definition: variables.c:22

References _variable::assign_hook, cmp(), free, name, _variable::name, _variable::next, pg_free(), pg_log_error, pg_malloc(), pg_strdup(), _variable::substitute_hook, valid_variable_name(), _variable::value, and value.

Referenced by DeleteVariable(), do_lo_import(), exec_command_encoding(), exec_command_getenv(), exec_command_prompt(), exec_command_set(), exec_command_unset(), ExecQueryAndProcessResults(), main(), parse_psql_options(), PrintQueryStatus(), SendQuery(), SetResultVariables(), SetShellResultVariables(), SetVariableBool(), StoreQueryTuple(), SyncVariables(), and UnsyncVariables().

◆ SetVariableBool()

bool SetVariableBool ( VariableSpace  space,
const char *  name 
)

Definition at line 392 of file variables.c.

393 {
394  return SetVariable(space, name, "on");
395 }

References name, and SetVariable().

Referenced by main(), and parse_psql_options().

◆ SetVariableHooks()

void SetVariableHooks ( VariableSpace  space,
const char *  name,
VariableSubstituteHook  shook,
VariableAssignHook  ahook 
)

Definition at line 314 of file variables.c.

317 {
318  struct _variable *current,
319  *previous;
320 
321  if (!space || !name)
322  return;
323 
325  return;
326 
327  for (previous = space, current = space->next;
328  current;
329  previous = current, current = current->next)
330  {
331  int cmp = strcmp(current->name, name);
332 
333  if (cmp == 0)
334  {
335  /* found entry, so update */
336  current->substitute_hook = shook;
337  current->assign_hook = ahook;
338  if (shook)
339  current->value = (*shook) (current->value);
340  if (ahook)
341  (void) (*ahook) (current->value);
342  return;
343  }
344  if (cmp > 0)
345  break; /* it's not there */
346  }
347 
348  /* not present, make new entry */
349  current = pg_malloc(sizeof *current);
350  current->name = pg_strdup(name);
351  current->value = NULL;
352  current->substitute_hook = shook;
353  current->assign_hook = ahook;
354  current->next = previous->next;
355  previous->next = current;
356  if (shook)
357  current->value = (*shook) (current->value);
358  if (ahook)
359  (void) (*ahook) (current->value);
360 }

References _variable::assign_hook, cmp(), name, _variable::name, _variable::next, pg_malloc(), pg_strdup(), _variable::substitute_hook, valid_variable_name(), and _variable::value.

Referenced by EstablishVariableSpace().

◆ valid_variable_name()

static bool valid_variable_name ( const char *  name)
static

Definition at line 22 of file variables.c.

23 {
24  const unsigned char *ptr = (const unsigned char *) name;
25 
26  /* Mustn't be zero-length */
27  if (*ptr == '\0')
28  return false;
29 
30  while (*ptr)
31  {
32  if (IS_HIGHBIT_SET(*ptr) ||
33  strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
34  "_0123456789", *ptr) != NULL)
35  ptr++;
36  else
37  return false;
38  }
39 
40  return true;
41 }
#define IS_HIGHBIT_SET(ch)
Definition: c.h:1160

References IS_HIGHBIT_SET, and name.

Referenced by SetVariable(), and SetVariableHooks().

◆ VariableHasHook()

bool VariableHasHook ( VariableSpace  space,
const char *  name 
)

Definition at line 367 of file variables.c.

368 {
369  struct _variable *current;
370 
371  Assert(space);
372  Assert(name);
373 
374  for (current = space->next; current; current = current->next)
375  {
376  int cmp = strcmp(current->name, name);
377 
378  if (cmp == 0)
379  return (current->substitute_hook != NULL ||
380  current->assign_hook != NULL);
381  if (cmp > 0)
382  break; /* it's not there */
383  }
384 
385  return false;
386 }
#define Assert(condition)
Definition: c.h:863

References Assert, _variable::assign_hook, cmp(), name, _variable::name, _variable::next, and _variable::substitute_hook.

Referenced by StoreQueryTuple().