LCOV - code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18beta1 Lines: 1648 2538 64.9 %
Date: 2025-06-28 02:17:32 Functions: 97 107 90.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.16