PostgreSQL Source Code  git master
command.c File Reference
#include "postgres_fe.h"
#include "command.h"
#include <ctype.h>
#include <time.h>
#include <pwd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "catalog/pg_class.h"
#include "portability/instr_time.h"
#include "libpq-fe.h"
#include "pqexpbuffer.h"
#include "fe_utils/string_utils.h"
#include "common.h"
#include "copy.h"
#include "crosstabview.h"
#include "describe.h"
#include "help.h"
#include "input.h"
#include "large_obj.h"
#include "mainloop.h"
#include "fe_utils/print.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 PARAMS_ARRAY_SIZE   8
 
#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_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 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 exec_command_gdesc (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_gexec (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_gset (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_help (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_html (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_include (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_if (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static backslashResult exec_command_list (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_lo (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_out (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_print (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_password (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_prompt (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_pset (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_quit (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_reset (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
 
static backslashResult exec_command_s (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_set (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_setenv (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_sf_sv (PsqlScanState scan_state, bool active_branch, const char *cmd, bool is_func)
 
static backslashResult exec_command_t (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_T (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_timing (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_unset (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_write (PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_watch (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static backslashResult exec_command_x (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_z (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_shell_escape (PsqlScanState scan_state, bool active_branch)
 
static backslashResult exec_command_slash_command_help (PsqlScanState scan_state, bool active_branch)
 
static char * read_connect_arg (PsqlScanState scan_state)
 
static PQExpBuffer gather_boolean_expression (PsqlScanState scan_state)
 
static bool is_true_boolean_expression (PsqlScanState scan_state, const char *name)
 
static void ignore_boolean_expression (PsqlScanState scan_state)
 
static void ignore_slash_options (PsqlScanState scan_state)
 
static void ignore_slash_filepipe (PsqlScanState scan_state)
 
static void ignore_slash_whole_line (PsqlScanState scan_state)
 
static bool is_branching_command (const char *cmd)
 
static void save_query_text_state (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static void discard_query_text (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
 
static void 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 *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, const char *header_keyword)
 
static void minimal_error_message (PGresult *res)
 
static void printSSLInfo (void)
 
static bool printPsetInfo (const char *param, struct printQueryOpt *popt)
 
static char * pset_value_string (const char *param, struct printQueryOpt *popt)
 
backslashResult HandleSlashCmds (PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
 
static char * prompt_for_password (const char *username)
 
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)
 
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 4188 of file command.c.

Referenced by do_shell().

◆ PARAMS_ARRAY_SIZE

#define PARAMS_ARRAY_SIZE   8

Referenced by do_connect().

Typedef Documentation

◆ EditableObjectType

Enumeration Type Documentation

◆ EditableObjectType

Enumerator
EditableFunction 
EditableView 

Definition at line 49 of file command.c.

Function Documentation

◆ _align2string()

static const char* _align2string ( enum printFormat  in)
static

Definition at line 3564 of file command.c.

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

Referenced by printPsetInfo(), and pset_value_string().

3565 {
3566  switch (in)
3567  {
3568  case PRINT_NOTHING:
3569  return "nothing";
3570  break;
3571  case PRINT_UNALIGNED:
3572  return "unaligned";
3573  break;
3574  case PRINT_ALIGNED:
3575  return "aligned";
3576  break;
3577  case PRINT_WRAPPED:
3578  return "wrapped";
3579  break;
3580  case PRINT_HTML:
3581  return "html";
3582  break;
3583  case PRINT_ASCIIDOC:
3584  return "asciidoc";
3585  break;
3586  case PRINT_LATEX:
3587  return "latex";
3588  break;
3589  case PRINT_LATEX_LONGTABLE:
3590  return "latex-longtable";
3591  break;
3592  case PRINT_TROFF_MS:
3593  return "troff-ms";
3594  break;
3595  }
3596  return "unknown";
3597 }

◆ _unicode_linestyle2string()

static const char* _unicode_linestyle2string ( int  linestyle)
static

Definition at line 3617 of file command.c.

References UNICODE_LINESTYLE_DOUBLE, and UNICODE_LINESTYLE_SINGLE.

Referenced by printPsetInfo(), and pset_value_string().

3618 {
3619  switch (linestyle)
3620  {
3622  return "single";
3623  break;
3625  return "double";
3626  break;
3627  }
3628  return "unknown";
3629 }

◆ connection_warnings()

void connection_warnings ( bool  in_startup)

Definition at line 3103 of file command.c.

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

Referenced by do_connect(), and main().

3104 {
3105  if (!pset.quiet && !pset.notty)
3106  {
3107  int client_ver = PG_VERSION_NUM;
3108  char cverbuf[32];
3109  char sverbuf[32];
3110 
3111  if (pset.sversion != client_ver)
3112  {
3113  const char *server_version;
3114 
3115  /* Try to get full text form, might include "devel" etc */
3116  server_version = PQparameterStatus(pset.db, "server_version");
3117  /* Otherwise fall back on pset.sversion */
3118  if (!server_version)
3119  {
3121  sverbuf, sizeof(sverbuf));
3122  server_version = sverbuf;
3123  }
3124 
3125  printf(_("%s (%s, server %s)\n"),
3126  pset.progname, PG_VERSION, server_version);
3127  }
3128  /* For version match, only print psql banner on startup. */
3129  else if (in_startup)
3130  printf("%s (%s)\n", pset.progname, PG_VERSION);
3131 
3132  if (pset.sversion / 100 > client_ver / 100)
3133  printf(_("WARNING: %s major version %s, server major version %s.\n"
3134  " Some psql features might not work.\n"),
3135  pset.progname,
3136  formatPGVersionNumber(client_ver, false,
3137  cverbuf, sizeof(cverbuf)),
3139  sverbuf, sizeof(sverbuf)));
3140 
3141 #ifdef WIN32
3142  checkWin32Codepage();
3143 #endif
3144  printSSLInfo();
3145  }
3146 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
const char * PQparameterStatus(const PGconn *conn, const char *paramName)
Definition: fe-connect.c:6071
const char * progname
Definition: settings.h:108
static int server_version
Definition: pg_dumpall.c:82
static void printSSLInfo(void)
Definition: command.c:3155
#define _(x)
Definition: elog.c:84
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182

◆ copy_previous_query()

static void copy_previous_query ( PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 2816 of file command.c.

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

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

2817 {
2818  if (query_buf && query_buf->len == 0)
2819  appendPQExpBufferStr(query_buf, previous_buf->data);
2820 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385

◆ count_lines_in_buf()

static int count_lines_in_buf ( PQExpBuffer  buf)
static

Definition at line 4671 of file command.c.

References PQExpBufferData::data.

Referenced by exec_command_sf_sv().

4672 {
4673  int lineno = 0;
4674  const char *lines = buf->data;
4675 
4676  while (*lines != '\0')
4677  {
4678  lineno++;
4679  /* find start of next line */
4680  lines = strchr(lines, '\n');
4681  if (!lines)
4682  break;
4683  lines++;
4684  }
4685 
4686  return lineno;
4687 }

◆ discard_query_text()

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

Definition at line 2793 of file command.c.

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

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

2795 {
2796  if (query_buf)
2797  {
2798  int new_len = conditional_stack_get_query_len(cstack);
2799 
2800  Assert(new_len >= 0 && new_len <= query_buf->len);
2801  query_buf->len = new_len;
2802  query_buf->data[new_len] = '\0';
2803  }
2804  psql_scan_set_paren_depth(scan_state,
2806 }
void psql_scan_set_paren_depth(PsqlScanState state, int depth)
int conditional_stack_get_query_len(ConditionalStack cstack)
Definition: conditional.c:126
#define Assert(condition)
Definition: c.h:670
int conditional_stack_get_paren_depth(ConditionalStack cstack)
Definition: conditional.c:148

◆ do_connect()

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

Definition at line 2870 of file command.c.

References _, appendConnStrVal(), appendPQExpBuffer(), CONNECTION_OK, connection_warnings(), connstr, _psqlSettings::cur_cmd_interactive, PQExpBufferData::data, _psqlSettings::db, dbname, _psqlSettings::getPassword, initPQExpBuffer(), is_absolute_path, NoticeProcessor(), _psqlSettings::notty, param_is_newly_set(), PARAMS_ARRAY_SIZE, password, pg_free(), pg_malloc(), pg_strdup(), port, PQconnectdbParams(), PQconnectionNeedsPassword(), PQdb(), PQerrorMessage(), PQfinish(), PQhost(), PQpass(), PQport(), PQsetNoticeProcessor(), PQstatus(), PQuser(), _psqlSettings::progname, prompt_for_password(), pset, psql_error(), _psqlSettings::quiet, recognized_connection_string(), SyncVariables(), termPQExpBuffer(), TRI_NO, TRI_YES, user, and values.

Referenced by exec_command_connect().

2872 {
2873  PGconn *o_conn = pset.db,
2874  *n_conn;
2875  char *password = NULL;
2876  bool keep_password;
2877  bool has_connection_string;
2878  bool reuse_previous;
2880 
2881  if (!o_conn && (!dbname || !user || !host || !port))
2882  {
2883  /*
2884  * We don't know the supplied connection parameters and don't want to
2885  * connect to the wrong database by using defaults, so require all
2886  * parameters to be specified.
2887  */
2888  psql_error("All connection parameters must be supplied because no "
2889  "database connection exists\n");
2890  return false;
2891  }
2892 
2893  has_connection_string = dbname ?
2895  switch (reuse_previous_specification)
2896  {
2897  case TRI_YES:
2898  reuse_previous = true;
2899  break;
2900  case TRI_NO:
2901  reuse_previous = false;
2902  break;
2903  default:
2904  reuse_previous = !has_connection_string;
2905  break;
2906  }
2907  /* Silently ignore arguments subsequent to a connection string. */
2908  if (has_connection_string)
2909  {
2910  user = NULL;
2911  host = NULL;
2912  port = NULL;
2913  }
2914 
2915  /* grab missing values from the old connection */
2916  if (!user && reuse_previous)
2917  user = PQuser(o_conn);
2918  if (!host && reuse_previous)
2919  host = PQhost(o_conn);
2920  if (!port && reuse_previous)
2921  port = PQport(o_conn);
2922 
2923  /*
2924  * Any change in the parameters read above makes us discard the password.
2925  * We also discard it if we're to use a conninfo rather than the
2926  * positional syntax.
2927  */
2928  if (has_connection_string)
2929  keep_password = false;
2930  else
2931  keep_password =
2932  (user && PQuser(o_conn) && strcmp(user, PQuser(o_conn)) == 0) &&
2933  (host && PQhost(o_conn) && strcmp(host, PQhost(o_conn)) == 0) &&
2934  (port && PQport(o_conn) && strcmp(port, PQport(o_conn)) == 0);
2935 
2936  /*
2937  * Grab missing dbname from old connection. No password discard if this
2938  * changes: passwords aren't (usually) database-specific.
2939  */
2940  if (!dbname && reuse_previous)
2941  {
2942  initPQExpBuffer(&connstr);
2943  appendPQExpBuffer(&connstr, "dbname=");
2944  appendConnStrVal(&connstr, PQdb(o_conn));
2945  dbname = connstr.data;
2946  /* has_connection_string=true would be a dead store */
2947  }
2948  else
2949  connstr.data = NULL;
2950 
2951  /*
2952  * If the user asked to be prompted for a password, ask for one now. If
2953  * not, use the password from the old connection, provided the username
2954  * etc have not changed. Otherwise, try to connect without a password
2955  * first, and then ask for a password if needed.
2956  *
2957  * XXX: this behavior leads to spurious connection attempts recorded in
2958  * the postmaster's log. But libpq offers no API that would let us obtain
2959  * a password and then continue with the first connection attempt.
2960  */
2961  if (pset.getPassword == TRI_YES)
2962  {
2963  password = prompt_for_password(user);
2964  }
2965  else if (o_conn && keep_password)
2966  {
2967  password = PQpass(o_conn);
2968  if (password && *password)
2969  password = pg_strdup(password);
2970  else
2971  password = NULL;
2972  }
2973 
2974  while (true)
2975  {
2976 #define PARAMS_ARRAY_SIZE 8
2977  const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
2978  const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
2979  int paramnum = -1;
2980 
2981  keywords[++paramnum] = "host";
2982  values[paramnum] = host;
2983  keywords[++paramnum] = "port";
2984  values[paramnum] = port;
2985  keywords[++paramnum] = "user";
2986  values[paramnum] = user;
2987 
2988  /*
2989  * Position in the array matters when the dbname is a connection
2990  * string, because settings in a connection string override earlier
2991  * array entries only. Thus, user= in the connection string always
2992  * takes effect, but client_encoding= often will not.
2993  *
2994  * If you change this code, also change the initial-connection code in
2995  * main(). For no good reason, a connection string password= takes
2996  * precedence in main() but not here.
2997  */
2998  keywords[++paramnum] = "dbname";
2999  values[paramnum] = dbname;
3000  keywords[++paramnum] = "password";
3001  values[paramnum] = password;
3002  keywords[++paramnum] = "fallback_application_name";
3003  values[paramnum] = pset.progname;
3004  keywords[++paramnum] = "client_encoding";
3005  values[paramnum] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
3006 
3007  /* add array terminator */
3008  keywords[++paramnum] = NULL;
3009  values[paramnum] = NULL;
3010 
3011  n_conn = PQconnectdbParams(keywords, values, true);
3012 
3013  pg_free(keywords);
3014  pg_free(values);
3015 
3016  /* We can immediately discard the password -- no longer needed */
3017  if (password)
3018  pg_free(password);
3019 
3020  if (PQstatus(n_conn) == CONNECTION_OK)
3021  break;
3022 
3023  /*
3024  * Connection attempt failed; either retry the connection attempt with
3025  * a new password, or give up.
3026  */
3027  if (!password && PQconnectionNeedsPassword(n_conn) && pset.getPassword != TRI_NO)
3028  {
3029  PQfinish(n_conn);
3030  password = prompt_for_password(user);
3031  continue;
3032  }
3033 
3034  /*
3035  * Failed to connect to the database. In interactive mode, keep the
3036  * previous connection to the DB; in scripting mode, close our
3037  * previous connection as well.
3038  */
3040  {
3041  psql_error("%s", PQerrorMessage(n_conn));
3042 
3043  /* pset.db is left unmodified */
3044  if (o_conn)
3045  psql_error("Previous connection kept\n");
3046  }
3047  else
3048  {
3049  psql_error("\\connect: %s", PQerrorMessage(n_conn));
3050  if (o_conn)
3051  {
3052  PQfinish(o_conn);
3053  pset.db = NULL;
3054  }
3055  }
3056 
3057  PQfinish(n_conn);
3058  if (connstr.data)
3059  termPQExpBuffer(&connstr);
3060  return false;
3061  }
3062  if (connstr.data)
3063  termPQExpBuffer(&connstr);
3064 
3065  /*
3066  * Replace the old connection with the new one, and update
3067  * connection-dependent variables.
3068  */
3069  PQsetNoticeProcessor(n_conn, NoticeProcessor, NULL);
3070  pset.db = n_conn;
3071  SyncVariables();
3072  connection_warnings(false); /* Must be after SyncVariables */
3073 
3074  /* Tell the user about the new connection */
3075  if (!pset.quiet)
3076  {
3077  if (!o_conn ||
3078  param_is_newly_set(PQhost(o_conn), PQhost(pset.db)) ||
3079  param_is_newly_set(PQport(o_conn), PQport(pset.db)))
3080  {
3081  char *host = PQhost(pset.db);
3082 
3083  /* If the host is an absolute path, the connection is via socket */
3084  if (is_absolute_path(host))
3085  printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
3086  PQdb(pset.db), PQuser(pset.db), host, PQport(pset.db));
3087  else
3088  printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
3089  PQdb(pset.db), PQuser(pset.db), host, PQport(pset.db));
3090  }
3091  else
3092  printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
3093  PQdb(pset.db), PQuser(pset.db));
3094  }
3095 
3096  if (o_conn)
3097  PQfinish(o_conn);
3098  return true;
3099 }
static char password[100]
Definition: streamutil.c:45
static bool param_is_newly_set(const char *old_val, const char *new_val)
Definition: command.c:2846
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
PsqlSettings pset
Definition: startup.c:33
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6282
void SyncVariables(void)
Definition: command.c:3210
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
bool recognized_connection_string(const char *connstr)
Definition: common.c:2390
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3629
char * PQport(const PGconn *conn)
Definition: fe-connect.c:6027
#define false
Definition: c.h:255
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:529
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5982
bool cur_cmd_interactive
Definition: settings.h:106
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#define is_absolute_path(filename)
Definition: port.h:86
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:5990
static char * prompt_for_password(const char *username)
Definition: command.c:2828
static int port
Definition: pg_regress.c:90
#define PARAMS_ARRAY_SIZE
void psql_error(const char *fmt,...)
Definition: common.c:221
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:6007
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:242
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5974
const char * progname
Definition: settings.h:108
enum trivalue getPassword
Definition: settings.h:103
char * dbname
Definition: streamutil.c:42
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static Datum values[MAXATTR]
Definition: bootstrap.c:164
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6140
static char * user
Definition: pg_regress.c:93
void connection_warnings(bool in_startup)
Definition: command.c:3103
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6053
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:64

◆ do_edit()

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

Definition at line 3338 of file command.c.

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

Referenced by exec_command_edit(), and exec_command_ef_ev().

3340 {
3341  char fnametmp[MAXPGPATH];
3342  FILE *stream = NULL;
3343  const char *fname;
3344  bool error = false;
3345  int fd;
3346 
3347  struct stat before,
3348  after;
3349 
3350  if (filename_arg)
3351  fname = filename_arg;
3352  else
3353  {
3354  /* make a temp file to edit */
3355 #ifndef WIN32
3356  const char *tmpdir = getenv("TMPDIR");
3357 
3358  if (!tmpdir)
3359  tmpdir = "/tmp";
3360 #else
3361  char tmpdir[MAXPGPATH];
3362  int ret;
3363 
3364  ret = GetTempPath(MAXPGPATH, tmpdir);
3365  if (ret == 0 || ret > MAXPGPATH)
3366  {
3367  psql_error("could not locate temporary directory: %s\n",
3368  !ret ? strerror(errno) : "");
3369  return false;
3370  }
3371 
3372  /*
3373  * No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
3374  * current directory to the supplied path unless we use only
3375  * backslashes, so we do that.
3376  */
3377 #endif
3378 #ifndef WIN32
3379  snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
3380  "/", (int) getpid());
3381 #else
3382  snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
3383  "" /* trailing separator already present */ , (int) getpid());
3384 #endif
3385 
3386  fname = (const char *) fnametmp;
3387 
3388  fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
3389  if (fd != -1)
3390  stream = fdopen(fd, "w");
3391 
3392  if (fd == -1 || !stream)
3393  {
3394  psql_error("could not open temporary file \"%s\": %s\n", fname, strerror(errno));
3395  error = true;
3396  }
3397  else
3398  {
3399  unsigned int ql = query_buf->len;
3400 
3401  if (ql == 0 || query_buf->data[ql - 1] != '\n')
3402  {
3403  appendPQExpBufferChar(query_buf, '\n');
3404  ql++;
3405  }
3406 
3407  if (fwrite(query_buf->data, 1, ql, stream) != ql)
3408  {
3409  psql_error("%s: %s\n", fname, strerror(errno));
3410 
3411  if (fclose(stream) != 0)
3412  psql_error("%s: %s\n", fname, strerror(errno));
3413 
3414  if (remove(fname) != 0)
3415  psql_error("%s: %s\n", fname, strerror(errno));
3416 
3417  error = true;
3418  }
3419  else if (fclose(stream) != 0)
3420  {
3421  psql_error("%s: %s\n", fname, strerror(errno));
3422  if (remove(fname) != 0)
3423  psql_error("%s: %s\n", fname, strerror(errno));
3424  error = true;
3425  }
3426  }
3427  }
3428 
3429  if (!error && stat(fname, &before) != 0)
3430  {
3431  psql_error("%s: %s\n", fname, strerror(errno));
3432  error = true;
3433  }
3434 
3435  /* call editor */
3436  if (!error)
3437  error = !editFile(fname, lineno);
3438 
3439  if (!error && stat(fname, &after) != 0)
3440  {
3441  psql_error("%s: %s\n", fname, strerror(errno));
3442  error = true;
3443  }
3444 
3445  if (!error && before.st_mtime != after.st_mtime)
3446  {
3447  stream = fopen(fname, PG_BINARY_R);
3448  if (!stream)
3449  {
3450  psql_error("%s: %s\n", fname, strerror(errno));
3451  error = true;
3452  }
3453  else
3454  {
3455  /* read file back into query_buf */
3456  char line[1024];
3457 
3458  resetPQExpBuffer(query_buf);
3459  while (fgets(line, sizeof(line), stream) != NULL)
3460  appendPQExpBufferStr(query_buf, line);
3461 
3462  if (ferror(stream))
3463  {
3464  psql_error("%s: %s\n", fname, strerror(errno));
3465  error = true;
3466  }
3467  else if (edited)
3468  {
3469  *edited = true;
3470  }
3471 
3472  fclose(stream);
3473  }
3474  }
3475 
3476  /* remove temp file */
3477  if (!filename_arg)
3478  {
3479  if (remove(fname) == -1)
3480  {
3481  psql_error("%s: %s\n", fname, strerror(errno));
3482  error = true;
3483  }
3484  }
3485 
3486  return !error;
3487 }
static void error(void)
Definition: sql-dyntest.c:147
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PG_BINARY_R
Definition: c.h:1027
static int fd(const char *x, int i)
Definition: preproc-init.c:105
static int before(chr x, chr y)
Definition: regc_locale.c:496
#define MAXPGPATH
static bool editFile(const char *fname, int lineno)
Definition: command.c:3270
#define stat(a, b)
Definition: win32_port.h:266
void psql_error(const char *fmt,...)
Definition: common.c:221
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
const char * strerror(int errnum)
Definition: strerror.c:19
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145

◆ do_pset()

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

Definition at line 3636 of file command.c.

References Assert, printTableOpt::border, printTableOpt::columns, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::fieldSep, printTableOpt::format, free, printTableOpt::line_style, printQueryOpt::nullPrint, printTableOpt::numericLocale, printTableOpt::pager, printTableOpt::pager_min_lines, ParseVariableBool(), pg_asciiformat, pg_asciiformat_old, pg_strcasecmp(), pg_strdup(), pg_strncasecmp(), pg_utf8format, _psqlSettings::popt, PRINT_ALIGNED, PRINT_ASCIIDOC, PRINT_HTML, PRINT_LATEX, PRINT_LATEX_LONGTABLE, PRINT_TROFF_MS, PRINT_UNALIGNED, PRINT_WRAPPED, printPsetInfo(), pset, psql_error(), 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, and printTableOpt::unicode_header_linestyle.

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(), and parse_psql_options().

3637 {
3638  size_t vallen = 0;
3639 
3640  Assert(param != NULL);
3641 
3642  if (value)
3643  vallen = strlen(value);
3644 
3645  /* set format */
3646  if (strcmp(param, "format") == 0)
3647  {
3648  if (!value)
3649  ;
3650  else if (pg_strncasecmp("unaligned", value, vallen) == 0)
3651  popt->topt.format = PRINT_UNALIGNED;
3652  else if (pg_strncasecmp("aligned", value, vallen) == 0)
3653  popt->topt.format = PRINT_ALIGNED;
3654  else if (pg_strncasecmp("wrapped", value, vallen) == 0)
3655  popt->topt.format = PRINT_WRAPPED;
3656  else if (pg_strncasecmp("html", value, vallen) == 0)
3657  popt->topt.format = PRINT_HTML;
3658  else if (pg_strncasecmp("asciidoc", value, vallen) == 0)
3659  popt->topt.format = PRINT_ASCIIDOC;
3660  else if (pg_strncasecmp("latex", value, vallen) == 0)
3661  popt->topt.format = PRINT_LATEX;
3662  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
3664  else if (pg_strncasecmp("troff-ms", value, vallen) == 0)
3665  popt->topt.format = PRINT_TROFF_MS;
3666  else
3667  {
3668  psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, asciidoc, latex, latex-longtable, troff-ms\n");
3669  return false;
3670  }
3671  }
3672 
3673  /* set table line style */
3674  else if (strcmp(param, "linestyle") == 0)
3675  {
3676  if (!value)
3677  ;
3678  else if (pg_strncasecmp("ascii", value, vallen) == 0)
3679  popt->topt.line_style = &pg_asciiformat;
3680  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
3682  else if (pg_strncasecmp("unicode", value, vallen) == 0)
3683  popt->topt.line_style = &pg_utf8format;
3684  else
3685  {
3686  psql_error("\\pset: allowed line styles are ascii, old-ascii, unicode\n");
3687  return false;
3688  }
3689  }
3690 
3691  /* set unicode border line style */
3692  else if (strcmp(param, "unicode_border_linestyle") == 0)
3693  {
3694  if (!value)
3695  ;
3696  else if (set_unicode_line_style(value, vallen,
3698  refresh_utf8format(&(popt->topt));
3699  else
3700  {
3701  psql_error("\\pset: allowed Unicode border line styles are single, double\n");
3702  return false;
3703  }
3704  }
3705 
3706  /* set unicode column line style */
3707  else if (strcmp(param, "unicode_column_linestyle") == 0)
3708  {
3709  if (!value)
3710  ;
3711  else if (set_unicode_line_style(value, vallen,
3713  refresh_utf8format(&(popt->topt));
3714  else
3715  {
3716  psql_error("\\pset: allowed Unicode column line styles are single, double\n");
3717  return false;
3718  }
3719  }
3720 
3721  /* set unicode header line style */
3722  else if (strcmp(param, "unicode_header_linestyle") == 0)
3723  {
3724  if (!value)
3725  ;
3726  else if (set_unicode_line_style(value, vallen,
3728  refresh_utf8format(&(popt->topt));
3729  else
3730  {
3731  psql_error("\\pset: allowed Unicode header line styles are single, double\n");
3732  return false;
3733  }
3734  }
3735 
3736  /* set border style/width */
3737  else if (strcmp(param, "border") == 0)
3738  {
3739  if (value)
3740  popt->topt.border = atoi(value);
3741  }
3742 
3743  /* set expanded/vertical mode */
3744  else if (strcmp(param, "x") == 0 ||
3745  strcmp(param, "expanded") == 0 ||
3746  strcmp(param, "vertical") == 0)
3747  {
3748  if (value && pg_strcasecmp(value, "auto") == 0)
3749  popt->topt.expanded = 2;
3750  else if (value)
3751  {
3752  bool on_off;
3753 
3754  if (ParseVariableBool(value, NULL, &on_off))
3755  popt->topt.expanded = on_off ? 1 : 0;
3756  else
3757  {
3758  PsqlVarEnumError(param, value, "on, off, auto");
3759  return false;
3760  }
3761  }
3762  else
3763  popt->topt.expanded = !popt->topt.expanded;
3764  }
3765 
3766  /* locale-aware numeric output */
3767  else if (strcmp(param, "numericlocale") == 0)
3768  {
3769  if (value)
3770  return ParseVariableBool(value, param, &popt->topt.numericLocale);
3771  else
3772  popt->topt.numericLocale = !popt->topt.numericLocale;
3773  }
3774 
3775  /* null display */
3776  else if (strcmp(param, "null") == 0)
3777  {
3778  if (value)
3779  {
3780  free(popt->nullPrint);
3781  popt->nullPrint = pg_strdup(value);
3782  }
3783  }
3784 
3785  /* field separator for unaligned text */
3786  else if (strcmp(param, "fieldsep") == 0)
3787  {
3788  if (value)
3789  {
3790  free(popt->topt.fieldSep.separator);
3792  popt->topt.fieldSep.separator_zero = false;
3793  }
3794  }
3795 
3796  else if (strcmp(param, "fieldsep_zero") == 0)
3797  {
3798  free(popt->topt.fieldSep.separator);
3799  popt->topt.fieldSep.separator = NULL;
3800  popt->topt.fieldSep.separator_zero = true;
3801  }
3802 
3803  /* record separator for unaligned text */
3804  else if (strcmp(param, "recordsep") == 0)
3805  {
3806  if (value)
3807  {
3808  free(popt->topt.recordSep.separator);
3810  popt->topt.recordSep.separator_zero = false;
3811  }
3812  }
3813 
3814  else if (strcmp(param, "recordsep_zero") == 0)
3815  {
3816  free(popt->topt.recordSep.separator);
3817  popt->topt.recordSep.separator = NULL;
3818  popt->topt.recordSep.separator_zero = true;
3819  }
3820 
3821  /* toggle between full and tuples-only format */
3822  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
3823  {
3824  if (value)
3825  return ParseVariableBool(value, param, &popt->topt.tuples_only);
3826  else
3827  popt->topt.tuples_only = !popt->topt.tuples_only;
3828  }
3829 
3830  /* set title override */
3831  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
3832  {
3833  free(popt->title);
3834  if (!value)
3835  popt->title = NULL;
3836  else
3837  popt->title = pg_strdup(value);
3838  }
3839 
3840  /* set HTML table tag options */
3841  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
3842  {
3843  free(popt->topt.tableAttr);
3844  if (!value)
3845  popt->topt.tableAttr = NULL;
3846  else
3847  popt->topt.tableAttr = pg_strdup(value);
3848  }
3849 
3850  /* toggle use of pager */
3851  else if (strcmp(param, "pager") == 0)
3852  {
3853  if (value && pg_strcasecmp(value, "always") == 0)
3854  popt->topt.pager = 2;
3855  else if (value)
3856  {
3857  bool on_off;
3858 
3859  if (!ParseVariableBool(value, NULL, &on_off))
3860  {
3861  PsqlVarEnumError(param, value, "on, off, always");
3862  return false;
3863  }
3864  popt->topt.pager = on_off ? 1 : 0;
3865  }
3866  else if (popt->topt.pager == 1)
3867  popt->topt.pager = 0;
3868  else
3869  popt->topt.pager = 1;
3870  }
3871 
3872  /* set minimum lines for pager use */
3873  else if (strcmp(param, "pager_min_lines") == 0)
3874  {
3875  if (value)
3876  popt->topt.pager_min_lines = atoi(value);
3877  }
3878 
3879  /* disable "(x rows)" footer */
3880  else if (strcmp(param, "footer") == 0)
3881  {
3882  if (value)
3883  return ParseVariableBool(value, param, &popt->topt.default_footer);
3884  else
3885  popt->topt.default_footer = !popt->topt.default_footer;
3886  }
3887 
3888  /* set border style/width */
3889  else if (strcmp(param, "columns") == 0)
3890  {
3891  if (value)
3892  popt->topt.columns = atoi(value);
3893  }
3894  else
3895  {
3896  psql_error("\\pset: unknown option: %s\n", param);
3897  return false;
3898  }
3899 
3900  if (!quiet)
3901  printPsetInfo(param, &pset.popt);
3902 
3903  return true;
3904 }
char * nullPrint
Definition: print.h:166
PsqlSettings pset
Definition: startup.c:33
const printTextFormat * line_style
Definition: print.h:112
printTextFormat pg_utf8format
Definition: print.c:102
const printTextFormat pg_asciiformat_old
Definition: print.c:80
enum printFormat format
Definition: print.h:98
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
printTableOpt topt
Definition: print.h:165
unicode_linestyle unicode_header_linestyle
Definition: print.h:123
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
bool separator_zero
Definition: print.h:93
unicode_linestyle unicode_border_linestyle
Definition: print.h:121
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
Definition: pgstrcasecmp.c:69
unsigned short int border
Definition: print.h:101
unsigned short int expanded
Definition: print.h:99
static bool printPsetInfo(const char *param, struct printQueryOpt *popt)
Definition: command.c:3908
unicode_linestyle unicode_column_linestyle
Definition: print.h:122
static struct @121 value
bool tuples_only
Definition: print.h:107
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * tableAttr
Definition: print.h:117
void psql_error(const char *fmt,...)
Definition: common.c:221
unsigned short int pager
Definition: print.h:103
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:113
char * title
Definition: print.h:167
int pager_min_lines
Definition: print.h:105
bool default_footer
Definition: print.h:110
#define Assert(condition)
Definition: c.h:670
printQueryOpt popt
Definition: settings.h:91
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:3604
bool numericLocale
Definition: print.h:115
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3428
const printTextFormat pg_asciiformat
Definition: print.c:59
char * separator
Definition: print.h:92
struct separator recordSep
Definition: print.h:114
int columns
Definition: print.h:120

◆ do_shell()

static bool do_shell ( const char *  command)
static

Definition at line 4198 of file command.c.

References DEFAULT_SHELL, free, psprintf(), and psql_error().

Referenced by exec_command_shell_escape().

4199 {
4200  int result;
4201 
4202  if (!command)
4203  {
4204  char *sys;
4205  const char *shellName;
4206 
4207  shellName = getenv("SHELL");
4208 #ifdef WIN32
4209  if (shellName == NULL)
4210  shellName = getenv("COMSPEC");
4211 #endif
4212  if (shellName == NULL)
4213  shellName = DEFAULT_SHELL;
4214 
4215  /* See EDITOR handling comment for an explanation */
4216 #ifndef WIN32
4217  sys = psprintf("exec %s", shellName);
4218 #else
4219  sys = psprintf("\"%s\"", shellName);
4220 #endif
4221  result = system(sys);
4222  free(sys);
4223  }
4224  else
4225  result = system(command);
4226 
4227  if (result == 127 || result == -1)
4228  {
4229  psql_error("\\!: failed\n");
4230  return false;
4231  }
4232  return true;
4233 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
#define DEFAULT_SHELL
Definition: command.c:4188

◆ do_watch()

static bool do_watch ( PQExpBuffer  query_buf,
double  sleep 
)
static

Definition at line 4242 of file command.c.

References _, cancel_pressed, PQExpBufferData::data, i, PQExpBufferData::len, Min, printTableOpt::pager, pg_free(), pg_malloc(), pg_usleep(), _psqlSettings::popt, pset, psql_error(), PSQLexecWatch(), sigint_interrupt_enabled, sigint_interrupt_jmp, snprintf(), printQueryOpt::title, and printQueryOpt::topt.

Referenced by exec_command_watch().

4243 {
4244  long sleep_ms = (long) (sleep * 1000);
4245  printQueryOpt myopt = pset.popt;
4246  const char *strftime_fmt;
4247  const char *user_title;
4248  char *title;
4249  int title_len;
4250  int res = 0;
4251 
4252  if (!query_buf || query_buf->len <= 0)
4253  {
4254  psql_error(_("\\watch cannot be used with an empty query\n"));
4255  return false;
4256  }
4257 
4258  /*
4259  * Choose format for timestamps. We might eventually make this a \pset
4260  * option. In the meantime, using a variable for the format suppresses
4261  * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
4262  */
4263  strftime_fmt = "%c";
4264 
4265  /*
4266  * Set up rendering options, in particular, disable the pager, because
4267  * nobody wants to be prompted while watching the output of 'watch'.
4268  */
4269  myopt.topt.pager = 0;
4270 
4271  /*
4272  * If there's a title in the user configuration, make sure we have room
4273  * for it in the title buffer. Allow 128 bytes for the timestamp plus 128
4274  * bytes for the rest.
4275  */
4276  user_title = myopt.title;
4277  title_len = (user_title ? strlen(user_title) : 0) + 256;
4278  title = pg_malloc(title_len);
4279 
4280  for (;;)
4281  {
4282  time_t timer;
4283  char timebuf[128];
4284  long i;
4285 
4286  /*
4287  * Prepare title for output. Note that we intentionally include a
4288  * newline at the end of the title; this is somewhat historical but it
4289  * makes for reasonably nicely formatted output in simple cases.
4290  */
4291  timer = time(NULL);
4292  strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
4293 
4294  if (user_title)
4295  snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
4296  user_title, timebuf, sleep);
4297  else
4298  snprintf(title, title_len, _("%s (every %gs)\n"),
4299  timebuf, sleep);
4300  myopt.title = title;
4301 
4302  /* Run the query and print out the results */
4303  res = PSQLexecWatch(query_buf->data, &myopt);
4304 
4305  /*
4306  * PSQLexecWatch handles the case where we can no longer repeat the
4307  * query, and returns 0 or -1.
4308  */
4309  if (res <= 0)
4310  break;
4311 
4312  /*
4313  * Set up cancellation of 'watch' via SIGINT. We redo this each time
4314  * through the loop since it's conceivable something inside
4315  * PSQLexecWatch could change sigint_interrupt_jmp.
4316  */
4317  if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
4318  break;
4319 
4320  /*
4321  * Enable 'watch' cancellations and wait a while before running the
4322  * query again. Break the sleep into short intervals (at most 1s)
4323  * since pg_usleep isn't interruptible on some platforms.
4324  */
4325  sigint_interrupt_enabled = true;
4326  i = sleep_ms;
4327  while (i > 0)
4328  {
4329  long s = Min(i, 1000L);
4330 
4331  pg_usleep(s * 1000L);
4332  if (cancel_pressed)
4333  break;
4334  i -= s;
4335  }
4336  sigint_interrupt_enabled = false;
4337  }
4338 
4339  pg_free(title);
4340  return (res >= 0);
4341 }
volatile bool sigint_interrupt_enabled
Definition: common.c:274
PsqlSettings pset
Definition: startup.c:33
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
int PSQLexecWatch(const char *query, const printQueryOpt *opt)
Definition: common.c:745
#define Min(x, y)
Definition: c.h:802
printTableOpt topt
Definition: print.h:165
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
sigjmp_buf sigint_interrupt_jmp
Definition: common.c:276
volatile bool cancel_pressed
Definition: print.c:46
void pg_usleep(long microsec)
Definition: signal.c:53
void psql_error(const char *fmt,...)
Definition: common.c:221
unsigned short int pager
Definition: print.h:103
char * title
Definition: print.h:167
printQueryOpt popt
Definition: settings.h:91
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int i
#define _(x)
Definition: elog.c:84

◆ echo_hidden_command()

static bool echo_hidden_command ( const char *  query)
static

Definition at line 4348 of file command.c.

References _, _psqlSettings::echo_hidden, _psqlSettings::logfile, pset, PSQL_ECHO_HIDDEN_NOEXEC, and PSQL_ECHO_HIDDEN_OFF.

Referenced by get_create_object_cmd(), and lookup_object_oid().

4349 {
4351  {
4352  printf(_("********* QUERY **********\n"
4353  "%s\n"
4354  "**************************\n\n"), query);
4355  fflush(stdout);
4356  if (pset.logfile)
4357  {
4358  fprintf(pset.logfile,
4359  _("********* QUERY **********\n"
4360  "%s\n"
4361  "**************************\n\n"), query);
4362  fflush(pset.logfile);
4363  }
4364 
4366  return false;
4367  }
4368  return true;
4369 }
PsqlSettings pset
Definition: startup.c:33
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:133
FILE * logfile
Definition: settings.h:115
#define _(x)
Definition: elog.c:84

◆ editFile()

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

Definition at line 3270 of file command.c.

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

Referenced by do_edit().

3271 {
3272  const char *editorName;
3273  const char *editor_lineno_arg = NULL;
3274  char *sys;
3275  int result;
3276 
3277  Assert(fname != NULL);
3278 
3279  /* Find an editor to use */
3280  editorName = getenv("PSQL_EDITOR");
3281  if (!editorName)
3282  editorName = getenv("EDITOR");
3283  if (!editorName)
3284  editorName = getenv("VISUAL");
3285  if (!editorName)
3286  editorName = DEFAULT_EDITOR;
3287 
3288  /* Get line number argument, if we need it. */
3289  if (lineno > 0)
3290  {
3291  editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
3292 #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
3293  if (!editor_lineno_arg)
3294  editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
3295 #endif
3296  if (!editor_lineno_arg)
3297  {
3298  psql_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number\n");
3299  return false;
3300  }
3301  }
3302 
3303  /*
3304  * On Unix the EDITOR value should *not* be quoted, since it might include
3305  * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
3306  * if necessary. But this policy is not very workable on Windows, due to
3307  * severe brain damage in their command shell plus the fact that standard
3308  * program paths include spaces.
3309  */
3310 #ifndef WIN32
3311  if (lineno > 0)
3312  sys = psprintf("exec %s %s%d '%s'",
3313  editorName, editor_lineno_arg, lineno, fname);
3314  else
3315  sys = psprintf("exec %s '%s'",
3316  editorName, fname);
3317 #else
3318  if (lineno > 0)
3319  sys = psprintf("\"%s\" %s%d \"%s\"",
3320  editorName, editor_lineno_arg, lineno, fname);
3321  else
3322  sys = psprintf("\"%s\" \"%s\"",
3323  editorName, fname);
3324 #endif
3325  result = system(sys);
3326  if (result == -1)
3327  psql_error("could not start editor \"%s\"\n", editorName);
3328  else if (result == 127)
3329  psql_error("could not start /bin/sh\n");
3330  free(sys);
3331 
3332  return result == 0;
3333 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define DEFAULT_EDITOR
Definition: settings.h:22
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
#define Assert(condition)
Definition: c.h:670
#define DEFAULT_EDITOR_LINENUMBER_ARG
Definition: settings.h:23

◆ exec_command()

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

Definition at line 273 of file command.c.

References conditional_active(), copy_previous_query(), _psqlSettings::cur_cmd_interactive, exec_command_a(), 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_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_strcasecmp(), pset, PSQL_CMD_SEND, PSQL_CMD_UNKNOWN, psql_error(), and status().

Referenced by HandleSlashCmds().

278 {
280  bool active_branch = conditional_active(cstack);
281 
282  /*
283  * In interactive mode, warn when we're ignoring a command within a false
284  * \if-branch. But we continue on, so as to parse and discard the right
285  * amount of parameter text. Each individual backslash command subroutine
286  * is responsible for doing nothing after discarding appropriate
287  * arguments, if !active_branch.
288  */
289  if (pset.cur_cmd_interactive && !active_branch &&
290  !is_branching_command(cmd))
291  {
292  psql_error("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block\n",
293  cmd);
294  }
295 
296  if (strcmp(cmd, "a") == 0)
297  status = exec_command_a(scan_state, active_branch);
298  else if (strcmp(cmd, "C") == 0)
299  status = exec_command_C(scan_state, active_branch);
300  else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
301  status = exec_command_connect(scan_state, active_branch);
302  else if (strcmp(cmd, "cd") == 0)
303  status = exec_command_cd(scan_state, active_branch, cmd);
304  else if (strcmp(cmd, "conninfo") == 0)
305  status = exec_command_conninfo(scan_state, active_branch);
306  else if (pg_strcasecmp(cmd, "copy") == 0)
307  status = exec_command_copy(scan_state, active_branch);
308  else if (strcmp(cmd, "copyright") == 0)
309  status = exec_command_copyright(scan_state, active_branch);
310  else if (strcmp(cmd, "crosstabview") == 0)
311  status = exec_command_crosstabview(scan_state, active_branch);
312  else if (cmd[0] == 'd')
313  status = exec_command_d(scan_state, active_branch, cmd);
314  else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
315  status = exec_command_edit(scan_state, active_branch,
316  query_buf, previous_buf);
317  else if (strcmp(cmd, "ef") == 0)
318  status = exec_command_ef_ev(scan_state, active_branch, query_buf, true);
319  else if (strcmp(cmd, "ev") == 0)
320  status = exec_command_ef_ev(scan_state, active_branch, query_buf, false);
321  else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0)
322  status = exec_command_echo(scan_state, active_branch, cmd);
323  else if (strcmp(cmd, "elif") == 0)
324  status = exec_command_elif(scan_state, cstack, query_buf);
325  else if (strcmp(cmd, "else") == 0)
326  status = exec_command_else(scan_state, cstack, query_buf);
327  else if (strcmp(cmd, "endif") == 0)
328  status = exec_command_endif(scan_state, cstack, query_buf);
329  else if (strcmp(cmd, "encoding") == 0)
330  status = exec_command_encoding(scan_state, active_branch);
331  else if (strcmp(cmd, "errverbose") == 0)
332  status = exec_command_errverbose(scan_state, active_branch);
333  else if (strcmp(cmd, "f") == 0)
334  status = exec_command_f(scan_state, active_branch);
335  else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
336  status = exec_command_g(scan_state, active_branch, cmd);
337  else if (strcmp(cmd, "gdesc") == 0)
338  status = exec_command_gdesc(scan_state, active_branch);
339  else if (strcmp(cmd, "gexec") == 0)
340  status = exec_command_gexec(scan_state, active_branch);
341  else if (strcmp(cmd, "gset") == 0)
342  status = exec_command_gset(scan_state, active_branch);
343  else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
344  status = exec_command_help(scan_state, active_branch);
345  else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
346  status = exec_command_html(scan_state, active_branch);
347  else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
348  strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
349  status = exec_command_include(scan_state, active_branch, cmd);
350  else if (strcmp(cmd, "if") == 0)
351  status = exec_command_if(scan_state, cstack, query_buf);
352  else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
353  strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
354  status = exec_command_list(scan_state, active_branch, cmd);
355  else if (strncmp(cmd, "lo_", 3) == 0)
356  status = exec_command_lo(scan_state, active_branch, cmd);
357  else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
358  status = exec_command_out(scan_state, active_branch);
359  else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
360  status = exec_command_print(scan_state, active_branch,
361  query_buf, previous_buf);
362  else if (strcmp(cmd, "password") == 0)
363  status = exec_command_password(scan_state, active_branch);
364  else if (strcmp(cmd, "prompt") == 0)
365  status = exec_command_prompt(scan_state, active_branch, cmd);
366  else if (strcmp(cmd, "pset") == 0)
367  status = exec_command_pset(scan_state, active_branch);
368  else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
369  status = exec_command_quit(scan_state, active_branch);
370  else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
371  status = exec_command_reset(scan_state, active_branch, query_buf);
372  else if (strcmp(cmd, "s") == 0)
373  status = exec_command_s(scan_state, active_branch);
374  else if (strcmp(cmd, "set") == 0)
375  status = exec_command_set(scan_state, active_branch);
376  else if (strcmp(cmd, "setenv") == 0)
377  status = exec_command_setenv(scan_state, active_branch, cmd);
378  else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
379  status = exec_command_sf_sv(scan_state, active_branch, cmd, true);
380  else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
381  status = exec_command_sf_sv(scan_state, active_branch, cmd, false);
382  else if (strcmp(cmd, "t") == 0)
383  status = exec_command_t(scan_state, active_branch);
384  else if (strcmp(cmd, "T") == 0)
385  status = exec_command_T(scan_state, active_branch);
386  else if (strcmp(cmd, "timing") == 0)
387  status = exec_command_timing(scan_state, active_branch);
388  else if (strcmp(cmd, "unset") == 0)
389  status = exec_command_unset(scan_state, active_branch, cmd);
390  else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
391  status = exec_command_write(scan_state, active_branch, cmd,
392  query_buf, previous_buf);
393  else if (strcmp(cmd, "watch") == 0)
394  status = exec_command_watch(scan_state, active_branch,
395  query_buf, previous_buf);
396  else if (strcmp(cmd, "x") == 0)
397  status = exec_command_x(scan_state, active_branch);
398  else if (strcmp(cmd, "z") == 0)
399  status = exec_command_z(scan_state, active_branch);
400  else if (strcmp(cmd, "!") == 0)
401  status = exec_command_shell_escape(scan_state, active_branch);
402  else if (strcmp(cmd, "?") == 0)
403  status = exec_command_slash_command_help(scan_state, active_branch);
404  else
405  status = PSQL_CMD_UNKNOWN;
406 
407  /*
408  * All the commands that return PSQL_CMD_SEND want to execute previous_buf
409  * if query_buf is empty. For convenience we implement that here, not in
410  * the individual command subroutines.
411  */
412  if (status == PSQL_CMD_SEND)
413  copy_previous_query(query_buf, previous_buf);
414 
415  return status;
416 }
static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1650
static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1104
static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1382
static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2350
static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1545
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:104
static backslashResult exec_command_prompt(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1872
static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch)
Definition: command.c:425
PsqlSettings pset
Definition: startup.c:33
static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1332
static bool is_branching_command(const char *cmd)
Definition: command.c:2757
static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:905
static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1215
static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2575
static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:1778
static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2274
static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1946
static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1185
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
static backslashResult exec_command_password(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1804
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2530
static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1238
static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2469
bool cur_cmd_interactive
Definition: settings.h:106
static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2553
static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2112
static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
Definition: command.c:588
static backslashResult exec_command_ef_ev(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, bool is_func)
Definition: command.c:982
static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1469
static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
Definition: command.c:672
static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch)
Definition: command.c:477
static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1303
static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1610
static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2059
static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1423
static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1286
void psql_error(const char *fmt,...)
Definition: common.c:221
static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2318
static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1147
static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1363
enum _backslashResult backslashResult
static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2032
static void copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2816
static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2380
static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2000
static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2508
static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:530
static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch)
Definition: command.c:660
static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1679
static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2296
static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1755
static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch)
Definition: command.c:638
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:2014
static backslashResult exec_command_gdesc(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1269
static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch)
Definition: command.c:444
static backslashResult exec_command_sf_sv(PsqlScanState scan_state, bool active_branch, const char *cmd, bool is_func)
Definition: command.c:2169
static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:696

◆ exec_command_a()

static backslashResult exec_command_a ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 425 of file command.c.

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

426 {
427  bool success = true;
428 
429  if (active_branch)
430  {
432  success = do_pset("format", "aligned", &pset.popt, pset.quiet);
433  else
434  success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
435  }
436 
437  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
438 }
PsqlSettings pset
Definition: startup.c:33
enum printFormat format
Definition: print.h:98
printTableOpt topt
Definition: print.h:165
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
printQueryOpt popt
Definition: settings.h:91

◆ exec_command_C()

static backslashResult exec_command_C ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 444 of file command.c.

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

445 {
446  bool success = true;
447 
448  if (active_branch)
449  {
450  char *opt = psql_scan_slash_option(scan_state,
451  OT_NORMAL, NULL, true);
452 
453  success = do_pset("title", opt, &pset.popt, pset.quiet);
454  free(opt);
455  }
456  else
457  ignore_slash_options(scan_state);
458 
459  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
460 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710

◆ exec_command_cd()

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

Definition at line 530 of file command.c.

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

Referenced by exec_command().

531 {
532  bool success = true;
533 
534  if (active_branch)
535  {
536  char *opt = psql_scan_slash_option(scan_state,
537  OT_NORMAL, NULL, true);
538  char *dir;
539 
540  if (opt)
541  dir = opt;
542  else
543  {
544 #ifndef WIN32
545  struct passwd *pw;
546  uid_t user_id = geteuid();
547 
548  errno = 0; /* clear errno before call */
549  pw = getpwuid(user_id);
550  if (!pw)
551  {
552  psql_error("could not get home directory for user ID %ld: %s\n",
553  (long) user_id,
554  errno ? strerror(errno) : _("user does not exist"));
555  exit(EXIT_FAILURE);
556  }
557  dir = pw->pw_dir;
558 #else /* WIN32 */
559 
560  /*
561  * On Windows, 'cd' without arguments prints the current
562  * directory, so if someone wants to code this here instead...
563  */
564  dir = "/";
565 #endif /* WIN32 */
566  }
567 
568  if (chdir(dir) == -1)
569  {
570  psql_error("\\%s: could not change directory to \"%s\": %s\n",
571  cmd, dir, strerror(errno));
572  success = false;
573  }
574 
575  if (opt)
576  free(opt);
577  }
578  else
579  ignore_slash_options(scan_state);
580 
581  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
582 }
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
int uid_t
Definition: win32_port.h:241
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
const char * strerror(int errnum)
Definition: strerror.c:19
#define EXIT_FAILURE
Definition: settings.h:152
#define _(x)
Definition: elog.c:84

◆ exec_command_connect()

static backslashResult exec_command_connect ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 477 of file command.c.

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

478 {
479  bool success = true;
480 
481  if (active_branch)
482  {
483  static const char prefix[] = "-reuse-previous=";
484  char *opt1,
485  *opt2,
486  *opt3,
487  *opt4;
488  enum trivalue reuse_previous = TRI_DEFAULT;
489 
490  opt1 = read_connect_arg(scan_state);
491  if (opt1 != NULL && strncmp(opt1, prefix, sizeof(prefix) - 1) == 0)
492  {
493  bool on_off;
494 
495  success = ParseVariableBool(opt1 + sizeof(prefix) - 1,
496  "-reuse-previous",
497  &on_off);
498  if (success)
499  {
500  reuse_previous = on_off ? TRI_YES : TRI_NO;
501  free(opt1);
502  opt1 = read_connect_arg(scan_state);
503  }
504  }
505 
506  if (success) /* give up if reuse_previous was invalid */
507  {
508  opt2 = read_connect_arg(scan_state);
509  opt3 = read_connect_arg(scan_state);
510  opt4 = read_connect_arg(scan_state);
511 
512  success = do_connect(reuse_previous, opt1, opt2, opt3, opt4);
513 
514  free(opt2);
515  free(opt3);
516  free(opt4);
517  }
518  free(opt1);
519  }
520  else
521  ignore_slash_options(scan_state);
522 
523  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
524 }
static char * read_connect_arg(PsqlScanState scan_state)
Definition: command.c:2607
static bool success
Definition: pg_basebackup.c:99
trivalue
Definition: vacuumlo.c:31
#define free(a)
Definition: header.h:65
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
static bool do_connect(enum trivalue reuse_previous_specification, char *dbname, char *user, char *host, char *port)
Definition: command.c:2870

◆ exec_command_conninfo()

static backslashResult exec_command_conninfo ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 588 of file command.c.

References _, _psqlSettings::db, EXIT_FAILURE, is_absolute_path, _PQconninfoOption::keyword, PQconninfo(), PQconninfoFree(), PQdb(), PQhost(), PQport(), PQuser(), printSSLInfo(), pset, PSQL_CMD_SKIP_LINE, psql_error(), and _PQconninfoOption::val.

Referenced by exec_command().

589 {
590  if (active_branch)
591  {
592  char *db = PQdb(pset.db);
593 
594  if (db == NULL)
595  printf(_("You are currently not connected to a database.\n"));
596  else
597  {
598  char *host;
599  PQconninfoOption *connOptions;
601 
602  host = PQhost(pset.db);
603  /* A usable "hostaddr" overrides the basic sense of host. */
604  connOptions = PQconninfo(pset.db);
605  if (connOptions == NULL)
606  {
607  psql_error("out of memory\n");
608  exit(EXIT_FAILURE);
609  }
610  for (option = connOptions; option && option->keyword; option++)
611  if (strcmp(option->keyword, "hostaddr") == 0)
612  {
613  if (option->val != NULL && option->val[0] != '\0')
614  host = option->val;
615  break;
616  }
617 
618  /* If the host is an absolute path, the connection is via socket */
619  if (is_absolute_path(host))
620  printf(_("You are connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
621  db, PQuser(pset.db), host, PQport(pset.db));
622  else
623  printf(_("You are connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
624  db, PQuser(pset.db), host, PQport(pset.db));
625  printSSLInfo();
626 
627  PQconninfoFree(connOptions);
628  }
629  }
630 
631  return PSQL_CMD_SKIP_LINE;
632 }
PGconn * db
Definition: settings.h:82
PsqlSettings pset
Definition: startup.c:33
char * PQport(const PGconn *conn)
Definition: fe-connect.c:6027
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5982
void PQconninfoFree(PQconninfoOption *connOptions)
Definition: fe-connect.c:5956
#define is_absolute_path(filename)
Definition: port.h:86
PQconninfoOption * PQconninfo(PGconn *conn)
Definition: fe-connect.c:5915
void psql_error(const char *fmt,...)
Definition: common.c:221
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:6007
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5974
#define EXIT_FAILURE
Definition: settings.h:152
static void printSSLInfo(void)
Definition: command.c:3155
#define _(x)
Definition: elog.c:84

◆ exec_command_copy()

static backslashResult exec_command_copy ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 638 of file command.c.

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

639 {
640  bool success = true;
641 
642  if (active_branch)
643  {
644  char *opt = psql_scan_slash_option(scan_state,
645  OT_WHOLE_LINE, NULL, false);
646 
647  success = do_copy(opt);
648  free(opt);
649  }
650  else
651  ignore_slash_whole_line(scan_state);
652 
653  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
654 }
bool do_copy(const char *args)
Definition: copy.c:269
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
#define free(a)
Definition: header.h:65
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2744

◆ exec_command_copyright()

static backslashResult exec_command_copyright ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 660 of file command.c.

References print_copyright(), and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

661 {
662  if (active_branch)
663  print_copyright();
664 
665  return PSQL_CMD_SKIP_LINE;
666 }
void print_copyright(void)
Definition: help.c:651

◆ exec_command_crosstabview()

static backslashResult exec_command_crosstabview ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 672 of file command.c.

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

673 {
675 
676  if (active_branch)
677  {
678  int i;
679 
680  for (i = 0; i < lengthof(pset.ctv_args); i++)
681  pset.ctv_args[i] = psql_scan_slash_option(scan_state,
682  OT_NORMAL, NULL, true);
683  pset.crosstab_flag = true;
684  status = PSQL_CMD_SEND;
685  }
686  else
687  ignore_slash_options(scan_state);
688 
689  return status;
690 }
PsqlSettings pset
Definition: startup.c:33
#define lengthof(array)
Definition: c.h:600
char * ctv_args[4]
Definition: settings.h:99
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
enum _backslashResult backslashResult
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
int i
bool crosstab_flag
Definition: settings.h:98
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_d()

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

Definition at line 696 of file command.c.

References describeAccessMethods(), describeAggregates(), describeFunctions(), describeOperators(), describePublications(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_lo_list(), free, ignore_slash_options(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), 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().

697 {
699  bool success = true;
700 
701  if (active_branch)
702  {
703  char *pattern;
704  bool show_verbose,
705  show_system;
706 
707  /* We don't do SQLID reduction on the pattern yet */
708  pattern = psql_scan_slash_option(scan_state,
709  OT_NORMAL, NULL, true);
710 
711  show_verbose = strchr(cmd, '+') ? true : false;
712  show_system = strchr(cmd, 'S') ? true : false;
713 
714  switch (cmd[1])
715  {
716  case '\0':
717  case '+':
718  case 'S':
719  if (pattern)
720  success = describeTableDetails(pattern, show_verbose, show_system);
721  else
722  /* standard listing of interesting things */
723  success = listTables("tvmsE", NULL, show_verbose, show_system);
724  break;
725  case 'A':
726  success = describeAccessMethods(pattern, show_verbose);
727  break;
728  case 'a':
729  success = describeAggregates(pattern, show_verbose, show_system);
730  break;
731  case 'b':
732  success = describeTablespaces(pattern, show_verbose);
733  break;
734  case 'c':
735  success = listConversions(pattern, show_verbose, show_system);
736  break;
737  case 'C':
738  success = listCasts(pattern, show_verbose);
739  break;
740  case 'd':
741  if (strncmp(cmd, "ddp", 3) == 0)
742  success = listDefaultACLs(pattern);
743  else
744  success = objectDescription(pattern, show_system);
745  break;
746  case 'D':
747  success = listDomains(pattern, show_verbose, show_system);
748  break;
749  case 'f': /* function subsystem */
750  switch (cmd[2])
751  {
752  case '\0':
753  case '+':
754  case 'S':
755  case 'a':
756  case 'n':
757  case 't':
758  case 'w':
759  success = describeFunctions(&cmd[2], pattern, show_verbose, show_system);
760  break;
761  default:
762  status = PSQL_CMD_UNKNOWN;
763  break;
764  }
765  break;
766  case 'g':
767  /* no longer distinct from \du */
768  success = describeRoles(pattern, show_verbose, show_system);
769  break;
770  case 'l':
771  success = do_lo_list();
772  break;
773  case 'L':
774  success = listLanguages(pattern, show_verbose, show_system);
775  break;
776  case 'n':
777  success = listSchemas(pattern, show_verbose, show_system);
778  break;
779  case 'o':
780  success = describeOperators(pattern, show_verbose, show_system);
781  break;
782  case 'O':
783  success = listCollations(pattern, show_verbose, show_system);
784  break;
785  case 'p':
786  success = permissionsList(pattern);
787  break;
788  case 'T':
789  success = describeTypes(pattern, show_verbose, show_system);
790  break;
791  case 't':
792  case 'v':
793  case 'm':
794  case 'i':
795  case 's':
796  case 'E':
797  success = listTables(&cmd[1], pattern, show_verbose, show_system);
798  break;
799  case 'r':
800  if (cmd[2] == 'd' && cmd[3] == 's')
801  {
802  char *pattern2 = NULL;
803 
804  if (pattern)
805  pattern2 = psql_scan_slash_option(scan_state,
806  OT_NORMAL, NULL, true);
807  success = listDbRoleSettings(pattern, pattern2);
808 
809  if (pattern2)
810  free(pattern2);
811  }
812  else
813  status = PSQL_CMD_UNKNOWN;
814  break;
815  case 'R':
816  switch (cmd[2])
817  {
818  case 'p':
819  if (show_verbose)
820  success = describePublications(pattern);
821  else
822  success = listPublications(pattern);
823  break;
824  case 's':
825  success = describeSubscriptions(pattern, show_verbose);
826  break;
827  default:
828  status = PSQL_CMD_UNKNOWN;
829  }
830  break;
831  case 'u':
832  success = describeRoles(pattern, show_verbose, show_system);
833  break;
834  case 'F': /* text search subsystem */
835  switch (cmd[2])
836  {
837  case '\0':
838  case '+':
839  success = listTSConfigs(pattern, show_verbose);
840  break;
841  case 'p':
842  success = listTSParsers(pattern, show_verbose);
843  break;
844  case 'd':
845  success = listTSDictionaries(pattern, show_verbose);
846  break;
847  case 't':
848  success = listTSTemplates(pattern, show_verbose);
849  break;
850  default:
851  status = PSQL_CMD_UNKNOWN;
852  break;
853  }
854  break;
855  case 'e': /* SQL/MED subsystem */
856  switch (cmd[2])
857  {
858  case 's':
859  success = listForeignServers(pattern, show_verbose);
860  break;
861  case 'u':
862  success = listUserMappings(pattern, show_verbose);
863  break;
864  case 'w':
865  success = listForeignDataWrappers(pattern, show_verbose);
866  break;
867  case 't':
868  success = listForeignTables(pattern, show_verbose);
869  break;
870  default:
871  status = PSQL_CMD_UNKNOWN;
872  break;
873  }
874  break;
875  case 'x': /* Extensions */
876  if (show_verbose)
877  success = listExtensionContents(pattern);
878  else
879  success = listExtensions(pattern);
880  break;
881  case 'y': /* Event Triggers */
882  success = listEventTriggers(pattern, show_verbose);
883  break;
884  default:
885  status = PSQL_CMD_UNKNOWN;
886  }
887 
888  if (pattern)
889  free(pattern);
890  }
891  else
892  ignore_slash_options(scan_state);
893 
894  if (!success)
895  status = PSQL_CMD_ERROR;
896 
897  return status;
898 }
bool listTSConfigs(const char *pattern, bool verbose)
Definition: describe.c:4459
bool objectDescription(const char *pattern, bool showSystem)
Definition: describe.c:1102
bool listExtensionContents(const char *pattern)
Definition: describe.c:5026
bool listEventTriggers(const char *pattern, bool verbose)
Definition: describe.c:3764
bool listCollations(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3929
bool listLanguages(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3527
bool listDomains(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3602
bool listSchemas(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:4013
bool describeTablespaces(const char *pattern, bool verbose)
Definition: describe.c:208
bool describeSubscriptions(const char *pattern, bool verbose)
Definition: describe.c:5347
#define true
Definition: c.h:251
bool describeAggregates(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:62
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
bool listTables(const char *tabtypes, const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3364
bool describeTableDetails(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:1294
static bool success
Definition: pg_basebackup.c:99
bool listConversions(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3688
bool listForeignTables(const char *pattern, bool verbose)
Definition: describe.c:4891
bool describePublications(const char *pattern)
Definition: describe.c:5206
bool listTSDictionaries(const char *pattern, bool verbose)
Definition: describe.c:4317
bool listPublications(const char *pattern)
Definition: describe.c:5139
bool listForeignServers(const char *pattern, bool verbose)
Definition: describe.c:4748
#define free(a)
Definition: header.h:65
bool describeTypes(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:594
enum _backslashResult backslashResult
bool describeAccessMethods(const char *pattern, bool verbose)
Definition: describe.c:140
bool listForeignDataWrappers(const char *pattern, bool verbose)
Definition: describe.c:4665
bool listUserMappings(const char *pattern, bool verbose)
Definition: describe.c:4830
bool describeFunctions(const char *functypes, const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:303
bool do_lo_list(void)
Definition: large_obj.c:273
bool listExtensions(const char *pattern)
Definition: describe.c:4969
bool describeOperators(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:710
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
bool listCasts(const char *pattern, bool verbose)
Definition: describe.c:3830
bool listDefaultACLs(const char *pattern)
Definition: describe.c:1017
bool permissionsList(const char *pattern)
Definition: describe.c:857
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
bool listDbRoleSettings(const char *pattern, const char *pattern2)
Definition: describe.c:3280
bool listTSTemplates(const char *pattern, bool verbose)
Definition: describe.c:4388
bool describeRoles(const char *pattern, bool verbose, bool showSystem)
Definition: describe.c:3109
bool listTSParsers(const char *pattern, bool verbose)
Definition: describe.c:4070

◆ exec_command_echo()

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

Definition at line 1104 of file command.c.

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

Referenced by exec_command().

1105 {
1106  if (active_branch)
1107  {
1108  char *value;
1109  char quoted;
1110  bool no_newline = false;
1111  bool first = true;
1112  FILE *fout;
1113 
1114  if (strcmp(cmd, "qecho") == 0)
1115  fout = pset.queryFout;
1116  else
1117  fout = stdout;
1118 
1119  while ((value = psql_scan_slash_option(scan_state,
1120  OT_NORMAL, &quoted, false)))
1121  {
1122  if (!quoted && strcmp(value, "-n") == 0)
1123  no_newline = true;
1124  else
1125  {
1126  if (first)
1127  first = false;
1128  else
1129  fputc(' ', fout);
1130  fputs(value, fout);
1131  }
1132  free(value);
1133  }
1134  if (!no_newline)
1135  fputs("\n", fout);
1136  }
1137  else
1138  ignore_slash_options(scan_state);
1139 
1140  return PSQL_CMD_SKIP_LINE;
1141 }
PsqlSettings pset
Definition: startup.c:33
FILE * queryFout
Definition: settings.h:84
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static struct @121 value
#define free(a)
Definition: header.h:65
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710

◆ exec_command_edit()

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

Definition at line 905 of file command.c.

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

Referenced by exec_command().

907 {
909 
910  if (active_branch)
911  {
912  if (!query_buf)
913  {
914  psql_error("no query buffer\n");
915  status = PSQL_CMD_ERROR;
916  }
917  else
918  {
919  char *fname;
920  char *ln = NULL;
921  int lineno = -1;
922 
923  fname = psql_scan_slash_option(scan_state,
924  OT_NORMAL, NULL, true);
925  if (fname)
926  {
927  /* try to get separate lineno arg */
928  ln = psql_scan_slash_option(scan_state,
929  OT_NORMAL, NULL, true);
930  if (ln == NULL)
931  {
932  /* only one arg; maybe it is lineno not fname */
933  if (fname[0] &&
934  strspn(fname, "0123456789") == strlen(fname))
935  {
936  /* all digits, so assume it is lineno */
937  ln = fname;
938  fname = NULL;
939  }
940  }
941  }
942  if (ln)
943  {
944  lineno = atoi(ln);
945  if (lineno < 1)
946  {
947  psql_error("invalid line number: %s\n", ln);
948  status = PSQL_CMD_ERROR;
949  }
950  }
951  if (status != PSQL_CMD_ERROR)
952  {
953  expand_tilde(&fname);
954  if (fname)
955  canonicalize_path(fname);
956 
957  /* If query_buf is empty, recall previous query for editing */
958  copy_previous_query(query_buf, previous_buf);
959 
960  if (do_edit(fname, query_buf, lineno, NULL))
961  status = PSQL_CMD_NEWEDIT;
962  else
963  status = PSQL_CMD_ERROR;
964  }
965  if (fname)
966  free(fname);
967  if (ln)
968  free(ln);
969  }
970  }
971  else
972  ignore_slash_options(scan_state);
973 
974  return status;
975 }
void canonicalize_path(char *path)
Definition: path.c:254
void expand_tilde(char **filename)
Definition: common.c:2303
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
void psql_error(const char *fmt,...)
Definition: common.c:221
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool *edited)
Definition: command.c:3338
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
static void copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2816
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

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

References _, appendPQExpBufferStr(), PQExpBufferData::data, do_edit(), EditableFunction, EditableView, formatPGVersionNumber(), free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), OT_WHOLE_LINE, pset, PSQL_CMD_ERROR, PSQL_CMD_NEWEDIT, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), resetPQExpBuffer(), status(), strip_lineno_from_objdesc(), and _psqlSettings::sversion.

Referenced by exec_command().

984 {
986 
987  if (active_branch)
988  {
989  char *obj_desc = psql_scan_slash_option(scan_state,
991  NULL, true);
992  int lineno = -1;
993 
994  if (pset.sversion < (is_func ? 80400 : 70400))
995  {
996  char sverbuf[32];
997 
999  sverbuf, sizeof(sverbuf));
1000  if (is_func)
1001  psql_error("The server (version %s) does not support editing function source.\n",
1002  sverbuf);
1003  else
1004  psql_error("The server (version %s) does not support editing view definitions.\n",
1005  sverbuf);
1006  status = PSQL_CMD_ERROR;
1007  }
1008  else if (!query_buf)
1009  {
1010  psql_error("no query buffer\n");
1011  status = PSQL_CMD_ERROR;
1012  }
1013  else
1014  {
1015  Oid obj_oid = InvalidOid;
1017 
1018  lineno = strip_lineno_from_objdesc(obj_desc);
1019  if (lineno == 0)
1020  {
1021  /* error already reported */
1022  status = PSQL_CMD_ERROR;
1023  }
1024  else if (!obj_desc)
1025  {
1026  /* set up an empty command to fill in */
1027  resetPQExpBuffer(query_buf);
1028  if (is_func)
1029  appendPQExpBufferStr(query_buf,
1030  "CREATE FUNCTION ( )\n"
1031  " RETURNS \n"
1032  " LANGUAGE \n"
1033  " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
1034  "AS $function$\n"
1035  "\n$function$\n");
1036  else
1037  appendPQExpBufferStr(query_buf,
1038  "CREATE VIEW AS\n"
1039  " SELECT \n"
1040  " -- something...\n");
1041  }
1042  else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
1043  {
1044  /* error already reported */
1045  status = PSQL_CMD_ERROR;
1046  }
1047  else if (!get_create_object_cmd(eot, obj_oid, query_buf))
1048  {
1049  /* error already reported */
1050  status = PSQL_CMD_ERROR;
1051  }
1052  else if (is_func && lineno > 0)
1053  {
1054  /*
1055  * lineno "1" should correspond to the first line of the
1056  * function body. We expect that pg_get_functiondef() will
1057  * emit that on a line beginning with "AS ", and that there
1058  * can be no such line before the real start of the function
1059  * body. Increment lineno by the number of lines before that
1060  * line, so that it becomes relative to the first line of the
1061  * function definition.
1062  */
1063  const char *lines = query_buf->data;
1064 
1065  while (*lines != '\0')
1066  {
1067  if (strncmp(lines, "AS ", 3) == 0)
1068  break;
1069  lineno++;
1070  /* find start of next line */
1071  lines = strchr(lines, '\n');
1072  if (!lines)
1073  break;
1074  lines++;
1075  }
1076  }
1077  }
1078 
1079  if (status != PSQL_CMD_ERROR)
1080  {
1081  bool edited = false;
1082 
1083  if (!do_edit(NULL, query_buf, lineno, &edited))
1084  status = PSQL_CMD_ERROR;
1085  else if (!edited)
1086  puts(_("No changes"));
1087  else
1088  status = PSQL_CMD_NEWEDIT;
1089  }
1090 
1091  if (obj_desc)
1092  free(obj_desc);
1093  }
1094  else
1095  ignore_slash_whole_line(scan_state);
1096 
1097  return status;
1098 }
EditableObjectType
Definition: command.c:49
PsqlSettings pset
Definition: startup.c:33
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:4380
unsigned int Oid
Definition: postgres_ext.h:31
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
void psql_error(const char *fmt,...)
Definition: common.c:221
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool *edited)
Definition: command.c:3338
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
static int strip_lineno_from_objdesc(char *obj)
Definition: command.c:4613
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:4440
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
#define _(x)
Definition: elog.c:84
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2744

◆ exec_command_elif()

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

Definition at line 1469 of file command.c.

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(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), save_query_text_state(), and success.

Referenced by exec_command().

1471 {
1472  bool success = true;
1473 
1474  switch (conditional_stack_peek(cstack))
1475  {
1476  case IFSTATE_TRUE:
1477 
1478  /*
1479  * Just finished active branch of this \if block. Update saved
1480  * state so we will keep whatever data was put in query_buf by the
1481  * active branch.
1482  */
1483  save_query_text_state(scan_state, cstack, query_buf);
1484 
1485  /*
1486  * Discard \elif expression and ignore the rest until \endif.
1487  * Switch state before reading expression to ensure proper lexer
1488  * behavior.
1489  */
1491  ignore_boolean_expression(scan_state);
1492  break;
1493  case IFSTATE_FALSE:
1494 
1495  /*
1496  * Discard any query text added by the just-skipped branch.
1497  */
1498  discard_query_text(scan_state, cstack, query_buf);
1499 
1500  /*
1501  * Have not yet found a true expression in this \if block, so this
1502  * might be the first. We have to change state before examining
1503  * the expression, or the lexer won't do the right thing.
1504  */
1506  if (!is_true_boolean_expression(scan_state, "\\elif expression"))
1508  break;
1509  case IFSTATE_IGNORED:
1510 
1511  /*
1512  * Discard any query text added by the just-skipped branch.
1513  */
1514  discard_query_text(scan_state, cstack, query_buf);
1515 
1516  /*
1517  * Skip expression and move on. Either the \if block already had
1518  * an active section, or whole block is being skipped.
1519  */
1520  ignore_boolean_expression(scan_state);
1521  break;
1522  case IFSTATE_ELSE_TRUE:
1523  case IFSTATE_ELSE_FALSE:
1524  psql_error("\\elif: cannot occur after \\else\n");
1525  success = false;
1526  break;
1527  case IFSTATE_NONE:
1528  /* no \if to elif from */
1529  psql_error("\\elif: no matching \\if\n");
1530  success = false;
1531  break;
1532  }
1533 
1534  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1535 }
static void ignore_boolean_expression(PsqlScanState scan_state)
Definition: command.c:2694
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2773
static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name)
Definition: command.c:2677
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2793
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:70
bool conditional_stack_poke(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:82

◆ exec_command_else()

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

Definition at line 1545 of file command.c.

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

Referenced by exec_command().

1547 {
1548  bool success = true;
1549 
1550  switch (conditional_stack_peek(cstack))
1551  {
1552  case IFSTATE_TRUE:
1553 
1554  /*
1555  * Just finished active branch of this \if block. Update saved
1556  * state so we will keep whatever data was put in query_buf by the
1557  * active branch.
1558  */
1559  save_query_text_state(scan_state, cstack, query_buf);
1560 
1561  /* Now skip the \else branch */
1563  break;
1564  case IFSTATE_FALSE:
1565 
1566  /*
1567  * Discard any query text added by the just-skipped branch.
1568  */
1569  discard_query_text(scan_state, cstack, query_buf);
1570 
1571  /*
1572  * We've not found any true \if or \elif expression, so execute
1573  * the \else branch.
1574  */
1576  break;
1577  case IFSTATE_IGNORED:
1578 
1579  /*
1580  * Discard any query text added by the just-skipped branch.
1581  */
1582  discard_query_text(scan_state, cstack, query_buf);
1583 
1584  /*
1585  * Either we previously processed the active branch of this \if,
1586  * or the whole \if block is being skipped. Either way, skip the
1587  * \else branch.
1588  */
1590  break;
1591  case IFSTATE_ELSE_TRUE:
1592  case IFSTATE_ELSE_FALSE:
1593  psql_error("\\else: cannot occur after \\else\n");
1594  success = false;
1595  break;
1596  case IFSTATE_NONE:
1597  /* no \if to else from */
1598  psql_error("\\else: no matching \\if\n");
1599  success = false;
1600  break;
1601  }
1602 
1603  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1604 }
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2773
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2793
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:70
bool conditional_stack_poke(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:82

◆ exec_command_encoding()

static backslashResult exec_command_encoding ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1147 of file command.c.

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

Referenced by exec_command().

1148 {
1149  if (active_branch)
1150  {
1151  char *encoding = psql_scan_slash_option(scan_state,
1152  OT_NORMAL, NULL, false);
1153 
1154  if (!encoding)
1155  {
1156  /* show encoding */
1158  }
1159  else
1160  {
1161  /* set encoding */
1162  if (PQsetClientEncoding(pset.db, encoding) == -1)
1163  psql_error("%s: invalid encoding name or conversion procedure not found\n", encoding);
1164  else
1165  {
1166  /* save encoding info into psql internal data */
1169  SetVariable(pset.vars, "ENCODING",
1171  }
1172  free(encoding);
1173  }
1174  }
1175  else
1176  ignore_slash_options(scan_state);
1177 
1178  return PSQL_CMD_SKIP_LINE;
1179 }
PGconn * db
Definition: settings.h:82
int encoding
Definition: print.h:118
PsqlSettings pset
Definition: startup.c:33
printTableOpt topt
Definition: print.h:165
int PQclientEncoding(const PGconn *conn)
Definition: fe-connect.c:6166
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
void psql_error(const char *fmt,...)
Definition: common.c:221
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:6174
static char * encoding
Definition: initdb.c:123
#define free(a)
Definition: header.h:65
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
int encoding
Definition: settings.h:83
VariableSpace vars
Definition: settings.h:117

◆ exec_command_endif()

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

Definition at line 1610 of file command.c.

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, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), and success.

Referenced by exec_command().

1612 {
1613  bool success = true;
1614 
1615  switch (conditional_stack_peek(cstack))
1616  {
1617  case IFSTATE_TRUE:
1618  case IFSTATE_ELSE_TRUE:
1619  /* Close the \if block, keeping the query text */
1620  success = conditional_stack_pop(cstack);
1621  Assert(success);
1622  break;
1623  case IFSTATE_FALSE:
1624  case IFSTATE_IGNORED:
1625  case IFSTATE_ELSE_FALSE:
1626 
1627  /*
1628  * Discard any query text added by the just-skipped branch.
1629  */
1630  discard_query_text(scan_state, cstack, query_buf);
1631 
1632  /* Close the \if block */
1633  success = conditional_stack_pop(cstack);
1634  Assert(success);
1635  break;
1636  case IFSTATE_NONE:
1637  /* no \if to end */
1638  psql_error("\\endif: no matching \\if\n");
1639  success = false;
1640  break;
1641  }
1642 
1643  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1644 }
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:55
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2793
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:70
#define Assert(condition)
Definition: c.h:670

◆ exec_command_errverbose()

static backslashResult exec_command_errverbose ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1185 of file command.c.

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

Referenced by exec_command().

1186 {
1187  if (active_branch)
1188  {
1189  if (pset.last_error_result)
1190  {
1191  char *msg;
1192 
1196  if (msg)
1197  {
1198  psql_error("%s", msg);
1199  PQfreemem(msg);
1200  }
1201  else
1202  puts(_("out of memory"));
1203  }
1204  else
1205  puts(_("There is no previous error."));
1206  }
1207 
1208  return PSQL_CMD_SKIP_LINE;
1209 }
PsqlSettings pset
Definition: startup.c:33
char * PQresultVerboseErrorMessage(const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
Definition: fe-exec.c:2671
void psql_error(const char *fmt,...)
Definition: common.c:221
PGresult * last_error_result
Definition: settings.h:89
#define _(x)
Definition: elog.c:84
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251

◆ exec_command_f()

static backslashResult exec_command_f ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1215 of file command.c.

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

1216 {
1217  bool success = true;
1218 
1219  if (active_branch)
1220  {
1221  char *fname = psql_scan_slash_option(scan_state,
1222  OT_NORMAL, NULL, false);
1223 
1224  success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
1225  free(fname);
1226  }
1227  else
1228  ignore_slash_options(scan_state);
1229 
1230  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1231 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710

◆ exec_command_g()

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

Definition at line 1238 of file command.c.

References expand_tilde(), free, _psqlSettings::g_expanded, _psqlSettings::gfname, ignore_slash_filepipe(), OT_FILEPIPE, pg_strdup(), pset, PSQL_CMD_SEND, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and status().

Referenced by exec_command().

1239 {
1241 
1242  if (active_branch)
1243  {
1244  char *fname = psql_scan_slash_option(scan_state,
1245  OT_FILEPIPE, NULL, false);
1246 
1247  if (!fname)
1248  pset.gfname = NULL;
1249  else
1250  {
1251  expand_tilde(&fname);
1252  pset.gfname = pg_strdup(fname);
1253  }
1254  free(fname);
1255  if (strcmp(cmd, "gx") == 0)
1256  pset.g_expanded = true;
1257  status = PSQL_CMD_SEND;
1258  }
1259  else
1260  ignore_slash_filepipe(scan_state);
1261 
1262  return status;
1263 }
PsqlSettings pset
Definition: startup.c:33
void expand_tilde(char **filename)
Definition: common.c:2303
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static void ignore_slash_filepipe(PsqlScanState scan_state)
Definition: command.c:2727
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
bool g_expanded
Definition: settings.h:94
char * gfname
Definition: settings.h:93
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_gdesc()

static backslashResult exec_command_gdesc ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1269 of file command.c.

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

Referenced by exec_command().

1270 {
1272 
1273  if (active_branch)
1274  {
1275  pset.gdesc_flag = true;
1276  status = PSQL_CMD_SEND;
1277  }
1278 
1279  return status;
1280 }
PsqlSettings pset
Definition: startup.c:33
bool gdesc_flag
Definition: settings.h:96
enum _backslashResult backslashResult
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_gexec()

static backslashResult exec_command_gexec ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1286 of file command.c.

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

Referenced by exec_command().

1287 {
1289 
1290  if (active_branch)
1291  {
1292  pset.gexec_flag = true;
1293  status = PSQL_CMD_SEND;
1294  }
1295 
1296  return status;
1297 }
PsqlSettings pset
Definition: startup.c:33
bool gexec_flag
Definition: settings.h:97
enum _backslashResult backslashResult
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_gset()

static backslashResult exec_command_gset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1303 of file command.c.

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

1304 {
1306 
1307  if (active_branch)
1308  {
1309  char *prefix = psql_scan_slash_option(scan_state,
1310  OT_NORMAL, NULL, false);
1311 
1312  if (prefix)
1313  pset.gset_prefix = prefix;
1314  else
1315  {
1316  /* we must set a non-NULL prefix to trigger storing */
1317  pset.gset_prefix = pg_strdup("");
1318  }
1319  /* gset_prefix is freed later */
1320  status = PSQL_CMD_SEND;
1321  }
1322  else
1323  ignore_slash_options(scan_state);
1324 
1325  return status;
1326 }
char * gset_prefix
Definition: settings.h:95
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
enum _backslashResult backslashResult
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_help()

static backslashResult exec_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1332 of file command.c.

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

Referenced by exec_command().

1333 {
1334  if (active_branch)
1335  {
1336  char *opt = psql_scan_slash_option(scan_state,
1337  OT_WHOLE_LINE, NULL, false);
1338  size_t len;
1339 
1340  /* strip any trailing spaces and semicolons */
1341  if (opt)
1342  {
1343  len = strlen(opt);
1344  while (len > 0 &&
1345  (isspace((unsigned char) opt[len - 1])
1346  || opt[len - 1] == ';'))
1347  opt[--len] = '\0';
1348  }
1349 
1350  helpSQL(opt, pset.popt.topt.pager);
1351  free(opt);
1352  }
1353  else
1354  ignore_slash_whole_line(scan_state);
1355 
1356  return PSQL_CMD_SKIP_LINE;
1357 }
PsqlSettings pset
Definition: startup.c:33
printTableOpt topt
Definition: print.h:165
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
unsigned short int pager
Definition: print.h:103
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
void helpSQL(const char *topic, unsigned short int pager)
Definition: help.c:511
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2744

◆ exec_command_html()

static backslashResult exec_command_html ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1363 of file command.c.

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

1364 {
1365  bool success = true;
1366 
1367  if (active_branch)
1368  {
1369  if (pset.popt.topt.format != PRINT_HTML)
1370  success = do_pset("format", "html", &pset.popt, pset.quiet);
1371  else
1372  success = do_pset("format", "aligned", &pset.popt, pset.quiet);
1373  }
1374 
1375  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1376 }
PsqlSettings pset
Definition: startup.c:33
enum printFormat format
Definition: print.h:98
printTableOpt topt
Definition: print.h:165
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
printQueryOpt popt
Definition: settings.h:91

◆ exec_command_if()

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

Definition at line 1423 of file command.c.

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

1425 {
1426  if (conditional_active(cstack))
1427  {
1428  /*
1429  * First, push a new active stack entry; this ensures that the lexer
1430  * will perform variable substitution and backtick evaluation while
1431  * scanning the expression. (That should happen anyway, since we know
1432  * we're in an active outer branch, but let's be sure.)
1433  */
1435 
1436  /* Remember current query state in case we need to restore later */
1437  save_query_text_state(scan_state, cstack, query_buf);
1438 
1439  /*
1440  * Evaluate the expression; if it's false, change to inactive state.
1441  */
1442  if (!is_true_boolean_expression(scan_state, "\\if expression"))
1444  }
1445  else
1446  {
1447  /*
1448  * We're within an inactive outer branch, so this entire \if block
1449  * will be ignored. We don't want to evaluate the expression, so push
1450  * the "ignored" stack state before scanning it.
1451  */
1453 
1454  /* Remember current query state in case we need to restore later */
1455  save_query_text_state(scan_state, cstack, query_buf);
1456 
1457  ignore_boolean_expression(scan_state);
1458  }
1459 
1460  return PSQL_CMD_SKIP_LINE;
1461 }
static void ignore_boolean_expression(PsqlScanState scan_state)
Definition: command.c:2694
bool conditional_active(ConditionalStack cstack)
Definition: conditional.c:104
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2773
static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name)
Definition: command.c:2677
void conditional_stack_push(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:39
bool conditional_stack_poke(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:82

◆ exec_command_include()

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

Definition at line 1382 of file command.c.

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

Referenced by exec_command().

1383 {
1384  bool success = true;
1385 
1386  if (active_branch)
1387  {
1388  char *fname = psql_scan_slash_option(scan_state,
1389  OT_NORMAL, NULL, true);
1390 
1391  if (!fname)
1392  {
1393  psql_error("\\%s: missing required argument\n", cmd);
1394  success = false;
1395  }
1396  else
1397  {
1398  bool include_relative;
1399 
1400  include_relative = (strcmp(cmd, "ir") == 0
1401  || strcmp(cmd, "include_relative") == 0);
1402  expand_tilde(&fname);
1403  success = (process_file(fname, include_relative) == EXIT_SUCCESS);
1404  free(fname);
1405  }
1406  }
1407  else
1408  ignore_slash_options(scan_state);
1409 
1410  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1411 }
#define EXIT_SUCCESS
Definition: settings.h:148
void expand_tilde(char **filename)
Definition: common.c:2303
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
int process_file(char *filename, bool use_relative_path)
Definition: command.c:3502
#define free(a)
Definition: header.h:65
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710

◆ exec_command_list()

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

Definition at line 1650 of file command.c.

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

1651 {
1652  bool success = true;
1653 
1654  if (active_branch)
1655  {
1656  char *pattern;
1657  bool show_verbose;
1658 
1659  pattern = psql_scan_slash_option(scan_state,
1660  OT_NORMAL, NULL, true);
1661 
1662  show_verbose = strchr(cmd, '+') ? true : false;
1663 
1664  success = listAllDbs(pattern, show_verbose);
1665 
1666  if (pattern)
1667  free(pattern);
1668  }
1669  else
1670  ignore_slash_options(scan_state);
1671 
1672  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1673 }
#define true
Definition: c.h:251
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
#define free(a)
Definition: header.h:65
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:787

◆ exec_command_lo()

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

Definition at line 1679 of file command.c.

References do_lo_export(), do_lo_import(), do_lo_list(), do_lo_unlink(), expand_tilde(), free, ignore_slash_options(), OT_NORMAL, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, PSQL_CMD_UNKNOWN, psql_error(), psql_scan_slash_option(), status(), and success.

Referenced by exec_command().

1680 {
1682  bool success = true;
1683 
1684  if (active_branch)
1685  {
1686  char *opt1,
1687  *opt2;
1688 
1689  opt1 = psql_scan_slash_option(scan_state,
1690  OT_NORMAL, NULL, true);
1691  opt2 = psql_scan_slash_option(scan_state,
1692  OT_NORMAL, NULL, true);
1693 
1694  if (strcmp(cmd + 3, "export") == 0)
1695  {
1696  if (!opt2)
1697  {
1698  psql_error("\\%s: missing required argument\n", cmd);
1699  success = false;
1700  }
1701  else
1702  {
1703  expand_tilde(&opt2);
1704  success = do_lo_export(opt1, opt2);
1705  }
1706  }
1707 
1708  else if (strcmp(cmd + 3, "import") == 0)
1709  {
1710  if (!opt1)
1711  {
1712  psql_error("\\%s: missing required argument\n", cmd);
1713  success = false;
1714  }
1715  else
1716  {
1717  expand_tilde(&opt1);
1718  success = do_lo_import(opt1, opt2);
1719  }
1720  }
1721 
1722  else if (strcmp(cmd + 3, "list") == 0)
1723  success = do_lo_list();
1724 
1725  else if (strcmp(cmd + 3, "unlink") == 0)
1726  {
1727  if (!opt1)
1728  {
1729  psql_error("\\%s: missing required argument\n", cmd);
1730  success = false;
1731  }
1732  else
1733  success = do_lo_unlink(opt1);
1734  }
1735 
1736  else
1737  status = PSQL_CMD_UNKNOWN;
1738 
1739  free(opt1);
1740  free(opt2);
1741  }
1742  else
1743  ignore_slash_options(scan_state);
1744 
1745  if (!success)
1746  status = PSQL_CMD_ERROR;
1747 
1748  return status;
1749 }
bool do_lo_export(const char *loid_arg, const char *filename_arg)
Definition: large_obj.c:141
bool do_lo_unlink(const char *loid_arg)
Definition: large_obj.c:238
void expand_tilde(char **filename)
Definition: common.c:2303
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
bool do_lo_import(const char *filename_arg, const char *comment_arg)
Definition: large_obj.c:175
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
bool do_lo_list(void)
Definition: large_obj.c:273
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_out()

static backslashResult exec_command_out ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1755 of file command.c.

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

1756 {
1757  bool success = true;
1758 
1759  if (active_branch)
1760  {
1761  char *fname = psql_scan_slash_option(scan_state,
1762  OT_FILEPIPE, NULL, true);
1763 
1764  expand_tilde(&fname);
1765  success = setQFout(fname);
1766  free(fname);
1767  }
1768  else
1769  ignore_slash_filepipe(scan_state);
1770 
1771  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1772 }
void expand_tilde(char **filename)
Definition: common.c:2303
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
static void ignore_slash_filepipe(PsqlScanState scan_state)
Definition: command.c:2727
bool setQFout(const char *fname)
Definition: common.c:85
#define free(a)
Definition: header.h:65

◆ exec_command_password()

static backslashResult exec_command_password ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1804 of file command.c.

References appendStringLiteralConn(), buf, PQExpBufferData::data, _psqlSettings::db, fmtId(), free, ignore_slash_options(), initPQExpBuffer(), OT_SQLID, PQclear(), PQencryptPasswordConn(), PQerrorMessage(), PQfreemem(), PQuser(), printfPQExpBuffer(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), PSQLexec(), simple_prompt(), success, termPQExpBuffer(), and user.

Referenced by exec_command().

1805 {
1806  bool success = true;
1807 
1808  if (active_branch)
1809  {
1810  char *opt0 = psql_scan_slash_option(scan_state,
1811  OT_SQLID, NULL, true);
1812  char pw1[100];
1813  char pw2[100];
1814 
1815  simple_prompt("Enter new password: ", pw1, sizeof(pw1), false);
1816  simple_prompt("Enter it again: ", pw2, sizeof(pw2), false);
1817 
1818  if (strcmp(pw1, pw2) != 0)
1819  {
1820  psql_error("Passwords didn't match.\n");
1821  success = false;
1822  }
1823  else
1824  {
1825  char *user;
1826  char *encrypted_password;
1827 
1828  if (opt0)
1829  user = opt0;
1830  else
1831  user = PQuser(pset.db);
1832 
1833  encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
1834 
1835  if (!encrypted_password)
1836  {
1837  psql_error("%s", PQerrorMessage(pset.db));
1838  success = false;
1839  }
1840  else
1841  {
1843  PGresult *res;
1844 
1845  initPQExpBuffer(&buf);
1846  printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
1847  fmtId(user));
1848  appendStringLiteralConn(&buf, encrypted_password, pset.db);
1849  res = PSQLexec(buf.data);
1850  termPQExpBuffer(&buf);
1851  if (!res)
1852  success = false;
1853  else
1854  PQclear(res);
1855  PQfreemem(encrypted_password);
1856  }
1857  }
1858 
1859  if (opt0)
1860  free(opt0);
1861  }
1862  else
1863  ignore_slash_options(scan_state);
1864 
1865  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1866 }
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6106
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PsqlSettings pset
Definition: startup.c:33
const char * fmtId(const char *rawid)
Definition: string_utils.c:66
void termPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:128
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:5982
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
static char * buf
Definition: pg_test_fsync.c:67
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
void psql_error(const char *fmt,...)
Definition: common.c:221
void PQclear(PGresult *res)
Definition: fe-exec.c:671
#define free(a)
Definition: header.h:65
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
Definition: fe-auth.c:1175
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
static char * user
Definition: pg_regress.c:93
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
void PQfreemem(void *ptr)
Definition: fe-exec.c:3251
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:690

◆ exec_command_print()

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

Definition at line 1778 of file command.c.

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

Referenced by exec_command().

1780 {
1781  if (active_branch)
1782  {
1783  /*
1784  * We want to print the same thing \g would execute, but not to change
1785  * the query buffer state; so we can't use copy_previous_query().
1786  * Also, beware of possibility that buffer pointers are NULL.
1787  */
1788  if (query_buf && query_buf->len > 0)
1789  puts(query_buf->data);
1790  else if (previous_buf && previous_buf->len > 0)
1791  puts(previous_buf->data);
1792  else if (!pset.quiet)
1793  puts(_("Query buffer is empty."));
1794  fflush(stdout);
1795  }
1796 
1797  return PSQL_CMD_SKIP_LINE;
1798 }
PsqlSettings pset
Definition: startup.c:33
#define _(x)
Definition: elog.c:84

◆ exec_command_prompt()

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

Definition at line 1872 of file command.c.

References free, gets_fromFile(), ignore_slash_options(), _psqlSettings::inputfile, OT_NORMAL, pg_malloc(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), SetVariable(), simple_prompt(), success, and _psqlSettings::vars.

Referenced by exec_command().

1874 {
1875  bool success = true;
1876 
1877  if (active_branch)
1878  {
1879  char *opt,
1880  *prompt_text = NULL;
1881  char *arg1,
1882  *arg2;
1883 
1884  arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
1885  arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
1886 
1887  if (!arg1)
1888  {
1889  psql_error("\\%s: missing required argument\n", cmd);
1890  success = false;
1891  }
1892  else
1893  {
1894  char *result;
1895 
1896  if (arg2)
1897  {
1898  prompt_text = arg1;
1899  opt = arg2;
1900  }
1901  else
1902  opt = arg1;
1903 
1904  if (!pset.inputfile)
1905  {
1906  result = (char *) pg_malloc(4096);
1907  simple_prompt(prompt_text, result, 4096, true);
1908  }
1909  else
1910  {
1911  if (prompt_text)
1912  {
1913  fputs(prompt_text, stdout);
1914  fflush(stdout);
1915  }
1916  result = gets_fromFile(stdin);
1917  if (!result)
1918  {
1919  psql_error("\\%s: could not read value for variable\n",
1920  cmd);
1921  success = false;
1922  }
1923  }
1924 
1925  if (result &&
1926  !SetVariable(pset.vars, opt, result))
1927  success = false;
1928 
1929  if (result)
1930  free(result);
1931  if (prompt_text)
1932  free(prompt_text);
1933  free(opt);
1934  }
1935  }
1936  else
1937  ignore_slash_options(scan_state);
1938 
1939  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1940 }
PsqlSettings pset
Definition: startup.c:33
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
char * inputfile
Definition: settings.h:109
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
char * gets_fromFile(FILE *source)
Definition: input.c:187
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
VariableSpace vars
Definition: settings.h:117

◆ exec_command_pset()

static backslashResult exec_command_pset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1946 of file command.c.

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

Referenced by exec_command().

1947 {
1948  bool success = true;
1949 
1950  if (active_branch)
1951  {
1952  char *opt0 = psql_scan_slash_option(scan_state,
1953  OT_NORMAL, NULL, false);
1954  char *opt1 = psql_scan_slash_option(scan_state,
1955  OT_NORMAL, NULL, false);
1956 
1957  if (!opt0)
1958  {
1959  /* list all variables */
1960 
1961  int i;
1962  static const char *const my_list[] = {
1963  "border", "columns", "expanded", "fieldsep", "fieldsep_zero",
1964  "footer", "format", "linestyle", "null",
1965  "numericlocale", "pager", "pager_min_lines",
1966  "recordsep", "recordsep_zero",
1967  "tableattr", "title", "tuples_only",
1968  "unicode_border_linestyle",
1969  "unicode_column_linestyle",
1970  "unicode_header_linestyle",
1971  NULL
1972  };
1973 
1974  for (i = 0; my_list[i] != NULL; i++)
1975  {
1976  char *val = pset_value_string(my_list[i], &pset.popt);
1977 
1978  printf("%-24s %s\n", my_list[i], val);
1979  free(val);
1980  }
1981 
1982  success = true;
1983  }
1984  else
1985  success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
1986 
1987  free(opt0);
1988  free(opt1);
1989  }
1990  else
1991  ignore_slash_options(scan_state);
1992 
1993  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1994 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
static char * pset_value_string(const char *param, struct printQueryOpt *popt)
Definition: command.c:4129
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
int i
long val
Definition: informix.c:689

◆ exec_command_quit()

static backslashResult exec_command_quit ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2000 of file command.c.

References PSQL_CMD_SKIP_LINE, PSQL_CMD_TERMINATE, and status().

Referenced by exec_command().

2001 {
2003 
2004  if (active_branch)
2005  status = PSQL_CMD_TERMINATE;
2006 
2007  return status;
2008 }
enum _backslashResult backslashResult
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225

◆ exec_command_reset()

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

Definition at line 2014 of file command.c.

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

Referenced by exec_command().

2016 {
2017  if (active_branch)
2018  {
2019  resetPQExpBuffer(query_buf);
2020  psql_scan_reset(scan_state);
2021  if (!pset.quiet)
2022  puts(_("Query buffer reset (cleared)."));
2023  }
2024 
2025  return PSQL_CMD_SKIP_LINE;
2026 }
PsqlSettings pset
Definition: startup.c:33
void psql_scan_reset(PsqlScanState state)
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
#define _(x)
Definition: elog.c:84

◆ exec_command_s()

static backslashResult exec_command_s ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2032 of file command.c.

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

Referenced by exec_command().

2033 {
2034  bool success = true;
2035 
2036  if (active_branch)
2037  {
2038  char *fname = psql_scan_slash_option(scan_state,
2039  OT_NORMAL, NULL, true);
2040 
2041  expand_tilde(&fname);
2042  success = printHistory(fname, pset.popt.topt.pager);
2043  if (success && !pset.quiet && fname)
2044  printf(_("Wrote history to file \"%s\".\n"), fname);
2045  if (!fname)
2046  putchar('\n');
2047  free(fname);
2048  }
2049  else
2050  ignore_slash_options(scan_state);
2051 
2052  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2053 }
PsqlSettings pset
Definition: startup.c:33
printTableOpt topt
Definition: print.h:165
void expand_tilde(char **filename)
Definition: common.c:2303
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
unsigned short int pager
Definition: print.h:103
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
#define _(x)
Definition: elog.c:84
bool printHistory(const char *fname, unsigned short int pager)
Definition: input.c:490

◆ exec_command_set()

static backslashResult exec_command_set ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2059 of file command.c.

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

2060 {
2061  bool success = true;
2062 
2063  if (active_branch)
2064  {
2065  char *opt0 = psql_scan_slash_option(scan_state,
2066  OT_NORMAL, NULL, false);
2067 
2068  if (!opt0)
2069  {
2070  /* list all variables */
2072  success = true;
2073  }
2074  else
2075  {
2076  /*
2077  * Set variable to the concatenation of the arguments.
2078  */
2079  char *newval;
2080  char *opt;
2081 
2082  opt = psql_scan_slash_option(scan_state,
2083  OT_NORMAL, NULL, false);
2084  newval = pg_strdup(opt ? opt : "");
2085  free(opt);
2086 
2087  while ((opt = psql_scan_slash_option(scan_state,
2088  OT_NORMAL, NULL, false)))
2089  {
2090  newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
2091  strcat(newval, opt);
2092  free(opt);
2093  }
2094 
2095  if (!SetVariable(pset.vars, opt0, newval))
2096  success = false;
2097 
2098  free(newval);
2099  }
2100  free(opt0);
2101  }
2102  else
2103  ignore_slash_options(scan_state);
2104 
2105  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2106 }
PsqlSettings pset
Definition: startup.c:33
void PrintVariables(VariableSpace space)
Definition: variables.c:186
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void * pg_realloc(void *ptr, size_t size)
Definition: fe_memutils.c:65
#define free(a)
Definition: header.h:65
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
#define newval
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
VariableSpace vars
Definition: settings.h:117

◆ exec_command_setenv()

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

Definition at line 2112 of file command.c.

References free, ignore_slash_options(), newval, OT_NORMAL, psprintf(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), putenv, success, and unsetenv.

Referenced by exec_command().

2114 {
2115  bool success = true;
2116 
2117  if (active_branch)
2118  {
2119  char *envvar = psql_scan_slash_option(scan_state,
2120  OT_NORMAL, NULL, false);
2121  char *envval = psql_scan_slash_option(scan_state,
2122  OT_NORMAL, NULL, false);
2123 
2124  if (!envvar)
2125  {
2126  psql_error("\\%s: missing required argument\n", cmd);
2127  success = false;
2128  }
2129  else if (strchr(envvar, '=') != NULL)
2130  {
2131  psql_error("\\%s: environment variable name must not contain \"=\"\n",
2132  cmd);
2133  success = false;
2134  }
2135  else if (!envval)
2136  {
2137  /* No argument - unset the environment variable */
2138  unsetenv(envvar);
2139  success = true;
2140  }
2141  else
2142  {
2143  /* Set variable to the value of the next argument */
2144  char *newval;
2145 
2146  newval = psprintf("%s=%s", envvar, envval);
2147  putenv(newval);
2148  success = true;
2149 
2150  /*
2151  * Do not free newval here, it will screw up the environment if
2152  * you do. See putenv man page for details. That means we leak a
2153  * bit of memory here, but not enough to worry about.
2154  */
2155  }
2156  free(envvar);
2157  free(envval);
2158  }
2159  else
2160  ignore_slash_options(scan_state);
2161 
2162  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2163 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
#define newval
#define putenv(x)
Definition: win32_port.h:487
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
#define unsetenv(x)
Definition: win32_port.h:488

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

References buf, ClosePager(), count_lines_in_buf(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), EditableFunction, EditableView, formatPGVersionNumber(), free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), OT_WHOLE_LINE, output(), PageOutput(), _psqlSettings::popt, print_with_linenumbers(), pset, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), _psqlSettings::queryFout, status(), _psqlSettings::sversion, and printQueryOpt::topt.

Referenced by exec_command().

2171 {
2173 
2174  if (active_branch)
2175  {
2176  bool show_linenumbers = (strchr(cmd, '+') != NULL);
2177  PQExpBuffer buf;
2178  char *obj_desc;
2179  Oid obj_oid = InvalidOid;
2181 
2182  buf = createPQExpBuffer();
2183  obj_desc = psql_scan_slash_option(scan_state,
2184  OT_WHOLE_LINE, NULL, true);
2185  if (pset.sversion < (is_func ? 80400 : 70400))
2186  {
2187  char sverbuf[32];
2188 
2190  sverbuf, sizeof(sverbuf));
2191  if (is_func)
2192  psql_error("The server (version %s) does not support showing function source.\n",
2193  sverbuf);
2194  else
2195  psql_error("The server (version %s) does not support showing view definitions.\n",
2196  sverbuf);
2197  status = PSQL_CMD_ERROR;
2198  }
2199  else if (!obj_desc)
2200  {
2201  if (is_func)
2202  psql_error("function name is required\n");
2203  else
2204  psql_error("view name is required\n");
2205  status = PSQL_CMD_ERROR;
2206  }
2207  else if (!lookup_object_oid(eot, obj_desc, &obj_oid))
2208  {
2209  /* error already reported */
2210  status = PSQL_CMD_ERROR;
2211  }
2212  else if (!get_create_object_cmd(eot, obj_oid, buf))
2213  {
2214  /* error already reported */
2215  status = PSQL_CMD_ERROR;
2216  }
2217  else
2218  {
2219  FILE *output;
2220  bool is_pager;
2221 
2222  /* Select output stream: stdout, pager, or file */
2223  if (pset.queryFout == stdout)
2224  {
2225  /* count lines in function to see if pager is needed */
2226  int lineno = count_lines_in_buf(buf);
2227 
2228  output = PageOutput(lineno, &(pset.popt.topt));
2229  is_pager = true;
2230  }
2231  else
2232  {
2233  /* use previously set output file, without pager */
2234  output = pset.queryFout;
2235  is_pager = false;
2236  }
2237 
2238  if (show_linenumbers)
2239  {
2240  /*
2241  * For functions, lineno "1" should correspond to the first
2242  * line of the function body. We expect that
2243  * pg_get_functiondef() will emit that on a line beginning
2244  * with "AS ", and that there can be no such line before the
2245  * real start of the function body.
2246  */
2247  print_with_linenumbers(output, buf->data,
2248  is_func ? "AS " : NULL);
2249  }
2250  else
2251  {
2252  /* just send the definition to output */
2253  fputs(buf->data, output);
2254  }
2255 
2256  if (is_pager)
2257  ClosePager(output);
2258  }
2259 
2260  if (obj_desc)
2261  free(obj_desc);
2262  destroyPQExpBuffer(buf);
2263  }
2264  else
2265  ignore_slash_whole_line(scan_state);
2266 
2267  return status;
2268 }
EditableObjectType
Definition: command.c:49
PsqlSettings pset
Definition: startup.c:33
static void output(uint64 loop_count)
void ClosePager(FILE *pagerpipe)
Definition: print.c:2902
printTableOpt topt
Definition: print.h:165
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:4380
static void print_with_linenumbers(FILE *output, char *lines, const char *header_keyword)
Definition: command.c:4698
FILE * queryFout
Definition: settings.h:84
unsigned int Oid
Definition: postgres_ext.h:31
static int count_lines_in_buf(PQExpBuffer buf)
Definition: command.c:4671
void destroyPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:113
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:2851
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static char * buf
Definition: pg_test_fsync.c:67
void psql_error(const char *fmt,...)
Definition: common.c:221
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
printQueryOpt popt
Definition: settings.h:91
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:4440
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:225
char * formatPGVersionNumber(int version_number, bool include_minor, char *buf, size_t buflen)
Definition: string_utils.c:182
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2744

◆ exec_command_shell_escape()

static backslashResult exec_command_shell_escape ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2553 of file command.c.

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

2554 {
2555  bool success = true;
2556 
2557  if (active_branch)
2558  {
2559  char *opt = psql_scan_slash_option(scan_state,
2560  OT_WHOLE_LINE, NULL, false);
2561 
2562  success = do_shell(opt);
2563  free(opt);
2564  }
2565  else
2566  ignore_slash_whole_line(scan_state);
2567 
2568  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2569 }
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
#define free(a)
Definition: header.h:65
static bool do_shell(const char *command)
Definition: command.c:4198
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2744

◆ exec_command_slash_command_help()

static backslashResult exec_command_slash_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2575 of file command.c.

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

2576 {
2577  if (active_branch)
2578  {
2579  char *opt0 = psql_scan_slash_option(scan_state,
2580  OT_NORMAL, NULL, false);
2581 
2582  if (!opt0 || strcmp(opt0, "commands") == 0)
2584  else if (strcmp(opt0, "options") == 0)
2586  else if (strcmp(opt0, "variables") == 0)
2588  else
2590 
2591  if (opt0)
2592  free(opt0);
2593  }
2594  else
2595  ignore_slash_options(scan_state);
2596 
2597  return PSQL_CMD_SKIP_LINE;
2598 }
static void usage(void)
Definition: pg_standby.c:605
PsqlSettings pset
Definition: startup.c:33
printTableOpt topt
Definition: print.h:165
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
void helpVariables(unsigned short int pager)
Definition: help.c:330
unsigned short int pager
Definition: print.h:103
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
void slashUsage(unsigned short int pager)
Definition: help.c:158

◆ exec_command_t()

static backslashResult exec_command_t ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2274 of file command.c.

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

2275 {
2276  bool success = true;
2277 
2278  if (active_branch)
2279  {
2280  char *opt = psql_scan_slash_option(scan_state,
2281  OT_NORMAL, NULL, true);
2282 
2283  success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
2284  free(opt);
2285  }
2286  else
2287  ignore_slash_options(scan_state);
2288 
2289  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2290 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710

◆ exec_command_T()

static backslashResult exec_command_T ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2296 of file command.c.

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

2297 {
2298  bool success = true;
2299 
2300  if (active_branch)
2301  {
2302  char *value = psql_scan_slash_option(scan_state,
2303  OT_NORMAL, NULL, false);
2304 
2305  success = do_pset("tableattr", value, &pset.popt, pset.quiet);
2306  free(value);
2307  }
2308  else
2309  ignore_slash_options(scan_state);
2310 
2311  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2312 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static struct @121 value
static bool success
Definition: pg_basebackup.c:99
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3636
#define free(a)
Definition: header.h:65
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710

◆ exec_command_timing()

static backslashResult exec_command_timing ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2318 of file command.c.

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

2319 {
2320  bool success = true;
2321 
2322  if (active_branch)
2323  {
2324  char *opt = psql_scan_slash_option(scan_state,
2325  OT_NORMAL, NULL, false);
2326 
2327  if (opt)
2328  success = ParseVariableBool(opt, "\\timing", &pset.timing);
2329  else
2330  pset.timing = !pset.timing;
2331  if (!pset.quiet)
2332  {
2333  if (pset.timing)
2334  puts(_("Timing is on."));
2335  else
2336  puts(_("Timing is off."));
2337  }
2338  free(opt);
2339  }
2340  else
2341  ignore_slash_options(scan_state);
2342 
2343  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2344 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
#define free(a)
Definition: header.h:65
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
#define _(x)
Definition: elog.c:84

◆ exec_command_unset()

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

Definition at line 2350 of file command.c.

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

Referenced by exec_command().

2352 {
2353  bool success = true;
2354 
2355  if (active_branch)
2356  {
2357  char *opt = psql_scan_slash_option(scan_state,
2358  OT_NORMAL, NULL, false);
2359 
2360  if (!opt)
2361  {
2362  psql_error("\\%s: missing required argument\n", cmd);
2363  success = false;
2364  }
2365  else if (!SetVariable(pset.vars, opt, NULL))
2366  success = false;
2367 
2368  free(opt);
2369  }
2370  else
2371  ignore_slash_options(scan_state);
2372 
2373  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2374 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
void psql_error(const char *fmt,...)
Definition: common.c:221
#define free(a)
Definition: header.h:65
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
VariableSpace vars
Definition: settings.h:117

◆ exec_command_watch()

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

Definition at line 2469 of file command.c.

References copy_previous_query(), do_watch(), free, ignore_slash_options(), OT_NORMAL, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_reset(), psql_scan_slash_option(), resetPQExpBuffer(), and success.

Referenced by exec_command().

2471 {
2472  bool success = true;
2473 
2474  if (active_branch)
2475  {
2476  char *opt = psql_scan_slash_option(scan_state,
2477  OT_NORMAL, NULL, true);
2478  double sleep = 2;
2479 
2480  /* Convert optional sleep-length argument */
2481  if (opt)
2482  {
2483  sleep = strtod(opt, NULL);
2484  if (sleep <= 0)
2485  sleep = 1;
2486  free(opt);
2487  }
2488 
2489  /* If query_buf is empty, recall and execute previous query */
2490  copy_previous_query(query_buf, previous_buf);
2491 
2492  success = do_watch(query_buf, sleep);
2493 
2494  /* Reset the query buffer as though for \r */
2495  resetPQExpBuffer(query_buf);
2496  psql_scan_reset(scan_state);
2497  }
2498  else
2499  ignore_slash_options(scan_state);
2500 
2501  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2502 }
static bool do_watch(PQExpBuffer query_buf, double sleep)
Definition: command.c:4242
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:99
void psql_scan_reset(PsqlScanState state)
#define free(a)
Definition: header.h:65
static void copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2816
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2710
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145

◆ exec_command_write()

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

Definition at line 2380 of file command.c.

References canonicalize_path(), PQExpBufferData::data, disable_sigpipe_trap(), expand_tilde(), fd(), free, ignore_slash_filepipe(), PQExpBufferData::len, OT_FILEPIPE, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), restore_sigpipe_trap(), status(), and strerror().

Referenced by exec_command().

2383 {
2385 
2386  if (active_branch)
2387  {
2388  char *fname = psql_scan_slash_option(scan_state,
2389  OT_FILEPIPE, NULL, true);
2390  FILE *fd = NULL;
2391  bool is_pipe = false;
2392 
2393  if (!query_buf)
2394  {
2395  psql_error("no query buffer\n");
2396  status =