LCOV - differential code coverage report
Current view: top level - src/bin/psql - variables.c (source / functions) Coverage Total Hit UNC UBC CBC DUB
Current: 4b3c9325757dfd8f2312e9f1a8decf4431f344ae vs e87b024bfb8dd55e3049b1b6e6ab4e810cf1abc8 Lines: 80.3 % 188 151 1 36 151 1
Current Date: 2026-11-28 16:48:44 +0900 Functions: 84.6 % 13 11 1 1 11
Baseline: lcov-20261128-baseline Branches: 63.8 % 160 102 2 56 102
Baseline Date: 2026-11-28 16:48:48 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 0.0 % 1 0 1
(30,360] days: 75.0 % 4 3 1 3
(360..) days: 80.9 % 183 148 35 148
Function coverage date bins:
(360..) days: 84.6 % 13 11 1 1 11
Branch coverage date bins:
(7,30] days: 0.0 % 2 0 2
(360..) days: 64.6 % 158 102 56 102

 Age         Owner                    Branch data    TLA  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
 5573 tgl@sss.pgh.pa.us          25                 :CBC     2302273 : valid_variable_name(const char *name)
                                 26                 :                : {
                                 27                 :        2302273 :     const unsigned char *ptr = (const unsigned char *) name;
                                 28                 :                : 
                                 29                 :                :     /* Mustn't be zero-length */
                                 30         [ -  + ]:        2302273 :     if (*ptr == '\0')
 5573 tgl@sss.pgh.pa.us          31                 :UBC           0 :         return false;
                                 32                 :                : 
 5573 tgl@sss.pgh.pa.us          33         [ +  + ]:CBC    32407446 :     while (*ptr)
                                 34                 :                :     {
                                 35         [ +  - ]:       30105181 :         if (IS_HIGHBIT_SET(*ptr) ||
                                 36                 :       30105181 :             strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
                                 37         [ +  + ]:       30105181 :                    "_0123456789", *ptr) != NULL)
                                 38                 :       30105173 :             ptr++;
                                 39                 :                :         else
                                 40                 :              8 :             return false;
                                 41                 :                :     }
                                 42                 :                : 
                                 43                 :        2302265 :     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
 9886 bruce@momjian.us           54                 :          11566 : CreateVariableSpace(void)
                                 55                 :                : {
                                 56                 :                :     struct _variable *ptr;
                                 57                 :                : 
  274 michael@paquier.xyz        58                 :          11566 :     ptr = pg_malloc_object(struct _variable);
 7396 tgl@sss.pgh.pa.us          59                 :          11566 :     ptr->name = NULL;
                                 60                 :          11566 :     ptr->value = NULL;
 3587                            61                 :          11566 :     ptr->substitute_hook = NULL;
 7396                            62                 :          11566 :     ptr->assign_hook = NULL;
                                 63                 :          11566 :     ptr->next = NULL;
                                 64                 :                : 
 9886 bruce@momjian.us           65                 :          11566 :     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                 :           2942 : GetVariable(VariableSpace space, const char *name)
                                 75                 :                : {
                                 76                 :                :     struct _variable *current;
                                 77                 :                : 
                                 78         [ -  + ]:           2942 :     if (!space)
 9886 bruce@momjian.us           79                 :UBC           0 :         return NULL;
                                 80                 :                : 
 7465 tgl@sss.pgh.pa.us          81         [ +  + ]:CBC      115013 :     for (current = space->next; current; current = current->next)
                                 82                 :                :     {
 3587                            83                 :         115012 :         int         cmp = strcmp(current->name, name);
                                 84                 :                : 
                                 85         [ +  + ]:         115012 :         if (cmp == 0)
                                 86                 :                :         {
                                 87                 :                :             /* this is correct answer when value is NULL, too */
 9886 bruce@momjian.us           88                 :           2609 :             return current->value;
                                 89                 :                :         }
 3587 tgl@sss.pgh.pa.us          90         [ +  + ]:         112403 :         if (cmp > 0)
                                 91                 :            332 :             break;              /* it's not there */
                                 92                 :                :     }
                                 93                 :                : 
 9886 bruce@momjian.us           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
 3589 tgl@sss.pgh.pa.us         110                 :         159819 : ParseVariableBool(const char *value, const char *name, bool *result)
                                111                 :                : {
                                112                 :                :     size_t      len;
                                113                 :         159819 :     bool        valid = true;
                                114                 :                : 
                                115                 :                :     /* Treat "unset" as an empty string, which will lead to error below */
 6779 bruce@momjian.us          116         [ -  + ]:         159819 :     if (value == NULL)
 3587 tgl@sss.pgh.pa.us         117                 :UBC           0 :         value = "";
                                118                 :                : 
 6779 bruce@momjian.us          119                 :CBC      159819 :     len = strlen(value);
                                120                 :                : 
 3589 tgl@sss.pgh.pa.us         121   [ +  -  +  + ]:         159819 :     if (len > 0 && pg_strncasecmp(value, "true", len) == 0)
                                122                 :            155 :         *result = true;
                                123   [ +  -  +  + ]:         159664 :     else if (len > 0 && pg_strncasecmp(value, "false", len) == 0)
                                124                 :            201 :         *result = false;
                                125   [ +  -  +  + ]:         159463 :     else if (len > 0 && pg_strncasecmp(value, "yes", len) == 0)
                                126                 :              4 :         *result = true;
                                127   [ +  -  +  + ]:         159459 :     else if (len > 0 && pg_strncasecmp(value, "no", len) == 0)
                                128                 :              4 :         *result = false;
                                129                 :                :     /* 'o' is not unique enough */
 6779 bruce@momjian.us          130         [ +  + ]:         159455 :     else if (pg_strncasecmp(value, "on", (len > 2 ? len : 2)) == 0)
 3589 tgl@sss.pgh.pa.us         131                 :          35735 :         *result = true;
 6779 bruce@momjian.us          132         [ +  + ]:         123720 :     else if (pg_strncasecmp(value, "off", (len > 2 ? len : 2)) == 0)
 3589 tgl@sss.pgh.pa.us         133                 :         115797 :         *result = false;
 6779 bruce@momjian.us          134         [ +  + ]:           7923 :     else if (pg_strcasecmp(value, "1") == 0)
 3589 tgl@sss.pgh.pa.us         135                 :           7906 :         *result = true;
 6779 bruce@momjian.us          136         [ +  + ]:             17 :     else if (pg_strcasecmp(value, "0") == 0)
 3589 tgl@sss.pgh.pa.us         137                 :              5 :         *result = false;
                                138                 :                :     else
                                139                 :                :     {
                                140                 :                :         /* string is not recognized; don't clobber *result */
 4350                           141         [ +  + ]:             12 :         if (name)
 2798 peter@eisentraut.org      142                 :              8 :             pg_log_error("unrecognized value \"%s\" for \"%s\": Boolean expected",
                                143                 :                :                          value, name);
 3589 tgl@sss.pgh.pa.us         144                 :             12 :         valid = false;
                                145                 :                :     }
                                146                 :         159819 :     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                 :          34751 : 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         [ -  + ]:          34751 :     if (value == NULL)
 3587 tgl@sss.pgh.pa.us         166                 :UBC           0 :         value = "";
                                167                 :                : 
 3589 tgl@sss.pgh.pa.us         168                 :CBC       34751 :     errno = 0;
                                169                 :          34751 :     numval = strtol(value, &end, 0);
                                170   [ +  -  +  +  :          34751 :     if (errno == 0 && *end == '\0' && end != value && numval == (int) numval)
                                        +  -  +  - ]
                                171                 :                :     {
                                172                 :          34747 :         *result = (int) numval;
                                173                 :          34747 :         return true;
                                174                 :                :     }
                                175                 :                :     else
                                176                 :                :     {
                                177                 :                :         /* string is not recognized; don't clobber *result */
                                178         [ +  - ]:              4 :         if (name)
 2798 peter@eisentraut.org      179                 :              4 :             pg_log_error("invalid value \"%s\" for \"%s\": integer expected",
                                180                 :                :                          value, name);
 3589 tgl@sss.pgh.pa.us         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
  613 dgustafsson@postgres      196                 :          11570 : 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   [ +  -  -  + ]:          11570 :     if ((value == NULL) || (*value == '\0'))
                                206                 :                :     {
  613 dgustafsson@postgres      207         [ #  # ]:UBC           0 :         if (name)
  530 peter@eisentraut.org      208                 :              0 :             pg_log_error("invalid input syntax for variable \"%s\"", name);
  613 dgustafsson@postgres      209                 :              0 :         return false;
                                210                 :                :     }
                                211                 :                : 
  613 dgustafsson@postgres      212                 :CBC       11570 :     errno = 0;
                                213                 :          11570 :     dblval = strtod(value, &end);
                                214   [ +  +  +  -  :          11570 :     if (errno == 0 && *end == '\0' && end != value)
                                              +  - ]
                                215                 :                :     {
                                216         [ -  + ]:          11569 :         if (dblval < min)
                                217                 :                :         {
  613 dgustafsson@postgres      218         [ #  # ]:UBC           0 :             if (name)
  530 peter@eisentraut.org      219                 :              0 :                 pg_log_error("invalid value \"%s\" for variable \"%s\": must be greater than %.2f",
                                220                 :                :                              value, name, min);
  613 dgustafsson@postgres      221                 :              0 :             return false;
                                222                 :                :         }
  613 dgustafsson@postgres      223         [ -  + ]:CBC       11569 :         else if (dblval > max)
                                224                 :                :         {
  613 dgustafsson@postgres      225         [ #  # ]:UBC           0 :             if (name)
  530 peter@eisentraut.org      226                 :              0 :                 pg_log_error("invalid value \"%s\" for variable \"%s\": must be less than %.2f",
                                227                 :                :                              value, name, max);
  194 dgustafsson@postgres      228                 :              0 :             return false;
                                229                 :                :         }
  613 dgustafsson@postgres      230                 :CBC       11569 :         *result = dblval;
                                231                 :          11569 :         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                 :                :      */
  611                           239   [ +  -  +  - ]:              1 :     else if ((errno == ERANGE) &&
  613                           240   [ -  +  -  - ]:              1 :              (dblval == 0.0 || dblval >= HUGE_VAL || dblval <= -HUGE_VAL))
                                241                 :                :     {
                                242         [ +  - ]:              1 :         if (name)
  530 peter@eisentraut.org      243                 :              1 :             pg_log_error("value \"%s\" is out of range for variable \"%s\"", value, name);
  613 dgustafsson@postgres      244                 :              1 :         return false;
                                245                 :                :     }
                                246                 :                :     else
                                247                 :                :     {
  613 dgustafsson@postgres      248         [ #  # ]:UBC           0 :         if (name)
  530 peter@eisentraut.org      249                 :              0 :             pg_log_error("invalid value \"%s\" for variable \"%s\"", value, name);
  613 dgustafsson@postgres      250                 :              0 :         return false;
                                251                 :                :     }
                                252                 :                : }
                                253                 :                : 
                                254                 :                : /*
                                255                 :                :  * Print values of all variables.
                                256                 :                :  */
                                257                 :                : void
 8654 bruce@momjian.us          258                 :              0 : PrintVariables(VariableSpace space)
                                259                 :                : {
                                260                 :                :     struct _variable *ptr;
                                261                 :                : 
 7465 tgl@sss.pgh.pa.us         262         [ #  # ]:              0 :     if (!space)
                                263                 :              0 :         return;
                                264                 :                : 
 8517 bruce@momjian.us          265         [ #  # ]:              0 :     for (ptr = space->next; ptr; ptr = ptr->next)
                                266                 :                :     {
 7396 tgl@sss.pgh.pa.us         267         [ #  # ]:              0 :         if (ptr->value)
                                268                 :              0 :             printf("%s = '%s'\n", ptr->name, ptr->value);
    9 peter@eisentraut.org      269         [ #  # ]:UNC           0 :         if (CancelRequested)
 7472 tgl@sss.pgh.pa.us         270                 :UBC           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
 9886 bruce@momjian.us          283                 :CBC     2036255 : SetVariable(VariableSpace space, const char *name, const char *value)
                                284                 :                : {
                                285                 :                :     struct _variable *current,
                                286                 :                :                *previous;
                                287                 :                : 
 3589 tgl@sss.pgh.pa.us         288   [ +  -  -  + ]:        2036255 :     if (!space || !name)
 9886 bruce@momjian.us          289                 :UBC           0 :         return false;
                                290                 :                : 
 5573 tgl@sss.pgh.pa.us         291         [ +  + ]:CBC     2036255 :     if (!valid_variable_name(name))
                                292                 :                :     {
                                293                 :                :         /* Deletion of non-existent variable is not an error */
 3587                           294         [ -  + ]:              8 :         if (!value)
 3587 tgl@sss.pgh.pa.us         295                 :UBC           0 :             return true;
 2798 peter@eisentraut.org      296                 :CBC           8 :         pg_log_error("invalid variable name: \"%s\"", name);
 9886 bruce@momjian.us          297                 :              8 :         return false;
                                298                 :                :     }
                                299                 :                : 
 7465 tgl@sss.pgh.pa.us         300                 :        2036247 :     for (previous = space, current = space->next;
                                301         [ +  + ]:       44033398 :          current;
                                302                 :       41997151 :          previous = current, current = current->next)
                                303                 :                :     {
 3587                           304                 :       44032888 :         int         cmp = strcmp(current->name, name);
                                305                 :                : 
                                306         [ +  + ]:       44032888 :         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         [ +  + ]:        1812263 :             char       *new_value = value ? pg_strdup(value) : NULL;
                                318                 :                :             bool        confirmed;
                                319                 :                : 
                                320         [ +  + ]:        1812263 :             if (current->substitute_hook)
 3369 peter_e@gmx.net           321                 :          45234 :                 new_value = current->substitute_hook(new_value);
                                322                 :                : 
 7396 tgl@sss.pgh.pa.us         323         [ +  + ]:        1812263 :             if (current->assign_hook)
 3369 peter_e@gmx.net           324                 :          79932 :                 confirmed = current->assign_hook(new_value);
                                325                 :                :             else
 3589 tgl@sss.pgh.pa.us         326                 :        1732331 :                 confirmed = true;
                                327                 :                : 
                                328         [ +  + ]:        1812263 :             if (confirmed)
                                329                 :                :             {
 1625 peter@eisentraut.org      330                 :        1812250 :                 pg_free(current->value);
 3589 tgl@sss.pgh.pa.us         331                 :        1812250 :                 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                 :                :                  */
 3587                           337         [ +  + ]:        1812250 :                 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
 3447                           347                 :             13 :                 pg_free(new_value); /* current->value is left unchanged */
                                348                 :                : 
 3589                           349                 :        1812263 :             return confirmed;
                                350                 :                :         }
 3587                           351         [ +  + ]:       42220625 :         if (cmp > 0)
                                352                 :         223474 :             break;              /* it's not there */
                                353                 :                :     }
                                354                 :                : 
                                355                 :                :     /* not present, make new entry ... unless we were asked to delete */
                                356         [ +  + ]:         223984 :     if (value)
                                357                 :                :     {
  274 michael@paquier.xyz       358                 :         212332 :         current = pg_malloc_object(struct _variable);
 3587 tgl@sss.pgh.pa.us         359                 :         212332 :         current->name = pg_strdup(name);
                                360                 :         212332 :         current->value = pg_strdup(value);
                                361                 :         212332 :         current->substitute_hook = NULL;
                                362                 :         212332 :         current->assign_hook = NULL;
                                363                 :         212332 :         current->next = previous->next;
                                364                 :         212332 :         previous->next = current;
                                365                 :                :     }
 7396                           366                 :         223984 :     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
 3587                           386                 :         266018 : SetVariableHooks(VariableSpace space, const char *name,
                                387                 :                :                  VariableSubstituteHook shook,
                                388                 :                :                  VariableAssignHook ahook)
                                389                 :                : {
                                390                 :                :     struct _variable *current,
                                391                 :                :                *previous;
                                392                 :                : 
 3589                           393   [ +  -  -  + ]:         266018 :     if (!space || !name)
 3589 tgl@sss.pgh.pa.us         394                 :UBC           0 :         return;
                                395                 :                : 
 5573 tgl@sss.pgh.pa.us         396         [ -  + ]:CBC      266018 :     if (!valid_variable_name(name))
 3589 tgl@sss.pgh.pa.us         397                 :UBC           0 :         return;
                                398                 :                : 
 7396 tgl@sss.pgh.pa.us         399                 :CBC      266018 :     for (previous = space, current = space->next;
                                400         [ +  + ]:        2024050 :          current;
                                401                 :        1758032 :          previous = current, current = current->next)
                                402                 :                :     {
 3587                           403                 :        1943088 :         int         cmp = strcmp(current->name, name);
                                404                 :                : 
                                405         [ -  + ]:        1943088 :         if (cmp == 0)
                                406                 :                :         {
                                407                 :                :             /* found entry, so update */
 3587 tgl@sss.pgh.pa.us         408                 :UBC           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);
 3589                           414                 :              0 :             return;
                                415                 :                :         }
 3587 tgl@sss.pgh.pa.us         416         [ +  + ]:CBC     1943088 :         if (cmp > 0)
                                417                 :         185056 :             break;              /* it's not there */
                                418                 :                :     }
                                419                 :                : 
                                420                 :                :     /* not present, make new entry */
  274 michael@paquier.xyz       421                 :         266018 :     current = pg_malloc_object(struct _variable);
 7396 tgl@sss.pgh.pa.us         422                 :         266018 :     current->name = pg_strdup(name);
                                423                 :         266018 :     current->value = NULL;
 3587                           424                 :         266018 :     current->substitute_hook = shook;
                                425                 :         266018 :     current->assign_hook = ahook;
                                426                 :         266018 :     current->next = previous->next;
 7396                           427                 :         266018 :     previous->next = current;
 3587                           428         [ +  + ]:         266018 :     if (shook)
                                429                 :         219754 :         current->value = (*shook) (current->value);
                                430         [ +  - ]:         266018 :     if (ahook)
                                431                 :         266018 :         (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
 2210 noah@leadboat.com         439                 :            712 : VariableHasHook(VariableSpace space, const char *name)
                                440                 :                : {
                                441                 :                :     struct _variable *current;
                                442                 :                : 
                                443         [ -  + ]:            712 :     Assert(space);
                                444         [ -  + ]:            712 :     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
 9785 peter_e@gmx.net           464                 :          32951 : SetVariableBool(VariableSpace space, const char *name)
                                465                 :                : {
 8554 tgl@sss.pgh.pa.us         466                 :          32951 :     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
 9886 bruce@momjian.us          476                 :UBC           0 : DeleteVariable(VariableSpace space, const char *name)
                                477                 :                : {
 3587 tgl@sss.pgh.pa.us         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
 3589 tgl@sss.pgh.pa.us         488                 :CBC           4 : PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
                                489                 :                : {
 2798 peter@eisentraut.org      490                 :              4 :     pg_log_error("unrecognized value \"%s\" for \"%s\"\n"
                                491                 :                :                  "Available values are: %s.",
                                492                 :                :                  value, name, suggestions);
 3589 tgl@sss.pgh.pa.us         493                 :              4 : }
        

Generated by: LCOV version 2.0-1