Thanks to visit codestin.com
Credit goes to doxygen.postgresql.org

PostgreSQL Source Code git master
command.c File Reference
#include "postgres_fe.h"
#include <ctype.h>
#include <time.h>
#include <pwd.h>
#include <utime.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "catalog/pg_class_d.h"
#include "command.h"
#include "common.h"
#include "common/logging.h"
#include "common/string.h"
#include "copy.h"
#include "describe.h"
#include "fe_utils/cancel.h"
#include "fe_utils/print.h"
#include "fe_utils/string_utils.h"
#include "help.h"
#include "input.h"
#include "large_obj.h"
#include "libpq/pqcomm.h"
#include "mainloop.h"
#include "pqexpbuffer.h"
#include "psqlscanslash.h"
#include "settings.h"
#include "variables.h"
Include dependency graph for command.c:

Go to the source code of this file.

Macros

#define DEFAULT_SHELL   "/bin/sh"
 

Typedefs

typedef enum EditableObjectType EditableObjectType
 

Enumerations

enum  EditableObjectType { EditableFunction , EditableView }
 

Functions

static backslashResult exec_command (const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_a (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_bind (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_bind_named (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_C (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_connect (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_cd (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_close_prepared (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_conninfo (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_copy (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_copyright (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_crosstabview (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_d (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static bool exec_command_dfo (PsqlScanState scan_state, const char *cmd, const char *pattern, bool show_verbose, bool show_system)
 
static backslashResult exec_command_edit (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_ef_ev (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, bool is_func)
 
static backslashResult exec_command_echo (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_elif (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static backslashResult exec_command_else (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static backslashResult exec_command_endif (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static backslashResult exec_command_endpipeline (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_encoding (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_errverbose (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_f (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_flush (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_flushrequest (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_g (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult process_command_g_options (char *first_option, PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_gdesc (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_getenv (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_gexec (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_getresults (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_gset (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_help (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_html (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_include (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_if (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static backslashResult exec_command_list (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_lo (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_out (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_print (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_parse (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_password (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_prompt (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_pset (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_quit (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_reset (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
 
static backslashResult exec_command_restrict (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_s (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_sendpipeline (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_set (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_setenv (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_sf_sv (PsqlScanState scan_state, bool active_branch, const char *cmd, bool is_func)
 
static backslashResult exec_command_startpipeline (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_syncpipeline (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_t (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_T (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_timing (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_unrestrict (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_unset (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_write (PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_watch (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_x (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_z (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_shell_escape (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_slash_command_help (PsqlScanState scan_state, bool active_branch)
 
static char * read_connect_arg (PsqlScanState scan_state)
 
static PQExpBuffer gather_boolean_expression (PsqlScanState scan_state)
 
static bool is_true_boolean_expression (PsqlScanState scan_state, const char *name)
 
static void ignore_boolean_expression (PsqlScanState scan_state)
 
static void ignore_slash_options (PsqlScanState scan_state)
 
static void ignore_slash_filepipe (PsqlScanState scan_state)
 
static void ignore_slash_whole_line (PsqlScanState scan_state)
 
static bool is_branching_command (const char *cmd)
 
static void save_query_text_state (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static void discard_query_text (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static bool copy_previous_query (PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static bool do_connect (enum trivalue reuse_previous_specification, char *dbname, char *user, char *host, char *port)
 
static void wait_until_connected (PGconn *conn)
 
static bool do_edit (const char *filename_arg, PQExpBuffer query_buf, int lineno, bool discard_on_quit, bool *edited)
 
static bool do_shell (const char *command)
 
static bool do_watch (PQExpBuffer query_buf, double sleep, int iter, int min_rows)
 
static bool lookup_object_oid (EditableObjectType obj_type, const char *desc, Oid *obj_oid)
 
static bool get_create_object_cmd (EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
 
static int strip_lineno_from_objdesc (char *obj)
 
static int count_lines_in_buf (PQExpBuffer buf)
 
static void print_with_linenumbers (FILE *output, char *lines, bool is_func)
 
static void minimal_error_message (PGresult *res)
 
static void printSSLInfo (void)
 
static void printGSSInfo (void)
 
static bool printPsetInfo (const char *param, printQueryOpt *popt)
 
static char * pset_value_string (const char *param, printQueryOpt *popt)
 
backslashResult HandleSlashCmds (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static char * prompt_for_password (const char *username, bool *canceled)
 
static bool param_is_newly_set (const char *old_val, const char *new_val)
 
void connection_warnings (bool in_startup)
 
void SyncVariables (void)
 
void UnsyncVariables (void)
 
static bool editFile (const char *fname, int lineno)
 
int process_file (char *filename, bool use_relative_path)
 
static const char * _align2string (enum printFormat in)
 
static bool set_unicode_line_style (const char *value, size_t vallen, unicode_linestyle *linestyle)
 
static const char * _unicode_linestyle2string (int linestyle)
 
bool do_pset (const char *param, const char *value, printQueryOpt *popt, bool quiet)
 
printQueryOptsavePsetInfo (const printQueryOpt *popt)
 
void restorePsetInfo (printQueryOpt *popt, printQueryOpt *save)
 
static const char * pset_bool_string (bool val)
 
static char * pset_quoted_string (const char *str)
 
static bool echo_hidden_command (const char *query)
 

Variables

static bool restricted
 
static char * restrict_key
 

Macro Definition Documentation

◆ DEFAULT_SHELL

#define DEFAULT_SHELL   "/bin/sh"

Definition at line 5815 of file command.c.

Typedef Documentation

◆ EditableObjectType

Enumeration Type Documentation

◆ EditableObjectType

Enumerator
EditableFunction 
EditableView 

Definition at line 50 of file command.c.

51{
EditableObjectType
Definition: command.c:51
@ EditableFunction
Definition: command.c:52
@ EditableView
Definition: command.c:53

Function Documentation

◆ _align2string()

static const char * _align2string ( enum printFormat  in)
static

Definition at line 4990 of file command.c.

4991{
4992 switch (in)
4993 {
4994 case PRINT_NOTHING:
4995 return "nothing";
4996 break;
4997 case PRINT_ALIGNED:
4998 return "aligned";
4999 break;
5000 case PRINT_ASCIIDOC:
5001 return "asciidoc";
5002 break;
5003 case PRINT_CSV:
5004 return "csv";
5005 break;
5006 case PRINT_HTML:
5007 return "html";
5008 break;
5009 case PRINT_LATEX:
5010 return "latex";
5011 break;
5013 return "latex-longtable";
5014 break;
5015 case PRINT_TROFF_MS:
5016 return "troff-ms";
5017 break;
5018 case PRINT_UNALIGNED:
5019 return "unaligned";
5020 break;
5021 case PRINT_WRAPPED:
5022 return "wrapped";
5023 break;
5024 }
5025 return "unknown";
5026}
@ PRINT_LATEX_LONGTABLE
Definition: print.h:36
@ PRINT_CSV
Definition: print.h:33
@ PRINT_UNALIGNED
Definition: print.h:38
@ PRINT_ALIGNED
Definition: print.h:31
@ PRINT_TROFF_MS
Definition: print.h:37
@ PRINT_ASCIIDOC
Definition: print.h:32
@ PRINT_NOTHING
Definition: print.h:30
@ PRINT_LATEX
Definition: print.h:35
@ PRINT_HTML
Definition: print.h:34
@ PRINT_WRAPPED
Definition: print.h:39

References PRINT_ALIGNED, PRINT_ASCIIDOC, PRINT_CSV, PRINT_HTML, PRINT_LATEX, PRINT_LATEX_LONGTABLE, PRINT_NOTHING, PRINT_TROFF_MS, PRINT_UNALIGNED, and PRINT_WRAPPED.

Referenced by printPsetInfo(), and pset_value_string().

◆ _unicode_linestyle2string()

static const char * _unicode_linestyle2string ( int  linestyle)
static

Definition at line 5046 of file command.c.

5047{
5048 switch (linestyle)
5049 {
5051 return "single";
5052 break;
5054 return "double";
5055 break;
5056 }
5057 return "unknown";
5058}
@ UNICODE_LINESTYLE_SINGLE
Definition: print.h:101
@ UNICODE_LINESTYLE_DOUBLE
Definition: print.h:102

References UNICODE_LINESTYLE_DOUBLE, and UNICODE_LINESTYLE_SINGLE.

Referenced by printPsetInfo(), and pset_value_string().

◆ connection_warnings()

void connection_warnings ( bool  in_startup)

Definition at line 4441 of file command.c.

4442{
4443 if (!pset.quiet && !pset.notty)
4444 {
4445 int client_ver = PG_VERSION_NUM;
4446 char cverbuf[32];
4447 char sverbuf[32];
4448
4449 if (pset.sversion != client_ver)
4450 {
4451 const char *server_version;
4452
4453 /* Try to get full text form, might include "devel" etc */
4454 server_version = PQparameterStatus(pset.db, "server_version");
4455 /* Otherwise fall back on pset.sversion */
4456 if (!server_version)
4457 {
4459 sverbuf, sizeof(sverbuf));
4460 server_version = sverbuf;
4461 }
4462
4463 printf(_("%s (%s, server %s)\n"),
4464 pset.progname, PG_VERSION, server_version);
4465 }
4466 /* For version match, only print psql banner on startup. */
4467 else if (in_startup)
4468 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 */
4474 if (pset.sversion / 100 > client_ver / 100 ||
4475 pset.sversion < 90200)
4476 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)),
4482 sverbuf, sizeof(sverbuf)));
4483
4484#ifdef WIN32
4485 if (in_startup)
4486 checkWin32Codepage();
4487#endif
4488 printSSLInfo();
4489 printGSSInfo();
4490 }
4491}
static void printSSLInfo(void)
Definition: command.c:4500
static void printGSSInfo(void)
Definition: command.c:4528
#define _(x)
Definition: elog.c:91
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:7634
static int server_version
Definition: pg_dumpall.c:109
#define printf(...)
Definition: port.h:245
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:313
PGconn * db
Definition: settings.h:103
const char * progname
Definition: settings.h:142

References _, _psqlSettings::db, formatPGVersionNumber(), _psqlSettings::notty, PQparameterStatus(), printf, printGSSInfo(), printSSLInfo(), _psqlSettings::progname, pset, _psqlSettings::quiet, server_version, and _psqlSettings::sversion.

Referenced by CheckConnection(), do_connect(), and main().

◆ copy_previous_query()

static bool copy_previous_query ( PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 3846 of file command.c.

3847{
3848 if (query_buf && query_buf->len == 0)
3849 {
3850 appendPQExpBufferStr(query_buf, previous_buf->data);
3851 return true;
3852 }
3853 return false;
3854}
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:367

References appendPQExpBufferStr(), PQExpBufferData::data, and PQExpBufferData::len.

Referenced by exec_command(), exec_command_edit(), and exec_command_watch().

◆ count_lines_in_buf()

static int count_lines_in_buf ( PQExpBuffer  buf)
static

Definition at line 6427 of file command.c.

6428{
6429 int lineno = 0;
6430 const char *lines = buf->data;
6431
6432 while (*lines != '\0')
6433 {
6434 lineno++;
6435 /* find start of next line */
6436 lines = strchr(lines, '\n');
6437 if (!lines)
6438 break;
6439 lines++;
6440 }
6441
6442 return lineno;
6443}
static char * buf
Definition: pg_test_fsync.c:72

References buf.

Referenced by exec_command_sf_sv().

◆ discard_query_text()

static void discard_query_text ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 3820 of file command.c.

3822{
3823 if (query_buf)
3824 {
3825 int new_len = conditional_stack_get_query_len(cstack);
3826
3827 Assert(new_len >= 0 && new_len <= query_buf->len);
3828 query_buf->len = new_len;
3829 query_buf->data[new_len] = '\0';
3830 }
3831 psql_scan_set_paren_depth(scan_state,
3833}
int conditional_stack_get_query_len(ConditionalStack cstack)
Definition: conditional.c:162
int conditional_stack_get_paren_depth(ConditionalStack cstack)
Definition: conditional.c:184
Assert(PointerIsAligned(start, uint64))
const void size_t len
void psql_scan_set_paren_depth(PsqlScanState state, int depth)

References Assert(), conditional_stack_get_paren_depth(), conditional_stack_get_query_len(), PQExpBufferData::data, PQExpBufferData::len, len, and psql_scan_set_paren_depth().

Referenced by exec_command_elif(), exec_command_else(), and exec_command_endif().

◆ do_connect()

static bool do_connect ( enum trivalue  reuse_previous_specification,
char *  dbname,
char *  user,
char *  host,
char *  port 
)
static

Definition at line 3915 of file command.c.

3917{
3918 PGconn *o_conn = pset.db,
3919 *n_conn = NULL;
3920 PQconninfoOption *cinfo;
3921 int nconnopts = 0;
3922 bool same_host = false;
3923 char *password = NULL;
3924 char *client_encoding;
3925 bool success = true;
3926 bool keep_password = true;
3927 bool has_connection_string;
3928 bool reuse_previous;
3929
3930 has_connection_string = dbname ?
3932
3933 /* Complain if we have additional arguments after a connection string. */
3934 if (has_connection_string && (user || host || port))
3935 {
3936 pg_log_error("Do not give user, host, or port separately when using a connection string");
3937 return false;
3938 }
3939
3940 switch (reuse_previous_specification)
3941 {
3942 case TRI_YES:
3943 reuse_previous = true;
3944 break;
3945 case TRI_NO:
3946 reuse_previous = false;
3947 break;
3948 default:
3949 reuse_previous = !has_connection_string;
3950 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 */
3962 if (reuse_previous)
3963 {
3964 if (o_conn)
3965 cinfo = PQconninfo(o_conn);
3966 else if (pset.dead_conn)
3967 cinfo = PQconninfo(pset.dead_conn);
3968 else
3969 {
3970 /* This is reachable after a non-interactive \connect failure */
3971 pg_log_error("No database connection exists to re-use parameters from");
3972 return false;
3973 }
3974 }
3975 else
3976 cinfo = PQconndefaults();
3977
3978 if (cinfo)
3979 {
3980 if (has_connection_string)
3981 {
3982 /* Parse the connstring and insert values into cinfo */
3983 PQconninfoOption *replcinfo;
3984 char *errmsg;
3985
3986 replcinfo = PQconninfoParse(dbname, &errmsg);
3987 if (replcinfo)
3988 {
3989 PQconninfoOption *ci;
3990 PQconninfoOption *replci;
3991 bool have_password = false;
3992
3993 for (ci = cinfo, replci = replcinfo;
3994 ci->keyword && replci->keyword;
3995 ci++, replci++)
3996 {
3997 Assert(strcmp(ci->keyword, replci->keyword) == 0);
3998 /* Insert value from connstring if one was provided */
3999 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 char *swap = replci->val;
4007
4008 replci->val = ci->val;
4009 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 */
4018 if (replci->val == NULL ||
4019 strcmp(ci->val, replci->val) != 0)
4020 {
4021 if (strcmp(replci->keyword, "user") == 0 ||
4022 strcmp(replci->keyword, "host") == 0 ||
4023 strcmp(replci->keyword, "hostaddr") == 0 ||
4024 strcmp(replci->keyword, "port") == 0)
4025 keep_password = false;
4026 }
4027 /* Also note whether connstring contains a password. */
4028 if (strcmp(replci->keyword, "password") == 0)
4029 have_password = true;
4030 }
4031 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 */
4047 replci->val = ci->val;
4048 ci->val = NULL;
4049 }
4050 }
4051 Assert(ci->keyword == NULL && replci->keyword == NULL);
4052
4053 /* While here, determine how many option slots there are */
4054 nconnopts = ci - cinfo;
4055
4056 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 */
4063 if (have_password)
4064 keep_password = true;
4065
4066 /* Don't let code below try to inject dbname into params. */
4067 dbname = NULL;
4068 }
4069 else
4070 {
4071 /* PQconninfoParse failed */
4072 if (errmsg)
4073 {
4074 pg_log_error("%s", errmsg);
4076 }
4077 else
4078 pg_log_error("out of memory");
4079 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
4099 for (ci = cinfo; ci->keyword; ci++)
4100 {
4101 if (user && strcmp(ci->keyword, "user") == 0)
4102 {
4103 if (!(ci->val && strcmp(user, ci->val) == 0))
4104 keep_password = false;
4105 }
4106 else if (host && strcmp(ci->keyword, "host") == 0)
4107 {
4108 if (ci->val && strcmp(host, ci->val) == 0)
4109 same_host = true;
4110 else
4111 keep_password = false;
4112 }
4113 else if (port && strcmp(ci->keyword, "port") == 0)
4114 {
4115 if (!(ci->val && strcmp(port, ci->val) == 0))
4116 keep_password = false;
4117 }
4118 }
4119
4120 /* While here, determine how many option slots there are */
4121 nconnopts = ci - cinfo;
4122 }
4123 }
4124 else
4125 {
4126 /* We failed to create the cinfo structure */
4127 pg_log_error("out of memory");
4128 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 */
4141 if (pset.getPassword == TRI_YES && success)
4142 {
4143 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 password = prompt_for_password(has_connection_string ? NULL : user,
4153 &canceled);
4154 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 */
4162 if (pset.notty || getenv("PGCLIENTENCODING"))
4163 client_encoding = NULL;
4164 else
4165 client_encoding = "auto";
4166
4167 /* Loop till we have a connection or fail, which we might've already */
4168 while (success)
4169 {
4170 const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
4171 const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
4172 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 for (ci = cinfo; ci->keyword; ci++)
4185 {
4186 keywords[paramnum] = ci->keyword;
4187
4188 if (dbname && strcmp(ci->keyword, "dbname") == 0)
4189 values[paramnum++] = dbname;
4190 else if (user && strcmp(ci->keyword, "user") == 0)
4191 values[paramnum++] = user;
4192 else if (host && strcmp(ci->keyword, "host") == 0)
4193 values[paramnum++] = host;
4194 else if (host && !same_host && strcmp(ci->keyword, "hostaddr") == 0)
4195 {
4196 /* If we're changing the host value, drop any old hostaddr */
4197 values[paramnum++] = NULL;
4198 }
4199 else if (port && strcmp(ci->keyword, "port") == 0)
4200 values[paramnum++] = port;
4201 /* If !keep_password, we unconditionally drop old password */
4202 else if ((password || !keep_password) &&
4203 strcmp(ci->keyword, "password") == 0)
4204 values[paramnum++] = password;
4205 else if (strcmp(ci->keyword, "fallback_application_name") == 0)
4206 values[paramnum++] = pset.progname;
4207 else if (client_encoding &&
4208 strcmp(ci->keyword, "client_encoding") == 0)
4209 values[paramnum++] = client_encoding;
4210 else if (ci->val)
4211 values[paramnum++] = ci->val;
4212 /* else, don't bother making libpq parse this keyword */
4213 }
4214 /* add array terminator */
4215 keywords[paramnum] = NULL;
4216 values[paramnum] = NULL;
4217
4218 /* Note we do not want libpq to re-expand the dbname parameter */
4219 n_conn = PQconnectStartParams(keywords, values, false);
4220
4222 pg_free(values);
4223
4224 wait_until_connected(n_conn);
4225 if (PQstatus(n_conn) == CONNECTION_OK)
4226 break;
4227
4228 /*
4229 * Connection attempt failed; either retry the connection attempt with
4230 * a new password, or give up.
4231 */
4233 {
4234 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 password = prompt_for_password(PQuser(n_conn), &canceled);
4241 PQfinish(n_conn);
4242 n_conn = NULL;
4243 success = !canceled;
4244 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 */
4251 if (n_conn == NULL)
4252 pg_log_error("out of memory");
4253
4254 success = false;
4255 } /* end retry loop */
4256
4257 /* Release locally allocated data, whether we succeeded or not */
4259 PQconninfoFree(cinfo);
4260
4261 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 */
4269 {
4270 if (n_conn)
4271 {
4272 pg_log_info("%s", PQerrorMessage(n_conn));
4273 PQfinish(n_conn);
4274 }
4275
4276 /* pset.db is left unmodified */
4277 if (o_conn)
4278 pg_log_info("Previous connection kept");
4279 }
4280 else
4281 {
4282 if (n_conn)
4283 {
4284 pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
4285 PQfinish(n_conn);
4286 }
4287
4288 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 PQfinish(o_conn);
4300 pset.db = NULL;
4303 }
4304
4305 /* On the same reasoning, release any dead_conn to prevent reuse */
4306 if (pset.dead_conn)
4307 {
4309 pset.dead_conn = NULL;
4310 }
4311 }
4312
4313 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 */
4322 pset.db = n_conn;
4323 SyncVariables();
4324 connection_warnings(false); /* Must be after SyncVariables */
4325
4326 /* Tell the user about the new connection */
4327 if (!pset.quiet)
4328 {
4329 if (!o_conn ||
4330 param_is_newly_set(PQhost(o_conn), PQhost(pset.db)) ||
4332 {
4333 char *connhost = PQhost(pset.db);
4334 char *hostaddr = PQhostaddr(pset.db);
4335
4336 if (is_unixsock_path(connhost))
4337 {
4338 /* hostaddr overrides connhost */
4339 if (hostaddr && *hostaddr)
4340 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 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 {
4348 if (hostaddr && *hostaddr && strcmp(connhost, hostaddr) != 0)
4349 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 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
4357 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) */
4362 if (o_conn)
4363 PQfinish(o_conn);
4364 if (pset.dead_conn)
4365 {
4367 pset.dead_conn = NULL;
4368 }
4369
4370 return true;
4371}
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:279
bool recognized_connection_string(const char *connstr)
Definition: common.c:2704
static Datum values[MAXATTR]
Definition: bootstrap.c:153
void ResetCancelConn(void)
Definition: cancel.c:107
static bool param_is_newly_set(const char *old_val, const char *new_val)
Definition: command.c:3892
void UnsyncVariables(void)
Definition: command.c:4624
static void wait_until_connected(PGconn *conn)
Definition: command.c:4379
void SyncVariables(void)
Definition: command.c:4569
void connection_warnings(bool in_startup)
Definition: command.c:4441
static char * prompt_for_password(const char *username, bool *canceled)
Definition: command.c:3864
int errmsg(const char *fmt,...)
Definition: elog.c:1071
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:7513
char * PQport(const PGconn *conn)
Definition: fe-connect.c:7582
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:7546
PQconninfoOption * PQconninfo(PGconn *conn)
Definition: fe-connect.c:7456
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:7500
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:6216
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7732
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7616
PGconn * PQconnectStartParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:870
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5305
char * PQhostaddr(const PGconn *conn)
Definition: fe-connect.c:7569
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:2196
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7521
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7860
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7679
void PQfreemem(void *ptr)
Definition: fe-exec.c:4043
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static bool success
Definition: initdb.c:187
return false
Definition: isn.c:135
static const JsonPathKeyword keywords[]
@ CONNECTION_OK
Definition: libpq-fe.h:84
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_info(...)
Definition: logging.h:124
static char * user
Definition: pg_regress.c:119
static int port
Definition: pg_regress.c:115
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:67
static char * password
Definition: streamutil.c:51
char * dbname
Definition: streamutil.c:49
PGconn * dead_conn
Definition: settings.h:158
enum trivalue getPassword
Definition: settings.h:137
bool cur_cmd_interactive
Definition: settings.h:140
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_NO
Definition: vacuumlo.c:37

References _, Assert(), CONNECTION_OK, connection_warnings(), _psqlSettings::cur_cmd_interactive, _psqlSettings::db, dbname, _psqlSettings::dead_conn, errmsg(), _psqlSettings::getPassword, is_unixsock_path(), _PQconninfoOption::keyword, keywords, NoticeProcessor(), _psqlSettings::notty, param_is_newly_set(), password, pg_free(), pg_log_error, pg_log_info, pg_malloc(), port, PQconndefaults(), PQconnectionNeedsPassword(), PQconnectStartParams(), PQconninfo(), PQconninfoFree(), PQconninfoParse(), PQdb(), PQerrorMessage(), PQfinish(), PQfreemem(), PQhost(), PQhostaddr(), PQport(), PQsetNoticeProcessor(), PQstatus(), PQuser(), printf, _psqlSettings::progname, prompt_for_password(), pset, _psqlSettings::quiet, recognized_connection_string(), ResetCancelConn(), success, SyncVariables(), TRI_NO, TRI_YES, UnsyncVariables(), user, _PQconninfoOption::val, values, and wait_until_connected().

Referenced by exec_command_connect().

◆ do_edit()

static bool do_edit ( const char *  filename_arg,
PQExpBuffer  query_buf,
int  lineno,
bool  discard_on_quit,
bool *  edited 
)
static

Definition at line 4728 of file command.c.

4730{
4731 char fnametmp[MAXPGPATH];
4732 FILE *stream = NULL;
4733 const char *fname;
4734 bool error = false;
4735 int fd;
4736 struct stat before,
4737 after;
4738
4739 if (filename_arg)
4740 fname = filename_arg;
4741 else
4742 {
4743 /* make a temp file to edit */
4744#ifndef WIN32
4745 const char *tmpdir = getenv("TMPDIR");
4746
4747 if (!tmpdir)
4748 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
4768 snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
4769 "/", (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
4775 fname = (const char *) fnametmp;
4776
4777 fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
4778 if (fd != -1)
4779 stream = fdopen(fd, "w");
4780
4781 if (fd == -1 || !stream)
4782 {
4783 pg_log_error("could not open temporary file \"%s\": %m", fname);
4784 error = true;
4785 }
4786 else
4787 {
4788 unsigned int ql = query_buf->len;
4789
4790 /* force newline-termination of what we send to editor */
4791 if (ql > 0 && query_buf->data[ql - 1] != '\n')
4792 {
4793 appendPQExpBufferChar(query_buf, '\n');
4794 ql++;
4795 }
4796
4797 if (fwrite(query_buf->data, 1, ql, stream) != ql)
4798 {
4799 pg_log_error("%s: %m", fname);
4800
4801 if (fclose(stream) != 0)
4802 pg_log_error("%s: %m", fname);
4803
4804 if (remove(fname) != 0)
4805 pg_log_error("%s: %m", fname);
4806
4807 error = true;
4808 }
4809 else if (fclose(stream) != 0)
4810 {
4811 pg_log_error("%s: %m", fname);
4812 if (remove(fname) != 0)
4813 pg_log_error("%s: %m", fname);
4814 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 */
4831 ut.modtime = ut.actime = time(NULL) - 2;
4832 (void) utime(fname, &ut);
4833 }
4834 }
4835 }
4836
4837 if (!error && stat(fname, &before) != 0)
4838 {
4839 pg_log_error("%s: %m", fname);
4840 error = true;
4841 }
4842
4843 /* call editor */
4844 if (!error)
4845 error = !editFile(fname, lineno);
4846
4847 if (!error && stat(fname, &after) != 0)
4848 {
4849 pg_log_error("%s: %m", fname);
4850 error = true;
4851 }
4852
4853 /* file was edited if the size or modification time has changed */
4854 if (!error &&
4855 (before.st_size != after.st_size ||
4856 before.st_mtime != after.st_mtime))
4857 {
4858 stream = fopen(fname, PG_BINARY_R);
4859 if (!stream)
4860 {
4861 pg_log_error("%s: %m", fname);
4862 error = true;
4863 }
4864 else
4865 {
4866 /* read file back into query_buf */
4867 char line[1024];
4868
4869 resetPQExpBuffer(query_buf);
4870 while (fgets(line, sizeof(line), stream) != NULL)
4871 appendPQExpBufferStr(query_buf, line);
4872
4873 if (ferror(stream))
4874 {
4875 pg_log_error("%s: %m", fname);
4876 error = true;
4877 resetPQExpBuffer(query_buf);
4878 }
4879 else if (edited)
4880 {
4881 *edited = true;
4882 }
4883
4884 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 */
4893 if (discard_on_quit)
4894 resetPQExpBuffer(query_buf);
4895 }
4896
4897 /* remove temp file */
4898 if (!filename_arg)
4899 {
4900 if (remove(fname) == -1)
4901 {
4902 pg_log_error("%s: %m", fname);
4903 error = true;
4904 }
4905 }
4906
4907 return !error;
4908}
#define PG_BINARY_R
Definition: c.h:1275
static bool editFile(const char *fname, int lineno)
Definition: command.c:4646
#define MAXPGPATH
#define strerror
Definition: port.h:252
#define snprintf
Definition: port.h:239
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:146
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:378
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static int before(chr x, chr y)
Definition: regc_locale.c:488
static void error(void)
Definition: sql-dyntest.c:147
#define stat
Definition: win32_port.h:274

References appendPQExpBufferChar(), appendPQExpBufferStr(), before(), PQExpBufferData::data, editFile(), error(), fd(), PQExpBufferData::len, MAXPGPATH, PG_BINARY_R, pg_log_error, resetPQExpBuffer(), snprintf, stat::st_mtime, stat::st_size, stat, and strerror.

Referenced by exec_command_edit(), and exec_command_ef_ev().

◆ do_pset()

bool do_pset ( const char *  param,
const char *  value,
printQueryOpt popt,
bool  quiet 
)

Definition at line 5076 of file command.c.

5077{
5078 size_t vallen = 0;
5079
5080 Assert(param != NULL);
5081
5082 if (value)
5083 vallen = strlen(value);
5084
5085 /* set format */
5086 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 if (!value)
5106 ;
5107 else
5108 {
5109 int match_pos = -1;
5110
5111 for (int i = 0; i < lengthof(formats); i++)
5112 {
5113 if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
5114 {
5115 if (match_pos < 0)
5116 match_pos = i;
5117 else
5118 {
5119 pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
5120 value,
5121 formats[match_pos].name, formats[i].name);
5122 return false;
5123 }
5124 }
5125 }
5126 if (match_pos >= 0)
5127 popt->topt.format = formats[match_pos].number;
5128 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 */
5136 }
5137 else
5138 {
5139 pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
5140 return false;
5141 }
5142 }
5143 }
5144
5145 /* set table line style */
5146 else if (strcmp(param, "linestyle") == 0)
5147 {
5148 if (!value)
5149 ;
5150 else if (pg_strncasecmp("ascii", value, vallen) == 0)
5152 else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
5154 else if (pg_strncasecmp("unicode", value, vallen) == 0)
5155 popt->topt.line_style = &pg_utf8format;
5156 else
5157 {
5158 pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
5159 return false;
5160 }
5161 }
5162
5163 /* set unicode border line style */
5164 else if (strcmp(param, "unicode_border_linestyle") == 0)
5165 {
5166 if (!value)
5167 ;
5168 else if (set_unicode_line_style(value, vallen,
5170 refresh_utf8format(&(popt->topt));
5171 else
5172 {
5173 pg_log_error("\\pset: allowed Unicode border line styles are single, double");
5174 return false;
5175 }
5176 }
5177
5178 /* set unicode column line style */
5179 else if (strcmp(param, "unicode_column_linestyle") == 0)
5180 {
5181 if (!value)
5182 ;
5183 else if (set_unicode_line_style(value, vallen,
5185 refresh_utf8format(&(popt->topt));
5186 else
5187 {
5188 pg_log_error("\\pset: allowed Unicode column line styles are single, double");
5189 return false;
5190 }
5191 }
5192
5193 /* set unicode header line style */
5194 else if (strcmp(param, "unicode_header_linestyle") == 0)
5195 {
5196 if (!value)
5197 ;
5198 else if (set_unicode_line_style(value, vallen,
5200 refresh_utf8format(&(popt->topt));
5201 else
5202 {
5203 pg_log_error("\\pset: allowed Unicode header line styles are single, double");
5204 return false;
5205 }
5206 }
5207
5208 /* set border style/width */
5209 else if (strcmp(param, "border") == 0)
5210 {
5211 if (value)
5212 popt->topt.border = atoi(value);
5213 }
5214
5215 /* set expanded/vertical mode */
5216 else if (strcmp(param, "x") == 0 ||
5217 strcmp(param, "expanded") == 0 ||
5218 strcmp(param, "vertical") == 0)
5219 {
5220 if (value && pg_strcasecmp(value, "auto") == 0)
5221 popt->topt.expanded = 2;
5222 else if (value)
5223 {
5224 bool on_off;
5225
5226 if (ParseVariableBool(value, NULL, &on_off))
5227 popt->topt.expanded = on_off ? 1 : 0;
5228 else
5229 {
5230 PsqlVarEnumError(param, value, "on, off, auto");
5231 return false;
5232 }
5233 }
5234 else
5235 popt->topt.expanded = !popt->topt.expanded;
5236 }
5237
5238 /* header line width in expanded mode */
5239 else if (strcmp(param, "xheader_width") == 0)
5240 {
5241 if (!value)
5242 ;
5243 else if (pg_strcasecmp(value, "full") == 0)
5245 else if (pg_strcasecmp(value, "column") == 0)
5247 else if (pg_strcasecmp(value, "page") == 0)
5249 else
5250 {
5251 int intval = atoi(value);
5252
5253 if (intval == 0)
5254 {
5255 pg_log_error("\\pset: allowed xheader_width values are \"%s\" (default), \"%s\", \"%s\", or a number specifying the exact width", "full", "column", "page");
5256 return false;
5257 }
5258
5260 popt->topt.expanded_header_exact_width = intval;
5261 }
5262 }
5263
5264 /* field separator for CSV format */
5265 else if (strcmp(param, "csv_fieldsep") == 0)
5266 {
5267 if (value)
5268 {
5269 /* CSV separator has to be a one-byte character */
5270 if (strlen(value) != 1)
5271 {
5272 pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
5273 return false;
5274 }
5275 if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
5276 {
5277 pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
5278 return false;
5279 }
5280 popt->topt.csvFieldSep[0] = value[0];
5281 }
5282 }
5283
5284 /* locale-aware numeric output */
5285 else if (strcmp(param, "numericlocale") == 0)
5286 {
5287 if (value)
5288 return ParseVariableBool(value, param, &popt->topt.numericLocale);
5289 else
5290 popt->topt.numericLocale = !popt->topt.numericLocale;
5291 }
5292
5293 /* null display */
5294 else if (strcmp(param, "null") == 0)
5295 {
5296 if (value)
5297 {
5298 free(popt->nullPrint);
5299 popt->nullPrint = pg_strdup(value);
5300 }
5301 }
5302
5303 /* field separator for unaligned text */
5304 else if (strcmp(param, "fieldsep") == 0)
5305 {
5306 if (value)
5307 {
5308 free(popt->topt.fieldSep.separator);
5310 popt->topt.fieldSep.separator_zero = false;
5311 }
5312 }
5313
5314 else if (strcmp(param, "fieldsep_zero") == 0)
5315 {
5316 free(popt->topt.fieldSep.separator);
5317 popt->topt.fieldSep.separator = NULL;
5318 popt->topt.fieldSep.separator_zero = true;
5319 }
5320
5321 /* record separator for unaligned text */
5322 else if (strcmp(param, "recordsep") == 0)
5323 {
5324 if (value)
5325 {
5328 popt->topt.recordSep.separator_zero = false;
5329 }
5330 }
5331
5332 else if (strcmp(param, "recordsep_zero") == 0)
5333 {
5335 popt->topt.recordSep.separator = NULL;
5336 popt->topt.recordSep.separator_zero = true;
5337 }
5338
5339 /* toggle between full and tuples-only format */
5340 else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5341 {
5342 if (value)
5343 return ParseVariableBool(value, param, &popt->topt.tuples_only);
5344 else
5345 popt->topt.tuples_only = !popt->topt.tuples_only;
5346 }
5347
5348 /* set title override */
5349 else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5350 {
5351 free(popt->title);
5352 if (!value)
5353 popt->title = NULL;
5354 else
5355 popt->title = pg_strdup(value);
5356 }
5357
5358 /* set HTML table tag options */
5359 else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5360 {
5361 free(popt->topt.tableAttr);
5362 if (!value)
5363 popt->topt.tableAttr = NULL;
5364 else
5365 popt->topt.tableAttr = pg_strdup(value);
5366 }
5367
5368 /* toggle use of pager */
5369 else if (strcmp(param, "pager") == 0)
5370 {
5371 if (value && pg_strcasecmp(value, "always") == 0)
5372 popt->topt.pager = 2;
5373 else if (value)
5374 {
5375 bool on_off;
5376
5377 if (!ParseVariableBool(value, NULL, &on_off))
5378 {
5379 PsqlVarEnumError(param, value, "on, off, always");
5380 return false;
5381 }
5382 popt->topt.pager = on_off ? 1 : 0;
5383 }
5384 else if (popt->topt.pager == 1)
5385 popt->topt.pager = 0;
5386 else
5387 popt->topt.pager = 1;
5388 }
5389
5390 /* set minimum lines for pager use */
5391 else if (strcmp(param, "pager_min_lines") == 0)
5392 {
5393 if (value &&
5394 !ParseVariableNum(value, "pager_min_lines", &popt->topt.pager_min_lines))
5395 return false;
5396 }
5397
5398 /* disable "(x rows)" footer */
5399 else if (strcmp(param, "footer") == 0)
5400 {
5401 if (value)
5402 return ParseVariableBool(value, param, &popt->topt.default_footer);
5403 else
5404 popt->topt.default_footer = !popt->topt.default_footer;
5405 }
5406
5407 /* set border style/width */
5408 else if (strcmp(param, "columns") == 0)
5409 {
5410 if (value)
5411 popt->topt.columns = atoi(value);
5412 }
5413 else
5414 {
5415 pg_log_error("\\pset: unknown option: %s", param);
5416 return false;
5417 }
5418
5419 if (!quiet)
5420 printPsetInfo(param, &pset.popt);
5421
5422 return true;
5423}
#define lengthof(array)
Definition: c.h:788
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:5033
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:5429
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3691
const printTextFormat pg_asciiformat
Definition: print.c:56
const printTextFormat pg_asciiformat_old
Definition: print.c:77
printTextFormat pg_utf8format
Definition: print.c:99
@ PRINT_XHEADER_EXACT_WIDTH
Definition: print.h:78
@ PRINT_XHEADER_PAGE
Definition: print.h:76
@ PRINT_XHEADER_COLUMN
Definition: print.h:74
@ PRINT_XHEADER_FULL
Definition: print.h:72
printFormat
Definition: print.h:29
#define free(a)
Definition: header.h:65
static struct @166 value
int i
Definition: isn.c:77
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
printQueryOpt popt
Definition: settings.h:112
printTableOpt topt
Definition: print.h:185
char * nullPrint
Definition: print.h:186
char * title
Definition: print.h:187
unsigned short int expanded
Definition: print.h:114
unicode_linestyle unicode_border_linestyle
Definition: print.h:141
bool tuples_only
Definition: print.h:126
int columns
Definition: print.h:140
enum printFormat format
Definition: print.h:113
struct separator fieldSep
Definition: print.h:132
int expanded_header_exact_width
Definition: print.h:118
struct separator recordSep
Definition: print.h:133
printXheaderWidthType expanded_header_width_type
Definition: print.h:116
char csvFieldSep[2]
Definition: print.h:134
const printTextFormat * line_style
Definition: print.h:131
bool default_footer
Definition: print.h:129
int pager_min_lines
Definition: print.h:124
unsigned short int pager
Definition: print.h:122
char * tableAttr
Definition: print.h:137
bool numericLocale
Definition: print.h:135
unsigned short int border
Definition: print.h:120
unicode_linestyle unicode_header_linestyle
Definition: print.h:143
unicode_linestyle unicode_column_linestyle
Definition: print.h:142
bool separator_zero
Definition: print.h:108
char * separator
Definition: print.h:107
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:486
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:109
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:158
const char * name

References Assert(), printTableOpt::border, printTableOpt::columns, printTableOpt::csvFieldSep, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::expanded_header_exact_width, printTableOpt::expanded_header_width_type, printTableOpt::fieldSep, printTableOpt::format, free, i, lengthof, printTableOpt::line_style, name, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, ParseVariableBool(), ParseVariableNum(), pg_asciiformat, pg_asciiformat_old, pg_log_error, pg_strcasecmp(), pg_strdup(), pg_strncasecmp(), pg_utf8format, _psqlSettings::popt, PRINT_ALIGNED, PRINT_ASCIIDOC, PRINT_CSV, PRINT_HTML, PRINT_LATEX, PRINT_LATEX_LONGTABLE, PRINT_TROFF_MS, PRINT_UNALIGNED, PRINT_WRAPPED, PRINT_XHEADER_COLUMN, PRINT_XHEADER_EXACT_WIDTH, PRINT_XHEADER_FULL, PRINT_XHEADER_PAGE, printPsetInfo(), pset, PsqlVarEnumError(), printTableOpt::recordSep, refresh_utf8format(), separator::separator, separator::separator_zero, set_unicode_line_style(), printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, printTableOpt::tuples_only, printTableOpt::unicode_border_linestyle, printTableOpt::unicode_column_linestyle, printTableOpt::unicode_header_linestyle, and value.

Referenced by exec_command_a(), exec_command_C(), exec_command_f(), exec_command_html(), exec_command_pset(), exec_command_t(), exec_command_T(), exec_command_x(), parse_psql_options(), and process_command_g_options().

◆ do_shell()

static bool do_shell ( const char *  command)
static

Definition at line 5825 of file command.c.

5826{
5827 int result;
5828
5829 fflush(NULL);
5830 if (!command)
5831 {
5832 char *sys;
5833 const char *shellName;
5834
5835 shellName = getenv("SHELL");
5836#ifdef WIN32
5837 if (shellName == NULL)
5838 shellName = getenv("COMSPEC");
5839#endif
5840 if (shellName == NULL)
5841 shellName = DEFAULT_SHELL;
5842
5843 /* See EDITOR handling comment for an explanation */
5844#ifndef WIN32
5845 sys = psprintf("exec %s", shellName);
5846#else
5847 sys = psprintf("\"%s\"", shellName);
5848#endif
5849 result = system(sys);
5850 free(sys);
5851 }
5852 else
5853 result = system(command);
5854
5856
5857 if (result == 127 || result == -1)
5858 {
5859 pg_log_error("\\!: failed");
5860 return false;
5861 }
5862 return true;
5863}
void SetShellResultVariables(int wait_result)
Definition: common.c:516
#define DEFAULT_SHELL
Definition: command.c:5815
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43

References DEFAULT_SHELL, free, pg_log_error, psprintf(), and SetShellResultVariables().

Referenced by exec_command_shell_escape().

◆ do_watch()

static bool do_watch ( PQExpBuffer  query_buf,
double  sleep,
int  iter,
int  min_rows 
)
static

Definition at line 5876 of file command.c.

5877{
5878 long sleep_ms = (long) (sleep * 1000);
5879 printQueryOpt myopt = pset.popt;
5880 const char *strftime_fmt;
5881 const char *user_title;
5882 char *title;
5883 const char *pagerprog = NULL;
5884 FILE *pagerpipe = NULL;
5885 int title_len;
5886 int res = 0;
5887 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
5895 if (!query_buf || query_buf->len <= 0)
5896 {
5897 pg_log_error("\\watch cannot be used with an empty query");
5898 return false;
5899 }
5900
5901#ifndef WIN32
5902 sigemptyset(&sigalrm_sigchld_sigint);
5903 sigaddset(&sigalrm_sigchld_sigint, SIGCHLD);
5904 sigaddset(&sigalrm_sigchld_sigint, SIGALRM);
5905 sigaddset(&sigalrm_sigchld_sigint, SIGINT);
5906
5907 sigemptyset(&sigalrm_sigchld);
5908 sigaddset(&sigalrm_sigchld, SIGCHLD);
5909 sigaddset(&sigalrm_sigchld, SIGALRM);
5910
5911 sigemptyset(&sigint);
5912 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 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 interval.it_value.tv_sec = sleep_ms / 1000;
5925 interval.it_value.tv_usec = (sleep_ms % 1000) * 1000;
5926 interval.it_interval = interval.it_value;
5927 if (setitimer(ITIMER_REAL, &interval, NULL) < 0)
5928 {
5929 pg_log_error("could not set timer: %m");
5930 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
5943 pagerprog = getenv("PSQL_WATCH_PAGER");
5944 /* if variable is empty or all-white-space, don't use pager */
5945 if (pagerprog && strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
5946 pagerprog = NULL;
5947#endif
5948 if (pagerprog && myopt.topt.pager &&
5949 isatty(fileno(stdin)) && isatty(fileno(stdout)))
5950 {
5951 fflush(NULL);
5953 pagerpipe = popen(pagerprog, "w");
5954
5955 if (!pagerpipe)
5956 /* silently proceed without pager */
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 */
5965 strftime_fmt = "%c";
5966
5967 /*
5968 * Set up rendering options, in particular, disable the pager unless
5969 * PSQL_WATCH_PAGER was successfully launched.
5970 */
5971 if (!pagerpipe)
5972 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 */
5979 user_title = myopt.title;
5980 title_len = (user_title ? strlen(user_title) : 0) + 256;
5981 title = pg_malloc(title_len);
5982
5983 /* Loop to run query and then sleep awhile */
5984 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 */
5994 timer = time(NULL);
5995 strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
5996
5997 if (user_title)
5998 snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
5999 user_title, timebuf, sleep_ms / 1000.0);
6000 else
6001 snprintf(title, title_len, _("%s (every %gs)\n"),
6002 timebuf, sleep_ms / 1000.0);
6003 myopt.title = title;
6004
6005 /* Run the query and print out the result */
6006 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 */
6012 if (res <= 0)
6013 break;
6014
6015 /* If we have iteration count, check that it's not exceeded yet */
6016 if (iter && (--iter <= 0))
6017 break;
6018
6019 /* Quit if error on pager pipe (probably pager has quit) */
6020 if (pagerpipe && ferror(pagerpipe))
6021 break;
6022
6023 /* Tight loop, no wait needed */
6024 if (sleep_ms == 0)
6025 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. */
6048 sigprocmask(SIG_BLOCK, &sigint, NULL);
6049 if (cancel_pressed)
6050 done = true;
6051
6052 /* Wait for SIGINT, SIGCHLD or SIGALRM. */
6053 while (!done)
6054 {
6055 int signal_received;
6056
6057 errno = sigwait(&sigalrm_sigchld_sigint, &signal_received);
6058 if (errno != 0)
6059 {
6060 /* Some other signal arrived? */
6061 if (errno == EINTR)
6062 continue;
6063 else
6064 {
6065 pg_log_error("could not wait for signals: %m");
6066 done = true;
6067 break;
6068 }
6069 }
6070 /* On ^C or pager exit, it's time to stop running the query. */
6071 if (signal_received == SIGINT || signal_received == SIGCHLD)
6072 done = true;
6073 /* Otherwise, we must have SIGALRM. Time to run the query again. */
6074 break;
6075 }
6076
6077 /* Unblock SIGINT so that slow queries can be interrupted. */
6078 sigprocmask(SIG_UNBLOCK, &sigint, NULL);
6079#endif
6080 }
6081
6082 if (pagerpipe)
6083 {
6084 pclose(pagerpipe);
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 */
6096 fprintf(stdout, "\n");
6097 fflush(stdout);
6098 }
6099
6100#ifndef WIN32
6101 /* Disable the interval timer. */
6102 memset(&interval, 0, sizeof(interval));
6104 /* Unblock SIGINT, SIGCHLD and SIGALRM. */
6105 sigprocmask(SIG_UNBLOCK, &sigalrm_sigchld_sigint, NULL);
6106#endif
6107
6108 pg_free(title);
6109 return (res >= 0);
6110}
int PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout, int min_rows)
Definition: common.c:710
#define Min(x, y)
Definition: c.h:1004
#define fprintf(file, fmt, msg)
Definition: cubescan.l:21
void restore_sigpipe_trap(void)
Definition: print.c:3062
void disable_sigpipe_trap(void)
Definition: print.c:3039
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
void pg_usleep(long microsec)
Definition: signal.c:53
static long sleep_ms
Definition: slotsync.c:116
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: timer.c:86
#define SIGCHLD
Definition: win32_port.h:168
#define EINTR
Definition: win32_port.h:364
#define SIGALRM
Definition: win32_port.h:164
#define ITIMER_REAL
Definition: win32_port.h:180

References _, cancel_pressed, PQExpBufferData::data, disable_sigpipe_trap(), EINTR, fprintf, i, ITIMER_REAL, PQExpBufferData::len, Min, printTableOpt::pager, pg_free(), pg_log_error, pg_malloc(), pg_usleep(), _psqlSettings::popt, pset, PSQLexecWatch(), restore_sigpipe_trap(), setitimer(), SIGALRM, SIGCHLD, sleep_ms, snprintf, generate_unaccent_rules::stdout, printQueryOpt::title, and printQueryOpt::topt.

Referenced by exec_command_watch().

◆ echo_hidden_command()

static bool echo_hidden_command ( const char *  query)
static

Definition at line 6117 of file command.c.

6118{
6120 {
6121 printf(_("/******** QUERY *********/\n"
6122 "%s\n"
6123 "/************************/\n\n"), query);
6124 fflush(stdout);
6125 if (pset.logfile)
6126 {
6128 _("/******** QUERY *********/\n"
6129 "%s\n"
6130 "/************************/\n\n"), query);
6131 fflush(pset.logfile);
6132 }
6133
6135 return false;
6136 }
6137 return true;
6138}
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:53
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:51
FILE * logfile
Definition: settings.h:149
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:177

References _, _psqlSettings::echo_hidden, fprintf, _psqlSettings::logfile, printf, pset, PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, and generate_unaccent_rules::stdout.

Referenced by get_create_object_cmd(), and lookup_object_oid().

◆ editFile()

static bool editFile ( const char *  fname,
int  lineno 
)
static

Definition at line 4646 of file command.c.

4647{
4648 const char *editorName;
4649 const char *editor_lineno_arg = NULL;
4650 char *sys;
4651 int result;
4652
4653 Assert(fname != NULL);
4654
4655 /* Find an editor to use */
4656 editorName = getenv("PSQL_EDITOR");
4657 if (!editorName)
4658 editorName = getenv("EDITOR");
4659 if (!editorName)
4660 editorName = getenv("VISUAL");
4661 if (!editorName)
4662 editorName = DEFAULT_EDITOR;
4663
4664 /* Get line number argument, if we need it. */
4665 if (lineno > 0)
4666 {
4667 editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
4668#ifdef DEFAULT_EDITOR_LINENUMBER_ARG
4669 if (!editor_lineno_arg)
4670 editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
4671#endif
4672 if (!editor_lineno_arg)
4673 {
4674 pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
4675 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 if (lineno > 0)
4688 sys = psprintf("exec %s %s%d '%s'",
4689 editorName, editor_lineno_arg, lineno, fname);
4690 else
4691 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
4701 fflush(NULL);
4702 result = system(sys);
4703 if (result == -1)
4704 pg_log_error("could not start editor \"%s\"", editorName);
4705 else if (result == 127)
4706 pg_log_error("could not start /bin/sh");
4707 free(sys);
4708
4709 return result == 0;
4710}
#define DEFAULT_EDITOR_LINENUMBER_ARG
Definition: settings.h:23
#define DEFAULT_EDITOR
Definition: settings.h:22

References Assert(), DEFAULT_EDITOR, DEFAULT_EDITOR_LINENUMBER_ARG, free, pg_log_error, and psprintf().

Referenced by do_edit().

◆ exec_command()

static backslashResult exec_command ( const char *  cmd,
PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 315 of file command.c.

320{
321 backslashResult status;
322 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 if (pset.cur_cmd_interactive && !active_branch &&
333 {
334 pg_log_warning("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block",
335 cmd);
336 }
337
338 if (strcmp(cmd, "a") == 0)
339 status = exec_command_a(scan_state, active_branch);
340 else if (strcmp(cmd, "bind") == 0)
341 status = exec_command_bind(scan_state, active_branch);
342 else if (strcmp(cmd, "bind_named") == 0)
343 status = exec_command_bind_named(scan_state, active_branch, cmd);
344 else if (strcmp(cmd, "C") == 0)
345 status = exec_command_C(scan_state, active_branch);
346 else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
347 status = exec_command_connect(scan_state, active_branch);
348 else if (strcmp(cmd, "cd") == 0)
349 status = exec_command_cd(scan_state, active_branch, cmd);
350 else if (strcmp(cmd, "close_prepared") == 0)
351 status = exec_command_close_prepared(scan_state, active_branch, cmd);
352 else if (strcmp(cmd, "conninfo") == 0)
353 status = exec_command_conninfo(scan_state, active_branch);
354 else if (pg_strcasecmp(cmd, "copy") == 0)
355 status = exec_command_copy(scan_state, active_branch);
356 else if (strcmp(cmd, "copyright") == 0)
357 status = exec_command_copyright(scan_state, active_branch);
358 else if (strcmp(cmd, "crosstabview") == 0)
359 status = exec_command_crosstabview(scan_state, active_branch);
360 else if (cmd[0] == 'd')
361 status = exec_command_d(scan_state, active_branch, cmd);
362 else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
363 status = exec_command_edit(scan_state, active_branch,
364 query_buf, previous_buf);
365 else if (strcmp(cmd, "ef") == 0)
366 status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
367 else if (strcmp(cmd, "ev") == 0)
368 status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
369 else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0 ||
370 strcmp(cmd, "warn") == 0)
371 status = exec_command_echo(scan_state, active_branch, cmd);
372 else if (strcmp(cmd, "elif") == 0)
373 status = exec_command_elif(scan_state, cstack, query_buf);
374 else if (strcmp(cmd, "else") == 0)
375 status = exec_command_else(scan_state, cstack, query_buf);
376 else if (strcmp(cmd, "endif") == 0)
377 status = exec_command_endif(scan_state, cstack, query_buf);
378 else if (strcmp(cmd, "endpipeline") == 0)
379 status = exec_command_endpipeline(scan_state, active_branch);
380 else if (strcmp(cmd, "encoding") == 0)
381 status = exec_command_encoding(scan_state, active_branch);
382 else if (strcmp(cmd, "errverbose") == 0)
383 status = exec_command_errverbose(scan_state, active_branch);
384 else if (strcmp(cmd, "f") == 0)
385 status = exec_command_f(scan_state, active_branch);
386 else if (strcmp(cmd, "flush") == 0)
387 status = exec_command_flush(scan_state, active_branch);
388 else if (strcmp(cmd, "flushrequest") == 0)
389 status = exec_command_flushrequest(scan_state, active_branch);
390 else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
391 status = exec_command_g(scan_state, active_branch, cmd);
392 else if (strcmp(cmd, "gdesc") == 0)
393 status = exec_command_gdesc(scan_state, active_branch);
394 else if (strcmp(cmd, "getenv") == 0)
395 status = exec_command_getenv(scan_state, active_branch, cmd);
396 else if (strcmp(cmd, "getresults") == 0)
397 status = exec_command_getresults(scan_state, active_branch);
398 else if (strcmp(cmd, "gexec") == 0)
399 status = exec_command_gexec(scan_state, active_branch);
400 else if (strcmp(cmd, "gset") == 0)
401 status = exec_command_gset(scan_state, active_branch);
402 else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
403 status = exec_command_help(scan_state, active_branch);
404 else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
405 status = exec_command_html(scan_state, active_branch);
406 else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
407 strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
408 status = exec_command_include(scan_state, active_branch, cmd);
409 else if (strcmp(cmd, "if") == 0)
410 status = exec_command_if(scan_state, cstack, query_buf);
411 else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
412 strcmp(cmd, "lx") == 0 || strcmp(cmd, "listx") == 0 ||
413 strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0 ||
414 strcmp(cmd, "lx+") == 0 || strcmp(cmd, "listx+") == 0 ||
415 strcmp(cmd, "l+x") == 0 || strcmp(cmd, "list+x") == 0)
416 status = exec_command_list(scan_state, active_branch, cmd);
417 else if (strncmp(cmd, "lo_", 3) == 0)
418 status = exec_command_lo(scan_state, active_branch, cmd);
419 else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
420 status = exec_command_out(scan_state, active_branch);
421 else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
422 status = exec_command_print(scan_state, active_branch,
423 query_buf, previous_buf);
424 else if (strcmp(cmd, "parse") == 0)
425 status = exec_command_parse(scan_state, active_branch, cmd);
426 else if (strcmp(cmd, "password") == 0)
427 status = exec_command_password(scan_state, active_branch);
428 else if (strcmp(cmd, "prompt") == 0)
429 status = exec_command_prompt(scan_state, active_branch, cmd);
430 else if (strcmp(cmd, "pset") == 0)
431 status = exec_command_pset(scan_state, active_branch);
432 else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
433 status = exec_command_quit(scan_state, active_branch);
434 else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
435 status = exec_command_reset(scan_state, active_branch, query_buf);
436 else if (strcmp(cmd, "restrict") == 0)
437 status = exec_command_restrict(scan_state, active_branch, cmd);
438 else if (strcmp(cmd, "s") == 0)
439 status = exec_command_s(scan_state, active_branch);
440 else if (strcmp(cmd, "sendpipeline") == 0)
441 status = exec_command_sendpipeline(scan_state, active_branch);
442 else if (strcmp(cmd, "set") == 0)
443 status = exec_command_set(scan_state, active_branch);
444 else if (strcmp(cmd, "setenv") == 0)
445 status = exec_command_setenv(scan_state, active_branch, cmd);
446 else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
447 status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
448 else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
449 status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
450 else if (strcmp(cmd, "startpipeline") == 0)
451 status = exec_command_startpipeline(scan_state, active_branch);
452 else if (strcmp(cmd, "syncpipeline") == 0)
453 status = exec_command_syncpipeline(scan_state, active_branch);
454 else if (strcmp(cmd, "t") == 0)
455 status = exec_command_t(scan_state, active_branch);
456 else if (strcmp(cmd, "T") == 0)
457 status = exec_command_T(scan_state, active_branch);
458 else if (strcmp(cmd, "timing") == 0)
459 status = exec_command_timing(scan_state, active_branch);
460 else if (strcmp(cmd, "unrestrict") == 0)
461 status = exec_command_unrestrict(scan_state, active_branch, cmd);
462 else if (strcmp(cmd, "unset") == 0)
463 status = exec_command_unset(scan_state, active_branch, cmd);
464 else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
465 status = exec_command_write(scan_state, active_branch, cmd,
466 query_buf, previous_buf);
467 else if (strcmp(cmd, "watch") == 0)
468 status = exec_command_watch(scan_state, active_branch,
469 query_buf, previous_buf);
470 else if (strcmp(cmd, "x") == 0)
471 status = exec_command_x(scan_state, active_branch);
472 else if (strcmp(cmd, "z") == 0 ||
473 strcmp(cmd, "zS") == 0 || strcmp(cmd, "zx") == 0 ||
474 strcmp(cmd, "zSx") == 0 || strcmp(cmd, "zxS") == 0)
475 status = exec_command_z(scan_state, active_branch, cmd);
476 else if (strcmp(cmd, "!") == 0)
477 status = exec_command_shell_escape(scan_state, active_branch);
478 else if (strcmp(cmd, "?") == 0)
479 status = exec_command_slash_command_help(scan_state, active_branch);
480 else
481 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 if (status == PSQL_CMD_SEND)
489 (void) copy_previous_query(query_buf, previous_buf);
490
491 return status;
492}
static backslashResult exec_command_startpipeline(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3059
static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2291
static backslashResult exec_command_getresults(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1929
static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch)
Definition: command.c:985
static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1643
static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2482
static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2044
static bool copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:3846
static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1739
static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3138
static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1875
static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2024
static backslashResult exec_command_close_prepared(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:755
static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch)
Definition: command.c:501
static backslashResult exec_command_flushrequest(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1714
static backslashResult exec_command_bind(PsqlScanState scan_state, bool active_branch)
Definition: command.c:520
static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2542
static backslashResult exec_command_bind_named(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:556
static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3520
static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2104
static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2150
static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1673
static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:2764
static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2459
static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1559
static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1021
static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3579
static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:691
static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1988
static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, bool is_func)
Definition: command.c:1443
static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:3232
static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2875
static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2811
static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2618
static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:3364
static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2063
static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2331
static backslashResult exec_command_sendpipeline(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2838
static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3160
static backslashResult exec_command_restrict(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2782
static backslashResult exec_command_unrestrict(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:3192
static backslashResult exec_command_endpipeline(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3097
static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch)
Definition: command.c:605
static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1604
static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:1347
static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2226
static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
Definition: command.c:788
static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch)
Definition: command.c:963
static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3601
static backslashResult exec_command_parse(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2508
static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
Definition: command.c:997
static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2695
static backslashResult exec_command_flush(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1695
static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3116
static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:3262
static backslashResult exec_command_syncpipeline(PsqlScanState scan_state, bool active_branch)
Definition: command.c:3078
static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch, const char *cmd, bool is_func)
Definition: command.c:2976
static bool is_branching_command(const char *cmd)
Definition: command.c:3784
static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch)
Definition: command.c:638
static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1965
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:3542
static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2368
static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1892
static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2750
static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2928
@ PSQL_CMD_UNKNOWN
Definition: command.h:17
@ PSQL_CMD_SEND
Definition: command.h:18
enum _backslashResult backslashResult
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:140
#define pg_log_warning(...)
Definition: pgfnames.c:24

References conditional_active(), copy_previous_query(), _psqlSettings::cur_cmd_interactive, exec_command_a(), exec_command_bind(), exec_command_bind_named(), exec_command_C(), exec_command_cd(), exec_command_close_prepared(), exec_command_connect(), exec_command_conninfo(), exec_command_copy(), exec_command_copyright(), exec_command_crosstabview(), exec_command_d(), exec_command_echo(), exec_command_edit(), exec_command_ef_ev(), exec_command_elif(), exec_command_else(), exec_command_encoding(), exec_command_endif(), exec_command_endpipeline(), exec_command_errverbose(), exec_command_f(), exec_command_flush(), exec_command_flushrequest(), exec_command_g(), exec_command_gdesc(), exec_command_getenv(), exec_command_getresults(), exec_command_gexec(), exec_command_gset(), exec_command_help(), exec_command_html(), exec_command_if(), exec_command_include(), exec_command_list(), exec_command_lo(), exec_command_out(), exec_command_parse(), exec_command_password(), exec_command_print(), exec_command_prompt(), exec_command_pset(), exec_command_quit(), exec_command_reset(), exec_command_restrict(), exec_command_s(), exec_command_sendpipeline(), exec_command_set(), exec_command_setenv(), exec_command_sf_sv(), exec_command_shell_escape(), exec_command_slash_command_help(), exec_command_startpipeline(), exec_command_syncpipeline(), exec_command_t(), exec_command_T(), exec_command_timing(), exec_command_unrestrict(), exec_command_unset(), exec_command_watch(), exec_command_write(), exec_command_x(), exec_command_z(), is_branching_command(), pg_log_warning, pg_strcasecmp(), pset, PSQL_CMD_SEND, and PSQL_CMD_UNKNOWN.

Referenced by HandleSlashCmds().

◆ exec_command_a()

static backslashResult exec_command_a ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 501 of file command.c.

502{
503 bool success = true;
504
505 if (active_branch)
506 {
508 success = do_pset("format", "aligned", &pset.popt, pset.quiet);
509 else
510 success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
511 }
512
514}
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:5076
@ PSQL_CMD_ERROR
Definition: command.h:22
@ PSQL_CMD_SKIP_LINE
Definition: command.h:19

References do_pset(), printTableOpt::format, _psqlSettings::popt, PRINT_ALIGNED, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, _psqlSettings::quiet, success, and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_bind()

static backslashResult exec_command_bind ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 520 of file command.c.

521{
523
524 if (active_branch)
525 {
526 char *opt;
527 int nparams = 0;
528 int nalloc = 0;
529
531
532 while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
533 {
534 nparams++;
535 if (nparams > nalloc)
536 {
537 nalloc = nalloc ? nalloc * 2 : 1;
539 }
540 pset.bind_params[nparams - 1] = opt;
541 }
542
543 pset.bind_nparams = nparams;
545 }
546 else
547 ignore_slash_options(scan_state);
548
549 return status;
550}
void clean_extended_state(void)
Definition: common.c:2660
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:3735
#define pg_realloc_array(pointer, type, count)
Definition: fe_memutils.h:63
@ OT_NORMAL
Definition: psqlscanslash.h:17
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
@ PSQL_SEND_EXTENDED_QUERY_PARAMS
Definition: settings.h:76
char ** bind_params
Definition: settings.h:123
PSQL_SEND_MODE send_mode
Definition: settings.h:120
int bind_nparams
Definition: settings.h:122

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, clean_extended_state(), ignore_slash_options(), OT_NORMAL, pg_realloc_array, pset, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQL_SEND_EXTENDED_QUERY_PARAMS, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_bind_named()

static backslashResult exec_command_bind_named ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 556 of file command.c.

558{
560
561 if (active_branch)
562 {
563 char *opt;
564 int nparams = 0;
565 int nalloc = 0;
566
568
569 /* get the mandatory prepared statement name */
570 opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
571 if (!opt)
572 {
573 pg_log_error("\\%s: missing required argument", cmd);
574 status = PSQL_CMD_ERROR;
575 }
576 else
577 {
578 pset.stmtName = opt;
580
581 /* set of parameters */
582 while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
583 {
584 nparams++;
585 if (nparams > nalloc)
586 {
587 nalloc = nalloc ? nalloc * 2 : 1;
589 }
590 pset.bind_params[nparams - 1] = opt;
591 }
592 pset.bind_nparams = nparams;
593 }
594 }
595 else
596 ignore_slash_options(scan_state);
597
598 return status;
599}
@ PSQL_SEND_EXTENDED_QUERY_PREPARED
Definition: settings.h:77
char * stmtName
Definition: settings.h:124

References _psqlSettings::bind_nparams, _psqlSettings::bind_params, clean_extended_state(), ignore_slash_options(), OT_NORMAL, pg_log_error, pg_realloc_array, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQL_SEND_EXTENDED_QUERY_PREPARED, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command().

◆ exec_command_C()

static backslashResult exec_command_C ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 605 of file command.c.

606{
607 bool success = true;
608
609 if (active_branch)
610 {
611 char *opt = psql_scan_slash_option(scan_state,
612 OT_NORMAL, NULL, true);
613
614 success = do_pset("title", opt, &pset.popt, pset.quiet);
615 free(opt);
616 }
617 else
618 ignore_slash_options(scan_state);
619
621}

References do_pset(), free, ignore_slash_options(), OT_NORMAL, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, and success.

Referenced by exec_command().

◆ exec_command_cd()

static backslashResult exec_command_cd ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 691 of file command.c.

692{
693 bool success = true;
694
695 if (active_branch)
696 {
697 char *opt = psql_scan_slash_option(scan_state,
698 OT_NORMAL, NULL, true);
699 char *dir;
700
701 if (opt)
702 dir = opt;
703 else
704 {
705#ifndef WIN32
706 /* This should match get_home_path() */
707 dir = getenv("HOME");
708 if (dir == NULL || dir[0] == '\0')
709 {
710 uid_t user_id = geteuid();
711 struct passwd *pw;
712
713 errno = 0; /* clear errno before call */
714 pw = getpwuid(user_id);
715 if (pw)
716 dir = pw->pw_dir;
717 else
718 {
719 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 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 if (success &&
736 chdir(dir) < 0)
737 {
738 pg_log_error("\\%s: could not change directory to \"%s\": %m",
739 cmd, dir);
740 success = false;
741 }
742
743 free(opt);
744 }
745 else
746 ignore_slash_options(scan_state);
747
749}
int uid_t
Definition: win32_port.h:234

References _, free, ignore_slash_options(), OT_NORMAL, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), strerror, and success.

Referenced by exec_command().

◆ exec_command_close_prepared()

static backslashResult exec_command_close_prepared ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 755 of file command.c.

756{
758
759 if (active_branch)
760 {
761 char *opt = psql_scan_slash_option(scan_state,
762 OT_NORMAL, NULL, false);
763
765
766 if (!opt)
767 {
768 pg_log_error("\\%s: missing required argument", cmd);
769 status = PSQL_CMD_ERROR;
770 }
771 else
772 {
773 pset.stmtName = opt;
775 status = PSQL_CMD_SEND;
776 }
777 }
778 else
779 ignore_slash_options(scan_state);
780
781 return status;
782}
@ PSQL_SEND_EXTENDED_CLOSE
Definition: settings.h:74

References clean_extended_state(), ignore_slash_options(), OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQL_SEND_EXTENDED_CLOSE, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command().

◆ exec_command_connect()

static backslashResult exec_command_connect ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 638 of file command.c.

639{
640 bool success = true;
641
642 if (active_branch)
643 {
644 static const char prefix[] = "-reuse-previous=";
645 char *opt1,
646 *opt2,
647 *opt3,
648 *opt4;
649 enum trivalue reuse_previous = TRI_DEFAULT;
650
651 opt1 = read_connect_arg(scan_state);
652 if (opt1 != NULL && strncmp(opt1, prefix, sizeof(prefix) - 1) == 0)
653 {
654 bool on_off;
655
656 success = ParseVariableBool(opt1 + sizeof(prefix) - 1,
657 "-reuse-previous",
658 &on_off);
659 if (success)
660 {
661 reuse_previous = on_off ? TRI_YES : TRI_NO;
662 free(opt1);
663 opt1 = read_connect_arg(scan_state);
664 }
665 }
666
667 if (success) /* give up if reuse_previous was invalid */
668 {
669 opt2 = read_connect_arg(scan_state);
670 opt3 = read_connect_arg(scan_state);
671 opt4 = read_connect_arg(scan_state);
672
673 success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
674
675 free(opt2);
676 free(opt3);
677 free(opt4);
678 }
679 free(opt1);
680 }
681 else
682 ignore_slash_options(scan_state);
683
685}
static bool do_connect(enum trivalue reuse_previous_specification, char *dbname, char *user, char *host, char *port)
Definition: command.c:3915
static char * read_connect_arg(PsqlScanState scan_state)
Definition: command.c:3632
trivalue
Definition: vacuumlo.c:35
@ TRI_DEFAULT
Definition: vacuumlo.c:36

References do_connect(), free, ignore_slash_options(), ParseVariableBool(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, read_connect_arg(), success, TRI_DEFAULT, TRI_NO, and TRI_YES.

Referenced by exec_command().

◆ exec_command_conninfo()

static backslashResult exec_command_conninfo ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 788 of file command.c.

789{
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
805 if (!active_branch)
806 return PSQL_CMD_SKIP_LINE;
807
808 db = PQdb(pset.db);
809 if (db == NULL)
810 {
811 printf(_("You are currently not connected to a database.\n"));
812 return PSQL_CMD_SKIP_LINE;
813 }
814
815 /* Get values for the parameters */
816 host = PQhost(pset.db);
817 hostaddr = PQhostaddr(pset.db);
818 version_num = PQfullProtocolVersion(pset.db);
819 protocol_version = psprintf("%d.%d", version_num / 10000,
820 version_num % 10000);
821 ssl_in_use = PQsslInUse(pset.db);
822 password_used = PQconnectionUsedPassword(pset.db);
823 gssapi_used = PQconnectionUsedGSSAPI(pset.db);
824 backend_pid = psprintf("%d", PQbackendPID(pset.db));
825
826 /* Only print hostaddr if it differs from host, and not if unixsock */
827 print_hostaddr = (!is_unixsock_path(host) &&
828 hostaddr && *hostaddr && strcmp(host, hostaddr) != 0);
829
830 /* Determine the exact number of rows to print */
831 rows = 12;
832 cols = 2;
833 if (ssl_in_use)
834 rows += 6;
835 if (print_hostaddr)
836 rows++;
837
838 /* Set it all up */
839 printTableInit(&cont, &pset.popt.topt, _("Connection Information"), cols, rows);
840 printTableAddHeader(&cont, _("Parameter"), true, 'l');
841 printTableAddHeader(&cont, _("Value"), true, 'l');
842
843 /* Database */
844 printTableAddCell(&cont, _("Database"), false, false);
845 printTableAddCell(&cont, db, false, false);
846
847 /* Client User */
848 printTableAddCell(&cont, _("Client User"), false, false);
849 printTableAddCell(&cont, PQuser(pset.db), false, false);
850
851 /* Host/hostaddr/socket */
852 if (is_unixsock_path(host))
853 {
854 /* hostaddr if specified overrides socket, so suppress the latter */
855 if (hostaddr && *hostaddr)
856 {
857 printTableAddCell(&cont, _("Host Address"), false, false);
858 printTableAddCell(&cont, hostaddr, false, false);
859 }
860 else
861 {
862 printTableAddCell(&cont, _("Socket Directory"), false, false);
863 printTableAddCell(&cont, host, false, false);
864 }
865 }
866 else
867 {
868 printTableAddCell(&cont, _("Host"), false, false);
869 printTableAddCell(&cont, host, false, false);
870 if (print_hostaddr)
871 {
872 printTableAddCell(&cont, _("Host Address"), false, false);
873 printTableAddCell(&cont, hostaddr, false, false);
874 }
875 }
876
877 /* Server Port */
878 printTableAddCell(&cont, _("Server Port"), false, false);
879 printTableAddCell(&cont, PQport(pset.db), false, false);
880
881 /* Options */
882 printTableAddCell(&cont, _("Options"), false, false);
883 printTableAddCell(&cont, PQoptions(pset.db), false, false);
884
885 /* Protocol Version */
886 printTableAddCell(&cont, _("Protocol Version"), false, false);
887 printTableAddCell(&cont, protocol_version, false, false);
888
889 /* Password Used */
890 printTableAddCell(&cont, _("Password Used"), false, false);
891 printTableAddCell(&cont, password_used ? _("true") : _("false"), false, false);
892
893 /* GSSAPI Authenticated */
894 printTableAddCell(&cont, _("GSSAPI Authenticated"), false, false);
895 printTableAddCell(&cont, gssapi_used ? _("true") : _("false"), false, false);
896
897 /* Backend PID */
898 printTableAddCell(&cont, _("Backend PID"), false, false);
899 printTableAddCell(&cont, backend_pid, false, false);
900
901 /* SSL Connection */
902 printTableAddCell(&cont, _("SSL Connection"), false, false);
903 printTableAddCell(&cont, ssl_in_use ? _("true") : _("false"), false, false);
904
905 /* SSL Information */
906 if (ssl_in_use)
907 {
908 char *library,
909 *protocol,
910 *key_bits,
911 *cipher,
912 *compression,
913 *alpn;
914
915 library = (char *) PQsslAttribute(pset.db, "library");
916 protocol = (char *) PQsslAttribute(pset.db, "protocol");
917 key_bits = (char *) PQsslAttribute(pset.db, "key_bits");
918 cipher = (char *) PQsslAttribute(pset.db, "cipher");
919 compression = (char *) PQsslAttribute(pset.db, "compression");
920 alpn = (char *) PQsslAttribute(pset.db, "alpn");
921
922 printTableAddCell(&cont, _("SSL Library"), false, false);
923 printTableAddCell(&cont, library ? library : _("unknown"), false, false);
924
925 printTableAddCell(&cont, _("SSL Protocol"), false, false);
926 printTableAddCell(&cont, protocol ? protocol : _("unknown"), false, false);
927
928 printTableAddCell(&cont, _("SSL Key Bits"), false, false);
929 printTableAddCell(&cont, key_bits ? key_bits : _("unknown"), false, false);
930
931 printTableAddCell(&cont, _("SSL Cipher"), false, false);
932 printTableAddCell(&cont, cipher ? cipher : _("unknown"), false, false);
933
934 printTableAddCell(&cont, _("SSL Compression"), false, false);
935 printTableAddCell(&cont, (compression && strcmp(compression, "off") != 0) ?
936 _("true") : _("false"), false, false);
937
938 printTableAddCell(&cont, _("ALPN"), false, false);
939 printTableAddCell(&cont, (alpn && alpn[0] != '\0') ? alpn : _("none"), false, false);
940 }
941
942 paramval = (char *) PQparameterStatus(pset.db, "is_superuser");
943 printTableAddCell(&cont, "Superuser", false, false);
944 printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false);
945
946 paramval = (char *) PQparameterStatus(pset.db, "in_hot_standby");
947 printTableAddCell(&cont, "Hot Standby", false, false);
948 printTableAddCell(&cont, paramval ? paramval : _("unknown"), false, false);
949
950 printTable(&cont, pset.queryFout, false, pset.logfile);
951 printTableCleanup(&cont);
952
953 pfree(protocol_version);
954 pfree(backend_pid);
955
956 return PSQL_CMD_SKIP_LINE;
957}
char * PQoptions(const PGconn *conn)
Definition: fe-connect.c:7608
int PQfullProtocolVersion(const PGconn *conn)
Definition: fe-connect.c:7659
int PQconnectionUsedPassword(const PGconn *conn)
Definition: fe-connect.c:7747
int PQconnectionUsedGSSAPI(const PGconn *conn)
Definition: fe-connect.c:7758
int PQbackendPID(const PGconn *conn)
Definition: fe-connect.c:7715
const char * PQsslAttribute(PGconn *conn, const char *attribute_name)
int PQsslInUse(PGconn *conn)
Definition: fe-secure.c:103
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3172
void printTableCleanup(printTableContent *const content)
Definition: print.c:3353
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3260
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3443
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3220
void pfree(void *pointer)
Definition: mcxt.c:1594
FILE * queryFout
Definition: settings.h:105

References _, _psqlSettings::db, is_unixsock_path(), _psqlSettings::logfile, pfree(), _psqlSettings::popt, PQbackendPID(), PQconnectionUsedGSSAPI(), PQconnectionUsedPassword(), PQdb(), PQfullProtocolVersion(), PQhost(), PQhostaddr(), PQoptions(), PQparameterStatus(), PQport(), PQsslAttribute(), PQsslInUse(), PQuser(), printf, printTable(), printTableAddCell(), printTableAddHeader(), printTableCleanup(), printTableInit(), pset, psprintf(), PSQL_CMD_SKIP_LINE, _psqlSettings::queryFout, and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_copy()

static backslashResult exec_command_copy ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 963 of file command.c.

964{
965 bool success = true;
966
967 if (active_branch)
968 {
969 char *opt = psql_scan_slash_option(scan_state,
970 OT_WHOLE_LINE, NULL, false);
971
972 success = do_copy(opt);
973 free(opt);
974 }
975 else
976 ignore_slash_whole_line(scan_state);
977
979}
bool do_copy(const char *args)
Definition: copy.c:268
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:3772
@ OT_WHOLE_LINE
Definition: psqlscanslash.h:21

References do_copy(), free, ignore_slash_whole_line(), OT_WHOLE_LINE, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and success.

Referenced by exec_command().

◆ exec_command_copyright()

static backslashResult exec_command_copyright ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 985 of file command.c.

986{
987 if (active_branch)
989
990 return PSQL_CMD_SKIP_LINE;
991}
void print_copyright(void)
Definition: help.c:752

References print_copyright(), and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

◆ exec_command_crosstabview()

static backslashResult exec_command_crosstabview ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 997 of file command.c.

998{
1000
1001 if (active_branch)
1002 {
1003 int i;
1004
1005 for (i = 0; i < lengthof(pset.ctv_args); i++)
1006 pset.ctv_args[i] = psql_scan_slash_option(scan_state,
1007 OT_NORMAL, NULL, true);
1008 pset.crosstab_flag = true;
1009 status = PSQL_CMD_SEND;
1010 }
1011 else
1012 ignore_slash_options(scan_state);
1013
1014 return status;
1015}
char * ctv_args[4]
Definition: settings.h:133
bool crosstab_flag
Definition: settings.h:132

References _psqlSettings::crosstab_flag, _psqlSettings::ctv_args, i, ignore_slash_options(), lengthof, OT_NORMAL, pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, and psql_scan_slash_option().

Referenced by exec_command().

◆ exec_command_d()

static backslashResult exec_command_d ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1021 of file command.c.

1022{
1024 bool success = true;
1025
1026 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 */
1034 pattern = psql_scan_slash_option(scan_state,
1035 OT_NORMAL, NULL, true);
1036
1037 show_verbose = strchr(cmd, '+') ? true : false;
1038 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 */
1046 save_expanded = pset.popt.topt.expanded;
1047 if (cmd[1] != '\0' && strchr(&cmd[2], 'x'))
1048 pset.popt.topt.expanded = 1;
1049
1050 switch (cmd[1])
1051 {
1052 case '\0':
1053 case '+':
1054 case 'S':
1055 if (pattern)
1056 success = describeTableDetails(pattern, show_verbose, show_system);
1057 else
1058 /* standard listing of interesting things */
1059 success = listTables("tvmsE", NULL, show_verbose, show_system);
1060 break;
1061 case 'A':
1062 {
1063 char *pattern2 = NULL;
1064
1065 if (pattern && cmd[2] != '\0' && cmd[2] != '+' && cmd[2] != 'x')
1066 pattern2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
1067
1068 switch (cmd[2])
1069 {
1070 case '\0':
1071 case '+':
1072 case 'x':
1073 success = describeAccessMethods(pattern, show_verbose);
1074 break;
1075 case 'c':
1076 success = listOperatorClasses(pattern, pattern2, show_verbose);
1077 break;
1078 case 'f':
1079 success = listOperatorFamilies(pattern, pattern2, show_verbose);
1080 break;
1081 case 'o':
1082 success = listOpFamilyOperators(pattern, pattern2, show_verbose);
1083 break;
1084 case 'p':
1085 success = listOpFamilyFunctions(pattern, pattern2, show_verbose);
1086 break;
1087 default:
1088 status = PSQL_CMD_UNKNOWN;
1089 break;
1090 }
1091
1092 free(pattern2);
1093 }
1094 break;
1095 case 'a':
1096 success = describeAggregates(pattern, show_verbose, show_system);
1097 break;
1098 case 'b':
1099 success = describeTablespaces(pattern, show_verbose);
1100 break;
1101 case 'c':
1102 if (strncmp(cmd, "dconfig", 7) == 0)
1104 show_verbose,
1105 show_system);
1106 else
1107 success = listConversions(pattern,
1108 show_verbose,
1109 show_system);
1110 break;
1111 case 'C':
1112 success = listCasts(pattern, show_verbose);
1113 break;
1114 case 'd':
1115 if (strncmp(cmd, "ddp", 3) == 0)
1116 success = listDefaultACLs(pattern);
1117 else
1118 success = objectDescription(pattern, show_system);
1119 break;
1120 case 'D':
1121 success = listDomains(pattern, show_verbose, show_system);
1122 break;
1123 case 'f': /* function subsystem */
1124 switch (cmd[2])
1125 {
1126 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':
1135 success = exec_command_dfo(scan_state, cmd, pattern,
1136 show_verbose, show_system);
1137 break;
1138 default:
1139 status = PSQL_CMD_UNKNOWN;
1140 break;
1141 }
1142 break;
1143 case 'g':
1144 /* no longer distinct from \du */
1145 success = describeRoles(pattern, show_verbose, show_system);
1146 break;
1147 case 'l':
1148 success = listLargeObjects(show_verbose);
1149 break;
1150 case 'L':
1151 success = listLanguages(pattern, show_verbose, show_system);
1152 break;
1153 case 'n':
1154 success = listSchemas(pattern, show_verbose, show_system);
1155 break;
1156 case 'o':
1157 success = exec_command_dfo(scan_state, cmd, pattern,
1158 show_verbose, show_system);
1159 break;
1160 case 'O':
1161 success = listCollations(pattern, show_verbose, show_system);
1162 break;
1163 case 'p':
1164 success = permissionsList(pattern, show_system);
1165 break;
1166 case 'P':
1167 {
1168 switch (cmd[2])
1169 {
1170 case '\0':
1171 case '+':
1172 case 't':
1173 case 'i':
1174 case 'n':
1175 case 'x':
1176 success = listPartitionedTables(&cmd[2], pattern, show_verbose);
1177 break;
1178 default:
1179 status = PSQL_CMD_UNKNOWN;
1180 break;
1181 }
1182 }
1183 break;
1184 case 'T':
1185 success = describeTypes(pattern, show_verbose, show_system);
1186 break;
1187 case 't':
1188 case 'v':
1189 case 'm':
1190 case 'i':
1191 case 's':
1192 case 'E':
1193 success = listTables(&cmd[1], pattern, show_verbose, show_system);
1194 break;
1195 case 'r':
1196 if (cmd[2] == 'd' && cmd[3] == 's')
1197 {
1198 char *pattern2 = NULL;
1199
1200 if (pattern)
1201 pattern2 = psql_scan_slash_option(scan_state,
1202 OT_NORMAL, NULL, true);
1203 success = listDbRoleSettings(pattern, pattern2);
1204
1205 free(pattern2);
1206 }
1207 else if (cmd[2] == 'g')
1208 success = describeRoleGrants(pattern, show_system);
1209 else
1210 status = PSQL_CMD_UNKNOWN;
1211 break;
1212 case 'R':
1213 switch (cmd[2])
1214 {
1215 case 'p':
1216 if (show_verbose)
1217 success = describePublications(pattern);
1218 else
1219 success = listPublications(pattern);
1220 break;
1221 case 's':
1222 success = describeSubscriptions(pattern, show_verbose);
1223 break;
1224 default:
1225 status = PSQL_CMD_UNKNOWN;
1226 }
1227 break;
1228 case 'u':
1229 success = describeRoles(pattern, show_verbose, show_system);
1230 break;
1231 case 'F': /* text search subsystem */
1232 switch (cmd[2])
1233 {
1234 case '\0':
1235 case '+':
1236 case 'x':
1237 success = listTSConfigs(pattern, show_verbose);
1238 break;
1239 case 'p':
1240 success = listTSParsers(pattern, show_verbose);
1241 break;
1242 case 'd':
1243 success = listTSDictionaries(pattern, show_verbose);
1244 break;
1245 case 't':
1246 success = listTSTemplates(pattern, show_verbose);
1247 break;
1248 default:
1249 status = PSQL_CMD_UNKNOWN;
1250 break;
1251 }
1252 break;
1253 case 'e': /* SQL/MED subsystem */
1254 switch (cmd[2])
1255 {
1256 case 's':
1257 success = listForeignServers(pattern, show_verbose);
1258 break;
1259 case 'u':
1260 success = listUserMappings(pattern, show_verbose);
1261 break;
1262 case 'w':
1263 success = listForeignDataWrappers(pattern, show_verbose);
1264 break;
1265 case 't':
1266 success = listForeignTables(pattern, show_verbose);
1267 break;
1268 default:
1269 status = PSQL_CMD_UNKNOWN;
1270 break;
1271 }
1272 break;
1273 case 'x': /* Extensions */
1274 if (show_verbose)
1275 success = listExtensionContents(pattern);
1276 else
1277 success = listExtensions(pattern);
1278 break;
1279 case 'X': /* Extended Statistics */
1280 success = listExtendedStats(pattern);
1281 break;
1282 case 'y': /* Event Triggers */
1283 success = listEventTriggers(pattern, show_verbose);
1284 break;
1285 default:
1286 status = PSQL_CMD_UNKNOWN;
1287 }
1288
1289 /* Restore original expanded mode */
1290 pset.popt.topt.expanded = save_expanded;
1291
1292 free(pattern);
1293 }
1294 else
1295 ignore_slash_options(scan_state);
1296
1297 if (!success)
1298 status = PSQL_CMD_ERROR;
1299
1300 return status;
1301}
static bool exec_command_dfo(PsqlScanState scan_state, const char *cmd, const char *pattern, bool show_verbose, bool show_system)
Definition: command.c:1305
bool listUserMappings(const char *pattern, bool verbose)
Definition: describe.c:6051
bool listTSConfigs(const char *pattern, bool verbose)
Definition: describe.c:5700
bool describeRoles(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3712
bool listOpFamilyFunctions(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:7205
bool listPublications(const char *pattern)
Definition: describe.c:6396
bool listTSParsers(const char *pattern, bool verbose)
Definition: describe.c:5323
bool listExtensionContents(const char *pattern)
Definition: describe.c:6232
bool describeAggregates(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:78
bool listForeignDataWrappers(const char *pattern, bool verbose)
Definition: describe.c:5904
bool listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
Definition: describe.c:4262
bool describeSubscriptions(const char *pattern, bool verbose)
Definition: describe.c:6742
bool describeRoleGrants(const char *pattern, bool showSystem)
Definition: describe.c:3928
bool listExtendedStats(const char *pattern)
Definition: describe.c:4859
bool describeTypes(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:639
bool listOperatorFamilies(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:7009
bool listForeignServers(const char *pattern, bool verbose)
Definition: describe.c:5975
bool describeTableDetails(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:1488
bool listDomains(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4548
bool listTSDictionaries(const char *pattern, bool verbose)
Definition: describe.c:5570
bool listDbRoleSettings(const char *pattern, const char *pattern2)
Definition: describe.c:3859
bool listCollations(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:5079
bool listSchemas(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:5202
bool listExtensions(const char *pattern)
Definition: describe.c:6178
bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4007
bool listTSTemplates(const char *pattern, bool verbose)
Definition: describe.c:5635
bool describeTablespaces(const char *pattern, bool verbose)
Definition: describe.c:222
bool listCasts(const char *pattern, bool verbose)
Definition: describe.c:4955
bool listOpFamilyOperators(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:7098
bool listOperatorClasses(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6908
bool listForeignTables(const char *pattern, bool verbose)
Definition: describe.c:6106
bool describeConfigurationParameters(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4711
bool listEventTriggers(const char *pattern, bool verbose)
Definition: describe.c:4779
bool listDefaultACLs(const char *pattern)
Definition: describe.c:1214
bool listConversions(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4631
bool permissionsList(const char *pattern, bool showSystem)
Definition: describe.c:1050
bool describeAccessMethods(const char *pattern, bool verbose)
Definition: describe.c:148
bool listLargeObjects(bool verbose)
Definition: describe.c:7294
bool listLanguages(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4472
bool describePublications(const char *pattern)
Definition: describe.c:6527
bool objectDescription(const char *pattern, bool showSystem)
Definition: describe.c:1295
return true
Definition: isn.c:130

References describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describePublications(), describeRoleGrants(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), exec_command_dfo(), printTableOpt::expanded, free, ignore_slash_options(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listLargeObjects(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSDictionaries(), listTSParsers(), listTSTemplates(), listUserMappings(), objectDescription(), OT_NORMAL, permissionsList(), _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, PSQL_CMD_UNKNOWN, psql_scan_slash_option(), success, printQueryOpt::topt, and true.

Referenced by exec_command().

◆ exec_command_dfo()

static bool exec_command_dfo ( PsqlScanState  scan_state,
const char *  cmd,
const char *  pattern,
bool  show_verbose,
bool  show_system 
)
static

Definition at line 1305 of file command.c.

1308{
1309 bool success;
1310 char *arg_patterns[FUNC_MAX_ARGS];
1311 int num_arg_patterns = 0;
1312
1313 /* Collect argument-type patterns too */
1314 if (pattern) /* otherwise it was just \df or \do */
1315 {
1316 char *ap;
1317
1318 while ((ap = psql_scan_slash_option(scan_state,
1319 OT_NORMAL, NULL, true)) != NULL)
1320 {
1321 arg_patterns[num_arg_patterns++] = ap;
1322 if (num_arg_patterns >= FUNC_MAX_ARGS)
1323 break; /* protect limited-size array */
1324 }
1325 }
1326
1327 if (cmd[1] == 'f')
1328 success = describeFunctions(&cmd[2], pattern,
1329 arg_patterns, num_arg_patterns,
1330 show_verbose, show_system);
1331 else
1332 success = describeOperators(pattern,
1333 arg_patterns, num_arg_patterns,
1334 show_verbose, show_system);
1335
1336 while (--num_arg_patterns >= 0)
1337 free(arg_patterns[num_arg_patterns]);
1338
1339 return success;
1340}
bool describeFunctions(const char *functypes, const char *func_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
Definition: describe.c:295
bool describeOperators(const char *oper_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
Definition: describe.c:794
#define FUNC_MAX_ARGS

References describeFunctions(), describeOperators(), free, FUNC_MAX_ARGS, OT_NORMAL, psql_scan_slash_option(), and success.

Referenced by exec_command_d().

◆ exec_command_echo()

static backslashResult exec_command_echo ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1559 of file command.c.

1560{
1561 if (active_branch)
1562 {
1563 char *value;
1564 char quoted;
1565 bool no_newline = false;
1566 bool first = true;
1567 FILE *fout;
1568
1569 if (strcmp(cmd, "qecho") == 0)
1570 fout = pset.queryFout;
1571 else if (strcmp(cmd, "warn") == 0)
1572 fout = stderr;
1573 else
1574 fout = stdout;
1575
1576 while ((value = psql_scan_slash_option(scan_state,
1577 OT_NORMAL, &quoted, false)))
1578 {
1579 if (first && !no_newline && !quoted && strcmp(value, "-n") == 0)
1580 no_newline = true;
1581 else
1582 {
1583 if (first)
1584 first = false;
1585 else
1586 fputc(' ', fout);
1587 fputs(value, fout);
1588 }
1589 free(value);
1590 }
1591 if (!no_newline)
1592 fputs("\n", fout);
1593 }
1594 else
1595 ignore_slash_options(scan_state);
1596
1597 return PSQL_CMD_SKIP_LINE;
1598}

References free, ignore_slash_options(), OT_NORMAL, pset, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::queryFout, generate_unaccent_rules::stdout, and value.

Referenced by exec_command().

◆ exec_command_edit()

static backslashResult exec_command_edit ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 1347 of file command.c.

1349{
1351
1352 if (active_branch)
1353 {
1354 if (!query_buf)
1355 {
1356 pg_log_error("no query buffer");
1357 status = PSQL_CMD_ERROR;
1358 }
1359 else
1360 {
1361 char *fname;
1362 char *ln = NULL;
1363 int lineno = -1;
1364
1365 fname = psql_scan_slash_option(scan_state,
1366 OT_NORMAL, NULL, true);
1367 if (fname)
1368 {
1369 /* try to get separate lineno arg */
1370 ln = psql_scan_slash_option(scan_state,
1371 OT_NORMAL, NULL, true);
1372 if (ln == NULL)
1373 {
1374 /* only one arg; maybe it is lineno not fname */
1375 if (fname[0] &&
1376 strspn(fname, "0123456789") == strlen(fname))
1377 {
1378 /* all digits, so assume it is lineno */
1379 ln = fname;
1380 fname = NULL;
1381 }
1382 }
1383 }
1384 if (ln)
1385 {
1386 lineno = atoi(ln);
1387 if (lineno < 1)
1388 {
1389 pg_log_error("invalid line number: %s", ln);
1390 status = PSQL_CMD_ERROR;
1391 }
1392 }
1393 if (status != PSQL_CMD_ERROR)
1394 {
1395 bool discard_on_quit;
1396
1397 expand_tilde(&fname);
1398 if (fname)
1399 {
1401 /* Always clear buffer if the file isn't modified */
1402 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 discard_on_quit = copy_previous_query(query_buf,
1412 previous_buf);
1413 }
1414
1415 if (do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
1416 status = PSQL_CMD_NEWEDIT;
1417 else
1418 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 */
1425 if (status == PSQL_CMD_ERROR)
1426 resetPQExpBuffer(query_buf);
1427
1428 free(fname);
1429 free(ln);
1430 }
1431 }
1432 else
1433 ignore_slash_options(scan_state);
1434
1435 return status;
1436}
void expand_tilde(char **filename)
Definition: common.c:2576
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool discard_on_quit, bool *edited)
Definition: command.c:4728
@ PSQL_CMD_NEWEDIT
Definition: command.h:21
void canonicalize_path_enc(char *path, int encoding)
Definition: path.c:344

References canonicalize_path_enc(), copy_previous_query(), do_edit(), _psqlSettings::encoding, expand_tilde(), free, ignore_slash_options(), OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_NEWEDIT, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and resetPQExpBuffer().

Referenced by exec_command().

◆ exec_command_ef_ev()

static backslashResult exec_command_ef_ev ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf,
bool  is_func 
)
static

Definition at line 1443 of file command.c.

1445{
1447
1448 if (active_branch)
1449 {
1450 char *obj_desc = psql_scan_slash_option(scan_state,
1452 NULL, true);
1453 int lineno = -1;
1454
1455 if (!query_buf)
1456 {
1457 pg_log_error("no query buffer");
1458 status = PSQL_CMD_ERROR;
1459 }
1460 else
1461 {
1462 Oid obj_oid = InvalidOid;
1464
1465 lineno = strip_lineno_from_objdesc(obj_desc);
1466 if (lineno == 0)
1467 {
1468 /* error already reported */
1469 status = PSQL_CMD_ERROR;
1470 }
1471 else if (!obj_desc)
1472 {
1473 /* set up an empty command to fill in */
1474 resetPQExpBuffer(query_buf);
1475 if (is_func)
1476 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 appendPQExpBufferStr(query_buf,
1485 "CREATE VIEW AS\n"
1486 " SELECT \n"
1487 " -- something...\n");
1488 }
1489 else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
1490 {
1491 /* error already reported */
1492 status = PSQL_CMD_ERROR;
1493 }
1494 else if (!get_create_object_cmd(eot, obj_oid, query_buf))
1495 {
1496 /* error already reported */
1497 status = PSQL_CMD_ERROR;
1498 }
1499 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 */
1510 const char *lines = query_buf->data;
1511
1512 while (*lines != '\0')
1513 {
1514 if (strncmp(lines, "AS ", 3) == 0 ||
1515 strncmp(lines, "BEGIN ", 6) == 0 ||
1516 strncmp(lines, "RETURN ", 7) == 0)
1517 break;
1518 lineno++;
1519 /* find start of next line */
1520 lines = strchr(lines, '\n');
1521 if (!lines)
1522 break;
1523 lines++;
1524 }
1525 }
1526 }
1527
1528 if (status != PSQL_CMD_ERROR)
1529 {
1530 bool edited = false;
1531
1532 if (!do_edit(NULL, query_buf, lineno, true, &edited))
1533 status = PSQL_CMD_ERROR;
1534 else if (!edited)
1535 puts(_("No changes"));
1536 else
1537 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 */
1544 if (status == PSQL_CMD_ERROR)
1545 resetPQExpBuffer(query_buf);
1546
1547 free(obj_desc);
1548 }
1549 else
1550 ignore_slash_whole_line(scan_state);
1551
1552 return status;
1553}
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:6209
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:6149
static int strip_lineno_from_objdesc(char *obj)
Definition: command.c:6369
#define InvalidOid
Definition: postgres_ext.h:37
unsigned int Oid
Definition: postgres_ext.h:32

References _, appendPQExpBufferStr(), PQExpBufferData::data, do_edit(), EditableFunction, EditableView, free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), OT_WHOLE_LINE, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_NEWEDIT, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), resetPQExpBuffer(), and strip_lineno_from_objdesc().

Referenced by exec_command().

◆ exec_command_elif()

static backslashResult exec_command_elif ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 2150 of file command.c.

2152{
2153 bool success = true;
2154
2155 switch (conditional_stack_peek(cstack))
2156 {
2157 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 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 */
2172 ignore_boolean_expression(scan_state);
2173 break;
2174 case IFSTATE_FALSE:
2175
2176 /*
2177 * Discard any query text added by the just-skipped branch.
2178 */
2179 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 */
2187 if (!is_true_boolean_expression(scan_state, "\\elif expression"))
2189 break;
2190 case IFSTATE_IGNORED:
2191
2192 /*
2193 * Discard any query text added by the just-skipped branch.
2194 */
2195 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 ignore_boolean_expression(scan_state);
2202 break;
2203 case IFSTATE_ELSE_TRUE:
2204 case IFSTATE_ELSE_FALSE:
2205 pg_log_error("\\elif: cannot occur after \\else");
2206 success = false;
2207 break;
2208 case IFSTATE_NONE:
2209 /* no \if to elif from */
2210 pg_log_error("\\elif: no matching \\if");
2211 success = false;
2212 break;
2213 }
2214
2216}
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:3820
static void ignore_boolean_expression(PsqlScanState scan_state)
Definition: command.c:3719
static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name)
Definition: command.c:3702
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:3800
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:106
bool conditional_stack_poke(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:118
@ IFSTATE_FALSE
Definition: conditional.h:34
@ IFSTATE_ELSE_TRUE
Definition: conditional.h:40
@ IFSTATE_IGNORED
Definition: conditional.h:37
@ IFSTATE_TRUE
Definition: conditional.h:32
@ IFSTATE_NONE
Definition: conditional.h:31
@ IFSTATE_ELSE_FALSE
Definition: conditional.h:42

References conditional_stack_peek(), conditional_stack_poke(), discard_query_text(), IFSTATE_ELSE_FALSE, IFSTATE_ELSE_TRUE, IFSTATE_FALSE, IFSTATE_IGNORED, IFSTATE_NONE, IFSTATE_TRUE, ignore_boolean_expression(), is_true_boolean_expression(), pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, save_query_text_state(), and success.

Referenced by exec_command().

◆ exec_command_else()

static backslashResult exec_command_else ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 2226 of file command.c.

2228{
2229 bool success = true;
2230
2231 switch (conditional_stack_peek(cstack))
2232 {
2233 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 save_query_text_state(scan_state, cstack, query_buf);
2241
2242 /* Now skip the \else branch */
2244 break;
2245 case IFSTATE_FALSE:
2246
2247 /*
2248 * Discard any query text added by the just-skipped branch.
2249 */
2250 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 */
2257 break;
2258 case IFSTATE_IGNORED:
2259
2260 /*
2261 * Discard any query text added by the just-skipped branch.
2262 */
2263 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 */
2271 break;
2272 case IFSTATE_ELSE_TRUE:
2273 case IFSTATE_ELSE_FALSE:
2274 pg_log_error("\\else: cannot occur after \\else");
2275 success = false;
2276 break;
2277 case IFSTATE_NONE:
2278 /* no \if to else from */
2279 pg_log_error("\\else: no matching \\if");
2280 success = false;
2281 break;
2282 }
2283
2285}

References conditional_stack_peek(), conditional_stack_poke(), discard_query_text(), IFSTATE_ELSE_FALSE, IFSTATE_ELSE_TRUE, IFSTATE_FALSE, IFSTATE_IGNORED, IFSTATE_NONE, IFSTATE_TRUE, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, save_query_text_state(), and success.

Referenced by exec_command().

◆ exec_command_encoding()

static backslashResult exec_command_encoding ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1604 of file command.c.

1605{
1606 if (active_branch)
1607 {
1608 char *encoding = psql_scan_slash_option(scan_state,
1609 OT_NORMAL, NULL, false);
1610
1611 if (!encoding)
1612 {
1613 /* show encoding */
1615 }
1616 else
1617 {
1618 /* set encoding */
1619 if (PQsetClientEncoding(pset.db, encoding) == -1)
1620 pg_log_error("%s: invalid encoding name or conversion procedure not found", encoding);
1621 else
1622 {
1623 /* save encoding info into psql internal data */
1627 SetVariable(pset.vars, "ENCODING",
1629 }
1630 free(encoding);
1631 }
1632 }
1633 else
1634 ignore_slash_options(scan_state);
1635
1636 return PSQL_CMD_SKIP_LINE;
1637}
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:7769
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:7777
int32 encoding
Definition: pg_database.h:41
#define pg_encoding_to_char
Definition: pg_wchar.h:630
void setFmtEncoding(int encoding)
Definition: string_utils.c:69
VariableSpace vars
Definition: settings.h:151
int encoding
Definition: print.h:138
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:281

References _psqlSettings::db, _psqlSettings::encoding, encoding, printTableOpt::encoding, free, ignore_slash_options(), OT_NORMAL, pg_encoding_to_char, pg_log_error, _psqlSettings::popt, PQclientEncoding(), PQsetClientEncoding(), pset, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), setFmtEncoding(), SetVariable(), printQueryOpt::topt, and _psqlSettings::vars.

Referenced by exec_command().

◆ exec_command_endif()

static backslashResult exec_command_endif ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 2291 of file command.c.

2293{
2294 bool success = true;
2295
2296 switch (conditional_stack_peek(cstack))
2297 {
2298 case IFSTATE_TRUE:
2299 case IFSTATE_ELSE_TRUE:
2300 /* Close the \if block, keeping the query text */
2302 Assert(success);
2303 break;
2304 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 discard_query_text(scan_state, cstack, query_buf);
2312
2313 /* Close the \if block */
2315 Assert(success);
2316 break;
2317 case IFSTATE_NONE:
2318 /* no \if to end */
2319 pg_log_error("\\endif: no matching \\if");
2320 success = false;
2321 break;
2322 }
2323
2325}
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:69

References Assert(), conditional_stack_peek(), conditional_stack_pop(), discard_query_text(), IFSTATE_ELSE_FALSE, IFSTATE_ELSE_TRUE, IFSTATE_FALSE, IFSTATE_IGNORED, IFSTATE_NONE, IFSTATE_TRUE, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, and success.

Referenced by exec_command().

◆ exec_command_endpipeline()

static backslashResult exec_command_endpipeline ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3097 of file command.c.

3098{
3100
3101 if (active_branch)
3102 {
3104 status = PSQL_CMD_SEND;
3105 }
3106 else
3107 ignore_slash_options(scan_state);
3108
3109 return status;
3110}
@ PSQL_SEND_END_PIPELINE_MODE
Definition: settings.h:80

References ignore_slash_options(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_SEND_END_PIPELINE_MODE, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_errverbose()

static backslashResult exec_command_errverbose ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1643 of file command.c.

1644{
1645 if (active_branch)
1646 {
1648 {
1649 char *msg;
1650
1654 if (msg)
1655 {
1656 pg_log_error("%s", msg);
1657 PQfreemem(msg);
1658 }
1659 else
1660 puts(_("out of memory"));
1661 }
1662 else
1663 puts(_("There is no previous error."));
1664 }
1665
1666 return PSQL_CMD_SKIP_LINE;
1667}
char * PQresultVerboseErrorMessage(const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
Definition: fe-exec.c:3446
@ PQSHOW_CONTEXT_ALWAYS
Definition: libpq-fe.h:166
@ PQERRORS_VERBOSE
Definition: libpq-fe.h:158
PGresult * last_error_result
Definition: settings.h:110

References _, _psqlSettings::last_error_result, pg_log_error, PQERRORS_VERBOSE, PQfreemem(), PQresultVerboseErrorMessage(), PQSHOW_CONTEXT_ALWAYS, pset, and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

◆ exec_command_f()

static backslashResult exec_command_f ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1673 of file command.c.

1674{
1675 bool success = true;
1676
1677 if (active_branch)
1678 {
1679 char *fname = psql_scan_slash_option(scan_state,
1680 OT_NORMAL, NULL, false);
1681
1682 success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
1683 free(fname);
1684 }
1685 else
1686 ignore_slash_options(scan_state);
1687
1689}

References do_pset(), free, ignore_slash_options(), OT_NORMAL, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, and success.

Referenced by exec_command().

◆ exec_command_flush()

static backslashResult exec_command_flush ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1695 of file command.c.

1696{
1698
1699 if (active_branch)
1700 {
1702 status = PSQL_CMD_SEND;
1703 }
1704 else
1705 ignore_slash_options(scan_state);
1706
1707 return status;
1708}
@ PSQL_SEND_FLUSH
Definition: settings.h:81

References ignore_slash_options(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_SEND_FLUSH, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_flushrequest()

static backslashResult exec_command_flushrequest ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1714 of file command.c.

1715{
1717
1718 if (active_branch)
1719 {
1721 status = PSQL_CMD_SEND;
1722 }
1723 else
1724 ignore_slash_options(scan_state);
1725
1726 return status;
1727}
@ PSQL_SEND_FLUSH_REQUEST
Definition: settings.h:82

References ignore_slash_options(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_SEND_FLUSH_REQUEST, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_g()

static backslashResult exec_command_g ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1739 of file command.c.

1740{
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 */
1748 fname = psql_scan_slash_option(scan_state,
1749 OT_FILEPIPE, NULL, false);
1750
1751 if (fname && fname[0] == '(')
1752 {
1753 /* Consume pset options through trailing ')' ... */
1754 status = process_command_g_options(fname + 1, scan_state,
1755 active_branch, cmd);
1756 free(fname);
1757 /* ... and again attempt to scan the filename. */
1758 fname = psql_scan_slash_option(scan_state,
1759 OT_FILEPIPE, NULL, false);
1760 }
1761
1762 if (status == PSQL_CMD_SKIP_LINE && active_branch)
1763 {
1765 {
1766 pg_log_error("\\%s not allowed in pipeline mode", cmd);
1768 free(fname);
1769 return PSQL_CMD_ERROR;
1770 }
1771
1772 if (!fname)
1773 pset.gfname = NULL;
1774 else
1775 {
1776 expand_tilde(&fname);
1777 pset.gfname = pg_strdup(fname);
1778 }
1779 if (strcmp(cmd, "gx") == 0)
1780 {
1781 /* save settings if not done already, then force expanded=on */
1782 if (pset.gsavepopt == NULL)
1784 pset.popt.topt.expanded = 1;
1785 }
1786 status = PSQL_CMD_SEND;
1787 }
1788
1789 free(fname);
1790
1791 return status;
1792}
printQueryOpt * savePsetInfo(const printQueryOpt *popt)
Definition: command.c:5629
static backslashResult process_command_g_options(char *first_option, PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1800
PGpipelineStatus PQpipelineStatus(const PGconn *conn)
Definition: fe-connect.c:7723
@ PQ_PIPELINE_OFF
Definition: libpq-fe.h:187
@ OT_FILEPIPE
Definition: psqlscanslash.h:20
printQueryOpt * gsavepopt
Definition: settings.h:115
char * gfname
Definition: settings.h:114

References clean_extended_state(), _psqlSettings::db, expand_tilde(), printTableOpt::expanded, free, _psqlSettings::gfname, _psqlSettings::gsavepopt, OT_FILEPIPE, pg_log_error, pg_strdup(), _psqlSettings::popt, PQ_PIPELINE_OFF, PQpipelineStatus(), process_command_g_options(), pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), savePsetInfo(), and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_gdesc()

static backslashResult exec_command_gdesc ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1875 of file command.c.

1876{
1878
1879 if (active_branch)
1880 {
1881 pset.gdesc_flag = true;
1882 status = PSQL_CMD_SEND;
1883 }
1884
1885 return status;
1886}
bool gdesc_flag
Definition: settings.h:118

References _psqlSettings::gdesc_flag, pset, PSQL_CMD_SEND, and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

◆ exec_command_getenv()

static backslashResult exec_command_getenv ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1892 of file command.c.

1894{
1895 bool success = true;
1896
1897 if (active_branch)
1898 {
1899 char *myvar = psql_scan_slash_option(scan_state,
1900 OT_NORMAL, NULL, false);
1901 char *envvar = psql_scan_slash_option(scan_state,
1902 OT_NORMAL, NULL, false);
1903
1904 if (!myvar || !envvar)
1905 {
1906 pg_log_error("\\%s: missing required argument", cmd);
1907 success = false;
1908 }
1909 else
1910 {
1911 char *envval = getenv(envvar);
1912
1913 if (envval && !SetVariable(pset.vars, myvar, envval))
1914 success = false;
1915 }
1916 free(myvar);
1917 free(envvar);
1918 }
1919 else
1920 ignore_slash_options(scan_state);
1921
1923}

References free, ignore_slash_options(), OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), SetVariable(), success, and _psqlSettings::vars.

Referenced by exec_command().

◆ exec_command_getresults()

static backslashResult exec_command_getresults ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1929 of file command.c.

1930{
1932
1933 if (active_branch)
1934 {
1935 char *opt;
1936 int num_results;
1937
1939 status = PSQL_CMD_SEND;
1940 opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
1941
1943 if (opt != NULL)
1944 {
1945 num_results = atoi(opt);
1946 if (num_results < 0)
1947 {
1948 pg_log_error("\\getresults: invalid number of requested results");
1949 return PSQL_CMD_ERROR;
1950 }
1951 pset.requested_results = num_results;
1952 }
1953 }
1954 else
1955 ignore_slash_options(scan_state);
1956
1957 return status;
1958}
@ PSQL_SEND_GET_RESULTS
Definition: settings.h:83
int requested_results
Definition: settings.h:129

References ignore_slash_options(), OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQL_SEND_GET_RESULTS, _psqlSettings::requested_results, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_gexec()

static backslashResult exec_command_gexec ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1965 of file command.c.

1966{
1968
1969 if (active_branch)
1970 {
1972 {
1973 pg_log_error("\\%s not allowed in pipeline mode", "gexec");
1975 return PSQL_CMD_ERROR;
1976 }
1977 pset.gexec_flag = true;
1978 status = PSQL_CMD_SEND;
1979 }
1980
1981 return status;
1982}
bool gexec_flag
Definition: settings.h:119

References clean_extended_state(), _psqlSettings::db, _psqlSettings::gexec_flag, pg_log_error, PQ_PIPELINE_OFF, PQpipelineStatus(), pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

◆ exec_command_gset()

static backslashResult exec_command_gset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1988 of file command.c.

1989{
1991
1992 if (active_branch)
1993 {
1994 char *prefix = psql_scan_slash_option(scan_state,
1995 OT_NORMAL, NULL, false);
1996
1998 {
1999 pg_log_error("\\%s not allowed in pipeline mode", "gset");
2001 return PSQL_CMD_ERROR;
2002 }
2003
2004 if (prefix)
2005 pset.gset_prefix = prefix;
2006 else
2007 {
2008 /* we must set a non-NULL prefix to trigger storing */
2010 }
2011 /* gset_prefix is freed later */
2012 status = PSQL_CMD_SEND;
2013 }
2014 else
2015 ignore_slash_options(scan_state);
2016
2017 return status;
2018}
char * gset_prefix
Definition: settings.h:117

References clean_extended_state(), _psqlSettings::db, _psqlSettings::gset_prefix, ignore_slash_options(), OT_NORMAL, pg_log_error, pg_strdup(), PQ_PIPELINE_OFF, PQpipelineStatus(), pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, and psql_scan_slash_option().

Referenced by exec_command().

◆ exec_command_help()

static backslashResult exec_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2024 of file command.c.

2025{
2026 if (active_branch)
2027 {
2028 char *opt = psql_scan_slash_option(scan_state,
2029 OT_WHOLE_LINE, NULL, true);
2030
2031 helpSQL(opt, pset.popt.topt.pager);
2032 free(opt);
2033 }
2034 else
2035 ignore_slash_whole_line(scan_state);
2036
2037 return PSQL_CMD_SKIP_LINE;
2038}
void helpSQL(const char *topic, unsigned short int pager)
Definition: help.c:590

References free, helpSQL(), ignore_slash_whole_line(), OT_WHOLE_LINE, printTableOpt::pager, _psqlSettings::popt, pset, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_html()

static backslashResult exec_command_html ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2044 of file command.c.

2045{
2046 bool success = true;
2047
2048 if (active_branch)
2049 {
2051 success = do_pset("format", "html", &pset.popt, pset.quiet);
2052 else
2053 success = do_pset("format", "aligned", &pset.popt, pset.quiet);
2054 }
2055
2057}

References do_pset(), printTableOpt::format, _psqlSettings::popt, PRINT_HTML, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, _psqlSettings::quiet, success, and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_if()

static backslashResult exec_command_if ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 2104 of file command.c.

2106{
2107 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 */
2116
2117 /* Remember current query state in case we need to restore later */
2118 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 if (!is_true_boolean_expression(scan_state, "\\if expression"))
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 */
2134
2135 /* Remember current query state in case we need to restore later */
2136 save_query_text_state(scan_state, cstack, query_buf);
2137
2138 ignore_boolean_expression(scan_state);
2139 }
2140
2141 return PSQL_CMD_SKIP_LINE;
2142}
void conditional_stack_push(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:53

References conditional_active(), conditional_stack_poke(), conditional_stack_push(), IFSTATE_FALSE, IFSTATE_IGNORED, IFSTATE_TRUE, ignore_boolean_expression(), is_true_boolean_expression(), PSQL_CMD_SKIP_LINE, and save_query_text_state().

Referenced by exec_command().

◆ exec_command_include()

static backslashResult exec_command_include ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2063 of file command.c.

2064{
2065 bool success = true;
2066
2067 if (active_branch)
2068 {
2069 char *fname = psql_scan_slash_option(scan_state,
2070 OT_NORMAL, NULL, true);
2071
2072 if (!fname)
2073 {
2074 pg_log_error("\\%s: missing required argument", cmd);
2075 success = false;
2076 }
2077 else
2078 {
2079 bool include_relative;
2080
2081 include_relative = (strcmp(cmd, "ir") == 0
2082 || strcmp(cmd, "include_relative") == 0);
2083 expand_tilde(&fname);
2084 success = (process_file(fname, include_relative) == EXIT_SUCCESS);
2085 free(fname);
2086 }
2087 }
2088 else
2089 ignore_slash_options(scan_state);
2090
2092}
int process_file(char *filename, bool use_relative_path)
Definition: command.c:4923
#define EXIT_SUCCESS
Definition: settings.h:193

References EXIT_SUCCESS, expand_tilde(), free, ignore_slash_options(), OT_NORMAL, pg_log_error, process_file(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and success.

Referenced by exec_command().

◆ exec_command_list()

static backslashResult exec_command_list ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2331 of file command.c.

2332{
2333 bool success = true;
2334
2335 if (active_branch)
2336 {
2337 char *pattern;
2338 bool show_verbose;
2339 unsigned short int save_expanded;
2340
2341 pattern = psql_scan_slash_option(scan_state,
2342 OT_NORMAL, NULL, true);
2343
2344 show_verbose = strchr(cmd, '+') ? true : false;
2345
2346 /* if 'x' option specified, force expanded mode */
2347 save_expanded = pset.popt.topt.expanded;
2348 if (strchr(cmd, 'x'))
2349 pset.popt.topt.expanded = 1;
2350
2351 success = listAllDbs(pattern, show_verbose);
2352
2353 /* restore original expanded mode */
2354 pset.popt.topt.expanded = save_expanded;
2355
2356 free(pattern);
2357 }
2358 else
2359 ignore_slash_options(scan_state);
2360
2362}
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:946

References printTableOpt::expanded, free, ignore_slash_options(), listAllDbs(), OT_NORMAL, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), success, printQueryOpt::topt, and true.

Referenced by exec_command().

◆ exec_command_lo()

static backslashResult exec_command_lo ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2368 of file command.c.

2369{
2371 bool success = true;
2372
2373 if (active_branch)
2374 {
2375 char *opt1,
2376 *opt2;
2377
2378 opt1 = psql_scan_slash_option(scan_state,
2379 OT_NORMAL, NULL, true);
2380 opt2 = psql_scan_slash_option(scan_state,
2381 OT_NORMAL, NULL, true);
2382
2383 if (strcmp(cmd + 3, "export") == 0)
2384 {
2385 if (!opt2)
2386 {
2387 pg_log_error("\\%s: missing required argument", cmd);
2388 success = false;
2389 }
2390 else
2391 {
2392 expand_tilde(&opt2);
2393 success = do_lo_export(opt1, opt2);
2394 }
2395 }
2396
2397 else if (strcmp(cmd + 3, "import") == 0)
2398 {
2399 if (!opt1)
2400 {
2401 pg_log_error("\\%s: missing required argument", cmd);
2402 success = false;
2403 }
2404 else
2405 {
2406 expand_tilde(&opt1);
2407 success = do_lo_import(opt1, opt2);
2408 }
2409 }
2410
2411 else if (strncmp(cmd + 3, "list", 4) == 0)
2412 {
2413 bool show_verbose;
2414 unsigned short int save_expanded;
2415
2416 show_verbose = strchr(cmd, '+') ? true : false;
2417
2418 /* if 'x' option specified, force expanded mode */
2419 save_expanded = pset.popt.topt.expanded;
2420 if (strchr(cmd, 'x'))
2421 pset.popt.topt.expanded = 1;
2422
2423 success = listLargeObjects(show_verbose);
2424
2425 /* restore original expanded mode */
2426 pset.popt.topt.expanded = save_expanded;
2427 }
2428
2429 else if (strcmp(cmd + 3, "unlink") == 0)
2430 {
2431 if (!opt1)
2432 {
2433 pg_log_error("\\%s: missing required argument", cmd);
2434 success = false;
2435 }
2436 else
2437 success = do_lo_unlink(opt1);
2438 }
2439
2440 else
2441 status = PSQL_CMD_UNKNOWN;
2442
2443 free(opt1);
2444 free(opt2);
2445 }
2446 else
2447 ignore_slash_options(scan_state);
2448
2449 if (!success)
2450 status = PSQL_CMD_ERROR;
2451
2452 return status;
2453}
bool do_lo_export(const char *loid_arg, const char *filename_arg)
Definition: large_obj.c:142
bool do_lo_import(const char *filename_arg, const char *comment_arg)
Definition: large_obj.c:176
bool do_lo_unlink(const char *loid_arg)
Definition: large_obj.c:239

References do_lo_export(), do_lo_import(), do_lo_unlink(), expand_tilde(), printTableOpt::expanded, free, ignore_slash_options(), listLargeObjects(), OT_NORMAL, pg_log_error, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, PSQL_CMD_UNKNOWN, psql_scan_slash_option(), success, printQueryOpt::topt, and true.

Referenced by exec_command().

◆ exec_command_out()

static backslashResult exec_command_out ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2459 of file command.c.

2460{
2461 bool success = true;
2462
2463 if (active_branch)
2464 {
2465 char *fname = psql_scan_slash_option(scan_state,
2466 OT_FILEPIPE, NULL, true);
2467
2468 expand_tilde(&fname);
2469 success = setQFout(fname);
2470 free(fname);
2471 }
2472 else
2473 ignore_slash_filepipe(scan_state);
2474
2476}
bool setQFout(const char *fname)
Definition: common.c:144
static void ignore_slash_filepipe(PsqlScanState scan_state)
Definition: command.c:3752

References expand_tilde(), free, ignore_slash_filepipe(), OT_FILEPIPE, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), setQFout(), and success.

Referenced by exec_command().

◆ exec_command_parse()

static backslashResult exec_command_parse ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2508 of file command.c.

2510{
2512
2513 if (active_branch)
2514 {
2515 char *opt = psql_scan_slash_option(scan_state,
2516 OT_NORMAL, NULL, false);
2517
2519
2520 if (!opt)
2521 {
2522 pg_log_error("\\%s: missing required argument", cmd);
2523 status = PSQL_CMD_ERROR;
2524 }
2525 else
2526 {
2527 pset.stmtName = opt;
2529 status = PSQL_CMD_SEND;
2530 }
2531 }
2532 else
2533 ignore_slash_options(scan_state);
2534
2535 return status;
2536}
@ PSQL_SEND_EXTENDED_PARSE
Definition: settings.h:75

References clean_extended_state(), ignore_slash_options(), OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQL_SEND_EXTENDED_PARSE, _psqlSettings::send_mode, and _psqlSettings::stmtName.

Referenced by exec_command().

◆ exec_command_password()

static backslashResult exec_command_password ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2542 of file command.c.

2543{
2544 bool success = true;
2545
2546 if (active_branch)
2547 {
2548 char *user = psql_scan_slash_option(scan_state,
2549 OT_SQLID, NULL, true);
2550 char *pw1 = NULL;
2551 char *pw2 = NULL;
2553 PromptInterruptContext prompt_ctx;
2554
2555 if (user == NULL)
2556 {
2557 /* By default, the command applies to CURRENT_USER */
2558 PGresult *res;
2559
2560 res = PSQLexec("SELECT CURRENT_USER");
2561 if (!res)
2562 return PSQL_CMD_ERROR;
2563
2564 user = pg_strdup(PQgetvalue(res, 0, 0));
2565 PQclear(res);
2566 }
2567
2568 /* Set up to let SIGINT cancel simple_prompt_extended() */
2569 prompt_ctx.jmpbuf = sigint_interrupt_jmp;
2570 prompt_ctx.enabled = &sigint_interrupt_enabled;
2571 prompt_ctx.canceled = false;
2572
2574 printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
2575
2576 pw1 = simple_prompt_extended(buf.data, false, &prompt_ctx);
2577 if (!prompt_ctx.canceled)
2578 pw2 = simple_prompt_extended("Enter it again: ", false, &prompt_ctx);
2579
2580 if (prompt_ctx.canceled)
2581 {
2582 /* fail silently */
2583 success = false;
2584 }
2585 else if (strcmp(pw1, pw2) != 0)
2586 {
2587 pg_log_error("Passwords didn't match.");
2588 success = false;
2589 }
2590 else
2591 {
2592 PGresult *res = PQchangePassword(pset.db, user, pw1);
2593
2594 if (PQresultStatus(res) != PGRES_COMMAND_OK)
2595 {
2597 success = false;
2598 }
2599
2600 PQclear(res);
2601 }
2602
2603 free(user);
2604 free(pw1);
2605 free(pw2);
2607 }
2608 else
2609 ignore_slash_options(scan_state);
2610
2612}
PGresult * PSQLexec(const char *query)
Definition: common.c:655
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:304
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:306
PGresult * PQchangePassword(PGconn *conn, const char *user, const char *passwd)
Definition: fe-auth.c:1531
#define PQgetvalue
Definition: libpq-be-fe.h:253
#define PQclear
Definition: libpq-be-fe.h:245
#define PQresultStatus
Definition: libpq-be-fe.h:247
@ PGRES_COMMAND_OK
Definition: libpq-fe.h:125
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:235
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:90
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:129
@ OT_SQLID
Definition: psqlscanslash.h:18
char * simple_prompt_extended(const char *prompt, bool echo, PromptInterruptContext *prompt_ctx)
Definition: sprompt.c:53
volatile sig_atomic_t * enabled
Definition: string.h:22

References _, buf, PromptInterruptContext::canceled, _psqlSettings::db, PromptInterruptContext::enabled, free, ignore_slash_options(), initPQExpBuffer(), PromptInterruptContext::jmpbuf, OT_SQLID, pg_log_error, pg_log_info, pg_strdup(), PGRES_COMMAND_OK, PQchangePassword(), PQclear, PQerrorMessage(), PQgetvalue, PQresultStatus, printfPQExpBuffer(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQLexec(), sigint_interrupt_enabled, sigint_interrupt_jmp, simple_prompt_extended(), success, termPQExpBuffer(), and user.

Referenced by exec_command().

◆ exec_command_print()

static backslashResult exec_command_print ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 2482 of file command.c.

2484{
2485 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 */
2492 if (query_buf && query_buf->len > 0)
2493 puts(query_buf->data);
2494 else if (previous_buf && previous_buf->len > 0)
2495 puts(previous_buf->data);
2496 else if (!pset.quiet)
2497 puts(_("Query buffer is empty."));
2498 fflush(stdout);
2499 }
2500
2501 return PSQL_CMD_SKIP_LINE;
2502}

References _, PQExpBufferData::data, PQExpBufferData::len, pset, PSQL_CMD_SKIP_LINE, _psqlSettings::quiet, and generate_unaccent_rules::stdout.

Referenced by exec_command().

◆ exec_command_prompt()

static backslashResult exec_command_prompt ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2618 of file command.c.

2620{
2621 bool success = true;
2622
2623 if (active_branch)
2624 {
2625 char *opt,
2626 *prompt_text = NULL;
2627 char *arg1,
2628 *arg2;
2629
2630 arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
2631 arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
2632
2633 if (!arg1)
2634 {
2635 pg_log_error("\\%s: missing required argument", cmd);
2636 success = false;
2637 }
2638 else
2639 {
2640 char *result;
2641 PromptInterruptContext prompt_ctx;
2642
2643 /* Set up to let SIGINT cancel simple_prompt_extended() */
2644 prompt_ctx.jmpbuf = sigint_interrupt_jmp;
2645 prompt_ctx.enabled = &sigint_interrupt_enabled;
2646 prompt_ctx.canceled = false;
2647
2648 if (arg2)
2649 {
2650 prompt_text = arg1;
2651 opt = arg2;
2652 }
2653 else
2654 opt = arg1;
2655
2656 if (!pset.inputfile)
2657 {
2658 result = simple_prompt_extended(prompt_text, true, &prompt_ctx);
2659 }
2660 else
2661 {
2662 if (prompt_text)
2663 {
2664 fputs(prompt_text, stdout);
2665 fflush(stdout);
2666 }
2667 result = gets_fromFile(stdin);
2668 if (!result)
2669 {
2670 pg_log_error("\\%s: could not read value for variable",
2671 cmd);
2672 success = false;
2673 }
2674 }
2675
2676 if (prompt_ctx.canceled ||
2677 (result && !SetVariable(pset.vars, opt, result)))
2678 success = false;
2679
2680 free(result);
2681 free(prompt_text);
2682 free(opt);
2683 }
2684 }
2685 else
2686 ignore_slash_options(scan_state);
2687
2689}
char * gets_fromFile(FILE *source)
Definition: input.c:186
char * inputfile
Definition: settings.h:143

References PromptInterruptContext::canceled, PromptInterruptContext::enabled, free, gets_fromFile(), ignore_slash_options(), _psqlSettings::inputfile, PromptInterruptContext::jmpbuf, OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), SetVariable(), sigint_interrupt_enabled, sigint_interrupt_jmp, simple_prompt_extended(), generate_unaccent_rules::stdout, success, and _psqlSettings::vars.

Referenced by exec_command().

◆ exec_command_pset()

static backslashResult exec_command_pset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2695 of file command.c.

2696{
2697 bool success = true;
2698
2699 if (active_branch)
2700 {
2701 char *opt0 = psql_scan_slash_option(scan_state,
2702 OT_NORMAL, NULL, false);
2703 char *opt1 = psql_scan_slash_option(scan_state,
2704 OT_NORMAL, NULL, false);
2705
2706 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
2724 for (i = 0; my_list[i] != NULL; i++)
2725 {
2726 char *val = pset_value_string(my_list[i], &pset.popt);
2727
2728 printf("%-24s %s\n", my_list[i], val);
2729 free(val);
2730 }
2731
2732 success = true;
2733 }
2734 else
2735 success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
2736
2737 free(opt0);
2738 free(opt1);
2739 }
2740 else
2741 ignore_slash_options(scan_state);
2742
2744}
static char * pset_value_string(const char *param, printQueryOpt *popt)
Definition: command.c:5736
long val
Definition: informix.c:689

References do_pset(), free, i, ignore_slash_options(), OT_NORMAL, _psqlSettings::popt, printf, pset, pset_value_string(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, success, and val.

Referenced by exec_command().

◆ exec_command_quit()

static backslashResult exec_command_quit ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2750 of file command.c.

2751{
2753
2754 if (active_branch)
2755 status = PSQL_CMD_TERMINATE;
2756
2757 return status;
2758}
@ PSQL_CMD_TERMINATE
Definition: command.h:20

References PSQL_CMD_SKIP_LINE, and PSQL_CMD_TERMINATE.

Referenced by exec_command().

◆ exec_command_reset()

static backslashResult exec_command_reset ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf 
)
static

Definition at line 2764 of file command.c.

2766{
2767 if (active_branch)
2768 {
2769 resetPQExpBuffer(query_buf);
2770 psql_scan_reset(scan_state);
2771 if (!pset.quiet)
2772 puts(_("Query buffer reset (cleared)."));
2773 }
2774
2775 return PSQL_CMD_SKIP_LINE;
2776}
void psql_scan_reset(PsqlScanState state)
Definition: psqlscan.l:1275

References _, pset, PSQL_CMD_SKIP_LINE, psql_scan_reset(), _psqlSettings::quiet, and resetPQExpBuffer().

Referenced by exec_command().

◆ exec_command_restrict()

static backslashResult exec_command_restrict ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2782 of file command.c.

2784{
2785 if (active_branch)
2786 {
2787 char *opt;
2788
2790
2791 opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
2792 if (opt == NULL || opt[0] == '\0')
2793 {
2794 pg_log_error("\\%s: missing required argument", cmd);
2795 return PSQL_CMD_ERROR;
2796 }
2797
2798 restrict_key = pstrdup(opt);
2799 restricted = true;
2800 }
2801 else
2802 ignore_slash_options(scan_state);
2803
2804 return PSQL_CMD_SKIP_LINE;
2805}
static char * restrict_key
Definition: command.c:200
static bool restricted
Definition: command.c:199
char * pstrdup(const char *in)
Definition: mcxt.c:1759

References Assert(), ignore_slash_options(), OT_NORMAL, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), pstrdup(), restrict_key, and restricted.

Referenced by exec_command().

◆ exec_command_s()

static backslashResult exec_command_s ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2811 of file command.c.

2812{
2813 bool success = true;
2814
2815 if (active_branch)
2816 {
2817 char *fname = psql_scan_slash_option(scan_state,
2818 OT_NORMAL, NULL, true);
2819
2820 expand_tilde(&fname);
2822 if (success && !pset.quiet && fname)
2823 printf(_("Wrote history to file \"%s\".\n"), fname);
2824 if (!fname)
2825 putchar('\n');
2826 free(fname);
2827 }
2828 else
2829 ignore_slash_options(scan_state);
2830
2832}
bool printHistory(const char *fname, unsigned short int pager)
Definition: input.c:494

References _, expand_tilde(), free, ignore_slash_options(), OT_NORMAL, printTableOpt::pager, _psqlSettings::popt, printf, printHistory(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, success, and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_sendpipeline()

static backslashResult exec_command_sendpipeline ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2838 of file command.c.

2839{
2841
2842 if (active_branch)
2843 {
2845 {
2848 {
2849 status = PSQL_CMD_SEND;
2850 }
2851 else
2852 {
2853 pg_log_error("\\sendpipeline must be used after \\bind or \\bind_named");
2855 return PSQL_CMD_ERROR;
2856 }
2857 }
2858 else
2859 {
2860 pg_log_error("\\sendpipeline not allowed outside of pipeline mode");
2862 return PSQL_CMD_ERROR;
2863 }
2864 }
2865 else
2866 ignore_slash_options(scan_state);
2867
2868 return status;
2869}

References clean_extended_state(), _psqlSettings::db, ignore_slash_options(), pg_log_error, PQ_PIPELINE_OFF, PQpipelineStatus(), pset, PSQL_CMD_ERROR, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_SEND_EXTENDED_QUERY_PARAMS, PSQL_SEND_EXTENDED_QUERY_PREPARED, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_set()

static backslashResult exec_command_set ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2875 of file command.c.

2876{
2877 bool success = true;
2878
2879 if (active_branch)
2880 {
2881 char *opt0 = psql_scan_slash_option(scan_state,
2882 OT_NORMAL, NULL, false);
2883
2884 if (!opt0)
2885 {
2886 /* list all variables */
2888 success = true;
2889 }
2890 else
2891 {
2892 /*
2893 * Set variable to the concatenation of the arguments.
2894 */
2895 char *newval;
2896 char *opt;
2897
2898 opt = psql_scan_slash_option(scan_state,
2899 OT_NORMAL, NULL, false);
2900 newval = pg_strdup(opt ? opt : "");
2901 free(opt);
2902
2903 while ((opt = psql_scan_slash_option(scan_state,
2904 OT_NORMAL, NULL, false)))
2905 {
2906 newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
2907 strcat(newval, opt);
2908 free(opt);
2909 }
2910
2911 if (!SetVariable(pset.vars, opt0, newval))
2912 success = false;
2913
2914 free(newval);
2915 }
2916 free(opt0);
2917 }
2918 else
2919 ignore_slash_options(scan_state);
2920
2922}
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define newval
void PrintVariables(VariableSpace space)
Definition: variables.c:256

References free, ignore_slash_options(), newval, OT_NORMAL, pg_realloc(), pg_strdup(), PrintVariables(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), SetVariable(), success, and _psqlSettings::vars.

Referenced by exec_command().

◆ exec_command_setenv()

static backslashResult exec_command_setenv ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2928 of file command.c.

2930{
2931 bool success = true;
2932
2933 if (active_branch)
2934 {
2935 char *envvar = psql_scan_slash_option(scan_state,
2936 OT_NORMAL, NULL, false);
2937 char *envval = psql_scan_slash_option(scan_state,
2938 OT_NORMAL, NULL, false);
2939
2940 if (!envvar)
2941 {
2942 pg_log_error("\\%s: missing required argument", cmd);
2943 success = false;
2944 }
2945 else if (strchr(envvar, '=') != NULL)
2946 {
2947 pg_log_error("\\%s: environment variable name must not contain \"=\"",
2948 cmd);
2949 success = false;
2950 }
2951 else if (!envval)
2952 {
2953 /* No argument - unset the environment variable */
2954 unsetenv(envvar);
2955 success = true;
2956 }
2957 else
2958 {
2959 /* Set variable to the value of the next argument */
2960 setenv(envvar, envval, 1);
2961 success = true;
2962 }
2963 free(envvar);
2964 free(envval);
2965 }
2966 else
2967 ignore_slash_options(scan_state);
2968
2970}
#define unsetenv(x)
Definition: win32_port.h:546
#define setenv(x, y, z)
Definition: win32_port.h:545

References free, ignore_slash_options(), OT_NORMAL, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), setenv, success, and unsetenv.

Referenced by exec_command().

◆ exec_command_sf_sv()

static backslashResult exec_command_sf_sv ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd,
bool  is_func 
)
static

Definition at line 2976 of file command.c.

2978{
2980
2981 if (active_branch)
2982 {
2983 bool show_linenumbers = (strchr(cmd, '+') != NULL);
2985 char *obj_desc;
2986 Oid obj_oid = InvalidOid;
2988
2990 obj_desc = psql_scan_slash_option(scan_state,
2991 OT_WHOLE_LINE, NULL, true);
2992 if (!obj_desc)
2993 {
2994 if (is_func)
2995 pg_log_error("function name is required");
2996 else
2997 pg_log_error("view name is required");
2998 status = PSQL_CMD_ERROR;
2999 }
3000 else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
3001 {
3002 /* error already reported */
3003 status = PSQL_CMD_ERROR;
3004 }
3005 else if (!get_create_object_cmd(eot, obj_oid, buf))
3006 {
3007 /* error already reported */
3008 status = PSQL_CMD_ERROR;
3009 }
3010 else
3011 {
3012 FILE *output;
3013 bool is_pager;
3014
3015 /* Select output stream: stdout, pager, or file */
3016 if (pset.queryFout == stdout)
3017 {
3018 /* count lines in function to see if pager is needed */
3019 int lineno = count_lines_in_buf(buf);
3020
3021 output = PageOutput(lineno, &(pset.popt.topt));
3022 is_pager = true;
3023 }
3024 else
3025 {
3026 /* use previously set output file, without pager */
3028 is_pager = false;
3029 }
3030
3031 if (show_linenumbers)
3032 {
3033 /* add line numbers */
3034 print_with_linenumbers(output, buf->data, is_func);
3035 }
3036 else
3037 {
3038 /* just send the definition to output */
3039 fputs(buf->data, output);
3040 }
3041
3042 if (is_pager)
3044 }
3045
3046 free(obj_desc);
3048 }
3049 else
3050 ignore_slash_whole_line(scan_state);
3051
3052 return status;
3053}
static int count_lines_in_buf(PQExpBuffer buf)
Definition: command.c:6427
static void print_with_linenumbers(FILE *output, char *lines, bool is_func)
Definition: command.c:6457
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3089
void ClosePager(FILE *pagerpipe)
Definition: print.c:3141
FILE * output
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:72
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:114

References buf, ClosePager(), count_lines_in_buf(), createPQExpBuffer(), destroyPQExpBuffer(), EditableFunction, EditableView, free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), OT_WHOLE_LINE, output, PageOutput(), pg_log_error, _psqlSettings::popt, print_with_linenumbers(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::queryFout, generate_unaccent_rules::stdout, and printQueryOpt::topt.

Referenced by exec_command().

◆ exec_command_shell_escape()

static backslashResult exec_command_shell_escape ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3579 of file command.c.

3580{
3581 bool success = true;
3582
3583 if (active_branch)
3584 {
3585 char *opt = psql_scan_slash_option(scan_state,
3586 OT_WHOLE_LINE, NULL, false);
3587
3588 success = do_shell(opt);
3589 free(opt);
3590 }
3591 else
3592 ignore_slash_whole_line(scan_state);
3593
3595}
static bool do_shell(const char *command)
Definition: command.c:5825

References do_shell(), free, ignore_slash_whole_line(), OT_WHOLE_LINE, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and success.

Referenced by exec_command().

◆ exec_command_slash_command_help()

static backslashResult exec_command_slash_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3601 of file command.c.

3602{
3603 if (active_branch)
3604 {
3605 char *opt0 = psql_scan_slash_option(scan_state,
3606 OT_NORMAL, NULL, false);
3607
3608 if (!opt0 || strcmp(opt0, "commands") == 0)
3610 else if (strcmp(opt0, "options") == 0)
3612 else if (strcmp(opt0, "variables") == 0)
3614 else
3616
3617 free(opt0);
3618 }
3619 else
3620 ignore_slash_options(scan_state);
3621
3622 return PSQL_CMD_SKIP_LINE;
3623}
void helpVariables(unsigned short int pager)
Definition: help.c:374
void slashUsage(unsigned short int pager)
Definition: help.c:148
static void usage(const char *progname)
Definition: vacuumlo.c:414

References free, helpVariables(), ignore_slash_options(), OT_NORMAL, printTableOpt::pager, _psqlSettings::popt, pset, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), slashUsage(), printQueryOpt::topt, and usage().

Referenced by exec_command().

◆ exec_command_startpipeline()

static backslashResult exec_command_startpipeline ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3059 of file command.c.

3060{
3062
3063 if (active_branch)
3064 {
3066 status = PSQL_CMD_SEND;
3067 }
3068 else
3069 ignore_slash_options(scan_state);
3070
3071 return status;
3072}
@ PSQL_SEND_START_PIPELINE_MODE
Definition: settings.h:79

References ignore_slash_options(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_SEND_START_PIPELINE_MODE, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_syncpipeline()

static backslashResult exec_command_syncpipeline ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3078 of file command.c.

3079{
3081
3082 if (active_branch)
3083 {
3085 status = PSQL_CMD_SEND;
3086 }
3087 else
3088 ignore_slash_options(scan_state);
3089
3090 return status;
3091}
@ PSQL_SEND_PIPELINE_SYNC
Definition: settings.h:78

References ignore_slash_options(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, PSQL_SEND_PIPELINE_SYNC, and _psqlSettings::send_mode.

Referenced by exec_command().

◆ exec_command_t()

static backslashResult exec_command_t ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3116 of file command.c.

3117{
3118 bool success = true;
3119
3120 if (active_branch)
3121 {
3122 char *opt = psql_scan_slash_option(scan_state,
3123 OT_NORMAL, NULL, true);
3124
3125 success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
3126 free(opt);
3127 }
3128 else
3129 ignore_slash_options(scan_state);
3130
3132}

References do_pset(), free, ignore_slash_options(), OT_NORMAL, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, and success.

Referenced by exec_command().

◆ exec_command_T()

static backslashResult exec_command_T ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3138 of file command.c.

3139{
3140 bool success = true;
3141
3142 if (active_branch)
3143 {
3144 char *value = psql_scan_slash_option(scan_state,
3145 OT_NORMAL, NULL, false);
3146
3147 success = do_pset("tableattr", value, &pset.popt, pset.quiet);
3148 free(value);
3149 }
3150 else
3151 ignore_slash_options(scan_state);
3152
3154}

References do_pset(), free, ignore_slash_options(), OT_NORMAL, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, success, and value.

Referenced by exec_command().

◆ exec_command_timing()

static backslashResult exec_command_timing ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3160 of file command.c.

3161{
3162 bool success = true;
3163
3164 if (active_branch)
3165 {
3166 char *opt = psql_scan_slash_option(scan_state,
3167 OT_NORMAL, NULL, false);
3168
3169 if (opt)
3170 success = ParseVariableBool(opt, "\\timing", &pset.timing);
3171 else
3173 if (!pset.quiet)
3174 {
3175 if (pset.timing)
3176 puts(_("Timing is on."));
3177 else
3178 puts(_("Timing is off."));
3179 }
3180 free(opt);
3181 }
3182 else
3183 ignore_slash_options(scan_state);
3184
3186}

References _, free, ignore_slash_options(), OT_NORMAL, ParseVariableBool(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, success, and _psqlSettings::timing.

Referenced by exec_command().

◆ exec_command_unrestrict()

static backslashResult exec_command_unrestrict ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 3192 of file command.c.

3194{
3195 if (active_branch)
3196 {
3197 char *opt;
3198
3199 opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
3200 if (opt == NULL || opt[0] == '\0')
3201 {
3202 pg_log_error("\\%s: missing required argument", cmd);
3203 return PSQL_CMD_ERROR;
3204 }
3205
3206 if (!restricted)
3207 {
3208 pg_log_error("\\%s: not currently in restricted mode", cmd);
3209 return PSQL_CMD_ERROR;
3210 }
3211 else if (strcmp(opt, restrict_key) == 0)
3212 {
3214 restricted = false;
3215 }
3216 else
3217 {
3218 pg_log_error("\\%s: wrong key", cmd);
3219 return PSQL_CMD_ERROR;
3220 }
3221 }
3222 else
3223 ignore_slash_options(scan_state);
3224
3225 return PSQL_CMD_SKIP_LINE;
3226}

References ignore_slash_options(), OT_NORMAL, pfree(), pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), restrict_key, and restricted.

Referenced by exec_command().

◆ exec_command_unset()

static backslashResult exec_command_unset ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 3232 of file command.c.

3234{
3235 bool success = true;
3236
3237 if (active_branch)
3238 {
3239 char *opt = psql_scan_slash_option(scan_state,
3240 OT_NORMAL, NULL, false);
3241
3242 if (!opt)
3243 {
3244 pg_log_error("\\%s: missing required argument", cmd);
3245 success = false;
3246 }
3247 else if (!SetVariable(pset.vars, opt, NULL))
3248 success = false;
3249
3250 free(opt);
3251 }
3252 else
3253 ignore_slash_options(scan_state);
3254
3256}

References free, ignore_slash_options(), OT_NORMAL, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), SetVariable(), success, and _psqlSettings::vars.

Referenced by exec_command().

◆ exec_command_watch()

static backslashResult exec_command_watch ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 3364 of file command.c.

3366{
3367 bool success = true;
3368
3369 if (active_branch)
3370 {
3371 bool have_sleep = false;
3372 bool have_iter = false;
3373 bool have_min_rows = false;
3374 double sleep = pset.watch_interval;
3375 int iter = 0;
3376 int min_rows = 0;
3377
3379 {
3380 pg_log_error("\\%s not allowed in pipeline mode", "watch");
3382 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 */
3392 while (success)
3393 {
3394 char *opt = psql_scan_slash_option(scan_state,
3395 OT_NORMAL, NULL, true);
3396 char *valptr;
3397 char *opt_end;
3398
3399 if (!opt)
3400 break; /* no more arguments */
3401
3402 valptr = strchr(opt, '=');
3403 if (valptr)
3404 {
3405 /* Labeled argument */
3406 valptr++;
3407 if (strncmp("i=", opt, strlen("i=")) == 0 ||
3408 strncmp("interval=", opt, strlen("interval=")) == 0)
3409 {
3410 if (have_sleep)
3411 {
3412 pg_log_error("\\watch: interval value is specified more than once");
3413 success = false;
3414 }
3415 else
3416 {
3417 have_sleep = true;
3418 errno = 0;
3419 sleep = strtod(valptr, &opt_end);
3420 if (sleep < 0 || *opt_end || errno == ERANGE)
3421 {
3422 pg_log_error("\\watch: incorrect interval value \"%s\"", valptr);
3423 success = false;
3424 }
3425 }
3426 }
3427 else if (strncmp("c=", opt, strlen("c=")) == 0 ||
3428 strncmp("count=", opt, strlen("count=")) == 0)
3429 {
3430 if (have_iter)
3431 {
3432 pg_log_error("\\watch: iteration count is specified more than once");
3433 success = false;
3434 }
3435 else
3436 {
3437 have_iter = true;
3438 errno = 0;
3439 iter = strtoint(valptr, &opt_end, 10);
3440 if (iter <= 0 || *opt_end || errno == ERANGE)
3441 {
3442 pg_log_error("\\watch: incorrect iteration count \"%s\"", valptr);
3443 success = false;
3444 }
3445 }
3446 }
3447 else if (strncmp("m=", opt, strlen("m=")) == 0 ||
3448 strncmp("min_rows=", opt, strlen("min_rows=")) == 0)
3449 {
3450 if (have_min_rows)
3451 {
3452 pg_log_error("\\watch: minimum row count specified more than once");
3453 success = false;
3454 }
3455 else
3456 {
3457 have_min_rows = true;
3458 errno = 0;
3459 min_rows = strtoint(valptr, &opt_end, 10);
3460 if (min_rows <= 0 || *opt_end || errno == ERANGE)
3461 {
3462 pg_log_error("\\watch: incorrect minimum row count \"%s\"", valptr);
3463 success = false;
3464 }
3465 }
3466 }
3467 else
3468 {
3469 pg_log_error("\\watch: unrecognized parameter \"%s\"", opt);
3470 success = false;
3471 }
3472 }
3473 else
3474 {
3475 /* Unlabeled argument: take it as interval */
3476 if (have_sleep)
3477 {
3478 pg_log_error("\\watch: interval value is specified more than once");
3479 success = false;
3480 }
3481 else
3482 {
3483 have_sleep = true;
3484 errno = 0;
3485 sleep = strtod(opt, &opt_end);
3486 if (sleep < 0 || *opt_end || errno == ERANGE)
3487 {
3488 pg_log_error("\\watch: incorrect interval value \"%s\"", opt);
3489 success = false;
3490 }
3491 }
3492 }
3493
3494 free(opt);
3495 }
3496
3497 /* If we parsed arguments successfully, do the command */
3498 if (success)
3499 {
3500 /* If query_buf is empty, recall and execute previous query */
3501 (void) copy_previous_query(query_buf, previous_buf);
3502
3503 success = do_watch(query_buf, sleep, iter, min_rows);
3504 }
3505
3506 /* Reset the query buffer as though for \r */
3507 resetPQExpBuffer(query_buf);
3508 psql_scan_reset(scan_state);
3509 }
3510 else
3511 ignore_slash_options(scan_state);
3512
3514}
static bool do_watch(PQExpBuffer query_buf, double sleep, int iter, int min_rows)
Definition: command.c:5876
int strtoint(const char *pg_restrict str, char **pg_restrict endptr, int base)
Definition: string.c:50
double watch_interval
Definition: settings.h:175

References clean_extended_state(), copy_previous_query(), _psqlSettings::db, do_watch(), free, ignore_slash_options(), OT_NORMAL, pg_log_error, PQ_PIPELINE_OFF, PQpipelineStatus(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_reset(), psql_scan_slash_option(), resetPQExpBuffer(), strtoint(), success, and _psqlSettings::watch_interval.

Referenced by exec_command().

◆ exec_command_write()

static backslashResult exec_command_write ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 3262 of file command.c.

3265{
3267
3268 if (active_branch)
3269 {
3270 char *fname = psql_scan_slash_option(scan_state,
3271 OT_FILEPIPE, NULL, true);
3272 FILE *fd = NULL;
3273 bool is_pipe = false;
3274
3275 if (!query_buf)
3276 {
3277 pg_log_error("no query buffer");
3278 status = PSQL_CMD_ERROR;
3279 }
3280 else
3281 {
3282 if (!fname)
3283 {
3284 pg_log_error("\\%s: missing required argument", cmd);
3285 status = PSQL_CMD_ERROR;
3286 }
3287 else
3288 {
3289 expand_tilde(&fname);
3290 if (fname[0] == '|')
3291 {
3292 is_pipe = true;
3293 fflush(NULL);
3295 fd = popen(&fname[1], "w");
3296 }
3297 else
3298 {
3300 fd = fopen(fname, "w");
3301 }
3302 if (!fd)
3303 {
3304 pg_log_error("%s: %m", fname);
3305 status = PSQL_CMD_ERROR;
3306 }
3307 }
3308 }
3309
3310 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 if (query_buf && query_buf->len > 0)
3321 fprintf(fd, "%s\n", query_buf->data);
3322 else if (previous_buf && previous_buf->len > 0)
3323 fprintf(fd, "%s\n", previous_buf->data);
3324
3325 if (is_pipe)
3326 {
3327 result = pclose(fd);
3328
3329 if (result != 0)
3330 {
3331 pg_log_error("%s: %s", fname, wait_result_to_str(result));
3332 status = PSQL_CMD_ERROR;
3333 }
3335 }
3336 else
3337 {
3338 result = fclose(fd);
3339
3340 if (result == EOF)
3341 {
3342 pg_log_error("%s: %m", fname);
3343 status = PSQL_CMD_ERROR;
3344 }
3345 }
3346 }
3347
3348 if (is_pipe)
3350
3351 free(fname);
3352 }
3353 else
3354 ignore_slash_filepipe(scan_state);
3355
3356 return status;
3357}
char * wait_result_to_str(int exitstatus)
Definition: wait_error.c:33

References canonicalize_path_enc(), PQExpBufferData::data, disable_sigpipe_trap(), _psqlSettings::encoding, expand_tilde(), fd(), fprintf, free, ignore_slash_filepipe(), PQExpBufferData::len, OT_FILEPIPE, pg_log_error, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), restore_sigpipe_trap(), SetShellResultVariables(), and wait_result_to_str().

Referenced by exec_command().

◆ exec_command_x()

static backslashResult exec_command_x ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 3520 of file command.c.

3521{
3522 bool success = true;
3523
3524 if (active_branch)
3525 {
3526 char *opt = psql_scan_slash_option(scan_state,
3527 OT_NORMAL, NULL, true);
3528
3529 success = do_pset("expanded", opt, &pset.popt, pset.quiet);
3530 free(opt);
3531 }
3532 else
3533 ignore_slash_options(scan_state);
3534
3536}

References do_pset(), free, ignore_slash_options(), OT_NORMAL, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::quiet, and success.

Referenced by exec_command().

◆ exec_command_z()

static backslashResult exec_command_z ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 3542 of file command.c.

3543{
3544 bool success = true;
3545
3546 if (active_branch)
3547 {
3548 char *pattern;
3549 bool show_system;
3550 unsigned short int save_expanded;
3551
3552 pattern = psql_scan_slash_option(scan_state,
3553 OT_NORMAL, NULL, true);
3554
3555 show_system = strchr(cmd, 'S') ? true : false;
3556
3557 /* if 'x' option specified, force expanded mode */
3558 save_expanded = pset.popt.topt.expanded;
3559 if (strchr(cmd, 'x'))
3560 pset.popt.topt.expanded = 1;
3561
3562 success = permissionsList(pattern, show_system);
3563
3564 /* restore original expanded mode */
3565 pset.popt.topt.expanded = save_expanded;
3566
3567 free(pattern);
3568 }
3569 else
3570 ignore_slash_options(scan_state);
3571
3573}

References printTableOpt::expanded, free, ignore_slash_options(), OT_NORMAL, permissionsList(), _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), success, printQueryOpt::topt, and true.

Referenced by exec_command().

◆ gather_boolean_expression()

static PQExpBuffer gather_boolean_expression ( PsqlScanState  scan_state)
static

Definition at line 3672 of file command.c.

3673{
3674 PQExpBuffer exp_buf = createPQExpBuffer();
3675 int num_options = 0;
3676 char *value;
3677
3678 /* collect all arguments for the conditional command into exp_buf */
3679 while ((value = psql_scan_slash_option(scan_state,
3680 OT_NORMAL, NULL, false)) != NULL)
3681 {
3682 /* add spaces between tokens */
3683 if (num_options > 0)
3684 appendPQExpBufferChar(exp_buf, ' ');
3685 appendPQExpBufferStr(exp_buf, value);
3686 num_options++;
3687 free(value);
3688 }
3689
3690 return exp_buf;
3691}

References appendPQExpBufferChar(), appendPQExpBufferStr(), createPQExpBuffer(), free, OT_NORMAL, psql_scan_slash_option(), and value.

Referenced by ignore_boolean_expression(), and is_true_boolean_expression().

◆ get_create_object_cmd()

static bool get_create_object_cmd ( EditableObjectType  obj_type,
Oid  oid,
PQExpBuffer  buf 
)
static

Definition at line 6209 of file command.c.

6211{
6212 bool result = true;
6214 PGresult *res;
6215
6216 switch (obj_type)
6217 {
6218 case EditableFunction:
6219 printfPQExpBuffer(query,
6220 "SELECT pg_catalog.pg_get_functiondef(%u)",
6221 oid);
6222 break;
6223
6224 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 */
6238 if (pset.sversion >= 90400)
6239 {
6240 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 {
6253 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 }
6263 break;
6264 }
6265
6266 if (!echo_hidden_command(query->data))
6267 {
6268 destroyPQExpBuffer(query);
6269 return false;
6270 }
6271 res = PQexec(pset.db, query->data);
6272 if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
6273 {
6275 switch (obj_type)
6276 {
6277 case EditableFunction:
6278 appendPQExpBufferStr(buf, PQgetvalue(res, 0, 0));
6279 break;
6280
6281 case EditableView:
6282 {
6283 char *nspname = PQgetvalue(res, 0, 0);
6284 char *relname = PQgetvalue(res, 0, 1);
6285 char *relkind = PQgetvalue(res, 0, 2);
6286 char *viewdef = PQgetvalue(res, 0, 3);
6287 char *reloptions = PQgetvalue(res, 0, 4);
6288 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 */
6296 switch (relkind[0])
6297 {
6298#ifdef NOT_USED
6299 case RELKIND_MATVIEW:
6300 appendPQExpBufferStr(buf, "CREATE OR REPLACE MATERIALIZED VIEW ");
6301 break;
6302#endif
6303 case RELKIND_VIEW:
6304 appendPQExpBufferStr(buf, "CREATE OR REPLACE VIEW ");
6305 break;
6306 default:
6307 pg_log_error("\"%s.%s\" is not a view",
6308 nspname, relname);
6309 result = false;
6310 break;
6311 }
6312 appendPQExpBuffer(buf, "%s.", fmtId(nspname));
6314
6315 /* reloptions, if not an empty array "{}" */
6316 if (reloptions != NULL && strlen(reloptions) > 2)
6317 {
6318 appendPQExpBufferStr(buf, "\n WITH (");
6319 if (!appendReloptionsArray(buf, reloptions, "",
6320 pset.encoding,
6322 {
6323 pg_log_error("could not parse reloptions array");
6324 result = false;
6325 }
6327 }
6328
6329 /* View definition from pg_get_viewdef (a SELECT query) */
6330 appendPQExpBuffer(buf, " AS\n%s", viewdef);
6331
6332 /* Get rid of the semicolon that pg_get_viewdef appends */
6333 if (buf->len > 0 && buf->data[buf->len - 1] == ';')
6334 buf->data[--(buf->len)] = '\0';
6335
6336 /* WITH [LOCAL|CASCADED] CHECK OPTION */
6337 if (checkoption && checkoption[0] != '\0')
6338 appendPQExpBuffer(buf, "\n WITH %s CHECK OPTION",
6339 checkoption);
6340 }
6341 break;
6342 }
6343 /* Make sure result ends with a newline */
6344 if (buf->len > 0 && buf->data[buf->len - 1] != '\n')
6346 }
6347 else
6348 {
6350 result = false;
6351 }
6352
6353 PQclear(res);
6354 destroyPQExpBuffer(query);
6355
6356 return result;
6357}
bool standard_strings(void)
Definition: common.c:2500
static bool echo_hidden_command(const char *query)
Definition: command.c:6117
static void minimal_error_message(PGresult *res)
Definition: command.c:6499
PGresult * PQexec(PGconn *conn, const char *query)
Definition: fe-exec.c:2273
#define PQntuples
Definition: libpq-be-fe.h:251
@ PGRES_TUPLES_OK
Definition: libpq-fe.h:128
NameData relname
Definition: pg_class.h:38
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:265
const char * fmtId(const char *rawid)
Definition: string_utils.c:248
bool appendReloptionsArray(PQExpBuffer buffer, const char *reloptions, const char *prefix, int encoding, bool std_strings)
Definition: string_utils.c:966

References appendPQExpBuffer(), appendPQExpBufferChar(), appendPQExpBufferStr(), appendReloptionsArray(), buf, createPQExpBuffer(), PQExpBufferData::data, _psqlSettings::db, destroyPQExpBuffer(), echo_hidden_command(), EditableFunction, EditableView, _psqlSettings::encoding, fmtId(), minimal_error_message(), pg_log_error, PGRES_TUPLES_OK, PQclear, PQexec(), PQgetvalue, PQntuples, PQresultStatus, printfPQExpBuffer(), pset, relname, resetPQExpBuffer(), standard_strings(), and _psqlSettings::sversion.

Referenced by exec_command_ef_ev(), and exec_command_sf_sv().

◆ HandleSlashCmds()

backslashResult HandleSlashCmds ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)

Definition at line 231 of file command.c.

235{
236 backslashResult status;
237 char *cmd;
238 char *arg;
239
240 Assert(scan_state != NULL);
241 Assert(cstack != NULL);
242
243 /* Parse off the command name */
244 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 */
252 if (restricted && strcmp(cmd, "unrestrict") != 0)
253 {
254 pg_log_error("backslash commands are restricted; only \\unrestrict is allowed");
255 status = PSQL_CMD_ERROR;
256 }
257 else
258 status = exec_command(cmd, scan_state, cstack, query_buf, previous_buf);
259
260 if (status == PSQL_CMD_UNKNOWN)
261 {
262 pg_log_error("invalid command \\%s", cmd);
264 pg_log_error_hint("Try \\? for help.");
265 status = PSQL_CMD_ERROR;
266 }
267
268 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 */
275 bool active_branch = conditional_active(cstack);
276
278 while ((arg = psql_scan_slash_option(scan_state,
279 OT_NORMAL, NULL, false)))
280 {
281 if (active_branch)
282 pg_log_warning("\\%s: extra argument \"%s\" ignored", cmd, arg);
283 free(arg);
284 }
285 conditional_stack_pop(cstack);
286 }
287 else
288 {
289 /* silently throw away rest of line after an erroneous command */
290 while ((arg = psql_scan_slash_option(scan_state,
291 OT_WHOLE_LINE, NULL, false)))
292 free(arg);
293 }
294
295 /* if there is a trailing \\, swallow it */
296 psql_scan_slash_command_end(scan_state);
297
298 free(cmd);
299
300 /* some commands write to queryFout, so make sure output is sent */
301 fflush(pset.queryFout);
302
303 return status;
304}
static backslashResult exec_command(const char *cmd, PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:315
#define pg_log_error_hint(...)
Definition: logging.h:112
void * arg
void psql_scan_slash_command_end(PsqlScanState state)
char * psql_scan_slash_command(PsqlScanState state)

References arg, Assert(), conditional_active(), conditional_stack_pop(), conditional_stack_push(), _psqlSettings::cur_cmd_interactive, exec_command(), free, IFSTATE_IGNORED, OT_NORMAL, OT_WHOLE_LINE, pg_log_error, pg_log_error_hint, pg_log_warning, pset, PSQL_CMD_ERROR, PSQL_CMD_UNKNOWN, psql_scan_slash_command(), psql_scan_slash_command_end(), psql_scan_slash_option(), _psqlSettings::queryFout, and restricted.

Referenced by main(), and MainLoop().

◆ ignore_boolean_expression()

static void ignore_boolean_expression ( PsqlScanState  scan_state)
static

Definition at line 3719 of file command.c.

3720{
3722
3724}
static PQExpBuffer gather_boolean_expression(PsqlScanState scan_state)
Definition: command.c:3672

References buf, destroyPQExpBuffer(), and gather_boolean_expression().

Referenced by exec_command_elif(), and exec_command_if().

◆ ignore_slash_filepipe()

static void ignore_slash_filepipe ( PsqlScanState  scan_state)
static

Definition at line 3752 of file command.c.

3753{
3754 char *arg = psql_scan_slash_option(scan_state,
3755 OT_FILEPIPE, NULL, false);
3756
3757 free(arg);
3758}

References arg, free, OT_FILEPIPE, and psql_scan_slash_option().

Referenced by exec_command_out(), and exec_command_write().

◆ ignore_slash_options()

◆ ignore_slash_whole_line()

static void ignore_slash_whole_line ( PsqlScanState  scan_state)
static

Definition at line 3772 of file command.c.

3773{
3774 char *arg = psql_scan_slash_option(scan_state,
3775 OT_WHOLE_LINE, NULL, false);
3776
3777 free(arg);
3778}

References arg, free, OT_WHOLE_LINE, and psql_scan_slash_option().

Referenced by exec_command_copy(), exec_command_ef_ev(), exec_command_help(), exec_command_sf_sv(), and exec_command_shell_escape().

◆ is_branching_command()

static bool is_branching_command ( const char *  cmd)
static

Definition at line 3784 of file command.c.

3785{
3786 return (strcmp(cmd, "if") == 0 ||
3787 strcmp(cmd, "elif") == 0 ||
3788 strcmp(cmd, "else") == 0 ||
3789 strcmp(cmd, "endif") == 0);
3790}

Referenced by exec_command().

◆ is_true_boolean_expression()

static bool is_true_boolean_expression ( PsqlScanState  scan_state,
const char *  name 
)
static

Definition at line 3702 of file command.c.

3703{
3705 bool value = false;
3706 bool success = ParseVariableBool(buf->data, name, &value);
3707
3709 return success && value;
3710}

References buf, destroyPQExpBuffer(), gather_boolean_expression(), name, ParseVariableBool(), success, and value.

Referenced by exec_command_elif(), and exec_command_if().

◆ lookup_object_oid()

static bool lookup_object_oid ( EditableObjectType  obj_type,
const char *  desc,
Oid obj_oid 
)
static

Definition at line 6149 of file command.c.

6151{
6152 bool result = true;
6154 PGresult *res;
6155
6156 switch (obj_type)
6157 {
6158 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 appendPQExpBufferStr(query, "SELECT ");
6166 appendStringLiteralConn(query, desc, pset.db);
6167 appendPQExpBuffer(query, "::pg_catalog.%s::pg_catalog.oid",
6168 strchr(desc, '(') ? "regprocedure" : "regproc");
6169 break;
6170
6171 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 appendPQExpBufferStr(query, "SELECT ");
6179 appendStringLiteralConn(query, desc, pset.db);
6180 appendPQExpBufferStr(query, "::pg_catalog.regclass::pg_catalog.oid");
6181 break;
6182 }
6183
6184 if (!echo_hidden_command(query->data))
6185 {
6186 destroyPQExpBuffer(query);
6187 return false;
6188 }
6189 res = PQexec(pset.db, query->data);
6190 if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1)
6191 *obj_oid = atooid(PQgetvalue(res, 0, 0));
6192 else
6193 {
6195 result = false;
6196 }
6197
6198 PQclear(res);
6199 destroyPQExpBuffer(query);
6200
6201 return result;
6202}
#define atooid(x)
Definition: postgres_ext.h:43
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:446

References appendPQExpBuffer(), appendPQExpBufferStr(), appendStringLiteralConn(), atooid, createPQExpBuffer(), PQExpBufferData::data, _psqlSettings::db, destroyPQExpBuffer(), echo_hidden_command(), EditableFunction, EditableView, minimal_error_message(), PGRES_TUPLES_OK, PQclear, PQexec(), PQgetvalue, PQntuples, PQresultStatus, and pset.

Referenced by exec_command_ef_ev(), and exec_command_sf_sv().

◆ minimal_error_message()

static void minimal_error_message ( PGresult res)
static

Definition at line 6499 of file command.c.

6500{
6501 PQExpBuffer msg;
6502 const char *fld;
6503
6504 msg = createPQExpBuffer();
6505
6507 if (fld)
6508 printfPQExpBuffer(msg, "%s: ", fld);
6509 else
6510 printfPQExpBuffer(msg, "ERROR: ");
6512 if (fld)
6513 appendPQExpBufferStr(msg, fld);
6514 else
6515 appendPQExpBufferStr(msg, "(not available)");
6516 appendPQExpBufferChar(msg, '\n');
6517
6518 pg_log_error("%s", msg->data);
6519
6520 destroyPQExpBuffer(msg);
6521}
#define PQresultErrorField
Definition: libpq-be-fe.h:249
#define PG_DIAG_MESSAGE_PRIMARY
Definition: postgres_ext.h:58
#define PG_DIAG_SEVERITY
Definition: postgres_ext.h:55

References appendPQExpBufferChar(), appendPQExpBufferStr(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), PG_DIAG_MESSAGE_PRIMARY, PG_DIAG_SEVERITY, pg_log_error, PQresultErrorField, and printfPQExpBuffer().

Referenced by get_create_object_cmd(), and lookup_object_oid().

◆ param_is_newly_set()

static bool param_is_newly_set ( const char *  old_val,
const char *  new_val 
)
static

Definition at line 3892 of file command.c.

3893{
3894 if (new_val == NULL)
3895 return false;
3896
3897 if (old_val == NULL || strcmp(old_val, new_val) != 0)
3898 return true;
3899
3900 return false;
3901}

Referenced by do_connect().

◆ print_with_linenumbers()

static void print_with_linenumbers ( FILE *  output,
char *  lines,
bool  is_func 
)
static

Definition at line 6457 of file command.c.

6458{
6459 bool in_header = is_func;
6460 int lineno = 0;
6461
6462 while (*lines != '\0')
6463 {
6464 char *eol;
6465
6466 if (in_header &&
6467 (strncmp(lines, "AS ", 3) == 0 ||
6468 strncmp(lines, "BEGIN ", 6) == 0 ||
6469 strncmp(lines, "RETURN ", 7) == 0))
6470 in_header = false;
6471
6472 /* increment lineno only for body's lines */
6473 if (!in_header)
6474 lineno++;
6475
6476 /* find and mark end of current line */
6477 eol = strchr(lines, '\n');
6478 if (eol != NULL)
6479 *eol = '\0';
6480
6481 /* show current line as appropriate */
6482 if (in_header)
6483 fprintf(output, " %s\n", lines);
6484 else
6485 fprintf(output, "%-7d %s\n", lineno, lines);
6486
6487 /* advance to next line, if any */
6488 if (eol == NULL)
6489 break;
6490 lines = ++eol;
6491 }
6492}

References fprintf, and output.

Referenced by exec_command_sf_sv().

◆ printGSSInfo()

static void printGSSInfo ( void  )
static

Definition at line 4528 of file command.c.

4529{
4530 if (!PQgssEncInUse(pset.db))
4531 return; /* no GSSAPI encryption in use */
4532
4533 printf(_("GSSAPI-encrypted connection\n"));
4534}
int PQgssEncInUse(PGconn *conn)

References _, _psqlSettings::db, PQgssEncInUse(), printf, and pset.

Referenced by connection_warnings().

◆ printPsetInfo()

static bool printPsetInfo ( const char *  param,
printQueryOpt popt 
)
static

Definition at line 5429 of file command.c.

5430{
5431 Assert(param != NULL);
5432
5433 /* show border style/width */
5434 if (strcmp(param, "border") == 0)
5435 printf(_("Border style is %d.\n"), popt->topt.border);
5436
5437 /* show the target width for the wrapped format */
5438 else if (strcmp(param, "columns") == 0)
5439 {
5440 if (!popt->topt.columns)
5441 printf(_("Target width is unset.\n"));
5442 else
5443 printf(_("Target width is %d.\n"), popt->topt.columns);
5444 }
5445
5446 /* show expanded/vertical mode */
5447 else if (strcmp(param, "x") == 0 || strcmp(param, "expanded") == 0 || strcmp(param, "vertical") == 0)
5448 {
5449 if (popt->topt.expanded == 1)
5450 printf(_("Expanded display is on.\n"));
5451 else if (popt->topt.expanded == 2)
5452 printf(_("Expanded display is used automatically.\n"));
5453 else
5454 printf(_("Expanded display is off.\n"));
5455 }
5456
5457 /* show xheader width value */
5458 else if (strcmp(param, "xheader_width") == 0)
5459 {
5461 printf(_("Expanded header width is \"%s\".\n"), "full");
5463 printf(_("Expanded header width is \"%s\".\n"), "column");
5465 printf(_("Expanded header width is \"%s\".\n"), "page");
5467 printf(_("Expanded header width is %d.\n"), popt->topt.expanded_header_exact_width);
5468 }
5469
5470 /* show field separator for CSV format */
5471 else if (strcmp(param, "csv_fieldsep") == 0)
5472 {
5473 printf(_("Field separator for CSV is \"%s\".\n"),
5474 popt->topt.csvFieldSep);
5475 }
5476
5477 /* show field separator for unaligned text */
5478 else if (strcmp(param, "fieldsep") == 0)
5479 {
5480 if (popt->topt.fieldSep.separator_zero)
5481 printf(_("Field separator is zero byte.\n"));
5482 else
5483 printf(_("Field separator is \"%s\".\n"),
5484 popt->topt.fieldSep.separator);
5485 }
5486
5487 else if (strcmp(param, "fieldsep_zero") == 0)
5488 {
5489 printf(_("Field separator is zero byte.\n"));
5490 }
5491
5492 /* show disable "(x rows)" footer */
5493 else if (strcmp(param, "footer") == 0)
5494 {
5495 if (popt->topt.default_footer)
5496 printf(_("Default footer is on.\n"));
5497 else
5498 printf(_("Default footer is off.\n"));
5499 }
5500
5501 /* show format */
5502 else if (strcmp(param, "format") == 0)
5503 {
5504 printf(_("Output format is %s.\n"), _align2string(popt->topt.format));
5505 }
5506
5507 /* show table line style */
5508 else if (strcmp(param, "linestyle") == 0)
5509 {
5510 printf(_("Line style is %s.\n"),
5511 get_line_style(&popt->topt)->name);
5512 }
5513
5514 /* show null display */
5515 else if (strcmp(param, "null") == 0)
5516 {
5517 printf(_("Null display is \"%s\".\n"),
5518 popt->nullPrint ? popt->nullPrint : "");
5519 }
5520
5521 /* show locale-aware numeric output */
5522 else if (strcmp(param, "numericlocale") == 0)
5523 {
5524 if (popt->topt.numericLocale)
5525 printf(_("Locale-adjusted numeric output is on.\n"));
5526 else
5527 printf(_("Locale-adjusted numeric output is off.\n"));
5528 }
5529
5530 /* show toggle use of pager */
5531 else if (strcmp(param, "pager") == 0)
5532 {
5533 if (popt->topt.pager == 1)
5534 printf(_("Pager is used for long output.\n"));
5535 else if (popt->topt.pager == 2)
5536 printf(_("Pager is always used.\n"));
5537 else
5538 printf(_("Pager usage is off.\n"));
5539 }
5540
5541 /* show minimum lines for pager use */
5542 else if (strcmp(param, "pager_min_lines") == 0)
5543 {
5544 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 */
5551 else if (strcmp(param, "recordsep") == 0)
5552 {
5553 if (popt->topt.recordSep.separator_zero)
5554 printf(_("Record separator is zero byte.\n"));
5555 else if (strcmp(popt->topt.recordSep.separator, "\n") == 0)
5556 printf(_("Record separator is <newline>.\n"));
5557 else
5558 printf(_("Record separator is \"%s\".\n"),
5559 popt->topt.recordSep.separator);
5560 }
5561
5562 else if (strcmp(param, "recordsep_zero") == 0)
5563 {
5564 printf(_("Record separator is zero byte.\n"));
5565 }
5566
5567 /* show HTML table tag options */
5568 else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
5569 {
5570 if (popt->topt.tableAttr)
5571 printf(_("Table attributes are \"%s\".\n"),
5572 popt->topt.tableAttr);
5573 else
5574 printf(_("Table attributes unset.\n"));
5575 }
5576
5577 /* show title override */
5578 else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
5579 {
5580 if (popt->title)
5581 printf(_("Title is \"%s\".\n"), popt->title);
5582 else
5583 printf(_("Title is unset.\n"));
5584 }
5585
5586 /* show toggle between full and tuples-only format */
5587 else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
5588 {
5589 if (popt->topt.tuples_only)
5590 printf(_("Tuples only is on.\n"));
5591 else
5592 printf(_("Tuples only is off.\n"));
5593 }
5594
5595 /* Unicode style formatting */
5596 else if (strcmp(param, "unicode_border_linestyle") == 0)
5597 {
5598 printf(_("Unicode border line style is \"%s\".\n"),
5600 }
5601
5602 else if (strcmp(param, "unicode_column_linestyle") == 0)
5603 {
5604 printf(_("Unicode column line style is \"%s\".\n"),
5606 }
5607
5608 else if (strcmp(param, "unicode_header_linestyle") == 0)
5609 {
5610 printf(_("Unicode header line style is \"%s\".\n"),
5612 }
5613
5614 else
5615 {
5616 pg_log_error("\\pset: unknown option: %s", param);
5617 return false;
5618 }
5619
5620 return true;
5621}
#define ngettext(s, p, n)
Definition: c.h:1181
static const char * _unicode_linestyle2string(int linestyle)
Definition: command.c:5046
static const char * _align2string(enum printFormat in)
Definition: command.c:4990
const printTextFormat * get_line_style(const printTableOpt *opt)
Definition: print.c:3677
const char * name
Definition: print.h:84

References _, _align2string(), _unicode_linestyle2string(), Assert(), printTableOpt::border, printTableOpt::columns, printTableOpt::csvFieldSep, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::expanded_header_exact_width, printTableOpt::expanded_header_width_type, printTableOpt::fieldSep, printTableOpt::format, get_line_style(), printTextFormat::name, ngettext, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, pg_log_error, PRINT_XHEADER_COLUMN, PRINT_XHEADER_EXACT_WIDTH, PRINT_XHEADER_FULL, PRINT_XHEADER_PAGE, printf, printTableOpt::recordSep, separator::separator, separator::separator_zero, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, printTableOpt::tuples_only, printTableOpt::unicode_border_linestyle, printTableOpt::unicode_column_linestyle, and printTableOpt::unicode_header_linestyle.

Referenced by do_pset().

◆ printSSLInfo()

static void printSSLInfo ( void  )
static

Definition at line 4500 of file command.c.

4501{
4502 const char *protocol;
4503 const char *cipher;
4504 const char *compression;
4505 const char *alpn;
4506
4507 if (!PQsslInUse(pset.db))
4508 return; /* no SSL */
4509
4510 protocol = PQsslAttribute(pset.db, "protocol");
4511 cipher = PQsslAttribute(pset.db, "cipher");
4512 compression = PQsslAttribute(pset.db, "compression");
4513 alpn = PQsslAttribute(pset.db, "alpn");
4514
4515 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}

References _, _psqlSettings::db, PQsslAttribute(), PQsslInUse(), printf, and pset.

Referenced by connection_warnings().

◆ process_command_g_options()

static backslashResult process_command_g_options ( char *  first_option,
PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1800 of file command.c.

1802{
1803 bool success = true;
1804 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 if (first_option)
1813 option = first_option;
1814 else
1815 {
1816 option = psql_scan_slash_option(scan_state,
1817 OT_NORMAL, NULL, false);
1818 if (!option)
1819 {
1820 if (active_branch)
1821 {
1822 pg_log_error("\\%s: missing right parenthesis", cmd);
1823 success = false;
1824 }
1825 break;
1826 }
1827 }
1828
1829 /* Check for terminating right paren, and remove it from string */
1830 optlen = strlen(option);
1831 if (optlen > 0 && option[optlen - 1] == ')')
1832 {
1833 option[--optlen] = '\0';
1834 found_r_paren = true;
1835 }
1836
1837 /* If there was anything besides parentheses, parse/execute it */
1838 if (optlen > 0)
1839 {
1840 /* We can have either "name" or "name=value" */
1841 char *valptr = strchr(option, '=');
1842
1843 if (valptr)
1844 *valptr++ = '\0';
1845 if (active_branch)
1846 {
1847 /* save settings if not done already, then apply option */
1848 if (pset.gsavepopt == NULL)
1850 success &= do_pset(option, valptr, &pset.popt, true);
1851 }
1852 }
1853
1854 /* Clean up after this option. We should not free first_option. */
1855 if (first_option)
1856 first_option = NULL;
1857 else
1858 free(option);
1859 } while (!found_r_paren);
1860
1861 /* If we failed after already changing some options, undo side-effects */
1862 if (!success && active_branch && pset.gsavepopt)
1863 {
1865 pset.gsavepopt = NULL;
1866 }
1867
1869}
void restorePsetInfo(printQueryOpt *popt, printQueryOpt *save)
Definition: command.c:5665

References do_pset(), free, _psqlSettings::gsavepopt, OT_NORMAL, pg_log_error, _psqlSettings::popt, pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), restorePsetInfo(), savePsetInfo(), and success.

Referenced by exec_command_g().

◆ process_file()

int process_file ( char *  filename,
bool  use_relative_path 
)

Definition at line 4923 of file command.c.

4924{
4925 FILE *fd;
4926 int result;
4927 char *oldfilename;
4928 char relpath[MAXPGPATH];
4929
4930 if (!filename)
4931 {
4932 fd = stdin;
4933 filename = NULL;
4934 }
4935 else if (strcmp(filename, "-") != 0)
4936 {
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 */
4945 if (use_relative_path && pset.inputfile &&
4947 {
4952
4953 filename = relpath;
4954 }
4955
4956 fd = fopen(filename, PG_BINARY_R);
4957
4958 if (!fd)
4959 {
4960 pg_log_error("%s: %m", filename);
4961 return EXIT_FAILURE;
4962 }
4963 }
4964 else
4965 {
4966 fd = stdin;
4967 filename = "<stdin>"; /* for future error messages */
4968 }
4969
4970 oldfilename = pset.inputfile;
4972
4974
4975 result = MainLoop(fd);
4976
4977 if (fd != stdin)
4978 fclose(fd);
4979
4980 pset.inputfile = oldfilename;
4981
4983
4984 return result;
4985}
void pg_logging_config(int new_flags)
Definition: logging.c:166
#define PG_LOG_FLAG_TERSE
Definition: logging.h:86
int MainLoop(FILE *source)
Definition: mainloop.c:33
static char * filename
Definition: pg_dumpall.c:120
void join_path_components(char *ret_path, const char *head, const char *tail)
Definition: path.c:286
#define is_absolute_path(filename)
Definition: port.h:104
void get_parent_directory(char *path)
Definition: path.c:1068
bool has_drive_prefix(const char *path)
Definition: path.c:94
size_t strlcpy(char *dst, const char *src, size_t siz)
Definition: strlcpy.c:45
#define relpath(rlocator, forknum)
Definition: relpath.h:150
#define EXIT_FAILURE
Definition: settings.h:197

References canonicalize_path_enc(), _psqlSettings::encoding, EXIT_FAILURE, fd(), filename, get_parent_directory(), has_drive_prefix(), _psqlSettings::inputfile, is_absolute_path, join_path_components(), MainLoop(), MAXPGPATH, PG_BINARY_R, pg_log_error, PG_LOG_FLAG_TERSE, pg_logging_config(), pset, relpath, and strlcpy().

Referenced by exec_command_include().

◆ prompt_for_password()

static char * prompt_for_password ( const char *  username,
bool *  canceled 
)
static

Definition at line 3864 of file command.c.

3865{
3866 char *result;
3867 PromptInterruptContext prompt_ctx;
3868
3869 /* Set up to let SIGINT cancel simple_prompt_extended() */
3870 prompt_ctx.jmpbuf = sigint_interrupt_jmp;
3871 prompt_ctx.enabled = &sigint_interrupt_enabled;
3872 prompt_ctx.canceled = false;
3873
3874 if (username == NULL || username[0] == '\0')
3875 result = simple_prompt_extended("Password: ", false, &prompt_ctx);
3876 else
3877 {
3878 char *prompt_text;
3879
3880 prompt_text = psprintf(_("Password for user %s: "), username);
3881 result = simple_prompt_extended(prompt_text, false, &prompt_ctx);
3882 free(prompt_text);
3883 }
3884
3885 if (canceled)
3886 *canceled = prompt_ctx.canceled;
3887
3888 return result;
3889}
static char * username
Definition: initdb.c:153

References _, PromptInterruptContext::canceled, PromptInterruptContext::enabled, free, PromptInterruptContext::jmpbuf, psprintf(), sigint_interrupt_enabled, sigint_interrupt_jmp, simple_prompt_extended(), and username.

Referenced by do_connect().

◆ pset_bool_string()

static const char * pset_bool_string ( bool  val)
static

Definition at line 5691 of file command.c.

5692{
5693 return val ? "on" : "off";
5694}

References val.

Referenced by pset_value_string().

◆ pset_quoted_string()

static char * pset_quoted_string ( const char *  str)
static

Definition at line 5698 of file command.c.

5699{
5700 char *ret = pg_malloc(strlen(str) * 2 + 3);
5701 char *r = ret;
5702
5703 *r++ = '\'';
5704
5705 for (; *str; str++)
5706 {
5707 if (*str == '\n')
5708 {
5709 *r++ = '\\';
5710 *r++ = 'n';
5711 }
5712 else if (*str == '\'')
5713 {
5714 *r++ = '\\';
5715 *r++ = '\'';
5716 }
5717 else
5718 *r++ = *str;
5719 }
5720
5721 *r++ = '\'';
5722 *r = '\0';
5723
5724 return ret;
5725}
const char * str

References pg_malloc(), and str.

Referenced by pset_value_string().

◆ pset_value_string()

static char * pset_value_string ( const char *  param,
printQueryOpt popt 
)
static

Definition at line 5736 of file command.c.

5737{
5738 Assert(param != NULL);
5739
5740 if (strcmp(param, "border") == 0)
5741 return psprintf("%d", popt->topt.border);
5742 else if (strcmp(param, "columns") == 0)
5743 return psprintf("%d", popt->topt.columns);
5744 else if (strcmp(param, "csv_fieldsep") == 0)
5745 return pset_quoted_string(popt->topt.csvFieldSep);
5746 else if (strcmp(param, "expanded") == 0)
5747 return pstrdup(popt->topt.expanded == 2
5748 ? "auto"
5749 : pset_bool_string(popt->topt.expanded));
5750 else if (strcmp(param, "fieldsep") == 0)
5752 ? popt->topt.fieldSep.separator
5753 : "");
5754 else if (strcmp(param, "fieldsep_zero") == 0)
5756 else if (strcmp(param, "footer") == 0)
5758 else if (strcmp(param, "format") == 0)
5759 return pstrdup(_align2string(popt->topt.format));
5760 else if (strcmp(param, "linestyle") == 0)
5761 return pstrdup(get_line_style(&popt->topt)->name);
5762 else if (strcmp(param, "null") == 0)
5763 return pset_quoted_string(popt->nullPrint
5764 ? popt->nullPrint
5765 : "");
5766 else if (strcmp(param, "numericlocale") == 0)
5768 else if (strcmp(param, "pager") == 0)
5769 return psprintf("%d", popt->topt.pager);
5770 else if (strcmp(param, "pager_min_lines") == 0)
5771 return psprintf("%d", popt->topt.pager_min_lines);
5772 else if (strcmp(param, "recordsep") == 0)
5774 ? popt->topt.recordSep.separator
5775 : "");
5776 else if (strcmp(param, "recordsep_zero") == 0)
5778 else if (strcmp(param, "tableattr") == 0)
5779 return popt->topt.tableAttr ? pset_quoted_string(popt->topt.tableAttr) : pstrdup("");
5780 else if (strcmp(param, "title") == 0)
5781 return popt->title ? pset_quoted_string(popt->title) : pstrdup("");
5782 else if (strcmp(param, "tuples_only") == 0)
5784 else if (strcmp(param, "unicode_border_linestyle") == 0)
5786 else if (strcmp(param, "unicode_column_linestyle") == 0)
5788 else if (strcmp(param, "unicode_header_linestyle") == 0)
5790 else if (strcmp(param, "xheader_width") == 0)
5791 {
5793 return pstrdup("full");
5795 return pstrdup("column");
5797 return pstrdup("page");
5798 else
5799 {
5800 /* must be PRINT_XHEADER_EXACT_WIDTH */
5801 char wbuff[32];
5802
5803 snprintf(wbuff, sizeof(wbuff), "%d",
5805 return pstrdup(wbuff);
5806 }
5807 }
5808 else
5809 return pstrdup("ERROR");
5810}
static char * pset_quoted_string(const char *str)
Definition: command.c:5698
static const char * pset_bool_string(bool val)
Definition: command.c:5691

References _align2string(), _unicode_linestyle2string(), Assert(), printTableOpt::border, printTableOpt::columns, printTableOpt::csvFieldSep, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::expanded_header_exact_width, printTableOpt::expanded_header_width_type, printTableOpt::fieldSep, printTableOpt::format, get_line_style(), printTextFormat::name, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, PRINT_XHEADER_COLUMN, PRINT_XHEADER_FULL, PRINT_XHEADER_PAGE, pset_bool_string(), pset_quoted_string(), psprintf(), pstrdup(), printTableOpt::recordSep, separator::separator, separator::separator_zero, snprintf, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, printTableOpt::tuples_only, printTableOpt::unicode_border_linestyle, printTableOpt::unicode_column_linestyle, and printTableOpt::unicode_header_linestyle.

Referenced by exec_command_pset().

◆ read_connect_arg()

static char * read_connect_arg ( PsqlScanState  scan_state)
static

Definition at line 3632 of file command.c.

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 result = psql_scan_slash_option(scan_state, OT_SQLIDHACK, &quote, true);
3647
3648 if (!result)
3649 return NULL;
3650
3651 if (quote)
3652 return result;
3653
3654 if (*result == '\0' || strcmp(result, "-") == 0)
3655 {
3656 free(result);
3657 return NULL;
3658 }
3659
3660 return result;
3661}
@ OT_SQLIDHACK
Definition: psqlscanslash.h:19

References free, OT_SQLIDHACK, and psql_scan_slash_option().

Referenced by exec_command_connect().

◆ restorePsetInfo()

void restorePsetInfo ( printQueryOpt popt,
printQueryOpt save 
)

Definition at line 5665 of file command.c.

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 */
5670 free(popt->topt.fieldSep.separator);
5672 free(popt->topt.tableAttr);
5673 free(popt->nullPrint);
5674 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 */
5680 Assert(popt->footers == NULL);
5681 Assert(popt->translate_columns == NULL);
5682
5683 /* Now we may flat-copy all the fields, including pointers. */
5684 memcpy(popt, save, sizeof(printQueryOpt));
5685
5686 /* Lastly, free "save" ... but its sub-structures now belong to popt. */
5687 free(save);
5688}
const bool * translate_columns
Definition: print.h:190
char ** footers
Definition: print.h:188

References Assert(), printTableOpt::fieldSep, printQueryOpt::footers, free, printQueryOpt::nullPrint, printTableOpt::recordSep, separator::separator, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, and printQueryOpt::translate_columns.

Referenced by process_command_g_options(), and SendQuery().

◆ save_query_text_state()

static void save_query_text_state ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 3800 of file command.c.

3802{
3803 if (query_buf)
3804 conditional_stack_set_query_len(cstack, query_buf->len);
3806 psql_scan_get_paren_depth(scan_state));
3807}
void conditional_stack_set_paren_depth(ConditionalStack cstack, int depth)
Definition: conditional.c:173
void conditional_stack_set_query_len(ConditionalStack cstack, int len)
Definition: conditional.c:151
int psql_scan_get_paren_depth(PsqlScanState state)

References conditional_stack_set_paren_depth(), conditional_stack_set_query_len(), PQExpBufferData::len, and psql_scan_get_paren_depth().

Referenced by exec_command_elif(), exec_command_else(), and exec_command_if().

◆ savePsetInfo()

printQueryOpt * savePsetInfo ( const printQueryOpt popt)

Definition at line 5629 of file command.c.

5630{
5631 printQueryOpt *save;
5632
5633 save = (printQueryOpt *) pg_malloc(sizeof(printQueryOpt));
5634
5635 /* Flat-copy all the scalar fields, then duplicate sub-structures. */
5636 memcpy(save, popt, sizeof(printQueryOpt));
5637
5638 /* topt.line_style points to const data that need not be duplicated */
5639 if (popt->topt.fieldSep.separator)
5641 if (popt->topt.recordSep.separator)
5643 if (popt->topt.tableAttr)
5644 save->topt.tableAttr = pg_strdup(popt->topt.tableAttr);
5645 if (popt->nullPrint)
5646 save->nullPrint = pg_strdup(popt->nullPrint);
5647 if (popt->title)
5648 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 */
5654 Assert(popt->footers == NULL);
5655 Assert(popt->translate_columns == NULL);
5656
5657 return save;
5658}

References Assert(), printTableOpt::fieldSep, printQueryOpt::footers, printQueryOpt::nullPrint, pg_malloc(), pg_strdup(), printTableOpt::recordSep, separator::separator, printTableOpt::tableAttr, printQueryOpt::title, printQueryOpt::topt, and printQueryOpt::translate_columns.

Referenced by exec_command_g(), and process_command_g_options().

◆ set_unicode_line_style()

static bool set_unicode_line_style ( const char *  value,
size_t  vallen,
unicode_linestyle linestyle 
)
static

Definition at line 5033 of file command.c.

5035{
5036 if (pg_strncasecmp("single", value, vallen) == 0)
5037 *linestyle = UNICODE_LINESTYLE_SINGLE;
5038 else if (pg_strncasecmp("double", value, vallen) == 0)
5039 *linestyle = UNICODE_LINESTYLE_DOUBLE;
5040 else
5041 return false;
5042 return true;
5043}

References pg_strncasecmp(), UNICODE_LINESTYLE_DOUBLE, UNICODE_LINESTYLE_SINGLE, and value.

Referenced by do_pset().

◆ strip_lineno_from_objdesc()

static int strip_lineno_from_objdesc ( char *  obj)
static

Definition at line 6369 of file command.c.

6370{
6371 char *c;
6372 int lineno;
6373
6374 if (!obj || obj[0] == '\0')
6375 return -1;
6376
6377 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 while (c > obj && isascii((unsigned char) *c) && isspace((unsigned char) *c))
6391 c--;
6392
6393 /* must have a digit as last non-space char */
6394 if (c == obj || !isascii((unsigned char) *c) || !isdigit((unsigned char) *c))
6395 return -1;
6396
6397 /* find start of digit string */
6398 while (c > obj && isascii((unsigned char) *c) && isdigit((unsigned char) *c))
6399 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 ... */
6403 if (c == obj || !isascii((unsigned char) *c) ||
6404 !(isspace((unsigned char) *c) || *c == ')'))
6405 return -1;
6406
6407 /* parse digit string */
6408 c++;
6409 lineno = atoi(c);
6410 if (lineno < 1)
6411 {
6412 pg_log_error("invalid line number: %s", c);
6413 return 0;
6414 }
6415
6416 /* strip digit string from object name */
6417 *c = '\0';
6418
6419 return lineno;
6420}
char * c

References pg_log_error.

Referenced by exec_command_ef_ev().

◆ SyncVariables()

void SyncVariables ( void  )

Definition at line 4569 of file command.c.

4570{
4571 char vbuf[32];
4572 const char *server_version;
4573 char *service_name;
4574 char *service_file;
4575
4576 /* get stuff from connection */
4580
4582
4583 SetVariable(pset.vars, "DBNAME", PQdb(pset.db));
4584 SetVariable(pset.vars, "USER", PQuser(pset.db));
4585 SetVariable(pset.vars, "HOST", PQhost(pset.db));
4586 SetVariable(pset.vars, "PORT", PQport(pset.db));
4588
4589 service_name = get_conninfo_value("service");
4590 SetVariable(pset.vars, "SERVICE", service_name);
4591 if (service_name)
4592 pg_free(service_name);
4593
4594 service_file = get_conninfo_value("servicefile");
4595 SetVariable(pset.vars, "SERVICEFILE", service_file);
4596 if (service_file)
4597 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 */
4601 server_version = PQparameterStatus(pset.db, "server_version");
4602 /* Otherwise fall back on pset.sversion */
4603 if (!server_version)
4604 {
4605 formatPGVersionNumber(pset.sversion, true, vbuf, sizeof(vbuf));
4606 server_version = vbuf;
4607 }
4608 SetVariable(pset.vars, "SERVER_VERSION_NAME", server_version);
4609
4610 snprintf(vbuf, sizeof(vbuf), "%d", pset.sversion);
4611 SetVariable(pset.vars, "SERVER_VERSION_NUM", vbuf);
4612
4613 /* send stuff to it, too */
4616}
char * get_conninfo_value(const char *keyword)
Definition: common.c:2540
int PQserverVersion(const PGconn *conn)
Definition: fe-connect.c:7669
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7831
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7819
PGVerbosity verbosity
Definition: settings.h:184
PGContextVisibility show_context
Definition: settings.h:186

References _psqlSettings::db, _psqlSettings::encoding, printTableOpt::encoding, formatPGVersionNumber(), get_conninfo_value(), pg_encoding_to_char, pg_free(), _psqlSettings::popt, PQclientEncoding(), PQdb(), PQhost(), PQparameterStatus(), PQport(), PQserverVersion(), PQsetErrorContextVisibility(), PQsetErrorVerbosity(), PQuser(), pset, server_version, setFmtEncoding(), SetVariable(), _psqlSettings::show_context, snprintf, _psqlSettings::sversion, printQueryOpt::topt, _psqlSettings::vars, and _psqlSettings::verbosity.

Referenced by CheckConnection(), do_connect(), and main().

◆ UnsyncVariables()

void UnsyncVariables ( void  )

Definition at line 4624 of file command.c.

4625{
4626 SetVariable(pset.vars, "DBNAME", NULL);
4627 SetVariable(pset.vars, "SERVICE", NULL);
4628 SetVariable(pset.vars, "SERVICEFILE", NULL);
4629 SetVariable(pset.vars, "USER", NULL);
4630 SetVariable(pset.vars, "HOST", NULL);
4631 SetVariable(pset.vars, "PORT", NULL);
4632 SetVariable(pset.vars, "ENCODING", NULL);
4633 SetVariable(pset.vars, "SERVER_VERSION_NAME", NULL);
4634 SetVariable(pset.vars, "SERVER_VERSION_NUM", NULL);
4635}

References pset, SetVariable(), and _psqlSettings::vars.

Referenced by CheckConnection(), and do_connect().

◆ wait_until_connected()

static void wait_until_connected ( PGconn conn)
static

Definition at line 4379 of file command.c.

4380{
4381 bool forRead = false;
4382
4383 while (true)
4384 {
4385 int rc;
4386 int sock;
4388
4389 /*
4390 * On every iteration of the connection sequence, let's check if the
4391 * user has requested a cancellation.
4392 */
4393 if (cancel_pressed)
4394 break;
4395
4396 /*
4397 * Do not assume that the socket remains the same across
4398 * PQconnectPoll() calls.
4399 */
4400 sock = PQsocket(conn);
4401 if (sock == -1)
4402 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 */
4418 end_time = PQgetCurrentTimeUSec() + 1000000;
4419 rc = PQsocketPoll(sock, forRead, !forRead, end_time);
4420 if (rc == -1)
4421 return;
4422
4423 switch (PQconnectPoll(conn))
4424 {
4425 case PGRES_POLLING_OK:
4427 return;
4429 forRead = true;
4430 continue;
4432 forRead = false;
4433 continue;
4436 }
4437 }
4438}
#define pg_unreachable()
Definition: c.h:331
PostgresPollingStatusType PQconnectPoll(PGconn *conn)
Definition: fe-connect.c:2911
int PQsocket(const PGconn *conn)
Definition: fe-connect.c:7705
int PQsocketPoll(int sock, int forRead, int forWrite, pg_usec_time_t end_time)
Definition: fe-misc.c:1141
pg_usec_time_t PQgetCurrentTimeUSec(void)
Definition: fe-misc.c:1235
int64_t pg_usec_time_t
Definition: libpq-fe.h:238
@ PGRES_POLLING_ACTIVE
Definition: libpq-fe.h:119
@ PGRES_POLLING_OK
Definition: libpq-fe.h:118
@ PGRES_POLLING_READING
Definition: libpq-fe.h:116
@ PGRES_POLLING_WRITING
Definition: libpq-fe.h:117
@ PGRES_POLLING_FAILED
Definition: libpq-fe.h:115
static int64 end_time
Definition: pgbench.c:176
PGconn * conn
Definition: streamutil.c:52

References cancel_pressed, conn, end_time, pg_unreachable, PGRES_POLLING_ACTIVE, PGRES_POLLING_FAILED, PGRES_POLLING_OK, PGRES_POLLING_READING, PGRES_POLLING_WRITING, PQconnectPoll(), PQgetCurrentTimeUSec(), PQsocket(), and PQsocketPoll().

Referenced by do_connect().

Variable Documentation

◆ restrict_key

char* restrict_key
static

Definition at line 200 of file command.c.

Referenced by exec_command_restrict(), and exec_command_unrestrict().

◆ restricted