LCOV - code coverage report
Current view: top level - src/bin/psql - startup.c (source / functions) Hit Total Coverage
Test: PostgreSQL 17devel Lines: 421 566 74.4 %
Date: 2024-04-24 12:11:17 Functions: 39 41 95.1 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * psql - the PostgreSQL interactive terminal
       3             :  *
       4             :  * Copyright (c) 2000-2024, 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       60630 : log_pre_callback(void)
      93             : {
      94       60630 :     if (pset.queryFout && pset.queryFout != stdout)
      95           0 :         fflush(pset.queryFout);
      96       60630 : }
      97             : 
      98             : static void
      99       60630 : log_locus_callback(const char **filename, uint64 *lineno)
     100             : {
     101       60630 :     if (pset.inputfile)
     102             :     {
     103         808 :         *filename = pset.inputfile;
     104         808 :         *lineno = pset.lineno;
     105             :     }
     106             :     else
     107             :     {
     108       59822 :         *filename = NULL;
     109       59822 :         *lineno = 0;
     110             :     }
     111       60630 : }
     112             : 
     113             : #ifndef WIN32
     114             : static void
     115           8 : empty_signal_handler(SIGNAL_ARGS)
     116             : {
     117           8 : }
     118             : #endif
     119             : 
     120             : /*
     121             :  *
     122             :  * main
     123             :  *
     124             :  */
     125             : int
     126       14592 : main(int argc, char *argv[])
     127             : {
     128             :     struct adhoc_opts options;
     129             :     int         successResult;
     130       14592 :     char       *password = NULL;
     131             :     bool        new_pass;
     132             : 
     133       14592 :     pg_logging_init(argv[0]);
     134       14592 :     pg_logging_set_pre_callback(log_pre_callback);
     135       14592 :     pg_logging_set_locus_callback(log_locus_callback);
     136       14592 :     set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
     137             : 
     138       14592 :     if (argc > 1)
     139             :     {
     140       14592 :         if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
     141             :         {
     142           2 :             usage(NOPAGER);
     143           2 :             exit(EXIT_SUCCESS);
     144             :         }
     145       14590 :         if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
     146             :         {
     147          20 :             showVersion();
     148          20 :             exit(EXIT_SUCCESS);
     149             :         }
     150             :     }
     151             : 
     152       14570 :     pset.progname = get_progname(argv[0]);
     153             : 
     154       14570 :     pset.db = NULL;
     155       14570 :     pset.dead_conn = NULL;
     156       14570 :     setDecimalLocale();
     157       14570 :     pset.encoding = PQenv2encoding();
     158       14570 :     pset.queryFout = stdout;
     159       14570 :     pset.queryFoutPipe = false;
     160       14570 :     pset.copyStream = NULL;
     161       14570 :     pset.last_error_result = NULL;
     162       14570 :     pset.cur_cmd_source = stdin;
     163       14570 :     pset.cur_cmd_interactive = false;
     164             : 
     165             :     /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
     166       14570 :     pset.popt.topt.format = PRINT_ALIGNED;
     167       14570 :     pset.popt.topt.border = 1;
     168       14570 :     pset.popt.topt.pager = 1;
     169       14570 :     pset.popt.topt.pager_min_lines = 0;
     170       14570 :     pset.popt.topt.start_table = true;
     171       14570 :     pset.popt.topt.stop_table = true;
     172       14570 :     pset.popt.topt.default_footer = true;
     173             : 
     174       14570 :     pset.popt.topt.csvFieldSep[0] = DEFAULT_CSV_FIELD_SEP;
     175       14570 :     pset.popt.topt.csvFieldSep[1] = '\0';
     176             : 
     177       14570 :     pset.popt.topt.unicode_border_linestyle = UNICODE_LINESTYLE_SINGLE;
     178       14570 :     pset.popt.topt.unicode_column_linestyle = UNICODE_LINESTYLE_SINGLE;
     179       14570 :     pset.popt.topt.unicode_header_linestyle = UNICODE_LINESTYLE_SINGLE;
     180             : 
     181       14570 :     refresh_utf8format(&(pset.popt.topt));
     182             : 
     183             :     /* We must get COLUMNS here before readline() sets it */
     184       14570 :     pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
     185             : 
     186       14570 :     pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
     187             : 
     188       14570 :     pset.getPassword = TRI_DEFAULT;
     189             : 
     190       14570 :     EstablishVariableSpace();
     191             : 
     192             :     /* Create variables showing psql version number */
     193       14570 :     SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
     194       14570 :     SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
     195       14570 :     SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
     196             : 
     197             :     /* Initialize variables for last error */
     198       14570 :     SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
     199       14570 :     SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
     200             : 
     201             :     /* Default values for variables (that don't match the result of \unset) */
     202       14570 :     SetVariableBool(pset.vars, "AUTOCOMMIT");
     203       14570 :     SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
     204       14570 :     SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
     205       14570 :     SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
     206       14570 :     SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
     207             : 
     208       14570 :     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       14564 :     if (options.actions.head == NULL && pset.notty)
     216        3948 :         simple_action_list_append(&options.actions, ACT_FILE, NULL);
     217             : 
     218             :     /* Bail out if -1 was specified but will be ignored. */
     219       14564 :     if (options.single_txn && options.actions.head == NULL)
     220           0 :         pg_fatal("-1 can only be used in non-interactive mode");
     221             : 
     222       14564 :     if (!pset.popt.topt.fieldSep.separator &&
     223       14560 :         !pset.popt.topt.fieldSep.separator_zero)
     224             :     {
     225       14560 :         pset.popt.topt.fieldSep.separator = pg_strdup(DEFAULT_FIELD_SEP);
     226       14560 :         pset.popt.topt.fieldSep.separator_zero = false;
     227             :     }
     228       14564 :     if (!pset.popt.topt.recordSep.separator &&
     229       14564 :         !pset.popt.topt.recordSep.separator_zero)
     230             :     {
     231       14564 :         pset.popt.topt.recordSep.separator = pg_strdup(DEFAULT_RECORD_SEP);
     232       14564 :         pset.popt.topt.recordSep.separator_zero = false;
     233             :     }
     234             : 
     235       14564 :     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           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       14564 :         const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
     251       14564 :         const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
     252             : 
     253       14564 :         keywords[0] = "host";
     254       14564 :         values[0] = options.host;
     255       14564 :         keywords[1] = "port";
     256       14564 :         values[1] = options.port;
     257       14564 :         keywords[2] = "user";
     258       14564 :         values[2] = options.username;
     259       14564 :         keywords[3] = "password";
     260       14564 :         values[3] = password;
     261       14564 :         keywords[4] = "dbname"; /* see do_connect() */
     262           0 :         values[4] = (options.list_dbs && options.dbname == NULL) ?
     263       14564 :             "postgres" : options.dbname;
     264       14564 :         keywords[5] = "fallback_application_name";
     265       14564 :         values[5] = pset.progname;
     266       14564 :         keywords[6] = "client_encoding";
     267       14564 :         values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
     268       14564 :         keywords[7] = NULL;
     269       14564 :         values[7] = NULL;
     270             : 
     271       14564 :         new_pass = false;
     272       14564 :         pset.db = PQconnectdbParams(keywords, values, true);
     273       14564 :         free(keywords);
     274       14564 :         free(values);
     275             : 
     276       14812 :         if (PQstatus(pset.db) == CONNECTION_BAD &&
     277         250 :             PQconnectionNeedsPassword(pset.db) &&
     278           2 :             !password &&
     279           2 :             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           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       14564 :     } while (new_pass);
     301             : 
     302       14564 :     if (PQstatus(pset.db) == CONNECTION_BAD)
     303             :     {
     304         248 :         pg_log_error("%s", PQerrorMessage(pset.db));
     305         248 :         PQfinish(pset.db);
     306         248 :         exit(EXIT_BADCONN);
     307             :     }
     308             : 
     309       14316 :     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       14316 :     pqsignal(SIGCHLD, empty_signal_handler);
     320       14316 :     pqsignal(SIGALRM, empty_signal_handler);
     321             : #endif
     322             : 
     323       14316 :     PQsetNoticeProcessor(pset.db, NoticeProcessor, NULL);
     324             : 
     325       14316 :     SyncVariables();
     326             : 
     327       14316 :     if (options.list_dbs)
     328             :     {
     329             :         int         success;
     330             : 
     331           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       14316 :     if (options.logfilename)
     340             :     {
     341           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       14316 :     if (!options.no_psqlrc)
     348           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       14316 :     if (options.actions.head != NULL)
     355             :     {
     356             :         PGresult   *res;
     357             :         SimpleActionListCell *cell;
     358             : 
     359       14312 :         successResult = EXIT_SUCCESS;   /* silence compiler */
     360             : 
     361       14312 :         if (options.single_txn)
     362             :         {
     363          14 :             if ((res = PSQLexec("BEGIN")) == NULL)
     364             :             {
     365           0 :                 if (pset.on_error_stop)
     366             :                 {
     367           0 :                     successResult = EXIT_USER;
     368           0 :                     goto error;
     369             :                 }
     370             :             }
     371             :             else
     372          14 :                 PQclear(res);
     373             :         }
     374             : 
     375       28742 :         for (cell = options.actions.head; cell; cell = cell->next)
     376             :         {
     377       14556 :             if (cell->action == ACT_SINGLE_QUERY)
     378             :             {
     379         708 :                 pg_logging_config(PG_LOG_FLAG_TERSE);
     380             : 
     381         708 :                 if (pset.echo == PSQL_ECHO_ALL)
     382           0 :                     puts(cell->val);
     383             : 
     384         708 :                 successResult = SendQuery(cell->val)
     385         706 :                     ? EXIT_SUCCESS : EXIT_FAILURE;
     386             :             }
     387       13848 :             else if (cell->action == ACT_SINGLE_SLASH)
     388             :             {
     389             :                 PsqlScanState scan_state;
     390             :                 ConditionalStack cond_stack;
     391             : 
     392           4 :                 pg_logging_config(PG_LOG_FLAG_TERSE);
     393             : 
     394           4 :                 if (pset.echo == PSQL_ECHO_ALL)
     395           0 :                     puts(cell->val);
     396             : 
     397           4 :                 scan_state = psql_scan_create(&psqlscan_callbacks);
     398           4 :                 psql_scan_setup(scan_state,
     399           4 :                                 cell->val, strlen(cell->val),
     400           4 :                                 pset.encoding, standard_strings());
     401           4 :                 cond_stack = conditional_stack_create();
     402           4 :                 psql_scan_set_passthrough(scan_state, (void *) cond_stack);
     403             : 
     404           4 :                 successResult = HandleSlashCmds(scan_state,
     405             :                                                 cond_stack,
     406             :                                                 NULL,
     407             :                                                 NULL) != PSQL_CMD_ERROR
     408           4 :                     ? EXIT_SUCCESS : EXIT_FAILURE;
     409             : 
     410           4 :                 psql_scan_destroy(scan_state);
     411           4 :                 conditional_stack_destroy(cond_stack);
     412             :             }
     413       13844 :             else if (cell->action == ACT_FILE)
     414             :             {
     415       13844 :                 successResult = process_file(cell->val, false);
     416             :             }
     417             :             else
     418             :             {
     419             :                 /* should never come here */
     420             :                 Assert(false);
     421             :             }
     422             : 
     423       14540 :             if (successResult != EXIT_SUCCESS && pset.on_error_stop)
     424         110 :                 break;
     425             :         }
     426             : 
     427       14296 :         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          14 :             res = PSQLexec((successResult != EXIT_SUCCESS && pset.on_error_stop) ?
     435             :                            "ROLLBACK" : "COMMIT");
     436          14 :             if (res == NULL)
     437             :             {
     438           0 :                 if (pset.on_error_stop)
     439             :                 {
     440           0 :                     successResult = EXIT_USER;
     441           0 :                     goto error;
     442             :                 }
     443             :             }
     444             :             else
     445          14 :                 PQclear(res);
     446             :         }
     447             : 
     448       14296 : error:
     449             :         ;
     450             :     }
     451             : 
     452             :     /*
     453             :      * or otherwise enter interactive main loop
     454             :      */
     455             :     else
     456             :     {
     457           4 :         pg_logging_config(PG_LOG_FLAG_TERSE);
     458           4 :         connection_warnings(true);
     459           4 :         if (!pset.quiet)
     460           4 :             printf(_("Type \"help\" for help.\n\n"));
     461           4 :         initializeInput(options.no_readline ? 0 : 1);
     462           4 :         successResult = MainLoop(stdin);
     463             :     }
     464             : 
     465             :     /* clean up */
     466       14300 :     if (pset.logfile)
     467           0 :         fclose(pset.logfile);
     468       14300 :     if (pset.db)
     469       14300 :         PQfinish(pset.db);
     470       14300 :     if (pset.dead_conn)
     471           0 :         PQfinish(pset.dead_conn);
     472       14300 :     setQFout(NULL);
     473             : 
     474       14300 :     return successResult;
     475             : }
     476             : 
     477             : 
     478             : /*
     479             :  * Parse command line options
     480             :  */
     481             : 
     482             : static void
     483       14570 : 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       14570 :     memset(options, 0, sizeof *options);
     530             : 
     531      107342 :     while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
     532             :                             long_options, &optindex)) != -1)
     533             :     {
     534       92778 :         switch (c)
     535             :         {
     536        1906 :             case 'a':
     537        1906 :                 SetVariable(pset.vars, "ECHO", "all");
     538        1906 :                 break;
     539       12206 :             case 'A':
     540       12206 :                 pset.popt.topt.format = PRINT_UNALIGNED;
     541       12206 :                 break;
     542           0 :             case 'b':
     543           0 :                 SetVariable(pset.vars, "ECHO", "errors");
     544           0 :                 break;
     545         720 :             case 'c':
     546         720 :                 if (optarg[0] == '\\')
     547           4 :                     simple_action_list_append(&options->actions,
     548             :                                               ACT_SINGLE_SLASH,
     549           4 :                                               optarg + 1);
     550             :                 else
     551         716 :                     simple_action_list_append(&options->actions,
     552             :                                               ACT_SINGLE_QUERY,
     553             :                                               optarg);
     554         720 :                 break;
     555       14402 :             case 'd':
     556       14402 :                 options->dbname = pg_strdup(optarg);
     557       14402 :                 break;
     558           6 :             case 'e':
     559           6 :                 SetVariable(pset.vars, "ECHO", "queries");
     560           6 :                 break;
     561           0 :             case 'E':
     562           0 :                 SetVariableBool(pset.vars, "ECHO_HIDDEN");
     563           0 :                 break;
     564       10144 :             case 'f':
     565       10144 :                 simple_action_list_append(&options->actions,
     566             :                                           ACT_FILE,
     567             :                                           optarg);
     568       10144 :                 break;
     569           4 :             case 'F':
     570           4 :                 pset.popt.topt.fieldSep.separator = pg_strdup(optarg);
     571           4 :                 pset.popt.topt.fieldSep.separator_zero = false;
     572           4 :                 break;
     573           6 :             case 'h':
     574           6 :                 options->host = pg_strdup(optarg);
     575           6 :                 break;
     576           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           8 :             case 'p':
     593           8 :                 options->port = pg_strdup(optarg);
     594           8 :                 break;
     595           4 :             case 'P':
     596             :                 {
     597             :                     char       *value;
     598             :                     char       *equal_loc;
     599             :                     bool        result;
     600             : 
     601           4 :                     value = pg_strdup(optarg);
     602           4 :                     equal_loc = strchr(value, '=');
     603           4 :                     if (!equal_loc)
     604           0 :                         result = do_pset(value, NULL, &pset.popt, true);
     605             :                     else
     606             :                     {
     607           4 :                         *equal_loc = '\0';
     608           4 :                         result = do_pset(value, equal_loc + 1, &pset.popt, true);
     609             :                     }
     610             : 
     611           4 :                     if (!result)
     612           0 :                         pg_fatal("could not set printing parameter \"%s\"", value);
     613             : 
     614           4 :                     free(value);
     615           4 :                     break;
     616             :                 }
     617       12476 :             case 'q':
     618       12476 :                 SetVariableBool(pset.vars, "QUIET");
     619       12476 :                 break;
     620           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       12192 :             case 't':
     631       12192 :                 pset.popt.topt.tuples_only = true;
     632       12192 :                 break;
     633           0 :             case 'T':
     634           0 :                 pset.popt.topt.tableAttr = pg_strdup(optarg);
     635           0 :                 break;
     636          10 :             case 'U':
     637          10 :                 options->username = pg_strdup(optarg);
     638          10 :                 break;
     639       13508 :             case 'v':
     640             :                 {
     641             :                     char       *value;
     642             :                     char       *equal_loc;
     643             : 
     644       13508 :                     value = pg_strdup(optarg);
     645       13508 :                     equal_loc = strchr(value, '=');
     646       13508 :                     if (!equal_loc)
     647             :                     {
     648           0 :                         if (!DeleteVariable(pset.vars, value))
     649           0 :                             exit(EXIT_FAILURE); /* error already printed */
     650             :                     }
     651             :                     else
     652             :                     {
     653       13508 :                         *equal_loc = '\0';
     654       13508 :                         if (!SetVariable(pset.vars, value, equal_loc + 1))
     655           0 :                             exit(EXIT_FAILURE); /* error already printed */
     656             :                     }
     657             : 
     658       13508 :                     free(value);
     659       13508 :                     break;
     660             :                 }
     661           0 :             case 'V':
     662           0 :                 showVersion();
     663           0 :                 exit(EXIT_SUCCESS);
     664         602 :             case 'w':
     665         602 :                 pset.getPassword = TRI_NO;
     666         602 :                 break;
     667           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       14564 :             case 'X':
     674       14564 :                 options->no_psqlrc = true;
     675       14564 :                 break;
     676           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          14 :             case '1':
     683          14 :                 options->single_txn = true;
     684          14 :                 break;
     685           2 :             case '?':
     686           2 :                 if (optind <= argc &&
     687           2 :                     strcmp(argv[optind - 1], "-?") == 0)
     688             :                 {
     689             :                     /* actual help option given */
     690           0 :                     usage(NOPAGER);
     691           0 :                     exit(EXIT_SUCCESS);
     692             :                 }
     693             :                 else
     694             :                 {
     695             :                     /* getopt error (unknown option or missing argument) */
     696           2 :                     goto unknown_option;
     697             :                 }
     698             :                 break;
     699           4 :             case 1:
     700             :                 {
     701           4 :                     if (!optarg || strcmp(optarg, "options") == 0)
     702           0 :                         usage(NOPAGER);
     703           4 :                     else if (optarg && strcmp(optarg, "commands") == 0)
     704           2 :                         slashUsage(NOPAGER);
     705           2 :                     else if (optarg && strcmp(optarg, "variables") == 0)
     706           2 :                         helpVariables(NOPAGER);
     707             :                     else
     708           0 :                         goto unknown_option;
     709             : 
     710           4 :                     exit(EXIT_SUCCESS);
     711             :                 }
     712             :                 break;
     713           0 :             case 2:
     714           0 :                 pset.popt.topt.format = PRINT_CSV;
     715           0 :                 break;
     716             :             default:
     717           2 :         unknown_option:
     718             :                 /* getopt_long already emitted a complaint */
     719           2 :                 pg_log_error_hint("Try \"%s --help\" for more information.",
     720             :                                   pset.progname);
     721           2 :                 exit(EXIT_FAILURE);
     722             :         }
     723             :     }
     724             : 
     725             :     /*
     726             :      * if we still have arguments, use it as the database name and username
     727             :      */
     728       14760 :     while (argc - optind >= 1)
     729             :     {
     730         196 :         if (!options->dbname)
     731         196 :             options->dbname = argv[optind];
     732           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         196 :         optind++;
     739             :     }
     740       14564 : }
     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       14812 : simple_action_list_append(SimpleActionList *list,
     749             :                           enum _actions action, const char *val)
     750             : {
     751             :     SimpleActionListCell *cell;
     752             : 
     753       14812 :     cell = pg_malloc_object(SimpleActionListCell);
     754             : 
     755       14812 :     cell->next = NULL;
     756       14812 :     cell->action = action;
     757       14812 :     if (val)
     758       10864 :         cell->val = pg_strdup(val);
     759             :     else
     760        3948 :         cell->val = NULL;
     761             : 
     762       14812 :     if (list->tail)
     763         252 :         list->tail->next = cell;
     764             :     else
     765       14560 :         list->head = cell;
     766       14812 :     list->tail = cell;
     767       14812 : }
     768             : 
     769             : 
     770             : /*
     771             :  * Load .psqlrc file, if found.
     772             :  */
     773             : static void
     774           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          20 : showVersion(void)
     840             : {
     841          20 :     puts("psql (PostgreSQL) " PG_VERSION);
     842          20 : }
     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      201024 : bool_substitute_hook(char *newval)
     859             : {
     860      201024 :     if (newval == NULL)
     861             :     {
     862             :         /* "\unset FOO" becomes "\set FOO off" */
     863      145706 :         newval = pg_strdup("off");
     864             :     }
     865       55318 :     else if (newval[0] == '\0')
     866             :     {
     867             :         /* "\set FOO" becomes "\set FOO on" */
     868           6 :         pg_free(newval);
     869           6 :         newval = pg_strdup("on");
     870             :     }
     871      201024 :     return newval;
     872             : }
     873             : 
     874             : static bool
     875       29182 : autocommit_hook(const char *newval)
     876             : {
     877       29182 :     return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
     878             : }
     879             : 
     880             : static bool
     881       24266 : on_error_stop_hook(const char *newval)
     882             : {
     883       24266 :     return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
     884             : }
     885             : 
     886             : static bool
     887       27118 : quiet_hook(const char *newval)
     888             : {
     889       27118 :     return ParseVariableBool(newval, "QUIET", &pset.quiet);
     890             : }
     891             : 
     892             : static bool
     893       14570 : singleline_hook(const char *newval)
     894             : {
     895       14570 :     return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
     896             : }
     897             : 
     898             : static bool
     899       14570 : singlestep_hook(const char *newval)
     900             : {
     901       14570 :     return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
     902             : }
     903             : 
     904             : static char *
     905       14626 : fetch_count_substitute_hook(char *newval)
     906             : {
     907       14626 :     if (newval == NULL)
     908       14594 :         newval = pg_strdup("0");
     909       14626 :     return newval;
     910             : }
     911             : 
     912             : static bool
     913       14626 : fetch_count_hook(const char *newval)
     914             : {
     915       14626 :     return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
     916             : }
     917             : 
     918             : static bool
     919       14570 : 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       14570 :     return true;
     926             : }
     927             : 
     928             : static char *
     929       14570 : histsize_substitute_hook(char *newval)
     930             : {
     931       14570 :     if (newval == NULL)
     932       14570 :         newval = pg_strdup("500");
     933       14570 :     return newval;
     934             : }
     935             : 
     936             : static bool
     937       14570 : histsize_hook(const char *newval)
     938             : {
     939       14570 :     return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
     940             : }
     941             : 
     942             : static char *
     943       14570 : 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       14570 :     if (newval == NULL)
     957       14570 :         newval = pg_strdup("0");
     958           0 :     else if (!ParseVariableNum(newval, NULL, &dummy))
     959           0 :         newval = pg_strdup("10");
     960       14570 :     return newval;
     961             : }
     962             : 
     963             : static bool
     964       14570 : ignoreeof_hook(const char *newval)
     965             : {
     966       14570 :     return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
     967             : }
     968             : 
     969             : static char *
     970       16518 : echo_substitute_hook(char *newval)
     971             : {
     972       16518 :     if (newval == NULL)
     973       14570 :         newval = pg_strdup("none");
     974       16518 :     return newval;
     975             : }
     976             : 
     977             : static bool
     978       16518 : echo_hook(const char *newval)
     979             : {
     980             :     Assert(newval != NULL);     /* else substitute hook messed up */
     981       16518 :     if (pg_strcasecmp(newval, "queries") == 0)
     982           6 :         pset.echo = PSQL_ECHO_QUERIES;
     983       16512 :     else if (pg_strcasecmp(newval, "errors") == 0)
     984           6 :         pset.echo = PSQL_ECHO_ERRORS;
     985       16506 :     else if (pg_strcasecmp(newval, "all") == 0)
     986        1924 :         pset.echo = PSQL_ECHO_ALL;
     987       14582 :     else if (pg_strcasecmp(newval, "none") == 0)
     988       14582 :         pset.echo = PSQL_ECHO_NONE;
     989             :     else
     990             :     {
     991           0 :         PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
     992           0 :         return false;
     993             :     }
     994       16518 :     return true;
     995             : }
     996             : 
     997             : static bool
     998       14570 : echo_hidden_hook(const char *newval)
     999             : {
    1000             :     Assert(newval != NULL);     /* else substitute hook messed up */
    1001       14570 :     if (pg_strcasecmp(newval, "noexec") == 0)
    1002           0 :         pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
    1003             :     else
    1004             :     {
    1005             :         bool        on_off;
    1006             : 
    1007       14570 :         if (ParseVariableBool(newval, NULL, &on_off))
    1008       14570 :             pset.echo_hidden = on_off ? PSQL_ECHO_HIDDEN_ON : PSQL_ECHO_HIDDEN_OFF;
    1009             :         else
    1010             :         {
    1011           0 :             PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
    1012           0 :             return false;
    1013             :         }
    1014             :     }
    1015       14570 :     return true;
    1016             : }
    1017             : 
    1018             : static bool
    1019       14618 : on_error_rollback_hook(const char *newval)
    1020             : {
    1021             :     Assert(newval != NULL);     /* else substitute hook messed up */
    1022       14618 :     if (pg_strcasecmp(newval, "interactive") == 0)
    1023           0 :         pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
    1024             :     else
    1025             :     {
    1026             :         bool        on_off;
    1027             : 
    1028       14618 :         if (ParseVariableBool(newval, NULL, &on_off))
    1029       14612 :             pset.on_error_rollback = on_off ? PSQL_ERROR_ROLLBACK_ON : PSQL_ERROR_ROLLBACK_OFF;
    1030             :         else
    1031             :         {
    1032           6 :             PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
    1033           6 :             return false;
    1034             :         }
    1035             :     }
    1036       14612 :     return true;
    1037             : }
    1038             : 
    1039             : static char *
    1040       14578 : comp_keyword_case_substitute_hook(char *newval)
    1041             : {
    1042       14578 :     if (newval == NULL)
    1043       14570 :         newval = pg_strdup("preserve-upper");
    1044       14578 :     return newval;
    1045             : }
    1046             : 
    1047             : static bool
    1048       14578 : comp_keyword_case_hook(const char *newval)
    1049             : {
    1050             :     Assert(newval != NULL);     /* else substitute hook messed up */
    1051       14578 :     if (pg_strcasecmp(newval, "preserve-upper") == 0)
    1052       14572 :         pset.comp_case = PSQL_COMP_CASE_PRESERVE_UPPER;
    1053           6 :     else if (pg_strcasecmp(newval, "preserve-lower") == 0)
    1054           2 :         pset.comp_case = PSQL_COMP_CASE_PRESERVE_LOWER;
    1055           4 :     else if (pg_strcasecmp(newval, "upper") == 0)
    1056           2 :         pset.comp_case = PSQL_COMP_CASE_UPPER;
    1057           2 :     else if (pg_strcasecmp(newval, "lower") == 0)
    1058           2 :         pset.comp_case = PSQL_COMP_CASE_LOWER;
    1059             :     else
    1060             :     {
    1061           0 :         PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
    1062             :                          "lower, upper, preserve-lower, preserve-upper");
    1063           0 :         return false;
    1064             :     }
    1065       14578 :     return true;
    1066             : }
    1067             : 
    1068             : static char *
    1069       14570 : histcontrol_substitute_hook(char *newval)
    1070             : {
    1071       14570 :     if (newval == NULL)
    1072       14570 :         newval = pg_strdup("none");
    1073       14570 :     return newval;
    1074             : }
    1075             : 
    1076             : static bool
    1077       14570 : histcontrol_hook(const char *newval)
    1078             : {
    1079             :     Assert(newval != NULL);     /* else substitute hook messed up */
    1080       14570 :     if (pg_strcasecmp(newval, "ignorespace") == 0)
    1081           0 :         pset.histcontrol = hctl_ignorespace;
    1082       14570 :     else if (pg_strcasecmp(newval, "ignoredups") == 0)
    1083           0 :         pset.histcontrol = hctl_ignoredups;
    1084       14570 :     else if (pg_strcasecmp(newval, "ignoreboth") == 0)
    1085           0 :         pset.histcontrol = hctl_ignoreboth;
    1086       14570 :     else if (pg_strcasecmp(newval, "none") == 0)
    1087       14570 :         pset.histcontrol = hctl_none;
    1088             :     else
    1089             :     {
    1090           0 :         PsqlVarEnumError("HISTCONTROL", newval,
    1091             :                          "none, ignorespace, ignoredups, ignoreboth");
    1092           0 :         return false;
    1093             :     }
    1094       14570 :     return true;
    1095             : }
    1096             : 
    1097             : static bool
    1098       29140 : prompt1_hook(const char *newval)
    1099             : {
    1100       29140 :     pset.prompt1 = newval ? newval : "";
    1101       29140 :     return true;
    1102             : }
    1103             : 
    1104             : static bool
    1105       29140 : prompt2_hook(const char *newval)
    1106             : {
    1107       29140 :     pset.prompt2 = newval ? newval : "";
    1108       29140 :     return true;
    1109             : }
    1110             : 
    1111             : static bool
    1112       29140 : prompt3_hook(const char *newval)
    1113             : {
    1114       29140 :     pset.prompt3 = newval ? newval : "";
    1115       29140 :     return true;
    1116             : }
    1117             : 
    1118             : static char *
    1119       14756 : verbosity_substitute_hook(char *newval)
    1120             : {
    1121       14756 :     if (newval == NULL)
    1122       14570 :         newval = pg_strdup("default");
    1123       14756 :     return newval;
    1124             : }
    1125             : 
    1126             : static bool
    1127       14756 : verbosity_hook(const char *newval)
    1128             : {
    1129             :     Assert(newval != NULL);     /* else substitute hook messed up */
    1130       14756 :     if (pg_strcasecmp(newval, "default") == 0)
    1131       14650 :         pset.verbosity = PQERRORS_DEFAULT;
    1132         106 :     else if (pg_strcasecmp(newval, "verbose") == 0)
    1133           0 :         pset.verbosity = PQERRORS_VERBOSE;
    1134         106 :     else if (pg_strcasecmp(newval, "terse") == 0)
    1135          66 :         pset.verbosity = PQERRORS_TERSE;
    1136          40 :     else if (pg_strcasecmp(newval, "sqlstate") == 0)
    1137          40 :         pset.verbosity = PQERRORS_SQLSTATE;
    1138             :     else
    1139             :     {
    1140           0 :         PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
    1141           0 :         return false;
    1142             :     }
    1143             : 
    1144       14756 :     if (pset.db)
    1145         186 :         PQsetErrorVerbosity(pset.db, pset.verbosity);
    1146       14756 :     return true;
    1147             : }
    1148             : 
    1149             : static bool
    1150       29154 : show_all_results_hook(const char *newval)
    1151             : {
    1152       29154 :     return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
    1153             : }
    1154             : 
    1155             : static char *
    1156       14624 : show_context_substitute_hook(char *newval)
    1157             : {
    1158       14624 :     if (newval == NULL)
    1159       14572 :         newval = pg_strdup("errors");
    1160       14624 :     return newval;
    1161             : }
    1162             : 
    1163             : static bool
    1164       14624 : show_context_hook(const char *newval)
    1165             : {
    1166             :     Assert(newval != NULL);     /* else substitute hook messed up */
    1167       14624 :     if (pg_strcasecmp(newval, "never") == 0)
    1168          16 :         pset.show_context = PQSHOW_CONTEXT_NEVER;
    1169       14608 :     else if (pg_strcasecmp(newval, "errors") == 0)
    1170       14592 :         pset.show_context = PQSHOW_CONTEXT_ERRORS;
    1171          16 :     else if (pg_strcasecmp(newval, "always") == 0)
    1172          16 :         pset.show_context = PQSHOW_CONTEXT_ALWAYS;
    1173             :     else
    1174             :     {
    1175           0 :         PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
    1176           0 :         return false;
    1177             :     }
    1178             : 
    1179       14624 :     if (pset.db)
    1180          54 :         PQsetErrorContextVisibility(pset.db, pset.show_context);
    1181       14624 :     return true;
    1182             : }
    1183             : 
    1184             : static bool
    1185       16488 : hide_compression_hook(const char *newval)
    1186             : {
    1187       16488 :     return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
    1188             :                              &pset.hide_compression);
    1189             : }
    1190             : 
    1191             : static bool
    1192       16488 : hide_tableam_hook(const char *newval)
    1193             : {
    1194       16488 :     return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
    1195             : }
    1196             : 
    1197             : static void
    1198       14570 : EstablishVariableSpace(void)
    1199             : {
    1200       14570 :     pset.vars = CreateVariableSpace();
    1201             : 
    1202       14570 :     SetVariableHooks(pset.vars, "AUTOCOMMIT",
    1203             :                      bool_substitute_hook,
    1204             :                      autocommit_hook);
    1205       14570 :     SetVariableHooks(pset.vars, "ON_ERROR_STOP",
    1206             :                      bool_substitute_hook,
    1207             :                      on_error_stop_hook);
    1208       14570 :     SetVariableHooks(pset.vars, "QUIET",
    1209             :                      bool_substitute_hook,
    1210             :                      quiet_hook);
    1211       14570 :     SetVariableHooks(pset.vars, "SINGLELINE",
    1212             :                      bool_substitute_hook,
    1213             :                      singleline_hook);
    1214       14570 :     SetVariableHooks(pset.vars, "SINGLESTEP",
    1215             :                      bool_substitute_hook,
    1216             :                      singlestep_hook);
    1217       14570 :     SetVariableHooks(pset.vars, "FETCH_COUNT",
    1218             :                      fetch_count_substitute_hook,
    1219             :                      fetch_count_hook);
    1220       14570 :     SetVariableHooks(pset.vars, "HISTFILE",
    1221             :                      NULL,
    1222             :                      histfile_hook);
    1223       14570 :     SetVariableHooks(pset.vars, "HISTSIZE",
    1224             :                      histsize_substitute_hook,
    1225             :                      histsize_hook);
    1226       14570 :     SetVariableHooks(pset.vars, "IGNOREEOF",
    1227             :                      ignoreeof_substitute_hook,
    1228             :                      ignoreeof_hook);
    1229       14570 :     SetVariableHooks(pset.vars, "ECHO",
    1230             :                      echo_substitute_hook,
    1231             :                      echo_hook);
    1232       14570 :     SetVariableHooks(pset.vars, "ECHO_HIDDEN",
    1233             :                      bool_substitute_hook,
    1234             :                      echo_hidden_hook);
    1235       14570 :     SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
    1236             :                      bool_substitute_hook,
    1237             :                      on_error_rollback_hook);
    1238       14570 :     SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
    1239             :                      comp_keyword_case_substitute_hook,
    1240             :                      comp_keyword_case_hook);
    1241       14570 :     SetVariableHooks(pset.vars, "HISTCONTROL",
    1242             :                      histcontrol_substitute_hook,
    1243             :                      histcontrol_hook);
    1244       14570 :     SetVariableHooks(pset.vars, "PROMPT1",
    1245             :                      NULL,
    1246             :                      prompt1_hook);
    1247       14570 :     SetVariableHooks(pset.vars, "PROMPT2",
    1248             :                      NULL,
    1249             :                      prompt2_hook);
    1250       14570 :     SetVariableHooks(pset.vars, "PROMPT3",
    1251             :                      NULL,
    1252             :                      prompt3_hook);
    1253       14570 :     SetVariableHooks(pset.vars, "VERBOSITY",
    1254             :                      verbosity_substitute_hook,
    1255             :                      verbosity_hook);
    1256       14570 :     SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
    1257             :                      bool_substitute_hook,
    1258             :                      show_all_results_hook);
    1259       14570 :     SetVariableHooks(pset.vars, "SHOW_CONTEXT",
    1260             :                      show_context_substitute_hook,
    1261             :                      show_context_hook);
    1262       14570 :     SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
    1263             :                      bool_substitute_hook,
    1264             :                      hide_compression_hook);
    1265       14570 :     SetVariableHooks(pset.vars, "HIDE_TABLEAM",
    1266             :                      bool_substitute_hook,
    1267             :                      hide_tableam_hook);
    1268       14570 : }

Generated by: LCOV version 1.14