Header And Logo

PostgreSQL
| The world's most advanced open source database.

Functions

variables.c File Reference

#include "postgres_fe.h"
#include "common.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)
int ParseVariableNum (const char *val, int defaultval, int faultval, bool allowtrail)
int GetVariableNum (VariableSpace space, const char *name, int defaultval, int faultval, bool allowtrail)
void PrintVariables (VariableSpace space)
bool SetVariable (VariableSpace space, const char *name, const char *value)
bool SetVariableAssignHook (VariableSpace space, const char *name, VariableAssignHook hook)
bool SetVariableBool (VariableSpace space, const char *name)
bool DeleteVariable (VariableSpace space, const char *name)

Function Documentation

VariableSpace CreateVariableSpace ( void   ) 

Definition at line 48 of file variables.c.

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

Referenced by EstablishVariableSpace().

{
    struct _variable *ptr;

    ptr = pg_malloc(sizeof *ptr);
    ptr->name = NULL;
    ptr->value = NULL;
    ptr->assign_hook = NULL;
    ptr->next = NULL;

    return ptr;
}

bool DeleteVariable ( VariableSpace  space,
const char *  name 
)

Definition at line 267 of file variables.c.

References _variable::assign_hook, free, _variable::name, _variable::next, NULL, and _variable::value.

Referenced by parse_psql_options(), and SetVariable().

{
    struct _variable *current,
               *previous;

    if (!space)
        return false;

    for (previous = space, current = space->next;
         current;
         previous = current, current = current->next)
    {
        if (strcmp(current->name, name) == 0)
        {
            if (current->value)
                free(current->value);
            current->value = NULL;
            /* Physically delete only if no hook function to remember */
            if (current->assign_hook)
                (*current->assign_hook) (NULL);
            else
            {
                previous->next = current->next;
                free(current->name);
                free(current);
            }
            return true;
        }
    }

    return true;
}

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

Definition at line 62 of file variables.c.

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

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

{
    struct _variable *current;

    if (!space)
        return NULL;

    for (current = space->next; current; current = current->next)
    {
        if (strcmp(current->name, name) == 0)
        {
            /* this is correct answer when value is NULL, too */
            return current->value;
        }
    }

    return NULL;
}

int GetVariableNum ( VariableSpace  space,
const char *  name,
int  defaultval,
int  faultval,
bool  allowtrail 
)

Definition at line 151 of file variables.c.

References GetVariable(), ParseVariableNum(), and val.

Referenced by finishInput(), and MainLoop().

{
    const char *val;

    val = GetVariable(space, name);
    return ParseVariableNum(val, defaultval, faultval, allowtrail);
}

bool ParseVariableBool ( const char *  value  ) 

Definition at line 86 of file variables.c.

References NULL, pg_strcasecmp(), pg_strncasecmp(), and psql_error().

Referenced by autocommit_hook(), do_pset(), exec_command(), on_error_stop_hook(), quiet_hook(), singleline_hook(), and singlestep_hook().

{
    size_t      len;

    if (value == NULL)
        return false;           /* not set -> assume "off" */

    len = strlen(value);

    if (pg_strncasecmp(value, "true", len) == 0)
        return true;
    else if (pg_strncasecmp(value, "false", len) == 0)
        return false;
    else if (pg_strncasecmp(value, "yes", len) == 0)
        return true;
    else if (pg_strncasecmp(value, "no", len) == 0)
        return false;
    /* 'o' is not unique enough */
    else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
        return true;
    else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
        return false;
    else if (pg_strcasecmp(value, "1") == 0)
        return true;
    else if (pg_strcasecmp(value, "0") == 0)
        return false;
    else
    {
        /* NULL is treated as false, so a non-matching value is 'true' */
        psql_error("unrecognized Boolean value; assuming \"on\"\n");
        return true;
    }
}

int ParseVariableNum ( const char *  val,
int  defaultval,
int  faultval,
bool  allowtrail 
)

Definition at line 127 of file variables.c.

Referenced by fetch_count_hook(), and GetVariableNum().

{
    int         result;

    if (!val)
        result = defaultval;
    else if (!val[0])
        result = faultval;
    else
    {
        char       *end;

        result = strtol(val, &end, 0);
        if (!allowtrail && *end)
            result = faultval;
    }

    return result;
}

void PrintVariables ( VariableSpace  space  ) 

Definition at line 164 of file variables.c.

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

Referenced by exec_command().

{
    struct _variable *ptr;

    if (!space)
        return;

    for (ptr = space->next; ptr; ptr = ptr->next)
    {
        if (ptr->value)
            printf("%s = '%s'\n", ptr->name, ptr->value);
        if (cancel_pressed)
            break;
    }
}

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

Definition at line 181 of file variables.c.

References _variable::assign_hook, DeleteVariable(), free, _variable::name, _variable::next, pg_malloc(), pg_strdup(), valid_variable_name(), and _variable::value.

Referenced by do_lo_import(), exec_command(), main(), parse_psql_options(), PrintQueryStatus(), SendQuery(), SetVariableBool(), StoreQueryTuple(), SyncVariables(), and UnsyncVariables().

{
    struct _variable *current,
               *previous;

    if (!space)
        return false;

    if (!valid_variable_name(name))
        return false;

    if (!value)
        return DeleteVariable(space, name);

    for (previous = space, current = space->next;
         current;
         previous = current, current = current->next)
    {
        if (strcmp(current->name, name) == 0)
        {
            /* found entry, so update */
            if (current->value)
                free(current->value);
            current->value = pg_strdup(value);
            if (current->assign_hook)
                (*current->assign_hook) (current->value);
            return true;
        }
    }

    /* not present, make new entry */
    current = pg_malloc(sizeof *current);
    current->name = pg_strdup(name);
    current->value = pg_strdup(value);
    current->assign_hook = NULL;
    current->next = NULL;
    previous->next = current;
    return true;
}

bool SetVariableAssignHook ( VariableSpace  space,
const char *  name,
VariableAssignHook  hook 
)

Definition at line 225 of file variables.c.

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

Referenced by EstablishVariableSpace().

{
    struct _variable *current,
               *previous;

    if (!space)
        return false;

    if (!valid_variable_name(name))
        return false;

    for (previous = space, current = space->next;
         current;
         previous = current, current = current->next)
    {
        if (strcmp(current->name, name) == 0)
        {
            /* found entry, so update */
            current->assign_hook = hook;
            (*hook) (current->value);
            return true;
        }
    }

    /* not present, make new entry */
    current = pg_malloc(sizeof *current);
    current->name = pg_strdup(name);
    current->value = NULL;
    current->assign_hook = hook;
    current->next = NULL;
    previous->next = current;
    (*hook) (NULL);
    return true;
}

bool SetVariableBool ( VariableSpace  space,
const char *  name 
)

Definition at line 261 of file variables.c.

References SetVariable().

Referenced by main(), and parse_psql_options().

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

static bool valid_variable_name ( const char *  name  )  [static]

Definition at line 22 of file variables.c.

References IS_HIGHBIT_SET.

Referenced by SetVariable(), and SetVariableAssignHook().

{
    const unsigned char *ptr = (const unsigned char *) name;

    /* Mustn't be zero-length */
    if (*ptr == '\0')
        return false;

    while (*ptr)
    {
        if (IS_HIGHBIT_SET(*ptr) ||
            strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
                   "_0123456789", *ptr) != NULL)
            ptr++;
        else
            return false;
    }

    return true;
}