LCOV - differential code coverage report
Current view: top level - src/bin/psql - startup.c (source / functions) Coverage Total Hit UBC GNC CBC DCB
Current: Differential Code Coverage HEAD vs 15 Lines: 74.6 % 575 429 146 3 426 3
Current Date: 2023-04-08 15:15:32 Functions: 95.1 % 41 39 2 2 37
Baseline: 15
Baseline Date: 2023-04-08 15:09:40
Legend: Lines: hit not hit

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

Generated by: LCOV version v1.16-55-g56c0a2a