LCOV - code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13devel Lines: 993 1871 53.1 %
Date: 2019-09-22 08:06:49 Functions: 77 91 84.6 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13