LCOV - differential code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Coverage Total Hit UNC UBC GNC CBC DUB DCB
Current: ba12a202ce1b5581dc0ed149cf3f637d7897ad5d vs 2866d8c7dbfc9d882a7d80fef93fbbe763709932 Lines: 66.2 % 2633 1743 11 879 6 1737 12 7
Current Date: 2026-08-27 14:31:44 +0300 Functions: 91.7 % 109 100 3 6 7 93
Baseline: lcov-20260827-baseline Branches: 53.7 % 1904 1023 1 880 5 1018
Baseline Date: 2026-08-27 14:31:58 +0300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(1,7] days: 100.0 % 4 4 4
(7,30] days: 52.0 % 25 13 8 4 13
(30,360] days: 79.1 % 43 34 3 6 6 28
(360..) days: 66.1 % 2561 1692 869 1692
Function coverage date bins:
(360..) days: 91.7 % 109 100 3 6 7 93
Branch coverage date bins:
(7,30] days: 70.0 % 10 7 3 7
(30,360] days: 67.6 % 34 23 1 10 5 18
(360..) days: 53.4 % 1860 993 867 993

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

Generated by: LCOV version 2.0-1