You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
postgres/src/bin/psql/variables.c

157 lines
2.7 KiB

/*
* psql - the PostgreSQL interactive terminal
*
* Copyright 2000 by PostgreSQL Global Development Team
*
* $Header: /cvsroot/pgsql/src/bin/psql/variables.c,v 1.4 2000/01/18 23:30:24 petere Exp $
*/
#include <c.h>
#include "variables.h"
#include <stdlib.h>
#include <assert.h>
26 years ago
VariableSpace
CreateVariableSpace(void)
{
26 years ago
struct _variable *ptr;
ptr = calloc(1, sizeof *ptr);
if (!ptr)
return NULL;
ptr->name = strdup("@");
ptr->value = strdup("");
if (!ptr->name || !ptr->value)
{
free(ptr->name);
free(ptr->value);
free(ptr);
return NULL;
}
26 years ago
return ptr;
}
26 years ago
const char *
GetVariable(VariableSpace space, const char *name)
{
26 years ago
struct _variable *current;
26 years ago
if (!space)
return NULL;
26 years ago
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return NULL;
26 years ago
for (current = space; current; current = current->next)
{
#ifdef USE_ASSERT_CHECKING
26 years ago
assert(current->name);
assert(current->value);
#endif
26 years ago
if (strcmp(current->name, name) == 0)
return current->value;
}
26 years ago
return NULL;
}
26 years ago
bool
GetVariableBool(VariableSpace space, const char *name)
{
26 years ago
return GetVariable(space, name) != NULL ? true : false;
}
26 years ago
bool
SetVariable(VariableSpace space, const char *name, const char *value)
{
26 years ago
struct _variable *current,
*previous;
26 years ago
if (!space)
return false;
26 years ago
if (!value)
return DeleteVariable(space, name);
26 years ago
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return false;
for (current = space, previous = NULL; current; previous = current, current = current->next)
26 years ago
{
#ifdef USE_ASSERT_CHECKING
26 years ago
assert(current->name);
assert(current->value);
#endif
26 years ago
if (strcmp(current->name, name) == 0)
{
free(current->value);
current->value = strdup(value);
return current->value ? true : false;
}
}
26 years ago
previous->next = calloc(1, sizeof *(previous->next));
if (!previous->next)
return false;
previous->next->name = strdup(name);
if (!previous->next->name)
return false;
previous->next->value = strdup(value);
return previous->next->value ? true : false;
}
26 years ago
bool
DeleteVariable(VariableSpace space, const char *name)
{
26 years ago
struct _variable *current,
*previous;
26 years ago
if (!space)
return false;
26 years ago
if (strspn(name, VALID_VARIABLE_CHARS) != strlen(name))
return false;
26 years ago
for (current = space, previous = NULL; current; previous = current, current = current->next)
{
#ifdef USE_ASSERT_CHECKING
26 years ago
assert(current->name);
assert(current->value);
#endif
26 years ago
if (strcmp(current->name, name) == 0)
{
free(current->name);
free(current->value);
if (previous)
previous->next = current->next;
free(current);
return true;
}
}
26 years ago
return true;
}
26 years ago
void
DestroyVariableSpace(VariableSpace space)
{
26 years ago
if (!space)
return;
26 years ago
DestroyVariableSpace(space->next);
free(space);
}