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, const char *cmd)
 
static backslashResult exec_command_shell_escape (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_slash_command_help (PsqlScanState scan_state, bool active_branch)
 
static char * read_connect_arg (PsqlScanState scan_state)
 
static PQExpBuffer gather_boolean_expression (PsqlScanState scan_state)
 
static bool is_true_boolean_expression (PsqlScanState scan_state, const char *name)
 
static void ignore_boolean_expression (PsqlScanState scan_state)
 
static void ignore_slash_options (PsqlScanState scan_state)
 
static void ignore_slash_filepipe (PsqlScanState scan_state)
 
static void ignore_slash_whole_line (PsqlScanState scan_state)
 
static bool is_branching_command (const char *cmd)
 
static void save_query_text_state (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static void discard_query_text (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static bool copy_previous_query (PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static bool do_connect (enum trivalue reuse_previous_specification, char *dbname, char *user, char *host, char *port)
 
static 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 5004 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 4184 of file command.c.

4185 {
4186  switch (in)
4187  {
4188  case PRINT_NOTHING:
4189  return "nothing";
4190  break;
4191  case PRINT_ALIGNED:
4192  return "aligned";
4193  break;
4194  case PRINT_ASCIIDOC:
4195  return "asciidoc";
4196  break;
4197  case PRINT_CSV:
4198  return "csv";
4199  break;
4200  case PRINT_HTML:
4201  return "html";
4202  break;
4203  case PRINT_LATEX:
4204  return "latex";
4205  break;
4206  case PRINT_LATEX_LONGTABLE:
4207  return "latex-longtable";
4208  break;
4209  case PRINT_TROFF_MS:
4210  return "troff-ms";
4211  break;
4212  case PRINT_UNALIGNED:
4213  return "unaligned";
4214  break;
4215  case PRINT_WRAPPED:
4216  return "wrapped";
4217  break;
4218  }
4219  return "unknown";
4220 }
@ 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 4240 of file command.c.

4241 {
4242  switch (linestyle)
4243  {
4245  return "single";
4246  break;
4248  return "double";
4249  break;
4250  }
4251  return "unknown";
4252 }
@ 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 3654 of file command.c.

3655 {
3656  if (!pset.quiet && !pset.notty)
3657  {
3658  int client_ver = PG_VERSION_NUM;
3659  char cverbuf[32];
3660  char sverbuf[32];
3661 
3662  if (pset.sversion != client_ver)
3663  {
3664  const char *server_version;
3665 
3666  /* Try to get full text form, might include "devel" etc */
3667  server_version = PQparameterStatus(pset.db, "server_version");
3668  /* Otherwise fall back on pset.sversion */
3669  if (!server_version)
3670  {
3672  sverbuf, sizeof(sverbuf));
3673  server_version = sverbuf;
3674  }
3675 
3676  printf(_("%s (%s, server %s)\n"),
3677  pset.progname, PG_VERSION, server_version);
3678  }
3679  /* For version match, only print psql banner on startup. */
3680  else if (in_startup)
3681  printf("%s (%s)\n", pset.progname, PG_VERSION);
3682 
3683  /*
3684  * Warn if server's major version is newer than ours, or if server
3685  * predates our support cutoff (currently 9.2).
3686  */
3687  if (pset.sversion / 100 > client_ver / 100 ||
3688  pset.sversion < 90200)
3689  printf(_("WARNING: %s major version %s, server major version %s.\n"
3690  " Some psql features might not work.\n"),
3691  pset.progname,
3692  formatPGVersionNumber(client_ver, false,
3693  cverbuf, sizeof(cverbuf)),
3695  sverbuf, sizeof(sverbuf)));
3696 
3697 #ifdef WIN32
3698  if (in_startup)
3699  checkWin32Codepage();
3700 #endif
3701  printSSLInfo();
3702  printGSSInfo();
3703  }
3704 }
static void printSSLInfo(void)
Definition: command.c:3713
static void printGSSInfo(void)
Definition: command.c:3738
#define _(x)
Definition: elog.c:91
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6983
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 3126 of file command.c.

3127 {
3128  if (query_buf && query_buf->len == 0)
3129  {
3130  appendPQExpBufferStr(query_buf, previous_buf->data);
3131  return true;
3132  }
3133  return false;
3134 }
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 5621 of file command.c.

5622 {
5623  int lineno = 0;
5624  const char *lines = buf->data;
5625 
5626  while (*lines != '\0')
5627  {
5628  lineno++;
5629  /* find start of next line */
5630  lines = strchr(lines, '\n');
5631  if (!lines)
5632  break;
5633  lines++;
5634  }
5635 
5636  return lineno;
5637 }
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 3100 of file command.c.

3102 {
3103  if (query_buf)
3104  {
3105  int new_len = conditional_stack_get_query_len(cstack);
3106 
3107  Assert(new_len >= 0 && new_len <= query_buf->len);
3108  query_buf->len = new_len;
3109  query_buf->data[new_len] = '\0';
3110  }
3111  psql_scan_set_paren_depth(scan_state,
3113 }
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 3195 of file command.c.

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

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

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

5015 {
5016  int result;
5017 
5018  fflush(NULL);
5019  if (!command)
5020  {
5021  char *sys;
5022  const char *shellName;
5023 
5024  shellName = getenv("SHELL");
5025 #ifdef WIN32
5026  if (shellName == NULL)
5027  shellName = getenv("COMSPEC");
5028 #endif
5029  if (shellName == NULL)
5030  shellName = DEFAULT_SHELL;
5031 
5032  /* See EDITOR handling comment for an explanation */
5033 #ifndef WIN32
5034  sys = psprintf("exec %s", shellName);
5035 #else
5036  sys = psprintf("\"%s\"", shellName);
5037 #endif
5038  result = system(sys);
5039  free(sys);
5040  }
5041  else
5042  result = system(command);
5043 
5044  if (result == 0)
5045  {
5046  SetVariable(pset.vars, "SHELL_EXIT_CODE", "0");
5047  SetVariable(pset.vars, "SHELL_ERROR", "false");
5048  }
5049  else
5050  {
5051  int exit_code = wait_result_to_exit_code(result);
5052  char buf[32];
5053 
5054  snprintf(buf, sizeof(buf), "%d", exit_code);
5055  SetVariable(pset.vars, "SHELL_EXIT_CODE", buf);
5056  SetVariable(pset.vars, "SHELL_ERROR", "true");
5057  }
5058 
5059  if (result == 127 || result == -1)
5060  {
5061  pg_log_error("\\!: failed");
5062  return false;
5063  }
5064  return true;
5065 }
#define DEFAULT_SHELL
Definition: command.c:5004
static void const char fflush(stdout)
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
VariableSpace vars
Definition: settings.h:121
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
int wait_result_to_exit_code(int exit_status)
Definition: wait_error.c:138

References buf, DEFAULT_SHELL, fflush(), free, pg_log_error, pset, psprintf(), SetVariable(), snprintf, _psqlSettings::vars, and wait_result_to_exit_code().

Referenced by exec_command_shell_escape().

◆ do_watch()

static bool do_watch ( PQExpBuffer  query_buf,
double  sleep 
)
static

Definition at line 5074 of file command.c.

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

5312 {
5314  {
5315  printf(_("********* QUERY **********\n"
5316  "%s\n"
5317  "**************************\n\n"), query);
5318  fflush(stdout);
5319  if (pset.logfile)
5320  {
5322  _("********* QUERY **********\n"
5323  "%s\n"
5324  "**************************\n\n"), query);
5325  fflush(pset.logfile);
5326  }
5327 
5329  return false;
5330  }
5331  return true;
5332 }
@ 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 3840 of file command.c.

3841 {
3842  const char *editorName;
3843  const char *editor_lineno_arg = NULL;
3844  char *sys;
3845  int result;
3846 
3847  Assert(fname != NULL);
3848 
3849  /* Find an editor to use */
3850  editorName = getenv("PSQL_EDITOR");
3851  if (!editorName)
3852  editorName = getenv("EDITOR");
3853  if (!editorName)
3854  editorName = getenv("VISUAL");
3855  if (!editorName)
3856  editorName = DEFAULT_EDITOR;
3857 
3858  /* Get line number argument, if we need it. */
3859  if (lineno > 0)
3860  {
3861  editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
3862 #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
3863  if (!editor_lineno_arg)
3864  editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
3865 #endif
3866  if (!editor_lineno_arg)
3867  {
3868  pg_log_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number");
3869  return false;
3870  }
3871  }
3872 
3873  /*
3874  * On Unix the EDITOR value should *not* be quoted, since it might include
3875  * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
3876  * if necessary. But this policy is not very workable on Windows, due to
3877  * severe brain damage in their command shell plus the fact that standard
3878  * program paths include spaces.
3879  */
3880 #ifndef WIN32
3881  if (lineno > 0)
3882  sys = psprintf("exec %s %s%d '%s'",
3883  editorName, editor_lineno_arg, lineno, fname);
3884  else
3885  sys = psprintf("exec %s '%s'",
3886  editorName, fname);
3887 #else
3888  if (lineno > 0)
3889  sys = psprintf("\"%s\" %s%d \"%s\"",
3890  editorName, editor_lineno_arg, lineno, fname);
3891  else
3892  sys = psprintf("\"%s\" \"%s\"",
3893  editorName, fname);
3894 #endif
3895  fflush(NULL);
3896  result = system(sys);
3897  if (result == -1)
3898  pg_log_error("could not start editor \"%s\"", editorName);
3899  else if (result == 127)
3900  pg_log_error("could not start /bin/sh");
3901  free(sys);
3902 
3903  return result == 0;
3904 }
#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 287 of file command.c.

292 {
294  bool active_branch = conditional_active(cstack);
295 
296  /*
297  * In interactive mode, warn when we're ignoring a command within a false
298  * \if-branch. But we continue on, so as to parse and discard the right
299  * amount of parameter text. Each individual backslash command subroutine
300  * is responsible for doing nothing after discarding appropriate
301  * arguments, if !active_branch.
302  */
303  if (pset.cur_cmd_interactive && !active_branch &&
304  !is_branching_command(cmd))
305  {
306  pg_log_warning("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block",
307  cmd);
308  }
309 
310  if (strcmp(cmd, "a") == 0)
311  status = exec_command_a(scan_state, active_branch);
312  else if (strcmp(cmd, "bind") == 0)
313  status = exec_command_bind(scan_state, active_branch);
314  else if (strcmp(cmd, "C") == 0)
315  status = exec_command_C(scan_state, active_branch);
316  else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
317  status = exec_command_connect(scan_state, active_branch);
318  else if (strcmp(cmd, "cd") == 0)
319  status = exec_command_cd(scan_state, active_branch, cmd);
320  else if (strcmp(cmd, "conninfo") == 0)
321  status = exec_command_conninfo(scan_state, active_branch);
322  else if (pg_strcasecmp(cmd, "copy") == 0)
323  status = exec_command_copy(scan_state, active_branch);
324  else if (strcmp(cmd, "copyright") == 0)
325  status = exec_command_copyright(scan_state, active_branch);
326  else if (strcmp(cmd, "crosstabview") == 0)
327  status = exec_command_crosstabview(scan_state, active_branch);
328  else if (cmd[0] == 'd')
329  status = exec_command_d(scan_state, active_branch, cmd);
330  else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
331  status = exec_command_edit(scan_state, active_branch,
332  query_buf, previous_buf);
333  else if (strcmp(cmd, "ef") == 0)
334  status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
335  else if (strcmp(cmd, "ev") == 0)
336  status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
337  else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0 ||
338  strcmp(cmd, "warn") == 0)
339  status = exec_command_echo(scan_state, active_branch, cmd);
340  else if (strcmp(cmd, "elif") == 0)
341  status = exec_command_elif(scan_state, cstack, query_buf);
342  else if (strcmp(cmd, "else") == 0)
343  status = exec_command_else(scan_state, cstack, query_buf);
344  else if (strcmp(cmd, "endif") == 0)
345  status = exec_command_endif(scan_state, cstack, query_buf);
346  else if (strcmp(cmd, "encoding") == 0)
347  status = exec_command_encoding(scan_state, active_branch);
348  else if (strcmp(cmd, "errverbose") == 0)
349  status = exec_command_errverbose(scan_state, active_branch);
350  else if (strcmp(cmd, "f") == 0)
351  status = exec_command_f(scan_state, active_branch);
352  else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
353  status = exec_command_g(scan_state, active_branch, cmd);
354  else if (strcmp(cmd, "gdesc") == 0)
355  status = exec_command_gdesc(scan_state, active_branch);
356  else if (strcmp(cmd, "getenv") == 0)
357  status = exec_command_getenv(scan_state, active_branch, cmd);
358  else if (strcmp(cmd, "gexec") == 0)
359  status = exec_command_gexec(scan_state, active_branch);
360  else if (strcmp(cmd, "gset") == 0)
361  status = exec_command_gset(scan_state, active_branch);
362  else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
363  status = exec_command_help(scan_state, active_branch);
364  else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
365  status = exec_command_html(scan_state, active_branch);
366  else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
367  strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
368  status = exec_command_include(scan_state, active_branch, cmd);
369  else if (strcmp(cmd, "if") == 0)
370  status = exec_command_if(scan_state, cstack, query_buf);
371  else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
372  strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
373  status = exec_command_list(scan_state, active_branch, cmd);
374  else if (strncmp(cmd, "lo_", 3) == 0)
375  status = exec_command_lo(scan_state, active_branch, cmd);
376  else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
377  status = exec_command_out(scan_state, active_branch);
378  else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
379  status = exec_command_print(scan_state, active_branch,
380  query_buf, previous_buf);
381  else if (strcmp(cmd, "password") == 0)
382  status = exec_command_password(scan_state, active_branch);
383  else if (strcmp(cmd, "prompt") == 0)
384  status = exec_command_prompt(scan_state, active_branch, cmd);
385  else if (strcmp(cmd, "pset") == 0)
386  status = exec_command_pset(scan_state, active_branch);
387  else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
388  status = exec_command_quit(scan_state, active_branch);
389  else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
390  status = exec_command_reset(scan_state, active_branch, query_buf);
391  else if (strcmp(cmd, "s") == 0)
392  status = exec_command_s(scan_state, active_branch);
393  else if (strcmp(cmd, "set") == 0)
394  status = exec_command_set(scan_state, active_branch);
395  else if (strcmp(cmd, "setenv") == 0)
396  status = exec_command_setenv(scan_state, active_branch, cmd);
397  else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
398  status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
399  else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
400  status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
401  else if (strcmp(cmd, "t") == 0)
402  status = exec_command_t(scan_state, active_branch);
403  else if (strcmp(cmd, "T") == 0)
404  status = exec_command_T(scan_state, active_branch);
405  else if (strcmp(cmd, "timing") == 0)
406  status = exec_command_timing(scan_state, active_branch);
407  else if (strcmp(cmd, "unset") == 0)
408  status = exec_command_unset(scan_state, active_branch, cmd);
409  else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
410  status = exec_command_write(scan_state, active_branch, cmd,
411  query_buf, previous_buf);
412  else if (strcmp(cmd, "watch") == 0)
413  status = exec_command_watch(scan_state, active_branch,
414  query_buf, previous_buf);
415  else if (strcmp(cmd, "x") == 0)
416  status = exec_command_x(scan_state, active_branch);
417  else if (strcmp(cmd, "z") == 0 || strcmp(cmd, "zS") == 0)
418  status = exec_command_z(scan_state, active_branch, cmd);
419  else if (strcmp(cmd, "!") == 0)
420  status = exec_command_shell_escape(scan_state, active_branch);
421  else if (strcmp(cmd, "?") == 0)
422  status = exec_command_slash_command_help(scan_state, active_branch);
423  else
425 
426  /*
427  * All the commands that return PSQL_CMD_SEND want to execute previous_buf
428  * if query_buf is empty. For convenience we implement that here, not in
429  * the individual command subroutines.
430  */
431  if (status == PSQL_CMD_SEND)
432  (void) copy_previous_query(query_buf, previous_buf);
433 
434  return status;
435 }
static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1899
static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch)
Definition: command.c:713
static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1335
static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2068
static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1652
static bool copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:3126
static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1393
static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2581
static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1521
static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1621
static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch)
Definition: command.c:444
static backslashResult exec_command_bind(PsqlScanState scan_state, bool active_branch)
Definition: command.c:463
static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2094
static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2813
static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1712
static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1758
static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1365
static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:2330
static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2045
static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1252
static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:749
static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2863
static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:583
static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1592
static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, bool is_func)
Definition: command.c:1143
static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2635
static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2375
static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2348
static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2184
static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2765
static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1671
static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1939
static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2603
static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch)
Definition: command.c:497
static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1297
static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:1055
static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1834
static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
Definition: command.c:647
static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch)
Definition: command.c:691
static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2885
static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
Definition: command.c:725
static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2261
static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2559
static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2665
static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch, const char *cmd, bool is_func)
Definition: command.c:2476
static bool is_branching_command(const char *cmd)
Definition: command.c:3064
static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch)
Definition: command.c:530
static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1575
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2835
static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1967
static backslashResult exec_command_getenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1538
static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2316
static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2428
@ 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:224
#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 444 of file command.c.

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

464 {
466 
467  if (active_branch)
468  {
469  char *opt;
470  int nparams = 0;
471  int nalloc = 0;
472 
473  pset.bind_params = NULL;
474 
475  while ((opt = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false)))
476  {
477  nparams++;
478  if (nparams > nalloc)
479  {
480  nalloc = nalloc ? nalloc * 2 : 1;
481  pset.bind_params = pg_realloc_array(pset.bind_params, char *, nalloc);
482  }
483  pset.bind_params[nparams - 1] = opt;
484  }
485 
486  pset.bind_nparams = nparams;
487  pset.bind_flag = true;
488  }
489 
490  return status;
491 }
#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, 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 497 of file command.c.

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

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

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

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

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

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

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

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

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

726 {
728 
729  if (active_branch)
730  {
731  int i;
732 
733  for (i = 0; i < lengthof(pset.ctv_args); i++)
734  pset.ctv_args[i] = psql_scan_slash_option(scan_state,
735  OT_NORMAL, NULL, true);
736  pset.crosstab_flag = true;
738  }
739  else
740  ignore_slash_options(scan_state);
741 
742  return status;
743 }
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 749 of file command.c.

750 {
752  bool success = true;
753 
754  if (active_branch)
755  {
756  char *pattern;
757  bool show_verbose,
758  show_system;
759 
760  /* We don't do SQLID reduction on the pattern yet */
761  pattern = psql_scan_slash_option(scan_state,
762  OT_NORMAL, NULL, true);
763 
764  show_verbose = strchr(cmd, '+') ? true : false;
765  show_system = strchr(cmd, 'S') ? true : false;
766 
767  switch (cmd[1])
768  {
769  case '\0':
770  case '+':
771  case 'S':
772  if (pattern)
773  success = describeTableDetails(pattern, show_verbose, show_system);
774  else
775  /* standard listing of interesting things */
776  success = listTables("tvmsE", NULL, show_verbose, show_system);
777  break;
778  case 'A':
779  {
780  char *pattern2 = NULL;
781 
782  if (pattern && cmd[2] != '\0' && cmd[2] != '+')
783  pattern2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, true);
784 
785  switch (cmd[2])
786  {
787  case '\0':
788  case '+':
789  success = describeAccessMethods(pattern, show_verbose);
790  break;
791  case 'c':
792  success = listOperatorClasses(pattern, pattern2, show_verbose);
793  break;
794  case 'f':
795  success = listOperatorFamilies(pattern, pattern2, show_verbose);
796  break;
797  case 'o':
798  success = listOpFamilyOperators(pattern, pattern2, show_verbose);
799  break;
800  case 'p':
801  success = listOpFamilyFunctions(pattern, pattern2, show_verbose);
802  break;
803  default:
805  break;
806  }
807 
808  free(pattern2);
809  }
810  break;
811  case 'a':
812  success = describeAggregates(pattern, show_verbose, show_system);
813  break;
814  case 'b':
815  success = describeTablespaces(pattern, show_verbose);
816  break;
817  case 'c':
818  if (strncmp(cmd, "dconfig", 7) == 0)
820  show_verbose,
821  show_system);
822  else
823  success = listConversions(pattern,
824  show_verbose,
825  show_system);
826  break;
827  case 'C':
828  success = listCasts(pattern, show_verbose);
829  break;
830  case 'd':
831  if (strncmp(cmd, "ddp", 3) == 0)
832  success = listDefaultACLs(pattern);
833  else
834  success = objectDescription(pattern, show_system);
835  break;
836  case 'D':
837  success = listDomains(pattern, show_verbose, show_system);
838  break;
839  case 'f': /* function subsystem */
840  switch (cmd[2])
841  {
842  case '\0':
843  case '+':
844  case 'S':
845  case 'a':
846  case 'n':
847  case 'p':
848  case 't':
849  case 'w':
850  success = exec_command_dfo(scan_state, cmd, pattern,
851  show_verbose, show_system);
852  break;
853  default:
855  break;
856  }
857  break;
858  case 'g':
859  /* no longer distinct from \du */
860  success = describeRoles(pattern, show_verbose, show_system);
861  break;
862  case 'l':
863  success = listLargeObjects(show_verbose);
864  break;
865  case 'L':
866  success = listLanguages(pattern, show_verbose, show_system);
867  break;
868  case 'n':
869  success = listSchemas(pattern, show_verbose, show_system);
870  break;
871  case 'o':
872  success = exec_command_dfo(scan_state, cmd, pattern,
873  show_verbose, show_system);
874  break;
875  case 'O':
876  success = listCollations(pattern, show_verbose, show_system);
877  break;
878  case 'p':
879  success = permissionsList(pattern, show_system);
880  break;
881  case 'P':
882  {
883  switch (cmd[2])
884  {
885  case '\0':
886  case '+':
887  case 't':
888  case 'i':
889  case 'n':
890  success = listPartitionedTables(&cmd[2], pattern, show_verbose);
891  break;
892  default:
894  break;
895  }
896  }
897  break;
898  case 'T':
899  success = describeTypes(pattern, show_verbose, show_system);
900  break;
901  case 't':
902  case 'v':
903  case 'm':
904  case 'i':
905  case 's':
906  case 'E':
907  success = listTables(&cmd[1], pattern, show_verbose, show_system);
908  break;
909  case 'r':
910  if (cmd[2] == 'd' && cmd[3] == 's')
911  {
912  char *pattern2 = NULL;
913 
914  if (pattern)
915  pattern2 = psql_scan_slash_option(scan_state,
916  OT_NORMAL, NULL, true);
917  success = listDbRoleSettings(pattern, pattern2);
918 
919  free(pattern2);
920  }
921  else
923  break;
924  case 'R':
925  switch (cmd[2])
926  {
927  case 'p':
928  if (show_verbose)
929  success = describePublications(pattern);
930  else
931  success = listPublications(pattern);
932  break;
933  case 's':
934  success = describeSubscriptions(pattern, show_verbose);
935  break;
936  default:
938  }
939  break;
940  case 'u':
941  success = describeRoles(pattern, show_verbose, show_system);
942  break;
943  case 'F': /* text search subsystem */
944  switch (cmd[2])
945  {
946  case '\0':
947  case '+':
948  success = listTSConfigs(pattern, show_verbose);
949  break;
950  case 'p':
951  success = listTSParsers(pattern, show_verbose);
952  break;
953  case 'd':
954  success = listTSDictionaries(pattern, show_verbose);
955  break;
956  case 't':
957  success = listTSTemplates(pattern, show_verbose);
958  break;
959  default:
961  break;
962  }
963  break;
964  case 'e': /* SQL/MED subsystem */
965  switch (cmd[2])
966  {
967  case 's':
968  success = listForeignServers(pattern, show_verbose);
969  break;
970  case 'u':
971  success = listUserMappings(pattern, show_verbose);
972  break;
973  case 'w':
974  success = listForeignDataWrappers(pattern, show_verbose);
975  break;
976  case 't':
977  success = listForeignTables(pattern, show_verbose);
978  break;
979  default:
981  break;
982  }
983  break;
984  case 'x': /* Extensions */
985  if (show_verbose)
986  success = listExtensionContents(pattern);
987  else
988  success = listExtensions(pattern);
989  break;
990  case 'X': /* Extended Statistics */
991  success = listExtendedStats(pattern);
992  break;
993  case 'y': /* Event Triggers */
994  success = listEventTriggers(pattern, show_verbose);
995  break;
996  default:
998  }
999 
1000  free(pattern);
1001  }
1002  else
1003  ignore_slash_options(scan_state);
1004 
1005  if (!success)
1007 
1008  return status;
1009 }
static bool exec_command_dfo(PsqlScanState scan_state, const char *cmd, const char *pattern, bool show_verbose, bool show_system)
Definition: command.c:1013
bool listUserMappings(const char *pattern, bool verbose)
Definition: describe.c:5835
bool listTSConfigs(const char *pattern, bool verbose)
Definition: describe.c:5480
bool describeRoles(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3614
bool listOpFamilyFunctions(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:6917
bool listPublications(const char *pattern)
Definition: describe.c:6181
bool listTSParsers(const char *pattern, bool verbose)
Definition: describe.c:5098
bool listExtensionContents(const char *pattern)
Definition: describe.c:6016
bool describeAggregates(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:71
bool listForeignDataWrappers(const char *pattern, bool verbose)
Definition: describe.c:5686
bool listPartitionedTables(const char *reltypes, const char *pattern, bool verbose)
Definition: describe.c:4052
bool describeSubscriptions(const char *pattern, bool verbose)
Definition: describe.c:6490
bool listExtendedStats(const char *pattern)
Definition: describe.c:4645
bool describeTypes(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:619
bool listOperatorFamilies(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6728
bool listForeignServers(const char *pattern, bool verbose)
Definition: describe.c:5758
bool describeTableDetails(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:1446
bool listDomains(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4330
bool listTSDictionaries(const char *pattern, bool verbose)
Definition: describe.c:5348
bool listDbRoleSettings(const char *pattern, const char *pattern2)
Definition: describe.c:3769
bool listCollations(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4861
bool listSchemas(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4976
bool listExtensions(const char *pattern)
Definition: describe.c:5964
bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3853
bool listTSTemplates(const char *pattern, bool verbose)
Definition: describe.c:5414
bool describeTablespaces(const char *pattern, bool verbose)
Definition: describe.c:217
bool listCasts(const char *pattern, bool verbose)
Definition: describe.c:4742
bool listOpFamilyOperators(const char *access_method_pattern, const char *family_pattern, bool verbose)
Definition: describe.c:6818
bool listOperatorClasses(const char *access_method_pattern, const char *type_pattern, bool verbose)
Definition: describe.c:6626
bool listForeignTables(const char *pattern, bool verbose)
Definition: describe.c:5891
bool describeConfigurationParameters(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4495
bool listEventTriggers(const char *pattern, bool verbose)
Definition: describe.c:4564
bool listDefaultACLs(const char *pattern)
Definition: describe.c:1174
bool listConversions(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4414
bool permissionsList(const char *pattern, bool showSystem)
Definition: describe.c:1014
bool describeAccessMethods(const char *pattern, bool verbose)
Definition: describe.c:142
bool listLargeObjects(bool verbose)
Definition: describe.c:7007
bool listLanguages(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4253
bool describePublications(const char *pattern)
Definition: describe.c:6304
bool objectDescription(const char *pattern, bool showSystem)
Definition: describe.c:1252
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 1013 of file command.c.

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

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

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

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

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

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

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

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

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

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

1336 {
1337  if (active_branch)
1338  {
1339  if (pset.last_error_result)
1340  {
1341  char *msg;
1342 
1346  if (msg)
1347  {
1348  pg_log_error("%s", msg);
1349  PQfreemem(msg);
1350  }
1351  else
1352  puts(_("out of memory"));
1353  }
1354  else
1355  puts(_("There is no previous error."));
1356  }
1357 
1358  return PSQL_CMD_SKIP_LINE;
1359 }
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 1365 of file command.c.

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

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

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

1522 {
1524 
1525  if (active_branch)
1526  {
1527  pset.gdesc_flag = true;
1529  }
1530 
1531  return status;
1532 }
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 1538 of file command.c.

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

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

1576 {
1578 
1579  if (active_branch)
1580  {
1581  pset.gexec_flag = true;
1583  }
1584 
1585  return status;
1586 }
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 1592 of file command.c.

1593 {
1595 
1596  if (active_branch)
1597  {
1598  char *prefix = psql_scan_slash_option(scan_state,
1599  OT_NORMAL, NULL, false);
1600 
1601  if (prefix)
1602  pset.gset_prefix = prefix;
1603  else
1604  {
1605  /* we must set a non-NULL prefix to trigger storing */
1606  pset.gset_prefix = pg_strdup("");
1607  }
1608  /* gset_prefix is freed later */
1610  }
1611  else
1612  ignore_slash_options(scan_state);
1613 
1614  return status;
1615 }
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 1621 of file command.c.

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

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

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

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

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

1672 {
1673  bool success = true;
1674 
1675  if (active_branch)
1676  {
1677  char *fname = psql_scan_slash_option(scan_state,
1678  OT_NORMAL, NULL, true);
1679 
1680  if (!fname)
1681  {
1682  pg_log_error("\\%s: missing required argument", cmd);
1683  success = false;
1684  }
1685  else
1686  {
1687  bool include_relative;
1688 
1689  include_relative = (strcmp(cmd, "ir") == 0
1690  || strcmp(cmd, "include_relative") == 0);
1691  expand_tilde(&fname);
1692  success = (process_file(fname, include_relative) == EXIT_SUCCESS);
1693  free(fname);
1694  }
1695  }
1696  else
1697  ignore_slash_options(scan_state);
1698 
1700 }
int process_file(char *filename, bool use_relative_path)
Definition: command.c:4117
#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 1939 of file command.c.

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

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

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

2046 {
2047  bool success = true;
2048 
2049  if (active_branch)
2050  {
2051  char *fname = psql_scan_slash_option(scan_state,
2052  OT_FILEPIPE, NULL, true);
2053 
2054  expand_tilde(&fname);
2055  success = setQFout(fname);
2056  free(fname);
2057  }
2058  else
2059  ignore_slash_filepipe(scan_state);
2060 
2062 }
bool setQFout(const char *fname)
Definition: common.c:93
static void ignore_slash_filepipe(PsqlScanState scan_state)
Definition: command.c:3036

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

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

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

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

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

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

2317 {
2319 
2320  if (active_branch)
2322 
2323  return status;
2324 }
@ 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 2330 of file command.c.

2332 {
2333  if (active_branch)
2334  {
2335  resetPQExpBuffer(query_buf);
2336  psql_scan_reset(scan_state);
2337  if (!pset.quiet)
2338  puts(_("Query buffer reset (cleared)."));
2339  }
2340 
2341  return PSQL_CMD_SKIP_LINE;
2342 }
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 2348 of file command.c.

2349 {
2350  bool success = true;
2351 
2352  if (active_branch)
2353  {
2354  char *fname = psql_scan_slash_option(scan_state,
2355  OT_NORMAL, NULL, true);
2356 
2357  expand_tilde(&fname);
2358  success = printHistory(fname, pset.popt.topt.pager);
2359  if (success && !pset.quiet && fname)
2360  printf(_("Wrote history to file \"%s\".\n"), fname);
2361  if (!fname)
2362  putchar('\n');
2363  free(fname);
2364  }
2365  else
2366  ignore_slash_options(scan_state);
2367 
2369 }
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 2375 of file command.c.

2376 {
2377  bool success = true;
2378 
2379  if (active_branch)
2380  {
2381  char *opt0 = psql_scan_slash_option(scan_state,
2382  OT_NORMAL, NULL, false);
2383 
2384  if (!opt0)
2385  {
2386  /* list all variables */
2388  success = true;
2389  }
2390  else
2391  {
2392  /*
2393  * Set variable to the concatenation of the arguments.
2394  */
2395  char *newval;
2396  char *opt;
2397 
2398  opt = psql_scan_slash_option(scan_state,
2399  OT_NORMAL, NULL, false);
2400  newval = pg_strdup(opt ? opt : "");
2401  free(opt);
2402 
2403  while ((opt = psql_scan_slash_option(scan_state,
2404  OT_NORMAL, NULL, false)))
2405  {
2406  newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
2407  strcat(newval, opt);
2408  free(opt);
2409  }
2410 
2411  if (!SetVariable(pset.vars, opt0, newval))
2412  success = false;
2413 
2414  free(newval);
2415  }
2416  free(opt0);
2417  }
2418  else
2419  ignore_slash_options(scan_state);
2420 
2422 }
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 2428 of file command.c.

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

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

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

References buf, ClosePager(), count_lines_in_buf(), createPQExpBuffer(), destroyPQExpBuffer(), EditableFunction, EditableView, free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), OT_WHOLE_LINE, output, PageOutput(), pg_log_error, _psqlSettings::popt, print_with_linenumbers(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), _psqlSettings::queryFout, 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 2863 of file command.c.

2864 {
2865  bool success = true;
2866 
2867  if (active_branch)
2868  {
2869  char *opt = psql_scan_slash_option(scan_state,
2870  OT_WHOLE_LINE, NULL, false);
2871 
2872  success = do_shell(opt);
2873  free(opt);
2874  }
2875  else
2876  ignore_slash_whole_line(scan_state);
2877 
2879 }
static bool do_shell(const char *command)
Definition: command.c:5014

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

2886 {
2887  if (active_branch)
2888  {
2889  char *opt0 = psql_scan_slash_option(scan_state,
2890  OT_NORMAL, NULL, false);
2891 
2892  if (!opt0 || strcmp(opt0, "commands") == 0)
2894  else if (strcmp(opt0, "options") == 0)
2896  else if (strcmp(opt0, "variables") == 0)
2898  else
2900 
2901  free(opt0);
2902  }
2903  else
2904  ignore_slash_options(scan_state);
2905 
2906  return PSQL_CMD_SKIP_LINE;
2907 }
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 2559 of file command.c.

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

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

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

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

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

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

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

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_comma