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 "crosstabview.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-fe.h"
#include "libpq/pqcomm.h"
#include "mainloop.h"
#include "portability/instr_time.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_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_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_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_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_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_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_s (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_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_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)
 
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 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)
 
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)
 

Macro Definition Documentation

◆ DEFAULT_SHELL

#define DEFAULT_SHELL   "/bin/sh"

Definition at line 4988 of file command.c.

Typedef Documentation

◆ EditableObjectType

Enumeration Type Documentation

◆ EditableObjectType

Enumerator
EditableFunction 
EditableView 

Definition at line 53 of file command.c.

54 {
EditableObjectType
Definition: command.c:54
@ EditableFunction
Definition: command.c:55
@ EditableView
Definition: command.c:56

Function Documentation

◆ _align2string()

static const char* _align2string ( enum printFormat  in)
static

Definition at line 4168 of file command.c.

4169 {
4170  switch (in)
4171  {
4172  case PRINT_NOTHING:
4173  return "nothing";
4174  break;
4175  case PRINT_ALIGNED:
4176  return "aligned";
4177  break;
4178  case PRINT_ASCIIDOC:
4179  return "asciidoc";
4180  break;
4181  case PRINT_CSV:
4182  return "csv";
4183  break;
4184  case PRINT_HTML:
4185  return "html";
4186  break;
4187  case PRINT_LATEX:
4188  return "latex";
4189  break;
4190  case PRINT_LATEX_LONGTABLE:
4191  return "latex-longtable";
4192  break;
4193  case PRINT_TROFF_MS:
4194  return "troff-ms";
4195  break;
4196  case PRINT_UNALIGNED:
4197  return "unaligned";
4198  break;
4199  case PRINT_WRAPPED:
4200  return "wrapped";
4201  break;
4202  }
4203  return "unknown";
4204 }
@ 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 4224 of file command.c.

4225 {
4226  switch (linestyle)
4227  {
4229  return "single";
4230  break;
4232  return "double";
4233  break;
4234  }
4235  return "unknown";
4236 }
@ UNICODE_LINESTYLE_SINGLE
Definition: print.h:98
@ UNICODE_LINESTYLE_DOUBLE
Definition: print.h:99

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 3638 of file command.c.

3639 {
3640  if (!pset.quiet && !pset.notty)
3641  {
3642  int client_ver = PG_VERSION_NUM;
3643  char cverbuf[32];
3644  char sverbuf[32];
3645 
3646  if (pset.sversion != client_ver)
3647  {
3648  const char *server_version;
3649 
3650  /* Try to get full text form, might include "devel" etc */
3651  server_version = PQparameterStatus(pset.db, "server_version");
3652  /* Otherwise fall back on pset.sversion */
3653  if (!server_version)
3654  {
3656  sverbuf, sizeof(sverbuf));
3657  server_version = sverbuf;
3658  }
3659 
3660  printf(_("%s (%s, server %s)\n"),
3661  pset.progname, PG_VERSION, server_version);
3662  }
3663  /* For version match, only print psql banner on startup. */
3664  else if (in_startup)
3665  printf("%s (%s)\n", pset.progname, PG_VERSION);
3666 
3667  /*
3668  * Warn if server's major version is newer than ours, or if server
3669  * predates our support cutoff (currently 9.2).
3670  */
3671  if (pset.sversion / 100 > client_ver / 100 ||
3672  pset.sversion < 90200)
3673  printf(_("WARNING: %s major version %s, server major version %s.\n"
3674  " Some psql features might not work.\n"),
3675  pset.progname,
3676  formatPGVersionNumber(client_ver, false,
3677  cverbuf, sizeof(cverbuf)),
3679  sverbuf, sizeof(sverbuf)));
3680 
3681 #ifdef WIN32
3682  if (in_startup)
3683  checkWin32Codepage();
3684 #endif
3685  printSSLInfo();
3686  printGSSInfo();
3687  }
3688 }
static void printSSLInfo(void)
Definition: command.c:3697
static void printGSSInfo(void)
Definition: command.c:3722
#define _(x)
Definition: elog.c:90
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6708
static int server_version
Definition: pg_dumpall.c:110
#define printf(...)
Definition: port.h:244
PsqlSettings pset
Definition: startup.c:32
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:177
PGconn * db
Definition: settings.h:82
const char * progname
Definition: settings.h:112

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 3110 of file command.c.

3111 {
3112  if (query_buf && query_buf->len == 0)
3113  {
3114  appendPQExpBufferStr(query_buf, previous_buf->data);
3115  return true;
3116  }
3117  return false;
3118 }
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 5587 of file command.c.

5588 {
5589  int lineno = 0;
5590  const char *lines = buf->data;
5591 
5592  while (*lines != '\0')
5593  {
5594  lineno++;
5595  /* find start of next line */
5596  lines = strchr(lines, '\n');
5597  if (!lines)
5598  break;
5599  lines++;
5600  }
5601 
5602  return lineno;
5603 }
static char * buf
Definition: pg_test_fsync.c:67

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 3084 of file command.c.

3086 {
3087  if (query_buf)
3088  {
3089  int new_len = conditional_stack_get_query_len(cstack);
3090 
3091  Assert(new_len >= 0 && new_len <= query_buf->len);
3092  query_buf->len = new_len;
3093  query_buf->data[new_len] = '\0';
3094  }
3095  psql_scan_set_paren_depth(scan_state,
3097 }
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(fmt[strlen(fmt) - 1] !='\n')
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 3179 of file command.c.

3181 {
3182  PGconn *o_conn = pset.db,
3183  *n_conn = NULL;
3184  PQconninfoOption *cinfo;
3185  int nconnopts = 0;
3186  bool same_host = false;
3187  char *password = NULL;
3188  char *client_encoding;
3189  bool success = true;
3190  bool keep_password = true;
3191  bool has_connection_string;
3192  bool reuse_previous;
3193 
3194  has_connection_string = dbname ?
3196 
3197  /* Complain if we have additional arguments after a connection string. */
3198  if (has_connection_string && (user || host || port))
3199  {
3200  pg_log_error("Do not give user, host, or port separately when using a connection string");
3201  return false;
3202  }
3203 
3204  switch (reuse_previous_specification)
3205  {
3206  case TRI_YES:
3207  reuse_previous = true;
3208  break;
3209  case TRI_NO:
3210  reuse_previous = false;
3211  break;
3212  default:
3213  reuse_previous = !has_connection_string;
3214  break;
3215  }
3216 
3217  /*
3218  * If we intend to re-use connection parameters, collect them out of the
3219  * old connection, then replace individual values as necessary. (We may
3220  * need to resort to looking at pset.dead_conn, if the connection died
3221  * previously.) Otherwise, obtain a PQconninfoOption array containing
3222  * libpq's defaults, and modify that. Note this function assumes that
3223  * PQconninfo, PQconndefaults, and PQconninfoParse will all produce arrays
3224  * containing the same options in the same order.
3225  */
3226  if (reuse_previous)
3227  {
3228  if (o_conn)
3229  cinfo = PQconninfo(o_conn);
3230  else if (pset.dead_conn)
3231  cinfo = PQconninfo(pset.dead_conn);
3232  else
3233  {
3234  /* This is reachable after a non-interactive \connect failure */
3235  pg_log_error("No database connection exists to re-use parameters from");
3236  return false;
3237  }
3238  }
3239  else
3240  cinfo = PQconndefaults();
3241 
3242  if (cinfo)
3243  {
3244  if (has_connection_string)
3245  {
3246  /* Parse the connstring and insert values into cinfo */
3247  PQconninfoOption *replcinfo;
3248  char *errmsg;
3249 
3250  replcinfo = PQconninfoParse(dbname, &errmsg);
3251  if (replcinfo)
3252  {
3253  PQconninfoOption *ci;
3254  PQconninfoOption *replci;
3255  bool have_password = false;
3256 
3257  for (ci = cinfo, replci = replcinfo;
3258  ci->keyword && replci->keyword;
3259  ci++, replci++)
3260  {
3261  Assert(strcmp(ci->keyword, replci->keyword) == 0);
3262  /* Insert value from connstring if one was provided */
3263  if (replci->val)
3264  {
3265  /*
3266  * We know that both val strings were allocated by
3267  * libpq, so the least messy way to avoid memory leaks
3268  * is to swap them.
3269  */
3270  char *swap = replci->val;
3271 
3272  replci->val = ci->val;
3273  ci->val = swap;
3274 
3275  /*
3276  * Check whether connstring provides options affecting
3277  * password re-use. While any change in user, host,
3278  * hostaddr, or port causes us to ignore the old
3279  * connection's password, we don't force that for
3280  * dbname, since passwords aren't database-specific.
3281  */
3282  if (replci->val == NULL ||
3283  strcmp(ci->val, replci->val) != 0)
3284  {
3285  if (strcmp(replci->keyword, "user") == 0 ||
3286  strcmp(replci->keyword, "host") == 0 ||
3287  strcmp(replci->keyword, "hostaddr") == 0 ||
3288  strcmp(replci->keyword, "port") == 0)
3289  keep_password = false;
3290  }
3291  /* Also note whether connstring contains a password. */
3292  if (strcmp(replci->keyword, "password") == 0)
3293  have_password = true;
3294  }
3295  else if (!reuse_previous)
3296  {
3297  /*
3298  * When we have a connstring and are not re-using
3299  * parameters, swap *all* entries, even those not set
3300  * by the connstring. This avoids absorbing
3301  * environment-dependent defaults from the result of
3302  * PQconndefaults(). We don't want to do that because
3303  * they'd override service-file entries if the
3304  * connstring specifies a service parameter, whereas
3305  * the priority should be the other way around. libpq
3306  * can certainly recompute any defaults we don't pass
3307  * here. (In this situation, it's a bit wasteful to
3308  * have called PQconndefaults() at all, but not doing
3309  * so would require yet another major code path here.)
3310  */
3311  replci->val = ci->val;
3312  ci->val = NULL;
3313  }
3314  }
3315  Assert(ci->keyword == NULL && replci->keyword == NULL);
3316 
3317  /* While here, determine how many option slots there are */
3318  nconnopts = ci - cinfo;
3319 
3320  PQconninfoFree(replcinfo);
3321 
3322  /*
3323  * If the connstring contains a password, tell the loop below
3324  * that we may use it, regardless of other settings (i.e.,
3325  * cinfo's password is no longer an "old" password).
3326  */
3327  if (have_password)
3328  keep_password = true;
3329 
3330  /* Don't let code below try to inject dbname into params. */
3331  dbname = NULL;
3332  }
3333  else
3334  {
3335  /* PQconninfoParse failed */
3336  if (errmsg)
3337  {
3338  pg_log_error("%s", errmsg);
3339  PQfreemem(errmsg);
3340  }
3341  else
3342  pg_log_error("out of memory");
3343  success = false;
3344  }
3345  }
3346  else
3347  {
3348  /*
3349  * If dbname isn't a connection string, then we'll inject it and
3350  * the other parameters into the keyword array below. (We can't
3351  * easily insert them into the cinfo array because of memory
3352  * management issues: PQconninfoFree would misbehave on Windows.)
3353  * However, to avoid dependencies on the order in which parameters
3354  * appear in the array, make a preliminary scan to set
3355  * keep_password and same_host correctly.
3356  *
3357  * While any change in user, host, or port causes us to ignore the
3358  * old connection's password, we don't force that for dbname,
3359  * since passwords aren't database-specific.
3360  */
3361  PQconninfoOption *ci;
3362 
3363  for (ci = cinfo; ci->keyword; ci++)
3364  {
3365  if (user && strcmp(ci->keyword, "user") == 0)
3366  {
3367  if (!(ci->val && strcmp(user, ci->val) == 0))
3368  keep_password = false;
3369  }
3370  else if (host && strcmp(ci->keyword, "host") == 0)
3371  {
3372  if (ci->val && strcmp(host, ci->val) == 0)
3373  same_host = true;
3374  else
3375  keep_password = false;
3376  }
3377  else if (port && strcmp(ci->keyword, "port") == 0)
3378  {
3379  if (!(ci->val && strcmp(port, ci->val) == 0))
3380  keep_password = false;
3381  }
3382  }
3383 
3384  /* While here, determine how many option slots there are */
3385  nconnopts = ci - cinfo;
3386  }
3387  }
3388  else
3389  {
3390  /* We failed to create the cinfo structure */
3391  pg_log_error("out of memory");
3392  success = false;
3393  }
3394 
3395  /*
3396  * If the user asked to be prompted for a password, ask for one now. If
3397  * not, use the password from the old connection, provided the username
3398  * etc have not changed. Otherwise, try to connect without a password
3399  * first, and then ask for a password if needed.
3400  *
3401  * XXX: this behavior leads to spurious connection attempts recorded in
3402  * the postmaster's log. But libpq offers no API that would let us obtain
3403  * a password and then continue with the first connection attempt.
3404  */
3405  if (pset.getPassword == TRI_YES && success)
3406  {
3407  bool canceled = false;
3408 
3409  /*
3410  * If a connstring or URI is provided, we don't know which username
3411  * will be used, since we haven't dug that out of the connstring.
3412  * Don't risk issuing a misleading prompt. As in startup.c, it does
3413  * not seem worth working harder, since this getPassword setting is
3414  * normally only used in noninteractive cases.
3415  */
3416  password = prompt_for_password(has_connection_string ? NULL : user,
3417  &canceled);
3418  success = !canceled;
3419  }
3420 
3421  /*
3422  * Consider whether to force client_encoding to "auto" (overriding
3423  * anything in the connection string). We do so if we have a terminal
3424  * connection and there is no PGCLIENTENCODING environment setting.
3425  */
3426  if (pset.notty || getenv("PGCLIENTENCODING"))
3427  client_encoding = NULL;
3428  else
3429  client_encoding = "auto";
3430 
3431  /* Loop till we have a connection or fail, which we might've already */
3432  while (success)
3433  {
3434  const char **keywords = pg_malloc((nconnopts + 1) * sizeof(*keywords));
3435  const char **values = pg_malloc((nconnopts + 1) * sizeof(*values));
3436  int paramnum = 0;
3437  PQconninfoOption *ci;
3438 
3439  /*
3440  * Copy non-default settings into the PQconnectdbParams parameter
3441  * arrays; but inject any values specified old-style, as well as any
3442  * interactively-obtained password, and a couple of fields we want to
3443  * set forcibly.
3444  *
3445  * If you change this code, see also the initial-connection code in
3446  * main().
3447  */
3448  for (ci = cinfo; ci->keyword; ci++)
3449  {
3450  keywords[paramnum] = ci->keyword;
3451 
3452  if (dbname && strcmp(ci->keyword, "dbname") == 0)
3453  values[paramnum++] = dbname;
3454  else if (user && strcmp(ci->keyword, "user") == 0)
3455  values[paramnum++] = user;
3456  else if (host && strcmp(ci->keyword, "host") == 0)
3457  values[paramnum++] = host;
3458  else if (host && !same_host && strcmp(ci->keyword, "hostaddr") == 0)
3459  {
3460  /* If we're changing the host value, drop any old hostaddr */
3461  values[paramnum++] = NULL;
3462  }
3463  else if (port && strcmp(ci->keyword, "port") == 0)
3464  values[paramnum++] = port;
3465  /* If !keep_password, we unconditionally drop old password */
3466  else if ((password || !keep_password) &&
3467  strcmp(ci->keyword, "password") == 0)
3468  values[paramnum++] = password;
3469  else if (strcmp(ci->keyword, "fallback_application_name") == 0)
3470  values[paramnum++] = pset.progname;
3471  else if (client_encoding &&
3472  strcmp(ci->keyword, "client_encoding") == 0)
3473  values[paramnum++] = client_encoding;
3474  else if (ci->val)
3475  values[paramnum++] = ci->val;
3476  /* else, don't bother making libpq parse this keyword */
3477  }
3478  /* add array terminator */
3479  keywords[paramnum] = NULL;
3480  values[paramnum] = NULL;
3481 
3482  /* Note we do not want libpq to re-expand the dbname parameter */
3483  n_conn = PQconnectdbParams(keywords, values, false);
3484 
3485  pg_free(keywords);
3486  pg_free(values);
3487 
3488  if (PQstatus(n_conn) == CONNECTION_OK)
3489  break;
3490 
3491  /*
3492  * Connection attempt failed; either retry the connection attempt with
3493  * a new password, or give up.
3494  */
3496  {
3497  bool canceled = false;
3498 
3499  /*
3500  * Prompt for password using the username we actually connected
3501  * with --- it might've come out of "dbname" rather than "user".
3502  */
3503  password = prompt_for_password(PQuser(n_conn), &canceled);
3504  PQfinish(n_conn);
3505  n_conn = NULL;
3506  success = !canceled;
3507  continue;
3508  }
3509 
3510  /*
3511  * We'll report the error below ... unless n_conn is NULL, indicating
3512  * that libpq didn't have enough memory to make a PGconn.
3513  */
3514  if (n_conn == NULL)
3515  pg_log_error("out of memory");
3516 
3517  success = false;
3518  } /* end retry loop */
3519 
3520  /* Release locally allocated data, whether we succeeded or not */
3521  pg_free(password);
3522  PQconninfoFree(cinfo);
3523 
3524  if (!success)
3525  {
3526  /*
3527  * Failed to connect to the database. In interactive mode, keep the
3528  * previous connection to the DB; in scripting mode, close our
3529  * previous connection as well.
3530  */
3532  {
3533  if (n_conn)
3534  {
3535  pg_log_info("%s", PQerrorMessage(n_conn));
3536  PQfinish(n_conn);
3537  }
3538 
3539  /* pset.db is left unmodified */
3540  if (o_conn)
3541  pg_log_info("Previous connection kept");
3542  }
3543  else
3544  {
3545  if (n_conn)
3546  {
3547  pg_log_error("\\connect: %s", PQerrorMessage(n_conn));
3548  PQfinish(n_conn);
3549  }
3550 
3551  if (o_conn)
3552  {
3553  /*
3554  * Transition to having no connection.
3555  *
3556  * Unlike CheckConnection(), we close the old connection
3557  * immediately to prevent its parameters from being re-used.
3558  * This is so that a script cannot accidentally reuse
3559  * parameters it did not expect to. Otherwise, the state
3560  * cleanup should be the same as in CheckConnection().
3561  */
3562  PQfinish(o_conn);
3563  pset.db = NULL;
3564  ResetCancelConn();
3565  UnsyncVariables();
3566  }
3567 
3568  /* On the same reasoning, release any dead_conn to prevent reuse */
3569  if (pset.dead_conn)
3570  {
3572  pset.dead_conn = NULL;
3573  }
3574  }
3575 
3576  return false;
3577  }
3578 
3579  /*
3580  * Replace the old connection with the new one, and update
3581  * connection-dependent variables. Keep the resynchronization logic in
3582  * sync with CheckConnection().
3583  */
3584  PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
3585  pset.db = n_conn;
3586  SyncVariables();
3587  connection_warnings(false); /* Must be after SyncVariables */
3588 
3589  /* Tell the user about the new connection */
3590  if (!pset.quiet)
3591  {
3592  if (!o_conn ||
3593  param_is_newly_set(PQhost(o_conn), PQhost(pset.db)) ||
3594  param_is_newly_set(PQport(o_conn), PQport(pset.db)))
3595  {
3596  char *connhost = PQhost(pset.db);
3597  char *hostaddr = PQhostaddr(pset.db);
3598 
3599  if (is_unixsock_path(connhost))
3600  {
3601  /* hostaddr overrides connhost */
3602  if (hostaddr && *hostaddr)
3603  printf(_("You are now connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
3604  PQdb(pset.db), PQuser(pset.db), hostaddr, PQport(pset.db));
3605  else
3606  printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
3607  PQdb(pset.db), PQuser(pset.db), connhost, PQport(pset.db));
3608  }
3609  else
3610  {
3611  if (hostaddr && *hostaddr && strcmp(connhost, hostaddr) != 0)
3612  printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
3613  PQdb(pset.db), PQuser(pset.db), connhost, hostaddr, PQport(pset.db));
3614  else
3615  printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
3616  PQdb(pset.db), PQuser(pset.db), connhost, PQport(pset.db));
3617  }
3618  }
3619  else
3620  printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
3621  PQdb(pset.db), PQuser(pset.db));
3622  }
3623 
3624  /* Drop no-longer-needed connection(s) */
3625  if (o_conn)
3626  PQfinish(o_conn);
3627  if (pset.dead_conn)
3628  {
3630  pset.dead_conn = NULL;
3631  }
3632 
3633  return true;
3634 }
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:228
bool recognized_connection_string(const char *connstr)
Definition: common.c:2398
static Datum values[MAXATTR]
Definition: bootstrap.c:156
void ResetCancelConn(void)
Definition: cancel.c:107
static char * prompt_for_password(const char *username, bool *canceled)
Definition: command.c:3128
static bool param_is_newly_set(const char *old_val, const char *new_val)
Definition: command.c:3156
void UnsyncVariables(void)
Definition: command.c:3804
void SyncVariables(void)
Definition: command.c:3763
void connection_warnings(bool in_startup)
Definition: command.c:3638
int errmsg(const char *fmt,...)
Definition: elog.c:906
PQconninfoOption * PQconninfoParse(const char *conninfo, char **errmsg)
Definition: fe-connect.c:5357
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:6622
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:652
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:6589
PQconninfoOption * PQconndefaults(void)
Definition: fe-connect.c:1447
char * PQhostaddr(const PGconn *conn)
Definition: fe-connect.c:6645
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:6576
PQconninfoOption * PQconninfo(PGconn *conn)
Definition: fe-connect.c:6532
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6794
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6743
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6690
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4130
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6597
char * PQport(const PGconn *conn)
Definition: fe-connect.c:6658
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6911
void PQfreemem(void *ptr)
Definition: fe-exec.c:3865
void pg_free(void *ptr)
Definition: fe_memutils.c:105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
static bool success
Definition: initdb.c:170
return false
Definition: isn.c:131
@ CONNECTION_OK
Definition: libpq-fe.h:60
#define pg_log_error(...)
Definition: logging.h:106
#define pg_log_info(...)
Definition: logging.h:124
static char * user
Definition: pg_regress.c:93
static int port
Definition: pg_regress.c:90
static bool is_unixsock_path(const char *path)
Definition: pqcomm.h:55
static char * password
Definition: streamutil.c:53
char * dbname
Definition: streamutil.c:51
PGconn * dead_conn
Definition: settings.h:128
enum trivalue getPassword
Definition: settings.h:107
bool cur_cmd_interactive
Definition: settings.h:110
@ 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, NoticeProcessor(), _psqlSettings::notty, param_is_newly_set(), password, pg_free(), pg_log_error, pg_log_info, pg_malloc(), port, PQconndefaults(), PQconnectdbParams(), PQconnectionNeedsPassword(), 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, and values.

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 3906 of file command.c.

3908 {
3909  char fnametmp[MAXPGPATH];
3910  FILE *stream = NULL;
3911  const char *fname;
3912  bool error = false;
3913  int fd;
3914  struct stat before,
3915  after;
3916 
3917  if (filename_arg)
3918  fname = filename_arg;
3919  else
3920  {
3921  /* make a temp file to edit */
3922 #ifndef WIN32
3923  const char *tmpdir = getenv("TMPDIR");
3924 
3925  if (!tmpdir)
3926  tmpdir = "/tmp";
3927 #else
3928  char tmpdir[MAXPGPATH];
3929  int ret;
3930 
3931  ret = GetTempPath(MAXPGPATH, tmpdir);
3932  if (ret == 0 || ret > MAXPGPATH)
3933  {
3934  pg_log_error("could not locate temporary directory: %s",
3935  !ret ? strerror(errno) : "");
3936  return false;
3937  }
3938 #endif
3939 
3940  /*
3941  * No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
3942  * current directory to the supplied path unless we use only
3943  * backslashes, so we do that.
3944  */
3945 #ifndef WIN32
3946  snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
3947  "/", (int) getpid());
3948 #else
3949  snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
3950  "" /* trailing separator already present */ , (int) getpid());
3951 #endif
3952 
3953  fname = (const char *) fnametmp;
3954 
3955  fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
3956  if (fd != -1)
3957  stream = fdopen(fd, "w");
3958 
3959  if (fd == -1 || !stream)
3960  {
3961  pg_log_error("could not open temporary file \"%s\": %m", fname);
3962  error = true;
3963  }
3964  else
3965  {
3966  unsigned int ql = query_buf->len;
3967 
3968  /* force newline-termination of what we send to editor */
3969  if (ql > 0 && query_buf->data[ql - 1] != '\n')
3970  {
3971  appendPQExpBufferChar(query_buf, '\n');
3972  ql++;
3973  }
3974 
3975  if (fwrite(query_buf->data, 1, ql, stream) != ql)
3976  {
3977  pg_log_error("%s: %m", fname);
3978 
3979  if (fclose(stream) != 0)
3980  pg_log_error("%s: %m", fname);
3981 
3982  if (remove(fname) != 0)
3983  pg_log_error("%s: %m", fname);
3984 
3985  error = true;
3986  }
3987  else if (fclose(stream) != 0)
3988  {
3989  pg_log_error("%s: %m", fname);
3990  if (remove(fname) != 0)
3991  pg_log_error("%s: %m", fname);
3992  error = true;
3993  }
3994  else
3995  {
3996  struct utimbuf ut;
3997 
3998  /*
3999  * Try to set the file modification time of the temporary file
4000  * a few seconds in the past. Otherwise, the low granularity
4001  * (one second, or even worse on some filesystems) that we can
4002  * portably measure with stat(2) could lead us to not
4003  * recognize a modification, if the user typed very quickly.
4004  *
4005  * This is a rather unlikely race condition, so don't error
4006  * out if the utime(2) call fails --- that would make the cure
4007  * worse than the disease.
4008  */
4009  ut.modtime = ut.actime = time(NULL) - 2;
4010  (void) utime(fname, &ut);
4011  }
4012  }
4013  }
4014 
4015  if (!error && stat(fname, &before) != 0)
4016  {
4017  pg_log_error("%s: %m", fname);
4018  error = true;
4019  }
4020 
4021  /* call editor */
4022  if (!error)
4023  error = !editFile(fname, lineno);
4024 
4025  if (!error && stat(fname, &after) != 0)
4026  {
4027  pg_log_error("%s: %m", fname);
4028  error = true;
4029  }
4030 
4031  /* file was edited if the size or modification time has changed */
4032  if (!error &&
4033  (before.st_size != after.st_size ||
4034  before.st_mtime != after.st_mtime))
4035  {
4036  stream = fopen(fname, PG_BINARY_R);
4037  if (!stream)
4038  {
4039  pg_log_error("%s: %m", fname);
4040  error = true;
4041  }
4042  else
4043  {
4044  /* read file back into query_buf */
4045  char line[1024];
4046 
4047  resetPQExpBuffer(query_buf);
4048  while (fgets(line, sizeof(line), stream) != NULL)
4049  appendPQExpBufferStr(query_buf, line);
4050 
4051  if (ferror(stream))
4052  {
4053  pg_log_error("%s: %m", fname);
4054  error = true;
4055  resetPQExpBuffer(query_buf);
4056  }
4057  else if (edited)
4058  {
4059  *edited = true;
4060  }
4061 
4062  fclose(stream);
4063  }
4064  }
4065  else
4066  {
4067  /*
4068  * If the file was not modified, and the caller requested it, discard
4069  * the query buffer.
4070  */
4071  if (discard_on_quit)
4072  resetPQExpBuffer(query_buf);
4073  }
4074 
4075  /* remove temp file */
4076  if (!filename_arg)
4077  {
4078  if (remove(fname) == -1)
4079  {
4080  pg_log_error("%s: %m", fname);
4081  error = true;
4082  }
4083  }
4084 
4085  return !error;
4086 }
#define PG_BINARY_R
Definition: c.h:1211
static bool editFile(const char *fname, int lineno)
Definition: command.c:3824
#define MAXPGPATH
#define strerror
Definition: port.h:251
#define snprintf
Definition: port.h:238
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:492
static void error(void)
Definition: sql-dyntest.c:147
#define stat
Definition: win32_port.h:286

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 4254 of file command.c.

4255 {
4256  size_t vallen = 0;
4257 
4258  Assert(param != NULL);
4259 
4260  if (value)
4261  vallen = strlen(value);
4262 
4263  /* set format */
4264  if (strcmp(param, "format") == 0)
4265  {
4266  static const struct fmt
4267  {
4268  const char *name;
4269  enum printFormat number;
4270  } formats[] =
4271  {
4272  /* remember to update error message below when adding more */
4273  {"aligned", PRINT_ALIGNED},
4274  {"asciidoc", PRINT_ASCIIDOC},
4275  {"csv", PRINT_CSV},
4276  {"html", PRINT_HTML},
4277  {"latex", PRINT_LATEX},
4278  {"troff-ms", PRINT_TROFF_MS},
4279  {"unaligned", PRINT_UNALIGNED},
4280  {"wrapped", PRINT_WRAPPED}
4281  };
4282 
4283  if (!value)
4284  ;
4285  else
4286  {
4287  int match_pos = -1;
4288 
4289  for (int i = 0; i < lengthof(formats); i++)
4290  {
4291  if (pg_strncasecmp(formats[i].name, value, vallen) == 0)
4292  {
4293  if (match_pos < 0)
4294  match_pos = i;
4295  else
4296  {
4297  pg_log_error("\\pset: ambiguous abbreviation \"%s\" matches both \"%s\" and \"%s\"",
4298  value,
4299  formats[match_pos].name, formats[i].name);
4300  return false;
4301  }
4302  }
4303  }
4304  if (match_pos >= 0)
4305  popt->topt.format = formats[match_pos].number;
4306  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
4307  {
4308  /*
4309  * We must treat latex-longtable specially because latex is a
4310  * prefix of it; if both were in the table above, we'd think
4311  * "latex" is ambiguous.
4312  */
4314  }
4315  else
4316  {
4317  pg_log_error("\\pset: allowed formats are aligned, asciidoc, csv, html, latex, latex-longtable, troff-ms, unaligned, wrapped");
4318  return false;
4319  }
4320  }
4321  }
4322 
4323  /* set table line style */
4324  else if (strcmp(param, "linestyle") == 0)
4325  {
4326  if (!value)
4327  ;
4328  else if (pg_strncasecmp("ascii", value, vallen) == 0)
4329  popt->topt.line_style = &pg_asciiformat;
4330  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
4332  else if (pg_strncasecmp("unicode", value, vallen) == 0)
4333  popt->topt.line_style = &pg_utf8format;
4334  else
4335  {
4336  pg_log_error("\\pset: allowed line styles are ascii, old-ascii, unicode");
4337  return false;
4338  }
4339  }
4340 
4341  /* set unicode border line style */
4342  else if (strcmp(param, "unicode_border_linestyle") == 0)
4343  {
4344  if (!value)
4345  ;
4346  else if (set_unicode_line_style(value, vallen,
4348  refresh_utf8format(&(popt->topt));
4349  else
4350  {
4351  pg_log_error("\\pset: allowed Unicode border line styles are single, double");
4352  return false;
4353  }
4354  }
4355 
4356  /* set unicode column line style */
4357  else if (strcmp(param, "unicode_column_linestyle") == 0)
4358  {
4359  if (!value)
4360  ;
4361  else if (set_unicode_line_style(value, vallen,
4363  refresh_utf8format(&(popt->topt));
4364  else
4365  {
4366  pg_log_error("\\pset: allowed Unicode column line styles are single, double");
4367  return false;
4368  }
4369  }
4370 
4371  /* set unicode header line style */
4372  else if (strcmp(param, "unicode_header_linestyle") == 0)
4373  {
4374  if (!value)
4375  ;
4376  else if (set_unicode_line_style(value, vallen,
4378  refresh_utf8format(&(popt->topt));
4379  else
4380  {
4381  pg_log_error("\\pset: allowed Unicode header line styles are single, double");
4382  return false;
4383  }
4384  }
4385 
4386  /* set border style/width */
4387  else if (strcmp(param, "border") == 0)
4388  {
4389  if (value)
4390  popt->topt.border = atoi(value);
4391  }
4392 
4393  /* set expanded/vertical mode */
4394  else if (strcmp(param, "x") == 0 ||
4395  strcmp(param, "expanded") == 0 ||
4396  strcmp(param, "vertical") == 0)
4397  {
4398  if (value && pg_strcasecmp(value, "auto") == 0)
4399  popt->topt.expanded = 2;
4400  else if (value)
4401  {
4402  bool on_off;
4403 
4404  if (ParseVariableBool(value, NULL, &on_off))
4405  popt->topt.expanded = on_off ? 1 : 0;
4406  else
4407  {
4408  PsqlVarEnumError(param, value, "on, off, auto");
4409  return false;
4410  }
4411  }
4412  else
4413  popt->topt.expanded = !popt->topt.expanded;
4414  }
4415 
4416  /* header line width in expanded mode */
4417  else if (strcmp(param, "xheader_width") == 0)
4418  {
4419  if (! value)
4420  ;
4421  else if (pg_strcasecmp(value, "full") == 0)
4423  else if (pg_strcasecmp(value, "column") == 0)
4425  else if (pg_strcasecmp(value, "page") == 0)
4427  else
4428  {
4430  popt->topt.expanded_header_exact_width = atoi(value);
4431  if (popt->topt.expanded_header_exact_width == 0)
4432  {
4433  pg_log_error("\\pset: allowed xheader_width values are full (default), column, page, or a number specifying the exact width.");
4434  return false;
4435  }
4436  }
4437  }
4438 
4439  /* field separator for CSV format */
4440  else if (strcmp(param, "csv_fieldsep") == 0)
4441  {
4442  if (value)
4443  {
4444  /* CSV separator has to be a one-byte character */
4445  if (strlen(value) != 1)
4446  {
4447  pg_log_error("\\pset: csv_fieldsep must be a single one-byte character");
4448  return false;
4449  }
4450  if (value[0] == '"' || value[0] == '\n' || value[0] == '\r')
4451  {
4452  pg_log_error("\\pset: csv_fieldsep cannot be a double quote, a newline, or a carriage return");
4453  return false;
4454  }
4455  popt->topt.csvFieldSep[0] = value[0];
4456  }
4457  }
4458 
4459  /* locale-aware numeric output */
4460  else if (strcmp(param, "numericlocale") == 0)
4461  {
4462  if (value)
4463  return ParseVariableBool(value, param, &popt->topt.numericLocale);
4464  else
4465  popt->topt.numericLocale = !popt->topt.numericLocale;
4466  }
4467 
4468  /* null display */
4469  else if (strcmp(param, "null") == 0)
4470  {
4471  if (value)
4472  {
4473  free(popt->nullPrint);
4474  popt->nullPrint = pg_strdup(value);
4475  }
4476  }
4477 
4478  /* field separator for unaligned text */
4479  else if (strcmp(param, "fieldsep") == 0)
4480  {
4481  if (value)
4482  {
4483  free(popt->topt.fieldSep.separator);
4485  popt->topt.fieldSep.separator_zero = false;
4486  }
4487  }
4488 
4489  else if (strcmp(param, "fieldsep_zero") == 0)
4490  {
4491  free(popt->topt.fieldSep.separator);
4492  popt->topt.fieldSep.separator = NULL;
4493  popt->topt.fieldSep.separator_zero = true;
4494  }
4495 
4496  /* record separator for unaligned text */
4497  else if (strcmp(param, "recordsep") == 0)
4498  {
4499  if (value)
4500  {
4501  free(popt->topt.recordSep.separator);
4503  popt->topt.recordSep.separator_zero = false;
4504  }
4505  }
4506 
4507  else if (strcmp(param, "recordsep_zero") == 0)
4508  {
4509  free(popt->topt.recordSep.separator);
4510  popt->topt.recordSep.separator = NULL;
4511  popt->topt.recordSep.separator_zero = true;
4512  }
4513 
4514  /* toggle between full and tuples-only format */
4515  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
4516  {
4517  if (value)
4518  return ParseVariableBool(value, param, &popt->topt.tuples_only);
4519  else
4520  popt->topt.tuples_only = !popt->topt.tuples_only;
4521  }
4522 
4523  /* set title override */
4524  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
4525  {
4526  free(popt->title);
4527  if (!value)
4528  popt->title = NULL;
4529  else
4530  popt->title = pg_strdup(value);
4531  }
4532 
4533  /* set HTML table tag options */
4534  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
4535  {
4536  free(popt->topt.tableAttr);
4537  if (!value)
4538  popt->topt.tableAttr = NULL;
4539  else
4540  popt->topt.tableAttr = pg_strdup(value);
4541  }
4542 
4543  /* toggle use of pager */
4544  else if (strcmp(param, "pager") == 0)
4545  {
4546  if (value && pg_strcasecmp(value, "always") == 0)
4547  popt->topt.pager = 2;
4548  else if (value)
4549  {
4550  bool on_off;
4551 
4552  if (!ParseVariableBool(value, NULL, &on_off))
4553  {
4554  PsqlVarEnumError(param, value, "on, off, always");
4555  return false;
4556  }
4557  popt->topt.pager = on_off ? 1 : 0;
4558  }
4559  else if (popt->topt.pager == 1)
4560  popt->topt.pager = 0;
4561  else
4562  popt->topt.pager = 1;
4563  }
4564 
4565  /* set minimum lines for pager use */
4566  else if (strcmp(param, "pager_min_lines") == 0)
4567  {
4568  if (value)
4569  popt->topt.pager_min_lines = atoi(value);
4570  }
4571 
4572  /* disable "(x rows)" footer */
4573  else if (strcmp(param, "footer") == 0)
4574  {
4575  if (value)
4576  return ParseVariableBool(value, param, &popt->topt.default_footer);
4577  else
4578  popt->topt.default_footer = !popt->topt.default_footer;
4579  }
4580 
4581  /* set border style/width */
4582  else if (strcmp(param, "columns") == 0)
4583  {
4584  if (value)
4585  popt->topt.columns = atoi(value);
4586  }
4587  else
4588  {
4589  pg_log_error("\\pset: unknown option: %s", param);
4590  return false;
4591  }
4592 
4593  if (!quiet)
4594  printPsetInfo(param, &pset.popt);
4595 
4596  return true;
4597 }
#define lengthof(array)
Definition: c.h:724
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:4211
static bool printPsetInfo(const char *param, printQueryOpt *popt)
Definition: command.c:4603
const char * name
Definition: encode.c:561
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3676
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:75
@ PRINT_XHEADER_PAGE
Definition: print.h:74
@ PRINT_XHEADER_COLUMN
Definition: print.h:73
@ PRINT_XHEADER_FULL
Definition: print.h:72
printFormat
Definition: print.h:29
#define free(a)
Definition: header.h:65
static struct @143 value
int i
Definition: isn.c:73
static void const char * fmt
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:91
printTableOpt topt
Definition: print.h:180
char * nullPrint
Definition: print.h:181
char * title
Definition: print.h:182
unsigned short int expanded
Definition: print.h:111
unicode_linestyle unicode_border_linestyle
Definition: print.h:136
bool tuples_only
Definition: print.h:121
int columns
Definition: print.h:135
enum printFormat format
Definition: print.h:110
struct separator fieldSep
Definition: print.h:127
int expanded_header_exact_width
Definition: print.h:114
struct separator recordSep
Definition: print.h:128
printXheaderWidthType expanded_header_width_type
Definition: print.h:113
char csvFieldSep[2]
Definition: print.h:129
const printTextFormat * line_style
Definition: print.h:126
bool default_footer
Definition: print.h:124
int pager_min_lines
Definition: print.h:119
unsigned short int pager
Definition: print.h:117
char * tableAttr
Definition: print.h:132
bool numericLocale
Definition: print.h:130
unsigned short int border
Definition: print.h:115
unicode_linestyle unicode_header_linestyle
Definition: print.h:138
unicode_linestyle unicode_column_linestyle
Definition: print.h:137
bool separator_zero
Definition: print.h:105
char * separator
Definition: print.h:104
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:416
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

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, fmt, printTableOpt::format, free, i, lengthof, printTableOpt::line_style, name, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, ParseVariableBool(), 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 4998 of file command.c.

4999 {
5000  int result;
5001 
5002  fflush(NULL);
5003  if (!command)
5004  {
5005  char *sys;
5006  const char *shellName;
5007 
5008  shellName = getenv("SHELL");
5009 #ifdef WIN32
5010  if (shellName == NULL)
5011  shellName = getenv("COMSPEC");
5012 #endif
5013  if (shellName == NULL)
5014  shellName = DEFAULT_SHELL;
5015 
5016  /* See EDITOR handling comment for an explanation */
5017 #ifndef WIN32
5018  sys = psprintf("exec %s", shellName);
5019 #else
5020  sys = psprintf("\"%s\"", shellName);
5021 #endif
5022  result = system(sys);
5023  free(sys);
5024  }
5025  else
5026  result = system(command);
5027 
5028  if (result == 127 || result == -1)
5029  {
5030  pg_log_error("\\!: failed");
5031  return false;
5032  }
5033  return true;
5034 }
#define DEFAULT_SHELL
Definition: command.c:4988
static void const char fflush(stdout)
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46

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

Referenced by exec_command_shell_escape().

◆ do_watch()

static bool do_watch ( PQExpBuffer  query_buf,
double  sleep 
)
static

Definition at line 5043 of file command.c.

5044 {
5045  long sleep_ms = (long) (sleep * 1000);
5046  printQueryOpt myopt = pset.popt;
5047  const char *strftime_fmt;
5048  const char *user_title;
5049  char *title;
5050  const char *pagerprog = NULL;
5051  FILE *pagerpipe = NULL;
5052  int title_len;
5053  int res = 0;
5054 #ifndef WIN32
5055  sigset_t sigalrm_sigchld_sigint;
5056  sigset_t sigalrm_sigchld;
5057  sigset_t sigint;
5058  struct itimerval interval;
5059  bool done = false;
5060 #endif
5061 
5062  if (!query_buf || query_buf->len <= 0)
5063  {
5064  pg_log_error("\\watch cannot be used with an empty query");
5065  return false;
5066  }
5067 
5068 #ifndef WIN32
5069  sigemptyset(&sigalrm_sigchld_sigint);
5070  sigaddset(&sigalrm_sigchld_sigint, SIGCHLD);
5071  sigaddset(&sigalrm_sigchld_sigint, SIGALRM);
5072  sigaddset(&sigalrm_sigchld_sigint, SIGINT);
5073 
5074  sigemptyset(&sigalrm_sigchld);
5075  sigaddset(&sigalrm_sigchld, SIGCHLD);
5076  sigaddset(&sigalrm_sigchld, SIGALRM);
5077 
5078  sigemptyset(&sigint);
5079  sigaddset(&sigint, SIGINT);
5080 
5081  /*
5082  * Block SIGALRM and SIGCHLD before we start the timer and the pager (if
5083  * configured), to avoid races. sigwait() will receive them.
5084  */
5085  sigprocmask(SIG_BLOCK, &sigalrm_sigchld, NULL);
5086 
5087  /*
5088  * Set a timer to interrupt sigwait() so we can run the query at the
5089  * requested intervals.
5090  */
5091  interval.it_value.tv_sec = sleep_ms / 1000;
5092  interval.it_value.tv_usec = (sleep_ms % 1000) * 1000;
5093  interval.it_interval = interval.it_value;
5094  if (setitimer(ITIMER_REAL, &interval, NULL) < 0)
5095  {
5096  pg_log_error("could not set timer: %m");
5097  done = true;
5098  }
5099 #endif
5100 
5101  /*
5102  * For \watch, we ignore the size of the result and always use the pager
5103  * if PSQL_WATCH_PAGER is set. We also ignore the regular PSQL_PAGER or
5104  * PAGER environment variables, because traditional pagers probably won't
5105  * be very useful for showing a stream of results.
5106  */
5107 #ifndef WIN32
5108  pagerprog = getenv("PSQL_WATCH_PAGER");
5109 #endif
5110  if (pagerprog && myopt.topt.pager)
5111  {
5112  fflush(NULL);
5114  pagerpipe = popen(pagerprog, "w");
5115 
5116  if (!pagerpipe)
5117  /* silently proceed without pager */
5119  }
5120 
5121  /*
5122  * Choose format for timestamps. We might eventually make this a \pset
5123  * option. In the meantime, using a variable for the format suppresses
5124  * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
5125  */
5126  strftime_fmt = "%c";
5127 
5128  /*
5129  * Set up rendering options, in particular, disable the pager unless
5130  * PSQL_WATCH_PAGER was successfully launched.
5131  */
5132  if (!pagerpipe)
5133  myopt.topt.pager = 0;
5134 
5135 
5136  /*
5137  * If there's a title in the user configuration, make sure we have room
5138  * for it in the title buffer. Allow 128 bytes for the timestamp plus 128
5139  * bytes for the rest.
5140  */
5141  user_title = myopt.title;
5142  title_len = (user_title ? strlen(user_title) : 0) + 256;
5143  title = pg_malloc(title_len);
5144 
5145  for (;;)
5146  {
5147  time_t timer;
5148  char timebuf[128];
5149 
5150  /*
5151  * Prepare title for output. Note that we intentionally include a
5152  * newline at the end of the title; this is somewhat historical but it
5153  * makes for reasonably nicely formatted output in simple cases.
5154  */
5155  timer = time(NULL);
5156  strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
5157 
5158  if (user_title)
5159  snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
5160  user_title, timebuf, sleep);
5161  else
5162  snprintf(title, title_len, _("%s (every %gs)\n"),
5163  timebuf, sleep);
5164  myopt.title = title;
5165 
5166  /* Run the query and print out the result */
5167  res = PSQLexecWatch(query_buf->data, &myopt, pagerpipe);
5168 
5169  /*
5170  * PSQLexecWatch handles the case where we can no longer repeat the
5171  * query, and returns 0 or -1.
5172  */
5173  if (res <= 0)
5174  break;
5175 
5176  if (pagerpipe && ferror(pagerpipe))
5177  break;
5178 
5179 #ifdef WIN32
5180 
5181  /*
5182  * Set up cancellation of 'watch' via SIGINT. We redo this each time
5183  * through the loop since it's conceivable something inside
5184  * PSQLexecWatch could change sigint_interrupt_jmp.
5185  */
5186  if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
5187  break;
5188 
5189  /*
5190  * Enable 'watch' cancellations and wait a while before running the
5191  * query again. Break the sleep into short intervals (at most 1s).
5192  */
5193  sigint_interrupt_enabled = true;
5194  for (long i = sleep_ms; i > 0;)
5195  {
5196  long s = Min(i, 1000L);
5197 
5198  pg_usleep(s * 1000L);
5199  if (cancel_pressed)
5200  break;
5201  i -= s;
5202  }
5203  sigint_interrupt_enabled = false;
5204 #else
5205  /* sigwait() will handle SIGINT. */
5206  sigprocmask(SIG_BLOCK, &sigint, NULL);
5207  if (cancel_pressed)
5208  done = true;
5209 
5210  /* Wait for SIGINT, SIGCHLD or SIGALRM. */
5211  while (!done)
5212  {
5213  int signal_received;
5214 
5215  errno = sigwait(&sigalrm_sigchld_sigint, &signal_received);
5216  if (errno != 0)
5217  {
5218  /* Some other signal arrived? */
5219  if (errno == EINTR)
5220  continue;
5221  else
5222  {
5223  pg_log_error("could not wait for signals: %m");
5224  done = true;
5225  break;
5226  }
5227  }
5228  /* On ^C or pager exit, it's time to stop running the query. */
5229  if (signal_received == SIGINT || signal_received == SIGCHLD)
5230  done = true;
5231  /* Otherwise, we must have SIGALRM. Time to run the query again. */
5232  break;
5233  }
5234 
5235  /* Unblock SIGINT so that slow queries can be interrupted. */
5236  sigprocmask(SIG_UNBLOCK, &sigint, NULL);
5237  if (done)
5238  break;
5239 #endif
5240  }
5241 
5242  if (pagerpipe)
5243  {
5244  pclose(pagerpipe);
5246  }
5247  else
5248  {
5249  /*
5250  * If the terminal driver echoed "^C", libedit/libreadline might be
5251  * confused about the cursor position. Therefore, inject a newline
5252  * before the next prompt is displayed. We only do this when not
5253  * using a pager, because pagers are expected to restore the screen to
5254  * a sane state on exit.
5255  */
5256  fprintf(stdout, "\n");
5257  fflush(stdout);
5258  }
5259 
5260 #ifndef WIN32
5261  /* Disable the interval timer. */
5262  memset(&interval, 0, sizeof(interval));
5263  setitimer(ITIMER_REAL, &interval, NULL);
5264  /* Unblock SIGINT, SIGCHLD and SIGALRM. */
5265  sigprocmask(SIG_UNBLOCK, &sigalrm_sigchld_sigint, NULL);
5266 #endif
5267 
5268  pg_free(title);
5269  return (res >= 0);
5270 }
volatile sig_atomic_t sigint_interrupt_enabled
Definition: common.c:253
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:255
int PSQLexecWatch(const char *query, const printQueryOpt *opt, FILE *printQueryFout)
Definition: common.c:615
#define Min(x, y)
Definition: c.h:937
void restore_sigpipe_trap(void)
Definition: print.c:3061
void disable_sigpipe_trap(void)
Definition: print.c:3038
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
#define fprintf
Definition: port.h:242
void pg_usleep(long microsec)
Definition: signal.c:53
int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
Definition: timer.c:86
#define SIGCHLD
Definition: win32_port.h:186
#define EINTR
Definition: win32_port.h:369
#define SIGALRM
Definition: win32_port.h:182
#define ITIMER_REAL
Definition: win32_port.h:198

References _, cancel_pressed, PQExpBufferData::data, disable_sigpipe_trap(), EINTR, fflush(), fprintf, i, ITIMER_REAL, PQExpBufferData::len, Min, printTableOpt::pager, pg_free(), pg_log_error, pg_malloc(), pg_usleep(), _psqlSettings::popt, pset, PSQLexecWatch(), res, restore_sigpipe_trap(), setitimer(), SIGALRM, SIGCHLD, sigint_interrupt_enabled, sigint_interrupt_jmp, 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 5277 of file command.c.

5278 {
5280  {
5281  printf(_("********* QUERY **********\n"
5282  "%s\n"
5283  "**************************\n\n"), query);
5284  fflush(stdout);
5285  if (pset.logfile)
5286  {
5288  _("********* QUERY **********\n"
5289  "%s\n"
5290  "**************************\n\n"), query);
5291  fflush(pset.logfile);
5292  }
5293 
5295  return false;
5296  }
5297  return true;
5298 }
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
FILE * logfile
Definition: settings.h:119
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:146

References _, _psqlSettings::echo_hidden, fflush(), 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 3824 of file command.c.

3825 {
3826  const char *editorName;
3827  const char *editor_lineno_arg = NULL;
3828  char *sys;
3829  int result;
3830 
3831  Assert(fname != NULL);
3832 
3833  /* Find an editor to use */
3834  editorName = getenv("PSQL_EDITOR");
3835  if (!editorName)
3836  editorName = getenv("EDITOR");
3837  if (!editorName)
3838  editorName = getenv("VISUAL");
3839  if (!editorName)
3840  editorName = DEFAULT_EDITOR;
3841 
3842  /* Get line number argument, if we need it. */
3843  if (lineno > 0)
3844  {
3845  editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
3846 #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
3847  if (!editor_lineno_arg)
3848  editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
3849 #endif
3850  if (!editor_lineno_arg)
3851  {
3852  pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
3853  return false;
3854  }
3855  }
3856 
3857  /*
3858  * On Unix the EDITOR value should *not* be quoted, since it might include
3859  * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
3860  * if necessary. But this policy is not very workable on Windows, due to
3861  * severe brain damage in their command shell plus the fact that standard
3862  * program paths include spaces.
3863  */
3864 #ifndef WIN32
3865  if (lineno > 0)
3866  sys = psprintf("exec %s %s%d '%s'",
3867  editorName, editor_lineno_arg, lineno, fname);
3868  else
3869  sys = psprintf("exec %s '%s'",
3870  editorName, fname);
3871 #else
3872  if (lineno > 0)
3873  sys = psprintf("\"%s\" %s%d \"%s\"",
3874  editorName, editor_lineno_arg, lineno, fname);
3875  else
3876  sys = psprintf("\"%s\" \"%s\"",
3877  editorName, fname);
3878 #endif
3879  fflush(NULL);
3880  result = system(sys);
3881  if (result == -1)
3882  pg_log_error("could not start editor \"%s\"", editorName);
3883  else if (result == 127)
3884  pg_log_error("could not start /bin/sh");
3885  free(sys);
3886 
3887  return result == 0;
3888 }
#define DEFAULT_EDITOR_LINENUMBER_ARG
Definition: settings.h:23
#define DEFAULT_EDITOR
Definition: settings.h:22

References Assert(), DEFAULT_EDITOR, DEFAULT_EDITOR_LINENUMBER_ARG, fflush(), 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 286 of file command.c.

291 {
293  bool active_branch = conditional_active(cstack);
294 
295  /*
296  * In interactive mode, warn when we're ignoring a command within a false
297  * \if-branch. But we continue on, so as to parse and discard the right
298  * amount of parameter text. Each individual backslash command subroutine
299  * is responsible for doing nothing after discarding appropriate
300  * arguments, if !active_branch.
301  */
302  if (pset.cur_cmd_interactive && !active_branch &&
303  !is_branching_command(cmd))
304  {
305  pg_log_warning("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block",
306  cmd);
307  }
308 
309  if (strcmp(cmd, "a") == 0)
310  status = exec_command_a(scan_state, active_branch);
311  else if (strcmp(cmd, "bind") == 0)
312  status = exec_command_bind(scan_state, active_branch);
313  else if (strcmp(cmd, "C") == 0)
314  status = exec_command_C(scan_state, active_branch);
315  else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
316  status = exec_command_connect(scan_state, active_branch);
317  else if (strcmp(cmd, "cd") == 0)
318  status = exec_command_cd(scan_state, active_branch, cmd);
319  else if (strcmp(cmd, "conninfo") == 0)
320  status = exec_command_conninfo(scan_state, active_branch);
321  else if (pg_strcasecmp(cmd, "copy") == 0)
322  status = exec_command_copy(scan_state, active_branch);
323  else if (strcmp(cmd, "copyright") == 0)
324  status = exec_command_copyright(scan_state, active_branch);
325  else if (strcmp(cmd, "crosstabview") == 0)
326  status = exec_command_crosstabview(scan_state, active_branch);
327  else if (cmd[0] == 'd')
328  status = exec_command_d(scan_state, active_branch, cmd);
329  else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
330  status = exec_command_edit(scan_state, active_branch,
331  query_buf, previous_buf);
332  else if (strcmp(cmd, "ef") == 0)
333  status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
334  else if (strcmp(cmd, "ev") == 0)
335  status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
336  else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0 ||
337  strcmp(cmd, "warn") == 0)
338  status = exec_command_echo(scan_state, active_branch, cmd);
339  else if (strcmp(cmd, "elif") == 0)
340  status = exec_command_elif(scan_state, cstack, query_buf);
341  else if (strcmp(cmd, "else") == 0)
342  status = exec_command_else(scan_state, cstack, query_buf);
343  else if (strcmp(cmd, "endif") == 0)
344  status = exec_command_endif(scan_state, cstack, query_buf);
345  else if (strcmp(cmd, "encoding") == 0)
346  status = exec_command_encoding(scan_state, active_branch);
347  else if (strcmp(cmd, "errverbose") == 0)
348  status = exec_command_errverbose(scan_state, active_branch);
349  else if (strcmp(cmd, "f") == 0)
350  status = exec_command_f(scan_state, active_branch);
351  else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
352  status = exec_command_g(scan_state, active_branch, cmd);
353  else if (strcmp(cmd, "gdesc") == 0)
354  status = exec_command_gdesc(scan_state, active_branch);
355  else if (strcmp(cmd, "getenv") == 0)
356  status = exec_command_getenv(scan_state, active_branch, cmd);
357  else if (strcmp(cmd, "gexec") == 0)
358  status = exec_command_gexec(scan_state, active_branch);
359  else if (strcmp(cmd, "gset") == 0)
360  status = exec_command_gset(scan_state, active_branch);
361  else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
362  status = exec_command_help(scan_state, active_branch);
363  else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
364  status = exec_command_html(scan_state, active_branch);
365  else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
366  strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
367  status = exec_command_include(scan_state, active_branch, cmd);
368  else if (strcmp(cmd, "if") == 0)
369  status = exec_command_if(scan_state, cstack, query_buf);
370  else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
371  strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
372  status = exec_command_list(scan_state, active_branch, cmd);
373  else if (strncmp(cmd, "lo_", 3) == 0)
374  status = exec_command_lo(scan_state, active_branch, cmd);
375  else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
376  status = exec_command_out(scan_state, active_branch);
377  else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
378  status = exec_command_print(scan_state, active_branch,
379  query_buf, previous_buf);
380  else if (strcmp(cmd, "password") == 0)
381  status = exec_command_password(scan_state, active_branch);
382  else if (strcmp(cmd, "prompt") == 0)
383  status = exec_command_prompt(scan_state, active_branch, cmd);
384  else if (strcmp(cmd, "pset") == 0)
385  status = exec_command_pset(scan_state, active_branch);
386  else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
387  status = exec_command_quit(scan_state, active_branch);
388  else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
389  status = exec_command_reset(scan_state, active_branch, query_buf);
390  else if (strcmp(cmd, "s") == 0)
391  status = exec_command_s(scan_state, active_branch);
392  else if (strcmp(cmd, "set") == 0)
393  status = exec_command_set(scan_state, active_branch);
394  else if (strcmp(cmd, "setenv") == 0)
395  status = exec_command_setenv(scan_state, active_branch, cmd);
396  else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
397  status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
398  else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
399  status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
400  else if (strcmp(cmd, "t") == 0)
401  status = exec_command_t(scan_state, active_branch);
402  else if (strcmp(cmd, "T") == 0)
403  status = exec_command_T(scan_state, active_branch);
404  else if (strcmp(cmd, "timing") == 0)
405  status = exec_command_timing(scan_state, active_branch);
406  else if (strcmp(cmd, "unset") == 0)
407  status = exec_command_unset(scan_state, active_branch, cmd);
408  else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
409  status = exec_command_write(scan_state, active_branch, cmd,
410  query_buf, previous_buf);
411  else if (strcmp(cmd, "watch") == 0)
412  status = exec_command_watch(scan_state, active_branch,
413  query_buf, previous_buf);
414  else if (strcmp(cmd, "x") == 0)
415  status = exec_command_x(scan_state, active_branch);
416  else if (strcmp(cmd, "z") == 0)
417  status = exec_command_z(scan_state, active_branch);
418  else if (strcmp(cmd, "!") == 0)
419  status = exec_command_shell_escape(scan_state, active_branch);
420  else if (strcmp(cmd, "?") == 0)
421  status = exec_command_slash_command_help(scan_state, active_branch);
422  else
424 
425  /*
426  * All the commands that return PSQL_CMD_SEND want to execute previous_buf
427  * if query_buf is empty. For convenience we implement that here, not in
428  * the individual command subroutines.
429  */
430  if (status == PSQL_CMD_SEND)
431  (void) copy_previous_query(query_buf, previous_buf);
432 
433  return status;
434 }
static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1898
static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch)
Definition: command.c:712
static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1334
static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2067
static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1651
static bool copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:3110
static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1392
static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2580
static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1520
static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1620
static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch)
Definition: command.c:443
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2825
static backslashResult exec_command_bind(PsqlScanState scan_state, bool active_branch)
Definition: command.c:462
static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2093
static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2803
static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1711
static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1757
static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1364
static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:2329
static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2044
static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1251
static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:748
static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2847
static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:582
static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1591
static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, bool is_func)
Definition: command.c:1142
static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2634
static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2374
static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2347
static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2183
static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2764
static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1670
static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1938
static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2602
static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch)
Definition: command.c:496
static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1296
static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:1054
static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1833
static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
Definition: command.c:646
static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch)
Definition: command.c:690
static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2869
static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
Definition: command.c:724
static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2260
static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2558
static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2664
static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch, const char *cmd, bool is_func)
Definition: command.c:2475
static bool is_branching_command(const char *cmd)
Definition: command.c:3048
static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch)
Definition: command.c:529
static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1574
static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1966
static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1537
static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2315
static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2427
@ 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
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#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_C(), exec_command_cd(), 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_errverbose(), exec_command_f(), exec_command_g(), exec_command_gdesc(), exec_command_getenv(), 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_password(), exec_command_print(), exec_command_prompt(), exec_command_pset(), exec_command_quit(), exec_command_reset(), exec_command_s(), exec_command_set(), exec_command_setenv(), exec_command_sf_sv(), exec_command_shell_escape(), exec_command_slash_command_help(), exec_command_t(), exec_command_T(), exec_command_timing(), 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, PSQL_CMD_UNKNOWN, and status().

Referenced by HandleSlashCmds().

◆ exec_command_a()

static backslashResult exec_command_a ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 443 of file command.c.

444 {
445  bool success = true;
446 
447  if (active_branch)
448  {
450  success = do_pset("format", "aligned", &pset.popt, pset.quiet);
451  else
452  success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
453  }
454 
456 }
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:4254
@ 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 462 of file command.c.

463 {
465 
466  if (active_branch)
467  {
468  char *opt;
469  int nparams = 0;
470  int nalloc = 0;
471 
472  pset.bind_params = NULL;
473 
474  while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
475  {
476  nparams++;
477  if (nparams > nalloc)
478  {
479  nalloc = nalloc ? nalloc * 2 : 1;
480  pset.bind_params = pg_realloc_array(pset.bind_params, char *, nalloc);
481  }
482  pset.bind_params[nparams - 1] = pg_strdup(opt);
483  }
484 
485  pset.bind_nparams = nparams;
486  pset.bind_flag = true;
487  }
488 
489  return status;
490 }
#define pg_realloc_array(pointer, type, count)
Definition: fe_memutils.h:51
@ OT_NORMAL
Definition: psqlscanslash.h:17
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
bool bind_flag
Definition: settings.h:99
char ** bind_params
Definition: settings.h:101
int bind_nparams
Definition: settings.h:100

References _psqlSettings::bind_flag, _psqlSettings::bind_nparams, _psqlSettings::bind_params, OT_NORMAL, pg_realloc_array, pg_strdup(), pset, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and status().

Referenced by exec_command().

◆ exec_command_C()

static backslashResult exec_command_C ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 496 of file command.c.

497 {
498  bool success = true;
499 
500  if (active_branch)
501  {
502  char *opt = psql_scan_slash_option(scan_state,
503  OT_NORMAL, NULL, true);
504 
505  success = do_pset("title", opt, &pset.popt, pset.quiet);
506  free(opt);
507  }
508  else
509  ignore_slash_options(scan_state);
510 
512 }
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:3003

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 582 of file command.c.

583 {
584  bool success = true;
585 
586  if (active_branch)
587  {
588  char *opt = psql_scan_slash_option(scan_state,
589  OT_NORMAL, NULL, true);
590  char *dir;
591 
592  if (opt)
593  dir = opt;
594  else
595  {
596 #ifndef WIN32
597  /* This should match get_home_path() */
598  dir = getenv("HOME");
599  if (dir == NULL || dir[0] == '\0')
600  {
601  uid_t user_id = geteuid();
602  struct passwd *pw;
603 
604  errno = 0; /* clear errno before call */
605  pw = getpwuid(user_id);
606  if (pw)
607  dir = pw->pw_dir;
608  else
609  {
610  pg_log_error("could not get home directory for user ID %ld: %s",
611  (long) user_id,
612  errno ? strerror(errno) : _("user does not exist"));
613  success = false;
614  }
615  }
616 #else /* WIN32 */
617 
618  /*
619  * On Windows, 'cd' without arguments prints the current
620  * directory, so if someone wants to code this here instead...
621  */
622  dir = "/";
623 #endif /* WIN32 */
624  }
625 
626  if (success &&
627  chdir(dir) < 0)
628  {
629  pg_log_error("\\%s: could not change directory to \"%s\": %m",
630  cmd, dir);
631  success = false;
632  }
633 
634  free(opt);
635  }
636  else
637  ignore_slash_options(scan_state);
638 
640 }
int uid_t
Definition: win32_port.h:246

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_connect()

static backslashResult exec_command_connect ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 529 of file command.c.

530 {
531  bool success = true;
532 
533  if (active_branch)
534  {
535  static const char prefix[] = "-reuse-previous=";
536  char *opt1,
537  *opt2,
538  *opt3,
539  *opt4;
540  enum trivalue reuse_previous = TRI_DEFAULT;
541 
542  opt1 = read_connect_arg(scan_state);
543  if (opt1 != NULL && strncmp(opt1, prefix, sizeof(prefix) - 1) == 0)
544  {
545  bool on_off;
546 
547  success = ParseVariableBool(opt1 + sizeof(prefix) - 1,
548  "-reuse-previous",
549  &on_off);
550  if (success)
551  {
552  reuse_previous = on_off ? TRI_YES : TRI_NO;
553  free(opt1);
554  opt1 = read_connect_arg(scan_state);
555  }
556  }
557 
558  if (success) /* give up if reuse_previous was invalid */
559  {
560  opt2 = read_connect_arg(scan_state);
561  opt3 = read_connect_arg(scan_state);
562  opt4 = read_connect_arg(scan_state);
563 
564  success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
565 
566  free(opt2);
567  free(opt3);
568  free(opt4);
569  }
570  free(opt1);
571  }
572  else
573  ignore_slash_options(scan_state);
574 
576 }
static bool do_connect(enum trivalue reuse_previous_specification, char *dbname, char *user, char *host, char *port)
Definition: command.c:3179
static char * read_connect_arg(PsqlScanState scan_state)
Definition: command.c:2900
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 646 of file command.c.

647 {
648  if (active_branch)
649  {
650  char *db = PQdb(pset.db);
651 
652  if (db == NULL)
653  printf(_("You are currently not connected to a database.\n"));
654  else
655  {
656  char *host = PQhost(pset.db);
657  char *hostaddr = PQhostaddr(pset.db);
658 
659  if (is_unixsock_path(host))
660  {
661  /* hostaddr overrides host */
662  if (hostaddr && *hostaddr)
663  printf(_("You are connected to database \"%s\" as user \"%s\" on address \"%s\" at port \"%s\".\n"),
664  db, PQuser(pset.db), hostaddr, PQport(pset.db));
665  else
666  printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
667  db, PQuser(pset.db), host, PQport(pset.db));
668  }
669  else
670  {
671  if (hostaddr && *hostaddr && strcmp(host, hostaddr) != 0)
672  printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" (address \"%s\") at port \"%s\".\n"),
673  db, PQuser(pset.db), host, hostaddr, PQport(pset.db));
674  else
675  printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
676  db, PQuser(pset.db), host, PQport(pset.db));
677  }
678  printSSLInfo();
679  printGSSInfo();
680  }
681  }
682 
683  return PSQL_CMD_SKIP_LINE;
684 }

References _, _psqlSettings::db, is_unixsock_path(), PQdb(), PQhost(), PQhostaddr(), PQport(), PQuser(), printf, printGSSInfo(), printSSLInfo(), pset, and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

◆ exec_command_copy()

static backslashResult exec_command_copy ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 690 of file command.c.

691 {
692  bool success = true;
693 
694  if (active_branch)
695  {
696  char *opt = psql_scan_slash_option(scan_state,
697  OT_WHOLE_LINE, NULL, false);
698 
699  success = do_copy(opt);
700  free(opt);
701  }
702  else
703  ignore_slash_whole_line(scan_state);
704 
706 }
bool do_copy(const char *args)
Definition: copy.c:268
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:3036
@ 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 712 of file command.c.

713 {
714  if (active_branch)
715  print_copyright();
716 
717  return PSQL_CMD_SKIP_LINE;
718 }
void print_copyright(void)
Definition: help.c:747

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 724 of file command.c.

725 {
727 
728  if (active_branch)
729  {
730  int i;
731 
732  for (i = 0; i < lengthof(pset.ctv_args); i++)
733  pset.ctv_args[i] = psql_scan_slash_option(scan_state,
734  OT_NORMAL, NULL, true);
735  pset.crosstab_flag = true;
737  }
738  else
739  ignore_slash_options(scan_state);
740 
741  return status;
742 }
char * ctv_args[4]
Definition: settings.h:103
bool crosstab_flag
Definition: settings.h:102

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

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 748 of file command.c.

749 {
751  bool success = true;
752 
753  if (active_branch)
754  {
755  char *pattern;
756  bool show_verbose,
757  show_system;
758 
759  /* We don't do SQLID reduction on the pattern yet */
760  pattern = psql_scan_slash_option(scan_state,
761  OT_NORMAL, NULL, true);
762 
763  show_verbose = strchr(cmd, '+') ? true : false;
764  show_system = strchr(cmd, 'S') ? true : false;
765 
766  switch (cmd[1])
767  {
768  case '\0':
769  case '+':
770  case 'S':
771  if (pattern)
772  success = describeTableDetails(pattern, show_verbose, show_system);
773  else
774  /* standard listing of interesting things */
775  success = listTables("tvmsE", NULL, show_verbose, show_system);
776  break;
777  case 'A':
778  {
779  char *pattern2 = NULL;
780 
781  if (pattern && cmd[2] != '\0' && cmd[2] != '+')
782  pattern2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
783 
784  switch (cmd[2])
785  {
786  case '\0':
787  case '+':
788  success = describeAccessMethods(pattern, show_verbose);
789  break;
790  case 'c':
791  success = listOperatorClasses(pattern, pattern2, show_verbose);
792  break;
793  case 'f':
794  success = listOperatorFamilies(pattern, pattern2, show_verbose);
795  break;
796  case 'o':
797  success = listOpFamilyOperators(pattern, pattern2, show_verbose);
798  break;
799  case 'p':
800  success = listOpFamilyFunctions(pattern, pattern2, show_verbose);
801  break;
802  default:
804  break;
805  }
806 
807  free(pattern2);
808  }
809  break;
810  case 'a':
811  success = describeAggregates(pattern, show_verbose, show_system);
812  break;
813  case 'b':
814  success = describeTablespaces(pattern, show_verbose);
815  break;
816  case 'c':
817  if (strncmp(cmd, "dconfig", 7) == 0)
819  show_verbose,
820  show_system);
821  else
822  success = listConversions(pattern,
823  show_verbose,
824  show_system);
825  break;
826  case 'C':
827  success = listCasts(pattern, show_verbose);
828  break;
829  case 'd':
830  if (strncmp(cmd, "ddp", 3) == 0)
831  success = listDefaultACLs(pattern);
832  else
833  success = objectDescription(pattern, show_system);
834  break;
835  case 'D':
836  success = listDomains(pattern, show_verbose, show_system);
837  break;
838  case 'f': /* function subsystem */
839  switch (cmd[2])
840  {
841  case '\0':
842  case '+':
843  case 'S':
844  case 'a':
845  case 'n':
846  case 'p':
847  case 't':
848  case 'w':
849  success = exec_command_dfo(scan_state, cmd, pattern,
850  show_verbose, show_system);
851  break;
852  default:
854  break;
855  }
856  break;
857  case 'g':
858  /* no longer distinct from \du */
859  success = describeRoles(pattern, show_verbose, show_system);
860  break;
861  case 'l':
862  success = listLargeObjects(show_verbose);
863  break;
864  case 'L':
865  success = listLanguages(pattern, show_verbose, show_system);
866  break;
867  case 'n':
868  success = listSchemas(pattern, show_verbose, show_system);
869  break;
870  case 'o':
871  success = exec_command_dfo(scan_state, cmd, pattern,
872  show_verbose, show_system);
873  break;
874  case 'O':
875  success = listCollations(pattern, show_verbose, show_system);
876  break;
877  case 'p':
878  success = permissionsList(pattern);
879  break;
880  case 'P':
881  {
882  switch (cmd[2])
883  {
884  case '\0':
885  case '+':
886  case 't':
887  case 'i':
888  case 'n':
889  success = listPartitionedTables(&cmd[2], pattern, show_verbose);
890  break;
891  default:
893  break;
894  }
895  }
896  break;
897  case 'T':
898  success = describeTypes(pattern, show_verbose, show_system);
899  break;
900  case 't':
901  case 'v':
902  case 'm':
903  case 'i':
904  case 's':
905  case 'E':
906  success = listTables(&cmd[1], pattern, show_verbose, show_system);
907  break;
908  case 'r':
909  if (cmd[2] == 'd' && cmd[3] == 's')
910  {
911  char *pattern2 = NULL;
912 
913  if (pattern)
914  pattern2 = psql_scan_slash_option(scan_state,
915  OT_NORMAL, NULL, true);
916  success = listDbRoleSettings(pattern, pattern2);
917 
918  free(pattern2);
919  }
920  else
922  break;
923  case 'R':
924  switch (cmd[2])
925  {
926  case 'p':
927  if (show_verbose)
928  success = describePublications(pattern);
929  else
930  success = listPublications(pattern);
931  break;
932  case 's':
933  success = describeSubscriptions(pattern, show_verbose);
934  break;
935  default:
937  }
938  break;
939  case 'u':
940  success = describeRoles(pattern, show_verbose, show_system);
941  break;
942  case 'F': /* text search subsystem */
943  switch (cmd[2])
944  {
945  case '\0':
946  case '+':
947  success = listTSConfigs(pattern, show_verbose);
948  break;
949  case 'p':
950  success = listTSParsers(pattern, show_verbose);
951  break;
952  case 'd':
953  success = listTSDictionaries(pattern, show_verbose);
954  break;
955  case 't':
956  success = listTSTemplates(pattern, show_verbose);
957  break;
958  default:
960  break;
961  }
962  break;
963  case 'e': /* SQL/MED subsystem */
964  switch (cmd[2])
965  {
966  case 's':
967  success = listForeignServers(pattern, show_verbose);
968  break;
969  case 'u':
970  success = listUserMappings(pattern, show_verbose);
971  break;
972  case 'w':
973  success = listForeignDataWrappers(pattern, show_verbose);
974  break;
975  case 't':
976  success = listForeignTables(pattern, show_verbose);
977  break;
978  default:
980  break;
981  }
982  break;
983  case 'x': /* Extensions */
984  if (show_verbose)
985  success = listExtensionContents(pattern);
986  else
987  success = listExtensions(pattern);
988  break;
989  case 'X': /* Extended Statistics */
990  success = listExtendedStats(pattern);
991  break;
992  case 'y': /* Event Triggers */
993  success = listEventTriggers(pattern, show_verbose);
994  break;
995  default:
997  }
998 
999  free(pattern);
1000  }
1001  else
1002  ignore_slash_options(scan_state);
1003 
1004  if (!success)
1006 
1007  return status;
1008 }
static bool exec_command_dfo(PsqlScanState scan_state, const char *cmd, const char *pattern, bool show_verbose, bool show_system)
Definition: command.c:1012
bool listUserMappings(const char *pattern, bool verbose)
Definition: describe.c:5813
bool listTSConfigs(const char *pattern, bool verbose)
Definition: describe.c:5458
bool describeRoles(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3607
bool listOpFamilyFunctions(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:6882
bool listPublications(const char *pattern)
Definition: describe.c:6159
bool listTSParsers(const char *pattern, bool verbose)
Definition: describe.c:5076
bool listExtensionContents(const char *pattern)
Definition: describe.c:5994
bool describeAggregates(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:71
bool listForeignDataWrappers(const char *pattern, bool verbose)
Definition: describe.c:5664
bool listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
Definition: describe.c:4042
bool describeSubscriptions(const char *pattern, bool verbose)
Definition: describe.c:6468
bool listExtendedStats(const char *pattern)
Definition: describe.c:4635
bool describeTypes(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:624
bool listOperatorFamilies(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6693
bool listForeignServers(const char *pattern, bool verbose)
Definition: describe.c:5736
bool describeTableDetails(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:1439
bool listDomains(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4320
bool listTSDictionaries(const char *pattern, bool verbose)
Definition: describe.c:5326
bool listDbRoleSettings(const char *pattern, const char *pattern2)
Definition: describe.c:3762
bool listCollations(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4851
bool listSchemas(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4954
bool listExtensions(const char *pattern)
Definition: describe.c:5942
bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3843
bool listTSTemplates(const char *pattern, bool verbose)
Definition: describe.c:5392
bool describeTablespaces(const char *pattern, bool verbose)
Definition: describe.c:217
bool listCasts(const char *pattern, bool verbose)
Definition: describe.c:4732
bool listOpFamilyOperators(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:6783
bool permissionsList(const char *pattern)
Definition: describe.c:1005
bool listOperatorClasses(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6591
bool listForeignTables(const char *pattern, bool verbose)
Definition: describe.c:5869
bool describeConfigurationParameters(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4485
bool listEventTriggers(const char *pattern, bool verbose)
Definition: describe.c:4554
bool listDefaultACLs(const char *pattern)
Definition: describe.c:1167
bool listConversions(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4404
bool describeAccessMethods(const char *pattern, bool verbose)
Definition: describe.c:142
bool listLargeObjects(bool verbose)
Definition: describe.c:6972
bool listLanguages(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4243
bool describePublications(const char *pattern)
Definition: describe.c:6282
bool objectDescription(const char *pattern, bool showSystem)
Definition: describe.c:1245
return true
Definition: isn.c:126

References describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describePublications(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), exec_command_dfo(), 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(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, PSQL_CMD_UNKNOWN, psql_scan_slash_option(), status(), success, 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 1012 of file command.c.

1015 {
1016  bool success;
1017  char *arg_patterns[FUNC_MAX_ARGS];
1018  int num_arg_patterns = 0;
1019 
1020  /* Collect argument-type patterns too */
1021  if (pattern) /* otherwise it was just \df or \do */
1022  {
1023  char *ap;
1024 
1025  while ((ap = psql_scan_slash_option(scan_state,
1026  OT_NORMAL, NULL, true)) != NULL)
1027  {
1028  arg_patterns[num_arg_patterns++] = ap;
1029  if (num_arg_patterns >= FUNC_MAX_ARGS)
1030  break; /* protect limited-size array */
1031  }
1032  }
1033 
1034  if (cmd[1] == 'f')
1035  success = describeFunctions(&cmd[2], pattern,
1036  arg_patterns, num_arg_patterns,
1037  show_verbose, show_system);
1038  else
1039  success = describeOperators(pattern,
1040  arg_patterns, num_arg_patterns,
1041  show_verbose, show_system);
1042 
1043  while (--num_arg_patterns >= 0)
1044  free(arg_patterns[num_arg_patterns]);
1045 
1046  return success;
1047 }
bool describeFunctions(const char *functypes, const char *func_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
Definition: describe.c:291
bool describeOperators(const char *oper_pattern, char **arg_patterns, int num_arg_patterns, bool verbose, bool showSystem)
Definition: describe.c:780
#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 1251 of file command.c.

1252 {
1253  if (active_branch)
1254  {
1255  char *value;
1256  char quoted;
1257  bool no_newline = false;
1258  bool first = true;
1259  FILE *fout;
1260 
1261  if (strcmp(cmd, "qecho") == 0)
1262  fout = pset.queryFout;
1263  else if (strcmp(cmd, "warn") == 0)
1264  fout = stderr;
1265  else
1266  fout = stdout;
1267 
1268  while ((value = psql_scan_slash_option(scan_state,
1269  OT_NORMAL, &quoted, false)))
1270  {
1271  if (first && !no_newline && !quoted && strcmp(value, "-n") == 0)
1272  no_newline = true;
1273  else
1274  {
1275  if (first)
1276  first = false;
1277  else
1278  fputc(' ', fout);
1279  fputs(value, fout);
1280  }
1281  free(value);
1282  }
1283  if (!no_newline)
1284  fputs("\n", fout);
1285  }
1286  else
1287  ignore_slash_options(scan_state);
1288 
1289  return PSQL_CMD_SKIP_LINE;
1290 }
FILE * queryFout
Definition: settings.h:84

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 1054 of file command.c.

1056 {
1058 
1059  if (active_branch)
1060  {
1061  if (!query_buf)
1062  {
1063  pg_log_error("no query buffer");
1065  }
1066  else
1067  {
1068  char *fname;
1069  char *ln = NULL;
1070  int lineno = -1;
1071 
1072  fname = psql_scan_slash_option(scan_state,
1073  OT_NORMAL, NULL, true);
1074  if (fname)
1075  {
1076  /* try to get separate lineno arg */
1077  ln = psql_scan_slash_option(scan_state,
1078  OT_NORMAL, NULL, true);
1079  if (ln == NULL)
1080  {
1081  /* only one arg; maybe it is lineno not fname */
1082  if (fname[0] &&
1083  strspn(fname, "0123456789") == strlen(fname))
1084  {
1085  /* all digits, so assume it is lineno */
1086  ln = fname;
1087  fname = NULL;
1088  }
1089  }
1090  }
1091  if (ln)
1092  {
1093  lineno = atoi(ln);
1094  if (lineno < 1)
1095  {
1096  pg_log_error("invalid line number: %s", ln);
1098  }
1099  }
1100  if (status != PSQL_CMD_ERROR)
1101  {
1102  bool discard_on_quit;
1103 
1104  expand_tilde(&fname);
1105  if (fname)
1106  {
1107  canonicalize_path(fname);
1108  /* Always clear buffer if the file isn't modified */
1109  discard_on_quit = true;
1110  }
1111  else
1112  {
1113  /*
1114  * If query_buf is empty, recall previous query for
1115  * editing. But in that case, the query buffer should be
1116  * emptied if editing doesn't modify the file.
1117  */
1118  discard_on_quit = copy_previous_query(query_buf,
1119  previous_buf);
1120  }
1121 
1122  if (do_edit(fname, query_buf, lineno, discard_on_quit, NULL))
1124  else
1126  }
1127  free(fname);
1128  free(ln);
1129  }
1130  }
1131  else
1132  ignore_slash_options(scan_state);
1133 
1134  return status;
1135 }
void expand_tilde(char **filename)
Definition: common.c:2313
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool discard_on_quit, bool *edited)
Definition: command.c:3906
@ PSQL_CMD_NEWEDIT
Definition: command.h:21
void canonicalize_path(char *path)
Definition: path.c:264

References canonicalize_path(), copy_previous_query(), do_edit(), expand_tilde(), free, ignore_slash_options(), OT_NORMAL, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_NEWEDIT, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and status().

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 1142 of file command.c.

1144 {
1146 
1147  if (active_branch)
1148  {
1149  char *obj_desc = psql_scan_slash_option(scan_state,
1150  OT_WHOLE_LINE,
1151  NULL, true);
1152  int lineno = -1;
1153 
1154  if (!query_buf)
1155  {
1156  pg_log_error("no query buffer");
1158  }
1159  else
1160  {
1161  Oid obj_oid = InvalidOid;
1163 
1164  lineno = strip_lineno_from_objdesc(obj_desc);
1165  if (lineno == 0)
1166  {
1167  /* error already reported */
1169  }
1170  else if (!obj_desc)
1171  {
1172  /* set up an empty command to fill in */
1173  resetPQExpBuffer(query_buf);
1174  if (is_func)
1175  appendPQExpBufferStr(query_buf,
1176  "CREATE FUNCTION ( )\n"
1177  " RETURNS \n"
1178  " LANGUAGE \n"
1179  " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
1180  "AS $function$\n"
1181  "\n$function$\n");
1182  else
1183  appendPQExpBufferStr(query_buf,
1184  "CREATE VIEW AS\n"
1185  " SELECT \n"
1186  " -- something...\n");
1187  }
1188  else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
1189  {
1190  /* error already reported */
1192  }
1193  else if (!get_create_object_cmd(eot, obj_oid, query_buf))
1194  {
1195  /* error already reported */
1197  }
1198  else if (is_func && lineno > 0)
1199  {
1200  /*
1201  * lineno "1" should correspond to the first line of the
1202  * function body. We expect that pg_get_functiondef() will
1203  * emit that on a line beginning with "AS ", "BEGIN ", or
1204  * "RETURN ", and that there can be no such line before the
1205  * real start of the function body. Increment lineno by the
1206  * number of lines before that line, so that it becomes
1207  * relative to the first line of the function definition.
1208  */
1209  const char *lines = query_buf->data;
1210 
1211  while (*lines != '\0')
1212  {
1213  if (strncmp(lines, "AS ", 3) == 0 ||
1214  strncmp(lines, "BEGIN ", 6) == 0 ||
1215  strncmp(lines, "RETURN ", 7) == 0)
1216  break;
1217  lineno++;
1218  /* find start of next line */
1219  lines = strchr(lines, '\n');
1220  if (!lines)
1221  break;
1222  lines++;
1223  }
1224  }
1225  }
1226 
1227  if (status != PSQL_CMD_ERROR)
1228  {
1229  bool edited = false;
1230 
1231  if (!do_edit(NULL, query_buf, lineno, true, &edited))
1233  else if (!edited)
1234  puts(_("No changes"));
1235  else
1237  }
1238 
1239  free(obj_desc);
1240  }
1241  else
1242  ignore_slash_whole_line(scan_state);
1243 
1244  return status;
1245 }
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:5369
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:5309
static int strip_lineno_from_objdesc(char *obj)
Definition: command.c:5529
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31

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(), status(), 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 1757 of file command.c.

1759 {
1760  bool success = true;
1761 
1762  switch (conditional_stack_peek(cstack))
1763  {
1764  case IFSTATE_TRUE:
1765 
1766  /*
1767  * Just finished active branch of this \if block. Update saved
1768  * state so we will keep whatever data was put in query_buf by the
1769  * active branch.
1770  */
1771  save_query_text_state(scan_state, cstack, query_buf);
1772 
1773  /*
1774  * Discard \elif expression and ignore the rest until \endif.
1775  * Switch state before reading expression to ensure proper lexer
1776  * behavior.
1777  */
1779  ignore_boolean_expression(scan_state);
1780  break;
1781  case IFSTATE_FALSE:
1782 
1783  /*
1784  * Discard any query text added by the just-skipped branch.
1785  */
1786  discard_query_text(scan_state, cstack, query_buf);
1787 
1788  /*
1789  * Have not yet found a true expression in this \if block, so this
1790  * might be the first. We have to change state before examining
1791  * the expression, or the lexer won't do the right thing.
1792  */
1794  if (!is_true_boolean_expression(scan_state, "\\elif expression"))
1796  break;
1797  case IFSTATE_IGNORED:
1798 
1799  /*
1800  * Discard any query text added by the just-skipped branch.
1801  */
1802  discard_query_text(scan_state, cstack, query_buf);
1803 
1804  /*
1805  * Skip expression and move on. Either the \if block already had
1806  * an active section, or whole block is being skipped.
1807  */
1808  ignore_boolean_expression(scan_state);
1809  break;
1810  case IFSTATE_ELSE_TRUE:
1811  case IFSTATE_ELSE_FALSE:
1812  pg_log_error("\\elif: cannot occur after \\else");
1813  success = false;
1814  break;
1815  case IFSTATE_NONE:
1816  /* no \if to elif from */
1817  pg_log_error("\\elif: no matching \\if");
1818  success = false;
1819  break;
1820  }
1821 
1823 }
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:3084
static void ignore_boolean_expression(PsqlScanState scan_state)
Definition: command.c:2987
static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name)
Definition: command.c:2970
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:3064
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 1833 of file command.c.

1835 {
1836  bool success = true;
1837 
1838  switch (conditional_stack_peek(cstack))
1839  {
1840  case IFSTATE_TRUE:
1841 
1842  /*
1843  * Just finished active branch of this \if block. Update saved
1844  * state so we will keep whatever data was put in query_buf by the
1845  * active branch.
1846  */
1847  save_query_text_state(scan_state, cstack, query_buf);
1848 
1849  /* Now skip the \else branch */
1851  break;
1852  case IFSTATE_FALSE:
1853 
1854  /*
1855  * Discard any query text added by the just-skipped branch.
1856  */
1857  discard_query_text(scan_state, cstack, query_buf);
1858 
1859  /*
1860  * We've not found any true \if or \elif expression, so execute
1861  * the \else branch.
1862  */
1864  break;
1865  case IFSTATE_IGNORED:
1866 
1867  /*
1868  * Discard any query text added by the just-skipped branch.
1869  */
1870  discard_query_text(scan_state, cstack, query_buf);
1871 
1872  /*
1873  * Either we previously processed the active branch of this \if,
1874  * or the whole \if block is being skipped. Either way, skip the
1875  * \else branch.
1876  */
1878  break;
1879  case IFSTATE_ELSE_TRUE:
1880  case IFSTATE_ELSE_FALSE:
1881  pg_log_error("\\else: cannot occur after \\else");
1882  success = false;
1883  break;
1884  case IFSTATE_NONE:
1885  /* no \if to else from */
1886  pg_log_error("\\else: no matching \\if");
1887  success = false;
1888  break;
1889  }
1890 
1892 }

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 1296 of file command.c.

1297 {
1298  if (active_branch)
1299  {
1300  char *encoding = psql_scan_slash_option(scan_state,
1301  OT_NORMAL, NULL, false);
1302 
1303  if (!encoding)
1304  {
1305  /* show encoding */
1307  }
1308  else
1309  {
1310  /* set encoding */
1311  if (PQsetClientEncoding(pset.db, encoding) == -1)
1312  pg_log_error("%s: invalid encoding name or conversion procedure not found", encoding);
1313  else
1314  {
1315  /* save encoding info into psql internal data */
1318  SetVariable(pset.vars, "ENCODING",
1320  }
1321  free(encoding);
1322  }
1323  }
1324  else
1325  ignore_slash_options(scan_state);
1326 
1327  return PSQL_CMD_SKIP_LINE;
1328 }
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:588
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6820
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:6828
int32 encoding
Definition: pg_database.h:41
VariableSpace vars
Definition: settings.h:121
int encoding
Definition: settings.h:83
int encoding
Definition: print.h:133
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211

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(), 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 1898 of file command.c.

1900 {
1901  bool success = true;
1902 
1903  switch (conditional_stack_peek(cstack))
1904  {
1905  case IFSTATE_TRUE:
1906  case IFSTATE_ELSE_TRUE:
1907  /* Close the \if block, keeping the query text */
1908  success = conditional_stack_pop(cstack);
1909  Assert(success);
1910  break;
1911  case IFSTATE_FALSE:
1912  case IFSTATE_IGNORED:
1913  case IFSTATE_ELSE_FALSE:
1914 
1915  /*
1916  * Discard any query text added by the just-skipped branch.
1917  */
1918  discard_query_text(scan_state, cstack, query_buf);
1919 
1920  /* Close the \if block */
1921  success = conditional_stack_pop(cstack);
1922  Assert(success);
1923  break;
1924  case IFSTATE_NONE:
1925  /* no \if to end */
1926  pg_log_error("\\endif: no matching \\if");
1927  success = false;
1928  break;
1929  }
1930 
1932 }
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_errverbose()

static backslashResult exec_command_errverbose ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1334 of file command.c.

1335 {
1336  if (active_branch)
1337  {
1338  if (pset.last_error_result)
1339  {
1340  char *msg;
1341 
1345  if (msg)
1346  {
1347  pg_log_error("%s", msg);
1348  PQfreemem(msg);
1349  }
1350  else
1351  puts(_("out of memory"));
1352  }
1353  else
1354  puts(_("There is no previous error."));
1355  }
1356 
1357  return PSQL_CMD_SKIP_LINE;
1358 }
char * PQresultVerboseErrorMessage(const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
Definition: fe-exec.c:3264
@ PQSHOW_CONTEXT_ALWAYS
Definition: libpq-fe.h:137
@ PQERRORS_VERBOSE
Definition: libpq-fe.h:129
PGresult * last_error_result
Definition: settings.h:89

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 1364 of file command.c.

1365 {
1366  bool success = true;
1367 
1368  if (active_branch)
1369  {
1370  char *fname = psql_scan_slash_option(scan_state,
1371  OT_NORMAL, NULL, false);
1372 
1373  success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
1374  free(fname);
1375  }
1376  else
1377  ignore_slash_options(scan_state);
1378 
1380 }

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_g()

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

Definition at line 1392 of file command.c.

1393 {
1395  char *fname;
1396 
1397  /*
1398  * Because the option processing for this is fairly complicated, we do it
1399  * and then decide whether the branch is active.
1400  */
1401  fname = psql_scan_slash_option(scan_state,
1402  OT_FILEPIPE, NULL, false);
1403 
1404  if (fname && fname[0] == '(')
1405  {
1406  /* Consume pset options through trailing ')' ... */
1407  status = process_command_g_options(fname + 1, scan_state,
1408  active_branch, cmd);
1409  free(fname);
1410  /* ... and again attempt to scan the filename. */
1411  fname = psql_scan_slash_option(scan_state,
1412  OT_FILEPIPE, NULL, false);
1413  }
1414 
1415  if (status == PSQL_CMD_SKIP_LINE && active_branch)
1416  {
1417  if (!fname)
1418  pset.gfname = NULL;
1419  else
1420  {
1421  expand_tilde(&fname);
1422  pset.gfname = pg_strdup(fname);
1423  }
1424  if (strcmp(cmd, "gx") == 0)
1425  {
1426  /* save settings if not done already, then force expanded=on */
1427  if (pset.gsavepopt == NULL)
1429  pset.popt.topt.expanded = 1;
1430  }
1432  }
1433 
1434  free(fname);
1435 
1436  return status;
1437 }
printQueryOpt * savePsetInfo(const printQueryOpt *popt)
Definition: command.c:4803
static backslashResult process_command_g_options(char *first_option, PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1445
@ OT_FILEPIPE
Definition: psqlscanslash.h:20
printQueryOpt * gsavepopt
Definition: settings.h:94
char * gfname
Definition: settings.h:93

References expand_tilde(), printTableOpt::expanded, free, _psqlSettings::gfname, _psqlSettings::gsavepopt, OT_FILEPIPE, pg_strdup(), _psqlSettings::popt, process_command_g_options(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), savePsetInfo(), status(), 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 1520 of file command.c.

1521 {
1523 
1524  if (active_branch)
1525  {
1526  pset.gdesc_flag = true;
1528  }
1529 
1530  return status;
1531 }
bool gdesc_flag
Definition: settings.h:97

References _psqlSettings::gdesc_flag, pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, and status().

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 1537 of file command.c.

1539 {
1540  bool success = true;
1541 
1542  if (active_branch)
1543  {
1544  char *myvar = psql_scan_slash_option(scan_state,
1545  OT_NORMAL, NULL, false);
1546  char *envvar = psql_scan_slash_option(scan_state,
1547  OT_NORMAL, NULL, false);
1548 
1549  if (!myvar || !envvar)
1550  {
1551  pg_log_error("\\%s: missing required argument", cmd);
1552  success = false;
1553  }
1554  else
1555  {
1556  char *envval = getenv(envvar);
1557 
1558  if (envval && !SetVariable(pset.vars, myvar, envval))
1559  success = false;
1560  }
1561  free(myvar);
1562  free(envvar);
1563  }
1564  else
1565  ignore_slash_options(scan_state);
1566 
1568 }

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_gexec()

static backslashResult exec_command_gexec ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1574 of file command.c.

1575 {
1577 
1578  if (active_branch)
1579  {
1580  pset.gexec_flag = true;
1582  }
1583 
1584  return status;
1585 }
bool gexec_flag
Definition: settings.h:98

References _psqlSettings::gexec_flag, pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, and status().

Referenced by exec_command().

◆ exec_command_gset()

static backslashResult exec_command_gset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1591 of file command.c.

1592 {
1594 
1595  if (active_branch)
1596  {
1597  char *prefix = psql_scan_slash_option(scan_state,
1598  OT_NORMAL, NULL, false);
1599 
1600  if (prefix)
1601  pset.gset_prefix = prefix;
1602  else
1603  {
1604  /* we must set a non-NULL prefix to trigger storing */
1605  pset.gset_prefix = pg_strdup("");
1606  }
1607  /* gset_prefix is freed later */
1609  }
1610  else
1611  ignore_slash_options(scan_state);
1612 
1613  return status;
1614 }
char * gset_prefix
Definition: settings.h:96

References _psqlSettings::gset_prefix, ignore_slash_options(), OT_NORMAL, pg_strdup(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and status().

Referenced by exec_command().

◆ exec_command_help()

static backslashResult exec_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1620 of file command.c.

1621 {
1622  if (active_branch)
1623  {
1624  char *opt = psql_scan_slash_option(scan_state,
1625  OT_WHOLE_LINE, NULL, false);
1626  size_t len;
1627 
1628  /* strip any trailing spaces and semicolons */
1629  if (opt)
1630  {
1631  len = strlen(opt);
1632  while (len > 0 &&
1633  (isspace((unsigned char) opt[len - 1])
1634  || opt[len - 1] == ';'))
1635  opt[--len] = '\0';
1636  }
1637 
1638  helpSQL(opt, pset.popt.topt.pager);
1639  free(opt);
1640  }
1641  else
1642  ignore_slash_whole_line(scan_state);
1643 
1644  return PSQL_CMD_SKIP_LINE;
1645 }
void helpSQL(const char *topic, unsigned short int pager)
Definition: help.c:585

References free, helpSQL(), ignore_slash_whole_line(), len, 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 1651 of file command.c.

1652 {
1653  bool success = true;
1654 
1655  if (active_branch)
1656  {
1657  if (pset.popt.topt.format != PRINT_HTML)
1658  success = do_pset("format", "html", &pset.popt, pset.quiet);
1659  else
1660  success = do_pset("format", "aligned", &pset.popt, pset.quiet);
1661  }
1662 
1664 }

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 1711 of file command.c.

1713 {
1714  if (conditional_active(cstack))
1715  {
1716  /*
1717  * First, push a new active stack entry; this ensures that the lexer
1718  * will perform variable substitution and backtick evaluation while
1719  * scanning the expression. (That should happen anyway, since we know
1720  * we're in an active outer branch, but let's be sure.)
1721  */
1723 
1724  /* Remember current query state in case we need to restore later */
1725  save_query_text_state(scan_state, cstack, query_buf);
1726 
1727  /*
1728  * Evaluate the expression; if it's false, change to inactive state.
1729  */
1730  if (!is_true_boolean_expression(scan_state, "\\if expression"))
1732  }
1733  else
1734  {
1735  /*
1736  * We're within an inactive outer branch, so this entire \if block
1737  * will be ignored. We don't want to evaluate the expression, so push
1738  * the "ignored" stack state before scanning it.
1739  */
1741 
1742  /* Remember current query state in case we need to restore later */
1743  save_query_text_state(scan_state, cstack, query_buf);
1744 
1745  ignore_boolean_expression(scan_state);
1746  }
1747 
1748  return PSQL_CMD_SKIP_LINE;
1749 }
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 1670 of file command.c.

1671 {
1672  bool success = true;
1673 
1674  if (active_branch)
1675  {
1676  char *fname = psql_scan_slash_option(scan_state,
1677  OT_NORMAL, NULL, true);
1678 
1679  if (!fname)
1680  {
1681  pg_log_error("\\%s: missing required argument", cmd);
1682  success = false;
1683  }
1684  else
1685  {
1686  bool include_relative;
1687 
1688  include_relative = (strcmp(cmd, "ir") == 0
1689  || strcmp(cmd, "include_relative") == 0);
1690  expand_tilde(&fname);
1691  success = (process_file(fname, include_relative) == EXIT_SUCCESS);
1692  free(fname);
1693  }
1694  }
1695  else
1696  ignore_slash_options(scan_state);
1697 
1699 }
int process_file(char *filename, bool use_relative_path)
Definition: command.c:4101
#define EXIT_SUCCESS
Definition: settings.h:162

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 1938 of file command.c.

1939 {
1940  bool success = true;
1941 
1942  if (active_branch)
1943  {
1944  char *pattern;
1945  bool show_verbose;
1946 
1947  pattern = psql_scan_slash_option(scan_state,
1948  OT_NORMAL, NULL, true);
1949 
1950  show_verbose = strchr(cmd, '+') ? true : false;
1951 
1952  success = listAllDbs(pattern, show_verbose);
1953 
1954  free(pattern);
1955  }
1956  else
1957  ignore_slash_options(scan_state);
1958 
1960 }
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:922

References free, ignore_slash_options(), listAllDbs(), OT_NORMAL, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), success, 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 1966 of file command.c.

1967 {
1969  bool success = true;
1970 
1971  if (active_branch)
1972  {
1973  char *opt1,
1974  *opt2;
1975 
1976  opt1 = psql_scan_slash_option(scan_state,
1977  OT_NORMAL, NULL, true);
1978  opt2 = psql_scan_slash_option(scan_state,
1979  OT_NORMAL, NULL, true);
1980 
1981  if (strcmp(cmd + 3, "export") == 0)
1982  {
1983  if (!opt2)
1984  {
1985  pg_log_error("\\%s: missing required argument", cmd);
1986  success = false;
1987  }
1988  else
1989  {
1990  expand_tilde(&opt2);
1991  success = do_lo_export(opt1, opt2);
1992  }
1993  }
1994 
1995  else if (strcmp(cmd + 3, "import") == 0)
1996  {
1997  if (!opt1)
1998  {
1999  pg_log_error("\\%s: missing required argument", cmd);
2000  success = false;
2001  }
2002  else
2003  {
2004  expand_tilde(&opt1);
2005  success = do_lo_import(opt1, opt2);
2006  }
2007  }
2008 
2009  else if (strcmp(cmd + 3, "list") == 0)
2010  success = listLargeObjects(false);
2011  else if (strcmp(cmd + 3, "list+") == 0)
2012  success = listLargeObjects(true);
2013 
2014  else if (strcmp(cmd + 3, "unlink") == 0)
2015  {
2016  if (!opt1)
2017  {
2018  pg_log_error("\\%s: missing required argument", cmd);
2019  success = false;
2020  }
2021  else
2022  success = do_lo_unlink(opt1);
2023  }
2024 
2025  else
2027 
2028  free(opt1);
2029  free(opt2);
2030  }
2031  else
2032  ignore_slash_options(scan_state);
2033 
2034  if (!success)
2036 
2037  return status;
2038 }
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(), free, ignore_slash_options(), listLargeObjects(), OT_NORMAL, pg_log_error, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, PSQL_CMD_UNKNOWN, psql_scan_slash_option(), status(), and success.

Referenced by exec_command().

◆ exec_command_out()

static backslashResult exec_command_out ( 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  {
2050  char *fname = psql_scan_slash_option(scan_state,
2051  OT_FILEPIPE, NULL, true);
2052 
2053  expand_tilde(&fname);
2054  success = setQFout(fname);
2055  free(fname);
2056  }
2057  else
2058  ignore_slash_filepipe(scan_state);
2059 
2061 }
bool setQFout(const char *fname)
Definition: common.c:93
static void ignore_slash_filepipe(PsqlScanState scan_state)
Definition: command.c:3020

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_password()

static backslashResult exec_command_password ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2093 of file command.c.

2094 {
2095  bool success = true;
2096 
2097  if (active_branch)
2098  {
2099  char *user = psql_scan_slash_option(scan_state,
2100  OT_SQLID, NULL, true);
2101  char *pw1 = NULL;
2102  char *pw2 = NULL;
2104  PromptInterruptContext prompt_ctx;
2105 
2106  if (user == NULL)
2107  {
2108  /* By default, the command applies to CURRENT_USER */
2109  PGresult *res;
2110 
2111  res = PSQLexec("SELECT CURRENT_USER");
2112  if (!res)
2113  return PSQL_CMD_ERROR;
2114 
2115  user = pg_strdup(PQgetvalue(res, 0, 0));
2116  PQclear(res);
2117  }
2118 
2119  /* Set up to let SIGINT cancel simple_prompt_extended() */
2120  prompt_ctx.jmpbuf = sigint_interrupt_jmp;
2121  prompt_ctx.enabled = &sigint_interrupt_enabled;
2122  prompt_ctx.canceled = false;
2123 
2124  initPQExpBuffer(&buf);
2125  printfPQExpBuffer(&buf, _("Enter new password for user \"%s\": "), user);
2126 
2127  pw1 = simple_prompt_extended(buf.data, false, &prompt_ctx);
2128  if (!prompt_ctx.canceled)
2129  pw2 = simple_prompt_extended("Enter it again: ", false, &prompt_ctx);
2130 
2131  if (prompt_ctx.canceled)
2132  {
2133  /* fail silently */
2134  success = false;
2135  }
2136  else if (strcmp(pw1, pw2) != 0)
2137  {
2138  pg_log_error("Passwords didn't match.");
2139  success = false;
2140  }
2141  else
2142  {
2143  char *encrypted_password;
2144 
2145  encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
2146 
2147  if (!encrypted_password)
2148  {
2150  success = false;
2151  }
2152  else
2153  {
2154  PGresult *res;
2155 
2156  printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
2157  fmtId(user));
2158  appendStringLiteralConn(&buf, encrypted_password, pset.db);
2159  res = PSQLexec(buf.data);
2160  if (!res)
2161  success = false;
2162  else
2163  PQclear(res);
2164  PQfreemem(encrypted_password);
2165  }
2166  }
2167 
2168  free(user);
2169  free(pw1);
2170  free(pw2);
2171  termPQExpBuffer(&buf);
2172  }
2173  else
2174  ignore_slash_options(scan_state);
2175 
2177 }
PGresult * PSQLexec(const char *query)
Definition: common.c:560
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
Definition: fe-auth.c:1190
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3705
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
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:293
const char * fmtId(const char *rawid)
Definition: string_utils.c:64
volatile sig_atomic_t * enabled
Definition: string.h:21

References _, appendStringLiteralConn(), buf, PromptInterruptContext::canceled, _psqlSettings::db, PromptInterruptContext::enabled, fmtId(), free, ignore_slash_options(), initPQExpBuffer(), PromptInterruptContext::jmpbuf, OT_SQLID, pg_log_error, pg_log_info, pg_strdup(), PQclear(), PQencryptPasswordConn(), PQerrorMessage(), PQfreemem(), PQgetvalue(), printfPQExpBuffer(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), PSQLexec(), res, 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 2067 of file command.c.

2069 {
2070  if (active_branch)
2071  {
2072  /*
2073  * We want to print the same thing \g would execute, but not to change
2074  * the query buffer state; so we can't use copy_previous_query().
2075  * Also, beware of possibility that buffer pointers are NULL.
2076  */
2077  if (query_buf && query_buf->len > 0)
2078  puts(query_buf->data);
2079  else if (previous_buf && previous_buf->len > 0)
2080  puts(previous_buf->data);
2081  else if (!pset.quiet)
2082  puts(_("Query buffer is empty."));
2083  fflush(stdout);
2084  }
2085 
2086  return PSQL_CMD_SKIP_LINE;
2087 }

References _, PQExpBufferData::data, fflush(), 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 2183 of file command.c.

2185 {
2186  bool success = true;
2187 
2188  if (active_branch)
2189  {
2190  char *opt,
2191  *prompt_text = NULL;
2192  char *arg1,
2193  *arg2;
2194 
2195  arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
2196  arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
2197 
2198  if (!arg1)
2199  {
2200  pg_log_error("\\%s: missing required argument", cmd);
2201  success = false;
2202  }
2203  else
2204  {
2205  char *result;
2206  PromptInterruptContext prompt_ctx;
2207 
2208  /* Set up to let SIGINT cancel simple_prompt_extended() */
2209  prompt_ctx.jmpbuf = sigint_interrupt_jmp;
2210  prompt_ctx.enabled = &sigint_interrupt_enabled;
2211  prompt_ctx.canceled = false;
2212 
2213  if (arg2)
2214  {
2215  prompt_text = arg1;
2216  opt = arg2;
2217  }
2218  else
2219  opt = arg1;
2220 
2221  if (!pset.inputfile)
2222  {
2223  result = simple_prompt_extended(prompt_text, true, &prompt_ctx);
2224  }
2225  else
2226  {
2227  if (prompt_text)
2228  {
2229  fputs(prompt_text, stdout);
2230  fflush(stdout);
2231  }
2232  result = gets_fromFile(stdin);
2233  if (!result)
2234  {
2235  pg_log_error("\\%s: could not read value for variable",
2236  cmd);
2237  success = false;
2238  }
2239  }
2240 
2241  if (prompt_ctx.canceled ||
2242  (result && !SetVariable(pset.vars, opt, result)))
2243  success = false;
2244 
2245  free(result);
2246  free(prompt_text);
2247  free(opt);
2248  }
2249  }
2250  else
2251  ignore_slash_options(scan_state);
2252 
2254 }
char * gets_fromFile(FILE *source)
Definition: input.c:187
char * inputfile
Definition: settings.h:113

References PromptInterruptContext::canceled, PromptInterruptContext::enabled, fflush(), 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 2260 of file command.c.

2261 {
2262  bool success = true;
2263 
2264  if (active_branch)
2265  {
2266  char *opt0 = psql_scan_slash_option(scan_state,
2267  OT_NORMAL, NULL, false);
2268  char *opt1 = psql_scan_slash_option(scan_state,
2269  OT_NORMAL, NULL, false);
2270 
2271  if (!opt0)
2272  {
2273  /* list all variables */
2274 
2275  int i;
2276  static const char *const my_list[] = {
2277  "border", "columns", "csv_fieldsep", "expanded", "fieldsep",
2278  "fieldsep_zero", "footer", "format", "linestyle", "null",
2279  "numericlocale", "pager", "pager_min_lines",
2280  "recordsep", "recordsep_zero",
2281  "tableattr", "title", "tuples_only",
2282  "unicode_border_linestyle",
2283  "unicode_column_linestyle",
2284  "unicode_header_linestyle",
2285  "xheader_width",
2286  NULL
2287  };
2288 
2289  for (i = 0; my_list[i] != NULL; i++)
2290  {
2291  char *val = pset_value_string(my_list[i], &pset.popt);
2292 
2293  printf("%-24s %s\n", my_list[i], val);
2294  free(val);
2295  }
2296 
2297  success = true;
2298  }
2299  else
2300  success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
2301 
2302  free(opt0);
2303  free(opt1);
2304  }
2305  else
2306  ignore_slash_options(scan_state);
2307 
2309 }
static char * pset_value_string(const char *param, printQueryOpt *popt)
Definition: command.c:4910
long val
Definition: informix.c:664

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 2315 of file command.c.

2316 {
2318 
2319  if (active_branch)
2321 
2322  return status;
2323 }
@ PSQL_CMD_TERMINATE
Definition: command.h:20

References PSQL_CMD_SKIP_LINE, PSQL_CMD_TERMINATE, and status().

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 2329 of file command.c.

2331 {
2332  if (active_branch)
2333  {
2334  resetPQExpBuffer(query_buf);
2335  psql_scan_reset(scan_state);
2336  if (!pset.quiet)
2337  puts(_("Query buffer reset (cleared)."));
2338  }
2339 
2340  return PSQL_CMD_SKIP_LINE;
2341 }
void psql_scan_reset(PsqlScanState state)

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

Referenced by exec_command().

◆ exec_command_s()

static backslashResult exec_command_s ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2347 of file command.c.

2348 {
2349  bool success = true;
2350 
2351  if (active_branch)
2352  {
2353  char *fname = psql_scan_slash_option(scan_state,
2354  OT_NORMAL, NULL, true);
2355 
2356  expand_tilde(&fname);
2357  success = printHistory(fname, pset.popt.topt.pager);
2358  if (success && !pset.quiet && fname)
2359  printf(_("Wrote history to file \"%s\".\n"), fname);
2360  if (!fname)
2361  putchar('\n');
2362  free(fname);
2363  }
2364  else
2365  ignore_slash_options(scan_state);
2366 
2368 }
bool printHistory(const char *fname, unsigned short int pager)
Definition: input.c:495

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_set()

static backslashResult exec_command_set ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2374 of file command.c.

2375 {
2376  bool success = true;
2377 
2378  if (active_branch)
2379  {
2380  char *opt0 = psql_scan_slash_option(scan_state,
2381  OT_NORMAL, NULL, false);
2382 
2383  if (!opt0)
2384  {
2385  /* list all variables */
2387  success = true;
2388  }
2389  else
2390  {
2391  /*
2392  * Set variable to the concatenation of the arguments.
2393  */
2394  char *newval;
2395  char *opt;
2396 
2397  opt = psql_scan_slash_option(scan_state,
2398  OT_NORMAL, NULL, false);
2399  newval = pg_strdup(opt ? opt : "");
2400  free(opt);
2401 
2402  while ((opt = psql_scan_slash_option(scan_state,
2403  OT_NORMAL, NULL, false)))
2404  {
2405  newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
2406  strcat(newval, opt);
2407  free(opt);
2408  }
2409 
2410  if (!SetVariable(pset.vars, opt0, newval))
2411  success = false;
2412 
2413  free(newval);
2414  }
2415  free(opt0);
2416  }
2417  else
2418  ignore_slash_options(scan_state);
2419 
2421 }
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define newval
void PrintVariables(VariableSpace space)
Definition: variables.c:186

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 2427 of file command.c.

2429 {
2430  bool success = true;
2431 
2432  if (active_branch)
2433  {
2434  char *envvar = psql_scan_slash_option(scan_state,
2435  OT_NORMAL, NULL, false);
2436  char *envval = psql_scan_slash_option(scan_state,
2437  OT_NORMAL, NULL, false);
2438 
2439  if (!envvar)
2440  {
2441  pg_log_error("\\%s: missing required argument", cmd);
2442  success = false;
2443  }
2444  else if (strchr(envvar, '=') != NULL)
2445  {
2446  pg_log_error("\\%s: environment variable name must not contain \"=\"",
2447  cmd);
2448  success = false;
2449  }
2450  else if (!envval)
2451  {
2452  /* No argument - unset the environment variable */
2453  unsetenv(envvar);
2454  success = true;
2455  }
2456  else
2457  {
2458  /* Set variable to the value of the next argument */
2459  setenv(envvar, envval, 1);
2460  success = true;
2461  }
2462  free(envvar);
2463  free(envval);
2464  }
2465  else
2466  ignore_slash_options(scan_state);
2467 
2469 }
#define unsetenv(x)
Definition: win32_port.h:535
#define setenv(x, y, z)
Definition: win32_port.h:534

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 2475 of file command.c.

2477 {
2479 
2480  if (active_branch)
2481  {
2482  bool show_linenumbers = (strchr(cmd, '+') != NULL);
2483  PQExpBuffer buf;
2484  char *obj_desc;
2485  Oid obj_oid = InvalidOid;
2487 
2488  buf = createPQExpBuffer();
2489  obj_desc = psql_scan_slash_option(scan_state,
2490  OT_WHOLE_LINE, NULL, true);
2491  if (!obj_desc)
2492  {
2493  if (is_func)
2494  pg_log_error("function name is required");
2495  else
2496  pg_log_error("view name is required");
2498  }
2499  else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
2500  {
2501  /* error already reported */
2503  }
2504  else if (!get_create_object_cmd(eot, obj_oid, buf))
2505  {
2506  /* error already reported */
2508  }
2509  else
2510  {
2511  FILE *output;
2512  bool is_pager;
2513 
2514  /* Select output stream: stdout, pager, or file */
2515  if (pset.queryFout == stdout)
2516  {
2517  /* count lines in function to see if pager is needed */
2518  int lineno = count_lines_in_buf(buf);
2519 
2520  output = PageOutput(lineno, &(pset.popt.topt));
2521  is_pager = true;
2522  }
2523  else
2524  {
2525  /* use previously set output file, without pager */
2526  output = pset.queryFout;
2527  is_pager = false;
2528  }
2529 
2530  if (show_linenumbers)
2531  {
2532  /* add line numbers */
2533  print_with_linenumbers(output, buf->data, is_func);
2534  }
2535  else
2536  {
2537  /* just send the definition to output */
2538  fputs(buf->data, output);
2539  }
2540 
2541  if (is_pager)
2542  ClosePager(output);
2543  }
2544 
2545  free(obj_desc);
2547  }
2548  else
2549  ignore_slash_whole_line(scan_state);
2550 
2551  return status;
2552 }
static int count_lines_in_buf(PQExpBuffer buf)
Definition: command.c:5587
static void print_with_linenumbers(FILE *output, char *lines, bool is_func)
Definition: command.c:5617
void ClosePager(FILE *pagerpipe)
Definition: print.c:3140
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:3088
static void output(uint64 loop_count)
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, status(), 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 2847 of file command.c.

2848 {
2849  bool success = true;
2850 
2851  if (active_branch)
2852  {
2853  char *opt = psql_scan_slash_option(scan_state,
2854  OT_WHOLE_LINE, NULL, false);
2855 
2856  success = do_shell(opt);
2857  free(opt);
2858  }
2859  else
2860  ignore_slash_whole_line(scan_state);
2861 
2863 }
static bool do_shell(const char *command)
Definition: command.c:4998

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 2869 of file command.c.

2870 {
2871  if (active_branch)
2872  {
2873  char *opt0 = psql_scan_slash_option(scan_state,
2874  OT_NORMAL, NULL, false);
2875 
2876  if (!opt0 || strcmp(opt0, "commands") == 0)
2878  else if (strcmp(opt0, "options") == 0)
2880  else if (strcmp(opt0, "variables") == 0)
2882  else
2884 
2885  free(opt0);
2886  }
2887  else
2888  ignore_slash_options(scan_state);
2889 
2890  return PSQL_CMD_SKIP_LINE;
2891 }
void helpVariables(unsigned short int pager)
Definition: help.c:379
void slashUsage(unsigned short int pager)
Definition: help.c:176
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_t()

static backslashResult exec_command_t ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2558 of file command.c.

2559 {
2560  bool success = true;
2561 
2562  if (active_branch)
2563  {
2564  char *opt = psql_scan_slash_option(scan_state,
2565  OT_NORMAL, NULL, true);
2566 
2567  success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
2568  free(opt);
2569  }
2570  else
2571  ignore_slash_options(scan_state);
2572 
2574 }

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 2580 of file command.c.

2581 {
2582  bool success = true;
2583 
2584  if (active_branch)
2585  {
2586  char *value = psql_scan_slash_option(scan_state,
2587  OT_NORMAL, NULL, false);
2588 
2589  success = do_pset("tableattr", value, &pset.popt, pset.quiet);
2590  free(value);
2591  }
2592  else
2593  ignore_slash_options(scan_state);
2594 
2596 }

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 2602 of file command.c.

2603 {
2604  bool success = true;
2605 
2606  if (active_branch)
2607  {
2608  char *opt = psql_scan_slash_option(scan_state,
2609  OT_NORMAL, NULL, false);
2610 
2611  if (opt)
2612  success = ParseVariableBool(opt, "\\timing", &pset.timing);
2613  else
2614  pset.timing = !pset.timing;
2615  if (!pset.quiet)
2616  {
2617  if (pset.timing)
2618  puts(_("Timing is on."));
2619  else
2620  puts(_("Timing is off."));
2621  }
2622  free(opt);
2623  }
2624  else
2625  ignore_slash_options(scan_state);
2626 
2628 }

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_unset()

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

Definition at line 2634 of file command.c.

2636 {
2637  bool success = true;
2638 
2639  if (active_branch)
2640  {
2641  char *opt = psql_scan_slash_option(scan_state,
2642  OT_NORMAL, NULL, false);
2643 
2644  if (!opt)
2645  {
2646  pg_log_error("\\%s: missing required argument", cmd);
2647  success = false;
2648  }
2649  else if (!SetVariable(pset.vars, opt, NULL))
2650  success = false;
2651 
2652  free(opt);
2653  }
2654  else
2655  ignore_slash_options(scan_state);
2656 
2658 }

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 2764 of file command.c.

2766 {
2767  bool success = true;
2768 
2769  if (active_branch)
2770  {
2771  char *opt = psql_scan_slash_option(scan_state,
2772  OT_NORMAL, NULL, true);
2773  double sleep = 2;
2774 
2775  /* Convert optional sleep-length argument */
2776  if (opt)
2777  {
2778  sleep = strtod(opt, NULL);
2779  if (sleep <= 0)
2780  sleep = 1;
2781  free(opt);
2782  }
2783 
2784  /* If query_buf is empty, recall and execute previous query */
2785  (void) copy_previous_query(query_buf, previous_buf);
2786 
2787  success =