LCOV - code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Hit Total Coverage
Test: PostgreSQL 14devel Lines: 1139 2071 55.0 %
Date: 2020-11-27 12:05:55 Functions: 81 93 87.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13