LCOV - differential code coverage report
Current view: top level - src/bin/psql - command.c (source / functions) Coverage Total Hit UNC UBC GNC CBC
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 65.2 % 2597 1694 1 902 4 1690
Current Date: 2025-09-06 07:49:51 +0900 Functions: 90.8 % 109 99 1 9 1 98
Baseline: lcov-20250906-005545-baseline Branches: 52.6 % 1874 985 889 2 983
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 77.1 % 35 27 8 27
(30,360] days: 60.3 % 232 140 1 91 4 136
(360..) days: 65.5 % 2330 1527 803 1527
Function coverage date bins:
(7,30] days: 100.0 % 2 2 2
(30,360] days: 100.0 % 8 8 8
(360..) days: 89.9 % 99 89 1 9 1 88
Branch coverage date bins:
(7,30] days: 73.1 % 26 19 7 19
(30,360] days: 56.6 % 152 86 66 2 84
(360..) days: 51.9 % 1696 880 816 880

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

Generated by: LCOV version 2.4-beta