LCOV - code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Hit Total Coverage
Test: PostgreSQL 15devel Lines: 1185 2166 54.7 %
Date: 2021-12-04 23:09:10 Functions: 83 94 88.3 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * psql - the PostgreSQL interactive terminal
       3             :  *
       4             :  * Copyright (c) 2000-2021, PostgreSQL Global Development Group
       5             :  *
       6             :  * src/bin/psql/command.c
       7             :  */
       8             : #include "postgres_fe.h"
       9             : 
      10             : #include <ctype.h>
      11             : #include <time.h>
      12             : #include <pwd.h>
      13             : #include <utime.h>
      14             : #ifndef WIN32
      15             : #include <sys/stat.h>         /* for stat() */
      16             : #include <sys/time.h>         /* for setitimer() */
      17             : #include <fcntl.h>                /* open() flags */
      18             : #include <unistd.h>               /* for geteuid(), getpid(), stat() */
      19             : #else
      20             : #include <win32.h>
      21             : #include <io.h>
      22             : #include <fcntl.h>
      23             : #include <direct.h>
      24             : #include <sys/stat.h>         /* for stat() */
      25             : #endif
      26             : 
      27             : #include "catalog/pg_class_d.h"
      28             : #include "command.h"
      29             : #include "common.h"
      30             : #include "common/logging.h"
      31             : #include "common/string.h"
      32             : #include "copy.h"
      33             : #include "crosstabview.h"
      34             : #include "describe.h"
      35             : #include "fe_utils/cancel.h"
      36             : #include "fe_utils/print.h"
      37             : #include "fe_utils/string_utils.h"
      38             : #include "help.h"
      39             : #include "input.h"
      40             : #include "large_obj.h"
      41             : #include "libpq-fe.h"
      42             : #include "libpq/pqcomm.h"
      43             : #include "mainloop.h"
      44             : #include "portability/instr_time.h"
      45             : #include "pqexpbuffer.h"
      46             : #include "psqlscanslash.h"
      47             : #include "settings.h"
      48             : #include "variables.h"
      49             : 
      50             : /*
      51             :  * Editable database object types.
      52             :  */
      53             : typedef enum EditableObjectType
      54             : {
      55             :     EditableFunction,
      56             :     EditableView
      57             : } EditableObjectType;
      58             : 
      59             : /* local function declarations */
      60             : static backslashResult exec_command(const char *cmd,
      61             :                                     PsqlScanState scan_state,
      62             :                                     ConditionalStack cstack,
      63             :                                     PQExpBuffer query_buf,
      64             :                                     PQExpBuffer previous_buf);
      65             : static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch);
      66             : static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch);
      67             : static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch);
      68             : static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch,
      69             :                                        const char *cmd);
      70             : static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch);
      71             : static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch);
      72             : static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch);
      73             : static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch);
      74             : static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch,
      75             :                                       const char *cmd);
      76             : static bool exec_command_dfo(PsqlScanState scan_state, const char *cmd,
      77             :                              const char *pattern,
      78             :                              bool show_verbose, bool show_system);
      79             : static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch,
      80             :                                          PQExpBuffer query_buf, PQExpBuffer previous_buf);
      81             : static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
      82             :                                           PQExpBuffer query_buf, bool is_func);
      83             : static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch,
      84             :                                          const char *cmd);
      85             : static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack,
      86             :                                          PQExpBuffer query_buf);
      87             : static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack,
      88             :                                          PQExpBuffer query_buf);
      89             : static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack,
      90             :                                           PQExpBuffer query_buf);
      91             : static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch);
      92             : static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch);
      93             : static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch);
      94             : static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch,
      95             :                                       const char *cmd);
      96             : static backslashResult process_command_g_options(char *first_option,
      97             :                                                  PsqlScanState scan_state,
      98             :                                                  bool active_branch,
      99             :                                                  const char *cmd);
     100             : static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch);
     101             : static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch);
     102             : static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch);
     103             : static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch);
     104             : static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch);
     105             : static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch,
     106             :                                             const char *cmd);
     107             : static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack,
     108             :                                        PQExpBuffer query_buf);
     109             : static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch,
     110             :                                          const char *cmd);
     111             : static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch,
     112             :                                        const char *cmd);
     113             : static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch);
     114             : static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch,
     115             :                                           PQExpBuffer query_buf, PQExpBuffer previous_buf);
     116             : static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch);
     117             : static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch,
     118             :                                            const char *cmd);
     119             : static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch);
     120             : static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch);
     121             : static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch,
     122             :                                           PQExpBuffer query_buf);
     123             : static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch);
     124             : static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch);
     125             : static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch,
     126             :                                            const char *cmd);
     127             : static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
     128             :                                           const char *cmd, bool is_func);
     129             : static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch);
     130             : static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch);
     131             : static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch);
     132             : static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch,
     133             :                                           const char *cmd);
     134             : static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch,
     135             :                                           const char *cmd,
     136             :                                           PQExpBuffer query_buf, PQExpBuffer previous_buf);
     137             : static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch,
     138             :                                           PQExpBuffer query_buf, PQExpBuffer previous_buf);
     139             : static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch);
     140             : static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch);
     141             : static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch);
     142             : static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch);
     143             : static char *read_connect_arg(PsqlScanState scan_state);
     144             : static PQExpBuffer gather_boolean_expression(PsqlScanState scan_state);
     145             : static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name);
     146             : static void ignore_boolean_expression(PsqlScanState scan_state);
     147             : static void ignore_slash_options(PsqlScanState scan_state);
     148             : static void ignore_slash_filepipe(PsqlScanState scan_state);
     149             : static void ignore_slash_whole_line(PsqlScanState scan_state);
     150             : static bool is_branching_command(const char *cmd);
     151             : static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack,
     152             :                                   PQExpBuffer query_buf);
     153             : static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack,
     154             :                                PQExpBuffer query_buf);
     155             : static bool copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf);
     156             : static bool do_connect(enum trivalue reuse_previous_specification,
     157             :                        char *dbname, char *user, char *host, char *port);
     158             : static bool do_edit(const char *filename_arg, PQExpBuffer query_buf,
     159             :                     int lineno, bool discard_on_quit, bool *edited);
     160             : static bool do_shell(const char *command);
     161             : static bool do_watch(PQExpBuffer query_buf, double sleep);
     162             : static bool lookup_object_oid(EditableObjectType obj_type, const char *desc,
     163             :                               Oid *obj_oid);
     164             : static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid,
     165             :                                   PQExpBuffer buf);
     166             : static int  strip_lineno_from_objdesc(char *obj);
     167             : static int  count_lines_in_buf(PQExpBuffer buf);
     168             : static void print_with_linenumbers(FILE *output, char *lines,
     169             :                                    const char *header_keyword);
     170             : static void minimal_error_message(PGresult *res);
     171             : 
     172             : static void printSSLInfo(void);
     173             : static void printGSSInfo(void);
     174             : static bool printPsetInfo(const char *param, printQueryOpt *popt);
     175             : static char *pset_value_string(const char *param, printQueryOpt *popt);
     176             : 
     177             : #ifdef WIN32
     178             : static void checkWin32Codepage(void);
     179             : #endif
     180             : 
     181             : 
     182             : 
     183             : /*----------
     184             :  * HandleSlashCmds:
     185             :  *
     186             :  * Handles all the different commands that start with '\'.
     187             :  * Ordinarily called by MainLoop().
     188             :  *
     189             :  * scan_state is a lexer working state that is set to continue scanning
     190             :  * just after the '\'.  The lexer is advanced past the command and all
     191             :  * arguments on return.
     192             :  *
     193             :  * cstack is the current \if stack state.  This will be examined, and
     194             :  * possibly modified by conditional commands.
     195             :  *
     196             :  * query_buf contains the query-so-far, which may be modified by
     197             :  * execution of the backslash command (for example, \r clears it).
     198             :  *
     199             :  * previous_buf contains the query most recently sent to the server
     200             :  * (empty if none yet).  This should not be modified here, but some
     201             :  * commands copy its content into query_buf.
     202             :  *
     203             :  * query_buf and previous_buf will be NULL when executing a "-c"
     204             :  * command-line option.
     205             :  *
     206             :  * Returns a status code indicating what action is desired, see command.h.
     207             :  *----------
     208             :  */
     209             : 
     210             : backslashResult
     211        6004 : HandleSlashCmds(PsqlScanState scan_state,
     212             :                 ConditionalStack cstack,
     213             :                 PQExpBuffer query_buf,
     214             :                 PQExpBuffer previous_buf)
     215             : {
     216             :     backslashResult status;
     217             :     char       *cmd;
     218             :     char       *arg;
     219             : 
     220             :     Assert(scan_state != NULL);
     221             :     Assert(cstack != NULL);
     222             : 
     223             :     /* Parse off the command name */
     224        6004 :     cmd = psql_scan_slash_command(scan_state);
     225             : 
     226             :     /* And try to execute it */
     227        6004 :     status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
     228             : 
     229        6004 :     if (status == PSQL_CMD_UNKNOWN)
     230             :     {
     231           4 :         pg_log_error("invalid command \\%s", cmd);
     232           4 :         if (pset.cur_cmd_interactive)
     233           0 :             pg_log_info("Try \\? for help.");
     234           4 :         status = PSQL_CMD_ERROR;
     235             :     }
     236             : 
     237        6004 :     if (status != PSQL_CMD_ERROR)
     238             :     {
     239             :         /*
     240             :          * Eat any remaining arguments after a valid command.  We want to
     241             :          * suppress evaluation of backticks in this situation, so transiently
     242             :          * push an inactive conditional-stack entry.
     243             :          */
     244        5910 :         bool        active_branch = conditional_active(cstack);
     245             : 
     246        5910 :         conditional_stack_push(cstack, IFSTATE_IGNORED);
     247        5930 :         while ((arg = psql_scan_slash_option(scan_state,
     248             :                                              OT_NORMAL, NULL, false)))
     249             :         {
     250          20 :             if (active_branch)
     251          20 :                 pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
     252          20 :             free(arg);
     253             :         }
     254        5910 :         conditional_stack_pop(cstack);
     255             :     }
     256             :     else
     257             :     {
     258             :         /* silently throw away rest of line after an erroneous command */
     259         100 :         while ((arg = psql_scan_slash_option(scan_state,
     260             :                                              OT_WHOLE_LINE, NULL, false)))
     261           6 :             free(arg);
     262             :     }
     263             : 
     264             :     /* if there is a trailing \\, swallow it */
     265        6004 :     psql_scan_slash_command_end(scan_state);
     266             : 
     267        6004 :     free(cmd);
     268             : 
     269             :     /* some commands write to queryFout, so make sure output is sent */
     270        6004 :     fflush(pset.queryFout);
     271             : 
     272        6004 :     return status;
     273             : }
     274             : 
     275             : 
     276             : /*
     277             :  * Subroutine to actually try to execute a backslash command.
     278             :  *
     279             :  * The typical "success" result code is PSQL_CMD_SKIP_LINE, although some
     280             :  * commands return something else.  Failure results are PSQL_CMD_ERROR,
     281             :  * unless PSQL_CMD_UNKNOWN is more appropriate.
     282             :  */
     283             : static backslashResult
     284        6004 : exec_command(const char *cmd,
     285             :              PsqlScanState scan_state,
     286             :              ConditionalStack cstack,
     287             :              PQExpBuffer query_buf,
     288             :              PQExpBuffer previous_buf)
     289             : {
     290             :     backslashResult status;
     291        6004 :     bool        active_branch = conditional_active(cstack);
     292             : 
     293             :     /*
     294             :      * In interactive mode, warn when we're ignoring a command within a false
     295             :      * \if-branch.  But we continue on, so as to parse and discard the right
     296             :      * amount of parameter text.  Each individual backslash command subroutine
     297             :      * is responsible for doing nothing after discarding appropriate
     298             :      * arguments, if !active_branch.
     299             :      */
     300        6004 :     if (pset.cur_cmd_interactive && !active_branch &&
     301           0 :         !is_branching_command(cmd))
     302             :     {
     303           0 :         pg_log_warning("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block",
     304             :                        cmd);
     305             :     }
     306             : 
     307        6004 :     if (strcmp(cmd, "a") == 0)
     308          36 :         status = exec_command_a(scan_state, active_branch);
     309        5968 :     else if (strcmp(cmd, "C") == 0)
     310           4 :         status = exec_command_C(scan_state, active_branch);
     311        5964 :     else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
     312         188 :         status = exec_command_connect(scan_state, active_branch);
     313        5776 :     else if (strcmp(cmd, "cd") == 0)
     314           4 :         status = exec_command_cd(scan_state, active_branch, cmd);
     315        5772 :     else if (strcmp(cmd, "conninfo") == 0)
     316           4 :         status = exec_command_conninfo(scan_state, active_branch);
     317        5768 :     else if (pg_strcasecmp(cmd, "copy") == 0)
     318         132 :         status = exec_command_copy(scan_state, active_branch);
     319        5636 :     else if (strcmp(cmd, "copyright") == 0)
     320           6 :         status = exec_command_copyright(scan_state, active_branch);
     321        5630 :     else if (strcmp(cmd, "crosstabview") == 0)
     322          92 :         status = exec_command_crosstabview(scan_state, active_branch);
     323        5538 :     else if (cmd[0] == 'd')
     324        2686 :         status = exec_command_d(scan_state, active_branch, cmd);
     325        2852 :     else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
     326           4 :         status = exec_command_edit(scan_state, active_branch,
     327             :                                    query_buf, previous_buf);
     328        2848 :     else if (strcmp(cmd, "ef") == 0)
     329           4 :         status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
     330        2844 :     else if (strcmp(cmd, "ev") == 0)
     331           4 :         status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
     332        2840 :     else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0 ||
     333        2430 :              strcmp(cmd, "warn") == 0)
     334         418 :         status = exec_command_echo(scan_state, active_branch, cmd);
     335        2422 :     else if (strcmp(cmd, "elif") == 0)
     336          32 :         status = exec_command_elif(scan_state, cstack, query_buf);
     337        2390 :     else if (strcmp(cmd, "else") == 0)
     338          84 :         status = exec_command_else(scan_state, cstack, query_buf);
     339        2306 :     else if (strcmp(cmd, "endif") == 0)
     340          98 :         status = exec_command_endif(scan_state, cstack, query_buf);
     341        2208 :     else if (strcmp(cmd, "encoding") == 0)
     342           4 :         status = exec_command_encoding(scan_state, active_branch);
     343        2204 :     else if (strcmp(cmd, "errverbose") == 0)
     344           4 :         status = exec_command_errverbose(scan_state, active_branch);
     345        2200 :     else if (strcmp(cmd, "f") == 0)
     346           4 :         status = exec_command_f(scan_state, active_branch);
     347        2196 :     else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
     348          80 :         status = exec_command_g(scan_state, active_branch, cmd);
     349        2116 :     else if (strcmp(cmd, "gdesc") == 0)
     350          40 :         status = exec_command_gdesc(scan_state, active_branch);
     351        2076 :     else if (strcmp(cmd, "gexec") == 0)
     352          24 :         status = exec_command_gexec(scan_state, active_branch);
     353        2052 :     else if (strcmp(cmd, "gset") == 0)
     354         178 :         status = exec_command_gset(scan_state, active_branch);
     355        1874 :     else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
     356           8 :         status = exec_command_help(scan_state, active_branch);
     357        1866 :     else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
     358           4 :         status = exec_command_html(scan_state, active_branch);
     359        1862 :     else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
     360        1858 :              strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
     361           8 :         status = exec_command_include(scan_state, active_branch, cmd);
     362        1854 :     else if (strcmp(cmd, "if") == 0)
     363         102 :         status = exec_command_if(scan_state, cstack, query_buf);
     364        1752 :     else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
     365        1748 :              strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
     366           4 :         status = exec_command_list(scan_state, active_branch, cmd);
     367        1748 :     else if (strncmp(cmd, "lo_", 3) == 0)
     368          32 :         status = exec_command_lo(scan_state, active_branch, cmd);
     369        1716 :     else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
     370           4 :         status = exec_command_out(scan_state, active_branch);
     371        1712 :     else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
     372          28 :         status = exec_command_print(scan_state, active_branch,
     373             :                                     query_buf, previous_buf);
     374        1684 :     else if (strcmp(cmd, "password") == 0)
     375           4 :         status = exec_command_password(scan_state, active_branch);
     376        1680 :     else if (strcmp(cmd, "prompt") == 0)
     377           4 :         status = exec_command_prompt(scan_state, active_branch, cmd);
     378        1676 :     else if (strcmp(cmd, "pset") == 0)
     379        1148 :         status = exec_command_pset(scan_state, active_branch);
     380         528 :     else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
     381          40 :         status = exec_command_quit(scan_state, active_branch);
     382         488 :     else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
     383          26 :         status = exec_command_reset(scan_state, active_branch, query_buf);
     384         462 :     else if (strcmp(cmd, "s") == 0)
     385           4 :         status = exec_command_s(scan_state, active_branch);
     386         458 :     else if (strcmp(cmd, "set") == 0)
     387         288 :         status = exec_command_set(scan_state, active_branch);
     388         170 :     else if (strcmp(cmd, "setenv") == 0)
     389           4 :         status = exec_command_setenv(scan_state, active_branch, cmd);
     390         166 :     else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
     391          10 :         status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
     392         156 :     else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
     393           8 :         status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
     394         148 :     else if (strcmp(cmd, "t") == 0)
     395          52 :         status = exec_command_t(scan_state, active_branch);
     396          96 :     else if (strcmp(cmd, "T") == 0)
     397           4 :         status = exec_command_T(scan_state, active_branch);
     398          92 :     else if (strcmp(cmd, "timing") == 0)
     399           4 :         status = exec_command_timing(scan_state, active_branch);
     400          88 :     else if (strcmp(cmd, "unset") == 0)
     401          26 :         status = exec_command_unset(scan_state, active_branch, cmd);
     402          62 :     else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
     403           8 :         status = exec_command_write(scan_state, active_branch, cmd,
     404             :                                     query_buf, previous_buf);
     405          54 :     else if (strcmp(cmd, "watch") == 0)
     406           4 :         status = exec_command_watch(scan_state, active_branch,
     407             :                                     query_buf, previous_buf);
     408          50 :     else if (strcmp(cmd, "x") == 0)
     409          22 :         status = exec_command_x(scan_state, active_branch);
     410          28 :     else if (strcmp(cmd, "z") == 0)
     411          12 :         status = exec_command_z(scan_state, active_branch);
     412          16 :     else if (strcmp(cmd, "!") == 0)
     413           8 :         status = exec_command_shell_escape(scan_state, active_branch);
     414           8 :     else if (strcmp(cmd, "?") == 0)
     415           4 :         status = exec_command_slash_command_help(scan_state, active_branch);
     416             :     else
     417           4 :         status = PSQL_CMD_UNKNOWN;
     418             : 
     419             :     /*
     420             :      * All the commands that return PSQL_CMD_SEND want to execute previous_buf
     421             :      * if query_buf is empty.  For convenience we implement that here, not in
     422             :      * the individual command subroutines.
     423             :      */
     424        6004 :     if (status == PSQL_CMD_SEND)
     425         394 :         (void) copy_previous_query(query_buf, previous_buf);
     426             : 
     427        6004 :     return status;
     428             : }
     429             : 
     430             : 
     431             : /*
     432             :  * \a -- toggle field alignment
     433             :  *
     434             :  * This makes little sense but we keep it around.
     435             :  */
     436             : static backslashResult
     437          36 : exec_command_a(PsqlScanState scan_state, bool active_branch)
     438             : {
     439          36 :     bool        success = true;
     440             : 
     441          36 :     if (active_branch)
     442             :     {
     443          32 :         if (pset.popt.topt.format != PRINT_ALIGNED)
     444          16 :             success = do_pset("format", "aligned", &pset.popt, pset.quiet);
     445             :         else
     446          16 :             success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
     447             :     }
     448             : 
     449          36 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
     450             : }
     451             : 
     452             : /*
     453             :  * \C -- override table title (formerly change HTML caption)
     454             :  */
     455             : static backslashResult
     456           4 : exec_command_C(PsqlScanState scan_state, bool active_branch)
     457             : {
     458           4 :     bool        success = true;
     459             : 
     460           4 :     if (active_branch)
     461             :     {
     462           0 :         char       *opt = psql_scan_slash_option(scan_state,
     463             :                                                  OT_NORMAL, NULL, true);
     464             : 
     465           0 :         success = do_pset("title", opt, &pset.popt, pset.quiet);
     466           0 :         free(opt);
     467             :     }
     468             :     else
     469           4 :         ignore_slash_options(scan_state);
     470             : 
     471           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
     472             : }
     473             : 
     474             : /*
     475             :  * \c or \connect -- connect to database using the specified parameters.
     476             :  *
     477             :  * \c [-reuse-previous=BOOL] dbname user host port
     478             :  *
     479             :  * Specifying a parameter as '-' is equivalent to omitting it.  Examples:
     480             :  *
     481             :  * \c - - hst       Connect to current database on current port of
     482             :  *                  host "hst" as current user.
     483             :  * \c - usr - prt   Connect to current database on port "prt" of current host
     484             :  *                  as user "usr".
     485             :  * \c dbs           Connect to database "dbs" on current port of current host
     486             :  *                  as current user.
     487             :  */
     488             : static backslashResult
     489         188 : exec_command_connect(PsqlScanState scan_state, bool active_branch)
     490             : {
     491         188 :     bool        success = true;
     492             : 
     493         188 :     if (active_branch)
     494             :     {
     495             :         static const char prefix[] = "-reuse-previous=";
     496             :         char       *opt1,
     497             :                    *opt2,
     498             :                    *opt3,
     499             :                    *opt4;
     500         184 :         enum trivalue reuse_previous = TRI_DEFAULT;
     501             : 
     502         184 :         opt1 = read_connect_arg(scan_state);
     503         184 :         if (opt1 != NULL && strncmp(opt1, prefix, sizeof(prefix) - 1) == 0)
     504             :         {
     505             :             bool        on_off;
     506             : 
     507          16 :             success = ParseVariableBool(opt1 + sizeof(prefix) - 1,
     508             :                                         "-reuse-previous",
     509             :                                         &on_off);
     510          16 :             if (success)
     511             :             {
     512          16 :                 reuse_previous = on_off ? TRI_YES : TRI_NO;
     513          16 :                 free(opt1);
     514          16 :                 opt1 = read_connect_arg(scan_state);
     515             :             }
     516             :         }
     517             : 
     518         184 :         if (success)            /* give up if reuse_previous was invalid */
     519             :         {
     520         184 :             opt2 = read_connect_arg(scan_state);
     521         184 :             opt3 = read_connect_arg(scan_state);
     522         184 :             opt4 = read_connect_arg(scan_state);
     523             : 
     524         184 :             success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
     525             : 
     526         184 :             free(opt2);
     527         184 :             free(opt3);
     528         184 :             free(opt4);
     529             :         }
     530         184 :         free(opt1);
     531             :     }
     532             :     else
     533           4 :         ignore_slash_options(scan_state);
     534             : 
     535         188 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
     536             : }
     537             : 
     538             : /*
     539             :  * \cd -- change directory
     540             :  */
     541             : static backslashResult
     542           4 : exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
     543             : {
     544           4 :     bool        success = true;
     545             : 
     546           4 :     if (active_branch)
     547             :     {
     548           0 :         char       *opt = psql_scan_slash_option(scan_state,
     549             :                                                  OT_NORMAL, NULL, true);
     550             :         char       *dir;
     551             : 
     552           0 :         if (opt)
     553           0 :             dir = opt;
     554             :         else
     555             :         {
     556             : #ifndef WIN32
     557             :             struct passwd *pw;
     558           0 :             uid_t       user_id = geteuid();
     559             : 
     560           0 :             errno = 0;          /* clear errno before call */
     561           0 :             pw = getpwuid(user_id);
     562           0 :             if (!pw)
     563             :             {
     564           0 :                 pg_log_error("could not get home directory for user ID %ld: %s",
     565             :                              (long) user_id,
     566             :                              errno ? strerror(errno) : _("user does not exist"));
     567           0 :                 exit(EXIT_FAILURE);
     568             :             }
     569           0 :             dir = pw->pw_dir;
     570             : #else                           /* WIN32 */
     571             : 
     572             :             /*
     573             :              * On Windows, 'cd' without arguments prints the current
     574             :              * directory, so if someone wants to code this here instead...
     575             :              */
     576             :             dir = "/";
     577             : #endif                          /* WIN32 */
     578             :         }
     579             : 
     580           0 :         if (chdir(dir) == -1)
     581             :         {
     582           0 :             pg_log_error("\\%s: could not change directory to \"%s\": %m",
     583             :                          cmd, dir);
     584           0 :             success = false;
     585             :         }
     586             : 
     587           0 :         if (opt)
     588           0 :             free(opt);
     589             :     }
     590             :     else
     591           4 :         ignore_slash_options(scan_state);
     592             : 
     593           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
     594             : }
     595             : 
     596             : /*
     597             :  * \conninfo -- display information about the current connection
     598             :  */
     599             : static backslashResult
     600           4 : exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
     601             : {
     602           4 :     if (active_branch)
     603             :     {
     604           0 :         char       *db = PQdb(pset.db);
     605             : 
     606           0 :         if (db == NULL)
     607           0 :             printf(_("You are currently not connected to a database.\n"));
     608             :         else
     609             :         {
     610           0 :             char       *host = PQhost(pset.db);
     611           0 :             char       *hostaddr = PQhostaddr(pset.db);
     612             : 
     613           0 :             if (is_unixsock_path(host))
     614             :             {
     615             :                 /* hostaddr overrides host */
     616           0 :                 if (hostaddr && *hostaddr)
     617           0 :                     printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
     618             :                            db, PQuser(pset.db), hostaddr, PQport(pset.db));
     619             :                 else
     620           0 :                     printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
     621             :                            db, PQuser(pset.db), host, PQport(pset.db));
     622             :             }
     623             :             else
     624             :             {
     625           0 :                 if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
     626           0 :                     printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
     627             :                            db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
     628             :                 else
     629           0 :                     printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
     630             :                            db, PQuser(pset.db), host, PQport(pset.db));
     631             :             }
     632           0 :             printSSLInfo();
     633           0 :             printGSSInfo();
     634             :         }
     635             :     }
     636             : 
     637           4 :     return PSQL_CMD_SKIP_LINE;
     638             : }
     639             : 
     640             : /*
     641             :  * \copy -- run a COPY command
     642             :  */
     643             : static backslashResult
     644         132 : exec_command_copy(PsqlScanState scan_state, bool active_branch)
     645             : {
     646         132 :     bool        success = true;
     647             : 
     648         132 :     if (active_branch)
     649             :     {
     650         128 :         char       *opt = psql_scan_slash_option(scan_state,
     651             :                                                  OT_WHOLE_LINE, NULL, false);
     652             : 
     653         128 :         success = do_copy(opt);
     654         128 :         free(opt);
     655             :     }
     656             :     else
     657           4 :         ignore_slash_whole_line(scan_state);
     658             : 
     659         132 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
     660             : }
     661             : 
     662             : /*
     663             :  * \copyright -- print copyright notice
     664             :  */
     665             : static backslashResult
     666           6 : exec_command_copyright(PsqlScanState scan_state, bool active_branch)
     667             : {
     668           6 :     if (active_branch)
     669           2 :         print_copyright();
     670             : 
     671           6 :     return PSQL_CMD_SKIP_LINE;
     672             : }
     673             : 
     674             : /*
     675             :  * \crosstabview -- execute a query and display results in crosstab
     676             :  */
     677             : static backslashResult
     678          92 : exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
     679             : {
     680          92 :     backslashResult status = PSQL_CMD_SKIP_LINE;
     681             : 
     682          92 :     if (active_branch)
     683             :     {
     684             :         int         i;
     685             : 
     686         440 :         for (i = 0; i < lengthof(pset.ctv_args); i++)
     687         352 :             pset.ctv_args[i] = psql_scan_slash_option(scan_state,
     688             :                                                       OT_NORMAL, NULL, true);
     689          88 :         pset.crosstab_flag = true;
     690          88 :         status = PSQL_CMD_SEND;
     691             :     }
     692             :     else
     693           4 :         ignore_slash_options(scan_state);
     694             : 
     695          92 :     return status;
     696             : }
     697             : 
     698             : /*
     699             :  * \d* commands
     700             :  */
     701             : static backslashResult
     702        2686 : exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
     703             : {
     704        2686 :     backslashResult status = PSQL_CMD_SKIP_LINE;
     705        2686 :     bool        success = true;
     706             : 
     707        2686 :     if (active_branch)
     708             :     {
     709             :         char       *pattern;
     710             :         bool        show_verbose,
     711             :                     show_system;
     712             : 
     713             :         /* We don't do SQLID reduction on the pattern yet */
     714        2682 :         pattern = psql_scan_slash_option(scan_state,
     715             :                                          OT_NORMAL, NULL, true);
     716             : 
     717        2682 :         show_verbose = strchr(cmd, '+') ? true : false;
     718        2682 :         show_system = strchr(cmd, 'S') ? true : false;
     719             : 
     720        2682 :         switch (cmd[1])
     721             :         {
     722        1920 :             case '\0':
     723             :             case '+':
     724             :             case 'S':
     725        1920 :                 if (pattern)
     726        1912 :                     success = describeTableDetails(pattern, show_verbose, show_system);
     727             :                 else
     728             :                     /* standard listing of interesting things */
     729           8 :                     success = listTables("tvmsE", NULL, show_verbose, show_system);
     730        1920 :                 break;
     731          64 :             case 'A':
     732             :                 {
     733          64 :                     char       *pattern2 = NULL;
     734             : 
     735          64 :                     if (pattern && cmd[2] != '\0' && cmd[2] != '+')
     736          28 :                         pattern2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
     737             : 
     738          64 :                     switch (cmd[2])
     739             :                     {
     740          36 :                         case '\0':
     741             :                         case '+':
     742          36 :                             success = describeAccessMethods(pattern, show_verbose);
     743          36 :                             break;
     744           4 :                         case 'c':
     745           4 :                             success = listOperatorClasses(pattern, pattern2, show_verbose);
     746           4 :                             break;
     747           8 :                         case 'f':
     748           8 :                             success = listOperatorFamilies(pattern, pattern2, show_verbose);
     749           8 :                             break;
     750           8 :                         case 'o':
     751           8 :                             success = listOpFamilyOperators(pattern, pattern2, show_verbose);
     752           8 :                             break;
     753           8 :                         case 'p':
     754           8 :                             success = listOpFamilyFunctions(pattern, pattern2, show_verbose);
     755           8 :                             break;
     756           0 :                         default:
     757           0 :                             status = PSQL_CMD_UNKNOWN;
     758           0 :                             break;
     759             :                     }
     760             : 
     761          64 :                     if (pattern2)
     762          24 :                         free(pattern2);
     763             :                 }
     764          64 :                 break;
     765           4 :             case 'a':
     766           4 :                 success = describeAggregates(pattern, show_verbose, show_system);
     767           4 :                 break;
     768           0 :             case 'b':
     769           0 :                 success = describeTablespaces(pattern, show_verbose);
     770           0 :                 break;
     771           0 :             case 'c':
     772           0 :                 success = listConversions(pattern, show_verbose, show_system);
     773           0 :                 break;
     774           0 :             case 'C':
     775           0 :                 success = listCasts(pattern, show_verbose);
     776           0 :                 break;
     777           0 :             case 'd':
     778           0 :                 if (strncmp(cmd, "ddp", 3) == 0)
     779           0 :                     success = listDefaultACLs(pattern);
     780             :                 else
     781           0 :                     success = objectDescription(pattern, show_system);
     782           0 :                 break;
     783           0 :             case 'D':
     784           0 :                 success = listDomains(pattern, show_verbose, show_system);
     785           0 :                 break;
     786         158 :             case 'f':           /* function subsystem */
     787         158 :                 switch (cmd[2])
     788             :                 {
     789         158 :                     case '\0':
     790             :                     case '+':
     791             :                     case 'S':
     792             :                     case 'a':
     793             :                     case 'n':
     794             :                     case 'p':
     795             :                     case 't':
     796             :                     case 'w':
     797         158 :                         success = exec_command_dfo(scan_state, cmd, pattern,
     798             :                                                    show_verbose, show_system);
     799         158 :                         break;
     800           0 :                     default:
     801           0 :                         status = PSQL_CMD_UNKNOWN;
     802           0 :                         break;
     803             :                 }
     804         158 :                 break;
     805           0 :             case 'g':
     806             :                 /* no longer distinct from \du */
     807           0 :                 success = describeRoles(pattern, show_verbose, show_system);
     808           0 :                 break;
     809           0 :             case 'l':
     810           0 :                 success = do_lo_list();
     811           0 :                 break;
     812           0 :             case 'L':
     813           0 :                 success = listLanguages(pattern, show_verbose, show_system);
     814           0 :                 break;
     815           0 :             case 'n':
     816           0 :                 success = listSchemas(pattern, show_verbose, show_system);
     817           0 :                 break;
     818          16 :             case 'o':
     819          16 :                 success = exec_command_dfo(scan_state, cmd, pattern,
     820             :                                            show_verbose, show_system);
     821          16 :                 break;
     822           0 :             case 'O':
     823           0 :                 success = listCollations(pattern, show_verbose, show_system);
     824           0 :                 break;
     825          16 :             case 'p':
     826          16 :                 success = permissionsList(pattern);
     827          16 :                 break;
     828          44 :             case 'P':
     829             :                 {
     830          44 :                     switch (cmd[2])
     831             :                     {
     832          44 :                         case '\0':
     833             :                         case '+':
     834             :                         case 't':
     835             :                         case 'i':
     836             :                         case 'n':
     837          44 :                             success = listPartitionedTables(&cmd[2], pattern, show_verbose);
     838          44 :                             break;
     839           0 :                         default:
     840           0 :                             status = PSQL_CMD_UNKNOWN;
     841           0 :                             break;
     842             :                     }
     843             :                 }
     844          44 :                 break;
     845          14 :             case 'T':
     846          14 :                 success = describeTypes(pattern, show_verbose, show_system);
     847          14 :                 break;
     848          16 :             case 't':
     849             :             case 'v':
     850             :             case 'm':
     851             :             case 'i':
     852             :             case 's':
     853             :             case 'E':
     854          16 :                 success = listTables(&cmd[1], pattern, show_verbose, show_system);
     855          16 :                 break;
     856           2 :             case 'r':
     857           2 :                 if (cmd[2] == 'd' && cmd[3] == 's')
     858           2 :                 {
     859           2 :                     char       *pattern2 = NULL;
     860             : 
     861           2 :                     if (pattern)
     862           0 :                         pattern2 = psql_scan_slash_option(scan_state,
     863             :                                                           OT_NORMAL, NULL, true);
     864           2 :                     success = listDbRoleSettings(pattern, pattern2);
     865             : 
     866           2 :                     if (pattern2)
     867           0 :                         free(pattern2);
     868             :                 }
     869             :                 else
     870           0 :                     status = PSQL_CMD_UNKNOWN;
     871           2 :                 break;
     872         216 :             case 'R':
     873         216 :                 switch (cmd[2])
     874             :                 {
     875         160 :                     case 'p':
     876         160 :                         if (show_verbose)
     877         144 :                             success = describePublications(pattern);
     878             :                         else
     879          16 :                             success = listPublications(pattern);
     880         160 :                         break;
     881          56 :                     case 's':
     882          56 :                         success = describeSubscriptions(pattern, show_verbose);
     883          56 :                         break;
     884           0 :                     default:
     885           0 :                         status = PSQL_CMD_UNKNOWN;
     886             :                 }
     887         216 :                 break;
     888           0 :             case 'u':
     889           0 :                 success = describeRoles(pattern, show_verbose, show_system);
     890           0 :                 break;
     891           0 :             case 'F':           /* text search subsystem */
     892           0 :                 switch (cmd[2])
     893             :                 {
     894           0 :                     case '\0':
     895             :                     case '+':
     896           0 :                         success = listTSConfigs(pattern, show_verbose);
     897           0 :                         break;
     898           0 :                     case 'p':
     899           0 :                         success = listTSParsers(pattern, show_verbose);
     900           0 :                         break;
     901           0 :                     case 'd':
     902           0 :                         success = listTSDictionaries(pattern, show_verbose);
     903           0 :                         break;
     904           0 :                     case 't':
     905           0 :                         success = listTSTemplates(pattern, show_verbose);
     906           0 :                         break;
     907           0 :                     default:
     908           0 :                         status = PSQL_CMD_UNKNOWN;
     909           0 :                         break;
     910             :                 }
     911           0 :                 break;
     912         164 :             case 'e':           /* SQL/MED subsystem */
     913         164 :                 switch (cmd[2])
     914             :                 {
     915          48 :                     case 's':
     916          48 :                         success = listForeignServers(pattern, show_verbose);
     917          48 :                         break;
     918          40 :                     case 'u':
     919          40 :                         success = listUserMappings(pattern, show_verbose);
     920          40 :                         break;
     921          60 :                     case 'w':
     922          60 :                         success = listForeignDataWrappers(pattern, show_verbose);
     923          60 :                         break;
     924          16 :                     case 't':
     925          16 :                         success = listForeignTables(pattern, show_verbose);
     926          16 :                         break;
     927           0 :                     default:
     928           0 :                         status = PSQL_CMD_UNKNOWN;
     929           0 :                         break;
     930             :                 }
     931         164 :                 break;
     932          12 :             case 'x':           /* Extensions */
     933          12 :                 if (show_verbose)
     934          12 :                     success = listExtensionContents(pattern);
     935             :                 else
     936           0 :                     success = listExtensions(pattern);
     937          12 :                 break;
     938          36 :             case 'X':           /* Extended Statistics */
     939          36 :                 success = listExtendedStats(pattern);
     940          36 :                 break;
     941           0 :             case 'y':           /* Event Triggers */
     942           0 :                 success = listEventTriggers(pattern, show_verbose);
     943           0 :                 break;
     944           0 :             default:
     945           0 :                 status = PSQL_CMD_UNKNOWN;
     946             :         }
     947             : 
     948        2682 :         if (pattern)
     949        2380 :             free(pattern);
     950             :     }
     951             :     else
     952           4 :         ignore_slash_options(scan_state);
     953             : 
     954        2686 :     if (!success)
     955          18 :         status = PSQL_CMD_ERROR;
     956             : 
     957        2686 :     return status;
     958             : }
     959             : 
     960             : /* \df and \do; messy enough to split out of exec_command_d */
     961             : static bool
     962         174 : exec_command_dfo(PsqlScanState scan_state, const char *cmd,
     963             :                  const char *pattern,
     964             :                  bool show_verbose, bool show_system)
     965             : {
     966             :     bool        success;
     967             :     char       *arg_patterns[FUNC_MAX_ARGS];
     968         174 :     int         num_arg_patterns = 0;
     969             : 
     970             :     /* Collect argument-type patterns too */
     971         174 :     if (pattern)                /* otherwise it was just \df or \do */
     972             :     {
     973             :         char       *ap;
     974             : 
     975         226 :         while ((ap = psql_scan_slash_option(scan_state,
     976             :                                             OT_NORMAL, NULL, true)) != NULL)
     977             :         {
     978          56 :             arg_patterns[num_arg_patterns++] = ap;
     979          56 :             if (num_arg_patterns >= FUNC_MAX_ARGS)
     980           0 :                 break;          /* protect limited-size array */
     981             :         }
     982             :     }
     983             : 
     984         174 :     if (cmd[1] == 'f')
     985         158 :         success = describeFunctions(&cmd[2], pattern,
     986             :                                     arg_patterns, num_arg_patterns,
     987             :                                     show_verbose, show_system);
     988             :     else
     989          16 :         success = describeOperators(pattern,
     990             :                                     arg_patterns, num_arg_patterns,
     991             :                                     show_verbose, show_system);
     992             : 
     993         230 :     while (--num_arg_patterns >= 0)
     994          56 :         free(arg_patterns[num_arg_patterns]);
     995             : 
     996         174 :     return success;
     997             : }
     998             : 
     999             : /*
    1000             :  * \e or \edit -- edit the current query buffer, or edit a file and
    1001             :  * make it the query buffer
    1002             :  */
    1003             : static backslashResult
    1004           4 : exec_command_edit(PsqlScanState scan_state, bool active_branch,
    1005             :                   PQExpBuffer query_buf, PQExpBuffer previous_buf)
    1006             : {
    1007           4 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1008             : 
    1009           4 :     if (active_branch)
    1010             :     {
    1011           0 :         if (!query_buf)
    1012             :         {
    1013           0 :             pg_log_error("no query buffer");
    1014           0 :             status = PSQL_CMD_ERROR;
    1015             :         }
    1016             :         else
    1017             :         {
    1018             :             char       *fname;
    1019           0 :             char       *ln = NULL;
    1020           0 :             int         lineno = -1;
    1021             : 
    1022           0 :             fname = psql_scan_slash_option(scan_state,
    1023             :                                            OT_NORMAL, NULL, true);
    1024           0 :             if (fname)
    1025             :             {
    1026             :                 /* try to get separate lineno arg */
    1027           0 :                 ln = psql_scan_slash_option(scan_state,
    1028             :                                             OT_NORMAL, NULL, true);
    1029           0 :                 if (ln == NULL)
    1030             :                 {
    1031             :                     /* only one arg; maybe it is lineno not fname */
    1032           0 :                     if (fname[0] &&
    1033           0 :                         strspn(fname, "0123456789") == strlen(fname))
    1034             :                     {
    1035             :                         /* all digits, so assume it is lineno */
    1036           0 :                         ln = fname;
    1037           0 :                         fname = NULL;
    1038             :                     }
    1039             :                 }
    1040             :             }
    1041           0 :             if (ln)
    1042             :             {
    1043           0 :                 lineno = atoi(ln);
    1044           0 :                 if (lineno < 1)
    1045             :                 {
    1046           0 :                     pg_log_error("invalid line number: %s", ln);
    1047           0 :                     status = PSQL_CMD_ERROR;
    1048             :                 }
    1049             :             }
    1050           0 :             if (status != PSQL_CMD_ERROR)
    1051             :             {
    1052             :                 bool        discard_on_quit;
    1053             : 
    1054           0 :                 expand_tilde(&fname);
    1055           0 :                 if (fname)
    1056             :                 {
    1057           0 :                     canonicalize_path(fname);
    1058             :                     /* Always clear buffer if the file isn't modified */
    1059           0 :                     discard_on_quit = true;
    1060             :                 }
    1061             :                 else
    1062             :                 {
    1063             :                     /*
    1064             :                      * If query_buf is empty, recall previous query for
    1065             :                      * editing.  But in that case, the query buffer should be
    1066             :                      * emptied if editing doesn't modify the file.
    1067             :                      */
    1068           0 :                     discard_on_quit = copy_previous_query(query_buf,
    1069             :                                                           previous_buf);
    1070             :                 }
    1071             : 
    1072           0 :                 if (do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
    1073           0 :                     status = PSQL_CMD_NEWEDIT;
    1074             :                 else
    1075           0 :                     status = PSQL_CMD_ERROR;
    1076             :             }
    1077           0 :             if (fname)
    1078           0 :                 free(fname);
    1079           0 :             if (ln)
    1080           0 :                 free(ln);
    1081             :         }
    1082             :     }
    1083             :     else
    1084           4 :         ignore_slash_options(scan_state);
    1085             : 
    1086           4 :     return status;
    1087             : }
    1088             : 
    1089             : /*
    1090             :  * \ef/\ev -- edit the named function/view, or
    1091             :  * present a blank CREATE FUNCTION/VIEW template if no argument is given
    1092             :  */
    1093             : static backslashResult
    1094           8 : exec_command_ef_ev(PsqlScanState scan_state, bool active_branch,
    1095             :                    PQExpBuffer query_buf, bool is_func)
    1096             : {
    1097           8 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1098             : 
    1099           8 :     if (active_branch)
    1100             :     {
    1101           0 :         char       *obj_desc = psql_scan_slash_option(scan_state,
    1102             :                                                       OT_WHOLE_LINE,
    1103             :                                                       NULL, true);
    1104           0 :         int         lineno = -1;
    1105             : 
    1106           0 :         if (pset.sversion < (is_func ? 80400 : 70400))
    1107             :         {
    1108             :             char        sverbuf[32];
    1109             : 
    1110           0 :             formatPGVersionNumber(pset.sversion, false,
    1111             :                                   sverbuf, sizeof(sverbuf));
    1112           0 :             if (is_func)
    1113           0 :                 pg_log_error("The server (version %s) does not support editing function source.",
    1114             :                              sverbuf);
    1115             :             else
    1116           0 :                 pg_log_error("The server (version %s) does not support editing view definitions.",
    1117             :                              sverbuf);
    1118           0 :             status = PSQL_CMD_ERROR;
    1119             :         }
    1120           0 :         else if (!query_buf)
    1121             :         {
    1122           0 :             pg_log_error("no query buffer");
    1123           0 :             status = PSQL_CMD_ERROR;
    1124             :         }
    1125             :         else
    1126             :         {
    1127           0 :             Oid         obj_oid = InvalidOid;
    1128           0 :             EditableObjectType eot = is_func ? EditableFunction : EditableView;
    1129             : 
    1130           0 :             lineno = strip_lineno_from_objdesc(obj_desc);
    1131           0 :             if (lineno == 0)
    1132             :             {
    1133             :                 /* error already reported */
    1134           0 :                 status = PSQL_CMD_ERROR;
    1135             :             }
    1136           0 :             else if (!obj_desc)
    1137             :             {
    1138             :                 /* set up an empty command to fill in */
    1139           0 :                 resetPQExpBuffer(query_buf);
    1140           0 :                 if (is_func)
    1141           0 :                     appendPQExpBufferStr(query_buf,
    1142             :                                          "CREATE FUNCTION ( )\n"
    1143             :                                          " RETURNS \n"
    1144             :                                          " LANGUAGE \n"
    1145             :                                          " -- common options:  IMMUTABLE  STABLE  STRICT  SECURITY DEFINER\n"
    1146             :                                          "AS $function$\n"
    1147             :                                          "\n$function$\n");
    1148             :                 else
    1149           0 :                     appendPQExpBufferStr(query_buf,
    1150             :                                          "CREATE VIEW  AS\n"
    1151             :                                          " SELECT \n"
    1152             :                                          "  -- something...\n");
    1153             :             }
    1154           0 :             else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
    1155             :             {
    1156             :                 /* error already reported */
    1157           0 :                 status = PSQL_CMD_ERROR;
    1158             :             }
    1159           0 :             else if (!get_create_object_cmd(eot, obj_oid, query_buf))
    1160             :             {
    1161             :                 /* error already reported */
    1162           0 :                 status = PSQL_CMD_ERROR;
    1163             :             }
    1164           0 :             else if (is_func && lineno > 0)
    1165             :             {
    1166             :                 /*
    1167             :                  * lineno "1" should correspond to the first line of the
    1168             :                  * function body.  We expect that pg_get_functiondef() will
    1169             :                  * emit that on a line beginning with "AS ", and that there
    1170             :                  * can be no such line before the real start of the function
    1171             :                  * body.  Increment lineno by the number of lines before that
    1172             :                  * line, so that it becomes relative to the first line of the
    1173             :                  * function definition.
    1174             :                  */
    1175           0 :                 const char *lines = query_buf->data;
    1176             : 
    1177           0 :                 while (*lines != '\0')
    1178             :                 {
    1179           0 :                     if (strncmp(lines, "AS ", 3) == 0)
    1180           0 :                         break;
    1181           0 :                     lineno++;
    1182             :                     /* find start of next line */
    1183           0 :                     lines = strchr(lines, '\n');
    1184           0 :                     if (!lines)
    1185           0 :                         break;
    1186           0 :                     lines++;
    1187             :                 }
    1188             :             }
    1189             :         }
    1190             : 
    1191           0 :         if (status != PSQL_CMD_ERROR)
    1192             :         {
    1193           0 :             bool        edited = false;
    1194             : 
    1195           0 :             if (!do_edit(NULL, query_buf, lineno, true, &edited))
    1196           0 :                 status = PSQL_CMD_ERROR;
    1197           0 :             else if (!edited)
    1198           0 :                 puts(_("No changes"));
    1199             :             else
    1200           0 :                 status = PSQL_CMD_NEWEDIT;
    1201             :         }
    1202             : 
    1203           0 :         if (obj_desc)
    1204           0 :             free(obj_desc);
    1205             :     }
    1206             :     else
    1207           8 :         ignore_slash_whole_line(scan_state);
    1208             : 
    1209           8 :     return status;
    1210             : }
    1211             : 
    1212             : /*
    1213             :  * \echo, \qecho, and \warn -- echo arguments to stdout, query output, or stderr
    1214             :  */
    1215             : static backslashResult
    1216         418 : exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1217             : {
    1218         418 :     if (active_branch)
    1219             :     {
    1220             :         char       *value;
    1221             :         char        quoted;
    1222         326 :         bool        no_newline = false;
    1223         326 :         bool        first = true;
    1224             :         FILE       *fout;
    1225             : 
    1226         326 :         if (strcmp(cmd, "qecho") == 0)
    1227           8 :             fout = pset.queryFout;
    1228         318 :         else if (strcmp(cmd, "warn") == 0)
    1229           8 :             fout = stderr;
    1230             :         else
    1231         310 :             fout = stdout;
    1232             : 
    1233         940 :         while ((value = psql_scan_slash_option(scan_state,
    1234             :                                                OT_NORMAL, &quoted, false)))
    1235             :         {
    1236         614 :             if (first && !no_newline && !quoted && strcmp(value, "-n") == 0)
    1237           4 :                 no_newline = true;
    1238             :             else
    1239             :             {
    1240         610 :                 if (first)
    1241         326 :                     first = false;
    1242             :                 else
    1243         284 :                     fputc(' ', fout);
    1244         610 :                 fputs(value, fout);
    1245             :             }
    1246         614 :             free(value);
    1247             :         }
    1248         326 :         if (!no_newline)
    1249         322 :             fputs("\n", fout);
    1250             :     }
    1251             :     else
    1252          92 :         ignore_slash_options(scan_state);
    1253             : 
    1254         418 :     return PSQL_CMD_SKIP_LINE;
    1255             : }
    1256             : 
    1257             : /*
    1258             :  * \encoding -- set/show client side encoding
    1259             :  */
    1260             : static backslashResult
    1261           4 : exec_command_encoding(PsqlScanState scan_state, bool active_branch)
    1262             : {
    1263           4 :     if (active_branch)
    1264             :     {
    1265           0 :         char       *encoding = psql_scan_slash_option(scan_state,
    1266             :                                                       OT_NORMAL, NULL, false);
    1267             : 
    1268           0 :         if (!encoding)
    1269             :         {
    1270             :             /* show encoding */
    1271           0 :             puts(pg_encoding_to_char(pset.encoding));
    1272             :         }
    1273             :         else
    1274             :         {
    1275             :             /* set encoding */
    1276           0 :             if (PQsetClientEncoding(pset.db, encoding) == -1)
    1277           0 :                 pg_log_error("%s: invalid encoding name or conversion procedure not found", encoding);
    1278             :             else
    1279             :             {
    1280             :                 /* save encoding info into psql internal data */
    1281           0 :                 pset.encoding = PQclientEncoding(pset.db);
    1282           0 :                 pset.popt.topt.encoding = pset.encoding;
    1283           0 :                 SetVariable(pset.vars, "ENCODING",
    1284             :                             pg_encoding_to_char(pset.encoding));
    1285             :             }
    1286           0 :             free(encoding);
    1287             :         }
    1288             :     }
    1289             :     else
    1290           4 :         ignore_slash_options(scan_state);
    1291             : 
    1292           4 :     return PSQL_CMD_SKIP_LINE;
    1293             : }
    1294             : 
    1295             : /*
    1296             :  * \errverbose -- display verbose message from last failed query
    1297             :  */
    1298             : static backslashResult
    1299           4 : exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
    1300             : {
    1301           4 :     if (active_branch)
    1302             :     {
    1303           0 :         if (pset.last_error_result)
    1304             :         {
    1305             :             char       *msg;
    1306             : 
    1307           0 :             msg = PQresultVerboseErrorMessage(pset.last_error_result,
    1308             :                                               PQERRORS_VERBOSE,
    1309             :                                               PQSHOW_CONTEXT_ALWAYS);
    1310           0 :             if (msg)
    1311             :             {
    1312           0 :                 pg_log_error("%s", msg);
    1313           0 :                 PQfreemem(msg);
    1314             :             }
    1315             :             else
    1316           0 :                 puts(_("out of memory"));
    1317             :         }
    1318             :         else
    1319           0 :             puts(_("There is no previous error."));
    1320             :     }
    1321             : 
    1322           4 :     return PSQL_CMD_SKIP_LINE;
    1323             : }
    1324             : 
    1325             : /*
    1326             :  * \f -- change field separator
    1327             :  */
    1328             : static backslashResult
    1329           4 : exec_command_f(PsqlScanState scan_state, bool active_branch)
    1330             : {
    1331           4 :     bool        success = true;
    1332             : 
    1333           4 :     if (active_branch)
    1334             :     {
    1335           0 :         char       *fname = psql_scan_slash_option(scan_state,
    1336             :                                                    OT_NORMAL, NULL, false);
    1337             : 
    1338           0 :         success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
    1339           0 :         free(fname);
    1340             :     }
    1341             :     else
    1342           4 :         ignore_slash_options(scan_state);
    1343             : 
    1344           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1345             : }
    1346             : 
    1347             : /*
    1348             :  * \g  [(pset-option[=pset-value] ...)] [filename/shell-command]
    1349             :  * \gx [(pset-option[=pset-value] ...)] [filename/shell-command]
    1350             :  *
    1351             :  * Send the current query.  If pset options are specified, they are made
    1352             :  * active just for this query.  If a filename or pipe command is given,
    1353             :  * the query output goes there.  \gx implicitly sets "expanded=on" along
    1354             :  * with any other pset options that are specified.
    1355             :  */
    1356             : static backslashResult
    1357          80 : exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1358             : {
    1359          80 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1360             :     char       *fname;
    1361             : 
    1362             :     /*
    1363             :      * Because the option processing for this is fairly complicated, we do it
    1364             :      * and then decide whether the branch is active.
    1365             :      */
    1366          80 :     fname = psql_scan_slash_option(scan_state,
    1367             :                                    OT_FILEPIPE, NULL, false);
    1368             : 
    1369          80 :     if (fname && fname[0] == '(')
    1370             :     {
    1371             :         /* Consume pset options through trailing ')' ... */
    1372           8 :         status = process_command_g_options(fname + 1, scan_state,
    1373             :                                            active_branch, cmd);
    1374           8 :         free(fname);
    1375             :         /* ... and again attempt to scan the filename. */
    1376           8 :         fname = psql_scan_slash_option(scan_state,
    1377             :                                        OT_FILEPIPE, NULL, false);
    1378             :     }
    1379             : 
    1380          80 :     if (status == PSQL_CMD_SKIP_LINE && active_branch)
    1381             :     {
    1382          72 :         if (!fname)
    1383          72 :             pset.gfname = NULL;
    1384             :         else
    1385             :         {
    1386           0 :             expand_tilde(&fname);
    1387           0 :             pset.gfname = pg_strdup(fname);
    1388             :         }
    1389          72 :         if (strcmp(cmd, "gx") == 0)
    1390             :         {
    1391             :             /* save settings if not done already, then force expanded=on */
    1392          20 :             if (pset.gsavepopt == NULL)
    1393          16 :                 pset.gsavepopt = savePsetInfo(&pset.popt);
    1394          20 :             pset.popt.topt.expanded = 1;
    1395             :         }
    1396          72 :         status = PSQL_CMD_SEND;
    1397             :     }
    1398             : 
    1399          80 :     free(fname);
    1400             : 
    1401          80 :     return status;
    1402             : }
    1403             : 
    1404             : /*
    1405             :  * Process parenthesized pset options for \g
    1406             :  *
    1407             :  * Note: okay to modify first_option, but not to free it; caller does that
    1408             :  */
    1409             : static backslashResult
    1410           8 : process_command_g_options(char *first_option, PsqlScanState scan_state,
    1411             :                           bool active_branch, const char *cmd)
    1412             : {
    1413           8 :     bool        success = true;
    1414           8 :     bool        found_r_paren = false;
    1415             : 
    1416             :     do
    1417             :     {
    1418             :         char       *option;
    1419             :         size_t      optlen;
    1420             : 
    1421             :         /* If not first time through, collect a new option */
    1422          12 :         if (first_option)
    1423           8 :             option = first_option;
    1424             :         else
    1425             :         {
    1426           4 :             option = psql_scan_slash_option(scan_state,
    1427             :                                             OT_NORMAL, NULL, false);
    1428           4 :             if (!option)
    1429             :             {
    1430           0 :                 if (active_branch)
    1431             :                 {
    1432           0 :                     pg_log_error("\\%s: missing right parenthesis", cmd);
    1433           0 :                     success = false;
    1434             :                 }
    1435           0 :                 break;
    1436             :             }
    1437             :         }
    1438             : 
    1439             :         /* Check for terminating right paren, and remove it from string */
    1440          12 :         optlen = strlen(option);
    1441          12 :         if (optlen > 0 && option[optlen - 1] == ')')
    1442             :         {
    1443           8 :             option[--optlen] = '\0';
    1444           8 :             found_r_paren = true;
    1445             :         }
    1446             : 
    1447             :         /* If there was anything besides parentheses, parse/execute it */
    1448          12 :         if (optlen > 0)
    1449             :         {
    1450             :             /* We can have either "name" or "name=value" */
    1451          12 :             char       *valptr = strchr(option, '=');
    1452             : 
    1453          12 :             if (valptr)
    1454          12 :                 *valptr++ = '\0';
    1455          12 :             if (active_branch)
    1456             :             {
    1457             :                 /* save settings if not done already, then apply option */
    1458          12 :                 if (pset.gsavepopt == NULL)
    1459           8 :                     pset.gsavepopt = savePsetInfo(&pset.popt);
    1460          12 :                 success &= do_pset(option, valptr, &pset.popt, true);
    1461             :             }
    1462             :         }
    1463             : 
    1464             :         /* Clean up after this option.  We should not free first_option. */
    1465          12 :         if (first_option)
    1466           8 :             first_option = NULL;
    1467             :         else
    1468           4 :             free(option);
    1469          12 :     } while (!found_r_paren);
    1470             : 
    1471             :     /* If we failed after already changing some options, undo side-effects */
    1472           8 :     if (!success && active_branch && pset.gsavepopt)
    1473             :     {
    1474           0 :         restorePsetInfo(&pset.popt, pset.gsavepopt);
    1475           0 :         pset.gsavepopt = NULL;
    1476             :     }
    1477             : 
    1478           8 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1479             : }
    1480             : 
    1481             : /*
    1482             :  * \gdesc -- describe query result
    1483             :  */
    1484             : static backslashResult
    1485          40 : exec_command_gdesc(PsqlScanState scan_state, bool active_branch)
    1486             : {
    1487          40 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1488             : 
    1489          40 :     if (active_branch)
    1490             :     {
    1491          40 :         pset.gdesc_flag = true;
    1492          40 :         status = PSQL_CMD_SEND;
    1493             :     }
    1494             : 
    1495          40 :     return status;
    1496             : }
    1497             : 
    1498             : /*
    1499             :  * \gexec -- send query and execute each field of result
    1500             :  */
    1501             : static backslashResult
    1502          24 : exec_command_gexec(PsqlScanState scan_state, bool active_branch)
    1503             : {
    1504          24 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1505             : 
    1506          24 :     if (active_branch)
    1507             :     {
    1508          20 :         pset.gexec_flag = true;
    1509          20 :         status = PSQL_CMD_SEND;
    1510             :     }
    1511             : 
    1512          24 :     return status;
    1513             : }
    1514             : 
    1515             : /*
    1516             :  * \gset [prefix] -- send query and store result into variables
    1517             :  */
    1518             : static backslashResult
    1519         178 : exec_command_gset(PsqlScanState scan_state, bool active_branch)
    1520             : {
    1521         178 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1522             : 
    1523         178 :     if (active_branch)
    1524             :     {
    1525         174 :         char       *prefix = psql_scan_slash_option(scan_state,
    1526             :                                                     OT_NORMAL, NULL, false);
    1527             : 
    1528         174 :         if (prefix)
    1529          32 :             pset.gset_prefix = prefix;
    1530             :         else
    1531             :         {
    1532             :             /* we must set a non-NULL prefix to trigger storing */
    1533         142 :             pset.gset_prefix = pg_strdup("");
    1534             :         }
    1535             :         /* gset_prefix is freed later */
    1536         174 :         status = PSQL_CMD_SEND;
    1537             :     }
    1538             :     else
    1539           4 :         ignore_slash_options(scan_state);
    1540             : 
    1541         178 :     return status;
    1542             : }
    1543             : 
    1544             : /*
    1545             :  * \help [topic] -- print help about SQL commands
    1546             :  */
    1547             : static backslashResult
    1548           8 : exec_command_help(PsqlScanState scan_state, bool active_branch)
    1549             : {
    1550           8 :     if (active_branch)
    1551             :     {
    1552           4 :         char       *opt = psql_scan_slash_option(scan_state,
    1553             :                                                  OT_WHOLE_LINE, NULL, false);
    1554             :         size_t      len;
    1555             : 
    1556             :         /* strip any trailing spaces and semicolons */
    1557           4 :         if (opt)
    1558             :         {
    1559           2 :             len = strlen(opt);
    1560           2 :             while (len > 0 &&
    1561           2 :                    (isspace((unsigned char) opt[len - 1])
    1562           2 :                     || opt[len - 1] == ';'))
    1563           0 :                 opt[--len] = '\0';
    1564             :         }
    1565             : 
    1566           4 :         helpSQL(opt, pset.popt.topt.pager);
    1567           4 :         free(opt);
    1568             :     }
    1569             :     else
    1570           4 :         ignore_slash_whole_line(scan_state);
    1571             : 
    1572           8 :     return PSQL_CMD_SKIP_LINE;
    1573             : }
    1574             : 
    1575             : /*
    1576             :  * \H and \html -- toggle HTML formatting
    1577             :  */
    1578             : static backslashResult
    1579           4 : exec_command_html(PsqlScanState scan_state, bool active_branch)
    1580             : {
    1581           4 :     bool        success = true;
    1582             : 
    1583           4 :     if (active_branch)
    1584             :     {
    1585           0 :         if (pset.popt.topt.format != PRINT_HTML)
    1586           0 :             success = do_pset("format", "html", &pset.popt, pset.quiet);
    1587             :         else
    1588           0 :             success = do_pset("format", "aligned", &pset.popt, pset.quiet);
    1589             :     }
    1590             : 
    1591           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1592             : }
    1593             : 
    1594             : /*
    1595             :  * \i and \ir -- include a file
    1596             :  */
    1597             : static backslashResult
    1598           8 : exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1599             : {
    1600           8 :     bool        success = true;
    1601             : 
    1602           8 :     if (active_branch)
    1603             :     {
    1604           0 :         char       *fname = psql_scan_slash_option(scan_state,
    1605             :                                                    OT_NORMAL, NULL, true);
    1606             : 
    1607           0 :         if (!fname)
    1608             :         {
    1609           0 :             pg_log_error("\\%s: missing required argument", cmd);
    1610           0 :             success = false;
    1611             :         }
    1612             :         else
    1613             :         {
    1614             :             bool        include_relative;
    1615             : 
    1616           0 :             include_relative = (strcmp(cmd, "ir") == 0
    1617           0 :                                 || strcmp(cmd, "include_relative") == 0);
    1618           0 :             expand_tilde(&fname);
    1619           0 :             success = (process_file(fname, include_relative) == EXIT_SUCCESS);
    1620           0 :             free(fname);
    1621             :         }
    1622             :     }
    1623             :     else
    1624           8 :         ignore_slash_options(scan_state);
    1625             : 
    1626           8 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1627             : }
    1628             : 
    1629             : /*
    1630             :  * \if <expr> -- beginning of an \if..\endif block
    1631             :  *
    1632             :  * <expr> is parsed as a boolean expression.  Invalid expressions will emit a
    1633             :  * warning and be treated as false.  Statements that follow a false expression
    1634             :  * will be parsed but ignored.  Note that in the case where an \if statement
    1635             :  * is itself within an inactive section of a block, then the entire inner
    1636             :  * \if..\endif block will be parsed but ignored.
    1637             :  */
    1638             : static backslashResult
    1639         102 : exec_command_if(PsqlScanState scan_state, ConditionalStack cstack,
    1640             :                 PQExpBuffer query_buf)
    1641             : {
    1642         102 :     if (conditional_active(cstack))
    1643             :     {
    1644             :         /*
    1645             :          * First, push a new active stack entry; this ensures that the lexer
    1646             :          * will perform variable substitution and backtick evaluation while
    1647             :          * scanning the expression.  (That should happen anyway, since we know
    1648             :          * we're in an active outer branch, but let's be sure.)
    1649             :          */
    1650          98 :         conditional_stack_push(cstack, IFSTATE_TRUE);
    1651             : 
    1652             :         /* Remember current query state in case we need to restore later */
    1653          98 :         save_query_text_state(scan_state, cstack, query_buf);
    1654             : 
    1655             :         /*
    1656             :          * Evaluate the expression; if it's false, change to inactive state.
    1657             :          */
    1658          98 :         if (!is_true_boolean_expression(scan_state, "\\if expression"))
    1659          50 :             conditional_stack_poke(cstack, IFSTATE_FALSE);
    1660             :     }
    1661             :     else
    1662             :     {
    1663             :         /*
    1664             :          * We're within an inactive outer branch, so this entire \if block
    1665             :          * will be ignored.  We don't want to evaluate the expression, so push
    1666             :          * the "ignored" stack state before scanning it.
    1667             :          */
    1668           4 :         conditional_stack_push(cstack, IFSTATE_IGNORED);
    1669             : 
    1670             :         /* Remember current query state in case we need to restore later */
    1671           4 :         save_query_text_state(scan_state, cstack, query_buf);
    1672             : 
    1673           4 :         ignore_boolean_expression(scan_state);
    1674             :     }
    1675             : 
    1676         102 :     return PSQL_CMD_SKIP_LINE;
    1677             : }
    1678             : 
    1679             : /*
    1680             :  * \elif <expr> -- alternative branch in an \if..\endif block
    1681             :  *
    1682             :  * <expr> is evaluated the same as in \if <expr>.
    1683             :  */
    1684             : static backslashResult
    1685          32 : exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack,
    1686             :                   PQExpBuffer query_buf)
    1687             : {
    1688          32 :     bool        success = true;
    1689             : 
    1690          32 :     switch (conditional_stack_peek(cstack))
    1691             :     {
    1692           4 :         case IFSTATE_TRUE:
    1693             : 
    1694             :             /*
    1695             :              * Just finished active branch of this \if block.  Update saved
    1696             :              * state so we will keep whatever data was put in query_buf by the
    1697             :              * active branch.
    1698             :              */
    1699           4 :             save_query_text_state(scan_state, cstack, query_buf);
    1700             : 
    1701             :             /*
    1702             :              * Discard \elif expression and ignore the rest until \endif.
    1703             :              * Switch state before reading expression to ensure proper lexer
    1704             :              * behavior.
    1705             :              */
    1706           4 :             conditional_stack_poke(cstack, IFSTATE_IGNORED);
    1707           4 :             ignore_boolean_expression(scan_state);
    1708           4 :             break;
    1709          16 :         case IFSTATE_FALSE:
    1710             : 
    1711             :             /*
    1712             :              * Discard any query text added by the just-skipped branch.
    1713             :              */
    1714          16 :             discard_query_text(scan_state, cstack, query_buf);
    1715             : 
    1716             :             /*
    1717             :              * Have not yet found a true expression in this \if block, so this
    1718             :              * might be the first.  We have to change state before examining
    1719             :              * the expression, or the lexer won't do the right thing.
    1720             :              */
    1721          16 :             conditional_stack_poke(cstack, IFSTATE_TRUE);
    1722          16 :             if (!is_true_boolean_expression(scan_state, "\\elif expression"))
    1723          12 :                 conditional_stack_poke(cstack, IFSTATE_FALSE);
    1724          16 :             break;
    1725           4 :         case IFSTATE_IGNORED:
    1726             : 
    1727             :             /*
    1728             :              * Discard any query text added by the just-skipped branch.
    1729             :              */
    1730           4 :             discard_query_text(scan_state, cstack, query_buf);
    1731             : 
    1732             :             /*
    1733             :              * Skip expression and move on.  Either the \if block already had
    1734             :              * an active section, or whole block is being skipped.
    1735             :              */
    1736           4 :             ignore_boolean_expression(scan_state);
    1737           4 :             break;
    1738           4 :         case IFSTATE_ELSE_TRUE:
    1739             :         case IFSTATE_ELSE_FALSE:
    1740           4 :             pg_log_error("\\elif: cannot occur after \\else");
    1741           4 :             success = false;
    1742           4 :             break;
    1743           4 :         case IFSTATE_NONE:
    1744             :             /* no \if to elif from */
    1745           4 :             pg_log_error("\\elif: no matching \\if");
    1746           4 :             success = false;
    1747           4 :             break;
    1748             :     }
    1749             : 
    1750          32 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1751             : }
    1752             : 
    1753             : /*
    1754             :  * \else -- final alternative in an \if..\endif block
    1755             :  *
    1756             :  * Statements within an \else branch will only be executed if
    1757             :  * all previous \if and \elif expressions evaluated to false
    1758             :  * and the block was not itself being ignored.
    1759             :  */
    1760             : static backslashResult
    1761          84 : exec_command_else(PsqlScanState scan_state, ConditionalStack cstack,
    1762             :                   PQExpBuffer query_buf)
    1763             : {
    1764          84 :     bool        success = true;
    1765             : 
    1766          84 :     switch (conditional_stack_peek(cstack))
    1767             :     {
    1768          40 :         case IFSTATE_TRUE:
    1769             : 
    1770             :             /*
    1771             :              * Just finished active branch of this \if block.  Update saved
    1772             :              * state so we will keep whatever data was put in query_buf by the
    1773             :              * active branch.
    1774             :              */
    1775          40 :             save_query_text_state(scan_state, cstack, query_buf);
    1776             : 
    1777             :             /* Now skip the \else branch */
    1778          40 :             conditional_stack_poke(cstack, IFSTATE_ELSE_FALSE);
    1779          40 :             break;
    1780          28 :         case IFSTATE_FALSE:
    1781             : 
    1782             :             /*
    1783             :              * Discard any query text added by the just-skipped branch.
    1784             :              */
    1785          28 :             discard_query_text(scan_state, cstack, query_buf);
    1786             : 
    1787             :             /*
    1788             :              * We've not found any true \if or \elif expression, so execute
    1789             :              * the \else branch.
    1790             :              */
    1791          28 :             conditional_stack_poke(cstack, IFSTATE_ELSE_TRUE);
    1792          28 :             break;
    1793           8 :         case IFSTATE_IGNORED:
    1794             : 
    1795             :             /*
    1796             :              * Discard any query text added by the just-skipped branch.
    1797             :              */
    1798           8 :             discard_query_text(scan_state, cstack, query_buf);
    1799             : 
    1800             :             /*
    1801             :              * Either we previously processed the active branch of this \if,
    1802             :              * or the whole \if block is being skipped.  Either way, skip the
    1803             :              * \else branch.
    1804             :              */
    1805           8 :             conditional_stack_poke(cstack, IFSTATE_ELSE_FALSE);
    1806           8 :             break;
    1807           4 :         case IFSTATE_ELSE_TRUE:
    1808             :         case IFSTATE_ELSE_FALSE:
    1809           4 :             pg_log_error("\\else: cannot occur after \\else");
    1810           4 :             success = false;
    1811           4 :             break;
    1812           4 :         case IFSTATE_NONE:
    1813             :             /* no \if to else from */
    1814           4 :             pg_log_error("\\else: no matching \\if");
    1815           4 :             success = false;
    1816           4 :             break;
    1817             :     }
    1818             : 
    1819          84 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1820             : }
    1821             : 
    1822             : /*
    1823             :  * \endif -- ends an \if...\endif block
    1824             :  */
    1825             : static backslashResult
    1826          98 : exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack,
    1827             :                    PQExpBuffer query_buf)
    1828             : {
    1829          98 :     bool        success = true;
    1830             : 
    1831          98 :     switch (conditional_stack_peek(cstack))
    1832             :     {
    1833          28 :         case IFSTATE_TRUE:
    1834             :         case IFSTATE_ELSE_TRUE:
    1835             :             /* Close the \if block, keeping the query text */
    1836          28 :             success = conditional_stack_pop(cstack);
    1837             :             Assert(success);
    1838          28 :             break;
    1839          66 :         case IFSTATE_FALSE:
    1840             :         case IFSTATE_IGNORED:
    1841             :         case IFSTATE_ELSE_FALSE:
    1842             : 
    1843             :             /*
    1844             :              * Discard any query text added by the just-skipped branch.
    1845             :              */
    1846          66 :             discard_query_text(scan_state, cstack, query_buf);
    1847             : 
    1848             :             /* Close the \if block */
    1849          66 :             success = conditional_stack_pop(cstack);
    1850             :             Assert(success);
    1851          66 :             break;
    1852           4 :         case IFSTATE_NONE:
    1853             :             /* no \if to end */
    1854           4 :             pg_log_error("\\endif: no matching \\if");
    1855           4 :             success = false;
    1856           4 :             break;
    1857             :     }
    1858             : 
    1859          98 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1860             : }
    1861             : 
    1862             : /*
    1863             :  * \l -- list databases
    1864             :  */
    1865             : static backslashResult
    1866           4 : exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1867             : {
    1868           4 :     bool        success = true;
    1869             : 
    1870           4 :     if (active_branch)
    1871             :     {
    1872             :         char       *pattern;
    1873             :         bool        show_verbose;
    1874             : 
    1875           0 :         pattern = psql_scan_slash_option(scan_state,
    1876             :                                          OT_NORMAL, NULL, true);
    1877             : 
    1878           0 :         show_verbose = strchr(cmd, '+') ? true : false;
    1879             : 
    1880           0 :         success = listAllDbs(pattern, show_verbose);
    1881             : 
    1882           0 :         if (pattern)
    1883           0 :             free(pattern);
    1884             :     }
    1885             :     else
    1886           4 :         ignore_slash_options(scan_state);
    1887             : 
    1888           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1889             : }
    1890             : 
    1891             : /*
    1892             :  * \lo_* -- large object operations
    1893             :  */
    1894             : static backslashResult
    1895          32 : exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1896             : {
    1897          32 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1898          32 :     bool        success = true;
    1899             : 
    1900          32 :     if (active_branch)
    1901             :     {
    1902             :         char       *opt1,
    1903             :                    *opt2;
    1904             : 
    1905          28 :         opt1 = psql_scan_slash_option(scan_state,
    1906             :                                       OT_NORMAL, NULL, true);
    1907          28 :         opt2 = psql_scan_slash_option(scan_state,
    1908             :                                       OT_NORMAL, NULL, true);
    1909             : 
    1910          28 :         if (strcmp(cmd + 3, "export") == 0)
    1911             :         {
    1912           4 :             if (!opt2)
    1913             :             {
    1914           0 :                 pg_log_error("\\%s: missing required argument", cmd);
    1915           0 :                 success = false;
    1916             :             }
    1917             :             else
    1918             :             {
    1919           4 :                 expand_tilde(&opt2);
    1920           4 :                 success = do_lo_export(opt1, opt2);
    1921             :             }
    1922             :         }
    1923             : 
    1924          24 :         else if (strcmp(cmd + 3, "import") == 0)
    1925             :         {
    1926          12 :             if (!opt1)
    1927             :             {
    1928           0 :                 pg_log_error("\\%s: missing required argument", cmd);
    1929           0 :                 success = false;
    1930             :             }
    1931             :             else
    1932             :             {
    1933          12 :                 expand_tilde(&opt1);
    1934          12 :                 success = do_lo_import(opt1, opt2);
    1935             :             }
    1936             :         }
    1937             : 
    1938          12 :         else if (strcmp(cmd + 3, "list") == 0)
    1939           0 :             success = do_lo_list();
    1940             : 
    1941          12 :         else if (strcmp(cmd + 3, "unlink") == 0)
    1942             :         {
    1943          12 :             if (!opt1)
    1944             :             {
    1945           0 :                 pg_log_error("\\%s: missing required argument", cmd);
    1946           0 :                 success = false;
    1947             :             }
    1948             :             else
    1949          12 :                 success = do_lo_unlink(opt1);
    1950             :         }
    1951             : 
    1952             :         else
    1953           0 :             status = PSQL_CMD_UNKNOWN;
    1954             : 
    1955          28 :         free(opt1);
    1956          28 :         free(opt2);
    1957             :     }
    1958             :     else
    1959           4 :         ignore_slash_options(scan_state);
    1960             : 
    1961          32 :     if (!success)
    1962           2 :         status = PSQL_CMD_ERROR;
    1963             : 
    1964          32 :     return status;
    1965             : }
    1966             : 
    1967             : /*
    1968             :  * \o -- set query output
    1969             :  */
    1970             : static backslashResult
    1971           4 : exec_command_out(PsqlScanState scan_state, bool active_branch)
    1972             : {
    1973           4 :     bool        success = true;
    1974             : 
    1975           4 :     if (active_branch)
    1976             :     {
    1977           0 :         char       *fname = psql_scan_slash_option(scan_state,
    1978             :                                                    OT_FILEPIPE, NULL, true);
    1979             : 
    1980           0 :         expand_tilde(&fname);
    1981           0 :         success = setQFout(fname);
    1982           0 :         free(fname);
    1983             :     }
    1984             :     else
    1985           4 :         ignore_slash_filepipe(scan_state);
    1986             : 
    1987           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    1988             : }
    1989             : 
    1990             : /*
    1991             :  * \p -- print the current query buffer
    1992             :  */
    1993             : static backslashResult
    1994          28 : exec_command_print(PsqlScanState scan_state, bool active_branch,
    1995             :                    PQExpBuffer query_buf, PQExpBuffer previous_buf)
    1996             : {
    1997          28 :     if (active_branch)
    1998             :     {
    1999             :         /*
    2000             :          * We want to print the same thing \g would execute, but not to change
    2001             :          * the query buffer state; so we can't use copy_previous_query().
    2002             :          * Also, beware of possibility that buffer pointers are NULL.
    2003             :          */
    2004          24 :         if (query_buf && query_buf->len > 0)
    2005          12 :             puts(query_buf->data);
    2006          12 :         else if (previous_buf && previous_buf->len > 0)
    2007          12 :             puts(previous_buf->data);
    2008           0 :         else if (!pset.quiet)
    2009           0 :             puts(_("Query buffer is empty."));
    2010          24 :         fflush(stdout);
    2011             :     }
    2012             : 
    2013          28 :     return PSQL_CMD_SKIP_LINE;
    2014             : }
    2015             : 
    2016             : /*
    2017             :  * \password -- set user password
    2018             :  */
    2019             : static backslashResult
    2020           4 : exec_command_password(PsqlScanState scan_state, bool active_branch)
    2021             : {
    2022           4 :     bool        success = true;
    2023             : 
    2024           4 :     if (active_branch)
    2025             :     {
    2026           0 :         char       *user = psql_scan_slash_option(scan_state,
    2027             :                                                   OT_SQLID, NULL, true);
    2028           0 :         char       *pw1 = NULL;
    2029           0 :         char       *pw2 = NULL;
    2030             :         PQExpBufferData buf;
    2031             :         PromptInterruptContext prompt_ctx;
    2032             : 
    2033           0 :         if (user == NULL)
    2034             :         {
    2035             :             /* By default, the command applies to CURRENT_USER */
    2036             :             PGresult   *res;
    2037             : 
    2038           0 :             res = PSQLexec("SELECT CURRENT_USER");
    2039           0 :             if (!res)
    2040           0 :                 return PSQL_CMD_ERROR;
    2041             : 
    2042           0 :             user = pg_strdup(PQgetvalue(res, 0, 0));
    2043           0 :             PQclear(res);
    2044             :         }
    2045             : 
    2046             :         /* Set up to let SIGINT cancel simple_prompt_extended() */
    2047           0 :         prompt_ctx.jmpbuf = sigint_interrupt_jmp;
    2048           0 :         prompt_ctx.enabled = &sigint_interrupt_enabled;
    2049           0 :         prompt_ctx.canceled = false;
    2050             : 
    2051           0 :         initPQExpBuffer(&buf);
    2052           0 :         printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
    2053             : 
    2054           0 :         pw1 = simple_prompt_extended(buf.data, false, &prompt_ctx);
    2055           0 :         if (!prompt_ctx.canceled)
    2056           0 :             pw2 = simple_prompt_extended("Enter it again: ", false, &prompt_ctx);
    2057             : 
    2058           0 :         if (prompt_ctx.canceled)
    2059             :         {
    2060             :             /* fail silently */
    2061           0 :             success = false;
    2062             :         }
    2063           0 :         else if (strcmp(pw1, pw2) != 0)
    2064             :         {
    2065           0 :             pg_log_error("Passwords didn't match.");
    2066           0 :             success = false;
    2067             :         }
    2068             :         else
    2069             :         {
    2070             :             char       *encrypted_password;
    2071             : 
    2072           0 :             encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
    2073             : 
    2074           0 :             if (!encrypted_password)
    2075             :             {
    2076           0 :                 pg_log_info("%s", PQerrorMessage(pset.db));
    2077           0 :                 success = false;
    2078             :             }
    2079             :             else
    2080             :             {
    2081             :                 PGresult   *res;
    2082             : 
    2083           0 :                 printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
    2084             :                                   fmtId(user));
    2085           0 :                 appendStringLiteralConn(&buf, encrypted_password, pset.db);
    2086           0 :                 res = PSQLexec(buf.data);
    2087           0 :                 if (!res)
    2088           0 :                     success = false;
    2089             :                 else
    2090           0 :                     PQclear(res);
    2091           0 :                 PQfreemem(encrypted_password);
    2092             :             }
    2093             :         }
    2094             : 
    2095           0 :         free(user);
    2096           0 :         if (pw1)
    2097           0 :             free(pw1);
    2098           0 :         if (pw2)
    2099           0 :             free(pw2);
    2100           0 :         termPQExpBuffer(&buf);
    2101             :     }
    2102             :     else
    2103           4 :         ignore_slash_options(scan_state);
    2104             : 
    2105           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2106             : }
    2107             : 
    2108             : /*
    2109             :  * \prompt -- prompt and set variable
    2110             :  */
    2111             : static backslashResult
    2112           4 : exec_command_prompt(PsqlScanState scan_state, bool active_branch,
    2113             :                     const char *cmd)
    2114             : {
    2115           4 :     bool        success = true;
    2116             : 
    2117           4 :     if (active_branch)
    2118             :     {
    2119             :         char       *opt,
    2120           0 :                    *prompt_text = NULL;
    2121             :         char       *arg1,
    2122             :                    *arg2;
    2123             : 
    2124           0 :         arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
    2125           0 :         arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
    2126             : 
    2127           0 :         if (!arg1)
    2128             :         {
    2129           0 :             pg_log_error("\\%s: missing required argument", cmd);
    2130           0 :             success = false;
    2131             :         }
    2132             :         else
    2133             :         {
    2134             :             char       *result;
    2135             :             PromptInterruptContext prompt_ctx;
    2136             : 
    2137             :             /* Set up to let SIGINT cancel simple_prompt_extended() */
    2138           0 :             prompt_ctx.jmpbuf = sigint_interrupt_jmp;
    2139           0 :             prompt_ctx.enabled = &sigint_interrupt_enabled;
    2140           0 :             prompt_ctx.canceled = false;
    2141             : 
    2142           0 :             if (arg2)
    2143             :             {
    2144           0 :                 prompt_text = arg1;
    2145           0 :                 opt = arg2;
    2146             :             }
    2147             :             else
    2148           0 :                 opt = arg1;
    2149             : 
    2150           0 :             if (!pset.inputfile)
    2151             :             {
    2152           0 :                 result = simple_prompt_extended(prompt_text, true, &prompt_ctx);
    2153             :             }
    2154             :             else
    2155             :             {
    2156           0 :                 if (prompt_text)
    2157             :                 {
    2158           0 :                     fputs(prompt_text, stdout);
    2159           0 :                     fflush(stdout);
    2160             :                 }
    2161           0 :                 result = gets_fromFile(stdin);
    2162           0 :                 if (!result)
    2163             :                 {
    2164           0 :                     pg_log_error("\\%s: could not read value for variable",
    2165             :                                  cmd);
    2166           0 :                     success = false;
    2167             :                 }
    2168             :             }
    2169             : 
    2170           0 :             if (prompt_ctx.canceled ||
    2171           0 :                 (result && !SetVariable(pset.vars, opt, result)))
    2172           0 :                 success = false;
    2173             : 
    2174           0 :             if (result)
    2175           0 :                 free(result);
    2176           0 :             if (prompt_text)
    2177           0 :                 free(prompt_text);
    2178           0 :             free(opt);
    2179             :         }
    2180             :     }
    2181             :     else
    2182           4 :         ignore_slash_options(scan_state);
    2183             : 
    2184           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2185             : }
    2186             : 
    2187             : /*
    2188             :  * \pset -- set printing parameters
    2189             :  */
    2190             : static backslashResult
    2191        1148 : exec_command_pset(PsqlScanState scan_state, bool active_branch)
    2192             : {
    2193        1148 :     bool        success = true;
    2194             : 
    2195        1148 :     if (active_branch)
    2196             :     {
    2197        1140 :         char       *opt0 = psql_scan_slash_option(scan_state,
    2198             :                                                   OT_NORMAL, NULL, false);
    2199        1140 :         char       *opt1 = psql_scan_slash_option(scan_state,
    2200             :                                                   OT_NORMAL, NULL, false);
    2201             : 
    2202        1140 :         if (!opt0)
    2203             :         {
    2204             :             /* list all variables */
    2205             : 
    2206             :             int         i;
    2207             :             static const char *const my_list[] = {
    2208             :                 "border", "columns", "csv_fieldsep", "expanded", "fieldsep",
    2209             :                 "fieldsep_zero", "footer", "format", "linestyle", "null",
    2210             :                 "numericlocale", "pager", "pager_min_lines",
    2211             :                 "recordsep", "recordsep_zero",
    2212             :                 "tableattr", "title", "tuples_only",
    2213             :                 "unicode_border_linestyle",
    2214             :                 "unicode_column_linestyle",
    2215             :                 "unicode_header_linestyle",
    2216             :                 NULL
    2217             :             };
    2218             : 
    2219          88 :             for (i = 0; my_list[i] != NULL; i++)
    2220             :             {
    2221          84 :                 char       *val = pset_value_string(my_list[i], &pset.popt);
    2222             : 
    2223          84 :                 printf("%-24s %s\n", my_list[i], val);
    2224          84 :                 free(val);
    2225             :             }
    2226             : 
    2227           4 :             success = true;
    2228             :         }
    2229             :         else
    2230        1136 :             success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
    2231             : 
    2232        1140 :         free(opt0);
    2233        1140 :         free(opt1);
    2234             :     }
    2235             :     else
    2236           8 :         ignore_slash_options(scan_state);
    2237             : 
    2238        1148 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2239             : }
    2240             : 
    2241             : /*
    2242             :  * \q or \quit -- exit psql
    2243             :  */
    2244             : static backslashResult
    2245          40 : exec_command_quit(PsqlScanState scan_state, bool active_branch)
    2246             : {
    2247          40 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    2248             : 
    2249          40 :     if (active_branch)
    2250          18 :         status = PSQL_CMD_TERMINATE;
    2251             : 
    2252          40 :     return status;
    2253             : }
    2254             : 
    2255             : /*
    2256             :  * \r -- reset (clear) the query buffer
    2257             :  */
    2258             : static backslashResult
    2259          26 : exec_command_reset(PsqlScanState scan_state, bool active_branch,
    2260             :                    PQExpBuffer query_buf)
    2261             : {
    2262          26 :     if (active_branch)
    2263             :     {
    2264          22 :         resetPQExpBuffer(query_buf);
    2265          22 :         psql_scan_reset(scan_state);
    2266          22 :         if (!pset.quiet)
    2267          10 :             puts(_("Query buffer reset (cleared)."));
    2268             :     }
    2269             : 
    2270          26 :     return PSQL_CMD_SKIP_LINE;
    2271             : }
    2272             : 
    2273             : /*
    2274             :  * \s -- save history in a file or show it on the screen
    2275             :  */
    2276             : static backslashResult
    2277           4 : exec_command_s(PsqlScanState scan_state, bool active_branch)
    2278             : {
    2279           4 :     bool        success = true;
    2280             : 
    2281           4 :     if (active_branch)
    2282             :     {
    2283           0 :         char       *fname = psql_scan_slash_option(scan_state,
    2284             :                                                    OT_NORMAL, NULL, true);
    2285             : 
    2286           0 :         expand_tilde(&fname);
    2287           0 :         success = printHistory(fname, pset.popt.topt.pager);
    2288           0 :         if (success && !pset.quiet && fname)
    2289           0 :             printf(_("Wrote history to file \"%s\".\n"), fname);
    2290           0 :         if (!fname)
    2291           0 :             putchar('\n');
    2292           0 :         free(fname);
    2293             :     }
    2294             :     else
    2295           4 :         ignore_slash_options(scan_state);
    2296             : 
    2297           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2298             : }
    2299             : 
    2300             : /*
    2301             :  * \set -- set variable
    2302             :  */
    2303             : static backslashResult
    2304         288 : exec_command_set(PsqlScanState scan_state, bool active_branch)
    2305             : {
    2306         288 :     bool        success = true;
    2307             : 
    2308         288 :     if (active_branch)
    2309             :     {
    2310         284 :         char       *opt0 = psql_scan_slash_option(scan_state,
    2311             :                                                   OT_NORMAL, NULL, false);
    2312             : 
    2313         284 :         if (!opt0)
    2314             :         {
    2315             :             /* list all variables */
    2316           0 :             PrintVariables(pset.vars);
    2317           0 :             success = true;
    2318             :         }
    2319             :         else
    2320             :         {
    2321             :             /*
    2322             :              * Set variable to the concatenation of the arguments.
    2323             :              */
    2324             :             char       *newval;
    2325             :             char       *opt;
    2326             : 
    2327         284 :             opt = psql_scan_slash_option(scan_state,
    2328             :                                          OT_NORMAL, NULL, false);
    2329         284 :             newval = pg_strdup(opt ? opt : "");
    2330         284 :             free(opt);
    2331             : 
    2332         284 :             while ((opt = psql_scan_slash_option(scan_state,
    2333             :                                                  OT_NORMAL, NULL, false)))
    2334             :             {
    2335           0 :                 newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
    2336           0 :                 strcat(newval, opt);
    2337           0 :                 free(opt);
    2338             :             }
    2339             : 
    2340         284 :             if (!SetVariable(pset.vars, opt0, newval))
    2341          16 :                 success = false;
    2342             : 
    2343         284 :             free(newval);
    2344             :         }
    2345         284 :         free(opt0);
    2346             :     }
    2347             :     else
    2348           4 :         ignore_slash_options(scan_state);
    2349             : 
    2350         288 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2351             : }
    2352             : 
    2353             : /*
    2354             :  * \setenv -- set environment variable
    2355             :  */
    2356             : static backslashResult
    2357           4 : exec_command_setenv(PsqlScanState scan_state, bool active_branch,
    2358             :                     const char *cmd)
    2359             : {
    2360           4 :     bool        success = true;
    2361             : 
    2362           4 :     if (active_branch)
    2363             :     {
    2364           0 :         char       *envvar = psql_scan_slash_option(scan_state,
    2365             :                                                     OT_NORMAL, NULL, false);
    2366           0 :         char       *envval = psql_scan_slash_option(scan_state,
    2367             :                                                     OT_NORMAL, NULL, false);
    2368             : 
    2369           0 :         if (!envvar)
    2370             :         {
    2371           0 :             pg_log_error("\\%s: missing required argument", cmd);
    2372           0 :             success = false;
    2373             :         }
    2374           0 :         else if (strchr(envvar, '=') != NULL)
    2375             :         {
    2376           0 :             pg_log_error("\\%s: environment variable name must not contain \"=\"",
    2377             :                          cmd);
    2378           0 :             success = false;
    2379             :         }
    2380           0 :         else if (!envval)
    2381             :         {
    2382             :             /* No argument - unset the environment variable */
    2383           0 :             unsetenv(envvar);
    2384           0 :             success = true;
    2385             :         }
    2386             :         else
    2387             :         {
    2388             :             /* Set variable to the value of the next argument */
    2389           0 :             setenv(envvar, envval, 1);
    2390           0 :             success = true;
    2391             :         }
    2392           0 :         free(envvar);
    2393           0 :         free(envval);
    2394             :     }
    2395             :     else
    2396           4 :         ignore_slash_options(scan_state);
    2397             : 
    2398           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2399             : }
    2400             : 
    2401             : /*
    2402             :  * \sf/\sv -- show a function/view's source code
    2403             :  */
    2404             : static backslashResult
    2405          18 : exec_command_sf_sv(PsqlScanState scan_state, bool active_branch,
    2406             :                    const char *cmd, bool is_func)
    2407             : {
    2408          18 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    2409             : 
    2410          18 :     if (active_branch)
    2411             :     {
    2412          10 :         bool        show_linenumbers = (strchr(cmd, '+') != NULL);
    2413             :         PQExpBuffer buf;
    2414             :         char       *obj_desc;
    2415          10 :         Oid         obj_oid = InvalidOid;
    2416          10 :         EditableObjectType eot = is_func ? EditableFunction : EditableView;
    2417             : 
    2418          10 :         buf = createPQExpBuffer();
    2419          10 :         obj_desc = psql_scan_slash_option(scan_state,
    2420             :                                           OT_WHOLE_LINE, NULL, true);
    2421          10 :         if (pset.sversion < (is_func ? 80400 : 70400))
    2422             :         {
    2423             :             char        sverbuf[32];
    2424             : 
    2425           0 :             formatPGVersionNumber(pset.sversion, false,
    2426             :                                   sverbuf, sizeof(sverbuf));
    2427           0 :             if (is_func)
    2428           0 :                 pg_log_error("The server (version %s) does not support showing function source.",
    2429             :                              sverbuf);
    2430             :             else
    2431           0 :                 pg_log_error("The server (version %s) does not support showing view definitions.",
    2432             :                              sverbuf);
    2433           0 :             status = PSQL_CMD_ERROR;
    2434             :         }
    2435          10 :         else if (!obj_desc)
    2436             :         {
    2437           0 :             if (is_func)
    2438           0 :                 pg_log_error("function name is required");
    2439             :             else
    2440           0 :                 pg_log_error("view name is required");
    2441           0 :             status = PSQL_CMD_ERROR;
    2442             :         }
    2443          10 :         else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
    2444             :         {
    2445             :             /* error already reported */
    2446           0 :             status = PSQL_CMD_ERROR;
    2447             :         }
    2448          10 :         else if (!get_create_object_cmd(eot, obj_oid, buf))
    2449             :         {
    2450             :             /* error already reported */
    2451           0 :             status = PSQL_CMD_ERROR;
    2452             :         }
    2453             :         else
    2454             :         {
    2455             :             FILE       *output;
    2456             :             bool        is_pager;
    2457             : 
    2458             :             /* Select output stream: stdout, pager, or file */
    2459          10 :             if (pset.queryFout == stdout)
    2460             :             {
    2461             :                 /* count lines in function to see if pager is needed */
    2462          10 :                 int         lineno = count_lines_in_buf(buf);
    2463             : 
    2464          10 :                 output = PageOutput(lineno, &(pset.popt.topt));
    2465          10 :                 is_pager = true;
    2466             :             }
    2467             :             else
    2468             :             {
    2469             :                 /* use previously set output file, without pager */
    2470           0 :                 output = pset.queryFout;
    2471           0 :                 is_pager = false;
    2472             :             }
    2473             : 
    2474          10 :             if (show_linenumbers)
    2475             :             {
    2476             :                 /*
    2477             :                  * For functions, lineno "1" should correspond to the first
    2478             :                  * line of the function body.  We expect that
    2479             :                  * pg_get_functiondef() will emit that on a line beginning
    2480             :                  * with "AS ", and that there can be no such line before the
    2481             :                  * real start of the function body.
    2482             :                  */
    2483           0 :                 print_with_linenumbers(output, buf->data,
    2484             :                                        is_func ? "AS " : NULL);
    2485             :             }
    2486             :             else
    2487             :             {
    2488             :                 /* just send the definition to output */
    2489          10 :                 fputs(buf->data, output);
    2490             :             }
    2491             : 
    2492          10 :             if (is_pager)
    2493          10 :                 ClosePager(output);
    2494             :         }
    2495             : 
    2496          10 :         if (obj_desc)
    2497          10 :             free(obj_desc);
    2498          10 :         destroyPQExpBuffer(buf);
    2499             :     }
    2500             :     else
    2501           8 :         ignore_slash_whole_line(scan_state);
    2502             : 
    2503          18 :     return status;
    2504             : }
    2505             : 
    2506             : /*
    2507             :  * \t -- turn off table headers and row count
    2508             :  */
    2509             : static backslashResult
    2510          52 : exec_command_t(PsqlScanState scan_state, bool active_branch)
    2511             : {
    2512          52 :     bool        success = true;
    2513             : 
    2514          52 :     if (active_branch)
    2515             :     {
    2516          48 :         char       *opt = psql_scan_slash_option(scan_state,
    2517             :                                                  OT_NORMAL, NULL, true);
    2518             : 
    2519          48 :         success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
    2520          48 :         free(opt);
    2521             :     }
    2522             :     else
    2523           4 :         ignore_slash_options(scan_state);
    2524             : 
    2525          52 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2526             : }
    2527             : 
    2528             : /*
    2529             :  * \T -- define html <table ...> attributes
    2530             :  */
    2531             : static backslashResult
    2532           4 : exec_command_T(PsqlScanState scan_state, bool active_branch)
    2533             : {
    2534           4 :     bool        success = true;
    2535             : 
    2536           4 :     if (active_branch)
    2537             :     {
    2538           0 :         char       *value = psql_scan_slash_option(scan_state,
    2539             :                                                    OT_NORMAL, NULL, false);
    2540             : 
    2541           0 :         success = do_pset("tableattr", value, &pset.popt, pset.quiet);
    2542           0 :         free(value);
    2543             :     }
    2544             :     else
    2545           4 :         ignore_slash_options(scan_state);
    2546             : 
    2547           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2548             : }
    2549             : 
    2550             : /*
    2551             :  * \timing -- enable/disable timing of queries
    2552             :  */
    2553             : static backslashResult
    2554           4 : exec_command_timing(PsqlScanState scan_state, bool active_branch)
    2555             : {
    2556           4 :     bool        success = true;
    2557             : 
    2558           4 :     if (active_branch)
    2559             :     {
    2560           0 :         char       *opt = psql_scan_slash_option(scan_state,
    2561             :                                                  OT_NORMAL, NULL, false);
    2562             : 
    2563           0 :         if (opt)
    2564           0 :             success = ParseVariableBool(opt, "\\timing", &pset.timing);
    2565             :         else
    2566           0 :             pset.timing = !pset.timing;
    2567           0 :         if (!pset.quiet)
    2568             :         {
    2569           0 :             if (pset.timing)
    2570           0 :                 puts(_("Timing is on."));
    2571             :             else
    2572           0 :                 puts(_("Timing is off."));
    2573             :         }
    2574           0 :         free(opt);
    2575             :     }
    2576             :     else
    2577           4 :         ignore_slash_options(scan_state);
    2578             : 
    2579           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2580             : }
    2581             : 
    2582             : /*
    2583             :  * \unset -- unset variable
    2584             :  */
    2585             : static backslashResult
    2586          26 : exec_command_unset(PsqlScanState scan_state, bool active_branch,
    2587             :                    const char *cmd)
    2588             : {
    2589          26 :     bool        success = true;
    2590             : 
    2591          26 :     if (active_branch)
    2592             :     {
    2593          22 :         char       *opt = psql_scan_slash_option(scan_state,
    2594             :                                                  OT_NORMAL, NULL, false);
    2595             : 
    2596          22 :         if (!opt)
    2597             :         {
    2598           0 :             pg_log_error("\\%s: missing required argument", cmd);
    2599           0 :             success = false;
    2600             :         }
    2601          22 :         else if (!SetVariable(pset.vars, opt, NULL))
    2602           0 :             success = false;
    2603             : 
    2604          22 :         free(opt);
    2605             :     }
    2606             :     else
    2607           4 :         ignore_slash_options(scan_state);
    2608             : 
    2609          26 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2610             : }
    2611             : 
    2612             : /*
    2613             :  * \w -- write query buffer to file
    2614             :  */
    2615             : static backslashResult
    2616           8 : exec_command_write(PsqlScanState scan_state, bool active_branch,
    2617             :                    const char *cmd,
    2618             :                    PQExpBuffer query_buf, PQExpBuffer previous_buf)
    2619             : {
    2620           8 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    2621             : 
    2622           8 :     if (active_branch)
    2623             :     {
    2624           0 :         char       *fname = psql_scan_slash_option(scan_state,
    2625             :                                                    OT_FILEPIPE, NULL, true);
    2626           0 :         FILE       *fd = NULL;
    2627           0 :         bool        is_pipe = false;
    2628             : 
    2629           0 :         if (!query_buf)
    2630             :         {
    2631           0 :             pg_log_error("no query buffer");
    2632           0 :             status = PSQL_CMD_ERROR;
    2633             :         }
    2634             :         else
    2635             :         {
    2636           0 :             if (!fname)
    2637             :             {
    2638           0 :                 pg_log_error("\\%s: missing required argument", cmd);
    2639           0 :                 status = PSQL_CMD_ERROR;
    2640             :             }
    2641             :             else
    2642             :             {
    2643           0 :                 expand_tilde(&fname);
    2644           0 :                 if (fname[0] == '|')
    2645             :                 {
    2646           0 :                     is_pipe = true;
    2647           0 :                     disable_sigpipe_trap();
    2648           0 :                     fd = popen(&fname[1], "w");
    2649             :                 }
    2650             :                 else
    2651             :                 {
    2652           0 :                     canonicalize_path(fname);
    2653           0 :                     fd = fopen(fname, "w");
    2654             :                 }
    2655           0 :                 if (!fd)
    2656             :                 {
    2657           0 :                     pg_log_error("%s: %m", fname);
    2658           0 :                     status = PSQL_CMD_ERROR;
    2659             :                 }
    2660             :             }
    2661             :         }
    2662             : 
    2663           0 :         if (fd)
    2664             :         {
    2665             :             int         result;
    2666             : 
    2667             :             /*
    2668             :              * We want to print the same thing \g would execute, but not to
    2669             :              * change the query buffer state; so we can't use
    2670             :              * copy_previous_query().  Also, beware of possibility that buffer
    2671             :              * pointers are NULL.
    2672             :              */
    2673           0 :             if (query_buf && query_buf->len > 0)
    2674           0 :                 fprintf(fd, "%s\n", query_buf->data);
    2675           0 :             else if (previous_buf && previous_buf->len > 0)
    2676           0 :                 fprintf(fd, "%s\n", previous_buf->data);
    2677             : 
    2678           0 :             if (is_pipe)
    2679           0 :                 result = pclose(fd);
    2680             :             else
    2681           0 :                 result = fclose(fd);
    2682             : 
    2683           0 :             if (result == EOF)
    2684             :             {
    2685           0 :                 pg_log_error("%s: %m", fname);
    2686           0 :                 status = PSQL_CMD_ERROR;
    2687             :             }
    2688             :         }
    2689             : 
    2690           0 :         if (is_pipe)
    2691           0 :             restore_sigpipe_trap();
    2692             : 
    2693           0 :         free(fname);
    2694             :     }
    2695             :     else
    2696           8 :         ignore_slash_filepipe(scan_state);
    2697             : 
    2698           8 :     return status;
    2699             : }
    2700             : 
    2701             : /*
    2702             :  * \watch -- execute a query every N seconds
    2703             :  */
    2704             : static backslashResult
    2705           4 : exec_command_watch(PsqlScanState scan_state, bool active_branch,
    2706             :                    PQExpBuffer query_buf, PQExpBuffer previous_buf)
    2707             : {
    2708           4 :     bool        success = true;
    2709             : 
    2710           4 :     if (active_branch)
    2711             :     {
    2712           0 :         char       *opt = psql_scan_slash_option(scan_state,
    2713             :                                                  OT_NORMAL, NULL, true);
    2714           0 :         double      sleep = 2;
    2715             : 
    2716             :         /* Convert optional sleep-length argument */
    2717           0 :         if (opt)
    2718             :         {
    2719           0 :             sleep = strtod(opt, NULL);
    2720           0 :             if (sleep <= 0)
    2721           0 :                 sleep = 1;
    2722           0 :             free(opt);
    2723             :         }
    2724             : 
    2725             :         /* If query_buf is empty, recall and execute previous query */
    2726           0 :         (void) copy_previous_query(query_buf, previous_buf);
    2727             : 
    2728           0 :         success = do_watch(query_buf, sleep);
    2729             : 
    2730             :         /* Reset the query buffer as though for \r */
    2731           0 :         resetPQExpBuffer(query_buf);
    2732           0 :         psql_scan_reset(scan_state);
    2733             :     }
    2734             :     else
    2735           4 :         ignore_slash_options(scan_state);
    2736             : 
    2737           4 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2738             : }
    2739             : 
    2740             : /*
    2741             :  * \x -- set or toggle expanded table representation
    2742             :  */
    2743             : static backslashResult
    2744          22 : exec_command_x(PsqlScanState scan_state, bool active_branch)
    2745             : {
    2746          22 :     bool        success = true;
    2747             : 
    2748          22 :     if (active_branch)
    2749             :     {
    2750          18 :         char       *opt = psql_scan_slash_option(scan_state,
    2751             :                                                  OT_NORMAL, NULL, true);
    2752             : 
    2753          18 :         success = do_pset("expanded", opt, &pset.popt, pset.quiet);
    2754          18 :         free(opt);
    2755             :     }
    2756             :     else
    2757           4 :         ignore_slash_options(scan_state);
    2758             : 
    2759          22 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2760             : }
    2761             : 
    2762             : /*
    2763             :  * \z -- list table privileges (equivalent to \dp)
    2764             :  */
    2765             : static backslashResult
    2766          12 : exec_command_z(PsqlScanState scan_state, bool active_branch)
    2767             : {
    2768          12 :     bool        success = true;
    2769             : 
    2770          12 :     if (active_branch)
    2771             :     {
    2772           8 :         char       *pattern = psql_scan_slash_option(scan_state,
    2773             :                                                      OT_NORMAL, NULL, true);
    2774             : 
    2775           8 :         success = permissionsList(pattern);
    2776           8 :         if (pattern)
    2777           8 :             free(pattern);
    2778             :     }
    2779             :     else
    2780           4 :         ignore_slash_options(scan_state);
    2781             : 
    2782          12 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2783             : }
    2784             : 
    2785             : /*
    2786             :  * \! -- execute shell command
    2787             :  */
    2788             : static backslashResult
    2789           8 : exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
    2790             : {
    2791           8 :     bool        success = true;
    2792             : 
    2793           8 :     if (active_branch)
    2794             :     {
    2795           4 :         char       *opt = psql_scan_slash_option(scan_state,
    2796             :                                                  OT_WHOLE_LINE, NULL, false);
    2797             : 
    2798           4 :         success = do_shell(opt);
    2799           4 :         free(opt);
    2800             :     }
    2801             :     else
    2802           4 :         ignore_slash_whole_line(scan_state);
    2803             : 
    2804           8 :     return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
    2805             : }
    2806             : 
    2807             : /*
    2808             :  * \? -- print help about backslash commands
    2809             :  */
    2810             : static backslashResult
    2811           4 : exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
    2812             : {
    2813           4 :     if (active_branch)
    2814             :     {
    2815           0 :         char       *opt0 = psql_scan_slash_option(scan_state,
    2816             :                                                   OT_NORMAL, NULL, false);
    2817             : 
    2818           0 :         if (!opt0 || strcmp(opt0, "commands") == 0)
    2819           0 :             slashUsage(pset.popt.topt.pager);
    2820           0 :         else if (strcmp(opt0, "options") == 0)
    2821           0 :             usage(pset.popt.topt.pager);
    2822           0 :         else if (strcmp(opt0, "variables") == 0)
    2823           0 :             helpVariables(pset.popt.topt.pager);
    2824             :         else
    2825           0 :             slashUsage(pset.popt.topt.pager);
    2826             : 
    2827           0 :         if (opt0)
    2828           0 :             free(opt0);
    2829             :     }
    2830             :     else
    2831           4 :         ignore_slash_options(scan_state);
    2832             : 
    2833           4 :     return PSQL_CMD_SKIP_LINE;
    2834             : }
    2835             : 
    2836             : 
    2837             : /*
    2838             :  * Read and interpret an argument to the \connect slash command.
    2839             :  *
    2840             :  * Returns a malloc'd string, or NULL if no/empty argument.
    2841             :  */
    2842             : static char *
    2843         752 : read_connect_arg(PsqlScanState scan_state)
    2844             : {
    2845             :     char       *result;
    2846             :     char        quote;
    2847             : 
    2848             :     /*
    2849             :      * Ideally we should treat the arguments as SQL identifiers.  But for
    2850             :      * backwards compatibility with 7.2 and older pg_dump files, we have to
    2851             :      * take unquoted arguments verbatim (don't downcase them). For now,
    2852             :      * double-quoted arguments may be stripped of double quotes (as if SQL
    2853             :      * identifiers).  By 7.4 or so, pg_dump files can be expected to
    2854             :      * double-quote all mixed-case \connect arguments, and then we can get rid
    2855             :      * of OT_SQLIDHACK.
    2856             :      */
    2857         752 :     result = psql_scan_slash_option(scan_state, OT_SQLIDHACK, &quote, true);
    2858             : 
    2859         752 :     if (!result)
    2860         584 :         return NULL;
    2861             : 
    2862         168 :     if (quote)
    2863          18 :         return result;
    2864             : 
    2865         150 :     if (*result == '\0' || strcmp(result, "-") == 0)
    2866             :     {
    2867         124 :         free(result);
    2868         124 :         return NULL;
    2869             :     }
    2870             : 
    2871          26 :     return result;
    2872             : }
    2873             : 
    2874             : /*
    2875             :  * Read a boolean expression, return it as a PQExpBuffer string.
    2876             :  *
    2877             :  * Note: anything more or less than one token will certainly fail to be
    2878             :  * parsed by ParseVariableBool, so we don't worry about complaining here.
    2879             :  * This routine's return data structure will need to be rethought anyway
    2880             :  * to support likely future extensions such as "\if defined VARNAME".
    2881             :  */
    2882             : static PQExpBuffer
    2883         126 : gather_boolean_expression(PsqlScanState scan_state)
    2884             : {
    2885         126 :     PQExpBuffer exp_buf = createPQExpBuffer();
    2886         126 :     int         num_options = 0;
    2887             :     char       *value;
    2888             : 
    2889             :     /* collect all arguments for the conditional command into exp_buf */
    2890         260 :     while ((value = psql_scan_slash_option(scan_state,
    2891             :                                            OT_NORMAL, NULL, false)) != NULL)
    2892             :     {
    2893             :         /* add spaces between tokens */
    2894         134 :         if (num_options > 0)
    2895           8 :             appendPQExpBufferChar(exp_buf, ' ');
    2896         134 :         appendPQExpBufferStr(exp_buf, value);
    2897         134 :         num_options++;
    2898         134 :         free(value);
    2899             :     }
    2900             : 
    2901         126 :     return exp_buf;
    2902             : }
    2903             : 
    2904             : /*
    2905             :  * Read a boolean expression, return true if the expression
    2906             :  * was a valid boolean expression that evaluated to true.
    2907             :  * Otherwise return false.
    2908             :  *
    2909             :  * Note: conditional stack's top state must be active, else lexer will
    2910             :  * fail to expand variables and backticks.
    2911             :  */
    2912             : static bool
    2913         114 : is_true_boolean_expression(PsqlScanState scan_state, const char *name)
    2914             : {
    2915         114 :     PQExpBuffer buf = gather_boolean_expression(scan_state);
    2916         114 :     bool        value = false;
    2917         114 :     bool        success = ParseVariableBool(buf->data, name, &value);
    2918             : 
    2919         114 :     destroyPQExpBuffer(buf);
    2920         114 :     return success && value;
    2921             : }
    2922             : 
    2923             : /*
    2924             :  * Read a boolean expression, but do nothing with it.
    2925             :  *
    2926             :  * Note: conditional stack's top state must be INACTIVE, else lexer will
    2927             :  * expand variables and backticks, which we do not want here.
    2928             :  */
    2929             : static void
    2930          12 : ignore_boolean_expression(PsqlScanState scan_state)
    2931             : {
    2932          12 :     PQExpBuffer buf = gather_boolean_expression(scan_state);
    2933             : 
    2934          12 :     destroyPQExpBuffer(buf);
    2935          12 : }
    2936             : 
    2937             : /*
    2938             :  * Read and discard "normal" slash command options.
    2939             :  *
    2940             :  * This should be used for inactive-branch processing of any slash command
    2941             :  * that eats one or more OT_NORMAL, OT_SQLID, or OT_SQLIDHACK parameters.
    2942             :  * We don't need to worry about exactly how many it would eat, since the
    2943             :  * cleanup logic in HandleSlashCmds would silently discard any extras anyway.
    2944             :  */
    2945             : static void
    2946         204 : ignore_slash_options(PsqlScanState scan_state)
    2947             : {
    2948             :     char       *arg;
    2949             : 
    2950         488 :     while ((arg = psql_scan_slash_option(scan_state,
    2951             :                                          OT_NORMAL, NULL, false)) != NULL)
    2952         284 :         free(arg);
    2953         204 : }
    2954             : 
    2955             : /*
    2956             :  * Read and discard FILEPIPE slash command argument.
    2957             :  *
    2958             :  * This *MUST* be used for inactive-branch processing of any slash command
    2959             :  * that takes an OT_FILEPIPE option.  Otherwise we might consume a different
    2960             :  * amount of option text in active and inactive cases.
    2961             :  */
    2962             : static void
    2963          12 : ignore_slash_filepipe(PsqlScanState scan_state)
    2964             : {
    2965          12 :     char       *arg = psql_scan_slash_option(scan_state,
    2966             :                                              OT_FILEPIPE, NULL, false);
    2967             : 
    2968          12 :     if (arg)
    2969          12 :         free(arg);
    2970          12 : }
    2971             : 
    2972             : /*
    2973             :  * Read and discard whole-line slash command argument.
    2974             :  *
    2975             :  * This *MUST* be used for inactive-branch processing of any slash command
    2976             :  * that takes an OT_WHOLE_LINE option.  Otherwise we might consume a different
    2977             :  * amount of option text in active and inactive cases.
    2978             :  */
    2979             : static void
    2980          28 : ignore_slash_whole_line(PsqlScanState scan_state)
    2981             : {
    2982          28 :     char       *arg = psql_scan_slash_option(scan_state,
    2983             :                                              OT_WHOLE_LINE, NULL, false);
    2984             : 
    2985          28 :     if (arg)
    2986          24 :         free(arg);
    2987          28 : }
    2988             : 
    2989             : /*
    2990             :  * Return true if the command given is a branching command.
    2991             :  */
    2992             : static bool
    2993           0 : is_branching_command(const char *cmd)
    2994             : {
    2995           0 :     return (strcmp(cmd, "if") == 0 ||
    2996           0 :             strcmp(cmd, "elif") == 0 ||
    2997           0 :             strcmp(cmd, "else") == 0 ||
    2998           0 :             strcmp(cmd, "endif") == 0);
    2999             : }
    3000             : 
    3001             : /*
    3002             :  * Prepare to possibly restore query buffer to its current state
    3003             :  * (cf. discard_query_text).
    3004             :  *
    3005             :  * We need to remember the length of the query buffer, and the lexer's
    3006             :  * notion of the parenthesis nesting depth.
    3007             :  */
    3008             : static void
    3009         146 : save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack,
    3010             :                       PQExpBuffer query_buf)
    3011             : {
    3012         146 :     if (query_buf)
    3013         146 :         conditional_stack_set_query_len(cstack, query_buf->len);
    3014         146 :     conditional_stack_set_paren_depth(cstack,
    3015             :                                       psql_scan_get_paren_depth(scan_state));
    3016         146 : }
    3017             : 
    3018             : /*
    3019             :  * Discard any query text absorbed during an inactive conditional branch.
    3020             :  *
    3021             :  * We must discard data that was appended to query_buf during an inactive
    3022             :  * \if branch.  We don't have to do anything there if there's no query_buf.
    3023             :  *
    3024             :  * Also, reset the lexer state to the same paren depth there was before.
    3025             :  * (The rest of its state doesn't need attention, since we could not be
    3026             :  * inside a comment or literal or partial token.)
    3027             :  */
    3028             : static void
    3029         122 : discard_query_text(PsqlScanState scan_state, ConditionalStack cstack,
    3030             :                    PQExpBuffer query_buf)
    3031             : {
    3032         122 :     if (query_buf)
    3033             :     {
    3034         122 :         int         new_len = conditional_stack_get_query_len(cstack);
    3035             : 
    3036             :         Assert(new_len >= 0 && new_len <= query_buf->len);
    3037         122 :         query_buf->len = new_len;
    3038         122 :         query_buf->data[new_len] = '\0';
    3039             :     }
    3040         122 :     psql_scan_set_paren_depth(scan_state,
    3041             :                               conditional_stack_get_paren_depth(cstack));
    3042         122 : }
    3043             : 
    3044             : /*
    3045             :  * If query_buf is empty, copy previous_buf into it.
    3046             :  *
    3047             :  * This is used by various slash commands for which re-execution of a
    3048             :  * previous query is a common usage.  For convenience, we allow the
    3049             :  * case of query_buf == NULL (and do nothing).
    3050             :  *
    3051             :  * Returns "true" if the previous query was copied into the query
    3052             :  * buffer, else "false".
    3053             :  */
    3054             : static bool
    3055         394 : copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
    3056             : {
    3057         394 :     if (query_buf && query_buf->len == 0)
    3058             :     {
    3059          48 :         appendPQExpBufferStr(query_buf, previous_buf->data);
    3060          48 :         return true;
    3061             :     }
    3062         346 :     return false;
    3063             : }
    3064             : 
    3065             : /*
    3066             :  * Ask the user for a password; 'username' is the username the
    3067             :  * password is for, if one has been explicitly specified.
    3068             :  * Returns a malloc'd string.
    3069             :  * If 'canceled' is provided, *canceled will be set to true if the prompt
    3070             :  * is canceled via SIGINT, and to false otherwise.
    3071             :  */
    3072             : static char *
    3073           0 : prompt_for_password(const char *username, bool *canceled)
    3074             : {
    3075             :     char       *result;
    3076             :     PromptInterruptContext prompt_ctx;
    3077             : 
    3078             :     /* Set up to let SIGINT cancel simple_prompt_extended() */
    3079           0 :     prompt_ctx.jmpbuf = sigint_interrupt_jmp;
    3080           0 :     prompt_ctx.enabled = &sigint_interrupt_enabled;
    3081           0 :     prompt_ctx.canceled = false;
    3082             : 
    3083           0 :     if (username == NULL || username[0] == '\0')
    3084           0 :         result = simple_prompt_extended("Password: ", false, &prompt_ctx);
    3085             :     else
    3086             :     {
    3087             :         char       *prompt_text;
    3088             : 
    3089           0 :         prompt_text = psprintf(_("Password for user %s: "), username);
    3090           0 :         result = simple_prompt_extended(prompt_text, false, &prompt_ctx);
    3091           0 :         free(prompt_text);
    3092             :     }
    3093             : 
    3094           0 :     if (canceled)
    3095           0 :         *canceled = prompt_ctx.canceled;
    3096             : 
    3097           0 :     return result;
    3098             : }
    3099             : 
    3100             : static bool
    3101          48 : param_is_newly_set(const char *old_val, const char *new_val)
    3102             : {
    3103          48 :     if (new_val == NULL)
    3104           0 :         return false;
    3105             : 
    3106          48 :     if (old_val == NULL || strcmp(old_val, new_val) != 0)
    3107           0 :         return true;
    3108             : 
    3109          48 :     return false;
    3110             : }
    3111             : 
    3112             : /*
    3113             :  * do_connect -- handler for \connect
    3114             :  *
    3115             :  * Connects to a database with given parameters.  If we are told to re-use
    3116             :  * parameters, parameters from the previous connection are used where the
    3117             :  * command's own options do not supply a value.  Otherwise, libpq defaults
    3118             :  * are used.
    3119             :  *
    3120             :  * In interactive mode, if connection fails with the given parameters,
    3121             :  * the old connection will be kept.
    3122             :  */
    3123             : static bool
    3124         184 : do_connect(enum trivalue reuse_previous_specification,
    3125             :            char *dbname, char *user, char *host, char *port)
    3126             : {
    3127         184 :     PGconn     *o_conn = pset.db,
    3128         184 :                *n_conn = NULL;
    3129             :     PQconninfoOption *cinfo;
    3130         184 :     int         nconnopts = 0;
    3131         184 :     bool        same_host = false;
    3132         184 :     char       *password = NULL;
    3133             :     char       *client_encoding;
    3134         184 :     bool        success = true;
    3135         184 :     bool        keep_password = true;
    3136             :     bool        has_connection_string;
    3137             :     bool        reuse_previous;
    3138             : 
    3139         184 :     has_connection_string = dbname ?
    3140         184 :         recognized_connection_string(dbname) : false;
    3141             : 
    3142             :     /* Complain if we have additional arguments after a connection string. */
    3143         184 :     if (has_connection_string && (user || host || port))
    3144             :     {
    3145           0 :         pg_log_error("Do not give user, host, or port separately when using a connection string");
    3146           0 :         return false;
    3147             :     }
    3148             : 
    3149         184 :     switch (reuse_previous_specification)
    3150             :     {
    3151          16 :         case TRI_YES:
    3152          16 :             reuse_previous = true;
    3153          16 :             break;
    3154           0 :         case TRI_NO:
    3155           0 :             reuse_previous = false;
    3156           0 :             break;
    3157         168 :         default:
    3158         168 :             reuse_previous = !has_connection_string;
    3159         168 :             break;
    3160             :     }
    3161             : 
    3162             :     /*
    3163             :      * If we intend to re-use connection parameters, collect them out of the
    3164             :      * old connection, then replace individual values as necessary.  (We may
    3165             :      * need to resort to looking at pset.dead_conn, if the connection died
    3166             :      * previously.)  Otherwise, obtain a PQconninfoOption array containing
    3167             :      * libpq's defaults, and modify that.  Note this function assumes that
    3168             :      * PQconninfo, PQconndefaults, and PQconninfoParse will all produce arrays
    3169             :      * containing the same options in the same order.
    3170             :      */
    3171         184 :     if (reuse_previous)
    3172             :     {
    3173         184 :         if (o_conn)
    3174         184 :             cinfo = PQconninfo(o_conn);
    3175           0 :         else if (pset.dead_conn)
    3176           0 :             cinfo = PQconninfo(pset.dead_conn);
    3177             :         else
    3178             :         {
    3179             :             /* This is reachable after a non-interactive \connect failure */
    3180           0 :             pg_log_error("No database connection exists to re-use parameters from");
    3181           0 :             return false;
    3182             :         }
    3183             :     }
    3184             :     else
    3185           0 :         cinfo = PQconndefaults();
    3186             : 
    3187         184 :     if (cinfo)
    3188             :     {
    3189         184 :         if (has_connection_string)
    3190             :         {
    3191             :             /* Parse the connstring and insert values into cinfo */
    3192             :             PQconninfoOption *replcinfo;
    3193             :             char       *errmsg;
    3194             : 
    3195          16 :             replcinfo = PQconninfoParse(dbname, &errmsg);
    3196          16 :             if (replcinfo)
    3197             :             {
    3198             :                 PQconninfoOption *ci;
    3199             :                 PQconninfoOption *replci;
    3200          16 :                 bool        have_password = false;
    3201             : 
    3202          16 :                 for (ci = cinfo, replci = replcinfo;
    3203         592 :                      ci->keyword && replci->keyword;
    3204         576 :                      ci++, replci++)
    3205             :                 {
    3206             :                     Assert(strcmp(ci->keyword, replci->keyword) == 0);
    3207             :                     /* Insert value from connstring if one was provided */
    3208         576 :                     if (replci->val)
    3209             :                     {
    3210             :                         /*
    3211             :                          * We know that both val strings were allocated by
    3212             :                          * libpq, so the least messy way to avoid memory leaks
    3213             :                          * is to swap them.
    3214             :                          */
    3215          16 :                         char       *swap = replci->val;
    3216             : 
    3217          16 :                         replci->val = ci->val;
    3218          16 :                         ci->val = swap;
    3219             : 
    3220             :                         /*
    3221             :                          * Check whether connstring provides options affecting
    3222             :                          * password re-use.  While any change in user, host,
    3223             :                          * hostaddr, or port causes us to ignore the old
    3224             :                          * connection's password, we don't force that for
    3225             :                          * dbname, since passwords aren't database-specific.
    3226             :                          */
    3227          16 :                         if (replci->val == NULL ||
    3228          16 :                             strcmp(ci->val, replci->val) != 0)
    3229             :                         {
    3230          16 :                             if (strcmp(replci->keyword, "user") == 0 ||
    3231          16 :                                 strcmp(replci->keyword, "host") == 0 ||
    3232          16 :                                 strcmp(replci->keyword, "hostaddr") == 0 ||
    3233          16 :                                 strcmp(replci->keyword, "port") == 0)
    3234           0 :                                 keep_password = false;
    3235             :                         }
    3236             :                         /* Also note whether connstring contains a password. */
    3237          16 :                         if (strcmp(replci->keyword, "password") == 0)
    3238           0 :                             have_password = true;
    3239             :                     }
    3240         560 :                     else if (!reuse_previous)
    3241             :                     {
    3242             :                         /*
    3243             :                          * When we have a connstring and are not re-using
    3244             :                          * parameters, swap *all* entries, even those not set
    3245             :                          * by the connstring.  This avoids absorbing
    3246             :                          * environment-dependent defaults from the result of
    3247             :                          * PQconndefaults().  We don't want to do that because
    3248             :                          * they'd override service-file entries if the
    3249             :                          * connstring specifies a service parameter, whereas
    3250             :                          * the priority should be the other way around.  libpq
    3251             :                          * can certainly recompute any defaults we don't pass
    3252             :                          * here.  (In this situation, it's a bit wasteful to
    3253             :                          * have called PQconndefaults() at all, but not doing
    3254             :                          * so would require yet another major code path here.)
    3255             :                          */
    3256           0 :                         replci->val = ci->val;
    3257           0 :                         ci->val = NULL;
    3258             :                     }
    3259             :                 }
    3260             :                 Assert(ci->keyword == NULL && replci->keyword == NULL);
    3261             : 
    3262             :                 /* While here, determine how many option slots there are */
    3263          16 :                 nconnopts = ci - cinfo;
    3264             : 
    3265          16 :                 PQconninfoFree(replcinfo);
    3266             : 
    3267             :                 /*
    3268             :                  * If the connstring contains a password, tell the loop below
    3269             :                  * that we may use it, regardless of other settings (i.e.,
    3270             :                  * cinfo's password is no longer an "old" password).
    3271             :                  */
    3272          16 :                 if (have_password)
    3273           0 :                     keep_password = true;
    3274             : 
    3275             :                 /* Don't let code below try to inject dbname into params. */
    3276          16 :                 dbname = NULL;
    3277             :             }
    3278             :             else
    3279             :             {
    3280             :                 /* PQconninfoParse failed */
    3281           0 :                 if (errmsg)
    3282             :                 {
    3283           0 :                     pg_log_error("%s", errmsg);
    3284           0 :                     PQfreemem(errmsg);
    3285             :                 }
    3286             :                 else
    3287           0 :                     pg_log_error("out of memory");
    3288           0 :                 success = false;
    3289             :             }
    3290             :         }
    3291             :         else
    3292             :         {
    3293             :             /*
    3294             :              * If dbname isn't a connection string, then we'll inject it and
    3295             :              * the other parameters into the keyword array below.  (We can't
    3296             :              * easily insert them into the cinfo array because of memory
    3297             :              * management issues: PQconninfoFree would misbehave on Windows.)
    3298             :              * However, to avoid dependencies on the order in which parameters
    3299             :              * appear in the array, make a preliminary scan to set
    3300             :              * keep_password and same_host correctly.
    3301             :              *
    3302             :              * While any change in user, host, or port causes us to ignore the
    3303             :              * old connection's password, we don't force that for dbname,
    3304             :              * since passwords aren't database-specific.
    3305             :              */
    3306             :             PQconninfoOption *ci;
    3307             : 
    3308        6216 :             for (ci = cinfo; ci->keyword; ci++)
    3309             :             {
    3310        6048 :                 if (user && strcmp(ci->keyword, "user") == 0)
    3311             :                 {
    3312           0 :                     if (!(ci->val && strcmp(user, ci->val) == 0))
    3313           0 :                         keep_password = false;
    3314             :                 }
    3315        6048 :                 else if (host && strcmp(ci->keyword, "host") == 0)
    3316             :                 {
    3317           0 :                     if (ci->val && strcmp(host, ci->val) == 0)
    3318           0 :                         same_host = true;
    3319             :                     else
    3320           0 :                         keep_password = false;
    3321             :                 }
    3322        6048 :                 else if (port && strcmp(ci->keyword, "port") == 0)
    3323             :                 {
    3324           0 :                     if (!(ci->val && strcmp(port, ci->val) == 0))
    3325           0 :                         keep_password = false;
    3326             :                 }
    3327             :             }
    3328             : 
    3329             :             /* While here, determine how many option slots there are */
    3330         168 :             nconnopts = ci - cinfo;
    3331             :         }
    3332             :     }
    3333             :     else
    3334             :     {
    3335             :         /* We failed to create the cinfo structure */
    3336           0 :         pg_log_error("out of memory");
    3337           0 :         success = false;
    3338             :     }
    3339             : 
    3340             :     /*
    3341             :      * If the user asked to be prompted for a password, ask for one now. If
    3342             :      * not, use the password from the old connection, provided the username
    3343             :      * etc have not changed. Otherwise, try to connect without a password
    3344             :      * first, and then ask for a password if needed.
    3345             :      *
    3346             :      * XXX: this behavior leads to spurious connection attempts recorded in
    3347             :      * the postmaster's log.  But libpq offers no API that would let us obtain
    3348             :      * a password and then continue with the first connection attempt.
    3349             :      */
    3350         184 :     if (pset.getPassword == TRI_YES && success)
    3351             :     {
    3352           0 :         bool        canceled = false;
    3353             : 
    3354             :         /*
    3355             :          * If a connstring or URI is provided, we don't know which username
    3356             :          * will be used, since we haven't dug that out of the connstring.
    3357             :          * Don't risk issuing a misleading prompt.  As in startup.c, it does
    3358             :          * not seem worth working harder, since this getPassword setting is
    3359             :          * normally only used in noninteractive cases.
    3360             :          */
    3361           0 :         password = prompt_for_password(has_connection_string ? NULL : user,
    3362             :                                        &canceled);
    3363           0 :         success = !canceled;
    3364             :     }
    3365             : 
    3366             :     /*
    3367             :      * Consider whether to force client_encoding to "auto" (overriding
    3368             :      * anything in the connection string).  We do so if we have a terminal
    3369             :      * connection and there is no PGCLIENTENCODING environment setting.
    3370             :      */
    3371         184 :     if (pset.notty || getenv("PGCLIENTENCODING"))
    3372         184 :         client_encoding = NULL;
    3373             :     else
    3374           0 :         client_encoding = "auto";
    3375             : 
    3376             :     /* Loop till we have a connection or fail, which we might've already */
    3377         184 :     while (success)
    3378             :     {
    3379         184 :         const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
    3380         184 :         const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
    3381         184 :         int         paramnum = 0;
    3382             :         PQconninfoOption *ci;
    3383             : 
    3384             :         /*
    3385             :          * Copy non-default settings into the PQconnectdbParams parameter
    3386             :          * arrays; but inject any values specified old-style, as well as any
    3387             :          * interactively-obtained password, and a couple of fields we want to
    3388             :          * set forcibly.
    3389             :          *
    3390             :          * If you change this code, see also the initial-connection code in
    3391             :          * main().
    3392             :          */
    3393        6808 :         for (ci = cinfo; ci->keyword; ci++)
    3394             :         {
    3395        6624 :             keywords[paramnum] = ci->keyword;
    3396             : 
    3397        6624 :             if (dbname && strcmp(ci->keyword, "dbname") == 0)
    3398          12 :                 values[paramnum++] = dbname;
    3399        6612 :             else if (user && strcmp(ci->keyword, "user") == 0)
    3400           0 :                 values[paramnum++] = user;
    3401        6612 :             else if (host && strcmp(ci->keyword, "host") == 0)
    3402           0 :                 values[paramnum++] = host;
    3403        6612 :             else if (host && !same_host && strcmp(ci->keyword, "hostaddr") == 0)
    3404             :             {
    3405             :                 /* If we're changing the host value, drop any old hostaddr */
    3406           0 :                 values[paramnum++] = NULL;
    3407             :             }
    3408        6612 :             else if (port && strcmp(ci->keyword, "port") == 0)
    3409           0 :                 values[paramnum++] = port;
    3410             :             /* If !keep_password, we unconditionally drop old password */
    3411        6612 :             else if ((password || !keep_password) &&
    3412           0 :                      strcmp(ci->keyword, "password") == 0)
    3413           0 :                 values[paramnum++] = password;
    3414        6612 :             else if (strcmp(ci->keyword, "fallback_application_name") == 0)
    3415         184 :                 values[paramnum++] = pset.progname;
    3416        6428 :             else if (client_encoding &&
    3417           0 :                      strcmp(ci->keyword, "client_encoding") == 0)
    3418           0 :                 values[paramnum++] = client_encoding;
    3419        6428 :             else if (ci->val)
    3420        2772 :                 values[paramnum++] = ci->val;
    3421             :             /* else, don't bother making libpq parse this keyword */
    3422             :         }
    3423             :         /* add array terminator */
    3424         184 :         keywords[paramnum] = NULL;
    3425         184 :         values[paramnum] = NULL;
    3426             : 
    3427             :         /* Note we do not want libpq to re-expand the dbname parameter */
    3428         184 :         n_conn = PQconnectdbParams(keywords, values, false);
    3429             : 
    3430         184 :         pg_free(keywords);
    3431         184 :         pg_free(values);
    3432             : 
    3433         184 :         if (PQstatus(n_conn) == CONNECTION_OK)
    3434         184 :             break;
    3435             : 
    3436             :         /*
    3437             :          * Connection attempt failed; either retry the connection attempt with
    3438             :          * a new password, or give up.
    3439             :          */
    3440           0 :         if (!password && PQconnectionNeedsPassword(n_conn) && pset.getPassword != TRI_NO)
    3441             :         {
    3442           0 :             bool        canceled = false;
    3443             : 
    3444             :             /*
    3445             :              * Prompt for password using the username we actually connected
    3446             :              * with --- it might've come out of "dbname" rather than "user".
    3447             :              */
    3448           0 :             password = prompt_for_password(PQuser(n_conn), &canceled);
    3449           0 :             PQfinish(n_conn);
    3450           0 :             n_conn = NULL;
    3451           0 :             success = !canceled;
    3452           0 :             continue;
    3453             :         }
    3454             : 
    3455             :         /*
    3456             :          * We'll report the error below ... unless n_conn is NULL, indicating
    3457             :          * that libpq didn't have enough memory to make a PGconn.
    3458             :          */
    3459           0 :         if (n_conn == NULL)
    3460           0 :             pg_log_error("out of memory");
    3461             : 
    3462           0 :         success = false;
    3463             :     }                           /* end retry loop */
    3464             : 
    3465             :     /* Release locally allocated data, whether we succeeded or not */
    3466         184 :     if (password)
    3467           0 :         pg_free(password);
    3468         184 :     if (cinfo)
    3469         184 :         PQconninfoFree(cinfo);
    3470             : 
    3471         184 :     if (!success)
    3472             :     {
    3473             :         /*
    3474             :          * Failed to connect to the database. In interactive mode, keep the
    3475             :          * previous connection to the DB; in scripting mode, close our
    3476             :          * previous connection as well.
    3477             :          */
    3478           0 :         if (pset.cur_cmd_interactive)
    3479             :         {
    3480           0 :             if (n_conn)
    3481             :             {
    3482           0 :                 pg_log_info("%s", PQerrorMessage(n_conn));
    3483           0 :                 PQfinish(n_conn);
    3484             :             }
    3485             : 
    3486             :             /* pset.db is left unmodified */
    3487           0 :             if (o_conn)
    3488           0 :                 pg_log_info("Previous connection kept");
    3489             :         }
    3490             :         else
    3491             :         {
    3492           0 :             if (n_conn)
    3493             :             {
    3494           0 :                 pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
    3495           0 :                 PQfinish(n_conn);
    3496             :             }
    3497             : 
    3498           0 :             if (o_conn)
    3499             :             {
    3500             :                 /*
    3501             :                  * Transition to having no connection.
    3502             :                  *
    3503             :                  * Unlike CheckConnection(), we close the old connection
    3504             :                  * immediately to prevent its parameters from being re-used.
    3505             :                  * This is so that a script cannot accidentally reuse
    3506             :                  * parameters it did not expect to.  Otherwise, the state
    3507             :                  * cleanup should be the same as in CheckConnection().
    3508             :                  */
    3509           0 :                 PQfinish(o_conn);
    3510           0 :                 pset.db = NULL;
    3511           0 :                 ResetCancelConn();
    3512           0 :                 UnsyncVariables();
    3513             :             }
    3514             : 
    3515             :             /* On the same reasoning, release any dead_conn to prevent reuse */
    3516           0 :             if (pset.dead_conn)
    3517             :             {
    3518           0 :                 PQfinish(pset.dead_conn);
    3519           0 :                 pset.dead_conn = NULL;
    3520             :             }
    3521             :         }
    3522             : 
    3523           0 :         return false;
    3524             :     }
    3525             : 
    3526             :     /*
    3527             :      * Replace the old connection with the new one, and update
    3528             :      * connection-dependent variables.  Keep the resynchronization logic in
    3529             :      * sync with CheckConnection().
    3530             :      */
    3531         184 :     PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
    3532         184 :     pset.db = n_conn;
    3533         184 :     SyncVariables();
    3534         184 :     connection_warnings(false); /* Must be after SyncVariables */
    3535             : 
    3536             :     /* Tell the user about the new connection */
    3537         184 :     if (!pset.quiet)
    3538             :     {
    3539          48 :         if (!o_conn ||
    3540          48 :             param_is_newly_set(PQhost(o_conn), PQhost(pset.db)) ||
    3541          24 :             param_is_newly_set(PQport(o_conn), PQport(pset.db)))
    3542           0 :         {
    3543           0 :             char       *host = PQhost(pset.db);
    3544           0 :             char       *hostaddr = PQhostaddr(pset.db);
    3545             : 
    3546           0 :             if (is_unixsock_path(host))
    3547             :             {
    3548             :                 /* hostaddr overrides host */
    3549           0 :                 if (hostaddr && *hostaddr)
    3550           0 :                     printf(_("You are now connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
    3551             :                            PQdb(pset.db), PQuser(pset.db), hostaddr, PQport(pset.db));
    3552             :                 else
    3553           0 :                     printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
    3554             :                            PQdb(pset.db), PQuser(pset.db), host, PQport(pset.db));
    3555             :             }
    3556             :             else
    3557             :             {
    3558           0 :                 if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
    3559           0 :                     printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
    3560             :                            PQdb(pset.db), PQuser(pset.db), host, hostaddr, PQport(pset.db));
    3561             :                 else
    3562           0 :                     printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
    3563             :                            PQdb(pset.db), PQuser(pset.db), host, PQport(pset.db));
    3564             :             }
    3565             :         }
    3566             :         else
    3567          24 :             printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
    3568             :                    PQdb(pset.db), PQuser(pset.db));
    3569             :     }
    3570             : 
    3571             :     /* Drop no-longer-needed connection(s) */
    3572         184 :     if (o_conn)
    3573         184 :         PQfinish(o_conn);
    3574         184 :     if (pset.dead_conn)
    3575             :     {
    3576           0 :         PQfinish(pset.dead_conn);
    3577           0 :         pset.dead_conn = NULL;
    3578             :     }
    3579             : 
    3580         184 :     return true;
    3581             : }
    3582             : 
    3583             : 
    3584             : void
    3585         186 : connection_warnings(bool in_startup)
    3586             : {
    3587         186 :     if (!pset.quiet && !pset.notty)
    3588             :     {
    3589           2 :         int         client_ver = PG_VERSION_NUM;
    3590             :         char        cverbuf[32];
    3591             :         char        sverbuf[32];
    3592             : 
    3593           2 :         if (pset.sversion != client_ver)
    3594             :         {
    3595             :             const char *server_version;
    3596             : 
    3597             :             /* Try to get full text form, might include "devel" etc */
    3598           0 :             server_version = PQparameterStatus(pset.db, "server_version");
    3599             :             /* Otherwise fall back on pset.sversion */
    3600           0 :             if (!server_version)
    3601             :             {
    3602           0 :                 formatPGVersionNumber(pset.sversion, true,
    3603             :                                       sverbuf, sizeof(sverbuf));
    3604           0 :                 server_version = sverbuf;
    3605             :             }
    3606             : 
    3607           0 :             printf(_("%s (%s, server %s)\n"),
    3608             :                    pset.progname, PG_VERSION, server_version);
    3609             :         }
    3610             :         /* For version match, only print psql banner on startup. */
    3611           2 :         else if (in_startup)
    3612           2 :             printf("%s (%s)\n", pset.progname, PG_VERSION);
    3613             : 
    3614           2 :         if (pset.sversion / 100 > client_ver / 100)
    3615           0 :             printf(_("WARNING: %s major version %s, server major version %s.\n"
    3616             :                      "         Some psql features might not work.\n"),
    3617             :                    pset.progname,
    3618             :                    formatPGVersionNumber(client_ver, false,
    3619             :                                          cverbuf, sizeof(cverbuf)),
    3620             :                    formatPGVersionNumber(pset.sversion, false,
    3621             :                                          sverbuf, sizeof(sverbuf)));
    3622             : 
    3623             : #ifdef WIN32
    3624             :         if (in_startup)
    3625             :             checkWin32Codepage();
    3626             : #endif
    3627           2 :         printSSLInfo();
    3628           2 :         printGSSInfo();
    3629             :     }
    3630         186 : }
    3631             : 
    3632             : 
    3633             : /*
    3634             :  * printSSLInfo
    3635             :  *
    3636             :  * Prints information about the current SSL connection, if SSL is in use
    3637             :  */
    3638             : static void
    3639           2 : printSSLInfo(void)
    3640             : {
    3641             :     const char *protocol;
    3642             :     const char *cipher;
    3643             :     const char *bits;
    3644             :     const char *compression;
    3645             : 
    3646           2 :     if (!PQsslInUse(pset.db))
    3647           2 :         return;                 /* no SSL */
    3648             : 
    3649           0 :     protocol = PQsslAttribute(pset.db, "protocol");
    3650           0 :     cipher = PQsslAttribute(pset.db, "cipher");
    3651           0 :     bits = PQsslAttribute(pset.db, "key_bits");
    3652           0 :     compression = PQsslAttribute(pset.db, "compression");
    3653             : 
    3654           0 :     printf(_("SSL connection (protocol: %s, cipher: %s, bits: %s, compression: %s)\n"),
    3655             :            protocol ? protocol : _("unknown"),
    3656             :            cipher ? cipher : _("unknown"),
    3657             :            bits ? bits : _("unknown"),
    3658             :            (compression && strcmp(compression, "off") != 0) ? _("on") : _("off"));
    3659             : }
    3660             : 
    3661             : /*
    3662             :  * printGSSInfo
    3663             :  *
    3664             :  * Prints information about the current GSSAPI connection, if GSSAPI encryption is in use
    3665             :  */
    3666             : static void
    3667           2 : printGSSInfo(void)
    3668             : {
    3669           2 :     if (!PQgssEncInUse(pset.db))
    3670           2 :         return;                 /* no GSSAPI encryption in use */
    3671             : 
    3672           0 :     printf(_("GSSAPI-encrypted connection\n"));
    3673             : }
    3674             : 
    3675             : 
    3676             : /*
    3677             :  * checkWin32Codepage
    3678             :  *
    3679             :  * Prints a warning when win32 console codepage differs from Windows codepage
    3680             :  */
    3681             : #ifdef WIN32
    3682             : static void
    3683             : checkWin32Codepage(void)
    3684             : {
    3685             :     unsigned int wincp,
    3686             :                 concp;
    3687             : 
    3688             :     wincp = GetACP();
    3689             :     concp = GetConsoleCP();
    3690             :     if (wincp != concp)
    3691             :     {
    3692             :         printf(_("WARNING: Console code page (%u) differs from Windows code page (%u)\n"
    3693             :                  "         8-bit characters might not work correctly. See psql reference\n"
    3694             :                  "         page \"Notes for Windows users\" for details.\n"),
    3695             :                concp, wincp);
    3696             :     }
    3697             : }
    3698             : #endif
    3699             : 
    3700             : 
    3701             : /*
    3702             :  * SyncVariables
    3703             :  *
    3704             :  * Make psql's internal variables agree with connection state upon
    3705             :  * establishing a new connection.
    3706             :  */
    3707             : void
    3708        8024 : SyncVariables(void)
    3709             : {
    3710             :     char        vbuf[32];
    3711             :     const char *server_version;
    3712             : 
    3713             :     /* get stuff from connection */
    3714        8024 :     pset.encoding = PQclientEncoding(pset.db);
    3715        8024 :     pset.popt.topt.encoding = pset.encoding;
    3716        8024 :     pset.sversion = PQserverVersion(pset.db);
    3717             : 
    3718        8024 :     SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
    3719        8024 :     SetVariable(pset.vars, "USER", PQuser(pset.db));
    3720        8024 :     SetVariable(pset.vars, "HOST", PQhost(pset.db));
    3721        8024 :     SetVariable(pset.vars, "PORT", PQport(pset.db));
    3722        8024 :     SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
    3723             : 
    3724             :     /* this bit should match connection_warnings(): */
    3725             :     /* Try to get full text form of version, might include "devel" etc */
    3726        8024 :     server_version = PQparameterStatus(pset.db, "server_version");
    3727             :     /* Otherwise fall back on pset.sversion */
    3728        8024 :     if (!server_version)
    3729             :     {
    3730           0 :         formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
    3731           0 :         server_version = vbuf;
    3732             :     }
    3733        8024 :     SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
    3734             : 
    3735        8024 :     snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
    3736        8024 :     SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
    3737             : 
    3738             :     /* send stuff to it, too */
    3739        8024 :     PQsetErrorVerbosity(pset.db, pset.verbosity);
    3740        8024 :     PQsetErrorContextVisibility(pset.db, pset.show_context);
    3741        8024 : }
    3742             : 
    3743             : /*
    3744             :  * UnsyncVariables
    3745             :  *
    3746             :  * Clear variables that should be not be set when there is no connection.
    3747             :  */
    3748             : void
    3749           0 : UnsyncVariables(void)
    3750             : {
    3751           0 :     SetVariable(pset.vars, "DBNAME", NULL);
    3752           0 :     SetVariable(pset.vars, "USER", NULL);
    3753           0 :     SetVariable(pset.vars, "HOST", NULL);
    3754           0 :     SetVariable(pset.vars, "PORT", NULL);
    3755           0 :     SetVariable(pset.vars, "ENCODING", NULL);
    3756           0 :     SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
    3757           0 :     SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
    3758           0 : }
    3759             : 
    3760             : 
    3761             : /*
    3762             :  * helper for do_edit(): actually invoke the editor
    3763             :  *
    3764             :  * Returns true on success, false if we failed to invoke the editor or
    3765             :  * it returned nonzero status.  (An error message is printed for failed-
    3766             :  * to-invoke cases, but not if the editor returns nonzero status.)
    3767             :  */
    3768             : static bool
    3769           0 : editFile(const char *fname, int lineno)
    3770             : {
    3771             :     const char *editorName;
    3772           0 :     const char *editor_lineno_arg = NULL;
    3773             :     char       *sys;
    3774             :     int         result;
    3775             : 
    3776             :     Assert(fname != NULL);
    3777             : 
    3778             :     /* Find an editor to use */
    3779           0 :     editorName = getenv("PSQL_EDITOR");
    3780           0 :     if (!editorName)
    3781           0 :         editorName = getenv("EDITOR");
    3782           0 :     if (!editorName)
    3783           0 :         editorName = getenv("VISUAL");
    3784           0 :     if (!editorName)
    3785           0 :         editorName = DEFAULT_EDITOR;
    3786             : 
    3787             :     /* Get line number argument, if we need it. */
    3788           0 :     if (lineno > 0)
    3789             :     {
    3790           0 :         editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
    3791             : #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
    3792           0 :         if (!editor_lineno_arg)
    3793           0 :             editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
    3794             : #endif
    3795           0 :         if (!editor_lineno_arg)
    3796             :         {
    3797           0 :             pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
    3798           0 :             return false;
    3799             :         }
    3800             :     }
    3801             : 
    3802             :     /*
    3803             :      * On Unix the EDITOR value should *not* be quoted, since it might include
    3804             :      * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
    3805             :      * if necessary.  But this policy is not very workable on Windows, due to
    3806             :      * severe brain damage in their command shell plus the fact that standard
    3807             :      * program paths include spaces.
    3808             :      */
    3809             : #ifndef WIN32
    3810           0 :     if (lineno > 0)
    3811           0 :         sys = psprintf("exec %s %s%d '%s'",
    3812             :                        editorName, editor_lineno_arg, lineno, fname);
    3813             :     else
    3814           0 :         sys = psprintf("exec %s '%s'",
    3815             :                        editorName, fname);
    3816             : #else
    3817             :     if (lineno > 0)
    3818             :         sys = psprintf("\"%s\" %s%d \"%s\"",
    3819             :                        editorName, editor_lineno_arg, lineno, fname);
    3820             :     else
    3821             :         sys = psprintf("\"%s\" \"%s\"",
    3822             :                        editorName, fname);
    3823             : #endif
    3824           0 :     result = system(sys);
    3825           0 :     if (result == -1)
    3826           0 :         pg_log_error("could not start editor \"%s\"", editorName);
    3827           0 :     else if (result == 127)
    3828           0 :         pg_log_error("could not start /bin/sh");
    3829           0 :     free(sys);
    3830             : 
    3831           0 :     return result == 0;
    3832             : }
    3833             : 
    3834             : 
    3835             : /*
    3836             :  * do_edit -- handler for \e
    3837             :  *
    3838             :  * If you do not specify a filename, the current query buffer will be copied
    3839             :  * into a temporary file.
    3840             :  *
    3841             :  * After this function is done, the resulting file will be copied back into the
    3842             :  * query buffer.  As an exception to this, the query buffer will be emptied
    3843             :  * if the file was not modified (or the editor failed) and the caller passes
    3844             :  * "discard_on_quit" = true.
    3845             :  *
    3846             :  * If "edited" isn't NULL, *edited will be set to true if the query buffer
    3847             :  * is successfully replaced.
    3848             :  */
    3849             : static bool
    3850           0 : do_edit(const char *filename_arg, PQExpBuffer query_buf,
    3851             :         int lineno, bool discard_on_quit, bool *edited)
    3852             : {
    3853             :     char        fnametmp[MAXPGPATH];
    3854           0 :     FILE       *stream = NULL;
    3855             :     const char *fname;
    3856           0 :     bool        error = false;
    3857             :     int         fd;
    3858             :     struct stat before,
    3859             :                 after;
    3860             : 
    3861           0 :     if (filename_arg)
    3862           0 :         fname = filename_arg;
    3863             :     else
    3864             :     {
    3865             :         /* make a temp file to edit */
    3866             : #ifndef WIN32
    3867           0 :         const char *tmpdir = getenv("TMPDIR");
    3868             : 
    3869           0 :         if (!tmpdir)
    3870           0 :             tmpdir = "/tmp";
    3871             : #else
    3872             :         char        tmpdir[MAXPGPATH];
    3873             :         int         ret;
    3874             : 
    3875             :         ret = GetTempPath(MAXPGPATH, tmpdir);
    3876             :         if (ret == 0 || ret > MAXPGPATH)
    3877             :         {
    3878             :             pg_log_error("could not locate temporary directory: %s",
    3879             :                          !ret ? strerror(errno) : "");
    3880             :             return false;
    3881             :         }
    3882             : #endif
    3883             : 
    3884             :         /*
    3885             :          * No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
    3886             :          * current directory to the supplied path unless we use only
    3887             :          * backslashes, so we do that.
    3888             :          */
    3889             : #ifndef WIN32
    3890           0 :         snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
    3891           0 :                  "/", (int) getpid());
    3892             : #else
    3893             :         snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
    3894             :                  "" /* trailing separator already present */ , (int) getpid());
    3895             : #endif
    3896             : 
    3897           0 :         fname = (const char *) fnametmp;
    3898             : 
    3899           0 :         fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
    3900           0 :         if (fd != -1)
    3901           0 :             stream = fdopen(fd, "w");
    3902             : 
    3903           0 :         if (fd == -1 || !stream)
    3904             :         {
    3905           0 :             pg_log_error("could not open temporary file \"%s\": %m", fname);
    3906           0 :             error = true;
    3907             :         }
    3908             :         else
    3909             :         {
    3910           0 :             unsigned int ql = query_buf->len;
    3911             : 
    3912             :             /* force newline-termination of what we send to editor */
    3913           0 :             if (ql > 0 && query_buf->data[ql - 1] != '\n')
    3914             :             {
    3915           0 :                 appendPQExpBufferChar(query_buf, '\n');
    3916           0 :                 ql++;
    3917             :             }
    3918             : 
    3919           0 :             if (fwrite(query_buf->data, 1, ql, stream) != ql)
    3920             :             {
    3921           0 :                 pg_log_error("%s: %m", fname);
    3922             : 
    3923           0 :                 if (fclose(stream) != 0)
    3924           0 :                     pg_log_error("%s: %m", fname);
    3925             : 
    3926           0 :                 if (remove(fname) != 0)
    3927           0 :                     pg_log_error("%s: %m", fname);
    3928             : 
    3929           0 :                 error = true;
    3930             :             }
    3931           0 :             else if (fclose(stream) != 0)
    3932             :             {
    3933           0 :                 pg_log_error("%s: %m", fname);
    3934           0 :                 if (remove(fname) != 0)
    3935           0 :                     pg_log_error("%s: %m", fname);
    3936           0 :                 error = true;
    3937             :             }
    3938             :             else
    3939             :             {
    3940             :                 struct utimbuf ut;
    3941             : 
    3942             :                 /*
    3943             :                  * Try to set the file modification time of the temporary file
    3944             :                  * a few seconds in the past.  Otherwise, the low granularity
    3945             :                  * (one second, or even worse on some filesystems) that we can
    3946             :                  * portably measure with stat(2) could lead us to not
    3947             :                  * recognize a modification, if the user typed very quickly.
    3948             :                  *
    3949             :                  * This is a rather unlikely race condition, so don't error
    3950             :                  * out if the utime(2) call fails --- that would make the cure
    3951             :                  * worse than the disease.
    3952             :                  */
    3953           0 :                 ut.modtime = ut.actime = time(NULL) - 2;
    3954           0 :                 (void) utime(fname, &ut);
    3955             :             }
    3956             :         }
    3957             :     }
    3958             : 
    3959           0 :     if (!error && stat(fname, &before) != 0)
    3960             :     {
    3961           0 :         pg_log_error("%s: %m", fname);
    3962           0 :         error = true;
    3963             :     }
    3964             : 
    3965             :     /* call editor */
    3966           0 :     if (!error)
    3967           0 :         error = !editFile(fname, lineno);
    3968             : 
    3969           0 :     if (!error && stat(fname, &after) != 0)
    3970             :     {
    3971           0 :         pg_log_error("%s: %m", fname);
    3972           0 :         error = true;
    3973             :     }
    3974             : 
    3975             :     /* file was edited if the size or modification time has changed */
    3976           0 :     if (!error &&
    3977           0 :         (before.st_size != after.st_size ||
    3978           0 :          before.st_mtime != after.st_mtime))
    3979             :     {
    3980           0 :         stream = fopen(fname, PG_BINARY_R);
    3981           0 :         if (!stream)
    3982             :         {
    3983           0 :             pg_log_error("%s: %m", fname);
    3984           0 :             error = true;
    3985             :         }
    3986             :         else
    3987             :         {
    3988             :             /* read file back into query_buf */
    3989             :             char        line[1024];
    3990             : 
    3991           0 :             resetPQExpBuffer(query_buf);
    3992           0 :             while (fgets(line, sizeof(line), stream) != NULL)
    3993           0 :                 appendPQExpBufferStr(query_buf, line);
    3994             : 
    3995           0 :             if (ferror(stream))
    3996             :             {
    3997           0 :                 pg_log_error("%s: %m", fname);
    3998           0 :                 error = true;
    3999           0 :                 resetPQExpBuffer(query_buf);
    4000             :             }
    4001           0 :             else if (edited)
    4002             :             {
    4003           0 :                 *edited = true;
    4004             :             }
    4005             : 
    4006           0 :             fclose(stream);
    4007             :         }
    4008             :     }
    4009             :     else
    4010             :     {
    4011             :         /*
    4012             :          * If the file was not modified, and the caller requested it, discard
    4013             :          * the query buffer.
    4014             :          */
    4015           0 :         if (discard_on_quit)
    4016           0 :             resetPQExpBuffer(query_buf);
    4017             :     }
    4018             : 
    4019             :     /* remove temp file */
    4020           0 :     if (!filename_arg)
    4021             :     {
    4022           0 :         if (remove(fname) == -1)
    4023             :         {
    4024           0 :             pg_log_error("%s: %m", fname);
    4025           0 :             error = true;
    4026             :         }
    4027             :     }
    4028             : 
    4029           0 :     return !error;
    4030             : }
    4031             : 
    4032             : 
    4033             : 
    4034             : /*
    4035             :  * process_file
    4036             :  *
    4037             :  * Reads commands from filename and passes them to the main processing loop.
    4038             :  * Handler for \i and \ir, but can be used for other things as well.  Returns
    4039             :  * MainLoop() error code.
    4040             :  *
    4041             :  * If use_relative_path is true and filename is not an absolute path, then open
    4042             :  * the file from where the currently processed file (if any) is located.
    4043             :  */
    4044             : int
    4045        7430 : process_file(char *filename, bool use_relative_path)
    4046             : {
    4047             :     FILE       *fd;
    4048             :     int         result;
    4049             :     char       *oldfilename;
    4050             :     char        relpath[MAXPGPATH];
    4051             : 
    4052        7430 :     if (!filename)
    4053             :     {
    4054        3036 :         fd = stdin;
    4055        3036 :         filename = NULL;
    4056             :     }
    4057        4394 :     else if (strcmp(filename, "-") != 0)
    4058             :     {
    4059           6 :         canonicalize_path(filename);
    4060             : 
    4061             :         /*
    4062             :          * If we were asked to resolve the pathname relative to the location
    4063             :          * of the currently executing script, and there is one, and this is a
    4064             :          * relative pathname, then prepend all but the last pathname component
    4065             :          * of the current script to this pathname.
    4066             :          */
    4067           6 :         if (use_relative_path && pset.inputfile &&
    4068           0 :             !is_absolute_path(filename) && !has_drive_prefix(filename))
    4069             :         {
    4070           0 :             strlcpy(relpath, pset.inputfile, sizeof(relpath));
    4071           0 :             get_parent_directory(relpath);
    4072           0 :             join_path_components(relpath, relpath, filename);
    4073           0 :             canonicalize_path(relpath);
    4074             : 
    4075           0 :             filename = relpath;
    4076             :         }
    4077             : 
    4078           6 :         fd = fopen(filename, PG_BINARY_R);
    4079             : 
    4080           6 :         if (!fd)
    4081             :         {
    4082           0 :             pg_log_error("%s: %m", filename);
    4083           0 :             return EXIT_FAILURE;
    4084             :         }
    4085             :     }
    4086             :     else
    4087             :     {
    4088        4388 :         fd = stdin;
    4089        4388 :         filename = "<stdin>";   /* for future error messages */
    4090             :     }
    4091             : 
    4092        7430 :     oldfilename = pset.inputfile;
    4093        7430 :     pset.inputfile = filename;
    4094             : 
    4095        7430 :     pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE);
    4096             : 
    4097        7430 :     result = MainLoop(fd);
    4098             : 
    4099        7418 :     if (fd != stdin)
    4100           6 :         fclose(fd);
    4101             : 
    4102        7418 :     pset.inputfile = oldfilename;
    4103             : 
    4104        7418 :     pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE);
    4105             : 
    4106        7418 :     return result;
    4107             : }
    4108             : 
    4109             : 
    4110             : 
    4111             : static const char *
    4112           4 : _align2string(enum printFormat in)
    4113             : {
    4114           4 :     switch (in)
    4115             :     {
    4116           0 :         case PRINT_NOTHING:
    4117           0 :             return "nothing";
    4118             :             break;
    4119           4 :         case PRINT_ALIGNED:
    4120           4 :             return "aligned";
    4121             :             break;
    4122           0 :         case PRINT_ASCIIDOC:
    4123           0 :             return "asciidoc";
    4124             :             break;
    4125           0 :         case PRINT_CSV:
    4126           0 :             return "csv";
    4127             :             break;
    4128           0 :         case PRINT_HTML:
    4129           0 :             return "html";
    4130             :             break;
    4131           0 :         case PRINT_LATEX:
    4132           0 :             return "latex";
    4133             :             break;
    4134           0 :         case PRINT_LATEX_LONGTABLE:
    4135           0 :             return "latex-longtable";
    4136             :             break;
    4137           0 :         case PRINT_TROFF_MS:
    4138           0 :             return "troff-ms";
    4139             :             break;
    4140           0 :         case PRINT_UNALIGNED:
    4141           0 :             return "unaligned";
    4142             :             break;
    4143           0 :         case PRINT_WRAPPED:
    4144           0 :             return "wrapped";
    4145             :             break;
    4146             :     }
    4147           0 :     return "unknown";
    4148             : }
    4149             : 
    4150             : /*
    4151             :  * Parse entered Unicode linestyle.  If ok, update *linestyle and return
    4152             :  * true, else return false.
    4153             :  */
    4154             : static bool
    4155           0 : set_unicode_line_style(const char *value, size_t vallen,
    4156             :                        unicode_linestyle *linestyle)
    4157             : {
    4158           0 :     if (pg_strncasecmp("single", value, vallen) == 0)
    4159           0 :         *linestyle = UNICODE_LINESTYLE_SINGLE;
    4160           0 :     else if (pg_strncasecmp("double", value, vallen) == 0)
    4161           0 :         *linestyle = UNICODE_LINESTYLE_DOUBLE;
    4162             :     else
    4163           0 :         return false;
    4164           0 :     return true;
    4165             : }
    4166             : 
    4167             : static const char *
    4168          12 : _unicode_linestyle2string(int linestyle)
    4169             : {
    4170          12 :     switch (linestyle)
    4171             :     {
    4172          12 :         case UNICODE_LINESTYLE_SINGLE:
    4173          12 :             return "single";
    4174             :             break;
    4175           0 :         case UNICODE_LINESTYLE_DOUBLE:
    4176           0 :             return "double";
    4177             :             break;
    4178             :     }
    4179           0 :     return "unknown";
    4180             : }
    4181             : 
    4182             : /*
    4183             :  * do_pset
    4184             :  *
    4185             :  * Performs the assignment "param = value", where value could be NULL;
    4186             :  * for some params that has an effect such as inversion, for others
    4187             :  * it does nothing.
    4188             :  *
    4189             :  * Adjusts the state of the formatting options at *popt.  (In practice that
    4190             :  * is always pset.popt, but maybe someday it could be different.)
    4191             :  *
    4192             :  * If successful and quiet is false, then invokes printPsetInfo() to report
    4193             :  * the change.
    4194             :  *
    4195             :  * Returns true if successful, else false (eg for invalid param or value).
    4196             :  */
    4197             : bool
    4198        1250 : do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
    4199             : {
    4200        1250 :     size_t      vallen = 0;
    4201             : 
    4202             :     Assert(param != NULL);
    4203             : 
    4204        1250 :     if (value)
    4205        1184 :         vallen = strlen(value);
    4206             : 
    4207             :     /* set format */
    4208        1250 :     if (strcmp(param, "format") == 0)
    4209             :     {
    4210             :         static const struct fmt
    4211             :         {
    4212             :             const char *name;
    4213             :             enum printFormat number;
    4214             :         }           formats[] =
    4215             :         {
    4216             :             /* remember to update error message below when adding more */
    4217             :             {"aligned", PRINT_ALIGNED},
    4218             :             {"asciidoc", PRINT_ASCIIDOC},
    4219             :             {"csv", PRINT_CSV},
    4220             :             {"html", PRINT_HTML},
    4221             :             {"latex", PRINT_LATEX},
    4222             :             {"troff-ms", PRINT_TROFF_MS},
    4223             :             {"unaligned", PRINT_UNALIGNED},
    4224             :             {"wrapped", PRINT_WRAPPED}
    4225             :         };
    4226             : 
    4227         412 :         if (!value)
    4228             :             ;
    4229             :         else
    4230             :         {
    4231         412 :             int         match_pos = -1;
    4232             : 
    4233        3680 :             for (int i = 0; i < lengthof(formats); i++)
    4234             :             {
    4235        3272 :                 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
    4236             :                 {
    4237         412 :                     if (match_pos < 0)
    4238         408 :                         match_pos = i;
    4239             :                     else
    4240             :                     {
    4241           4 :                         pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
    4242             :                                      value,
    4243             :                                      formats[match_pos].name, formats[i].name);
    4244           4 :                         return false;
    4245             :                     }
    4246             :                 }
    4247             :             }
    4248         408 :             if (match_pos >= 0)
    4249         404 :                 popt->topt.format = formats[match_pos].number;
    4250           4 :             else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
    4251             :             {
    4252             :                 /*
    4253             :                  * We must treat latex-longtable specially because latex is a
    4254             :                  * prefix of it; if both were in the table above, we'd think
    4255             :                  * "latex" is ambiguous.
    4256             :                  */
    4257           4 :                 popt->topt.format = PRINT_LATEX_LONGTABLE;
    4258             :             }
    4259             :             else
    4260             :             {
    4261           0 :                 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
    4262           0 :                 return false;
    4263             :             }
    4264             :         }
    4265             :     }
    4266             : 
    4267             :     /* set table line style */
    4268         838 :     else if (strcmp(param, "linestyle") == 0)
    4269             :     {
    4270          20 :         if (!value)
    4271             :             ;
    4272          20 :         else if (pg_strncasecmp("ascii", value, vallen) == 0)
    4273          12 :             popt->topt.line_style = &pg_asciiformat;
    4274           8 :         else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
    4275           8 :             popt->topt.line_style = &pg_asciiformat_old;
    4276           0 :         else if (pg_strncasecmp("unicode", value, vallen) == 0)
    4277           0 :             popt->topt.line_style = &pg_utf8format;
    4278             :         else
    4279             :         {
    4280           0 :             pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
    4281           0 :             return false;
    4282             :         }
    4283             :     }
    4284             : 
    4285             :     /* set unicode border line style */
    4286         818 :     else if (strcmp(param, "unicode_border_linestyle") == 0)
    4287             :     {
    4288           0 :         if (!value)
    4289             :             ;
    4290           0 :         else if (set_unicode_line_style(value, vallen,
    4291             :                                         &popt->topt.unicode_border_linestyle))
    4292           0 :             refresh_utf8format(&(popt->topt));
    4293             :         else
    4294             :         {
    4295           0 :             pg_log_error("\\pset: allowed Unicode border line styles are single, double");
    4296           0 :             return false;
    4297             :         }
    4298             :     }
    4299             : 
    4300             :     /* set unicode column line style */
    4301         818 :     else if (strcmp(param, "unicode_column_linestyle") == 0)
    4302             :     {
    4303           0 :         if (!value)
    4304             :             ;
    4305           0 :         else if (set_unicode_line_style(value, vallen,
    4306             :                                         &popt->topt.unicode_column_linestyle))
    4307           0 :             refresh_utf8format(&(popt->topt));
    4308             :         else
    4309             :         {
    4310           0 :             pg_log_error("\\pset: allowed Unicode column line styles are single, double");
    4311           0 :             return false;
    4312             :         }
    4313             :     }
    4314             : 
    4315             :     /* set unicode header line style */
    4316         818 :     else if (strcmp(param, "unicode_header_linestyle") == 0)
    4317             :     {
    4318           0 :         if (!value)
    4319             :             ;
    4320           0 :         else if (set_unicode_line_style(value, vallen,
    4321             :                                         &popt->topt.unicode_header_linestyle))
    4322           0 :             refresh_utf8format(&(popt->topt));
    4323             :         else
    4324             :         {
    4325           0 :             pg_log_error("\\pset: allowed Unicode header line styles are single, double");
    4326           0 :             return false;
    4327             :         }
    4328             :     }
    4329             : 
    4330             :     /* set border style/width */
    4331         818 :     else if (strcmp(param, "border") == 0)
    4332             :     {
    4333         268 :         if (value)
    4334         268 :             popt->topt.border = atoi(value);
    4335             :     }
    4336             : 
    4337             :     /* set expanded/vertical mode */
    4338         550 :     else if (strcmp(param, "x") == 0 ||
    4339         550 :              strcmp(param, "expanded") == 0 ||
    4340         358 :              strcmp(param, "vertical") == 0)
    4341             :     {
    4342         192 :         if (value && pg_strcasecmp(value, "auto") == 0)
    4343           0 :             popt->topt.expanded = 2;
    4344         192 :         else if (value)
    4345             :         {
    4346             :             bool        on_off;
    4347             : 
    4348         174 :             if (ParseVariableBool(value, NULL, &on_off))
    4349         174 :                 popt->topt.expanded = on_off ? 1 : 0;
    4350             :             else
    4351             :             {
    4352           0 :                 PsqlVarEnumError(param, value, "on, off, auto");
    4353           0 :                 return false;
    4354             :             }
    4355             :         }
    4356             :         else
    4357          18 :             popt->topt.expanded = !popt->topt.expanded;
    4358             :     }
    4359             : 
    4360             :     /* field separator for CSV format */
    4361         358 :     else if (strcmp(param, "csv_fieldsep") == 0)
    4362             :     {
    4363          40 :         if (value)
    4364             :         {
    4365             :             /* CSV separator has to be a one-byte character */
    4366          40 :             if (strlen(value) != 1)
    4367             :             {
    4368          12 :                 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
    4369          12 :                 return false;
    4370             :             }
    4371          28 :             if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
    4372             :             {
    4373          12 :                 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
    4374          12 :                 return false;
    4375             :             }
    4376          16 :             popt->topt.csvFieldSep[0] = value[0];
    4377             :         }
    4378             :     }
    4379             : 
    4380             :     /* locale-aware numeric output */
    4381         318 :     else if (strcmp(param, "numericlocale") == 0)
    4382             :     {
    4383           8 :         if (value)
    4384           8 :             return ParseVariableBool(value, param, &popt->topt.numericLocale);
    4385             :         else
    4386           0 :             popt->topt.numericLocale = !popt->topt.numericLocale;
    4387             :     }
    4388             : 
    4389             :     /* null display */
    4390         310 :     else if (strcmp(param, "null") == 0)
    4391             :     {
    4392          46 :         if (value)
    4393             :         {
    4394          46 :             free(popt->nullPrint);
    4395          46 :             popt->nullPrint = pg_strdup(value);
    4396             :         }
    4397             :     }
    4398             : 
    4399             :     /* field separator for unaligned text */
    4400         264 :     else if (strcmp(param, "fieldsep") == 0)
    4401             :     {
    4402           4 :         if (value)
    4403             :         {
    4404           4 :             free(popt->topt.fieldSep.separator);
    4405           4 :             popt->topt.fieldSep.separator = pg_strdup(value);
    4406           4 :             popt->topt.fieldSep.separator_zero = false;
    4407             :         }
    4408             :     }
    4409             : 
    4410         260 :     else if (strcmp(param, "fieldsep_zero") == 0)
    4411             :     {
    4412           0 :         free(popt->topt.fieldSep.separator);
    4413           0 :         popt->topt.fieldSep.separator = NULL;
    4414           0 :         popt->topt.fieldSep.separator_zero = true;
    4415             :     }
    4416             : 
    4417             :     /* record separator for unaligned text */
    4418         260 :     else if (strcmp(param, "recordsep") == 0)
    4419             :     {
    4420           0 :         if (value)
    4421             :         {
    4422           0 :             free(popt->topt.recordSep.separator);
    4423           0 :             popt->topt.recordSep.separator = pg_strdup(value);
    4424           0 :             popt->topt.recordSep.separator_zero = false;
    4425             :         }
    4426             :     }
    4427             : 
    4428         260 :     else if (strcmp(param, "recordsep_zero") == 0)
    4429             :     {
    4430           0 :         free(popt->topt.recordSep.separator);
    4431           0 :         popt->topt.recordSep.separator = NULL;
    4432           0 :         popt->topt.recordSep.separator_zero = true;
    4433             :     }
    4434             : 
    4435             :     /* toggle between full and tuples-only format */
    4436         260 :     else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
    4437             :     {
    4438         192 :         if (value)
    4439         160 :             return ParseVariableBool(value, param, &popt->topt.tuples_only);
    4440             :         else
    4441          32 :             popt->topt.tuples_only = !popt->topt.tuples_only;
    4442             :     }
    4443             : 
    4444             :     /* set title override */
    4445          68 :     else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
    4446             :     {
    4447           4 :         free(popt->title);
    4448           4 :         if (!value)
    4449           0 :             popt->title = NULL;
    4450             :         else
    4451           4 :             popt->title = pg_strdup(value);
    4452             :     }
    4453             : 
    4454             :     /* set HTML table tag options */
    4455          64 :     else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
    4456             :     {
    4457          32 :         free(popt->topt.tableAttr);
    4458          32 :         if (!value)
    4459          16 :             popt->topt.tableAttr = NULL;
    4460             :         else
    4461          16 :             popt->topt.tableAttr = pg_strdup(value);
    4462             :     }
    4463             : 
    4464             :     /* toggle use of pager */
    4465          32 :     else if (strcmp(param, "pager") == 0)
    4466             :     {
    4467           0 :         if (value && pg_strcasecmp(value, "always") == 0)
    4468           0 :             popt->topt.pager = 2;
    4469           0 :         else if (value)
    4470             :         {
    4471             :             bool        on_off;
    4472             : 
    4473           0 :             if (!ParseVariableBool(value, NULL, &on_off))
    4474             :             {
    4475           0 :                 PsqlVarEnumError(param, value, "on, off, always");
    4476           0 :                 return false;
    4477             :             }
    4478           0 :             popt->topt.pager = on_off ? 1 : 0;
    4479             :         }
    4480           0 :         else if (popt->topt.pager == 1)
    4481           0 :             popt->topt.pager = 0;
    4482             :         else
    4483           0 :             popt->topt.pager = 1;
    4484             :     }
    4485             : 
    4486             :     /* set minimum lines for pager use */
    4487          32 :     else if (strcmp(param, "pager_min_lines") == 0)
    4488             :     {
    4489           0 :         if (value)
    4490           0 :             popt->topt.pager_min_lines = atoi(value);
    4491             :     }
    4492             : 
    4493             :     /* disable "(x rows)" footer */
    4494          32 :     else if (strcmp(param, "footer") == 0)
    4495             :     {
    4496           0 :         if (value)
    4497           0 :             return ParseVariableBool(value, param, &popt->topt.default_footer);
    4498             :         else
    4499           0 :             popt->topt.default_footer = !popt->topt.default_footer;
    4500             :     }
    4501             : 
    4502             :     /* set border style/width */
    4503          32 :     else if (strcmp(param, "columns") == 0)
    4504             :     {
    4505          32 :         if (value)
    4506          32 :             popt->topt.columns = atoi(value);
    4507             :     }
    4508             :     else
    4509             :     {
    4510           0 :         pg_log_error("\\pset: unknown option: %s", param);
    4511           0 :         return false;
    4512             :     }
    4513             : 
    4514        1054 :     if (!quiet)
    4515           0 :         printPsetInfo(param, &pset.popt);
    4516             : 
    4517        1054 :     return true;
    4518             : }
    4519             : 
    4520             : /*
    4521             :  * printPsetInfo: print the state of the "param" formatting parameter in popt.
    4522             :  */
    4523             : static bool
    4524           0 : printPsetInfo(const char *param, printQueryOpt *popt)
    4525             : {
    4526             :     Assert(param != NULL);
    4527             : 
    4528             :     /* show border style/width */
    4529           0 :     if (strcmp(param, "border") == 0)
    4530           0 :         printf(_("Border style is %d.\n"), popt->topt.border);
    4531             : 
    4532             :     /* show the target width for the wrapped format */
    4533           0 :     else if (strcmp(param, "columns") == 0)
    4534             :     {
    4535           0 :         if (!popt->topt.columns)
    4536           0 :             printf(_("Target width is unset.\n"));
    4537             :         else
    4538           0 :             printf(_("Target width is %d.\n"), popt->topt.columns);
    4539             :     }
    4540             : 
    4541             :     /* show expanded/vertical mode */
    4542           0 :     else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
    4543             :     {
    4544           0 :         if (popt->topt.expanded == 1)
    4545           0 :             printf(_("Expanded display is on.\n"));
    4546           0 :         else if (popt->topt.expanded == 2)
    4547           0 :             printf(_("Expanded display is used automatically.\n"));
    4548             :         else
    4549           0 :             printf(_("Expanded display is off.\n"));
    4550             :     }
    4551             : 
    4552             :     /* show field separator for CSV format */
    4553           0 :     else if (strcmp(param, "csv_fieldsep") == 0)
    4554             :     {
    4555           0 :         printf(_("Field separator for CSV is \"%s\".\n"),
    4556             :                popt->topt.csvFieldSep);
    4557             :     }
    4558             : 
    4559             :     /* show field separator for unaligned text */
    4560           0 :     else if (strcmp(param, "fieldsep") == 0)
    4561             :     {
    4562           0 :         if (popt->topt.fieldSep.separator_zero)
    4563           0 :             printf(_("Field separator is zero byte.\n"));
    4564             :         else
    4565           0 :             printf(_("Field separator is \"%s\".\n"),
    4566             :                    popt->topt.fieldSep.separator);
    4567             :     }
    4568             : 
    4569           0 :     else if (strcmp(param, "fieldsep_zero") == 0)
    4570             :     {
    4571           0 :         printf(_("Field separator is zero byte.\n"));
    4572             :     }
    4573             : 
    4574             :     /* show disable "(x rows)" footer */
    4575           0 :     else if (strcmp(param, "footer") == 0)
    4576             :     {
    4577           0 :         if (popt->topt.default_footer)
    4578           0 :             printf(_("Default footer is on.\n"));
    4579             :         else
    4580           0 :             printf(_("Default footer is off.\n"));
    4581             :     }
    4582             : 
    4583             :     /* show format */
    4584           0 :     else if (strcmp(param, "format") == 0)
    4585             :     {
    4586           0 :         printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
    4587             :     }
    4588             : 
    4589             :     /* show table line style */
    4590           0 :     else if (strcmp(param, "linestyle") == 0)
    4591             :     {
    4592           0 :         printf(_("Line style is %s.\n"),
    4593             :                get_line_style(&popt->topt)->name);
    4594             :     }
    4595             : 
    4596             :     /* show null display */
    4597           0 :     else if (strcmp(param, "null") == 0)
    4598             :     {
    4599           0 :         printf(_("Null display is \"%s\".\n"),
    4600             :                popt->nullPrint ? popt->nullPrint : "");
    4601             :     }
    4602             : 
    4603             :     /* show locale-aware numeric output */
    4604           0 :     else if (strcmp(param, "numericlocale") == 0)
    4605             :     {
    4606           0 :         if (popt->topt.numericLocale)
    4607           0 :             printf(_("Locale-adjusted numeric output is on.\n"));
    4608             :         else
    4609           0 :             printf(_("Locale-adjusted numeric output is off.\n"));
    4610             :     }
    4611             : 
    4612             :     /* show toggle use of pager */
    4613           0 :     else if (strcmp(param, "pager") == 0)
    4614             :     {
    4615           0 :         if (popt->topt.pager == 1)
    4616           0 :             printf(_("Pager is used for long output.\n"));
    4617           0 :         else if (popt->topt.pager == 2)
    4618           0 :             printf(_("Pager is always used.\n"));
    4619             :         else
    4620           0 :             printf(_("Pager usage is off.\n"));
    4621             :     }
    4622             : 
    4623             :     /* show minimum lines for pager use */
    4624           0 :     else if (strcmp(param, "pager_min_lines") == 0)
    4625             :     {
    4626           0 :         printf(ngettext("Pager won't be used for less than %d line.\n",
    4627             :                         "Pager won't be used for less than %d lines.\n",
    4628             :                         popt->topt.pager_min_lines),
    4629             :                popt->topt.pager_min_lines);
    4630             :     }
    4631             : 
    4632             :     /* show record separator for unaligned text */
    4633           0 :     else if (strcmp(param, "recordsep") == 0)
    4634             :     {
    4635           0 :         if (popt->topt.recordSep.separator_zero)
    4636           0 :             printf(_("Record separator is zero byte.\n"));
    4637           0 :         else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
    4638           0 :             printf(_("Record separator is <newline>.\n"));
    4639             :         else
    4640           0 :             printf(_("Record separator is \"%s\".\n"),
    4641             :                    popt->topt.recordSep.separator);
    4642             :     }
    4643             : 
    4644           0 :     else if (strcmp(param, "recordsep_zero") == 0)
    4645             :     {
    4646           0 :         printf(_("Record separator is zero byte.\n"));
    4647             :     }
    4648             : 
    4649             :     /* show HTML table tag options */
    4650           0 :     else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
    4651             :     {
    4652           0 :         if (popt->topt.tableAttr)
    4653           0 :             printf(_("Table attributes are \"%s\".\n"),
    4654             :                    popt->topt.tableAttr);
    4655             :         else
    4656           0 :             printf(_("Table attributes unset.\n"));
    4657             :     }
    4658             : 
    4659             :     /* show title override */
    4660           0 :     else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
    4661             :     {
    4662           0 :         if (popt->title)
    4663           0 :             printf(_("Title is \"%s\".\n"), popt->title);
    4664             :         else
    4665           0 :             printf(_("Title is unset.\n"));
    4666             :     }
    4667             : 
    4668             :     /* show toggle between full and tuples-only format */
    4669           0 :     else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
    4670             :     {
    4671           0 :         if (popt->topt.tuples_only)
    4672           0 :             printf(_("Tuples only is on.\n"));
    4673             :         else
    4674           0 :             printf(_("Tuples only is off.\n"));
    4675             :     }
    4676             : 
    4677             :     /* Unicode style formatting */
    4678           0 :     else if (strcmp(param, "unicode_border_linestyle") == 0)
    4679             :     {
    4680           0 :         printf(_("Unicode border line style is \"%s\".\n"),
    4681             :                _unicode_linestyle2string(popt->topt.unicode_border_linestyle));
    4682             :     }
    4683             : 
    4684           0 :     else if (strcmp(param, "unicode_column_linestyle") == 0)
    4685             :     {
    4686           0 :         printf(_("Unicode column line style is \"%s\".\n"),
    4687             :                _unicode_linestyle2string(popt->topt.unicode_column_linestyle));
    4688             :     }
    4689             : 
    4690           0 :     else if (strcmp(param, "unicode_header_linestyle") == 0)
    4691             :     {
    4692           0 :         printf(_("Unicode header line style is \"%s\".\n"),
    4693             :                _unicode_linestyle2string(popt->topt.unicode_header_linestyle));
    4694             :     }
    4695             : 
    4696             :     else
    4697             :     {
    4698           0 :         pg_log_error("\\pset: unknown option: %s", param);
    4699           0 :         return false;
    4700             :     }
    4701             : 
    4702           0 :     return true;
    4703             : }
    4704             : 
    4705             : /*
    4706             :  * savePsetInfo: make a malloc'd copy of the data in *popt.
    4707             :  *
    4708             :  * Possibly this should be somewhere else, but it's a bit specific to psql.
    4709             :  */
    4710             : printQueryOpt *
    4711          24 : savePsetInfo(const printQueryOpt *popt)
    4712             : {
    4713             :     printQueryOpt *save;
    4714             : 
    4715          24 :     save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
    4716             : 
    4717             :     /* Flat-copy all the scalar fields, then duplicate sub-structures. */
    4718          24 :     memcpy(save, popt, sizeof(printQueryOpt));
    4719             : 
    4720             :     /* topt.line_style points to const data that need not be duplicated */
    4721          24 :     if (popt->topt.fieldSep.separator)
    4722          24 :         save->topt.fieldSep.separator = pg_strdup(popt->topt.fieldSep.separator);
    4723          24 :     if (popt->topt.recordSep.separator)
    4724          24 :         save->topt.recordSep.separator = pg_strdup(popt->topt.recordSep.separator);
    4725          24 :     if (popt->topt.tableAttr)
    4726           0 :         save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
    4727          24 :     if (popt->nullPrint)
    4728           0 :         save->nullPrint = pg_strdup(popt->nullPrint);
    4729          24 :     if (popt->title)
    4730           0 :         save->title = pg_strdup(popt->title);
    4731             : 
    4732             :     /*
    4733             :      * footers and translate_columns are never set in psql's print settings,
    4734             :      * so we needn't write code to duplicate them.
    4735             :      */
    4736             :     Assert(popt->footers == NULL);
    4737             :     Assert(popt->translate_columns == NULL);
    4738             : 
    4739          24 :     return save;
    4740             : }
    4741             : 
    4742             : /*
    4743             :  * restorePsetInfo: restore *popt from the previously-saved copy *save,
    4744             :  * then free *save.
    4745             :  */
    4746             : void
    4747          24 : restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
    4748             : {
    4749             :     /* Free all the old data we're about to overwrite the pointers to. */
    4750             : 
    4751             :     /* topt.line_style points to const data that need not be duplicated */
    4752          24 :     if (popt->topt.fieldSep.separator)
    4753          24 :         free(popt->topt.fieldSep.separator);
    4754          24 :     if (popt->topt.recordSep.separator)
    4755          24 :         free(popt->topt.recordSep.separator);
    4756          24 :     if (popt->topt.tableAttr)
    4757           0 :         free(popt->topt.tableAttr);
    4758          24 :     if (popt->nullPrint)
    4759           0 :         free(popt->nullPrint);
    4760          24 :     if (popt->title)
    4761           4 :         free(popt->title);
    4762             : 
    4763             :     /*
    4764             :      * footers and translate_columns are never set in psql's print settings,
    4765             :      * so we needn't write code to duplicate them.
    4766             :      */
    4767             :     Assert(popt->footers == NULL);
    4768             :     Assert(popt->translate_columns == NULL);
    4769             : 
    4770             :     /* Now we may flat-copy all the fields, including pointers. */
    4771          24 :     memcpy(popt, save, sizeof(printQueryOpt));
    4772             : 
    4773             :     /* Lastly, free "save" ... but its sub-structures now belong to popt. */
    4774          24 :     free(save);
    4775          24 : }
    4776             : 
    4777             : static const char *
    4778          24 : pset_bool_string(bool val)
    4779             : {
    4780          24 :     return val ? "on" : "off";
    4781             : }
    4782             : 
    4783             : 
    4784             : static char *
    4785          16 : pset_quoted_string(const char *str)
    4786             : {
    4787          16 :     char       *ret = pg_malloc(strlen(str) * 2 + 3);
    4788          16 :     char       *r = ret;
    4789             : 
    4790          16 :     *r++ = '\'';
    4791             : 
    4792          28 :     for (; *str; str++)
    4793             :     {
    4794          12 :         if (*str == '\n')
    4795             :         {
    4796           4 :             *r++ = '\\';
    4797           4 :             *r++ = 'n';
    4798             :         }
    4799           8 :         else if (*str == '\'')
    4800             :         {
    4801           0 :             *r++ = '\\';
    4802           0 :             *r++ = '\'';
    4803             :         }
    4804             :         else
    4805           8 :             *r++ = *str;
    4806             :     }
    4807             : 
    4808          16 :     *r++ = '\'';
    4809          16 :     *r = '\0';
    4810             : 
    4811          16 :     return ret;
    4812             : }
    4813             : 
    4814             : 
    4815             : /*
    4816             :  * Return a malloc'ed string for the \pset value.
    4817             :  *
    4818             :  * Note that for some string parameters, print.c distinguishes between unset
    4819             :  * and empty string, but for others it doesn't.  This function should produce
    4820             :  * output that produces the correct setting when fed back into \pset.
    4821             :  */
    4822             : static char *
    4823          84 : pset_value_string(const char *param, printQueryOpt *popt)
    4824             : {
    4825             :     Assert(param != NULL);
    4826             : 
    4827          84 :     if (strcmp(param, "border") == 0)
    4828           4 :         return psprintf("%d", popt->topt.border);
    4829          80 :     else if (strcmp(param, "columns") == 0)
    4830           4 :         return psprintf("%d", popt->topt.columns);
    4831          76 :     else if (strcmp(param, "csv_fieldsep") == 0)
    4832           4 :         return pset_quoted_string(popt->topt.csvFieldSep);
    4833          72 :     else if (strcmp(param, "expanded") == 0)
    4834           8 :         return pstrdup(popt->topt.expanded == 2
    4835             :                        ? "auto"
    4836           4 :                        : pset_bool_string(popt->topt.expanded));
    4837          68 :     else if (strcmp(param, "fieldsep") == 0)
    4838           4 :         return pset_quoted_string(popt->topt.fieldSep.separator
    4839             :                                   ? popt->topt.fieldSep.separator
    4840             :                                   : "");
    4841          64 :     else if (strcmp(param, "fieldsep_zero") == 0)
    4842           4 :         return pstrdup(pset_bool_string(popt->topt.fieldSep.separator_zero));
    4843          60 :     else if (strcmp(param, "footer") == 0)
    4844           4 :         return pstrdup(pset_bool_string(popt->topt.default_footer));
    4845          56 :     else if (strcmp(param, "format") == 0)
    4846           4 :         return psprintf("%s", _align2string(popt->topt.format));
    4847          52 :     else if (strcmp(param, "linestyle") == 0)
    4848           4 :         return psprintf("%s", get_line_style(&popt->topt)->name);
    4849          48 :     else if (strcmp(param, "null") == 0)
    4850           4 :         return pset_quoted_string(popt->nullPrint
    4851             :                                   ? popt->nullPrint
    4852             :                                   : "");
    4853          44 :     else if (strcmp(param, "numericlocale") == 0)
    4854           4 :         return pstrdup(pset_bool_string(popt->topt.numericLocale));
    4855          40 :     else if (strcmp(param, "pager") == 0)
    4856           4 :         return psprintf("%d", popt->topt.pager);
    4857          36 :     else if (strcmp(param, "pager_min_lines") == 0)
    4858           4 :         return psprintf("%d", popt->topt.pager_min_lines);
    4859          32 :     else if (strcmp(param, "recordsep") == 0)
    4860           4 :         return pset_quoted_string(popt->topt.recordSep.separator
    4861             :                                   ? popt->topt.recordSep.separator
    4862             :                                   : "");
    4863          28 :     else if (strcmp(param, "recordsep_zero") == 0)
    4864           4 :         return pstrdup(pset_bool_string(popt->topt.recordSep.separator_zero));
    4865          24 :     else if (strcmp(param, "tableattr") == 0)
    4866           4 :         return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
    4867          20 :     else if (strcmp(param, "title") == 0)
    4868           4 :         return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
    4869          16 :     else if (strcmp(param, "tuples_only") == 0)
    4870           4 :         return pstrdup(pset_bool_string(popt->topt.tuples_only));
    4871          12 :     else if (strcmp(param, "unicode_border_linestyle") == 0)
    4872           4 :         return pstrdup(_unicode_linestyle2string(popt->topt.unicode_border_linestyle));
    4873           8 :     else if (strcmp(param, "unicode_column_linestyle") == 0)
    4874           4 :         return pstrdup(_unicode_linestyle2string(popt->topt.unicode_column_linestyle));
    4875           4 :     else if (strcmp(param, "unicode_header_linestyle") == 0)
    4876           4 :         return pstrdup(_unicode_linestyle2string(popt->topt.unicode_header_linestyle));
    4877             :     else
    4878           0 :         return pstrdup("ERROR");
    4879             : }
    4880             : 
    4881             : 
    4882             : 
    4883             : #ifndef WIN32
    4884             : #define DEFAULT_SHELL "/bin/sh"
    4885             : #else
    4886             : /*
    4887             :  *  CMD.EXE is in different places in different Win32 releases so we
    4888             :  *  have to rely on the path to find it.
    4889             :  */
    4890             : #define DEFAULT_SHELL "cmd.exe"
    4891             : #endif
    4892             : 
    4893             : static bool
    4894           4 : do_shell(const char *command)
    4895             : {
    4896             :     int         result;
    4897             : 
    4898           4 :     if (!command)
    4899             :     {
    4900             :         char       *sys;
    4901             :         const char *shellName;
    4902             : 
    4903           0 :         shellName = getenv("SHELL");
    4904             : #ifdef WIN32
    4905             :         if (shellName == NULL)
    4906             :             shellName = getenv("COMSPEC");
    4907             : #endif
    4908           0 :         if (shellName == NULL)
    4909           0 :             shellName = DEFAULT_SHELL;
    4910             : 
    4911             :         /* See EDITOR handling comment for an explanation */
    4912             : #ifndef WIN32
    4913           0 :         sys = psprintf("exec %s", shellName);
    4914             : #else
    4915             :         sys = psprintf("\"%s\"", shellName);
    4916             : #endif
    4917           0 :         result = system(sys);
    4918           0 :         free(sys);
    4919             :     }
    4920             :     else
    4921           4 :         result = system(command);
    4922             : 
    4923           4 :     if (result == 127 || result == -1)
    4924             :     {
    4925           0 :         pg_log_error("\\!: failed");
    4926           0 :         return false;
    4927             :     }
    4928           4 :     return true;
    4929             : }
    4930             : 
    4931             : /*
    4932             :  * do_watch -- handler for \watch
    4933             :  *
    4934             :  * We break this out of exec_command to avoid having to plaster "volatile"
    4935             :  * onto a bunch of exec_command's variables to silence stupider compilers.
    4936             :  */
    4937             : static bool
    4938           0 : do_watch(PQExpBuffer query_buf, double sleep)
    4939             : {
    4940           0 :     long        sleep_ms = (long) (sleep * 1000);
    4941           0 :     printQueryOpt myopt = pset.popt;
    4942             :     const char *strftime_fmt;
    4943             :     const char *user_title;
    4944             :     char       *title;
    4945           0 :     const char *pagerprog = NULL;
    4946           0 :     FILE       *pagerpipe = NULL;
    4947             :     int         title_len;
    4948           0 :     int         res = 0;
    4949             : #ifdef HAVE_POSIX_DECL_SIGWAIT
    4950             :     sigset_t    sigalrm_sigchld_sigint;
    4951             :     sigset_t    sigalrm_sigchld;
    4952             :     sigset_t    sigint;
    4953             :     struct itimerval interval;
    4954           0 :     bool        done = false;
    4955             : #endif
    4956             : 
    4957           0 :     if (!query_buf || query_buf->len <= 0)
    4958             :     {
    4959           0 :         pg_log_error("\\watch cannot be used with an empty query");
    4960           0 :         return false;
    4961             :     }
    4962             : 
    4963             : #ifdef HAVE_POSIX_DECL_SIGWAIT
    4964           0 :     sigemptyset(&sigalrm_sigchld_sigint);
    4965           0 :     sigaddset(&sigalrm_sigchld_sigint, SIGCHLD);
    4966           0 :     sigaddset(&sigalrm_sigchld_sigint, SIGALRM);
    4967           0 :     sigaddset(&sigalrm_sigchld_sigint, SIGINT);
    4968             : 
    4969           0 :     sigemptyset(&sigalrm_sigchld);
    4970           0 :     sigaddset(&sigalrm_sigchld, SIGCHLD);
    4971           0 :     sigaddset(&sigalrm_sigchld, SIGALRM);
    4972             : 
    4973           0 :     sigemptyset(&sigint);
    4974           0 :     sigaddset(&sigint, SIGINT);
    4975             : 
    4976             :     /*
    4977             :      * Block SIGALRM and SIGCHLD before we start the timer and the pager (if
    4978             :      * configured), to avoid races.  sigwait() will receive them.
    4979             :      */
    4980           0 :     sigprocmask(SIG_BLOCK, &sigalrm_sigchld, NULL);
    4981             : 
    4982             :     /*
    4983             :      * Set a timer to interrupt sigwait() so we can run the query at the
    4984             :      * requested intervals.
    4985             :      */
    4986           0 :     interval.it_value.tv_sec = sleep_ms / 1000;
    4987           0 :     interval.it_value.tv_usec = (sleep_ms % 1000) * 1000;
    4988           0 :     interval.it_interval = interval.it_value;
    4989           0 :     if (setitimer(ITIMER_REAL, &interval, NULL) < 0)
    4990             :     {
    4991           0 :         pg_log_error("could not set timer: %m");
    4992           0 :         done = true;
    4993             :     }
    4994             : #endif
    4995             : 
    4996             :     /*
    4997             :      * For \watch, we ignore the size of the result and always use the pager
    4998             :      * if PSQL_WATCH_PAGER is set.  We also ignore the regular PSQL_PAGER or
    4999             :      * PAGER environment variables, because traditional pagers probably won't
    5000             :      * be very useful for showing a stream of results.
    5001             :      */
    5002             : #ifdef HAVE_POSIX_DECL_SIGWAIT
    5003           0 :     pagerprog = getenv("PSQL_WATCH_PAGER");
    5004             : #endif
    5005           0 :     if (pagerprog && myopt.topt.pager)
    5006             :     {
    5007           0 :         disable_sigpipe_trap();
    5008           0 :         pagerpipe = popen(pagerprog, "w");
    5009             : 
    5010           0 :         if (!pagerpipe)
    5011             :             /* silently proceed without pager */
    5012           0 :             restore_sigpipe_trap();
    5013             :     }
    5014             : 
    5015             :     /*
    5016             :      * Choose format for timestamps.  We might eventually make this a \pset
    5017             :      * option.  In the meantime, using a variable for the format suppresses
    5018             :      * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
    5019             :      */
    5020           0 :     strftime_fmt = "%c";
    5021             : 
    5022             :     /*
    5023             :      * Set up rendering options, in particular, disable the pager unless
    5024             :      * PSQL_WATCH_PAGER was successfully launched.
    5025             :      */
    5026           0 :     if (!pagerpipe)
    5027           0 :         myopt.topt.pager = 0;
    5028             : 
    5029             : 
    5030             :     /*
    5031             :      * If there's a title in the user configuration, make sure we have room
    5032             :      * for it in the title buffer.  Allow 128 bytes for the timestamp plus 128
    5033             :      * bytes for the rest.
    5034             :      */
    5035           0 :     user_title = myopt.title;
    5036           0 :     title_len = (user_title ? strlen(user_title) : 0) + 256;
    5037           0 :     title = pg_malloc(title_len);
    5038             : 
    5039             :     for (;;)
    5040           0 :     {
    5041             :         time_t      timer;
    5042             :         char        timebuf[128];
    5043             : 
    5044             :         /*
    5045             :          * Prepare title for output.  Note that we intentionally include a
    5046             :          * newline at the end of the title; this is somewhat historical but it
    5047             :          * makes for reasonably nicely formatted output in simple cases.
    5048             :          */
    5049           0 :         timer = time(NULL);
    5050           0 :         strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
    5051             : 
    5052           0 :         if (user_title)
    5053           0 :             snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
    5054             :                      user_title, timebuf, sleep);
    5055             :         else
    5056           0 :             snprintf(title, title_len, _("%s (every %gs)\n"),
    5057             :                      timebuf, sleep);
    5058           0 :         myopt.title = title;
    5059             : 
    5060             :         /* Run the query and print out the results */
    5061           0 :         res = PSQLexecWatch(query_buf->data, &myopt, pagerpipe);
    5062             : 
    5063             :         /*
    5064             :          * PSQLexecWatch handles the case where we can no longer repeat the
    5065             :          * query, and returns 0 or -1.
    5066             :          */
    5067           0 :         if (res <= 0)
    5068           0 :             break;
    5069             : 
    5070           0 :         if (pagerpipe && ferror(pagerpipe))
    5071           0 :             break;
    5072             : 
    5073             : #ifndef HAVE_POSIX_DECL_SIGWAIT
    5074             : 
    5075             :         /*
    5076             :          * Set up cancellation of 'watch' via SIGINT.  We redo this each time
    5077             :          * through the loop since it's conceivable something inside
    5078             :          * PSQLexecWatch could change sigint_interrupt_jmp.
    5079             :          */
    5080             :         if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
    5081             :             break;
    5082             : 
    5083             :         /*
    5084             :          * Enable 'watch' cancellations and wait a while before running the
    5085             :          * query again.  Break the sleep into short intervals (at most 1s).
    5086             :          */
    5087             :         sigint_interrupt_enabled = true;
    5088             :         for (long i = sleep_ms; i > 0;)
    5089             :         {
    5090             :             long        s = Min(i, 1000L);
    5091             : 
    5092             :             pg_usleep(s * 1000L);
    5093             :             if (cancel_pressed)
    5094             :                 break;
    5095             :             i -= s;
    5096             :         }
    5097             :         sigint_interrupt_enabled = false;
    5098             : #else
    5099             :         /* sigwait() will handle SIGINT. */
    5100           0 :         sigprocmask(SIG_BLOCK, &sigint, NULL);
    5101           0 :         if (cancel_pressed)
    5102           0 :             done = true;
    5103             : 
    5104             :         /* Wait for SIGINT, SIGCHLD or SIGALRM. */
    5105           0 :         while (!done)
    5106             :         {
    5107             :             int         signal_received;
    5108             : 
    5109           0 :             errno = sigwait(&sigalrm_sigchld_sigint, &signal_received);
    5110           0 :             if (errno != 0)
    5111             :             {
    5112             :                 /* Some other signal arrived? */
    5113           0 :                 if (errno == EINTR)
    5114           0 :                     continue;
    5115             :                 else
    5116             :                 {
    5117           0 :                     pg_log_error("could not wait for signals: %m");
    5118           0 :                     done = true;
    5119           0 :                     break;
    5120             :                 }
    5121             :             }
    5122             :             /* On ^C or pager exit, it's time to stop running the query. */
    5123           0 :             if (signal_received == SIGINT || signal_received == SIGCHLD)
    5124           0 :                 done = true;
    5125             :             /* Otherwise, we must have SIGALRM.  Time to run the query again. */
    5126           0 :             break;
    5127             :         }
    5128             : 
    5129             :         /* Unblock SIGINT so that slow queries can be interrupted. */
    5130           0 :         sigprocmask(SIG_UNBLOCK, &sigint, NULL);
    5131           0 :         if (done)
    5132           0 :             break;
    5133             : #endif
    5134             :     }
    5135             : 
    5136           0 :     if (pagerpipe)
    5137             :     {
    5138           0 :         pclose(pagerpipe);
    5139           0 :         restore_sigpipe_trap();
    5140             :     }
    5141             : 
    5142             : #ifdef HAVE_POSIX_DECL_SIGWAIT
    5143             :     /* Disable the interval timer. */
    5144           0 :     memset(&interval, 0, sizeof(interval));
    5145           0 :     setitimer(ITIMER_REAL, &interval, NULL);
    5146             :     /* Unblock SIGINT, SIGCHLD and SIGALRM. */
    5147           0 :     sigprocmask(SIG_UNBLOCK, &sigalrm_sigchld_sigint, NULL);
    5148             : #endif
    5149             : 
    5150           0 :     pg_free(title);
    5151           0 :     return (res >= 0);
    5152             : }
    5153             : 
    5154             : /*
    5155             :  * a little code borrowed from PSQLexec() to manage ECHO_HIDDEN output.
    5156             :  * returns true unless we have ECHO_HIDDEN_NOEXEC.
    5157             :  */
    5158             : static bool
    5159          20 : echo_hidden_command(const char *query)
    5160             : {
    5161          20 :     if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
    5162             :     {
    5163           0 :         printf(_("********* QUERY **********\n"
    5164             :                  "%s\n"
    5165             :                  "**************************\n\n"), query);
    5166           0 :         fflush(stdout);
    5167           0 :         if (pset.logfile)
    5168             :         {
    5169           0 :             fprintf(pset.logfile,
    5170           0 :                     _("********* QUERY **********\n"
    5171             :                       "%s\n"
    5172             :                       "**************************\n\n"), query);
    5173           0 :             fflush(pset.logfile);
    5174             :         }
    5175             : 
    5176           0 :         if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
    5177           0 :             return false;
    5178             :     }
    5179          20 :     return true;
    5180             : }
    5181             : 
    5182             : /*
    5183             :  * Look up the object identified by obj_type and desc.  If successful,
    5184             :  * store its OID in *obj_oid and return true, else return false.
    5185             :  *
    5186             :  * Note that we'll fail if the object doesn't exist OR if there are multiple
    5187             :  * matching candidates OR if there's something syntactically wrong with the
    5188             :  * object description; unfortunately it can be hard to tell the difference.
    5189             :  */
    5190             : static bool
    5191          10 : lookup_object_oid(EditableObjectType obj_type, const char *desc,
    5192             :                   Oid *obj_oid)
    5193             : {
    5194          10 :     bool        result = true;
    5195          10 :     PQExpBuffer query = createPQExpBuffer();
    5196             :     PGresult   *res;
    5197             : 
    5198          10 :     switch (obj_type)
    5199             :     {
    5200           6 :         case EditableFunction:
    5201             : 
    5202             :             /*
    5203             :              * We have a function description, e.g. "x" or "x(int)".  Issue a
    5204             :              * query to retrieve the function's OID using a cast to regproc or
    5205             :              * regprocedure (as appropriate).
    5206             :              */
    5207           6 :             appendPQExpBufferStr(query, "SELECT ");
    5208           6 :             appendStringLiteralConn(query, desc, pset.db);
    5209           6 :             appendPQExpBuffer(query, "::pg_catalog.%s::pg_catalog.oid",
    5210           6 :                               strchr(desc, '(') ? "regprocedure" : "regproc");
    5211           6 :             break;
    5212             : 
    5213           4 :         case EditableView:
    5214             : 
    5215             :             /*
    5216             :              * Convert view name (possibly schema-qualified) to OID.  Note:
    5217             :              * this code doesn't check if the relation is actually a view.
    5218             :              * We'll detect that in get_create_object_cmd().
    5219             :              */
    5220           4 :             appendPQExpBufferStr(query, "SELECT ");
    5221           4 :             appendStringLiteralConn(query, desc, pset.db);
    5222           4 :             appendPQExpBufferStr(query, "::pg_catalog.regclass::pg_catalog.oid");
    5223           4 :             break;
    5224             :     }
    5225             : 
    5226          10 :     if (!echo_hidden_command(query->data))
    5227             :     {
    5228           0 :         destroyPQExpBuffer(query);
    5229           0 :         return false;
    5230             :     }
    5231          10 :     res = PQexec(pset.db, query->data);
    5232          10 :     if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
    5233          10 :         *obj_oid = atooid(PQgetvalue(res, 0, 0));
    5234             :     else
    5235             :     {
    5236           0 :         minimal_error_message(res);
    5237           0 :         result = false;
    5238             :     }
    5239             : 
    5240          10 :     PQclear(res);
    5241          10 :     destroyPQExpBuffer(query);
    5242             : 
    5243          10 :     return result;
    5244             : }
    5245             : 
    5246             : /*
    5247             :  * Construct a "CREATE OR REPLACE ..." command that describes the specified
    5248             :  * database object.  If successful, the result is stored in buf.
    5249             :  */
    5250             : static bool
    5251          10 : get_create_object_cmd(EditableObjectType obj_type, Oid oid,
    5252             :                       PQExpBuffer buf)
    5253             : {
    5254          10 :     bool        result = true;
    5255          10 :     PQExpBuffer query = createPQExpBuffer();
    5256             :     PGresult   *res;
    5257             : 
    5258          10 :     switch (obj_type)
    5259             :     {
    5260           6 :         case EditableFunction:
    5261           6 :             printfPQExpBuffer(query,
    5262             :                               "SELECT pg_catalog.pg_get_functiondef(%u)",
    5263             :                               oid);
    5264           6 :             break;
    5265             : 
    5266           4 :         case EditableView:
    5267             : 
    5268             :             /*
    5269             :              * pg_get_viewdef() just prints the query, so we must prepend
    5270             :              * CREATE for ourselves.  We must fully qualify the view name to
    5271             :              * ensure the right view gets replaced.  Also, check relation kind
    5272             :              * to be sure it's a view.
    5273             :              *
    5274             :              * Starting with 9.2, views may have reloptions (security_barrier)
    5275             :              * and from 9.4 onwards they may also have WITH [LOCAL|CASCADED]
    5276             :              * CHECK OPTION.  These are not part of the view definition
    5277             :              * returned by pg_get_viewdef() and so need to be retrieved
    5278             :              * separately.  Materialized views (introduced in 9.3) may have
    5279             :              * arbitrary storage parameter reloptions.
    5280             :              */
    5281           4 :             if (pset.sversion >= 90400)
    5282             :             {
    5283           4 :                 printfPQExpBuffer(query,
    5284             :                                   "SELECT nspname, relname, relkind, "
    5285             :                                   "pg_catalog.pg_get_viewdef(c.oid, true), "
    5286             :                                   "pg_catalog.array_remove(pg_catalog.array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
    5287             :                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
    5288             :                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END AS checkoption "
    5289             :                                   "FROM pg_catalog.pg_class c "
    5290             :                                   "LEFT JOIN pg_catalog.pg_namespace n "
    5291             :                                   "ON c.relnamespace = n.oid WHERE c.oid = %u",
    5292             :                                   oid);
    5293             :             }
    5294           0 :             else if (pset.sversion >= 90200)
    5295             :             {
    5296           0 :                 printfPQExpBuffer(query,
    5297             :                                   "SELECT nspname, relname, relkind, "
    5298             :                                   "pg_catalog.pg_get_viewdef(c.oid, true), "
    5299             :                                   "c.reloptions AS reloptions, "
    5300             :                                   "NULL AS checkoption "
    5301             :                                   "FROM pg_catalog.pg_class c "
    5302             :                                   "LEFT JOIN pg_catalog.pg_namespace n "
    5303             :                                   "ON c.relnamespace = n.oid WHERE c.oid = %u",
    5304             :                                   oid);
    5305             :             }
    5306             :             else
    5307             :             {
    5308           0 :                 printfPQExpBuffer(query,
    5309             :                                   "SELECT nspname, relname, relkind, "
    5310             :                                   "pg_catalog.pg_get_viewdef(c.oid, true), "
    5311             :                                   "NULL AS reloptions, "
    5312             :                                   "NULL AS checkoption "
    5313             :                                   "FROM pg_catalog.pg_class c "
    5314             :                                   "LEFT JOIN pg_catalog.pg_namespace n "
    5315             :                                   "ON c.relnamespace = n.oid WHERE c.oid = %u",
    5316             :                                   oid);
    5317             :             }
    5318           4 :             break;
    5319             :     }
    5320             : 
    5321          10 :     if (!echo_hidden_command(query->data))
    5322             :     {
    5323           0 :         destroyPQExpBuffer(query);
    5324           0 :         return false;
    5325             :     }
    5326          10 :     res = PQexec(pset.db, query->data);
    5327          10 :     if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
    5328             :     {
    5329          10 :         resetPQExpBuffer(buf);
    5330          10 :         switch (obj_type)
    5331             :         {
    5332           6 :             case EditableFunction:
    5333           6 :                 appendPQExpBufferStr(buf, PQgetvalue(res, 0, 0));
    5334           6 :                 break;
    5335             : 
    5336           4 :             case EditableView:
    5337             :                 {
    5338           4 :                     char       *nspname = PQgetvalue(res, 0, 0);
    5339           4 :                     char       *relname = PQgetvalue(res, 0, 1);
    5340           4 :                     char       *relkind = PQgetvalue(res, 0, 2);
    5341           4 :                     char       *viewdef = PQgetvalue(res, 0, 3);
    5342           4 :                     char       *reloptions = PQgetvalue(res, 0, 4);
    5343           4 :                     char       *checkoption = PQgetvalue(res, 0, 5);
    5344             : 
    5345             :                     /*
    5346             :                      * If the backend ever supports CREATE OR REPLACE
    5347             :                      * MATERIALIZED VIEW, allow that here; but as of today it
    5348             :                      * does not, so editing a matview definition in this way
    5349             :                      * is impossible.
    5350             :                      */
    5351           4 :                     switch (relkind[0])
    5352             :                     {
    5353             : #ifdef NOT_USED
    5354             :                         case RELKIND_MATVIEW:
    5355             :                             appendPQExpBufferStr(buf, "CREATE OR REPLACE MATERIALIZED VIEW ");
    5356             :                             break;
    5357             : #endif
    5358           4 :                         case RELKIND_VIEW:
    5359           4 :                             appendPQExpBufferStr(buf, "CREATE OR REPLACE VIEW ");
    5360           4 :                             break;
    5361           0 :                         default:
    5362           0 :                             pg_log_error("\"%s.%s\" is not a view",
    5363             :                                          nspname, relname);
    5364           0 :                             result = false;
    5365           0 :                             break;
    5366             :                     }
    5367           4 :                     appendPQExpBuffer(buf, "%s.", fmtId(nspname));
    5368           4 :                     appendPQExpBufferStr(buf, fmtId(relname));
    5369             : 
    5370             :                     /* reloptions, if not an empty array "{}" */
    5371           4 :                     if (reloptions != NULL && strlen(reloptions) > 2)
    5372             :                     {
    5373           0 :                         appendPQExpBufferStr(buf, "\n WITH (");
    5374           0 :                         if (!appendReloptionsArray(buf, reloptions, "",
    5375             :                                                    pset.encoding,
    5376           0 :                                                    standard_strings()))
    5377             :                         {
    5378           0 :                             pg_log_error("could not parse reloptions array");
    5379           0 :                             result = false;
    5380             :                         }
    5381           0 :                         appendPQExpBufferChar(buf, ')');
    5382             :                     }
    5383             : 
    5384             :                     /* View definition from pg_get_viewdef (a SELECT query) */
    5385           4 :                     appendPQExpBuffer(buf, " AS\n%s", viewdef);
    5386             : 
    5387             :                     /* Get rid of the semicolon that pg_get_viewdef appends */
    5388           4 :                     if (buf->len > 0 && buf->data[buf->len - 1] == ';')
    5389           4 :                         buf->data[--(buf->len)] = '\0';
    5390             : 
    5391             :                     /* WITH [LOCAL|CASCADED] CHECK OPTION */
    5392           4 :                     if (checkoption && checkoption[0] != '\0')
    5393           0 :                         appendPQExpBuffer(buf, "\n WITH %s CHECK OPTION",
    5394             :                                           checkoption);
    5395             :                 }
    5396           4 :                 break;
    5397             :         }
    5398             :         /* Make sure result ends with a newline */
    5399          10 :         if (buf->len > 0 && buf->data[buf->len - 1] != '\n')
    5400           4 :             appendPQExpBufferChar(buf, '\n');
    5401             :     }
    5402             :     else
    5403             :     {
    5404           0 :         minimal_error_message(res);
    5405           0 :         result = false;
    5406             :     }
    5407             : 
    5408          10 :     PQclear(res);
    5409          10 :     destroyPQExpBuffer(query);
    5410             : 
    5411          10 :     return result;
    5412             : }
    5413             : 
    5414             : /*
    5415             :  * If the given argument of \ef or \ev ends with a line number, delete the line
    5416             :  * number from the argument string and return it as an integer.  (We need
    5417             :  * this kluge because we're too lazy to parse \ef's function or \ev's view
    5418             :  * argument carefully --- we just slop it up in OT_WHOLE_LINE mode.)
    5419             :  *
    5420             :  * Returns -1 if no line number is present, 0 on error, or a positive value
    5421             :  * on success.
    5422             :  */
    5423             : static int
    5424           0 : strip_lineno_from_objdesc(char *obj)
    5425             : {
    5426             :     char       *c;
    5427             :     int         lineno;
    5428             : 
    5429           0 :     if (!obj || obj[0] == '\0')
    5430           0 :         return -1;
    5431             : 
    5432           0 :     c = obj + strlen(obj) - 1;
    5433             : 
    5434             :     /*
    5435             :      * This business of parsing backwards is dangerous as can be in a
    5436             :      * multibyte environment: there is no reason to believe that we are
    5437             :      * looking at the first byte of a character, nor are we necessarily
    5438             :      * working in a "safe" encoding.  Fortunately the bitpatterns we are
    5439             :      * looking for are unlikely to occur as non-first bytes, but beware of
    5440             :      * trying to expand the set of cases that can be recognized.  We must
    5441             :      * guard the <ctype.h> macros by using isascii() first, too.
    5442             :      */
    5443             : 
    5444             :     /* skip trailing whitespace */
    5445           0 :     while (c > obj && isascii((unsigned char) *c) && isspace((unsigned char) *c))
    5446           0 :         c--;
    5447             : 
    5448             :     /* must have a digit as last non-space char */
    5449           0 :     if (c == obj || !isascii((unsigned char) *c) || !isdigit((unsigned char) *c))
    5450           0 :         return -1;
    5451             : 
    5452             :     /* find start of digit string */
    5453           0 :     while (c > obj && isascii((unsigned char) *c) && isdigit((unsigned char) *c))
    5454           0 :         c--;
    5455             : 
    5456             :     /* digits must be separated from object name by space or closing paren */
    5457             :     /* notice also that we are not allowing an empty object name ... */
    5458           0 :     if (c == obj || !isascii((unsigned char) *c) ||
    5459           0 :         !(isspace((unsigned char) *c) || *c == ')'))
    5460           0 :         return -1;
    5461             : 
    5462             :     /* parse digit string */
    5463           0 :     c++;
    5464           0 :     lineno = atoi(c);
    5465           0 :     if (lineno < 1)
    5466             :     {
    5467           0 :         pg_log_error("invalid line number: %s", c);
    5468           0 :         return 0;
    5469             :     }
    5470             : 
    5471             :     /* strip digit string from object name */
    5472           0 :     *c = '\0';
    5473             : 
    5474           0 :     return lineno;
    5475             : }
    5476             : 
    5477             : /*
    5478             :  * Count number of lines in the buffer.
    5479             :  * This is used to test if pager is needed or not.
    5480             :  */
    5481             : static int
    5482          10 : count_lines_in_buf(PQExpBuffer buf)
    5483             : {
    5484          10 :     int         lineno = 0;
    5485          10 :     const char *lines = buf->data;
    5486             : 
    5487         126 :     while (*lines != '\0')
    5488             :     {
    5489         116 :         lineno++;
    5490             :         /* find start of next line */
    5491         116 :         lines = strchr(lines, '\n');
    5492         116 :         if (!lines)
    5493           0 :             break;
    5494         116 :         lines++;
    5495             :     }
    5496             : 
    5497          10 :     return lineno;
    5498             : }
    5499             : 
    5500             : /*
    5501             :  * Write text at *lines to output with line numbers.
    5502             :  *
    5503             :  * If header_keyword isn't NULL, then line 1 should be the first line beginning
    5504             :  * with header_keyword; lines before that are unnumbered.
    5505             :  *
    5506             :  * Caution: this scribbles on *lines.
    5507             :  */
    5508             : static void
    5509           0 : print_with_linenumbers(FILE *output, char *lines,
    5510             :                        const char *header_keyword)
    5511             : {
    5512           0 :     bool        in_header = (header_keyword != NULL);
    5513           0 :     size_t      header_sz = in_header ? strlen(header_keyword) : 0;
    5514           0 :     int         lineno = 0;
    5515             : 
    5516           0 :     while (*lines != '\0')
    5517             :     {
    5518             :         char       *eol;
    5519             : 
    5520           0 :         if (in_header && strncmp(lines, header_keyword, header_sz) == 0)
    5521           0 :             in_header = false;
    5522             : 
    5523             :         /* increment lineno only for body's lines */
    5524           0 :         if (!in_header)
    5525           0 :             lineno++;
    5526             : 
    5527             :         /* find and mark end of current line */
    5528           0 :         eol = strchr(lines, '\n');
    5529           0 :         if (eol != NULL)
    5530           0 :             *eol = '\0';
    5531             : 
    5532             :         /* show current line as appropriate */
    5533           0 :         if (in_header)
    5534           0 :             fprintf(output, "        %s\n", lines);
    5535             :         else
    5536           0 :             fprintf(output, "%-7d %s\n", lineno, lines);
    5537             : 
    5538             :         /* advance to next line, if any */
    5539           0 :         if (eol == NULL)
    5540           0 :             break;
    5541           0 :         lines = ++eol;
    5542             :     }
    5543           0 : }
    5544             : 
    5545             : /*
    5546             :  * Report just the primary error; this is to avoid cluttering the output
    5547             :  * with, for instance, a redisplay of the internally generated query
    5548             :  */
    5549             : static void
    5550           0 : minimal_error_message(PGresult *res)
    5551             : {
    5552             :     PQExpBuffer msg;
    5553             :     const char *fld;
    5554             : 
    5555           0 :     msg = createPQExpBuffer();
    5556             : 
    5557           0 :     fld = PQresultErrorField(res, PG_DIAG_SEVERITY);
    5558           0 :     if (fld)
    5559           0 :         printfPQExpBuffer(msg, "%s:  ", fld);
    5560             :     else
    5561           0 :         printfPQExpBuffer(msg, "ERROR:  ");
    5562           0 :     fld = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
    5563           0 :     if (fld)
    5564           0 :         appendPQExpBufferStr(msg, fld);
    5565             :     else
    5566           0 :         appendPQExpBufferStr(msg, "(not available)");
    5567           0 :     appendPQExpBufferChar(msg, '\n');
    5568             : 
    5569           0 :     pg_log_error("%s", msg->data);
    5570             : 
    5571           0 :     destroyPQExpBuffer(msg);
    5572           0 : }

Generated by: LCOV version 1.14