LCOV - code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Hit Total Coverage
Test: PostgreSQL 19devel Lines: 1655 2546 65.0 %
Date: 2025-07-17 22:18:38 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       18760 : 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       18760 :     cmd = psql_scan_slash_command(scan_state);
     239             : 
     240             :     /* And try to execute it */
     241       18760 :     status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
     242             : 
     243       18758 :     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       18758 :     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       17564 :         bool        active_branch = conditional_active(cstack);
     259             : 
     260       17564 :         conditional_stack_push(cstack, IFSTATE_IGNORED);
     261       17594 :         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       17564 :         conditional_stack_pop(cstack);
     269             :     }
     270             :     else
     271             :     {
     272             :         /* silently throw away rest of line after an erroneous command */
     273        1212 :         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       18758 :     psql_scan_slash_command_end(scan_state);
     280             : 
     281       18758 :     free(cmd);
     282             : 
     283             :     /* some commands write to queryFout, so make sure output is sent */
     284       18758 :     fflush(pset.queryFout);
     285             : 
     286       18758 :     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       18760 : 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       18760 :     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       18760 :     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       18760 :     if (strcmp(cmd, "a") == 0)
     322          54 :         status = exec_command_a(scan_state, active_branch);
     323       18706 :     else if (strcmp(cmd, "bind") == 0)
     324         660 :         status = exec_command_bind(scan_state, active_branch);
     325       18046 :     else if (strcmp(cmd, "bind_named") == 0)
     326         168 :         status = exec_command_bind_named(scan_state, active_branch, cmd);
     327       17878 :     else if (strcmp(cmd, "C") == 0)
     328           6 :         status = exec_command_C(scan_state, active_branch);
     329       17872 :     else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
     330         354 :         status = exec_command_connect(scan_state, active_branch);
     331       17518 :     else if (strcmp(cmd, "cd") == 0)
     332           6 :         status = exec_command_cd(scan_state, active_branch, cmd);
     333       17512 :     else if (strcmp(cmd, "close_prepared") == 0)
     334          48 :         status = exec_command_close_prepared(scan_state, active_branch, cmd);
     335       17464 :     else if (strcmp(cmd, "conninfo") == 0)
     336           6 :         status = exec_command_conninfo(scan_state, active_branch);
     337       17458 :     else if (pg_strcasecmp(cmd, "copy") == 0)
     338         172 :         status = exec_command_copy(scan_state, active_branch);
     339       17286 :     else if (strcmp(cmd, "copyright") == 0)
     340           8 :         status = exec_command_copyright(scan_state, active_branch);
     341       17278 :     else if (strcmp(cmd, "crosstabview") == 0)
     342         138 :         status = exec_command_crosstabview(scan_state, active_branch);
     343       17140 :     else if (cmd[0] == 'd')
     344        6800 :         status = exec_command_d(scan_state, active_branch, cmd);
     345       10340 :     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       10334 :     else if (strcmp(cmd, "ef") == 0)
     349           6 :         status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
     350       10328 :     else if (strcmp(cmd, "ev") == 0)
     351           6 :         status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
     352       10322 :     else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0 ||
     353        8578 :              strcmp(cmd, "warn") == 0)
     354        2698 :         status = exec_command_echo(scan_state, active_branch, cmd);
     355        7624 :     else if (strcmp(cmd, "elif") == 0)
     356          48 :         status = exec_command_elif(scan_state, cstack, query_buf);
     357        7576 :     else if (strcmp(cmd, "else") == 0)
     358         132 :         status = exec_command_else(scan_state, cstack, query_buf);
     359        7444 :     else if (strcmp(cmd, "endif") == 0)
     360         186 :         status = exec_command_endif(scan_state, cstack, query_buf);
     361        7258 :     else if (strcmp(cmd, "endpipeline") == 0)
     362         296 :         status = exec_command_endpipeline(scan_state, active_branch);
     363        6962 :     else if (strcmp(cmd, "encoding") == 0)
     364          22 :         status = exec_command_encoding(scan_state, active_branch);
     365        6940 :     else if (strcmp(cmd, "errverbose") == 0)
     366          14 :         status = exec_command_errverbose(scan_state, active_branch);
     367        6926 :     else if (strcmp(cmd, "f") == 0)
     368           6 :         status = exec_command_f(scan_state, active_branch);
     369        6920 :     else if (strcmp(cmd, "flush") == 0)
     370          24 :         status = exec_command_flush(scan_state, active_branch);
     371        6896 :     else if (strcmp(cmd, "flushrequest") == 0)
     372          60 :         status = exec_command_flushrequest(scan_state, active_branch);
     373        6836 :     else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
     374         352 :         status = exec_command_g(scan_state, active_branch, cmd);
     375        6484 :     else if (strcmp(cmd, "gdesc") == 0)
     376          86 :         status = exec_command_gdesc(scan_state, active_branch);
     377        6398 :     else if (strcmp(cmd, "getenv") == 0)
     378         298 :         status = exec_command_getenv(scan_state, active_branch, cmd);
     379        6100 :     else if (strcmp(cmd, "getresults") == 0)
     380         156 :         status = exec_command_getresults(scan_state, active_branch);
     381        5944 :     else if (strcmp(cmd, "gexec") == 0)
     382          58 :         status = exec_command_gexec(scan_state, active_branch);
     383        5886 :     else if (strcmp(cmd, "gset") == 0)
     384         778 :         status = exec_command_gset(scan_state, active_branch);
     385        5108 :     else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
     386          10 :         status = exec_command_help(scan_state, active_branch);
     387        5098 :     else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
     388           6 :         status = exec_command_html(scan_state, active_branch);
     389        5092 :     else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
     390        5086 :              strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
     391          12 :         status = exec_command_include(scan_state, active_branch, cmd);
     392        5080 :     else if (strcmp(cmd, "if") == 0)
     393         200 :         status = exec_command_if(scan_state, cstack, query_buf);
     394        4880 :     else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
     395        4874 :              strcmp(cmd, "lx") == 0 || strcmp(cmd, "listx") == 0 ||
     396        4874 :              strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0 ||
     397        4874 :              strcmp(cmd, "lx+") == 0 || strcmp(cmd, "listx+") == 0 ||
     398        4874 :              strcmp(cmd, "l+x") == 0 || strcmp(cmd, "list+x") == 0)
     399           6 :         status = exec_command_list(scan_state, active_branch, cmd);
     400        4874 :     else if (strncmp(cmd, "lo_", 3) == 0)
     401          62 :         status = exec_command_lo(scan_state, active_branch, cmd);
     402        4812 :     else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
     403          42 :         status = exec_command_out(scan_state, active_branch);
     404        4770 :     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        4728 :     else if (strcmp(cmd, "parse") == 0)
     408         108 :         status = exec_command_parse(scan_state, active_branch, cmd);
     409        4620 :     else if (strcmp(cmd, "password") == 0)
     410           8 :         status = exec_command_password(scan_state, active_branch);
     411        4612 :     else if (strcmp(cmd, "prompt") == 0)
     412           6 :         status = exec_command_prompt(scan_state, active_branch, cmd);
     413        4606 :     else if (strcmp(cmd, "pset") == 0)
     414        1740 :         status = exec_command_pset(scan_state, active_branch);
     415        2866 :     else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
     416         248 :         status = exec_command_quit(scan_state, active_branch);
     417        2618 :     else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
     418          96 :         status = exec_command_reset(scan_state, active_branch, query_buf);
     419        2522 :     else if (strcmp(cmd, "s") == 0)
     420           6 :         status = exec_command_s(scan_state, active_branch);
     421        2516 :     else if (strcmp(cmd, "sendpipeline") == 0)
     422         588 :         status = exec_command_sendpipeline(scan_state, active_branch);
     423        1928 :     else if (strcmp(cmd, "set") == 0)
     424         980 :         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       18758 :     if (status == PSQL_CMD_SEND)
     468        2910 :         (void) copy_previous_query(query_buf, previous_buf);
     469             : 
     470       18758 :     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         354 : exec_command_connect(PsqlScanState scan_state, bool active_branch)
     618             : {
     619         354 :     bool        success = true;
     620             : 
     621         354 :     if (active_branch)
     622             :     {
     623             :         static const char prefix[] = "-reuse-previous=";
     624             :         char       *opt1,
     625             :                    *opt2,
     626             :                    *opt3,
     627             :                    *opt4;
     628         348 :         enum trivalue reuse_previous = TRI_DEFAULT;
     629             : 
     630         348 :         opt1 = read_connect_arg(scan_state);
     631         348 :         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         348 :         if (success)            /* give up if reuse_previous was invalid */
     647             :         {
     648         348 :             opt2 = read_connect_arg(scan_state);
     649         348 :             opt3 = read_connect_arg(scan_state);
     650         348 :             opt4 = read_connect_arg(scan_state);
     651             : 
     652         348 :             success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
     653             : 
     654         348 :             free(opt2);
     655         348 :             free(opt3);
     656         348 :             free(opt4);
     657             :         }
     658         348 :         free(opt1);
     659             :     }
     660             :     else
     661           6 :         ignore_slash_options(scan_state);
     662             : 
     663         354 :     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        6800 : exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1001             : {
    1002        6800 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1003        6800 :     bool        success = true;
    1004             : 
    1005        6800 :     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        6794 :         pattern = psql_scan_slash_option(scan_state,
    1014             :                                          OT_NORMAL, NULL, true);
    1015             : 
    1016        6794 :         show_verbose = strchr(cmd, '+') ? true : false;
    1017        6794 :         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        6794 :         save_expanded = pset.popt.topt.expanded;
    1026        6794 :         if (cmd[1] != '\0' && strchr(&cmd[2], 'x'))
    1027          24 :             pset.popt.topt.expanded = 1;
    1028             : 
    1029        6794 :         switch (cmd[1])
    1030             :         {
    1031        4004 :             case '\0':
    1032             :             case '+':
    1033             :             case 'S':
    1034        4004 :                 if (pattern)
    1035        3986 :                     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        4004 :                 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        6794 :         pset.popt.topt.expanded = save_expanded;
    1270             : 
    1271        6794 :         free(pattern);
    1272             :     }
    1273             :     else
    1274           6 :         ignore_slash_options(scan_state);
    1275             : 
    1276        6800 :     if (!success)
    1277         932 :         status = PSQL_CMD_ERROR;
    1278             : 
    1279        6800 :     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        2698 : exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
    1539             : {
    1540        2698 :     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         144 :         ignore_slash_options(scan_state);
    1575             : 
    1576        2698 :     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         778 : exec_command_gset(PsqlScanState scan_state, bool active_branch)
    1968             : {
    1969         778 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    1970             : 
    1971         778 :     if (active_branch)
    1972             :     {
    1973         772 :         char       *prefix = psql_scan_slash_option(scan_state,
    1974             :                                                     OT_NORMAL, NULL, false);
    1975             : 
    1976         772 :         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         760 :         if (prefix)
    1984         108 :             pset.gset_prefix = prefix;
    1985             :         else
    1986             :         {
    1987             :             /* we must set a non-NULL prefix to trigger storing */
    1988         652 :             pset.gset_prefix = pg_strdup("");
    1989             :         }
    1990             :         /* gset_prefix is freed later */
    1991         760 :         status = PSQL_CMD_SEND;
    1992             :     }
    1993             :     else
    1994           6 :         ignore_slash_options(scan_state);
    1995             : 
    1996         766 :     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         200 : exec_command_if(PsqlScanState scan_state, ConditionalStack cstack,
    2084             :                 PQExpBuffer query_buf)
    2085             : {
    2086         200 :     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         194 :         conditional_stack_push(cstack, IFSTATE_TRUE);
    2095             : 
    2096             :         /* Remember current query state in case we need to restore later */
    2097         194 :         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         194 :         if (!is_true_boolean_expression(scan_state, "\\if expression"))
    2103         114 :             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         200 :     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         186 : exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack,
    2271             :                    PQExpBuffer query_buf)
    2272             : {
    2273         186 :     bool        success = true;
    2274             : 
    2275         186 :     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         132 :         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         132 :             discard_query_text(scan_state, cstack, query_buf);
    2291             : 
    2292             :             /* Close the \if block */
    2293         132 :             success = conditional_stack_pop(cstack);
    2294             :             Assert(success);
    2295         132 :             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         186 :     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         248 : exec_command_quit(PsqlScanState scan_state, bool active_branch)
    2730             : {
    2731         248 :     backslashResult status = PSQL_CMD_SKIP_LINE;
    2732             : 
    2733         248 :     if (active_branch)
    2734         182 :         status = PSQL_CMD_TERMINATE;
    2735             : 
    2736         248 :     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         980 : exec_command_set(PsqlScanState scan_state, bool active_branch)
    2826             : {
    2827         980 :     bool        success = true;
    2828             : 
    2829         980 :     if (active_branch)
    2830             :     {
    2831         974 :         char       *opt0 = psql_scan_slash_option(scan_state,
    2832             :                                                   OT_NORMAL, NULL, false);
    2833             : 
    2834         974 :         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         974 :             opt = psql_scan_slash_option(scan_state,
    2849             :                                          OT_NORMAL, NULL, false);
    2850         974 :             newval = pg_strdup(opt ? opt : "");
    2851         974 :             free(opt);
    2852             : 
    2853        1442 :             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         974 :             if (!SetVariable(pset.vars, opt0, newval))
    2862          26 :                 success = false;
    2863             : 
    2864         974 :             free(newval);
    2865             :         }
    2866         974 :         free(opt0);
    2867             :     }
    2868             :     else
    2869           6 :         ignore_slash_options(scan_state);
    2870             : 
    2871         980 :     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        1414 : 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        1414 :     result = psql_scan_slash_option(scan_state, OT_SQLIDHACK, &quote, true);
    3557             : 
    3558        1414 :     if (!result)
    3559        1134 :         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         236 : gather_boolean_expression(PsqlScanState scan_state)
    3583             : {
    3584         236 :     PQExpBuffer exp_buf = createPQExpBuffer();
    3585         236 :     int         num_options = 0;
    3586             :     char       *value;
    3587             : 
    3588             :     /* collect all arguments for the conditional command into exp_buf */
    3589         484 :     while ((value = psql_scan_slash_option(scan_state,
    3590         484 :                                            OT_NORMAL, NULL, false)) != NULL)
    3591             :     {
    3592             :         /* add spaces between tokens */
    3593         248 :         if (num_options > 0)
    3594          12 :             appendPQExpBufferChar(exp_buf, ' ');
    3595         248 :         appendPQExpBufferStr(exp_buf, value);
    3596         248 :         num_options++;
    3597         248 :         free(value);
    3598             :     }
    3599             : 
    3600         236 :     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         218 : is_true_boolean_expression(PsqlScanState scan_state, const char *name)
    3613             : {
    3614         218 :     PQExpBuffer buf = gather_boolean_expression(scan_state);
    3615         218 :     bool        value = false;
    3616         218 :     bool        success = ParseVariableBool(buf->data, name, &value);
    3617             : 
    3618         218 :     destroyPQExpBuffer(buf);
    3619         218 :     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         378 : ignore_slash_options(PsqlScanState scan_state)
    3646             : {
    3647             :     char       *arg;
    3648             : 
    3649         846 :     while ((arg = psql_scan_slash_option(scan_state,
    3650         846 :                                          OT_NORMAL, NULL, false)) != NULL)
    3651         468 :         free(arg);
    3652         378 : }
    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         266 : save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack,
    3711             :                       PQExpBuffer query_buf)
    3712             : {
    3713         266 :     if (query_buf)
    3714         266 :         conditional_stack_set_query_len(cstack, query_buf->len);
    3715         266 :     conditional_stack_set_paren_depth(cstack,
    3716             :                                       psql_scan_get_paren_depth(scan_state));
    3717         266 : }
    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         222 : discard_query_text(PsqlScanState scan_state, ConditionalStack cstack,
    3731             :                    PQExpBuffer query_buf)
    3732             : {
    3733         222 :     if (query_buf)
    3734             :     {
    3735         222 :         int         new_len = conditional_stack_get_query_len(cstack);
    3736             : 
    3737             :         Assert(new_len >= 0 && new_len <= query_buf->len);
    3738         222 :         query_buf->len = new_len;
    3739         222 :         query_buf->data[new_len] = '\0';
    3740             :     }
    3741         222 :     psql_scan_set_paren_depth(scan_state,
    3742             :                               conditional_stack_get_paren_depth(cstack));
    3743         222 : }
    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        2920 : copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
    3757             : {
    3758        2920 :     if (query_buf && query_buf->len == 0)
    3759             :     {
    3760        1162 :         appendPQExpBufferStr(query_buf, previous_buf->data);
    3761        1162 :         return true;
    3762             :     }
    3763        1758 :     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         348 : do_connect(enum trivalue reuse_previous_specification,
    3826             :            char *dbname, char *user, char *host, char *port)
    3827             : {
    3828         348 :     PGconn     *o_conn = pset.db,
    3829         348 :                *n_conn = NULL;
    3830             :     PQconninfoOption *cinfo;
    3831         348 :     int         nconnopts = 0;
    3832         348 :     bool        same_host = false;
    3833         348 :     char       *password = NULL;
    3834             :     char       *client_encoding;
    3835         348 :     bool        success = true;
    3836         348 :     bool        keep_password = true;
    3837             :     bool        has_connection_string;
    3838             :     bool        reuse_previous;
    3839             : 
    3840         348 :     has_connection_string = dbname ?
    3841         348 :         recognized_connection_string(dbname) : false;
    3842             : 
    3843             :     /* Complain if we have additional arguments after a connection string. */
    3844         348 :     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         348 :     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         326 :         default:
    3859         326 :             reuse_previous = !has_connection_string;
    3860         326 :             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         348 :     if (reuse_previous)
    3873             :     {
    3874         348 :         if (o_conn)
    3875         348 :             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         348 :     if (cinfo)
    3889             :     {
    3890         348 :         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        1144 :                      ci->keyword && replci->keyword;
    3905        1122 :                      ci++, replci++)
    3906             :                 {
    3907             :                     Assert(strcmp(ci->keyword, replci->keyword) == 0);
    3908             :                     /* Insert value from connstring if one was provided */
    3909        1122 :                     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        1094 :                     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       16952 :             for (ci = cinfo; ci->keyword; ci++)
    4010             :             {
    4011       16626 :                 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       16618 :                 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       16618 :                 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         326 :             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         348 :     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         348 :     if (pset.notty || getenv("PGCLIENTENCODING"))
    4073         348 :         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         348 :     while (success)
    4079             :     {
    4080         348 :         const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
    4081         348 :         const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
    4082         348 :         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       18096 :         for (ci = cinfo; ci->keyword; ci++)
    4095             :         {
    4096       17748 :             keywords[paramnum] = ci->keyword;
    4097             : 
    4098       17748 :             if (dbname && strcmp(ci->keyword, "dbname") == 0)
    4099          22 :                 values[paramnum++] = dbname;
    4100       17726 :             else if (user && strcmp(ci->keyword, "user") == 0)
    4101           8 :                 values[paramnum++] = user;
    4102       17718 :             else if (host && strcmp(ci->keyword, "host") == 0)
    4103           0 :                 values[paramnum++] = host;
    4104       17718 :             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       17718 :             else if (port && strcmp(ci->keyword, "port") == 0)
    4110           0 :                 values[paramnum++] = port;
    4111             :             /* If !keep_password, we unconditionally drop old password */
    4112       17718 :             else if ((password || !keep_password) &&
    4113         706 :                      strcmp(ci->keyword, "password") == 0)
    4114          14 :                 values[paramnum++] = password;
    4115       17704 :             else if (strcmp(ci->keyword, "fallback_application_name") == 0)
    4116         348 :                 values[paramnum++] = pset.progname;
    4117       17356 :             else if (client_encoding &&
    4118           0 :                      strcmp(ci->keyword, "client_encoding") == 0)
    4119           0 :                 values[paramnum++] = client_encoding;
    4120       17356 :             else if (ci->val)
    4121        6606 :                 values[paramnum++] = ci->val;
    4122             :             /* else, don't bother making libpq parse this keyword */
    4123             :         }
    4124             :         /* add array terminator */
    4125         348 :         keywords[paramnum] = NULL;
    4126         348 :         values[paramnum] = NULL;
    4127             : 
    4128             :         /* Note we do not want libpq to re-expand the dbname parameter */
    4129         348 :         n_conn = PQconnectStartParams(keywords, values, false);
    4130             : 
    4131         348 :         pg_free(keywords);
    4132         348 :         pg_free(values);
    4133             : 
    4134         348 :         wait_until_connected(n_conn);
    4135         348 :         if (PQstatus(n_conn) == CONNECTION_OK)
    4136         348 :             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         348 :     pg_free(password);
    4169         348 :     PQconninfoFree(cinfo);
    4170             : 
    4171         348 :     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         348 :     PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
    4232         348 :     pset.db = n_conn;
    4233         348 :     SyncVariables();
    4234         348 :     connection_warnings(false); /* Must be after SyncVariables */
    4235             : 
    4236             :     /* Tell the user about the new connection */
    4237         348 :     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         348 :     if (o_conn)
    4273         348 :         PQfinish(o_conn);
    4274         348 :     if (pset.dead_conn)
    4275             :     {
    4276           0 :         PQfinish(pset.dead_conn);
    4277           0 :         pset.dead_conn = NULL;
    4278             :     }
    4279             : 
    4280         348 :     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         348 : wait_until_connected(PGconn *conn)
    4290             : {
    4291         348 :     bool        forRead = false;
    4292             : 
    4293             :     while (true)
    4294         360 :     {
    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         708 :         if (cancel_pressed)
    4304           0 :             break;
    4305             : 
    4306             :         /*
    4307             :          * Do not assume that the socket remains the same across
    4308             :          * PQconnectPoll() calls.
    4309             :          */
    4310         708 :         sock = PQsocket(conn);
    4311         708 :         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         708 :         end_time = PQgetCurrentTimeUSec() + 1000000;
    4329         708 :         rc = PQsocketPoll(sock, forRead, !forRead, end_time);
    4330         708 :         if (rc == -1)
    4331           0 :             return;
    4332             : 
    4333         708 :         switch (PQconnectPoll(conn))
    4334             :         {
    4335         348 :             case PGRES_POLLING_OK:
    4336             :             case PGRES_POLLING_FAILED:
    4337         348 :                 return;
    4338         360 :             case PGRES_POLLING_READING:
    4339         360 :                 forRead = true;
    4340         360 :                 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         352 : connection_warnings(bool in_startup)
    4352             : {
    4353         352 :     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         352 : }
    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       18018 : SyncVariables(void)
    4480             : {
    4481             :     char        vbuf[32];
    4482             :     const char *server_version;
    4483             :     char       *service_name;
    4484             :     char       *service_file;
    4485             : 
    4486             :     /* get stuff from connection */
    4487       18018 :     pset.encoding = PQclientEncoding(pset.db);
    4488       18018 :     pset.popt.topt.encoding = pset.encoding;
    4489       18018 :     pset.sversion = PQserverVersion(pset.db);
    4490             : 
    4491       18018 :     setFmtEncoding(pset.encoding);
    4492             : 
    4493       18018 :     SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
    4494       18018 :     SetVariable(pset.vars, "USER", PQuser(pset.db));
    4495       18018 :     SetVariable(pset.vars, "HOST", PQhost(pset.db));
    4496       18018 :     SetVariable(pset.vars, "PORT", PQport(pset.db));
    4497       18018 :     SetVariable(pset.vars, "ENCODING", pg_encoding_to_char(pset.encoding));
    4498             : 
    4499       18018 :     service_name = get_conninfo_value("service");
    4500       18018 :     SetVariable(pset.vars, "SERVICE", service_name);
    4501       18018 :     if (service_name)
    4502          22 :         pg_free(service_name);
    4503             : 
    4504       18018 :     service_file = get_conninfo_value("servicefile");
    4505       18018 :     SetVariable(pset.vars, "SERVICEFILE", service_file);
    4506       18018 :     if (service_file)
    4507          22 :         pg_free(service_file);
    4508             : 
    4509             :     /* this bit should match connection_warnings(): */
    4510             :     /* Try to get full text form of version, might include "devel" etc */
    4511       18018 :     server_version = PQparameterStatus(pset.db, "server_version");
    4512             :     /* Otherwise fall back on pset.sversion */
    4513       18018 :     if (!server_version)
    4514             :     {
    4515           0 :         formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
    4516           0 :         server_version = vbuf;
    4517             :     }
    4518       18018 :     SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
    4519             : 
    4520       18018 :     snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
    4521       18018 :     SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
    4522             : 
    4523             :     /* send stuff to it, too */
    4524       18018 :     PQsetErrorVerbosity(pset.db, pset.verbosity);
    4525       18018 :     PQsetErrorContextVisibility(pset.db, pset.show_context);
    4526       18018 : }
    4527             : 
    4528             : /*
    4529             :  * UnsyncVariables
    4530             :  *
    4531             :  * Clear variables that should be not be set when there is no connection.
    4532             :  */
    4533             : void
    4534           0 : UnsyncVariables(void)
    4535             : {
    4536           0 :     SetVariable(pset.vars, "DBNAME", NULL);
    4537           0 :     SetVariable(pset.vars, "SERVICE", NULL);
    4538           0 :     SetVariable(pset.vars, "SERVICEFILE", NULL);
    4539           0 :     SetVariable(pset.vars, "USER", NULL);
    4540           0 :     SetVariable(pset.vars, "HOST", NULL);
    4541           0 :     SetVariable(pset.vars, "PORT", NULL);
    4542           0 :     SetVariable(pset.vars, "ENCODING", NULL);
    4543           0 :     SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
    4544           0 :     SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
    4545           0 : }
    4546             : 
    4547             : 
    4548             : /*
    4549             :  * helper for do_edit(): actually invoke the editor
    4550             :  *
    4551             :  * Returns true on success, false if we failed to invoke the editor or
    4552             :  * it returned nonzero status.  (An error message is printed for failed-
    4553             :  * to-invoke cases, but not if the editor returns nonzero status.)
    4554             :  */
    4555             : static bool
    4556           0 : editFile(const char *fname, int lineno)
    4557             : {
    4558             :     const char *editorName;
    4559           0 :     const char *editor_lineno_arg = NULL;
    4560             :     char       *sys;
    4561             :     int         result;
    4562             : 
    4563             :     Assert(fname != NULL);
    4564             : 
    4565             :     /* Find an editor to use */
    4566           0 :     editorName = getenv("PSQL_EDITOR");
    4567           0 :     if (!editorName)
    4568           0 :         editorName = getenv("EDITOR");
    4569           0 :     if (!editorName)
    4570           0 :         editorName = getenv("VISUAL");
    4571           0 :     if (!editorName)
    4572           0 :         editorName = DEFAULT_EDITOR;
    4573             : 
    4574             :     /* Get line number argument, if we need it. */
    4575           0 :     if (lineno > 0)
    4576             :     {
    4577           0 :         editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
    4578             : #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
    4579           0 :         if (!editor_lineno_arg)
    4580           0 :             editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
    4581             : #endif
    4582           0 :         if (!editor_lineno_arg)
    4583             :         {
    4584           0 :             pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
    4585           0 :             return false;
    4586             :         }
    4587             :     }
    4588             : 
    4589             :     /*
    4590             :      * On Unix the EDITOR value should *not* be quoted, since it might include
    4591             :      * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
    4592             :      * if necessary.  But this policy is not very workable on Windows, due to
    4593             :      * severe brain damage in their command shell plus the fact that standard
    4594             :      * program paths include spaces.
    4595             :      */
    4596             : #ifndef WIN32
    4597           0 :     if (lineno > 0)
    4598           0 :         sys = psprintf("exec %s %s%d '%s'",
    4599             :                        editorName, editor_lineno_arg, lineno, fname);
    4600             :     else
    4601           0 :         sys = psprintf("exec %s '%s'",
    4602             :                        editorName, fname);
    4603             : #else
    4604             :     if (lineno > 0)
    4605             :         sys = psprintf("\"%s\" %s%d \"%s\"",
    4606             :                        editorName, editor_lineno_arg, lineno, fname);
    4607             :     else
    4608             :         sys = psprintf("\"%s\" \"%s\"",
    4609             :                        editorName, fname);
    4610             : #endif
    4611           0 :     fflush(NULL);
    4612           0 :     result = system(sys);
    4613           0 :     if (result == -1)
    4614           0 :         pg_log_error("could not start editor \"%s\"", editorName);
    4615           0 :     else if (result == 127)
    4616           0 :         pg_log_error("could not start /bin/sh");
    4617           0 :     free(sys);
    4618             : 
    4619           0 :     return result == 0;
    4620             : }
    4621             : 
    4622             : 
    4623             : /*
    4624             :  * do_edit -- handler for \e
    4625             :  *
    4626             :  * If you do not specify a filename, the current query buffer will be copied
    4627             :  * into a temporary file.
    4628             :  *
    4629             :  * After this function is done, the resulting file will be copied back into the
    4630             :  * query buffer.  As an exception to this, the query buffer will be emptied
    4631             :  * if the file was not modified (or the editor failed) and the caller passes
    4632             :  * "discard_on_quit" = true.
    4633             :  *
    4634             :  * If "edited" isn't NULL, *edited will be set to true if the query buffer
    4635             :  * is successfully replaced.
    4636             :  */
    4637             : static bool
    4638           0 : do_edit(const char *filename_arg, PQExpBuffer query_buf,
    4639             :         int lineno, bool discard_on_quit, bool *edited)
    4640             : {
    4641             :     char        fnametmp[MAXPGPATH];
    4642           0 :     FILE       *stream = NULL;
    4643             :     const char *fname;
    4644           0 :     bool        error = false;
    4645             :     int         fd;
    4646             :     struct stat before,
    4647             :                 after;
    4648             : 
    4649           0 :     if (filename_arg)
    4650           0 :         fname = filename_arg;
    4651             :     else
    4652             :     {
    4653             :         /* make a temp file to edit */
    4654             : #ifndef WIN32
    4655           0 :         const char *tmpdir = getenv("TMPDIR");
    4656             : 
    4657           0 :         if (!tmpdir)
    4658           0 :             tmpdir = "/tmp";
    4659             : #else
    4660             :         char        tmpdir[MAXPGPATH];
    4661             :         int         ret;
    4662             : 
    4663             :         ret = GetTempPath(MAXPGPATH, tmpdir);
    4664             :         if (ret == 0 || ret > MAXPGPATH)
    4665             :         {
    4666             :             pg_log_error("could not locate temporary directory: %s",
    4667             :                          !ret ? strerror(errno) : "");
    4668             :             return false;
    4669             :         }
    4670             : #endif
    4671             : 
    4672             :         /*
    4673             :          * No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
    4674             :          * current directory to the supplied path unless we use only
    4675             :          * backslashes, so we do that.
    4676             :          */
    4677             : #ifndef WIN32
    4678           0 :         snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
    4679           0 :                  "/", (int) getpid());
    4680             : #else
    4681             :         snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
    4682             :                  "" /* trailing separator already present */ , (int) getpid());
    4683             : #endif
    4684             : 
    4685           0 :         fname = (const char *) fnametmp;
    4686             : 
    4687           0 :         fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
    4688           0 :         if (fd != -1)
    4689           0 :             stream = fdopen(fd, "w");
    4690             : 
    4691           0 :         if (fd == -1 || !stream)
    4692             :         {
    4693           0 :             pg_log_error("could not open temporary file \"%s\": %m", fname);
    4694           0 :             error = true;
    4695             :         }
    4696             :         else
    4697             :         {
    4698           0 :             unsigned int ql = query_buf->len;
    4699             : 
    4700             :             /* force newline-termination of what we send to editor */
    4701           0 :             if (ql > 0 && query_buf->data[ql - 1] != '\n')
    4702             :             {
    4703           0 :                 appendPQExpBufferChar(query_buf, '\n');
    4704           0 :                 ql++;
    4705             :             }
    4706             : 
    4707           0 :             if (fwrite(query_buf->data, 1, ql, stream) != ql)
    4708             :             {
    4709           0 :                 pg_log_error("%s: %m", fname);
    4710             : 
    4711           0 :                 if (fclose(stream) != 0)
    4712           0 :                     pg_log_error("%s: %m", fname);
    4713             : 
    4714           0 :                 if (remove(fname) != 0)
    4715           0 :                     pg_log_error("%s: %m", fname);
    4716             : 
    4717           0 :                 error = true;
    4718             :             }
    4719           0 :             else if (fclose(stream) != 0)
    4720             :             {
    4721           0 :                 pg_log_error("%s: %m", fname);
    4722           0 :                 if (remove(fname) != 0)
    4723           0 :                     pg_log_error("%s: %m", fname);
    4724           0 :                 error = true;
    4725             :             }
    4726             :             else
    4727             :             {
    4728             :                 struct utimbuf ut;
    4729             : 
    4730             :                 /*
    4731             :                  * Try to set the file modification time of the temporary file
    4732             :                  * a few seconds in the past.  Otherwise, the low granularity
    4733             :                  * (one second, or even worse on some filesystems) that we can
    4734             :                  * portably measure with stat(2) could lead us to not
    4735             :                  * recognize a modification, if the user typed very quickly.
    4736             :                  *
    4737             :                  * This is a rather unlikely race condition, so don't error
    4738             :                  * out if the utime(2) call fails --- that would make the cure
    4739             :                  * worse than the disease.
    4740             :                  */
    4741           0 :                 ut.modtime = ut.actime = time(NULL) - 2;
    4742           0 :                 (void) utime(fname, &ut);
    4743             :             }
    4744             :         }
    4745             :     }
    4746             : 
    4747           0 :     if (!error && stat(fname, &before) != 0)
    4748             :     {
    4749           0 :         pg_log_error("%s: %m", fname);
    4750           0 :         error = true;
    4751             :     }
    4752             : 
    4753             :     /* call editor */
    4754           0 :     if (!error)
    4755           0 :         error = !editFile(fname, lineno);
    4756             : 
    4757           0 :     if (!error && stat(fname, &after) != 0)
    4758             :     {
    4759           0 :         pg_log_error("%s: %m", fname);
    4760           0 :         error = true;
    4761             :     }
    4762             : 
    4763             :     /* file was edited if the size or modification time has changed */
    4764           0 :     if (!error &&
    4765           0 :         (before.st_size != after.st_size ||
    4766           0 :          before.st_mtime != after.st_mtime))
    4767             :     {
    4768           0 :         stream = fopen(fname, PG_BINARY_R);
    4769           0 :         if (!stream)
    4770             :         {
    4771           0 :             pg_log_error("%s: %m", fname);
    4772           0 :             error = true;
    4773             :         }
    4774             :         else
    4775             :         {
    4776             :             /* read file back into query_buf */
    4777             :             char        line[1024];
    4778             : 
    4779           0 :             resetPQExpBuffer(query_buf);
    4780           0 :             while (fgets(line, sizeof(line), stream) != NULL)
    4781           0 :                 appendPQExpBufferStr(query_buf, line);
    4782             : 
    4783           0 :             if (ferror(stream))
    4784             :             {
    4785           0 :                 pg_log_error("%s: %m", fname);
    4786           0 :                 error = true;
    4787           0 :                 resetPQExpBuffer(query_buf);
    4788             :             }
    4789           0 :             else if (edited)
    4790             :             {
    4791           0 :                 *edited = true;
    4792             :             }
    4793             : 
    4794           0 :             fclose(stream);
    4795             :         }
    4796             :     }
    4797             :     else
    4798             :     {
    4799             :         /*
    4800             :          * If the file was not modified, and the caller requested it, discard
    4801             :          * the query buffer.
    4802             :          */
    4803           0 :         if (discard_on_quit)
    4804           0 :             resetPQExpBuffer(query_buf);
    4805             :     }
    4806             : 
    4807             :     /* remove temp file */
    4808           0 :     if (!filename_arg)
    4809             :     {
    4810           0 :         if (remove(fname) == -1)
    4811             :         {
    4812           0 :             pg_log_error("%s: %m", fname);
    4813           0 :             error = true;
    4814             :         }
    4815             :     }
    4816             : 
    4817           0 :     return !error;
    4818             : }
    4819             : 
    4820             : 
    4821             : 
    4822             : /*
    4823             :  * process_file
    4824             :  *
    4825             :  * Reads commands from filename and passes them to the main processing loop.
    4826             :  * Handler for \i and \ir, but can be used for other things as well.  Returns
    4827             :  * MainLoop() error code.
    4828             :  *
    4829             :  * If use_relative_path is true and filename is not an absolute path, then open
    4830             :  * the file from where the currently processed file (if any) is located.
    4831             :  */
    4832             : int
    4833       17158 : process_file(char *filename, bool use_relative_path)
    4834             : {
    4835             :     FILE       *fd;
    4836             :     int         result;
    4837             :     char       *oldfilename;
    4838             :     char        relpath[MAXPGPATH];
    4839             : 
    4840       17158 :     if (!filename)
    4841             :     {
    4842        4958 :         fd = stdin;
    4843        4958 :         filename = NULL;
    4844             :     }
    4845       12200 :     else if (strcmp(filename, "-") != 0)
    4846             :     {
    4847          40 :         canonicalize_path_enc(filename, pset.encoding);
    4848             : 
    4849             :         /*
    4850             :          * If we were asked to resolve the pathname relative to the location
    4851             :          * of the currently executing script, and there is one, and this is a
    4852             :          * relative pathname, then prepend all but the last pathname component
    4853             :          * of the current script to this pathname.
    4854             :          */
    4855          40 :         if (use_relative_path && pset.inputfile &&
    4856           0 :             !is_absolute_path(filename) && !has_drive_prefix(filename))
    4857             :         {
    4858           0 :             strlcpy(relpath, pset.inputfile, sizeof(relpath));
    4859           0 :             get_parent_directory(relpath);
    4860           0 :             join_path_components(relpath, relpath, filename);
    4861           0 :             canonicalize_path_enc(relpath, pset.encoding);
    4862             : 
    4863           0 :             filename = relpath;
    4864             :         }
    4865             : 
    4866          40 :         fd = fopen(filename, PG_BINARY_R);
    4867             : 
    4868          40 :         if (!fd)
    4869             :         {
    4870           0 :             pg_log_error("%s: %m", filename);
    4871           0 :             return EXIT_FAILURE;
    4872             :         }
    4873             :     }
    4874             :     else
    4875             :     {
    4876       12160 :         fd = stdin;
    4877       12160 :         filename = "<stdin>";   /* for future error messages */
    4878             :     }
    4879             : 
    4880       17158 :     oldfilename = pset.inputfile;
    4881       17158 :     pset.inputfile = filename;
    4882             : 
    4883       17158 :     pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE);
    4884             : 
    4885       17158 :     result = MainLoop(fd);
    4886             : 
    4887       17130 :     if (fd != stdin)
    4888          40 :         fclose(fd);
    4889             : 
    4890       17130 :     pset.inputfile = oldfilename;
    4891             : 
    4892       17130 :     pg_logging_config(pset.inputfile ? 0 : PG_LOG_FLAG_TERSE);
    4893             : 
    4894       17130 :     return result;
    4895             : }
    4896             : 
    4897             : 
    4898             : 
    4899             : static const char *
    4900           6 : _align2string(enum printFormat in)
    4901             : {
    4902           6 :     switch (in)
    4903             :     {
    4904           0 :         case PRINT_NOTHING:
    4905           0 :             return "nothing";
    4906             :             break;
    4907           6 :         case PRINT_ALIGNED:
    4908           6 :             return "aligned";
    4909             :             break;
    4910           0 :         case PRINT_ASCIIDOC:
    4911           0 :             return "asciidoc";
    4912             :             break;
    4913           0 :         case PRINT_CSV:
    4914           0 :             return "csv";
    4915             :             break;
    4916           0 :         case PRINT_HTML:
    4917           0 :             return "html";
    4918             :             break;
    4919           0 :         case PRINT_LATEX:
    4920           0 :             return "latex";
    4921             :             break;
    4922           0 :         case PRINT_LATEX_LONGTABLE:
    4923           0 :             return "latex-longtable";
    4924             :             break;
    4925           0 :         case PRINT_TROFF_MS:
    4926           0 :             return "troff-ms";
    4927             :             break;
    4928           0 :         case PRINT_UNALIGNED:
    4929           0 :             return "unaligned";
    4930             :             break;
    4931           0 :         case PRINT_WRAPPED:
    4932           0 :             return "wrapped";
    4933             :             break;
    4934             :     }
    4935           0 :     return "unknown";
    4936             : }
    4937             : 
    4938             : /*
    4939             :  * Parse entered Unicode linestyle.  If ok, update *linestyle and return
    4940             :  * true, else return false.
    4941             :  */
    4942             : static bool
    4943           0 : set_unicode_line_style(const char *value, size_t vallen,
    4944             :                        unicode_linestyle *linestyle)
    4945             : {
    4946           0 :     if (pg_strncasecmp("single", value, vallen) == 0)
    4947           0 :         *linestyle = UNICODE_LINESTYLE_SINGLE;
    4948           0 :     else if (pg_strncasecmp("double", value, vallen) == 0)
    4949           0 :         *linestyle = UNICODE_LINESTYLE_DOUBLE;
    4950             :     else
    4951           0 :         return false;
    4952           0 :     return true;
    4953             : }
    4954             : 
    4955             : static const char *
    4956          18 : _unicode_linestyle2string(int linestyle)
    4957             : {
    4958          18 :     switch (linestyle)
    4959             :     {
    4960          18 :         case UNICODE_LINESTYLE_SINGLE:
    4961          18 :             return "single";
    4962             :             break;
    4963           0 :         case UNICODE_LINESTYLE_DOUBLE:
    4964           0 :             return "double";
    4965             :             break;
    4966             :     }
    4967           0 :     return "unknown";
    4968             : }
    4969             : 
    4970             : /*
    4971             :  * do_pset
    4972             :  *
    4973             :  * Performs the assignment "param = value", where value could be NULL;
    4974             :  * for some params that has an effect such as inversion, for others
    4975             :  * it does nothing.
    4976             :  *
    4977             :  * Adjusts the state of the formatting options at *popt.  (In practice that
    4978             :  * is always pset.popt, but maybe someday it could be different.)
    4979             :  *
    4980             :  * If successful and quiet is false, then invokes printPsetInfo() to report
    4981             :  * the change.
    4982             :  *
    4983             :  * Returns true if successful, else false (eg for invalid param or value).
    4984             :  */
    4985             : bool
    4986        1950 : do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
    4987             : {
    4988        1950 :     size_t      vallen = 0;
    4989             : 
    4990             :     Assert(param != NULL);
    4991             : 
    4992        1950 :     if (value)
    4993        1820 :         vallen = strlen(value);
    4994             : 
    4995             :     /* set format */
    4996        1950 :     if (strcmp(param, "format") == 0)
    4997             :     {
    4998             :         static const struct fmt
    4999             :         {
    5000             :             const char *name;
    5001             :             enum printFormat number;
    5002             :         }           formats[] =
    5003             :         {
    5004             :             /* remember to update error message below when adding more */
    5005             :             {"aligned", PRINT_ALIGNED},
    5006             :             {"asciidoc", PRINT_ASCIIDOC},
    5007             :             {"csv", PRINT_CSV},
    5008             :             {"html", PRINT_HTML},
    5009             :             {"latex", PRINT_LATEX},
    5010             :             {"troff-ms", PRINT_TROFF_MS},
    5011             :             {"unaligned", PRINT_UNALIGNED},
    5012             :             {"wrapped", PRINT_WRAPPED}
    5013             :         };
    5014             : 
    5015         638 :         if (!value)
    5016             :             ;
    5017             :         else
    5018             :         {
    5019         638 :             int         match_pos = -1;
    5020             : 
    5021        5700 :             for (int i = 0; i < lengthof(formats); i++)
    5022             :             {
    5023        5068 :                 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
    5024             :                 {
    5025         638 :                     if (match_pos < 0)
    5026         632 :                         match_pos = i;
    5027             :                     else
    5028             :                     {
    5029           6 :                         pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
    5030             :                                      value,
    5031             :                                      formats[match_pos].name, formats[i].name);
    5032           6 :                         return false;
    5033             :                     }
    5034             :                 }
    5035             :             }
    5036         632 :             if (match_pos >= 0)
    5037         626 :                 popt->topt.format = formats[match_pos].number;
    5038           6 :             else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
    5039             :             {
    5040             :                 /*
    5041             :                  * We must treat latex-longtable specially because latex is a
    5042             :                  * prefix of it; if both were in the table above, we'd think
    5043             :                  * "latex" is ambiguous.
    5044             :                  */
    5045           6 :                 popt->topt.format = PRINT_LATEX_LONGTABLE;
    5046             :             }
    5047             :             else
    5048             :             {
    5049           0 :                 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
    5050           0 :                 return false;
    5051             :             }
    5052             :         }
    5053             :     }
    5054             : 
    5055             :     /* set table line style */
    5056        1312 :     else if (strcmp(param, "linestyle") == 0)
    5057             :     {
    5058          30 :         if (!value)
    5059             :             ;
    5060          30 :         else if (pg_strncasecmp("ascii", value, vallen) == 0)
    5061          18 :             popt->topt.line_style = &pg_asciiformat;
    5062          12 :         else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
    5063          12 :             popt->topt.line_style = &pg_asciiformat_old;
    5064           0 :         else if (pg_strncasecmp("unicode", value, vallen) == 0)
    5065           0 :             popt->topt.line_style = &pg_utf8format;
    5066             :         else
    5067             :         {
    5068           0 :             pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
    5069           0 :             return false;
    5070             :         }
    5071             :     }
    5072             : 
    5073             :     /* set unicode border line style */
    5074        1282 :     else if (strcmp(param, "unicode_border_linestyle") == 0)
    5075             :     {
    5076           0 :         if (!value)
    5077             :             ;
    5078           0 :         else if (set_unicode_line_style(value, vallen,
    5079             :                                         &popt->topt.unicode_border_linestyle))
    5080           0 :             refresh_utf8format(&(popt->topt));
    5081             :         else
    5082             :         {
    5083           0 :             pg_log_error("\\pset: allowed Unicode border line styles are single, double");
    5084           0 :             return false;
    5085             :         }
    5086             :     }
    5087             : 
    5088             :     /* set unicode column line style */
    5089        1282 :     else if (strcmp(param, "unicode_column_linestyle") == 0)
    5090             :     {
    5091           0 :         if (!value)
    5092             :             ;
    5093           0 :         else if (set_unicode_line_style(value, vallen,
    5094             :                                         &popt->topt.unicode_column_linestyle))
    5095           0 :             refresh_utf8format(&(popt->topt));
    5096             :         else
    5097             :         {
    5098           0 :             pg_log_error("\\pset: allowed Unicode column line styles are single, double");
    5099           0 :             return false;
    5100             :         }
    5101             :     }
    5102             : 
    5103             :     /* set unicode header line style */
    5104        1282 :     else if (strcmp(param, "unicode_header_linestyle") == 0)
    5105             :     {
    5106           0 :         if (!value)
    5107             :             ;
    5108           0 :         else if (set_unicode_line_style(value, vallen,
    5109             :                                         &popt->topt.unicode_header_linestyle))
    5110           0 :             refresh_utf8format(&(popt->topt));
    5111             :         else
    5112             :         {
    5113           0 :             pg_log_error("\\pset: allowed Unicode header line styles are single, double");
    5114           0 :             return false;
    5115             :         }
    5116             :     }
    5117             : 
    5118             :     /* set border style/width */
    5119        1282 :     else if (strcmp(param, "border") == 0)
    5120             :     {
    5121         402 :         if (value)
    5122         402 :             popt->topt.border = atoi(value);
    5123             :     }
    5124             : 
    5125             :     /* set expanded/vertical mode */
    5126         880 :     else if (strcmp(param, "x") == 0 ||
    5127         880 :              strcmp(param, "expanded") == 0 ||
    5128         550 :              strcmp(param, "vertical") == 0)
    5129             :     {
    5130         330 :         if (value && pg_strcasecmp(value, "auto") == 0)
    5131           0 :             popt->topt.expanded = 2;
    5132         330 :         else if (value)
    5133             :         {
    5134             :             bool        on_off;
    5135             : 
    5136         260 :             if (ParseVariableBool(value, NULL, &on_off))
    5137         260 :                 popt->topt.expanded = on_off ? 1 : 0;
    5138             :             else
    5139             :             {
    5140           0 :                 PsqlVarEnumError(param, value, "on, off, auto");
    5141           0 :                 return false;
    5142             :             }
    5143             :         }
    5144             :         else
    5145          70 :             popt->topt.expanded = !popt->topt.expanded;
    5146             :     }
    5147             : 
    5148             :     /* header line width in expanded mode */
    5149         550 :     else if (strcmp(param, "xheader_width") == 0)
    5150             :     {
    5151           0 :         if (!value)
    5152             :             ;
    5153           0 :         else if (pg_strcasecmp(value, "full") == 0)
    5154           0 :             popt->topt.expanded_header_width_type = PRINT_XHEADER_FULL;
    5155           0 :         else if (pg_strcasecmp(value, "column") == 0)
    5156           0 :             popt->topt.expanded_header_width_type = PRINT_XHEADER_COLUMN;
    5157           0 :         else if (pg_strcasecmp(value, "page") == 0)
    5158           0 :             popt->topt.expanded_header_width_type = PRINT_XHEADER_PAGE;
    5159             :         else
    5160             :         {
    5161           0 :             int         intval = atoi(value);
    5162             : 
    5163           0 :             if (intval == 0)
    5164             :             {
    5165           0 :                 pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
    5166           0 :                 return false;
    5167             :             }
    5168             : 
    5169           0 :             popt->topt.expanded_header_width_type = PRINT_XHEADER_EXACT_WIDTH;
    5170           0 :             popt->topt.expanded_header_exact_width = intval;
    5171             :         }
    5172             :     }
    5173             : 
    5174             :     /* field separator for CSV format */
    5175         550 :     else if (strcmp(param, "csv_fieldsep") == 0)
    5176             :     {
    5177          60 :         if (value)
    5178             :         {
    5179             :             /* CSV separator has to be a one-byte character */
    5180          60 :             if (strlen(value) != 1)
    5181             :             {
    5182          18 :                 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
    5183          18 :                 return false;
    5184             :             }
    5185          42 :             if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
    5186             :             {
    5187          18 :                 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
    5188          18 :                 return false;
    5189             :             }
    5190          24 :             popt->topt.csvFieldSep[0] = value[0];
    5191             :         }
    5192             :     }
    5193             : 
    5194             :     /* locale-aware numeric output */
    5195         490 :     else if (strcmp(param, "numericlocale") == 0)
    5196             :     {
    5197          12 :         if (value)
    5198          12 :             return ParseVariableBool(value, param, &popt->topt.numericLocale);
    5199             :         else
    5200           0 :             popt->topt.numericLocale = !popt->topt.numericLocale;
    5201             :     }
    5202             : 
    5203             :     /* null display */
    5204         478 :     else if (strcmp(param, "null") == 0)
    5205             :     {
    5206          78 :         if (value)
    5207             :         {
    5208          78 :             free(popt->nullPrint);
    5209          78 :             popt->nullPrint = pg_strdup(value);
    5210             :         }
    5211             :     }
    5212             : 
    5213             :     /* field separator for unaligned text */
    5214         400 :     else if (strcmp(param, "fieldsep") == 0)
    5215             :     {
    5216           6 :         if (value)
    5217             :         {
    5218           6 :             free(popt->topt.fieldSep.separator);
    5219           6 :             popt->topt.fieldSep.separator = pg_strdup(value);
    5220           6 :             popt->topt.fieldSep.separator_zero = false;
    5221             :         }
    5222             :     }
    5223             : 
    5224         394 :     else if (strcmp(param, "fieldsep_zero") == 0)
    5225             :     {
    5226           0 :         free(popt->topt.fieldSep.separator);
    5227           0 :         popt->topt.fieldSep.separator = NULL;
    5228           0 :         popt->topt.fieldSep.separator_zero = true;
    5229             :     }
    5230             : 
    5231             :     /* record separator for unaligned text */
    5232         394 :     else if (strcmp(param, "recordsep") == 0)
    5233             :     {
    5234           0 :         if (value)
    5235             :         {
    5236           0 :             free(popt->topt.recordSep.separator);
    5237           0 :             popt->topt.recordSep.separator = pg_strdup(value);
    5238           0 :             popt->topt.recordSep.separator_zero = false;
    5239             :         }
    5240             :     }
    5241             : 
    5242         394 :     else if (strcmp(param, "recordsep_zero") == 0)
    5243             :     {
    5244           0 :         free(popt->topt.recordSep.separator);
    5245           0 :         popt->topt.recordSep.separator = NULL;
    5246           0 :         popt->topt.recordSep.separator_zero = true;
    5247             :     }
    5248             : 
    5249             :     /* toggle between full and tuples-only format */
    5250         394 :     else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
    5251             :     {
    5252         292 :         if (value)
    5253         256 :             return ParseVariableBool(value, param, &popt->topt.tuples_only);
    5254             :         else
    5255          36 :             popt->topt.tuples_only = !popt->topt.tuples_only;
    5256             :     }
    5257             : 
    5258             :     /* set title override */
    5259         102 :     else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
    5260             :     {
    5261           6 :         free(popt->title);
    5262           6 :         if (!value)
    5263           0 :             popt->title = NULL;
    5264             :         else
    5265           6 :             popt->title = pg_strdup(value);
    5266             :     }
    5267             : 
    5268             :     /* set HTML table tag options */
    5269          96 :     else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
    5270             :     {
    5271          48 :         free(popt->topt.tableAttr);
    5272          48 :         if (!value)
    5273          24 :             popt->topt.tableAttr = NULL;
    5274             :         else
    5275          24 :             popt->topt.tableAttr = pg_strdup(value);
    5276             :     }
    5277             : 
    5278             :     /* toggle use of pager */
    5279          48 :     else if (strcmp(param, "pager") == 0)
    5280             :     {
    5281           0 :         if (value && pg_strcasecmp(value, "always") == 0)
    5282           0 :             popt->topt.pager = 2;
    5283           0 :         else if (value)
    5284             :         {
    5285             :             bool        on_off;
    5286             : 
    5287           0 :             if (!ParseVariableBool(value, NULL, &on_off))
    5288             :             {
    5289           0 :                 PsqlVarEnumError(param, value, "on, off, always");
    5290           0 :                 return false;
    5291             :             }
    5292           0 :             popt->topt.pager = on_off ? 1 : 0;
    5293             :         }
    5294           0 :         else if (popt->topt.pager == 1)
    5295           0 :             popt->topt.pager = 0;
    5296             :         else
    5297           0 :             popt->topt.pager = 1;
    5298             :     }
    5299             : 
    5300             :     /* set minimum lines for pager use */
    5301          48 :     else if (strcmp(param, "pager_min_lines") == 0)
    5302             :     {
    5303           0 :         if (value &&
    5304           0 :             !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
    5305           0 :             return false;
    5306             :     }
    5307             : 
    5308             :     /* disable "(x rows)" footer */
    5309          48 :     else if (strcmp(param, "footer") == 0)
    5310             :     {
    5311           0 :         if (value)
    5312           0 :             return ParseVariableBool(value, param, &popt->topt.default_footer);
    5313             :         else
    5314           0 :             popt->topt.default_footer = !popt->topt.default_footer;
    5315             :     }
    5316             : 
    5317             :     /* set border style/width */
    5318          48 :     else if (strcmp(param, "columns") == 0)
    5319             :     {
    5320          48 :         if (value)
    5321          48 :             popt->topt.columns = atoi(value);
    5322             :     }
    5323             :     else
    5324             :     {
    5325           0 :         pg_log_error("\\pset: unknown option: %s", param);
    5326           0 :         return false;
    5327             :     }
    5328             : 
    5329        1640 :     if (!quiet)
    5330           0 :         printPsetInfo(param, &pset.popt);
    5331             : 
    5332        1640 :     return true;
    5333             : }
    5334             : 
    5335             : /*
    5336             :  * printPsetInfo: print the state of the "param" formatting parameter in popt.
    5337             :  */
    5338             : static bool
    5339           0 : printPsetInfo(const char *param, printQueryOpt *popt)
    5340             : {
    5341             :     Assert(param != NULL);
    5342             : 
    5343             :     /* show border style/width */
    5344           0 :     if (strcmp(param, "border") == 0)
    5345           0 :         printf(_("Border style is %d.\n"), popt->topt.border);
    5346             : 
    5347             :     /* show the target width for the wrapped format */
    5348           0 :     else if (strcmp(param, "columns") == 0)
    5349             :     {
    5350           0 :         if (!popt->topt.columns)
    5351           0 :             printf(_("Target width is unset.\n"));
    5352             :         else
    5353           0 :             printf(_("Target width is %d.\n"), popt->topt.columns);
    5354             :     }
    5355             : 
    5356             :     /* show expanded/vertical mode */
    5357           0 :     else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
    5358             :     {
    5359           0 :         if (popt->topt.expanded == 1)
    5360           0 :             printf(_("Expanded display is on.\n"));
    5361           0 :         else if (popt->topt.expanded == 2)
    5362           0 :             printf(_("Expanded display is used automatically.\n"));
    5363             :         else
    5364           0 :             printf(_("Expanded display is off.\n"));
    5365             :     }
    5366             : 
    5367             :     /* show xheader width value */
    5368           0 :     else if (strcmp(param, "xheader_width") == 0)
    5369             :     {
    5370           0 :         if (popt->topt.expanded_header_width_type == PRINT_XHEADER_FULL)
    5371           0 :             printf(_("Expanded header width is \"%s\".\n"), "full");
    5372           0 :         else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_COLUMN)
    5373           0 :             printf(_("Expanded header width is \"%s\".\n"), "column");
    5374           0 :         else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_PAGE)
    5375           0 :             printf(_("Expanded header width is \"%s\".\n"), "page");
    5376           0 :         else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH)
    5377           0 :             printf(_("Expanded header width is %d.\n"), popt->topt.expanded_header_exact_width);
    5378             :     }
    5379             : 
    5380             :     /* show field separator for CSV format */
    5381           0 :     else if (strcmp(param, "csv_fieldsep") == 0)
    5382             :     {
    5383           0 :         printf(_("Field separator for CSV is \"%s\".\n"),
    5384             :                popt->topt.csvFieldSep);
    5385             :     }
    5386             : 
    5387             :     /* show field separator for unaligned text */
    5388           0 :     else if (strcmp(param, "fieldsep") == 0)
    5389             :     {
    5390           0 :         if (popt->topt.fieldSep.separator_zero)
    5391           0 :             printf(_("Field separator is zero byte.\n"));
    5392             :         else
    5393           0 :             printf(_("Field separator is \"%s\".\n"),
    5394             :                    popt->topt.fieldSep.separator);
    5395             :     }
    5396             : 
    5397           0 :     else if (strcmp(param, "fieldsep_zero") == 0)
    5398             :     {
    5399           0 :         printf(_("Field separator is zero byte.\n"));
    5400             :     }
    5401             : 
    5402             :     /* show disable "(x rows)" footer */
    5403           0 :     else if (strcmp(param, "footer") == 0)
    5404             :     {
    5405           0 :         if (popt->topt.default_footer)
    5406           0 :             printf(_("Default footer is on.\n"));
    5407             :         else
    5408           0 :             printf(_("Default footer is off.\n"));
    5409             :     }
    5410             : 
    5411             :     /* show format */
    5412           0 :     else if (strcmp(param, "format") == 0)
    5413             :     {
    5414           0 :         printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
    5415             :     }
    5416             : 
    5417             :     /* show table line style */
    5418           0 :     else if (strcmp(param, "linestyle") == 0)
    5419             :     {
    5420           0 :         printf(_("Line style is %s.\n"),
    5421             :                get_line_style(&popt->topt)->name);
    5422             :     }
    5423             : 
    5424             :     /* show null display */
    5425           0 :     else if (strcmp(param, "null") == 0)
    5426             :     {
    5427           0 :         printf(_("Null display is \"%s\".\n"),
    5428             :                popt->nullPrint ? popt->nullPrint : "");
    5429             :     }
    5430             : 
    5431             :     /* show locale-aware numeric output */
    5432           0 :     else if (strcmp(param, "numericlocale") == 0)
    5433             :     {
    5434           0 :         if (popt->topt.numericLocale)
    5435           0 :             printf(_("Locale-adjusted numeric output is on.\n"));
    5436             :         else
    5437           0 :             printf(_("Locale-adjusted numeric output is off.\n"));
    5438             :     }
    5439             : 
    5440             :     /* show toggle use of pager */
    5441           0 :     else if (strcmp(param, "pager") == 0)
    5442             :     {
    5443           0 :         if (popt->topt.pager == 1)
    5444           0 :             printf(_("Pager is used for long output.\n"));
    5445           0 :         else if (popt->topt.pager == 2)
    5446           0 :             printf(_("Pager is always used.\n"));
    5447             :         else
    5448           0 :             printf(_("Pager usage is off.\n"));
    5449             :     }
    5450             : 
    5451             :     /* show minimum lines for pager use */
    5452           0 :     else if (strcmp(param, "pager_min_lines") == 0)
    5453             :     {
    5454           0 :         printf(ngettext("Pager won't be used for less than %d line.\n",
    5455             :                         "Pager won't be used for less than %d lines.\n",
    5456             :                         popt->topt.pager_min_lines),
    5457             :                popt->topt.pager_min_lines);
    5458             :     }
    5459             : 
    5460             :     /* show record separator for unaligned text */
    5461           0 :     else if (strcmp(param, "recordsep") == 0)
    5462             :     {
    5463           0 :         if (popt->topt.recordSep.separator_zero)
    5464           0 :             printf(_("Record separator is zero byte.\n"));
    5465           0 :         else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
    5466           0 :             printf(_("Record separator is <newline>.\n"));
    5467             :         else
    5468           0 :             printf(_("Record separator is \"%s\".\n"),
    5469             :                    popt->topt.recordSep.separator);
    5470             :     }
    5471             : 
    5472           0 :     else if (strcmp(param, "recordsep_zero") == 0)
    5473             :     {
    5474           0 :         printf(_("Record separator is zero byte.\n"));
    5475             :     }
    5476             : 
    5477             :     /* show HTML table tag options */
    5478           0 :     else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
    5479             :     {
    5480           0 :         if (popt->topt.tableAttr)
    5481           0 :             printf(_("Table attributes are \"%s\".\n"),
    5482             :                    popt->topt.tableAttr);
    5483             :         else
    5484           0 :             printf(_("Table attributes unset.\n"));
    5485             :     }
    5486             : 
    5487             :     /* show title override */
    5488           0 :     else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
    5489             :     {
    5490           0 :         if (popt->title)
    5491           0 :             printf(_("Title is \"%s\".\n"), popt->title);
    5492             :         else
    5493           0 :             printf(_("Title is unset.\n"));
    5494             :     }
    5495             : 
    5496             :     /* show toggle between full and tuples-only format */
    5497           0 :     else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
    5498             :     {
    5499           0 :         if (popt->topt.tuples_only)
    5500           0 :             printf(_("Tuples only is on.\n"));
    5501             :         else
    5502           0 :             printf(_("Tuples only is off.\n"));
    5503             :     }
    5504             : 
    5505             :     /* Unicode style formatting */
    5506           0 :     else if (strcmp(param, "unicode_border_linestyle") == 0)
    5507             :     {
    5508           0 :         printf(_("Unicode border line style is \"%s\".\n"),
    5509             :                _unicode_linestyle2string(popt->topt.unicode_border_linestyle));
    5510             :     }
    5511             : 
    5512           0 :     else if (strcmp(param, "unicode_column_linestyle") == 0)
    5513             :     {
    5514           0 :         printf(_("Unicode column line style is \"%s\".\n"),
    5515             :                _unicode_linestyle2string(popt->topt.unicode_column_linestyle));
    5516             :     }
    5517             : 
    5518           0 :     else if (strcmp(param, "unicode_header_linestyle") == 0)
    5519             :     {
    5520           0 :         printf(_("Unicode header line style is \"%s\".\n"),
    5521             :                _unicode_linestyle2string(popt->topt.unicode_header_linestyle));
    5522             :     }
    5523             : 
    5524             :     else
    5525             :     {
    5526           0 :         pg_log_error("\\pset: unknown option: %s", param);
    5527           0 :         return false;
    5528             :     }
    5529             : 
    5530           0 :     return true;
    5531             : }
    5532             : 
    5533             : /*
    5534             :  * savePsetInfo: make a malloc'd copy of the data in *popt.
    5535             :  *
    5536             :  * Possibly this should be somewhere else, but it's a bit specific to psql.
    5537             :  */
    5538             : printQueryOpt *
    5539          42 : savePsetInfo(const printQueryOpt *popt)
    5540             : {
    5541             :     printQueryOpt *save;
    5542             : 
    5543          42 :     save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
    5544             : 
    5545             :     /* Flat-copy all the scalar fields, then duplicate sub-structures. */
    5546          42 :     memcpy(save, popt, sizeof(printQueryOpt));
    5547             : 
    5548             :     /* topt.line_style points to const data that need not be duplicated */
    5549          42 :     if (popt->topt.fieldSep.separator)
    5550          42 :         save->topt.fieldSep.separator = pg_strdup(popt->topt.fieldSep.separator);
    5551          42 :     if (popt->topt.recordSep.separator)
    5552          42 :         save->topt.recordSep.separator = pg_strdup(popt->topt.recordSep.separator);
    5553          42 :     if (popt->topt.tableAttr)
    5554           0 :         save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
    5555          42 :     if (popt->nullPrint)
    5556           0 :         save->nullPrint = pg_strdup(popt->nullPrint);
    5557          42 :     if (popt->title)
    5558           0 :         save->title = pg_strdup(popt->title);
    5559             : 
    5560             :     /*
    5561             :      * footers and translate_columns are never set in psql's print settings,
    5562             :      * so we needn't write code to duplicate them.
    5563             :      */
    5564             :     Assert(popt->footers == NULL);
    5565             :     Assert(popt->translate_columns == NULL);
    5566             : 
    5567          42 :     return save;
    5568             : }
    5569             : 
    5570             : /*
    5571             :  * restorePsetInfo: restore *popt from the previously-saved copy *save,
    5572             :  * then free *save.
    5573             :  */
    5574             : void
    5575          42 : restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
    5576             : {
    5577             :     /* Free all the old data we're about to overwrite the pointers to. */
    5578             : 
    5579             :     /* topt.line_style points to const data that need not be duplicated */
    5580          42 :     free(popt->topt.fieldSep.separator);
    5581          42 :     free(popt->topt.recordSep.separator);
    5582          42 :     free(popt->topt.tableAttr);
    5583          42 :     free(popt->nullPrint);
    5584          42 :     free(popt->title);
    5585             : 
    5586             :     /*
    5587             :      * footers and translate_columns are never set in psql's print settings,
    5588             :      * so we needn't write code to duplicate them.
    5589             :      */
    5590             :     Assert(popt->footers == NULL);
    5591             :     Assert(popt->translate_columns == NULL);
    5592             : 
    5593             :     /* Now we may flat-copy all the fields, including pointers. */
    5594          42 :     memcpy(popt, save, sizeof(printQueryOpt));
    5595             : 
    5596             :     /* Lastly, free "save" ... but its sub-structures now belong to popt. */
    5597          42 :     free(save);
    5598          42 : }
    5599             : 
    5600             : static const char *
    5601          36 : pset_bool_string(bool val)
    5602             : {
    5603          36 :     return val ? "on" : "off";
    5604             : }
    5605             : 
    5606             : 
    5607             : static char *
    5608          24 : pset_quoted_string(const char *str)
    5609             : {
    5610          24 :     char       *ret = pg_malloc(strlen(str) * 2 + 3);
    5611          24 :     char       *r = ret;
    5612             : 
    5613          24 :     *r++ = '\'';
    5614             : 
    5615          42 :     for (; *str; str++)
    5616             :     {
    5617          18 :         if (*str == '\n')
    5618             :         {
    5619           6 :             *r++ = '\\';
    5620           6 :             *r++ = 'n';
    5621             :         }
    5622          12 :         else if (*str == '\'')
    5623             :         {
    5624           0 :             *r++ = '\\';
    5625           0 :             *r++ = '\'';
    5626             :         }
    5627             :         else
    5628          12 :             *r++ = *str;
    5629             :     }
    5630             : 
    5631          24 :     *r++ = '\'';
    5632          24 :     *r = '\0';
    5633             : 
    5634          24 :     return ret;
    5635             : }
    5636             : 
    5637             : 
    5638             : /*
    5639             :  * Return a malloc'ed string for the \pset value.
    5640             :  *
    5641             :  * Note that for some string parameters, print.c distinguishes between unset
    5642             :  * and empty string, but for others it doesn't.  This function should produce
    5643             :  * output that produces the correct setting when fed back into \pset.
    5644             :  */
    5645             : static char *
    5646         132 : pset_value_string(const char *param, printQueryOpt *popt)
    5647             : {
    5648             :     Assert(param != NULL);
    5649             : 
    5650         132 :     if (strcmp(param, "border") == 0)
    5651           6 :         return psprintf("%d", popt->topt.border);
    5652         126 :     else if (strcmp(param, "columns") == 0)
    5653           6 :         return psprintf("%d", popt->topt.columns);
    5654         120 :     else if (strcmp(param, "csv_fieldsep") == 0)
    5655           6 :         return pset_quoted_string(popt->topt.csvFieldSep);
    5656         114 :     else if (strcmp(param, "expanded") == 0)
    5657          12 :         return pstrdup(popt->topt.expanded == 2
    5658             :                        ? "auto"
    5659           6 :                        : pset_bool_string(popt->topt.expanded));
    5660         108 :     else if (strcmp(param, "fieldsep") == 0)
    5661           6 :         return pset_quoted_string(popt->topt.fieldSep.separator
    5662             :                                   ? popt->topt.fieldSep.separator
    5663             :                                   : "");
    5664         102 :     else if (strcmp(param, "fieldsep_zero") == 0)
    5665           6 :         return pstrdup(pset_bool_string(popt->topt.fieldSep.separator_zero));
    5666          96 :     else if (strcmp(param, "footer") == 0)
    5667           6 :         return pstrdup(pset_bool_string(popt->topt.default_footer));
    5668          90 :     else if (strcmp(param, "format") == 0)
    5669           6 :         return pstrdup(_align2string(popt->topt.format));
    5670          84 :     else if (strcmp(param, "linestyle") == 0)
    5671           6 :         return pstrdup(get_line_style(&popt->topt)->name);
    5672          78 :     else if (strcmp(param, "null") == 0)
    5673           6 :         return pset_quoted_string(popt->nullPrint
    5674             :                                   ? popt->nullPrint
    5675             :                                   : "");
    5676          72 :     else if (strcmp(param, "numericlocale") == 0)
    5677           6 :         return pstrdup(pset_bool_string(popt->topt.numericLocale));
    5678          66 :     else if (strcmp(param, "pager") == 0)
    5679           6 :         return psprintf("%d", popt->topt.pager);
    5680          60 :     else if (strcmp(param, "pager_min_lines") == 0)
    5681           6 :         return psprintf("%d", popt->topt.pager_min_lines);
    5682          54 :     else if (strcmp(param, "recordsep") == 0)
    5683           6 :         return pset_quoted_string(popt->topt.recordSep.separator
    5684             :                                   ? popt->topt.recordSep.separator
    5685             :                                   : "");
    5686          48 :     else if (strcmp(param, "recordsep_zero") == 0)
    5687           6 :         return pstrdup(pset_bool_string(popt->topt.recordSep.separator_zero));
    5688          42 :     else if (strcmp(param, "tableattr") == 0)
    5689           6 :         return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
    5690          36 :     else if (strcmp(param, "title") == 0)
    5691           6 :         return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
    5692          30 :     else if (strcmp(param, "tuples_only") == 0)
    5693           6 :         return pstrdup(pset_bool_string(popt->topt.tuples_only));
    5694          24 :     else if (strcmp(param, "unicode_border_linestyle") == 0)
    5695           6 :         return pstrdup(_unicode_linestyle2string(popt->topt.unicode_border_linestyle));
    5696          18 :     else if (strcmp(param, "unicode_column_linestyle") == 0)
    5697           6 :         return pstrdup(_unicode_linestyle2string(popt->topt.unicode_column_linestyle));
    5698          12 :     else if (strcmp(param, "unicode_header_linestyle") == 0)
    5699           6 :         return pstrdup(_unicode_linestyle2string(popt->topt.unicode_header_linestyle));
    5700           6 :     else if (strcmp(param, "xheader_width") == 0)
    5701             :     {
    5702           6 :         if (popt->topt.expanded_header_width_type == PRINT_XHEADER_FULL)
    5703           6 :             return pstrdup("full");
    5704           0 :         else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_COLUMN)
    5705           0 :             return pstrdup("column");
    5706           0 :         else if (popt->topt.expanded_header_width_type == PRINT_XHEADER_PAGE)
    5707           0 :             return pstrdup("page");
    5708             :         else
    5709             :         {
    5710             :             /* must be PRINT_XHEADER_EXACT_WIDTH */
    5711             :             char        wbuff[32];
    5712             : 
    5713           0 :             snprintf(wbuff, sizeof(wbuff), "%d",
    5714             :                      popt->topt.expanded_header_exact_width);
    5715           0 :             return pstrdup(wbuff);
    5716             :         }
    5717             :     }
    5718             :     else
    5719           0 :         return pstrdup("ERROR");
    5720             : }
    5721             : 
    5722             : 
    5723             : 
    5724             : #ifndef WIN32
    5725             : #define DEFAULT_SHELL "/bin/sh"
    5726             : #else
    5727             : /*
    5728             :  *  CMD.EXE is in different places in different Win32 releases so we
    5729             :  *  have to rely on the path to find it.
    5730             :  */
    5731             : #define DEFAULT_SHELL "cmd.exe"
    5732             : #endif
    5733             : 
    5734             : static bool
    5735           0 : do_shell(const char *command)
    5736             : {
    5737             :     int         result;
    5738             : 
    5739           0 :     fflush(NULL);
    5740           0 :     if (!command)
    5741             :     {
    5742             :         char       *sys;
    5743             :         const char *shellName;
    5744             : 
    5745           0 :         shellName = getenv("SHELL");
    5746             : #ifdef WIN32
    5747             :         if (shellName == NULL)
    5748             :             shellName = getenv("COMSPEC");
    5749             : #endif
    5750           0 :         if (shellName == NULL)
    5751           0 :             shellName = DEFAULT_SHELL;
    5752             : 
    5753             :         /* See EDITOR handling comment for an explanation */
    5754             : #ifndef WIN32
    5755           0 :         sys = psprintf("exec %s", shellName);
    5756             : #else
    5757             :         sys = psprintf("\"%s\"", shellName);
    5758             : #endif
    5759           0 :         result = system(sys);
    5760           0 :         free(sys);
    5761             :     }
    5762             :     else
    5763           0 :         result = system(command);
    5764             : 
    5765           0 :     SetShellResultVariables(result);
    5766             : 
    5767           0 :     if (result == 127 || result == -1)
    5768             :     {
    5769           0 :         pg_log_error("\\!: failed");
    5770           0 :         return false;
    5771             :     }
    5772           0 :     return true;
    5773             : }
    5774             : 
    5775             : /*
    5776             :  * do_watch -- handler for \watch
    5777             :  *
    5778             :  * We break this out of exec_command to avoid having to plaster "volatile"
    5779             :  * onto a bunch of exec_command's variables to silence stupider compilers.
    5780             :  *
    5781             :  * "sleep" is the amount of time to sleep during each loop, measured in
    5782             :  * seconds.  The internals of this function should use "sleep_ms" for
    5783             :  * precise sleep time calculations.
    5784             :  */
    5785             : static bool
    5786          10 : do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows)
    5787             : {
    5788          10 :     long        sleep_ms = (long) (sleep * 1000);
    5789          10 :     printQueryOpt myopt = pset.popt;
    5790             :     const char *strftime_fmt;
    5791             :     const char *user_title;
    5792             :     char       *title;
    5793          10 :     const char *pagerprog = NULL;
    5794          10 :     FILE       *pagerpipe = NULL;
    5795             :     int         title_len;
    5796          10 :     int         res = 0;
    5797          10 :     bool        done = false;
    5798             : #ifndef WIN32
    5799             :     sigset_t    sigalrm_sigchld_sigint;
    5800             :     sigset_t    sigalrm_sigchld;
    5801             :     sigset_t    sigint;
    5802             :     struct itimerval interval;
    5803             : #endif
    5804             : 
    5805          10 :     if (!query_buf || query_buf->len <= 0)
    5806             :     {
    5807           0 :         pg_log_error("\\watch cannot be used with an empty query");
    5808           0 :         return false;
    5809             :     }
    5810             : 
    5811             : #ifndef WIN32
    5812          10 :     sigemptyset(&sigalrm_sigchld_sigint);
    5813          10 :     sigaddset(&sigalrm_sigchld_sigint, SIGCHLD);
    5814          10 :     sigaddset(&sigalrm_sigchld_sigint, SIGALRM);
    5815          10 :     sigaddset(&sigalrm_sigchld_sigint, SIGINT);
    5816             : 
    5817          10 :     sigemptyset(&sigalrm_sigchld);
    5818          10 :     sigaddset(&sigalrm_sigchld, SIGCHLD);
    5819          10 :     sigaddset(&sigalrm_sigchld, SIGALRM);
    5820             : 
    5821          10 :     sigemptyset(&sigint);
    5822          10 :     sigaddset(&sigint, SIGINT);
    5823             : 
    5824             :     /*
    5825             :      * Block SIGALRM and SIGCHLD before we start the timer and the pager (if
    5826             :      * configured), to avoid races.  sigwait() will receive them.
    5827             :      */
    5828          10 :     sigprocmask(SIG_BLOCK, &sigalrm_sigchld, NULL);
    5829             : 
    5830             :     /*
    5831             :      * Set a timer to interrupt sigwait() so we can run the query at the
    5832             :      * requested intervals.
    5833             :      */
    5834          10 :     interval.it_value.tv_sec = sleep_ms / 1000;
    5835          10 :     interval.it_value.tv_usec = (sleep_ms % 1000) * 1000;
    5836          10 :     interval.it_interval = interval.it_value;
    5837          10 :     if (setitimer(ITIMER_REAL, &interval, NULL) < 0)
    5838             :     {
    5839           0 :         pg_log_error("could not set timer: %m");
    5840           0 :         done = true;
    5841             :     }
    5842             : #endif
    5843             : 
    5844             :     /*
    5845             :      * For \watch, we ignore the size of the result and always use the pager
    5846             :      * as long as we're talking to a terminal and "\pset pager" is enabled.
    5847             :      * However, we'll only use the pager identified by PSQL_WATCH_PAGER.  We
    5848             :      * ignore the regular PSQL_PAGER or PAGER environment variables, because
    5849             :      * traditional pagers probably won't be very useful for showing a stream
    5850             :      * of results.
    5851             :      */
    5852             : #ifndef WIN32
    5853          10 :     pagerprog = getenv("PSQL_WATCH_PAGER");
    5854             :     /* if variable is empty or all-white-space, don't use pager */
    5855          10 :     if (pagerprog && strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
    5856           0 :         pagerprog = NULL;
    5857             : #endif
    5858          10 :     if (pagerprog && myopt.topt.pager &&
    5859           0 :         isatty(fileno(stdin)) && isatty(fileno(stdout)))
    5860             :     {
    5861           0 :         fflush(NULL);
    5862           0 :         disable_sigpipe_trap();
    5863           0 :         pagerpipe = popen(pagerprog, "w");
    5864             : 
    5865           0 :         if (!pagerpipe)
    5866             :             /* silently proceed without pager */
    5867           0 :             restore_sigpipe_trap();
    5868             :     }
    5869             : 
    5870             :     /*
    5871             :      * Choose format for timestamps.  We might eventually make this a \pset
    5872             :      * option.  In the meantime, using a variable for the format suppresses
    5873             :      * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
    5874             :      */
    5875          10 :     strftime_fmt = "%c";
    5876             : 
    5877             :     /*
    5878             :      * Set up rendering options, in particular, disable the pager unless
    5879             :      * PSQL_WATCH_PAGER was successfully launched.
    5880             :      */
    5881          10 :     if (!pagerpipe)
    5882          10 :         myopt.topt.pager = 0;
    5883             : 
    5884             :     /*
    5885             :      * If there's a title in the user configuration, make sure we have room
    5886             :      * for it in the title buffer.  Allow 128 bytes for the timestamp plus 128
    5887             :      * bytes for the rest.
    5888             :      */
    5889          10 :     user_title = myopt.title;
    5890          10 :     title_len = (user_title ? strlen(user_title) : 0) + 256;
    5891          10 :     title = pg_malloc(title_len);
    5892             : 
    5893             :     /* Loop to run query and then sleep awhile */
    5894          42 :     while (!done)
    5895             :     {
    5896             :         time_t      timer;
    5897             :         char        timebuf[128];
    5898             : 
    5899             :         /*
    5900             :          * Prepare title for output.  Note that we intentionally include a
    5901             :          * newline at the end of the title; this is somewhat historical but it
    5902             :          * makes for reasonably nicely formatted output in simple cases.
    5903             :          */
    5904          42 :         timer = time(NULL);
    5905          42 :         strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
    5906             : 
    5907          42 :         if (user_title)
    5908           0 :             snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
    5909             :                      user_title, timebuf, sleep_ms / 1000.0);
    5910             :         else
    5911          42 :             snprintf(title, title_len, _("%s (every %gs)\n"),
    5912             :                      timebuf, sleep_ms / 1000.0);
    5913          42 :         myopt.title = title;
    5914             : 
    5915             :         /* Run the query and print out the result */
    5916          42 :         res = PSQLexecWatch(query_buf->data, &myopt, pagerpipe, min_rows);
    5917             : 
    5918             :         /*
    5919             :          * PSQLexecWatch handles the case where we can no longer repeat the
    5920             :          * query, and returns 0 or -1.
    5921             :          */
    5922          40 :         if (res <= 0)
    5923           8 :             break;
    5924             : 
    5925             :         /* If we have iteration count, check that it's not exceeded yet */
    5926          38 :         if (iter && (--iter <= 0))
    5927           6 :             break;
    5928             : 
    5929             :         /* Quit if error on pager pipe (probably pager has quit) */
    5930          32 :         if (pagerpipe && ferror(pagerpipe))
    5931           0 :             break;
    5932             : 
    5933             :         /* Tight loop, no wait needed */
    5934          32 :         if (sleep_ms == 0)
    5935           8 :             continue;
    5936             : 
    5937             : #ifdef WIN32
    5938             : 
    5939             :         /*
    5940             :          * Wait a while before running the query again.  Break the sleep into
    5941             :          * short intervals (at most 1s); that's probably unnecessary since
    5942             :          * pg_usleep is interruptible on Windows, but it's cheap insurance.
    5943             :          */
    5944             :         for (long i = sleep_ms; i > 0;)
    5945             :         {
    5946             :             long        s = Min(i, 1000L);
    5947             : 
    5948             :             pg_usleep(s * 1000L);
    5949             :             if (cancel_pressed)
    5950             :             {
    5951             :                 done = true;
    5952             :                 break;
    5953             :             }
    5954             :             i -= s;
    5955             :         }
    5956             : #else
    5957             :         /* sigwait() will handle SIGINT. */
    5958          24 :         sigprocmask(SIG_BLOCK, &sigint, NULL);
    5959          24 :         if (cancel_pressed)
    5960           0 :             done = true;
    5961             : 
    5962             :         /* Wait for SIGINT, SIGCHLD or SIGALRM. */
    5963          24 :         while (!done)
    5964             :         {
    5965             :             int         signal_received;
    5966             : 
    5967          24 :             errno = sigwait(&sigalrm_sigchld_sigint, &signal_received);
    5968          24 :             if (errno != 0)
    5969             :             {
    5970             :                 /* Some other signal arrived? */
    5971           0 :                 if (errno == EINTR)
    5972           0 :                     continue;
    5973             :                 else
    5974             :                 {
    5975           0 :                     pg_log_error("could not wait for signals: %m");
    5976           0 :                     done = true;
    5977          24 :                     break;
    5978             :                 }
    5979             :             }
    5980             :             /* On ^C or pager exit, it's time to stop running the query. */
    5981          24 :             if (signal_received == SIGINT || signal_received == SIGCHLD)
    5982           0 :                 done = true;
    5983             :             /* Otherwise, we must have SIGALRM.  Time to run the query again. */
    5984          24 :             break;
    5985             :         }
    5986             : 
    5987             :         /* Unblock SIGINT so that slow queries can be interrupted. */
    5988          24 :         sigprocmask(SIG_UNBLOCK, &sigint, NULL);
    5989             : #endif
    5990             :     }
    5991             : 
    5992           8 :     if (pagerpipe)
    5993             :     {
    5994           0 :         pclose(pagerpipe);
    5995           0 :         restore_sigpipe_trap();
    5996             :     }
    5997             :     else
    5998             :     {
    5999             :         /*
    6000             :          * If the terminal driver echoed "^C", libedit/libreadline might be
    6001             :          * confused about the cursor position.  Therefore, inject a newline
    6002             :          * before the next prompt is displayed.  We only do this when not
    6003             :          * using a pager, because pagers are expected to restore the screen to
    6004             :          * a sane state on exit.
    6005             :          */
    6006           8 :         fprintf(stdout, "\n");
    6007           8 :         fflush(stdout);
    6008             :     }
    6009             : 
    6010             : #ifndef WIN32
    6011             :     /* Disable the interval timer. */
    6012           8 :     memset(&interval, 0, sizeof(interval));
    6013           8 :     setitimer(ITIMER_REAL, &interval, NULL);
    6014             :     /* Unblock SIGINT, SIGCHLD and SIGALRM. */
    6015           8 :     sigprocmask(SIG_UNBLOCK, &sigalrm_sigchld_sigint, NULL);
    6016             : #endif
    6017             : 
    6018           8 :     pg_free(title);
    6019           8 :     return (res >= 0);
    6020             : }
    6021             : 
    6022             : /*
    6023             :  * a little code borrowed from PSQLexec() to manage ECHO_HIDDEN output.
    6024             :  * returns true unless we have ECHO_HIDDEN_NOEXEC.
    6025             :  */
    6026             : static bool
    6027         372 : echo_hidden_command(const char *query)
    6028             : {
    6029         372 :     if (pset.echo_hidden != PSQL_ECHO_HIDDEN_OFF)
    6030             :     {
    6031           0 :         printf(_("/******** QUERY *********/\n"
    6032             :                  "%s\n"
    6033             :                  "/************************/\n\n"), query);
    6034           0 :         fflush(stdout);
    6035           0 :         if (pset.logfile)
    6036             :         {
    6037           0 :             fprintf(pset.logfile,
    6038           0 :                     _("/******** QUERY *********/\n"
    6039             :                       "%s\n"
    6040             :                       "/************************/\n\n"), query);
    6041           0 :             fflush(pset.logfile);
    6042             :         }
    6043             : 
    6044           0 :         if (pset.echo_hidden == PSQL_ECHO_HIDDEN_NOEXEC)
    6045           0 :             return false;
    6046             :     }
    6047         372 :     return true;
    6048             : }
    6049             : 
    6050             : /*
    6051             :  * Look up the object identified by obj_type and desc.  If successful,
    6052             :  * store its OID in *obj_oid and return true, else return false.
    6053             :  *
    6054             :  * Note that we'll fail if the object doesn't exist OR if there are multiple
    6055             :  * matching candidates OR if there's something syntactically wrong with the
    6056             :  * object description; unfortunately it can be hard to tell the difference.
    6057             :  */
    6058             : static bool
    6059         186 : lookup_object_oid(EditableObjectType obj_type, const char *desc,
    6060             :                   Oid *obj_oid)
    6061             : {
    6062         186 :     bool        result = true;
    6063         186 :     PQExpBuffer query = createPQExpBuffer();
    6064             :     PGresult   *res;
    6065             : 
    6066         186 :     switch (obj_type)
    6067             :     {
    6068          60 :         case EditableFunction:
    6069             : 
    6070             :             /*
    6071             :              * We have a function description, e.g. "x" or "x(int)".  Issue a
    6072             :              * query to retrieve the function's OID using a cast to regproc or
    6073             :              * regprocedure (as appropriate).
    6074             :              */
    6075          60 :             appendPQExpBufferStr(query, "SELECT ");
    6076          60 :             appendStringLiteralConn(query, desc, pset.db);
    6077          60 :             appendPQExpBuffer(query, "::pg_catalog.%s::pg_catalog.oid",
    6078          60 :                               strchr(desc, '(') ? "regprocedure" : "regproc");
    6079          60 :             break;
    6080             : 
    6081         126 :         case EditableView:
    6082             : 
    6083             :             /*
    6084             :              * Convert view name (possibly schema-qualified) to OID.  Note:
    6085             :              * this code doesn't check if the relation is actually a view.
    6086             :              * We'll detect that in get_create_object_cmd().
    6087             :              */
    6088         126 :             appendPQExpBufferStr(query, "SELECT ");
    6089         126 :             appendStringLiteralConn(query, desc, pset.db);
    6090         126 :             appendPQExpBufferStr(query, "::pg_catalog.regclass::pg_catalog.oid");
    6091         126 :             break;
    6092             :     }
    6093             : 
    6094         186 :     if (!echo_hidden_command(query->data))
    6095             :     {
    6096           0 :         destroyPQExpBuffer(query);
    6097           0 :         return false;
    6098             :     }
    6099         186 :     res = PQexec(pset.db, query->data);
    6100         186 :     if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
    6101         186 :         *obj_oid = atooid(PQgetvalue(res, 0, 0));
    6102             :     else
    6103             :     {
    6104           0 :         minimal_error_message(res);
    6105           0 :         result = false;
    6106             :     }
    6107             : 
    6108         186 :     PQclear(res);
    6109         186 :     destroyPQExpBuffer(query);
    6110             : 
    6111         186 :     return result;
    6112             : }
    6113             : 
    6114             : /*
    6115             :  * Construct a "CREATE OR REPLACE ..." command that describes the specified
    6116             :  * database object.  If successful, the result is stored in buf.
    6117             :  */
    6118             : static bool
    6119         186 : get_create_object_cmd(EditableObjectType obj_type, Oid oid,
    6120             :                       PQExpBuffer buf)
    6121             : {
    6122         186 :     bool        result = true;
    6123         186 :     PQExpBuffer query = createPQExpBuffer();
    6124             :     PGresult   *res;
    6125             : 
    6126         186 :     switch (obj_type)
    6127             :     {
    6128          60 :         case EditableFunction:
    6129          60 :             printfPQExpBuffer(query,
    6130             :                               "SELECT pg_catalog.pg_get_functiondef(%u)",
    6131             :                               oid);
    6132          60 :             break;
    6133             : 
    6134         126 :         case EditableView:
    6135             : 
    6136             :             /*
    6137             :              * pg_get_viewdef() just prints the query, so we must prepend
    6138             :              * CREATE for ourselves.  We must fully qualify the view name to
    6139             :              * ensure the right view gets replaced.  Also, check relation kind
    6140             :              * to be sure it's a view.
    6141             :              *
    6142             :              * Starting with PG 9.4, views may have WITH [LOCAL|CASCADED]
    6143             :              * CHECK OPTION.  These are not part of the view definition
    6144             :              * returned by pg_get_viewdef() and so need to be retrieved
    6145             :              * separately.  Materialized views (introduced in 9.3) may have
    6146             :              * arbitrary storage parameter reloptions.
    6147             :              */
    6148         126 :             if (pset.sversion >= 90400)
    6149             :             {
    6150         126 :                 printfPQExpBuffer(query,
    6151             :                                   "SELECT nspname, relname, relkind, "
    6152             :                                   "pg_catalog.pg_get_viewdef(c.oid, true), "
    6153             :                                   "pg_catalog.array_remove(pg_catalog.array_remove(c.reloptions,'check_option=local'),'check_option=cascaded') AS reloptions, "
    6154             :                                   "CASE WHEN 'check_option=local' = ANY (c.reloptions) THEN 'LOCAL'::text "
    6155             :                                   "WHEN 'check_option=cascaded' = ANY (c.reloptions) THEN 'CASCADED'::text ELSE NULL END 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             :             else
    6162             :             {
    6163           0 :                 printfPQExpBuffer(query,
    6164             :                                   "SELECT nspname, relname, relkind, "
    6165             :                                   "pg_catalog.pg_get_viewdef(c.oid, true), "
    6166             :                                   "c.reloptions AS reloptions, "
    6167             :                                   "NULL AS checkoption "
    6168             :                                   "FROM pg_catalog.pg_class c "
    6169             :                                   "LEFT JOIN pg_catalog.pg_namespace n "
    6170             :                                   "ON c.relnamespace = n.oid WHERE c.oid = %u",
    6171             :                                   oid);
    6172             :             }
    6173         126 :             break;
    6174             :     }
    6175             : 
    6176         186 :     if (!echo_hidden_command(query->data))
    6177             :     {
    6178           0 :         destroyPQExpBuffer(query);
    6179           0 :         return false;
    6180             :     }
    6181         186 :     res = PQexec(pset.db, query->data);
    6182         186 :     if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
    6183             :     {
    6184         186 :         resetPQExpBuffer(buf);
    6185         186 :         switch (obj_type)
    6186             :         {
    6187          60 :             case EditableFunction:
    6188          60 :                 appendPQExpBufferStr(buf, PQgetvalue(res, 0, 0));
    6189          60 :                 break;
    6190             : 
    6191         126 :             case EditableView:
    6192             :                 {
    6193         126 :                     char       *nspname = PQgetvalue(res, 0, 0);
    6194         126 :                     char       *relname = PQgetvalue(res, 0, 1);
    6195         126 :                     char       *relkind = PQgetvalue(res, 0, 2);
    6196         126 :                     char       *viewdef = PQgetvalue(res, 0, 3);
    6197         126 :                     char       *reloptions = PQgetvalue(res, 0, 4);
    6198         126 :                     char       *checkoption = PQgetvalue(res, 0, 5);
    6199             : 
    6200             :                     /*
    6201             :                      * If the backend ever supports CREATE OR REPLACE
    6202             :                      * MATERIALIZED VIEW, allow that here; but as of today it
    6203             :                      * does not, so editing a matview definition in this way
    6204             :                      * is impossible.
    6205             :                      */
    6206         126 :                     switch (relkind[0])
    6207             :                     {
    6208             : #ifdef NOT_USED
    6209             :                         case RELKIND_MATVIEW:
    6210             :                             appendPQExpBufferStr(buf, "CREATE OR REPLACE MATERIALIZED VIEW ");
    6211             :                             break;
    6212             : #endif
    6213         126 :                         case RELKIND_VIEW:
    6214         126 :                             appendPQExpBufferStr(buf, "CREATE OR REPLACE VIEW ");
    6215         126 :                             break;
    6216           0 :                         default:
    6217           0 :                             pg_log_error("\"%s.%s\" is not a view",
    6218             :                                          nspname, relname);
    6219           0 :                             result = false;
    6220           0 :                             break;
    6221             :                     }
    6222         126 :                     appendPQExpBuffer(buf, "%s.", fmtId(nspname));
    6223         126 :                     appendPQExpBufferStr(buf, fmtId(relname));
    6224             : 
    6225             :                     /* reloptions, if not an empty array "{}" */
    6226         126 :                     if (reloptions != NULL && strlen(reloptions) > 2)
    6227             :                     {
    6228           0 :                         appendPQExpBufferStr(buf, "\n WITH (");
    6229           0 :                         if (!appendReloptionsArray(buf, reloptions, "",
    6230             :                                                    pset.encoding,
    6231           0 :                                                    standard_strings()))
    6232             :                         {
    6233           0 :                             pg_log_error("could not parse reloptions array");
    6234           0 :                             result = false;
    6235             :                         }
    6236           0 :                         appendPQExpBufferChar(buf, ')');
    6237             :                     }
    6238             : 
    6239             :                     /* View definition from pg_get_viewdef (a SELECT query) */
    6240         126 :                     appendPQExpBuffer(buf, " AS\n%s", viewdef);
    6241             : 
    6242             :                     /* Get rid of the semicolon that pg_get_viewdef appends */
    6243         126 :                     if (buf->len > 0 && buf->data[buf->len - 1] == ';')
    6244         126 :                         buf->data[--(buf->len)] = '\0';
    6245             : 
    6246             :                     /* WITH [LOCAL|CASCADED] CHECK OPTION */
    6247         126 :                     if (checkoption && checkoption[0] != '\0')
    6248           0 :                         appendPQExpBuffer(buf, "\n WITH %s CHECK OPTION",
    6249             :                                           checkoption);
    6250             :                 }
    6251         126 :                 break;
    6252             :         }
    6253             :         /* Make sure result ends with a newline */
    6254         186 :         if (buf->len > 0 && buf->data[buf->len - 1] != '\n')
    6255         126 :             appendPQExpBufferChar(buf, '\n');
    6256             :     }
    6257             :     else
    6258             :     {
    6259           0 :         minimal_error_message(res);
    6260           0 :         result = false;
    6261             :     }
    6262             : 
    6263         186 :     PQclear(res);
    6264         186 :     destroyPQExpBuffer(query);
    6265             : 
    6266         186 :     return result;
    6267             : }
    6268             : 
    6269             : /*
    6270             :  * If the given argument of \ef or \ev ends with a line number, delete the line
    6271             :  * number from the argument string and return it as an integer.  (We need
    6272             :  * this kluge because we're too lazy to parse \ef's function or \ev's view
    6273             :  * argument carefully --- we just slop it up in OT_WHOLE_LINE mode.)
    6274             :  *
    6275             :  * Returns -1 if no line number is present, 0 on error, or a positive value
    6276             :  * on success.
    6277             :  */
    6278             : static int
    6279           0 : strip_lineno_from_objdesc(char *obj)
    6280             : {
    6281             :     char       *c;
    6282             :     int         lineno;
    6283             : 
    6284           0 :     if (!obj || obj[0] == '\0')
    6285           0 :         return -1;
    6286             : 
    6287           0 :     c = obj + strlen(obj) - 1;
    6288             : 
    6289             :     /*
    6290             :      * This business of parsing backwards is dangerous as can be in a
    6291             :      * multibyte environment: there is no reason to believe that we are
    6292             :      * looking at the first byte of a character, nor are we necessarily
    6293             :      * working in a "safe" encoding.  Fortunately the bitpatterns we are
    6294             :      * looking for are unlikely to occur as non-first bytes, but beware of
    6295             :      * trying to expand the set of cases that can be recognized.  We must
    6296             :      * guard the <ctype.h> macros by using isascii() first, too.
    6297             :      */
    6298             : 
    6299             :     /* skip trailing whitespace */
    6300           0 :     while (c > obj && isascii((unsigned char) *c) && isspace((unsigned char) *c))
    6301           0 :         c--;
    6302             : 
    6303             :     /* must have a digit as last non-space char */
    6304           0 :     if (c == obj || !isascii((unsigned char) *c) || !isdigit((unsigned char) *c))
    6305           0 :         return -1;
    6306             : 
    6307             :     /* find start of digit string */
    6308           0 :     while (c > obj && isascii((unsigned char) *c) && isdigit((unsigned char) *c))
    6309           0 :         c--;
    6310             : 
    6311             :     /* digits must be separated from object name by space or closing paren */
    6312             :     /* notice also that we are not allowing an empty object name ... */
    6313           0 :     if (c == obj || !isascii((unsigned char) *c) ||
    6314           0 :         !(isspace((unsigned char) *c) || *c == ')'))
    6315           0 :         return -1;
    6316             : 
    6317             :     /* parse digit string */
    6318           0 :     c++;
    6319           0 :     lineno = atoi(c);
    6320           0 :     if (lineno < 1)
    6321             :     {
    6322           0 :         pg_log_error("invalid line number: %s", c);
    6323           0 :         return 0;
    6324             :     }
    6325             : 
    6326             :     /* strip digit string from object name */
    6327           0 :     *c = '\0';
    6328             : 
    6329           0 :     return lineno;
    6330             : }
    6331             : 
    6332             : /*
    6333             :  * Count number of lines in the buffer.
    6334             :  * This is used to test if pager is needed or not.
    6335             :  */
    6336             : static int
    6337         186 : count_lines_in_buf(PQExpBuffer buf)
    6338             : {
    6339         186 :     int         lineno = 0;
    6340         186 :     const char *lines = buf->data;
    6341             : 
    6342        2896 :     while (*lines != '\0')
    6343             :     {
    6344        2710 :         lineno++;
    6345             :         /* find start of next line */
    6346        2710 :         lines = strchr(lines, '\n');
    6347        2710 :         if (!lines)
    6348           0 :             break;
    6349        2710 :         lines++;
    6350             :     }
    6351             : 
    6352         186 :     return lineno;
    6353             : }
    6354             : 
    6355             : /*
    6356             :  * Write text at *lines to output with line numbers.
    6357             :  *
    6358             :  * For functions, lineno "1" should correspond to the first line of the
    6359             :  * function body; lines before that are unnumbered.  We expect that
    6360             :  * pg_get_functiondef() will emit that on a line beginning with "AS ",
    6361             :  * "BEGIN ", or "RETURN ", and that there can be no such line before
    6362             :  * the real start of the function body.
    6363             :  *
    6364             :  * Caution: this scribbles on *lines.
    6365             :  */
    6366             : static void
    6367          18 : print_with_linenumbers(FILE *output, char *lines, bool is_func)
    6368             : {
    6369          18 :     bool        in_header = is_func;
    6370          18 :     int         lineno = 0;
    6371             : 
    6372         192 :     while (*lines != '\0')
    6373             :     {
    6374             :         char       *eol;
    6375             : 
    6376         174 :         if (in_header &&
    6377          90 :             (strncmp(lines, "AS ", 3) == 0 ||
    6378          90 :              strncmp(lines, "BEGIN ", 6) == 0 ||
    6379          78 :              strncmp(lines, "RETURN ", 7) == 0))
    6380          18 :             in_header = false;
    6381             : 
    6382             :         /* increment lineno only for body's lines */
    6383         174 :         if (!in_header)
    6384         102 :             lineno++;
    6385             : 
    6386             :         /* find and mark end of current line */
    6387         174 :         eol = strchr(lines, '\n');
    6388         174 :         if (eol != NULL)
    6389         174 :             *eol = '\0';
    6390             : 
    6391             :         /* show current line as appropriate */
    6392         174 :         if (in_header)
    6393          72 :             fprintf(output, "        %s\n", lines);
    6394             :         else
    6395         102 :             fprintf(output, "%-7d %s\n", lineno, lines);
    6396             : 
    6397             :         /* advance to next line, if any */
    6398         174 :         if (eol == NULL)
    6399           0 :             break;
    6400         174 :         lines = ++eol;
    6401             :     }
    6402          18 : }
    6403             : 
    6404             : /*
    6405             :  * Report just the primary error; this is to avoid cluttering the output
    6406             :  * with, for instance, a redisplay of the internally generated query
    6407             :  */
    6408             : static void
    6409           0 : minimal_error_message(PGresult *res)
    6410             : {
    6411             :     PQExpBuffer msg;
    6412             :     const char *fld;
    6413             : 
    6414           0 :     msg = createPQExpBuffer();
    6415             : 
    6416           0 :     fld = PQresultErrorField(res, PG_DIAG_SEVERITY);
    6417           0 :     if (fld)
    6418           0 :         printfPQExpBuffer(msg, "%s:  ", fld);
    6419             :     else
    6420           0 :         printfPQExpBuffer(msg, "ERROR:  ");
    6421           0 :     fld = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
    6422           0 :     if (fld)
    6423           0 :         appendPQExpBufferStr(msg, fld);
    6424             :     else
    6425           0 :         appendPQExpBufferStr(msg, "(not available)");
    6426           0 :     appendPQExpBufferChar(msg, '\n');
    6427             : 
    6428           0 :     pg_log_error("%s", msg->data);
    6429             : 
    6430           0 :     destroyPQExpBuffer(msg);
    6431           0 : }

Generated by: LCOV version 1.16