LCOV - differential code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 65.7 % 2617 1719 8 890 9 27 1683 3 7
Current Date: 2026-05-05 10:23:31 +0900 Functions: 91.7 % 109 100 1 8 10 90
Baseline: lcov-20260505-025707-baseline Branches: 53.1 % 1898 1008 10 880 8 16 984
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 72.7 % 88 64 8 16 27 37
(360..) days: 65.4 % 2529 1655 874 9 1646
Function coverage date bins:
(30,360] days: 100.0 % 3 3 3
(360..) days: 91.5 % 106 97 1 8 10 87
Branch coverage date bins:
(30,360] days: 69.6 % 56 39 10 7 16 23
(360..) days: 52.6 % 1842 969 873 8 961

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

Generated by: LCOV version 2.5.0-beta