LCOV - code coverage report
Current view: top level - src/bin/psql - startup.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 61.0 % 607 370
Test Date: 2026-01-26 10:56:24 Functions: 90.7 % 43 39
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 41.6 % 305 127

             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/startup.c
       7                 :             :  */
       8                 :             : #include "postgres_fe.h"
       9                 :             : 
      10                 :             : #ifndef WIN32
      11                 :             : #include <unistd.h>
      12                 :             : #else                                                   /* WIN32 */
      13                 :             : #include <io.h>
      14                 :             : #include <win32.h>
      15                 :             : #endif                                                  /* WIN32 */
      16                 :             : 
      17                 :             : #include "command.h"
      18                 :             : #include "common.h"
      19                 :             : #include "common/logging.h"
      20                 :             : #include "common/string.h"
      21                 :             : #include "describe.h"
      22                 :             : #include "fe_utils/print.h"
      23                 :             : #include "getopt_long.h"
      24                 :             : #include "help.h"
      25                 :             : #include "input.h"
      26                 :             : #include "mainloop.h"
      27                 :             : #include "settings.h"
      28                 :             : 
      29                 :             : /*
      30                 :             :  * Global psql options
      31                 :             :  */
      32                 :             : PsqlSettings pset;
      33                 :             : 
      34                 :             : #ifndef WIN32
      35                 :             : #define SYSPSQLRC       "psqlrc"
      36                 :             : #define PSQLRC          ".psqlrc"
      37                 :             : #else
      38                 :             : #define SYSPSQLRC       "psqlrc"
      39                 :             : #define PSQLRC          "psqlrc.conf"
      40                 :             : #endif
      41                 :             : 
      42                 :             : /*
      43                 :             :  * Structures to pass information between the option parsing routine
      44                 :             :  * and the main function
      45                 :             :  */
      46                 :             : enum _actions
      47                 :             : {
      48                 :             :         ACT_SINGLE_QUERY,
      49                 :             :         ACT_SINGLE_SLASH,
      50                 :             :         ACT_FILE,
      51                 :             : };
      52                 :             : 
      53                 :             : typedef struct SimpleActionListCell
      54                 :             : {
      55                 :             :         struct SimpleActionListCell *next;
      56                 :             :         enum _actions action;
      57                 :             :         char       *val;
      58                 :             : } SimpleActionListCell;
      59                 :             : 
      60                 :             : typedef struct SimpleActionList
      61                 :             : {
      62                 :             :         SimpleActionListCell *head;
      63                 :             :         SimpleActionListCell *tail;
      64                 :             : } SimpleActionList;
      65                 :             : 
      66                 :             : struct adhoc_opts
      67                 :             : {
      68                 :             :         char       *dbname;
      69                 :             :         char       *host;
      70                 :             :         char       *port;
      71                 :             :         char       *username;
      72                 :             :         char       *logfilename;
      73                 :             :         bool            no_readline;
      74                 :             :         bool            no_psqlrc;
      75                 :             :         bool            single_txn;
      76                 :             :         bool            list_dbs;
      77                 :             :         SimpleActionList actions;
      78                 :             : };
      79                 :             : 
      80                 :             : static void parse_psql_options(int argc, char *argv[],
      81                 :             :                                                            struct adhoc_opts *options);
      82                 :             : static void simple_action_list_append(SimpleActionList *list,
      83                 :             :                                                                           enum _actions action, const char *val);
      84                 :             : static void process_psqlrc(char *argv0);
      85                 :             : static void process_psqlrc_file(char *filename);
      86                 :             : static void showVersion(void);
      87                 :             : static void EstablishVariableSpace(void);
      88                 :             : 
      89                 :             : #define NOPAGER         0
      90                 :             : 
      91                 :             : static void
      92                 :        9852 : log_pre_callback(void)
      93                 :             : {
      94   [ +  -  +  - ]:        9852 :         if (pset.queryFout && pset.queryFout != stdout)
      95                 :           0 :                 fflush(pset.queryFout);
      96                 :        9852 : }
      97                 :             : 
      98                 :             : static void
      99                 :        9852 : log_locus_callback(const char **filename, uint64 *lineno)
     100                 :             : {
     101         [ -  + ]:        9852 :         if (pset.inputfile)
     102                 :             :         {
     103                 :           0 :                 *filename = pset.inputfile;
     104                 :           0 :                 *lineno = pset.lineno;
     105                 :           0 :         }
     106                 :             :         else
     107                 :             :         {
     108                 :        9852 :                 *filename = NULL;
     109                 :        9852 :                 *lineno = 0;
     110                 :             :         }
     111                 :        9852 : }
     112                 :             : 
     113                 :             : #ifndef WIN32
     114                 :             : static void
     115                 :           0 : empty_signal_handler(SIGNAL_ARGS)
     116                 :             : {
     117                 :           0 : }
     118                 :             : #endif
     119                 :             : 
     120                 :             : /*
     121                 :             :  *
     122                 :             :  * main
     123                 :             :  *
     124                 :             :  */
     125                 :             : int
     126                 :         272 : main(int argc, char *argv[])
     127                 :             : {
     128                 :         272 :         struct adhoc_opts options;
     129                 :         272 :         int                     successResult;
     130                 :         272 :         char       *password = NULL;
     131                 :         272 :         bool            new_pass;
     132                 :             : 
     133                 :         272 :         pg_logging_init(argv[0]);
     134                 :         272 :         pg_logging_set_pre_callback(log_pre_callback);
     135                 :         272 :         pg_logging_set_locus_callback(log_locus_callback);
     136                 :         272 :         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
     137                 :             : 
     138         [ -  + ]:         272 :         if (argc > 1)
     139                 :             :         {
     140   [ +  -  -  + ]:         272 :                 if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
     141                 :             :                 {
     142                 :           0 :                         usage(NOPAGER);
     143                 :           0 :                         exit(EXIT_SUCCESS);
     144                 :             :                 }
     145         [ +  - ]:         272 :                 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     146                 :             :                 {
     147                 :           0 :                         showVersion();
     148                 :           0 :                         exit(EXIT_SUCCESS);
     149                 :             :                 }
     150                 :         272 :         }
     151                 :             : 
     152                 :         272 :         pset.progname = get_progname(argv[0]);
     153                 :             : 
     154                 :         272 :         pset.db = NULL;
     155                 :         272 :         pset.dead_conn = NULL;
     156                 :         272 :         setDecimalLocale();
     157                 :         272 :         pset.encoding = PQenv2encoding();
     158                 :         272 :         pset.queryFout = stdout;
     159                 :         272 :         pset.queryFoutPipe = false;
     160                 :         272 :         pset.copyStream = NULL;
     161                 :         272 :         pset.last_error_result = NULL;
     162                 :         272 :         pset.cur_cmd_source = stdin;
     163                 :         272 :         pset.cur_cmd_interactive = false;
     164                 :             : 
     165                 :             :         /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
     166                 :         272 :         pset.popt.topt.format = PRINT_ALIGNED;
     167                 :         272 :         pset.popt.topt.border = 1;
     168                 :         272 :         pset.popt.topt.pager = 1;
     169                 :         272 :         pset.popt.topt.pager_min_lines = 0;
     170                 :         272 :         pset.popt.topt.start_table = true;
     171                 :         272 :         pset.popt.topt.stop_table = true;
     172                 :         272 :         pset.popt.topt.default_footer = true;
     173                 :             : 
     174                 :         272 :         pset.popt.topt.csvFieldSep[0] = DEFAULT_CSV_FIELD_SEP;
     175                 :         272 :         pset.popt.topt.csvFieldSep[1] = '\0';
     176                 :             : 
     177                 :         272 :         pset.popt.topt.unicode_border_linestyle = UNICODE_LINESTYLE_SINGLE;
     178                 :         272 :         pset.popt.topt.unicode_column_linestyle = UNICODE_LINESTYLE_SINGLE;
     179                 :         272 :         pset.popt.topt.unicode_header_linestyle = UNICODE_LINESTYLE_SINGLE;
     180                 :             : 
     181                 :         272 :         refresh_utf8format(&(pset.popt.topt));
     182                 :             : 
     183                 :             :         /* We must get COLUMNS here before readline() sets it */
     184         [ -  + ]:         272 :         pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
     185                 :             : 
     186         [ +  - ]:         272 :         pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
     187                 :             : 
     188                 :         272 :         pset.getPassword = TRI_DEFAULT;
     189                 :             : 
     190                 :         272 :         EstablishVariableSpace();
     191                 :             : 
     192                 :             :         /* Create variables showing psql version number */
     193                 :         272 :         SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
     194                 :         272 :         SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
     195                 :         272 :         SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
     196                 :             : 
     197                 :             :         /* Initialize variables for last error */
     198                 :         272 :         SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
     199                 :         272 :         SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
     200                 :             : 
     201                 :             :         /* Default values for variables (that don't match the result of \unset) */
     202                 :         272 :         SetVariableBool(pset.vars, "AUTOCOMMIT");
     203                 :         272 :         SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
     204                 :         272 :         SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
     205                 :         272 :         SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
     206                 :         272 :         SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
     207                 :             : 
     208                 :             :         /* Initialize pipeline variables */
     209                 :         272 :         SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0");
     210                 :         272 :         SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0");
     211                 :         272 :         SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0");
     212                 :             : 
     213                 :         272 :         parse_psql_options(argc, argv, &options);
     214                 :             : 
     215                 :             :         /*
     216                 :             :          * If no action was specified and we're in non-interactive mode, treat it
     217                 :             :          * as if the user had specified "-f -".  This lets single-transaction mode
     218                 :             :          * work in this case.
     219                 :             :          */
     220   [ +  +  -  + ]:         272 :         if (options.actions.head == NULL && pset.notty)
     221                 :         246 :                 simple_action_list_append(&options.actions, ACT_FILE, NULL);
     222                 :             : 
     223                 :             :         /* Bail out if -1 was specified but will be ignored. */
     224   [ -  +  #  # ]:         272 :         if (options.single_txn && options.actions.head == NULL)
     225                 :           0 :                 pg_fatal("-1 can only be used in non-interactive mode");
     226                 :             : 
     227   [ +  -  -  + ]:         272 :         if (!pset.popt.topt.fieldSep.separator &&
     228                 :         272 :                 !pset.popt.topt.fieldSep.separator_zero)
     229                 :             :         {
     230                 :         272 :                 pset.popt.topt.fieldSep.separator = pg_strdup(DEFAULT_FIELD_SEP);
     231                 :         272 :                 pset.popt.topt.fieldSep.separator_zero = false;
     232                 :         272 :         }
     233   [ +  -  -  + ]:         272 :         if (!pset.popt.topt.recordSep.separator &&
     234                 :         272 :                 !pset.popt.topt.recordSep.separator_zero)
     235                 :             :         {
     236                 :         272 :                 pset.popt.topt.recordSep.separator = pg_strdup(DEFAULT_RECORD_SEP);
     237                 :         272 :                 pset.popt.topt.recordSep.separator_zero = false;
     238                 :         272 :         }
     239                 :             : 
     240         [ +  - ]:         272 :         if (pset.getPassword == TRI_YES)
     241                 :             :         {
     242                 :             :                 /*
     243                 :             :                  * We can't be sure yet of the username that will be used, so don't
     244                 :             :                  * offer a potentially wrong one.  Typical uses of this option are
     245                 :             :                  * noninteractive anyway.  (Note: since we've not yet set up our
     246                 :             :                  * cancel handler, there's no need to use simple_prompt_extended.)
     247                 :             :                  */
     248                 :           0 :                 password = simple_prompt("Password: ", false);
     249                 :           0 :         }
     250                 :             : 
     251                 :             :         /* loop until we have a password if requested by backend */
     252                 :         272 :         do
     253                 :             :         {
     254                 :             : #define PARAMS_ARRAY_SIZE       8
     255                 :         272 :                 const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
     256                 :         272 :                 const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
     257                 :             : 
     258                 :         272 :                 keywords[0] = "host";
     259                 :         272 :                 values[0] = options.host;
     260                 :         272 :                 keywords[1] = "port";
     261                 :         272 :                 values[1] = options.port;
     262                 :         272 :                 keywords[2] = "user";
     263                 :         272 :                 values[2] = options.username;
     264                 :         272 :                 keywords[3] = "password";
     265                 :         272 :                 values[3] = password;
     266                 :         272 :                 keywords[4] = "dbname"; /* see do_connect() */
     267   [ -  +  #  # ]:         272 :                 values[4] = (options.list_dbs && options.dbname == NULL) ?
     268                 :         272 :                         "postgres" : options.dbname;
     269                 :         272 :                 keywords[5] = "fallback_application_name";
     270                 :         272 :                 values[5] = pset.progname;
     271                 :         272 :                 keywords[6] = "client_encoding";
     272         [ +  - ]:         272 :                 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
     273                 :         272 :                 keywords[7] = NULL;
     274                 :         272 :                 values[7] = NULL;
     275                 :             : 
     276                 :         272 :                 new_pass = false;
     277                 :         272 :                 pset.db = PQconnectdbParams(keywords, values, true);
     278                 :         272 :                 free(keywords);
     279                 :         272 :                 free(values);
     280                 :             : 
     281         [ -  + ]:         272 :                 if (PQstatus(pset.db) == CONNECTION_BAD &&
     282         [ #  # ]:           0 :                         PQconnectionNeedsPassword(pset.db) &&
     283   [ #  #  #  # ]:           0 :                         !password &&
     284                 :           0 :                         pset.getPassword != TRI_NO)
     285                 :             :                 {
     286                 :             :                         /*
     287                 :             :                          * Before closing the old PGconn, extract the user name that was
     288                 :             :                          * actually connected with --- it might've come out of a URI or
     289                 :             :                          * connstring "database name" rather than options.username.
     290                 :             :                          */
     291                 :           0 :                         const char *realusername = PQuser(pset.db);
     292                 :           0 :                         char       *password_prompt;
     293                 :             : 
     294   [ #  #  #  # ]:           0 :                         if (realusername && realusername[0])
     295                 :           0 :                                 password_prompt = psprintf(_("Password for user %s: "),
     296                 :           0 :                                                                                    realusername);
     297                 :             :                         else
     298                 :           0 :                                 password_prompt = pg_strdup(_("Password: "));
     299                 :           0 :                         PQfinish(pset.db);
     300                 :             : 
     301                 :           0 :                         password = simple_prompt(password_prompt, false);
     302                 :           0 :                         free(password_prompt);
     303                 :           0 :                         new_pass = true;
     304                 :           0 :                 }
     305         [ -  + ]:         272 :         } while (new_pass);
     306                 :             : 
     307         [ +  - ]:         272 :         if (PQstatus(pset.db) == CONNECTION_BAD)
     308                 :             :         {
     309                 :           0 :                 pg_log_error("%s", PQerrorMessage(pset.db));
     310                 :           0 :                 PQfinish(pset.db);
     311                 :           0 :                 exit(EXIT_BADCONN);
     312                 :             :         }
     313                 :             : 
     314                 :         272 :         psql_setup_cancel_handler();
     315                 :             : 
     316                 :             : #ifndef WIN32
     317                 :             : 
     318                 :             :         /*
     319                 :             :          * do_watch() needs signal handlers installed (otherwise sigwait() will
     320                 :             :          * filter them out on some platforms), but doesn't need them to do
     321                 :             :          * anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
     322                 :             :          * arrives due to a race when do_watch() cancels an itimer).
     323                 :             :          */
     324                 :         272 :         pqsignal(SIGCHLD, empty_signal_handler);
     325                 :         272 :         pqsignal(SIGALRM, empty_signal_handler);
     326                 :             : #endif
     327                 :             : 
     328                 :         272 :         PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
     329                 :             : 
     330                 :         272 :         SyncVariables();
     331                 :             : 
     332         [ +  - ]:         272 :         if (options.list_dbs)
     333                 :             :         {
     334                 :           0 :                 int                     success;
     335                 :             : 
     336         [ #  # ]:           0 :                 if (!options.no_psqlrc)
     337                 :           0 :                         process_psqlrc(argv[0]);
     338                 :             : 
     339                 :           0 :                 success = listAllDbs(NULL, false);
     340                 :           0 :                 PQfinish(pset.db);
     341                 :           0 :                 exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
     342                 :             :         }
     343                 :             : 
     344         [ +  - ]:         272 :         if (options.logfilename)
     345                 :             :         {
     346                 :           0 :                 pset.logfile = fopen(options.logfilename, "a");
     347         [ #  # ]:           0 :                 if (!pset.logfile)
     348                 :           0 :                         pg_fatal("could not open log file \"%s\": %m",
     349                 :             :                                          options.logfilename);
     350                 :           0 :         }
     351                 :             : 
     352         [ +  - ]:         272 :         if (!options.no_psqlrc)
     353                 :           0 :                 process_psqlrc(argv[0]);
     354                 :             : 
     355                 :             :         /*
     356                 :             :          * If any actions were given by user, process them in the order in which
     357                 :             :          * they were specified.  Note single_txn is only effective in this mode.
     358                 :             :          */
     359         [ +  - ]:         272 :         if (options.actions.head != NULL)
     360                 :             :         {
     361                 :         272 :                 PGresult   *res;
     362                 :         272 :                 SimpleActionListCell *cell;
     363                 :             : 
     364                 :         272 :                 successResult = EXIT_SUCCESS;   /* silence compiler */
     365                 :             : 
     366         [ +  - ]:         272 :                 if (options.single_txn)
     367                 :             :                 {
     368         [ #  # ]:           0 :                         if ((res = PSQLexec("BEGIN")) == NULL)
     369                 :             :                         {
     370         [ #  # ]:           0 :                                 if (pset.on_error_stop)
     371                 :             :                                 {
     372                 :           0 :                                         successResult = EXIT_USER;
     373                 :           0 :                                         goto error;
     374                 :             :                                 }
     375                 :           0 :                         }
     376                 :             :                         else
     377                 :           0 :                                 PQclear(res);
     378                 :           0 :                 }
     379                 :             : 
     380         [ +  + ]:         547 :                 for (cell = options.actions.head; cell; cell = cell->next)
     381                 :             :                 {
     382         [ +  + ]:         275 :                         if (cell->action == ACT_SINGLE_QUERY)
     383                 :             :                         {
     384                 :           6 :                                 pg_logging_config(PG_LOG_FLAG_TERSE);
     385                 :             : 
     386         [ +  - ]:           6 :                                 if (pset.echo == PSQL_ECHO_ALL)
     387                 :           0 :                                         puts(cell->val);
     388                 :             : 
     389                 :           6 :                                 successResult = SendQuery(cell->val)
     390                 :             :                                         ? EXIT_SUCCESS : EXIT_FAILURE;
     391                 :           6 :                         }
     392         [ -  + ]:         269 :                         else if (cell->action == ACT_SINGLE_SLASH)
     393                 :             :                         {
     394                 :           0 :                                 PsqlScanState scan_state;
     395                 :           0 :                                 ConditionalStack cond_stack;
     396                 :             : 
     397                 :           0 :                                 pg_logging_config(PG_LOG_FLAG_TERSE);
     398                 :             : 
     399         [ #  # ]:           0 :                                 if (pset.echo == PSQL_ECHO_ALL)
     400                 :           0 :                                         puts(cell->val);
     401                 :             : 
     402                 :           0 :                                 scan_state = psql_scan_create(&psqlscan_callbacks);
     403                 :           0 :                                 psql_scan_setup(scan_state,
     404                 :           0 :                                                                 cell->val, strlen(cell->val),
     405                 :           0 :                                                                 pset.encoding, standard_strings());
     406                 :           0 :                                 cond_stack = conditional_stack_create();
     407                 :           0 :                                 psql_scan_set_passthrough(scan_state, cond_stack);
     408                 :             : 
     409                 :           0 :                                 successResult = HandleSlashCmds(scan_state,
     410                 :           0 :                                                                                                 cond_stack,
     411                 :             :                                                                                                 NULL,
     412                 :           0 :                                                                                                 NULL) != PSQL_CMD_ERROR
     413                 :             :                                         ? EXIT_SUCCESS : EXIT_FAILURE;
     414                 :             : 
     415                 :           0 :                                 psql_scan_destroy(scan_state);
     416                 :           0 :                                 conditional_stack_destroy(cond_stack);
     417                 :           0 :                         }
     418         [ +  - ]:         269 :                         else if (cell->action == ACT_FILE)
     419                 :             :                         {
     420                 :         269 :                                 successResult = process_file(cell->val, false);
     421                 :         269 :                         }
     422                 :             :                         else
     423                 :             :                         {
     424                 :             :                                 /* should never come here */
     425                 :           0 :                                 Assert(false);
     426                 :             :                         }
     427                 :             : 
     428   [ -  +  #  # ]:         275 :                         if (successResult != EXIT_SUCCESS && pset.on_error_stop)
     429                 :           0 :                                 break;
     430                 :         275 :                 }
     431                 :             : 
     432         [ +  - ]:         272 :                 if (options.single_txn)
     433                 :             :                 {
     434                 :             :                         /*
     435                 :             :                          * Rollback the contents of the single transaction if the caller
     436                 :             :                          * has set ON_ERROR_STOP and one of the steps has failed.  This
     437                 :             :                          * check needs to match the one done a couple of lines above.
     438                 :             :                          */
     439         [ #  # ]:           0 :                         res = PSQLexec((successResult != EXIT_SUCCESS && pset.on_error_stop) ?
     440                 :             :                                                    "ROLLBACK" : "COMMIT");
     441         [ #  # ]:           0 :                         if (res == NULL)
     442                 :             :                         {
     443         [ #  # ]:           0 :                                 if (pset.on_error_stop)
     444                 :             :                                 {
     445                 :           0 :                                         successResult = EXIT_USER;
     446                 :           0 :                                         goto error;
     447                 :             :                                 }
     448                 :           0 :                         }
     449                 :             :                         else
     450                 :           0 :                                 PQclear(res);
     451                 :           0 :                 }
     452                 :             : 
     453                 :             : error:
     454                 :             :                 ;
     455                 :         272 :         }
     456                 :             : 
     457                 :             :         /*
     458                 :             :          * or otherwise enter interactive main loop
     459                 :             :          */
     460                 :             :         else
     461                 :             :         {
     462                 :           0 :                 pg_logging_config(PG_LOG_FLAG_TERSE);
     463                 :           0 :                 connection_warnings(true);
     464         [ #  # ]:           0 :                 if (!pset.quiet)
     465                 :           0 :                         printf(_("Type \"help\" for help.\n\n"));
     466                 :           0 :                 initializeInput(options.no_readline ? 0 : 1);
     467                 :           0 :                 successResult = MainLoop(stdin);
     468                 :             :         }
     469                 :             : 
     470                 :             :         /* clean up */
     471         [ +  - ]:         272 :         if (pset.logfile)
     472                 :           0 :                 fclose(pset.logfile);
     473         [ -  + ]:         272 :         if (pset.db)
     474                 :         272 :                 PQfinish(pset.db);
     475         [ +  - ]:         272 :         if (pset.dead_conn)
     476                 :           0 :                 PQfinish(pset.dead_conn);
     477                 :         272 :         setQFout(NULL);
     478                 :             : 
     479                 :         544 :         return successResult;
     480                 :         272 : }
     481                 :             : 
     482                 :             : 
     483                 :             : /*
     484                 :             :  * Parse command line options
     485                 :             :  */
     486                 :             : 
     487                 :             : static void
     488                 :         272 : parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
     489                 :             : {
     490                 :             :         static struct option long_options[] =
     491                 :             :         {
     492                 :             :                 {"echo-all", no_argument, NULL, 'a'},
     493                 :             :                 {"no-align", no_argument, NULL, 'A'},
     494                 :             :                 {"command", required_argument, NULL, 'c'},
     495                 :             :                 {"dbname", required_argument, NULL, 'd'},
     496                 :             :                 {"echo-queries", no_argument, NULL, 'e'},
     497                 :             :                 {"echo-errors", no_argument, NULL, 'b'},
     498                 :             :                 {"echo-hidden", no_argument, NULL, 'E'},
     499                 :             :                 {"file", required_argument, NULL, 'f'},
     500                 :             :                 {"field-separator", required_argument, NULL, 'F'},
     501                 :             :                 {"field-separator-zero", no_argument, NULL, 'z'},
     502                 :             :                 {"host", required_argument, NULL, 'h'},
     503                 :             :                 {"html", no_argument, NULL, 'H'},
     504                 :             :                 {"list", no_argument, NULL, 'l'},
     505                 :             :                 {"log-file", required_argument, NULL, 'L'},
     506                 :             :                 {"no-readline", no_argument, NULL, 'n'},
     507                 :             :                 {"single-transaction", no_argument, NULL, '1'},
     508                 :             :                 {"output", required_argument, NULL, 'o'},
     509                 :             :                 {"port", required_argument, NULL, 'p'},
     510                 :             :                 {"pset", required_argument, NULL, 'P'},
     511                 :             :                 {"quiet", no_argument, NULL, 'q'},
     512                 :             :                 {"record-separator", required_argument, NULL, 'R'},
     513                 :             :                 {"record-separator-zero", no_argument, NULL, '0'},
     514                 :             :                 {"single-step", no_argument, NULL, 's'},
     515                 :             :                 {"single-line", no_argument, NULL, 'S'},
     516                 :             :                 {"tuples-only", no_argument, NULL, 't'},
     517                 :             :                 {"table-attr", required_argument, NULL, 'T'},
     518                 :             :                 {"username", required_argument, NULL, 'U'},
     519                 :             :                 {"set", required_argument, NULL, 'v'},
     520                 :             :                 {"variable", required_argument, NULL, 'v'},
     521                 :             :                 {"version", no_argument, NULL, 'V'},
     522                 :             :                 {"no-password", no_argument, NULL, 'w'},
     523                 :             :                 {"password", no_argument, NULL, 'W'},
     524                 :             :                 {"expanded", no_argument, NULL, 'x'},
     525                 :             :                 {"no-psqlrc", no_argument, NULL, 'X'},
     526                 :             :                 {"help", optional_argument, NULL, 1},
     527                 :             :                 {"csv", no_argument, NULL, 2},
     528                 :             :                 {NULL, 0, NULL, 0}
     529                 :             :         };
     530                 :             : 
     531                 :         272 :         int                     optindex;
     532                 :         272 :         int                     c;
     533                 :             : 
     534                 :         272 :         memset(options, 0, sizeof *options);
     535                 :             : 
     536   [ +  +  +  + ]:        1921 :         while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
     537                 :        1921 :                                                         long_options, &optindex)) != -1)
     538                 :             :         {
     539   [ -  -  +  +  :        1649 :                 switch (c)
          -  +  +  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          +  -  -  -  +  
          -  -  +  -  -  
          -  +  -  -  -  
                -  -  - ]
     540                 :             :                 {
     541                 :             :                         case 'a':
     542                 :         246 :                                 SetVariable(pset.vars, "ECHO", "all");
     543                 :         246 :                                 break;
     544                 :             :                         case 'A':
     545                 :          23 :                                 pset.popt.topt.format = PRINT_UNALIGNED;
     546                 :          23 :                                 break;
     547                 :             :                         case 'b':
     548                 :           0 :                                 SetVariable(pset.vars, "ECHO", "errors");
     549                 :           0 :                                 break;
     550                 :             :                         case 'c':
     551         [ -  + ]:           6 :                                 if (optarg[0] == '\\')
     552                 :           0 :                                         simple_action_list_append(&options->actions,
     553                 :             :                                                                                           ACT_SINGLE_SLASH,
     554                 :           0 :                                                                                           optarg + 1);
     555                 :             :                                 else
     556                 :          12 :                                         simple_action_list_append(&options->actions,
     557                 :             :                                                                                           ACT_SINGLE_QUERY,
     558                 :           6 :                                                                                           optarg);
     559                 :           6 :                                 break;
     560                 :             :                         case 'd':
     561                 :         269 :                                 options->dbname = pg_strdup(optarg);
     562                 :         269 :                                 break;
     563                 :             :                         case 'e':
     564                 :           0 :                                 SetVariable(pset.vars, "ECHO", "queries");
     565                 :           0 :                                 break;
     566                 :             :                         case 'E':
     567                 :           0 :                                 SetVariableBool(pset.vars, "ECHO_HIDDEN");
     568                 :           0 :                                 break;
     569                 :             :                         case 'f':
     570                 :          46 :                                 simple_action_list_append(&options->actions,
     571                 :             :                                                                                   ACT_FILE,
     572                 :          23 :                                                                                   optarg);
     573                 :          23 :                                 break;
     574                 :             :                         case 'F':
     575                 :           0 :                                 pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
     576                 :           0 :                                 pset.popt.topt.fieldSep.separator_zero = false;
     577                 :           0 :                                 break;
     578                 :             :                         case 'h':
     579                 :           0 :                                 options->host = pg_strdup(optarg);
     580                 :           0 :                                 break;
     581                 :             :                         case 'H':
     582                 :           0 :                                 pset.popt.topt.format = PRINT_HTML;
     583                 :           0 :                                 break;
     584                 :             :                         case 'l':
     585                 :           0 :                                 options->list_dbs = true;
     586                 :           0 :                                 break;
     587                 :             :                         case 'L':
     588                 :           0 :                                 options->logfilename = pg_strdup(optarg);
     589                 :           0 :                                 break;
     590                 :             :                         case 'n':
     591                 :           0 :                                 options->no_readline = true;
     592                 :           0 :                                 break;
     593                 :             :                         case 'o':
     594         [ #  # ]:           0 :                                 if (!setQFout(optarg))
     595                 :           0 :                                         exit(EXIT_FAILURE);
     596                 :           0 :                                 break;
     597                 :             :                         case 'p':
     598                 :           0 :                                 options->port = pg_strdup(optarg);
     599                 :           0 :                                 break;
     600                 :             :                         case 'P':
     601                 :             :                                 {
     602                 :           0 :                                         char       *value;
     603                 :           0 :                                         char       *equal_loc;
     604                 :           0 :                                         bool            result;
     605                 :             : 
     606                 :           0 :                                         value = pg_strdup(optarg);
     607                 :           0 :                                         equal_loc = strchr(value, '=');
     608         [ #  # ]:           0 :                                         if (!equal_loc)
     609                 :           0 :                                                 result = do_pset(value, NULL, &pset.popt, true);
     610                 :             :                                         else
     611                 :             :                                         {
     612                 :           0 :                                                 *equal_loc = '\0';
     613                 :           0 :                                                 result = do_pset(value, equal_loc + 1, &pset.popt, true);
     614                 :             :                                         }
     615                 :             : 
     616         [ #  # ]:           0 :                                         if (!result)
     617                 :           0 :                                                 pg_fatal("could not set printing parameter \"%s\"", value);
     618                 :             : 
     619                 :           0 :                                         free(value);
     620                 :             :                                         break;
     621                 :           0 :                                 }
     622                 :             :                         case 'q':
     623                 :         272 :                                 SetVariableBool(pset.vars, "QUIET");
     624                 :         272 :                                 break;
     625                 :             :                         case 'R':
     626                 :           0 :                                 pset.popt.topt.recordSep.separator = pg_strdup(optarg);
     627                 :           0 :                                 pset.popt.topt.recordSep.separator_zero = false;
     628                 :           0 :                                 break;
     629                 :             :                         case 's':
     630                 :           0 :                                 SetVariableBool(pset.vars, "SINGLESTEP");
     631                 :           0 :                                 break;
     632                 :             :                         case 'S':
     633                 :           0 :                                 SetVariableBool(pset.vars, "SINGLELINE");
     634                 :           0 :                                 break;
     635                 :             :                         case 't':
     636                 :          23 :                                 pset.popt.topt.tuples_only = true;
     637                 :          23 :                                 break;
     638                 :             :                         case 'T':
     639                 :           0 :                                 pset.popt.topt.tableAttr = pg_strdup(optarg);
     640                 :           0 :                                 break;
     641                 :             :                         case 'U':
     642                 :           0 :                                 options->username = pg_strdup(optarg);
     643                 :           0 :                                 break;
     644                 :             :                         case 'v':
     645                 :             :                                 {
     646                 :         515 :                                         char       *value;
     647                 :         515 :                                         char       *equal_loc;
     648                 :             : 
     649                 :         515 :                                         value = pg_strdup(optarg);
     650                 :         515 :                                         equal_loc = strchr(value, '=');
     651         [ +  - ]:         515 :                                         if (!equal_loc)
     652                 :             :                                         {
     653         [ #  # ]:           0 :                                                 if (!DeleteVariable(pset.vars, value))
     654                 :           0 :                                                         exit(EXIT_FAILURE); /* error already printed */
     655                 :           0 :                                         }
     656                 :             :                                         else
     657                 :             :                                         {
     658                 :         515 :                                                 *equal_loc = '\0';
     659         [ +  - ]:         515 :                                                 if (!SetVariable(pset.vars, value, equal_loc + 1))
     660                 :           0 :                                                         exit(EXIT_FAILURE); /* error already printed */
     661                 :             :                                         }
     662                 :             : 
     663                 :         515 :                                         free(value);
     664                 :             :                                         break;
     665                 :         515 :                                 }
     666                 :             :                         case 'V':
     667                 :           0 :                                 showVersion();
     668                 :           0 :                                 exit(EXIT_SUCCESS);
     669                 :             :                         case 'w':
     670                 :           0 :                                 pset.getPassword = TRI_NO;
     671                 :           0 :                                 break;
     672                 :             :                         case 'W':
     673                 :           0 :                                 pset.getPassword = TRI_YES;
     674                 :           0 :                                 break;
     675                 :             :                         case 'x':
     676                 :           0 :                                 pset.popt.topt.expanded = true;
     677                 :           0 :                                 break;
     678                 :             :                         case 'X':
     679                 :         272 :                                 options->no_psqlrc = true;
     680                 :         272 :                                 break;
     681                 :             :                         case 'z':
     682                 :           0 :                                 pset.popt.topt.fieldSep.separator_zero = true;
     683                 :           0 :                                 break;
     684                 :             :                         case '0':
     685                 :           0 :                                 pset.popt.topt.recordSep.separator_zero = true;
     686                 :           0 :                                 break;
     687                 :             :                         case '1':
     688                 :           0 :                                 options->single_txn = true;
     689                 :           0 :                                 break;
     690                 :             :                         case '?':
     691   [ #  #  #  # ]:           0 :                                 if (optind <= argc &&
     692                 :           0 :                                         strcmp(argv[optind - 1], "-?") == 0)
     693                 :             :                                 {
     694                 :             :                                         /* actual help option given */
     695                 :           0 :                                         usage(NOPAGER);
     696                 :           0 :                                         exit(EXIT_SUCCESS);
     697                 :             :                                 }
     698                 :             :                                 else
     699                 :             :                                 {
     700                 :             :                                         /* getopt error (unknown option or missing argument) */
     701                 :           0 :                                         goto unknown_option;
     702                 :             :                                 }
     703                 :             :                                 break;
     704                 :             :                         case 1:
     705                 :             :                                 {
     706   [ #  #  #  # ]:           0 :                                         if (!optarg || strcmp(optarg, "options") == 0)
     707                 :           0 :                                                 usage(NOPAGER);
     708   [ #  #  #  # ]:           0 :                                         else if (optarg && strcmp(optarg, "commands") == 0)
     709                 :           0 :                                                 slashUsage(NOPAGER);
     710   [ #  #  #  # ]:           0 :                                         else if (optarg && strcmp(optarg, "variables") == 0)
     711                 :           0 :                                                 helpVariables(NOPAGER);
     712                 :             :                                         else
     713                 :           0 :                                                 goto unknown_option;
     714                 :             : 
     715                 :           0 :                                         exit(EXIT_SUCCESS);
     716                 :             :                                 }
     717                 :             :                                 break;
     718                 :             :                         case 2:
     719                 :           0 :                                 pset.popt.topt.format = PRINT_CSV;
     720                 :           0 :                                 break;
     721                 :             :                         default:
     722                 :             :                 unknown_option:
     723                 :             :                                 /* getopt_long already emitted a complaint */
     724                 :           0 :                                 pg_log_error_hint("Try \"%s --help\" for more information.",
     725                 :             :                                                                   pset.progname);
     726                 :           0 :                                 exit(EXIT_FAILURE);
     727                 :             :                 }
     728                 :             :         }
     729                 :             : 
     730                 :             :         /*
     731                 :             :          * if we still have arguments, use it as the database name and username
     732                 :             :          */
     733         [ +  + ]:         275 :         while (argc - optind >= 1)
     734                 :             :         {
     735         [ -  + ]:           3 :                 if (!options->dbname)
     736                 :           3 :                         options->dbname = argv[optind];
     737         [ #  # ]:           0 :                 else if (!options->username)
     738                 :           0 :                         options->username = argv[optind];
     739         [ #  # ]:           0 :                 else if (!pset.quiet)
     740                 :           0 :                         pg_log_warning("extra command-line argument \"%s\" ignored",
     741                 :             :                                                    argv[optind]);
     742                 :             : 
     743                 :           3 :                 optind++;
     744                 :             :         }
     745                 :         272 : }
     746                 :             : 
     747                 :             : 
     748                 :             : /*
     749                 :             :  * Append a new item to the end of the SimpleActionList.
     750                 :             :  * Note that "val" is copied if it's not NULL.
     751                 :             :  */
     752                 :             : static void
     753                 :         275 : simple_action_list_append(SimpleActionList *list,
     754                 :             :                                                   enum _actions action, const char *val)
     755                 :             : {
     756                 :         275 :         SimpleActionListCell *cell;
     757                 :             : 
     758                 :         275 :         cell = pg_malloc_object(SimpleActionListCell);
     759                 :             : 
     760                 :         275 :         cell->next = NULL;
     761                 :         275 :         cell->action = action;
     762         [ +  + ]:         275 :         if (val)
     763                 :          29 :                 cell->val = pg_strdup(val);
     764                 :             :         else
     765                 :         246 :                 cell->val = NULL;
     766                 :             : 
     767         [ +  + ]:         275 :         if (list->tail)
     768                 :           3 :                 list->tail->next = cell;
     769                 :             :         else
     770                 :         272 :                 list->head = cell;
     771                 :         275 :         list->tail = cell;
     772                 :         275 : }
     773                 :             : 
     774                 :             : 
     775                 :             : /*
     776                 :             :  * Load .psqlrc file, if found.
     777                 :             :  */
     778                 :             : static void
     779                 :           0 : process_psqlrc(char *argv0)
     780                 :             : {
     781                 :           0 :         char            home[MAXPGPATH];
     782                 :           0 :         char            rc_file[MAXPGPATH];
     783                 :           0 :         char            my_exec_path[MAXPGPATH];
     784                 :           0 :         char            etc_path[MAXPGPATH];
     785                 :           0 :         char       *envrc = getenv("PSQLRC");
     786                 :             : 
     787         [ #  # ]:           0 :         if (find_my_exec(argv0, my_exec_path) < 0)
     788                 :           0 :                 pg_fatal("could not find own program executable");
     789                 :             : 
     790                 :           0 :         get_etc_path(my_exec_path, etc_path);
     791                 :             : 
     792                 :           0 :         snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
     793                 :           0 :         process_psqlrc_file(rc_file);
     794                 :             : 
     795   [ #  #  #  # ]:           0 :         if (envrc != NULL && strlen(envrc) > 0)
     796                 :             :         {
     797                 :             :                 /* might need to free() this */
     798                 :           0 :                 char       *envrc_alloc = pstrdup(envrc);
     799                 :             : 
     800                 :           0 :                 expand_tilde(&envrc_alloc);
     801                 :           0 :                 process_psqlrc_file(envrc_alloc);
     802                 :           0 :         }
     803         [ #  # ]:           0 :         else if (get_home_path(home))
     804                 :             :         {
     805                 :           0 :                 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
     806                 :           0 :                 process_psqlrc_file(rc_file);
     807                 :           0 :         }
     808                 :           0 : }
     809                 :             : 
     810                 :             : 
     811                 :             : 
     812                 :             : static void
     813                 :           0 : process_psqlrc_file(char *filename)
     814                 :             : {
     815                 :           0 :         char       *psqlrc_minor,
     816                 :             :                            *psqlrc_major;
     817                 :             : 
     818                 :             : #if defined(WIN32) && (!defined(__MINGW32__))
     819                 :             : #define R_OK 4
     820                 :             : #endif
     821                 :             : 
     822                 :           0 :         psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
     823                 :           0 :         psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
     824                 :             : 
     825                 :             :         /* check for minor version first, then major, then no version */
     826         [ #  # ]:           0 :         if (access(psqlrc_minor, R_OK) == 0)
     827                 :           0 :                 (void) process_file(psqlrc_minor, false);
     828         [ #  # ]:           0 :         else if (access(psqlrc_major, R_OK) == 0)
     829                 :           0 :                 (void) process_file(psqlrc_major, false);
     830         [ #  # ]:           0 :         else if (access(filename, R_OK) == 0)
     831                 :           0 :                 (void) process_file(filename, false);
     832                 :             : 
     833                 :           0 :         free(psqlrc_minor);
     834                 :           0 :         free(psqlrc_major);
     835                 :           0 : }
     836                 :             : 
     837                 :             : 
     838                 :             : 
     839                 :             : /* showVersion
     840                 :             :  *
     841                 :             :  * This output format is intended to match GNU standards.
     842                 :             :  */
     843                 :             : static void
     844                 :           0 : showVersion(void)
     845                 :             : {
     846                 :           0 :         puts("psql (PostgreSQL) " PG_VERSION);
     847                 :           0 : }
     848                 :             : 
     849                 :             : 
     850                 :             : 
     851                 :             : /*
     852                 :             :  * Substitute hooks and assign hooks for psql variables.
     853                 :             :  *
     854                 :             :  * This isn't an amazingly good place for them, but neither is anywhere else.
     855                 :             :  *
     856                 :             :  * By policy, every special variable that controls any psql behavior should
     857                 :             :  * have one or both hooks, even if they're just no-ops.  This ensures that
     858                 :             :  * the variable will remain present in variables.c's list even when unset,
     859                 :             :  * which ensures that it's known to tab completion.
     860                 :             :  */
     861                 :             : 
     862                 :             : static char *
     863                 :        4088 : bool_substitute_hook(char *newval)
     864                 :             : {
     865         [ +  + ]:        4088 :         if (newval == NULL)
     866                 :             :         {
     867                 :             :                 /* "\unset FOO" becomes "\set FOO off" */
     868                 :        2721 :                 newval = pg_strdup("off");
     869                 :        2721 :         }
     870         [ +  + ]:        1367 :         else if (newval[0] == '\0')
     871                 :             :         {
     872                 :             :                 /* "\set FOO" becomes "\set FOO on" */
     873                 :           1 :                 pg_free(newval);
     874                 :           1 :                 newval = pg_strdup("on");
     875                 :           1 :         }
     876                 :        4088 :         return newval;
     877                 :             : }
     878                 :             : 
     879                 :             : static bool
     880                 :         551 : autocommit_hook(const char *newval)
     881                 :             : {
     882                 :         551 :         return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
     883                 :             : }
     884                 :             : 
     885                 :             : static bool
     886                 :         295 : on_error_stop_hook(const char *newval)
     887                 :             : {
     888                 :         295 :         return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
     889                 :             : }
     890                 :             : 
     891                 :             : static bool
     892                 :         556 : quiet_hook(const char *newval)
     893                 :             : {
     894                 :         556 :         return ParseVariableBool(newval, "QUIET", &pset.quiet);
     895                 :             : }
     896                 :             : 
     897                 :             : static bool
     898                 :         272 : singleline_hook(const char *newval)
     899                 :             : {
     900                 :         272 :         return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
     901                 :             : }
     902                 :             : 
     903                 :             : static bool
     904                 :         272 : singlestep_hook(const char *newval)
     905                 :             : {
     906                 :         272 :         return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
     907                 :             : }
     908                 :             : 
     909                 :             : static char *
     910                 :         285 : fetch_count_substitute_hook(char *newval)
     911                 :             : {
     912         [ +  + ]:         285 :         if (newval == NULL)
     913                 :         278 :                 newval = pg_strdup("0");
     914                 :         285 :         return newval;
     915                 :             : }
     916                 :             : 
     917                 :             : static bool
     918                 :         285 : fetch_count_hook(const char *newval)
     919                 :             : {
     920                 :         285 :         return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
     921                 :             : }
     922                 :             : 
     923                 :             : static bool
     924                 :         272 : histfile_hook(const char *newval)
     925                 :             : {
     926                 :             :         /*
     927                 :             :          * Someday we might try to validate the filename, but for now, this is
     928                 :             :          * just a placeholder to ensure HISTFILE is known to tab completion.
     929                 :             :          */
     930                 :         272 :         return true;
     931                 :             : }
     932                 :             : 
     933                 :             : static char *
     934                 :         272 : histsize_substitute_hook(char *newval)
     935                 :             : {
     936         [ -  + ]:         272 :         if (newval == NULL)
     937                 :         272 :                 newval = pg_strdup("500");
     938                 :         272 :         return newval;
     939                 :             : }
     940                 :             : 
     941                 :             : static bool
     942                 :         272 : histsize_hook(const char *newval)
     943                 :             : {
     944                 :         272 :         return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
     945                 :             : }
     946                 :             : 
     947                 :             : static char *
     948                 :         272 : watch_interval_substitute_hook(char *newval)
     949                 :             : {
     950         [ -  + ]:         272 :         if (newval == NULL)
     951                 :         272 :                 newval = pg_strdup(DEFAULT_WATCH_INTERVAL);
     952                 :         272 :         return newval;
     953                 :             : }
     954                 :             : 
     955                 :             : static bool
     956                 :         272 : watch_interval_hook(const char *newval)
     957                 :             : {
     958                 :         272 :         return ParseVariableDouble(newval, "WATCH_INTERVAL", &pset.watch_interval,
     959                 :             :                                                            0, DEFAULT_WATCH_INTERVAL_MAX);
     960                 :             : }
     961                 :             : 
     962                 :             : static char *
     963                 :         272 : ignoreeof_substitute_hook(char *newval)
     964                 :             : {
     965                 :         272 :         int                     dummy;
     966                 :             : 
     967                 :             :         /*
     968                 :             :          * This tries to mimic the behavior of bash, to wit "If set, the value is
     969                 :             :          * the number of consecutive EOF characters which must be typed as the
     970                 :             :          * first characters on an input line before bash exits.  If the variable
     971                 :             :          * exists but does not have a numeric value, or has no value, the default
     972                 :             :          * value is 10.  If it does not exist, EOF signifies the end of input to
     973                 :             :          * the shell."  Unlike bash, however, we insist on the stored value
     974                 :             :          * actually being a valid integer.
     975                 :             :          */
     976         [ -  + ]:         272 :         if (newval == NULL)
     977                 :         272 :                 newval = pg_strdup("0");
     978         [ #  # ]:           0 :         else if (!ParseVariableNum(newval, NULL, &dummy))
     979                 :           0 :                 newval = pg_strdup("10");
     980                 :         544 :         return newval;
     981                 :         272 : }
     982                 :             : 
     983                 :             : static bool
     984                 :         272 : ignoreeof_hook(const char *newval)
     985                 :             : {
     986                 :         272 :         return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
     987                 :             : }
     988                 :             : 
     989                 :             : static char *
     990                 :         524 : echo_substitute_hook(char *newval)
     991                 :             : {
     992         [ +  + ]:         524 :         if (newval == NULL)
     993                 :         272 :                 newval = pg_strdup("none");
     994                 :         524 :         return newval;
     995                 :             : }
     996                 :             : 
     997                 :             : static bool
     998                 :         524 : echo_hook(const char *newval)
     999                 :             : {
    1000         [ +  - ]:         524 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1001         [ +  - ]:         524 :         if (pg_strcasecmp(newval, "queries") == 0)
    1002                 :           0 :                 pset.echo = PSQL_ECHO_QUERIES;
    1003         [ +  + ]:         524 :         else if (pg_strcasecmp(newval, "errors") == 0)
    1004                 :           1 :                 pset.echo = PSQL_ECHO_ERRORS;
    1005         [ +  + ]:         523 :         else if (pg_strcasecmp(newval, "all") == 0)
    1006                 :         249 :                 pset.echo = PSQL_ECHO_ALL;
    1007         [ -  + ]:         274 :         else if (pg_strcasecmp(newval, "none") == 0)
    1008                 :         274 :                 pset.echo = PSQL_ECHO_NONE;
    1009                 :             :         else
    1010                 :             :         {
    1011                 :           0 :                 PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
    1012                 :           0 :                 return false;
    1013                 :             :         }
    1014                 :         524 :         return true;
    1015                 :         524 : }
    1016                 :             : 
    1017                 :             : static bool
    1018                 :         272 : echo_hidden_hook(const char *newval)
    1019                 :             : {
    1020         [ +  - ]:         272 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1021         [ +  - ]:         272 :         if (pg_strcasecmp(newval, "noexec") == 0)
    1022                 :           0 :                 pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
    1023                 :             :         else
    1024                 :             :         {
    1025                 :         272 :                 bool            on_off;
    1026                 :             : 
    1027         [ +  - ]:         272 :                 if (ParseVariableBool(newval, NULL, &on_off))
    1028                 :         272 :                         pset.echo_hidden = on_off ? PSQL_ECHO_HIDDEN_ON : PSQL_ECHO_HIDDEN_OFF;
    1029                 :             :                 else
    1030                 :             :                 {
    1031                 :           0 :                         PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
    1032                 :           0 :                         return false;
    1033                 :             :                 }
    1034      [ -  -  + ]:         272 :         }
    1035                 :         272 :         return true;
    1036                 :         272 : }
    1037                 :             : 
    1038                 :             : static bool
    1039                 :         280 : on_error_rollback_hook(const char *newval)
    1040                 :             : {
    1041         [ +  - ]:         280 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1042         [ +  - ]:         280 :         if (pg_strcasecmp(newval, "interactive") == 0)
    1043                 :           0 :                 pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
    1044                 :             :         else
    1045                 :             :         {
    1046                 :         280 :                 bool            on_off;
    1047                 :             : 
    1048         [ +  + ]:         280 :                 if (ParseVariableBool(newval, NULL, &on_off))
    1049                 :         279 :                         pset.on_error_rollback = on_off ? PSQL_ERROR_ROLLBACK_ON : PSQL_ERROR_ROLLBACK_OFF;
    1050                 :             :                 else
    1051                 :             :                 {
    1052                 :           1 :                         PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
    1053                 :           1 :                         return false;
    1054                 :             :                 }
    1055      [ -  +  + ]:         280 :         }
    1056                 :         279 :         return true;
    1057                 :         280 : }
    1058                 :             : 
    1059                 :             : static char *
    1060                 :         272 : comp_keyword_case_substitute_hook(char *newval)
    1061                 :             : {
    1062         [ -  + ]:         272 :         if (newval == NULL)
    1063                 :         272 :                 newval = pg_strdup("preserve-upper");
    1064                 :         272 :         return newval;
    1065                 :             : }
    1066                 :             : 
    1067                 :             : static bool
    1068                 :         272 : comp_keyword_case_hook(const char *newval)
    1069                 :             : {
    1070         [ +  - ]:         272 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1071         [ -  + ]:         272 :         if (pg_strcasecmp(newval, "preserve-upper") == 0)
    1072                 :         272 :                 pset.comp_case = PSQL_COMP_CASE_PRESERVE_UPPER;
    1073         [ #  # ]:           0 :         else if (pg_strcasecmp(newval, "preserve-lower") == 0)
    1074                 :           0 :                 pset.comp_case = PSQL_COMP_CASE_PRESERVE_LOWER;
    1075         [ #  # ]:           0 :         else if (pg_strcasecmp(newval, "upper") == 0)
    1076                 :           0 :                 pset.comp_case = PSQL_COMP_CASE_UPPER;
    1077         [ #  # ]:           0 :         else if (pg_strcasecmp(newval, "lower") == 0)
    1078                 :           0 :                 pset.comp_case = PSQL_COMP_CASE_LOWER;
    1079                 :             :         else
    1080                 :             :         {
    1081                 :           0 :                 PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
    1082                 :             :                                                  "lower, upper, preserve-lower, preserve-upper");
    1083                 :           0 :                 return false;
    1084                 :             :         }
    1085                 :         272 :         return true;
    1086                 :         272 : }
    1087                 :             : 
    1088                 :             : static char *
    1089                 :         272 : histcontrol_substitute_hook(char *newval)
    1090                 :             : {
    1091         [ -  + ]:         272 :         if (newval == NULL)
    1092                 :         272 :                 newval = pg_strdup("none");
    1093                 :         272 :         return newval;
    1094                 :             : }
    1095                 :             : 
    1096                 :             : static bool
    1097                 :         272 : histcontrol_hook(const char *newval)
    1098                 :             : {
    1099         [ +  - ]:         272 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1100         [ +  - ]:         272 :         if (pg_strcasecmp(newval, "ignorespace") == 0)
    1101                 :           0 :                 pset.histcontrol = hctl_ignorespace;
    1102         [ +  - ]:         272 :         else if (pg_strcasecmp(newval, "ignoredups") == 0)
    1103                 :           0 :                 pset.histcontrol = hctl_ignoredups;
    1104         [ +  - ]:         272 :         else if (pg_strcasecmp(newval, "ignoreboth") == 0)
    1105                 :           0 :                 pset.histcontrol = hctl_ignoreboth;
    1106         [ -  + ]:         272 :         else if (pg_strcasecmp(newval, "none") == 0)
    1107                 :         272 :                 pset.histcontrol = hctl_none;
    1108                 :             :         else
    1109                 :             :         {
    1110                 :           0 :                 PsqlVarEnumError("HISTCONTROL", newval,
    1111                 :             :                                                  "none, ignorespace, ignoredups, ignoreboth");
    1112                 :           0 :                 return false;
    1113                 :             :         }
    1114                 :         272 :         return true;
    1115                 :         272 : }
    1116                 :             : 
    1117                 :             : static bool
    1118                 :         544 : prompt1_hook(const char *newval)
    1119                 :             : {
    1120         [ +  + ]:         544 :         pset.prompt1 = newval ? newval : "";
    1121                 :         544 :         return true;
    1122                 :             : }
    1123                 :             : 
    1124                 :             : static bool
    1125                 :         544 : prompt2_hook(const char *newval)
    1126                 :             : {
    1127         [ +  + ]:         544 :         pset.prompt2 = newval ? newval : "";
    1128                 :         544 :         return true;
    1129                 :             : }
    1130                 :             : 
    1131                 :             : static bool
    1132                 :         544 : prompt3_hook(const char *newval)
    1133                 :             : {
    1134         [ +  + ]:         544 :         pset.prompt3 = newval ? newval : "";
    1135                 :         544 :         return true;
    1136                 :             : }
    1137                 :             : 
    1138                 :             : static char *
    1139                 :         291 : verbosity_substitute_hook(char *newval)
    1140                 :             : {
    1141         [ +  + ]:         291 :         if (newval == NULL)
    1142                 :         272 :                 newval = pg_strdup("default");
    1143                 :         291 :         return newval;
    1144                 :             : }
    1145                 :             : 
    1146                 :             : static bool
    1147                 :         291 : verbosity_hook(const char *newval)
    1148                 :             : {
    1149         [ +  - ]:         291 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1150         [ +  + ]:         291 :         if (pg_strcasecmp(newval, "default") == 0)
    1151                 :         280 :                 pset.verbosity = PQERRORS_DEFAULT;
    1152         [ +  - ]:          11 :         else if (pg_strcasecmp(newval, "verbose") == 0)
    1153                 :           0 :                 pset.verbosity = PQERRORS_VERBOSE;
    1154         [ +  + ]:          11 :         else if (pg_strcasecmp(newval, "terse") == 0)
    1155                 :           6 :                 pset.verbosity = PQERRORS_TERSE;
    1156         [ -  + ]:           5 :         else if (pg_strcasecmp(newval, "sqlstate") == 0)
    1157                 :           5 :                 pset.verbosity = PQERRORS_SQLSTATE;
    1158                 :             :         else
    1159                 :             :         {
    1160                 :           0 :                 PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
    1161                 :           0 :                 return false;
    1162                 :             :         }
    1163                 :             : 
    1164         [ +  + ]:         291 :         if (pset.db)
    1165                 :          19 :                 PQsetErrorVerbosity(pset.db, pset.verbosity);
    1166                 :         291 :         return true;
    1167                 :         291 : }
    1168                 :             : 
    1169                 :             : static bool
    1170                 :         546 : show_all_results_hook(const char *newval)
    1171                 :             : {
    1172                 :         546 :         return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
    1173                 :             : }
    1174                 :             : 
    1175                 :             : static char *
    1176                 :         277 : show_context_substitute_hook(char *newval)
    1177                 :             : {
    1178         [ +  + ]:         277 :         if (newval == NULL)
    1179                 :         272 :                 newval = pg_strdup("errors");
    1180                 :         277 :         return newval;
    1181                 :             : }
    1182                 :             : 
    1183                 :             : static bool
    1184                 :         277 : show_context_hook(const char *newval)
    1185                 :             : {
    1186         [ +  - ]:         277 :         Assert(newval != NULL);         /* else substitute hook messed up */
    1187         [ +  + ]:         277 :         if (pg_strcasecmp(newval, "never") == 0)
    1188                 :           1 :                 pset.show_context = PQSHOW_CONTEXT_NEVER;
    1189         [ +  + ]:         276 :         else if (pg_strcasecmp(newval, "errors") == 0)
    1190                 :         274 :                 pset.show_context = PQSHOW_CONTEXT_ERRORS;
    1191         [ -  + ]:           2 :         else if (pg_strcasecmp(newval, "always") == 0)
    1192                 :           2 :                 pset.show_context = PQSHOW_CONTEXT_ALWAYS;
    1193                 :             :         else
    1194                 :             :         {
    1195                 :           0 :                 PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
    1196                 :           0 :                 return false;
    1197                 :             :         }
    1198                 :             : 
    1199         [ +  + ]:         277 :         if (pset.db)
    1200                 :           5 :                 PQsetErrorContextVisibility(pset.db, pset.show_context);
    1201                 :         277 :         return true;
    1202                 :         277 : }
    1203                 :             : 
    1204                 :             : static bool
    1205                 :         524 : hide_compression_hook(const char *newval)
    1206                 :             : {
    1207                 :         524 :         return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
    1208                 :             :                                                          &pset.hide_compression);
    1209                 :             : }
    1210                 :             : 
    1211                 :             : static bool
    1212                 :         520 : hide_tableam_hook(const char *newval)
    1213                 :             : {
    1214                 :         520 :         return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
    1215                 :             : }
    1216                 :             : 
    1217                 :             : static void
    1218                 :         272 : EstablishVariableSpace(void)
    1219                 :             : {
    1220                 :         272 :         pset.vars = CreateVariableSpace();
    1221                 :             : 
    1222                 :         272 :         SetVariableHooks(pset.vars, "AUTOCOMMIT",
    1223                 :             :                                          bool_substitute_hook,
    1224                 :             :                                          autocommit_hook);
    1225                 :         272 :         SetVariableHooks(pset.vars, "ON_ERROR_STOP",
    1226                 :             :                                          bool_substitute_hook,
    1227                 :             :                                          on_error_stop_hook);
    1228                 :         272 :         SetVariableHooks(pset.vars, "QUIET",
    1229                 :             :                                          bool_substitute_hook,
    1230                 :             :                                          quiet_hook);
    1231                 :         272 :         SetVariableHooks(pset.vars, "SINGLELINE",
    1232                 :             :                                          bool_substitute_hook,
    1233                 :             :                                          singleline_hook);
    1234                 :         272 :         SetVariableHooks(pset.vars, "SINGLESTEP",
    1235                 :             :                                          bool_substitute_hook,
    1236                 :             :                                          singlestep_hook);
    1237                 :         272 :         SetVariableHooks(pset.vars, "FETCH_COUNT",
    1238                 :             :                                          fetch_count_substitute_hook,
    1239                 :             :                                          fetch_count_hook);
    1240                 :         272 :         SetVariableHooks(pset.vars, "HISTFILE",
    1241                 :             :                                          NULL,
    1242                 :             :                                          histfile_hook);
    1243                 :         272 :         SetVariableHooks(pset.vars, "HISTSIZE",
    1244                 :             :                                          histsize_substitute_hook,
    1245                 :             :                                          histsize_hook);
    1246                 :         272 :         SetVariableHooks(pset.vars, "IGNOREEOF",
    1247                 :             :                                          ignoreeof_substitute_hook,
    1248                 :             :                                          ignoreeof_hook);
    1249                 :         272 :         SetVariableHooks(pset.vars, "ECHO",
    1250                 :             :                                          echo_substitute_hook,
    1251                 :             :                                          echo_hook);
    1252                 :         272 :         SetVariableHooks(pset.vars, "ECHO_HIDDEN",
    1253                 :             :                                          bool_substitute_hook,
    1254                 :             :                                          echo_hidden_hook);
    1255                 :         272 :         SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
    1256                 :             :                                          bool_substitute_hook,
    1257                 :             :                                          on_error_rollback_hook);
    1258                 :         272 :         SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
    1259                 :             :                                          comp_keyword_case_substitute_hook,
    1260                 :             :                                          comp_keyword_case_hook);
    1261                 :         272 :         SetVariableHooks(pset.vars, "HISTCONTROL",
    1262                 :             :                                          histcontrol_substitute_hook,
    1263                 :             :                                          histcontrol_hook);
    1264                 :         272 :         SetVariableHooks(pset.vars, "PROMPT1",
    1265                 :             :                                          NULL,
    1266                 :             :                                          prompt1_hook);
    1267                 :         272 :         SetVariableHooks(pset.vars, "PROMPT2",
    1268                 :             :                                          NULL,
    1269                 :             :                                          prompt2_hook);
    1270                 :         272 :         SetVariableHooks(pset.vars, "PROMPT3",
    1271                 :             :                                          NULL,
    1272                 :             :                                          prompt3_hook);
    1273                 :         272 :         SetVariableHooks(pset.vars, "VERBOSITY",
    1274                 :             :                                          verbosity_substitute_hook,
    1275                 :             :                                          verbosity_hook);
    1276                 :         272 :         SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
    1277                 :             :                                          bool_substitute_hook,
    1278                 :             :                                          show_all_results_hook);
    1279                 :         272 :         SetVariableHooks(pset.vars, "SHOW_CONTEXT",
    1280                 :             :                                          show_context_substitute_hook,
    1281                 :             :                                          show_context_hook);
    1282                 :         272 :         SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
    1283                 :             :                                          bool_substitute_hook,
    1284                 :             :                                          hide_compression_hook);
    1285                 :         272 :         SetVariableHooks(pset.vars, "HIDE_TABLEAM",
    1286                 :             :                                          bool_substitute_hook,
    1287                 :             :                                          hide_tableam_hook);
    1288                 :         272 :         SetVariableHooks(pset.vars, "WATCH_INTERVAL",
    1289                 :             :                                          watch_interval_substitute_hook,
    1290                 :             :                                          watch_interval_hook);
    1291                 :         272 : }
        

Generated by: LCOV version 2.3.2-1