PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
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 (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
 
static backslashResult exec_command_ev (PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
 
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_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 (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
static backslashResult exec_command_sv (PsqlScanState scan_state, bool active_branch, const char *cmd)
 
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

#define DEFAULT_SHELL   "/bin/sh"

Definition at line 4297 of file command.c.

Referenced by do_shell().

#define PARAMS_ARRAY_SIZE   8

Referenced by do_connect().

Typedef Documentation

Enumeration Type Documentation

Enumerator
EditableFunction 
EditableView 

Definition at line 49 of file command.c.

Function Documentation

static const char* _align2string ( enum printFormat  in)
static

Definition at line 3673 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().

3674 {
3675  switch (in)
3676  {
3677  case PRINT_NOTHING:
3678  return "nothing";
3679  break;
3680  case PRINT_UNALIGNED:
3681  return "unaligned";
3682  break;
3683  case PRINT_ALIGNED:
3684  return "aligned";
3685  break;
3686  case PRINT_WRAPPED:
3687  return "wrapped";
3688  break;
3689  case PRINT_HTML:
3690  return "html";
3691  break;
3692  case PRINT_ASCIIDOC:
3693  return "asciidoc";
3694  break;
3695  case PRINT_LATEX:
3696  return "latex";
3697  break;
3698  case PRINT_LATEX_LONGTABLE:
3699  return "latex-longtable";
3700  break;
3701  case PRINT_TROFF_MS:
3702  return "troff-ms";
3703  break;
3704  }
3705  return "unknown";
3706 }
static const char* _unicode_linestyle2string ( int  linestyle)
static

Definition at line 3726 of file command.c.

References UNICODE_LINESTYLE_DOUBLE, and UNICODE_LINESTYLE_SINGLE.

Referenced by printPsetInfo(), and pset_value_string().

3727 {
3728  switch (linestyle)
3729  {
3731  return "single";
3732  break;
3734  return "double";
3735  break;
3736  }
3737  return "unknown";
3738 }
void connection_warnings ( bool  in_startup)

Definition at line 3231 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().

3232 {
3233  if (!pset.quiet && !pset.notty)
3234  {
3235  int client_ver = PG_VERSION_NUM;
3236  char cverbuf[32];
3237  char sverbuf[32];
3238 
3239  if (pset.sversion != client_ver)
3240  {
3241  const char *server_version;
3242 
3243  /* Try to get full text form, might include "devel" etc */
3244  server_version = PQparameterStatus(pset.db, "server_version");
3245  /* Otherwise fall back on pset.sversion */
3246  if (!server_version)
3247  {
3249  sverbuf, sizeof(sverbuf));
3250  server_version = sverbuf;
3251  }
3252 
3253  printf(_("%s (%s, server %s)\n"),
3254  pset.progname, PG_VERSION, server_version);
3255  }
3256  /* For version match, only print psql banner on startup. */
3257  else if (in_startup)
3258  printf("%s (%s)\n", pset.progname, PG_VERSION);
3259 
3260  if (pset.sversion / 100 > client_ver / 100)
3261  printf(_("WARNING: %s major version %s, server major version %s.\n"
3262  " Some psql features might not work.\n"),
3263  pset.progname,
3264  formatPGVersionNumber(client_ver, false,
3265  cverbuf, sizeof(cverbuf)),
3267  sverbuf, sizeof(sverbuf)));
3268 
3269 #ifdef WIN32
3270  checkWin32Codepage();
3271 #endif
3272  printSSLInfo();
3273  }
3274 }
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:6062
const char * progname
Definition: settings.h:107
static int server_version
Definition: pg_dumpall.c:82
static void printSSLInfo(void)
Definition: command.c:3283
#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 copy_previous_query ( PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 2944 of file command.c.

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

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

2945 {
2946  if (query_buf && query_buf->len == 0)
2947  appendPQExpBufferStr(query_buf, previous_buf->data);
2948 }
void appendPQExpBufferStr(PQExpBuffer str, const char *data)
Definition: pqexpbuffer.c:385
static int count_lines_in_buf ( PQExpBuffer  buf)
static

Definition at line 4780 of file command.c.

References PQExpBufferData::data.

Referenced by exec_command_sf(), and exec_command_sv().

4781 {
4782  int lineno = 0;
4783  const char *lines = buf->data;
4784 
4785  while (*lines != '\0')
4786  {
4787  lineno++;
4788  /* find start of next line */
4789  lines = strchr(lines, '\n');
4790  if (!lines)
4791  break;
4792  lines++;
4793  }
4794 
4795  return lineno;
4796 }
static void discard_query_text ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 2921 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().

2923 {
2924  if (query_buf)
2925  {
2926  int new_len = conditional_stack_get_query_len(cstack);
2927 
2928  Assert(new_len >= 0 && new_len <= query_buf->len);
2929  query_buf->len = new_len;
2930  query_buf->data[new_len] = '\0';
2931  }
2932  psql_scan_set_paren_depth(scan_state,
2934 }
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:676
int conditional_stack_get_paren_depth(ConditionalStack cstack)
Definition: conditional.c:148
static bool do_connect ( enum trivalue  reuse_previous_specification,
char *  dbname,
char *  user,
char *  host,
char *  port 
)
static

Definition at line 2998 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, NULL, 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().

3000 {
3001  PGconn *o_conn = pset.db,
3002  *n_conn;
3003  char *password = NULL;
3004  bool keep_password;
3005  bool has_connection_string;
3006  bool reuse_previous;
3008 
3009  if (!o_conn && (!dbname || !user || !host || !port))
3010  {
3011  /*
3012  * We don't know the supplied connection parameters and don't want to
3013  * connect to the wrong database by using defaults, so require all
3014  * parameters to be specified.
3015  */
3016  psql_error("All connection parameters must be supplied because no "
3017  "database connection exists\n");
3018  return false;
3019  }
3020 
3021  has_connection_string = dbname ?
3023  switch (reuse_previous_specification)
3024  {
3025  case TRI_YES:
3026  reuse_previous = true;
3027  break;
3028  case TRI_NO:
3029  reuse_previous = false;
3030  break;
3031  default:
3032  reuse_previous = !has_connection_string;
3033  break;
3034  }
3035  /* Silently ignore arguments subsequent to a connection string. */
3036  if (has_connection_string)
3037  {
3038  user = NULL;
3039  host = NULL;
3040  port = NULL;
3041  }
3042 
3043  /* grab missing values from the old connection */
3044  if (!user && reuse_previous)
3045  user = PQuser(o_conn);
3046  if (!host && reuse_previous)
3047  host = PQhost(o_conn);
3048  if (!port && reuse_previous)
3049  port = PQport(o_conn);
3050 
3051  /*
3052  * Any change in the parameters read above makes us discard the password.
3053  * We also discard it if we're to use a conninfo rather than the
3054  * positional syntax.
3055  */
3056  if (has_connection_string)
3057  keep_password = false;
3058  else
3059  keep_password =
3060  (user && PQuser(o_conn) && strcmp(user, PQuser(o_conn)) == 0) &&
3061  (host && PQhost(o_conn) && strcmp(host, PQhost(o_conn)) == 0) &&
3062  (port && PQport(o_conn) && strcmp(port, PQport(o_conn)) == 0);
3063 
3064  /*
3065  * Grab missing dbname from old connection. No password discard if this
3066  * changes: passwords aren't (usually) database-specific.
3067  */
3068  if (!dbname && reuse_previous)
3069  {
3070  initPQExpBuffer(&connstr);
3071  appendPQExpBuffer(&connstr, "dbname=");
3072  appendConnStrVal(&connstr, PQdb(o_conn));
3073  dbname = connstr.data;
3074  /* has_connection_string=true would be a dead store */
3075  }
3076  else
3077  connstr.data = NULL;
3078 
3079  /*
3080  * If the user asked to be prompted for a password, ask for one now. If
3081  * not, use the password from the old connection, provided the username
3082  * etc have not changed. Otherwise, try to connect without a password
3083  * first, and then ask for a password if needed.
3084  *
3085  * XXX: this behavior leads to spurious connection attempts recorded in
3086  * the postmaster's log. But libpq offers no API that would let us obtain
3087  * a password and then continue with the first connection attempt.
3088  */
3089  if (pset.getPassword == TRI_YES)
3090  {
3091  password = prompt_for_password(user);
3092  }
3093  else if (o_conn && keep_password)
3094  {
3095  password = PQpass(o_conn);
3096  if (password && *password)
3097  password = pg_strdup(password);
3098  else
3099  password = NULL;
3100  }
3101 
3102  while (true)
3103  {
3104 #define PARAMS_ARRAY_SIZE 8
3105  const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
3106  const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
3107  int paramnum = -1;
3108 
3109  keywords[++paramnum] = "host";
3110  values[paramnum] = host;
3111  keywords[++paramnum] = "port";
3112  values[paramnum] = port;
3113  keywords[++paramnum] = "user";
3114  values[paramnum] = user;
3115 
3116  /*
3117  * Position in the array matters when the dbname is a connection
3118  * string, because settings in a connection string override earlier
3119  * array entries only. Thus, user= in the connection string always
3120  * takes effect, but client_encoding= often will not.
3121  *
3122  * If you change this code, also change the initial-connection code in
3123  * main(). For no good reason, a connection string password= takes
3124  * precedence in main() but not here.
3125  */
3126  keywords[++paramnum] = "dbname";
3127  values[paramnum] = dbname;
3128  keywords[++paramnum] = "password";
3129  values[paramnum] = password;
3130  keywords[++paramnum] = "fallback_application_name";
3131  values[paramnum] = pset.progname;
3132  keywords[++paramnum] = "client_encoding";
3133  values[paramnum] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
3134 
3135  /* add array terminator */
3136  keywords[++paramnum] = NULL;
3137  values[paramnum] = NULL;
3138 
3139  n_conn = PQconnectdbParams(keywords, values, true);
3140 
3141  pg_free(keywords);
3142  pg_free(values);
3143 
3144  /* We can immediately discard the password -- no longer needed */
3145  if (password)
3146  pg_free(password);
3147 
3148  if (PQstatus(n_conn) == CONNECTION_OK)
3149  break;
3150 
3151  /*
3152  * Connection attempt failed; either retry the connection attempt with
3153  * a new password, or give up.
3154  */
3155  if (!password && PQconnectionNeedsPassword(n_conn) && pset.getPassword != TRI_NO)
3156  {
3157  PQfinish(n_conn);
3158  password = prompt_for_password(user);
3159  continue;
3160  }
3161 
3162  /*
3163  * Failed to connect to the database. In interactive mode, keep the
3164  * previous connection to the DB; in scripting mode, close our
3165  * previous connection as well.
3166  */
3168  {
3169  psql_error("%s", PQerrorMessage(n_conn));
3170 
3171  /* pset.db is left unmodified */
3172  if (o_conn)
3173  psql_error("Previous connection kept\n");
3174  }
3175  else
3176  {
3177  psql_error("\\connect: %s", PQerrorMessage(n_conn));
3178  if (o_conn)
3179  {
3180  PQfinish(o_conn);
3181  pset.db = NULL;
3182  }
3183  }
3184 
3185  PQfinish(n_conn);
3186  if (connstr.data)
3187  termPQExpBuffer(&connstr);
3188  return false;
3189  }
3190  if (connstr.data)
3191  termPQExpBuffer(&connstr);
3192 
3193  /*
3194  * Replace the old connection with the new one, and update
3195  * connection-dependent variables.
3196  */
3198  pset.db = n_conn;
3199  SyncVariables();
3200  connection_warnings(false); /* Must be after SyncVariables */
3201 
3202  /* Tell the user about the new connection */
3203  if (!pset.quiet)
3204  {
3205  if (!o_conn ||
3206  param_is_newly_set(PQhost(o_conn), PQhost(pset.db)) ||
3207  param_is_newly_set(PQport(o_conn), PQport(pset.db)))
3208  {
3209  char *host = PQhost(pset.db);
3210 
3211  /* If the host is an absolute path, the connection is via socket */
3212  if (is_absolute_path(host))
3213  printf(_("You are now connected to database \"%s\" as user \"%s\" via socket in \"%s\" at port \"%s\".\n"),
3214  PQdb(pset.db), PQuser(pset.db), host, PQport(pset.db));
3215  else
3216  printf(_("You are now connected to database \"%s\" as user \"%s\" on host \"%s\" at port \"%s\".\n"),
3217  PQdb(pset.db), PQuser(pset.db), host, PQport(pset.db));
3218  }
3219  else
3220  printf(_("You are now connected to database \"%s\" as user \"%s\".\n"),
3221  PQdb(pset.db), PQuser(pset.db));
3222  }
3223 
3224  if (o_conn)
3225  PQfinish(o_conn);
3226  return true;
3227 }
static char password[100]
Definition: streamutil.c:42
static bool param_is_newly_set(const char *old_val, const char *new_val)
Definition: command.c:2974
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
PsqlSettings pset
Definition: startup.c:33
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6273
void SyncVariables(void)
Definition: command.c:3338
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:2190
void appendConnStrVal(PQExpBuffer buf, const char *str)
Definition: string_utils.c:551
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3630
char * PQport(const PGconn *conn)
Definition: fe-connect.c:6018
#define false
Definition: c.h:210
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:5973
bool cur_cmd_interactive
Definition: settings.h:105
void appendPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:262
#define is_absolute_path(filename)
Definition: port.h:77
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
char * PQpass(const PGconn *conn)
Definition: fe-connect.c:5981
static char * prompt_for_password(const char *username)
Definition: command.c:2956
static int port
Definition: pg_regress.c:89
#define PARAMS_ARRAY_SIZE
void psql_error(const char *fmt,...)
Definition: common.c:220
char * PQhost(const PGconn *conn)
Definition: fe-connect.c:5998
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:241
char * PQdb(const PGconn *conn)
Definition: fe-connect.c:5965
#define NULL
Definition: c.h:229
const char * progname
Definition: settings.h:107
enum trivalue getPassword
Definition: settings.h:102
char * dbname
Definition: streamutil.c:39
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6131
static char * user
Definition: pg_regress.c:92
void connection_warnings(bool in_startup)
Definition: command.c:3231
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6044
#define _(x)
Definition: elog.c:84
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
static char * connstr
Definition: pg_dumpall.c:64
static bool do_edit ( const char *  filename_arg,
PQExpBuffer  query_buf,
int  lineno,
bool edited 
)
static

Definition at line 3447 of file command.c.

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

Referenced by exec_command_edit(), exec_command_ef(), and exec_command_ev().

3449 {
3450  char fnametmp[MAXPGPATH];
3451  FILE *stream = NULL;
3452  const char *fname;
3453  bool error = false;
3454  int fd;
3455 
3456  struct stat before,
3457  after;
3458 
3459  if (filename_arg)
3460  fname = filename_arg;
3461  else
3462  {
3463  /* make a temp file to edit */
3464 #ifndef WIN32
3465  const char *tmpdir = getenv("TMPDIR");
3466 
3467  if (!tmpdir)
3468  tmpdir = "/tmp";
3469 #else
3470  char tmpdir[MAXPGPATH];
3471  int ret;
3472 
3473  ret = GetTempPath(MAXPGPATH, tmpdir);
3474  if (ret == 0 || ret > MAXPGPATH)
3475  {
3476  psql_error("could not locate temporary directory: %s\n",
3477  !ret ? strerror(errno) : "");
3478  return false;
3479  }
3480 
3481  /*
3482  * No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
3483  * current directory to the supplied path unless we use only
3484  * backslashes, so we do that.
3485  */
3486 #endif
3487 #ifndef WIN32
3488  snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
3489  "/", (int) getpid());
3490 #else
3491  snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
3492  "" /* trailing separator already present */ , (int) getpid());
3493 #endif
3494 
3495  fname = (const char *) fnametmp;
3496 
3497  fd = open(fname, O_WRONLY | O_CREAT | O_EXCL, 0600);
3498  if (fd != -1)
3499  stream = fdopen(fd, "w");
3500 
3501  if (fd == -1 || !stream)
3502  {
3503  psql_error("could not open temporary file \"%s\": %s\n", fname, strerror(errno));
3504  error = true;
3505  }
3506  else
3507  {
3508  unsigned int ql = query_buf->len;
3509 
3510  if (ql == 0 || query_buf->data[ql - 1] != '\n')
3511  {
3512  appendPQExpBufferChar(query_buf, '\n');
3513  ql++;
3514  }
3515 
3516  if (fwrite(query_buf->data, 1, ql, stream) != ql)
3517  {
3518  psql_error("%s: %s\n", fname, strerror(errno));
3519 
3520  if (fclose(stream) != 0)
3521  psql_error("%s: %s\n", fname, strerror(errno));
3522 
3523  if (remove(fname) != 0)
3524  psql_error("%s: %s\n", fname, strerror(errno));
3525 
3526  error = true;
3527  }
3528  else if (fclose(stream) != 0)
3529  {
3530  psql_error("%s: %s\n", fname, strerror(errno));
3531  if (remove(fname) != 0)
3532  psql_error("%s: %s\n", fname, strerror(errno));
3533  error = true;
3534  }
3535  }
3536  }
3537 
3538  if (!error && stat(fname, &before) != 0)
3539  {
3540  psql_error("%s: %s\n", fname, strerror(errno));
3541  error = true;
3542  }
3543 
3544  /* call editor */
3545  if (!error)
3546  error = !editFile(fname, lineno);
3547 
3548  if (!error && stat(fname, &after) != 0)
3549  {
3550  psql_error("%s: %s\n", fname, strerror(errno));
3551  error = true;
3552  }
3553 
3554  if (!error && before.st_mtime != after.st_mtime)
3555  {
3556  stream = fopen(fname, PG_BINARY_R);
3557  if (!stream)
3558  {
3559  psql_error("%s: %s\n", fname, strerror(errno));
3560  error = true;
3561  }
3562  else
3563  {
3564  /* read file back into query_buf */
3565  char line[1024];
3566 
3567  resetPQExpBuffer(query_buf);
3568  while (fgets(line, sizeof(line), stream) != NULL)
3569  appendPQExpBufferStr(query_buf, line);
3570 
3571  if (ferror(stream))
3572  {
3573  psql_error("%s: %s\n", fname, strerror(errno));
3574  error = true;
3575  }
3576  else if (edited)
3577  {
3578  *edited = true;
3579  }
3580 
3581  fclose(stream);
3582  }
3583  }
3584 
3585  /* remove temp file */
3586  if (!filename_arg)
3587  {
3588  if (remove(fname) == -1)
3589  {
3590  psql_error("%s: %s\n", fname, strerror(errno));
3591  error = true;
3592  }
3593  }
3594 
3595  return !error;
3596 }
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:1041
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:3379
void psql_error(const char *fmt,...)
Definition: common.c:220
void appendPQExpBufferChar(PQExpBuffer str, char ch)
Definition: pqexpbuffer.c:396
#define NULL
Definition: c.h:229
const char * strerror(int errnum)
Definition: strerror.c:19
void resetPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:145
bool do_pset ( const char *  param,
const char *  value,
printQueryOpt popt,
bool  quiet 
)

Definition at line 3745 of file command.c.

References Assert, printTableOpt::border, printTableOpt::columns, printTableOpt::default_footer, printTableOpt::expanded, printTableOpt::fieldSep, printTableOpt::format, free, printTableOpt::line_style, NULL, 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().

3746 {
3747  size_t vallen = 0;
3748 
3749  Assert(param != NULL);
3750 
3751  if (value)
3752  vallen = strlen(value);
3753 
3754  /* set format */
3755  if (strcmp(param, "format") == 0)
3756  {
3757  if (!value)
3758  ;
3759  else if (pg_strncasecmp("unaligned", value, vallen) == 0)
3760  popt->topt.format = PRINT_UNALIGNED;
3761  else if (pg_strncasecmp("aligned", value, vallen) == 0)
3762  popt->topt.format = PRINT_ALIGNED;
3763  else if (pg_strncasecmp("wrapped", value, vallen) == 0)
3764  popt->topt.format = PRINT_WRAPPED;
3765  else if (pg_strncasecmp("html", value, vallen) == 0)
3766  popt->topt.format = PRINT_HTML;
3767  else if (pg_strncasecmp("asciidoc", value, vallen) == 0)
3768  popt->topt.format = PRINT_ASCIIDOC;
3769  else if (pg_strncasecmp("latex", value, vallen) == 0)
3770  popt->topt.format = PRINT_LATEX;
3771  else if (pg_strncasecmp("latex-longtable", value, vallen) == 0)
3773  else if (pg_strncasecmp("troff-ms", value, vallen) == 0)
3774  popt->topt.format = PRINT_TROFF_MS;
3775  else
3776  {
3777  psql_error("\\pset: allowed formats are unaligned, aligned, wrapped, html, asciidoc, latex, latex-longtable, troff-ms\n");
3778  return false;
3779  }
3780  }
3781 
3782  /* set table line style */
3783  else if (strcmp(param, "linestyle") == 0)
3784  {
3785  if (!value)
3786  ;
3787  else if (pg_strncasecmp("ascii", value, vallen) == 0)
3788  popt->topt.line_style = &pg_asciiformat;
3789  else if (pg_strncasecmp("old-ascii", value, vallen) == 0)
3791  else if (pg_strncasecmp("unicode", value, vallen) == 0)
3792  popt->topt.line_style = &pg_utf8format;
3793  else
3794  {
3795  psql_error("\\pset: allowed line styles are ascii, old-ascii, unicode\n");
3796  return false;
3797  }
3798  }
3799 
3800  /* set unicode border line style */
3801  else if (strcmp(param, "unicode_border_linestyle") == 0)
3802  {
3803  if (!value)
3804  ;
3805  else if (set_unicode_line_style(value, vallen,
3807  refresh_utf8format(&(popt->topt));
3808  else
3809  {
3810  psql_error("\\pset: allowed Unicode border line styles are single, double\n");
3811  return false;
3812  }
3813  }
3814 
3815  /* set unicode column line style */
3816  else if (strcmp(param, "unicode_column_linestyle") == 0)
3817  {
3818  if (!value)
3819  ;
3820  else if (set_unicode_line_style(value, vallen,
3822  refresh_utf8format(&(popt->topt));
3823  else
3824  {
3825  psql_error("\\pset: allowed Unicode column line styles are single, double\n");
3826  return false;
3827  }
3828  }
3829 
3830  /* set unicode header line style */
3831  else if (strcmp(param, "unicode_header_linestyle") == 0)
3832  {
3833  if (!value)
3834  ;
3835  else if (set_unicode_line_style(value, vallen,
3837  refresh_utf8format(&(popt->topt));
3838  else
3839  {
3840  psql_error("\\pset: allowed Unicode header line styles are single, double\n");
3841  return false;
3842  }
3843  }
3844 
3845  /* set border style/width */
3846  else if (strcmp(param, "border") == 0)
3847  {
3848  if (value)
3849  popt->topt.border = atoi(value);
3850  }
3851 
3852  /* set expanded/vertical mode */
3853  else if (strcmp(param, "x") == 0 ||
3854  strcmp(param, "expanded") == 0 ||
3855  strcmp(param, "vertical") == 0)
3856  {
3857  if (value && pg_strcasecmp(value, "auto") == 0)
3858  popt->topt.expanded = 2;
3859  else if (value)
3860  {
3861  bool on_off;
3862 
3863  if (ParseVariableBool(value, NULL, &on_off))
3864  popt->topt.expanded = on_off ? 1 : 0;
3865  else
3866  {
3867  PsqlVarEnumError(param, value, "on, off, auto");
3868  return false;
3869  }
3870  }
3871  else
3872  popt->topt.expanded = !popt->topt.expanded;
3873  }
3874 
3875  /* locale-aware numeric output */
3876  else if (strcmp(param, "numericlocale") == 0)
3877  {
3878  if (value)
3879  return ParseVariableBool(value, param, &popt->topt.numericLocale);
3880  else
3881  popt->topt.numericLocale = !popt->topt.numericLocale;
3882  }
3883 
3884  /* null display */
3885  else if (strcmp(param, "null") == 0)
3886  {
3887  if (value)
3888  {
3889  free(popt->nullPrint);
3890  popt->nullPrint = pg_strdup(value);
3891  }
3892  }
3893 
3894  /* field separator for unaligned text */
3895  else if (strcmp(param, "fieldsep") == 0)
3896  {
3897  if (value)
3898  {
3899  free(popt->topt.fieldSep.separator);
3901  popt->topt.fieldSep.separator_zero = false;
3902  }
3903  }
3904 
3905  else if (strcmp(param, "fieldsep_zero") == 0)
3906  {
3907  free(popt->topt.fieldSep.separator);
3908  popt->topt.fieldSep.separator = NULL;
3909  popt->topt.fieldSep.separator_zero = true;
3910  }
3911 
3912  /* record separator for unaligned text */
3913  else if (strcmp(param, "recordsep") == 0)
3914  {
3915  if (value)
3916  {
3917  free(popt->topt.recordSep.separator);
3919  popt->topt.recordSep.separator_zero = false;
3920  }
3921  }
3922 
3923  else if (strcmp(param, "recordsep_zero") == 0)
3924  {
3925  free(popt->topt.recordSep.separator);
3926  popt->topt.recordSep.separator = NULL;
3927  popt->topt.recordSep.separator_zero = true;
3928  }
3929 
3930  /* toggle between full and tuples-only format */
3931  else if (strcmp(param, "t") == 0 || strcmp(param, "tuples_only") == 0)
3932  {
3933  if (value)
3934  return ParseVariableBool(value, param, &popt->topt.tuples_only);
3935  else
3936  popt->topt.tuples_only = !popt->topt.tuples_only;
3937  }
3938 
3939  /* set title override */
3940  else if (strcmp(param, "C") == 0 || strcmp(param, "title") == 0)
3941  {
3942  free(popt->title);
3943  if (!value)
3944  popt->title = NULL;
3945  else
3946  popt->title = pg_strdup(value);
3947  }
3948 
3949  /* set HTML table tag options */
3950  else if (strcmp(param, "T") == 0 || strcmp(param, "tableattr") == 0)
3951  {
3952  free(popt->topt.tableAttr);
3953  if (!value)
3954  popt->topt.tableAttr = NULL;
3955  else
3956  popt->topt.tableAttr = pg_strdup(value);
3957  }
3958 
3959  /* toggle use of pager */
3960  else if (strcmp(param, "pager") == 0)
3961  {
3962  if (value && pg_strcasecmp(value, "always") == 0)
3963  popt->topt.pager = 2;
3964  else if (value)
3965  {
3966  bool on_off;
3967 
3968  if (!ParseVariableBool(value, NULL, &on_off))
3969  {
3970  PsqlVarEnumError(param, value, "on, off, always");
3971  return false;
3972  }
3973  popt->topt.pager = on_off ? 1 : 0;
3974  }
3975  else if (popt->topt.pager == 1)
3976  popt->topt.pager = 0;
3977  else
3978  popt->topt.pager = 1;
3979  }
3980 
3981  /* set minimum lines for pager use */
3982  else if (strcmp(param, "pager_min_lines") == 0)
3983  {
3984  if (value)
3985  popt->topt.pager_min_lines = atoi(value);
3986  }
3987 
3988  /* disable "(x rows)" footer */
3989  else if (strcmp(param, "footer") == 0)
3990  {
3991  if (value)
3992  return ParseVariableBool(value, param, &popt->topt.default_footer);
3993  else
3994  popt->topt.default_footer = !popt->topt.default_footer;
3995  }
3996 
3997  /* set border style/width */
3998  else if (strcmp(param, "columns") == 0)
3999  {
4000  if (value)
4001  popt->topt.columns = atoi(value);
4002  }
4003  else
4004  {
4005  psql_error("\\pset: unknown option: %s\n", param);
4006  return false;
4007  }
4008 
4009  if (!quiet)
4010  printPsetInfo(param, &pset.popt);
4011 
4012  return true;
4013 }
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:4017
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:220
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 NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
printQueryOpt popt
Definition: settings.h:91
static bool set_unicode_line_style(const char *value, size_t vallen, unicode_linestyle *linestyle)
Definition: command.c:3713
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:3426
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
static bool do_shell ( const char *  command)
static

Definition at line 4307 of file command.c.

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

Referenced by exec_command_shell_escape().

4308 {
4309  int result;
4310 
4311  if (!command)
4312  {
4313  char *sys;
4314  const char *shellName;
4315 
4316  shellName = getenv("SHELL");
4317 #ifdef WIN32
4318  if (shellName == NULL)
4319  shellName = getenv("COMSPEC");
4320 #endif
4321  if (shellName == NULL)
4322  shellName = DEFAULT_SHELL;
4323 
4324  /* See EDITOR handling comment for an explanation */
4325 #ifndef WIN32
4326  sys = psprintf("exec %s", shellName);
4327 #else
4328  sys = psprintf("\"%s\"", shellName);
4329 #endif
4330  result = system(sys);
4331  free(sys);
4332  }
4333  else
4334  result = system(command);
4335 
4336  if (result == 127 || result == -1)
4337  {
4338  psql_error("\\!: failed\n");
4339  return false;
4340  }
4341  return true;
4342 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
return result
Definition: formatting.c:1633
void psql_error(const char *fmt,...)
Definition: common.c:220
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define DEFAULT_SHELL
Definition: command.c:4297
static bool do_watch ( PQExpBuffer  query_buf,
double  sleep 
)
static

Definition at line 4351 of file command.c.

References _, cancel_pressed, PQExpBufferData::data, i, PQExpBufferData::len, Min, NULL, 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().

4352 {
4353  long sleep_ms = (long) (sleep * 1000);
4354  printQueryOpt myopt = pset.popt;
4355  const char *strftime_fmt;
4356  const char *user_title;
4357  char *title;
4358  int title_len;
4359  int res = 0;
4360 
4361  if (!query_buf || query_buf->len <= 0)
4362  {
4363  psql_error(_("\\watch cannot be used with an empty query\n"));
4364  return false;
4365  }
4366 
4367  /*
4368  * Choose format for timestamps. We might eventually make this a \pset
4369  * option. In the meantime, using a variable for the format suppresses
4370  * overly-anal-retentive gcc warnings about %c being Y2K sensitive.
4371  */
4372  strftime_fmt = "%c";
4373 
4374  /*
4375  * Set up rendering options, in particular, disable the pager, because
4376  * nobody wants to be prompted while watching the output of 'watch'.
4377  */
4378  myopt.topt.pager = 0;
4379 
4380  /*
4381  * If there's a title in the user configuration, make sure we have room
4382  * for it in the title buffer. Allow 128 bytes for the timestamp plus 128
4383  * bytes for the rest.
4384  */
4385  user_title = myopt.title;
4386  title_len = (user_title ? strlen(user_title) : 0) + 256;
4387  title = pg_malloc(title_len);
4388 
4389  for (;;)
4390  {
4391  time_t timer;
4392  char timebuf[128];
4393  long i;
4394 
4395  /*
4396  * Prepare title for output. Note that we intentionally include a
4397  * newline at the end of the title; this is somewhat historical but it
4398  * makes for reasonably nicely formatted output in simple cases.
4399  */
4400  timer = time(NULL);
4401  strftime(timebuf, sizeof(timebuf), strftime_fmt, localtime(&timer));
4402 
4403  if (user_title)
4404  snprintf(title, title_len, _("%s\t%s (every %gs)\n"),
4405  user_title, timebuf, sleep);
4406  else
4407  snprintf(title, title_len, _("%s (every %gs)\n"),
4408  timebuf, sleep);
4409  myopt.title = title;
4410 
4411  /* Run the query and print out the results */
4412  res = PSQLexecWatch(query_buf->data, &myopt);
4413 
4414  /*
4415  * PSQLexecWatch handles the case where we can no longer repeat the
4416  * query, and returns 0 or -1.
4417  */
4418  if (res <= 0)
4419  break;
4420 
4421  /*
4422  * Set up cancellation of 'watch' via SIGINT. We redo this each time
4423  * through the loop since it's conceivable something inside
4424  * PSQLexecWatch could change sigint_interrupt_jmp.
4425  */
4426  if (sigsetjmp(sigint_interrupt_jmp, 1) != 0)
4427  break;
4428 
4429  /*
4430  * Enable 'watch' cancellations and wait a while before running the
4431  * query again. Break the sleep into short intervals (at most 1s)
4432  * since pg_usleep isn't interruptible on some platforms.
4433  */
4434  sigint_interrupt_enabled = true;
4435  i = sleep_ms;
4436  while (i > 0)
4437  {
4438  long s = Min(i, 1000L);
4439 
4440  pg_usleep(s * 1000L);
4441  if (cancel_pressed)
4442  break;
4443  i -= s;
4444  }
4445  sigint_interrupt_enabled = false;
4446  }
4447 
4448  pg_free(title);
4449  return (res >= 0);
4450 }
volatile bool sigint_interrupt_enabled
Definition: common.c:273
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:697
#define Min(x, y)
Definition: c.h:807
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:275
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:220
unsigned short int pager
Definition: print.h:103
char * title
Definition: print.h:167
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void pg_free(void *ptr)
Definition: fe_memutils.c:105
int i
#define _(x)
Definition: elog.c:84
static bool echo_hidden_command ( const char *  query)
static

Definition at line 4457 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().

4458 {
4460  {
4461  printf(_("********* QUERY **********\n"
4462  "%s\n"
4463  "**************************\n\n"), query);
4464  fflush(stdout);
4465  if (pset.logfile)
4466  {
4467  fprintf(pset.logfile,
4468  _("********* QUERY **********\n"
4469  "%s\n"
4470  "**************************\n\n"), query);
4471  fflush(pset.logfile);
4472  }
4473 
4475  return false;
4476  }
4477  return true;
4478 }
PsqlSettings pset
Definition: startup.c:33
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:132
FILE * logfile
Definition: settings.h:114
#define _(x)
Definition: elog.c:84
static bool editFile ( const char *  fname,
int  lineno 
)
static

Definition at line 3379 of file command.c.

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

Referenced by do_edit().

3380 {
3381  const char *editorName;
3382  const char *editor_lineno_arg = NULL;
3383  char *sys;
3384  int result;
3385 
3386  Assert(fname != NULL);
3387 
3388  /* Find an editor to use */
3389  editorName = getenv("PSQL_EDITOR");
3390  if (!editorName)
3391  editorName = getenv("EDITOR");
3392  if (!editorName)
3393  editorName = getenv("VISUAL");
3394  if (!editorName)
3395  editorName = DEFAULT_EDITOR;
3396 
3397  /* Get line number argument, if we need it. */
3398  if (lineno > 0)
3399  {
3400  editor_lineno_arg = getenv("PSQL_EDITOR_LINENUMBER_ARG");
3401 #ifdef DEFAULT_EDITOR_LINENUMBER_ARG
3402  if (!editor_lineno_arg)
3403  editor_lineno_arg = DEFAULT_EDITOR_LINENUMBER_ARG;
3404 #endif
3405  if (!editor_lineno_arg)
3406  {
3407  psql_error("environment variable PSQL_EDITOR_LINENUMBER_ARG must be set to specify a line number\n");
3408  return false;
3409  }
3410  }
3411 
3412  /*
3413  * On Unix the EDITOR value should *not* be quoted, since it might include
3414  * switches, eg, EDITOR="pico -t"; it's up to the user to put quotes in it
3415  * if necessary. But this policy is not very workable on Windows, due to
3416  * severe brain damage in their command shell plus the fact that standard
3417  * program paths include spaces.
3418  */
3419 #ifndef WIN32
3420  if (lineno > 0)
3421  sys = psprintf("exec %s %s%d '%s'",
3422  editorName, editor_lineno_arg, lineno, fname);
3423  else
3424  sys = psprintf("exec %s '%s'",
3425  editorName, fname);
3426 #else
3427  if (lineno > 0)
3428  sys = psprintf("\"%s\" %s%d \"%s\"",
3429  editorName, editor_lineno_arg, lineno, fname);
3430  else
3431  sys = psprintf("\"%s\" \"%s\"",
3432  editorName, fname);
3433 #endif
3434  result = system(sys);
3435  if (result == -1)
3436  psql_error("could not start editor \"%s\"\n", editorName);
3437  else if (result == 127)
3438  psql_error("could not start /bin/sh\n");
3439  free(sys);
3440 
3441  return result == 0;
3442 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
return result
Definition: formatting.c:1633
#define DEFAULT_EDITOR
Definition: settings.h:22
void psql_error(const char *fmt,...)
Definition: common.c:220
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:676
#define DEFAULT_EDITOR_LINENUMBER_ARG
Definition: settings.h:23
static backslashResult exec_command ( const char *  cmd,
PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 276 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(), exec_command_elif(), exec_command_else(), exec_command_encoding(), exec_command_endif(), exec_command_errverbose(), exec_command_ev(), exec_command_f(), exec_command_g(), 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(), exec_command_shell_escape(), exec_command_slash_command_help(), exec_command_sv(), 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().

281 {
283  bool active_branch = conditional_active(cstack);
284 
285  /*
286  * In interactive mode, warn when we're ignoring a command within a false
287  * \if-branch. But we continue on, so as to parse and discard the right
288  * amount of parameter text. Each individual backslash command subroutine
289  * is responsible for doing nothing after discarding appropriate
290  * arguments, if !active_branch.
291  */
292  if (pset.cur_cmd_interactive && !active_branch &&
293  !is_branching_command(cmd))
294  {
295  psql_error("\\%s command ignored; use \\endif or Ctrl-C to exit current \\if block\n",
296  cmd);
297  }
298 
299  if (strcmp(cmd, "a") == 0)
300  status = exec_command_a(scan_state, active_branch);
301  else if (strcmp(cmd, "C") == 0)
302  status = exec_command_C(scan_state, active_branch);
303  else if (strcmp(cmd, "c") == 0 || strcmp(cmd, "connect") == 0)
304  status = exec_command_connect(scan_state, active_branch);
305  else if (strcmp(cmd, "cd") == 0)
306  status = exec_command_cd(scan_state, active_branch, cmd);
307  else if (strcmp(cmd, "conninfo") == 0)
308  status = exec_command_conninfo(scan_state, active_branch);
309  else if (pg_strcasecmp(cmd, "copy") == 0)
310  status = exec_command_copy(scan_state, active_branch);
311  else if (strcmp(cmd, "copyright") == 0)
312  status = exec_command_copyright(scan_state, active_branch);
313  else if (strcmp(cmd, "crosstabview") == 0)
314  status = exec_command_crosstabview(scan_state, active_branch);
315  else if (cmd[0] == 'd')
316  status = exec_command_d(scan_state, active_branch, cmd);
317  else if (strcmp(cmd, "e") == 0 || strcmp(cmd, "edit") == 0)
318  status = exec_command_edit(scan_state, active_branch,
319  query_buf, previous_buf);
320  else if (strcmp(cmd, "ef") == 0)
321  status = exec_command_ef(scan_state, active_branch, query_buf);
322  else if (strcmp(cmd, "ev") == 0)
323  status = exec_command_ev(scan_state, active_branch, query_buf);
324  else if (strcmp(cmd, "echo") == 0 || strcmp(cmd, "qecho") == 0)
325  status = exec_command_echo(scan_state, active_branch, cmd);
326  else if (strcmp(cmd, "elif") == 0)
327  status = exec_command_elif(scan_state, cstack, query_buf);
328  else if (strcmp(cmd, "else") == 0)
329  status = exec_command_else(scan_state, cstack, query_buf);
330  else if (strcmp(cmd, "endif") == 0)
331  status = exec_command_endif(scan_state, cstack, query_buf);
332  else if (strcmp(cmd, "encoding") == 0)
333  status = exec_command_encoding(scan_state, active_branch);
334  else if (strcmp(cmd, "errverbose") == 0)
335  status = exec_command_errverbose(scan_state, active_branch);
336  else if (strcmp(cmd, "f") == 0)
337  status = exec_command_f(scan_state, active_branch);
338  else if (strcmp(cmd, "g") == 0 || strcmp(cmd, "gx") == 0)
339  status = exec_command_g(scan_state, active_branch, cmd);
340  else if (strcmp(cmd, "gexec") == 0)
341  status = exec_command_gexec(scan_state, active_branch);
342  else if (strcmp(cmd, "gset") == 0)
343  status = exec_command_gset(scan_state, active_branch);
344  else if (strcmp(cmd, "h") == 0 || strcmp(cmd, "help") == 0)
345  status = exec_command_help(scan_state, active_branch);
346  else if (strcmp(cmd, "H") == 0 || strcmp(cmd, "html") == 0)
347  status = exec_command_html(scan_state, active_branch);
348  else if (strcmp(cmd, "i") == 0 || strcmp(cmd, "include") == 0 ||
349  strcmp(cmd, "ir") == 0 || strcmp(cmd, "include_relative") == 0)
350  status = exec_command_include(scan_state, active_branch, cmd);
351  else if (strcmp(cmd, "if") == 0)
352  status = exec_command_if(scan_state, cstack, query_buf);
353  else if (strcmp(cmd, "l") == 0 || strcmp(cmd, "list") == 0 ||
354  strcmp(cmd, "l+") == 0 || strcmp(cmd, "list+") == 0)
355  status = exec_command_list(scan_state, active_branch, cmd);
356  else if (strncmp(cmd, "lo_", 3) == 0)
357  status = exec_command_lo(scan_state, active_branch, cmd);
358  else if (strcmp(cmd, "o") == 0 || strcmp(cmd, "out") == 0)
359  status = exec_command_out(scan_state, active_branch);
360  else if (strcmp(cmd, "p") == 0 || strcmp(cmd, "print") == 0)
361  status = exec_command_print(scan_state, active_branch,
362  query_buf, previous_buf);
363  else if (strcmp(cmd, "password") == 0)
364  status = exec_command_password(scan_state, active_branch);
365  else if (strcmp(cmd, "prompt") == 0)
366  status = exec_command_prompt(scan_state, active_branch, cmd);
367  else if (strcmp(cmd, "pset") == 0)
368  status = exec_command_pset(scan_state, active_branch);
369  else if (strcmp(cmd, "q") == 0 || strcmp(cmd, "quit") == 0)
370  status = exec_command_quit(scan_state, active_branch);
371  else if (strcmp(cmd, "r") == 0 || strcmp(cmd, "reset") == 0)
372  status = exec_command_reset(scan_state, active_branch, query_buf);
373  else if (strcmp(cmd, "s") == 0)
374  status = exec_command_s(scan_state, active_branch);
375  else if (strcmp(cmd, "set") == 0)
376  status = exec_command_set(scan_state, active_branch);
377  else if (strcmp(cmd, "setenv") == 0)
378  status = exec_command_setenv(scan_state, active_branch, cmd);
379  else if (strcmp(cmd, "sf") == 0 || strcmp(cmd, "sf+") == 0)
380  status = exec_command_sf(scan_state, active_branch, cmd);
381  else if (strcmp(cmd, "sv") == 0 || strcmp(cmd, "sv+") == 0)
382  status = exec_command_sv(scan_state, active_branch, cmd);
383  else if (strcmp(cmd, "t") == 0)
384  status = exec_command_t(scan_state, active_branch);
385  else if (strcmp(cmd, "T") == 0)
386  status = exec_command_T(scan_state, active_branch);
387  else if (strcmp(cmd, "timing") == 0)
388  status = exec_command_timing(scan_state, active_branch);
389  else if (strcmp(cmd, "unset") == 0)
390  status = exec_command_unset(scan_state, active_branch, cmd);
391  else if (strcmp(cmd, "w") == 0 || strcmp(cmd, "write") == 0)
392  status = exec_command_write(scan_state, active_branch, cmd,
393  query_buf, previous_buf);
394  else if (strcmp(cmd, "watch") == 0)
395  status = exec_command_watch(scan_state, active_branch,
396  query_buf, previous_buf);
397  else if (strcmp(cmd, "x") == 0)
398  status = exec_command_x(scan_state, active_branch);
399  else if (strcmp(cmd, "z") == 0)
400  status = exec_command_z(scan_state, active_branch);
401  else if (strcmp(cmd, "!") == 0)
402  status = exec_command_shell_escape(scan_state, active_branch);
403  else if (strcmp(cmd, "?") == 0)
404  status = exec_command_slash_command_help(scan_state, active_branch);
405  else
406  status = PSQL_CMD_UNKNOWN;
407 
408  /*
409  * All the commands that return PSQL_CMD_SEND want to execute previous_buf
410  * if query_buf is empty. For convenience we implement that here, not in
411  * the individual command subroutines.
412  */
413  if (status == PSQL_CMD_SEND)
414  copy_previous_query(query_buf, previous_buf);
415 
416  return status;
417 }
static backslashResult exec_command_list(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1701
static backslashResult exec_command_echo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1172
static backslashResult exec_command_include(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1433
static backslashResult exec_command_unset(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2478
static backslashResult exec_command_else(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1596
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:1923
static backslashResult exec_command_a(PsqlScanState scan_state, bool active_branch)
Definition: command.c:426
PsqlSettings pset
Definition: startup.c:33
static backslashResult exec_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1383
static bool is_branching_command(const char *cmd)
Definition: command.c:2885
static backslashResult exec_command_edit(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:906
static backslashResult exec_command_ev(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:1092
static backslashResult exec_command_f(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1283
static backslashResult exec_command_sv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2314
static backslashResult exec_command_slash_command_help(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2703
static backslashResult exec_command_print(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:1829
static backslashResult exec_command_t(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2402
static backslashResult exec_command_pset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1997
static backslashResult exec_command_errverbose(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1253
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:1855
static backslashResult exec_command_z(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2658
static backslashResult exec_command_g(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1306
static backslashResult exec_command_watch(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2597
bool cur_cmd_interactive
Definition: settings.h:105
static backslashResult exec_command_shell_escape(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2681
static backslashResult exec_command_setenv(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2163
static backslashResult exec_command_conninfo(PsqlScanState scan_state, bool active_branch)
Definition: command.c:589
static backslashResult exec_command_elif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1520
static backslashResult exec_command_ef(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:983
static backslashResult exec_command_crosstabview(PsqlScanState scan_state, bool active_branch)
Definition: command.c:673
static backslashResult exec_command_connect(PsqlScanState scan_state, bool active_branch)
Definition: command.c:478
static backslashResult exec_command_gset(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1354
static backslashResult exec_command_endif(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1661
static backslashResult exec_command_set(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2110
static backslashResult exec_command_if(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:1474
static backslashResult exec_command_gexec(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1337
void psql_error(const char *fmt,...)
Definition: common.c:220
static backslashResult exec_command_timing(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2446
static backslashResult exec_command_encoding(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1215
static backslashResult exec_command_html(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1414
enum _backslashResult backslashResult
static backslashResult exec_command_s(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2083
static void copy_previous_query(PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2944
static backslashResult exec_command_write(PsqlScanState scan_state, bool active_branch, const char *cmd, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:2508
static backslashResult exec_command_quit(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2051
static backslashResult exec_command_x(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2636
static backslashResult exec_command_cd(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:531
static backslashResult exec_command_copyright(PsqlScanState scan_state, bool active_branch)
Definition: command.c:661
static backslashResult exec_command_lo(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:1730
static backslashResult exec_command_T(PsqlScanState scan_state, bool active_branch)
Definition: command.c:2424
static backslashResult exec_command_out(PsqlScanState scan_state, bool active_branch)
Definition: command.c:1806
static backslashResult exec_command_copy(PsqlScanState scan_state, bool active_branch)
Definition: command.c:639
static backslashResult exec_command_sf(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:2220
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static backslashResult exec_command_reset(PsqlScanState scan_state, bool active_branch, PQExpBuffer query_buf)
Definition: command.c:2065
static backslashResult exec_command_C(PsqlScanState scan_state, bool active_branch)
Definition: command.c:445
static backslashResult exec_command_d(PsqlScanState scan_state, bool active_branch, const char *cmd)
Definition: command.c:697
static backslashResult exec_command_a ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 426 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().

427 {
428  bool success = true;
429 
430  if (active_branch)
431  {
433  success = do_pset("format", "aligned", &pset.popt, pset.quiet);
434  else
435  success = do_pset("format", "unaligned", &pset.popt, pset.quiet);
436  }
437 
438  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
439 }
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:96
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3745
printQueryOpt popt
Definition: settings.h:91
static backslashResult exec_command_C ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 445 of file command.c.

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

Referenced by exec_command().

446 {
447  bool success = true;
448 
449  if (active_branch)
450  {
451  char *opt = psql_scan_slash_option(scan_state,
452  OT_NORMAL, NULL, true);
453 
454  success = do_pset("title", opt, &pset.popt, pset.quiet);
455  free(opt);
456  }
457  else
458  ignore_slash_options(scan_state);
459 
460  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
461 }
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:96
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3745
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
static backslashResult exec_command_cd ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 531 of file command.c.

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

Referenced by exec_command().

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

Definition at line 478 of file command.c.

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

Referenced by exec_command().

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

Definition at line 589 of file command.c.

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

Referenced by exec_command().

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

Definition at line 639 of file command.c.

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

Referenced by exec_command().

640 {
641  bool success = true;
642 
643  if (active_branch)
644  {
645  char *opt = psql_scan_slash_option(scan_state,
646  OT_WHOLE_LINE, NULL, false);
647 
648  success = do_copy(opt);
649  free(opt);
650  }
651  else
652  ignore_slash_whole_line(scan_state);
653 
654  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
655 }
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:96
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2872
static backslashResult exec_command_copyright ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 661 of file command.c.

References print_copyright(), and PSQL_CMD_SKIP_LINE.

Referenced by exec_command().

662 {
663  if (active_branch)
664  print_copyright();
665 
666  return PSQL_CMD_SKIP_LINE;
667 }
void print_copyright(void)
Definition: help.c:581
static backslashResult exec_command_crosstabview ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 673 of file command.c.

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

Referenced by exec_command().

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

Definition at line 697 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(), NULL, objectDescription(), OT_NORMAL, permissionsList(), PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, PSQL_CMD_UNKNOWN, psql_scan_slash_option(), status(), and success.

Referenced by exec_command().

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

Definition at line 1172 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().

1173 {
1174  if (active_branch)
1175  {
1176  char *value;
1177  char quoted;
1178  bool no_newline = false;
1179  bool first = true;
1180  FILE *fout;
1181 
1182  if (strcmp(cmd, "qecho") == 0)
1183  fout = pset.queryFout;
1184  else
1185  fout = stdout;
1186 
1187  while ((value = psql_scan_slash_option(scan_state,
1188  OT_NORMAL, &quoted, false)))
1189  {
1190  if (!quoted && strcmp(value, "-n") == 0)
1191  no_newline = true;
1192  else
1193  {
1194  if (first)
1195  first = false;
1196  else
1197  fputc(' ', fout);
1198  fputs(value, fout);
1199  }
1200  free(value);
1201  }
1202  if (!no_newline)
1203  fputs("\n", fout);
1204  }
1205  else
1206  ignore_slash_options(scan_state);
1207 
1208  return PSQL_CMD_SKIP_LINE;
1209 }
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:2838
static backslashResult exec_command_edit ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 906 of file command.c.

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

Referenced by exec_command().

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

Definition at line 983 of file command.c.

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

Referenced by exec_command().

985 {
987 
988  if (active_branch)
989  {
990  char *func = psql_scan_slash_option(scan_state,
991  OT_WHOLE_LINE, NULL, true);
992  int lineno = -1;
993 
994  if (pset.sversion < 80400)
995  {
996  char sverbuf[32];
997 
998  psql_error("The server (version %s) does not support editing function source.\n",
1000  sverbuf, sizeof(sverbuf)));
1001  status = PSQL_CMD_ERROR;
1002  }
1003  else if (!query_buf)
1004  {
1005  psql_error("no query buffer\n");
1006  status = PSQL_CMD_ERROR;
1007  }
1008  else
1009  {
1010  Oid foid = InvalidOid;
1011 
1012  lineno = strip_lineno_from_objdesc(func);
1013  if (lineno == 0)
1014  {
1015  /* error already reported */
1016  status = PSQL_CMD_ERROR;
1017  }
1018  else if (!func)
1019  {
1020  /* set up an empty command to fill in */
1021  printfPQExpBuffer(query_buf,
1022  "CREATE FUNCTION ( )\n"
1023  " RETURNS \n"
1024  " LANGUAGE \n"
1025  " -- common options: IMMUTABLE STABLE STRICT SECURITY DEFINER\n"
1026  "AS $function$\n"
1027  "\n$function$\n");
1028  }
1029  else if (!lookup_object_oid(EditableFunction, func, &foid))
1030  {
1031  /* error already reported */
1032  status = PSQL_CMD_ERROR;
1033  }
1034  else if (!get_create_object_cmd(EditableFunction, foid, query_buf))
1035  {
1036  /* error already reported */
1037  status = PSQL_CMD_ERROR;
1038  }
1039  else if (lineno > 0)
1040  {
1041  /*
1042  * lineno "1" should correspond to the first line of the
1043  * function body. We expect that pg_get_functiondef() will
1044  * emit that on a line beginning with "AS ", and that there
1045  * can be no such line before the real start of the function
1046  * body. Increment lineno by the number of lines before that
1047  * line, so that it becomes relative to the first line of the
1048  * function definition.
1049  */
1050  const char *lines = query_buf->data;
1051 
1052  while (*lines != '\0')
1053  {
1054  if (strncmp(lines, "AS ", 3) == 0)
1055  break;
1056  lineno++;
1057  /* find start of next line */
1058  lines = strchr(lines, '\n');
1059  if (!lines)
1060  break;
1061  lines++;
1062  }
1063  }
1064  }
1065 
1066  if (status != PSQL_CMD_ERROR)
1067  {
1068  bool edited = false;
1069 
1070  if (!do_edit(NULL, query_buf, lineno, &edited))
1071  status = PSQL_CMD_ERROR;
1072  else if (!edited)
1073  puts(_("No changes"));
1074  else
1075  status = PSQL_CMD_NEWEDIT;
1076  }
1077 
1078  if (func)
1079  free(func);
1080  }
1081  else
1082  ignore_slash_whole_line(scan_state);
1083 
1084  return status;
1085 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PsqlSettings pset
Definition: startup.c:33
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:4489
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:220
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool *edited)
Definition: command.c:3447
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
#define NULL
Definition: c.h:229
static int strip_lineno_from_objdesc(char *obj)
Definition: command.c:4722
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:4549
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
#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:2872
static backslashResult exec_command_elif ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 1520 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().

1522 {
1523  bool success = true;
1524 
1525  switch (conditional_stack_peek(cstack))
1526  {
1527  case IFSTATE_TRUE:
1528 
1529  /*
1530  * Just finished active branch of this \if block. Update saved
1531  * state so we will keep whatever data was put in query_buf by the
1532  * active branch.
1533  */
1534  save_query_text_state(scan_state, cstack, query_buf);
1535 
1536  /*
1537  * Discard \elif expression and ignore the rest until \endif.
1538  * Switch state before reading expression to ensure proper lexer
1539  * behavior.
1540  */
1542  ignore_boolean_expression(scan_state);
1543  break;
1544  case IFSTATE_FALSE:
1545 
1546  /*
1547  * Discard any query text added by the just-skipped branch.
1548  */
1549  discard_query_text(scan_state, cstack, query_buf);
1550 
1551  /*
1552  * Have not yet found a true expression in this \if block, so this
1553  * might be the first. We have to change state before examining
1554  * the expression, or the lexer won't do the right thing.
1555  */
1557  if (!is_true_boolean_expression(scan_state, "\\elif expression"))
1559  break;
1560  case IFSTATE_IGNORED:
1561 
1562  /*
1563  * Discard any query text added by the just-skipped branch.
1564  */
1565  discard_query_text(scan_state, cstack, query_buf);
1566 
1567  /*
1568  * Skip expression and move on. Either the \if block already had
1569  * an active section, or whole block is being skipped.
1570  */
1571  ignore_boolean_expression(scan_state);
1572  break;
1573  case IFSTATE_ELSE_TRUE:
1574  case IFSTATE_ELSE_FALSE:
1575  psql_error("\\elif: cannot occur after \\else\n");
1576  success = false;
1577  break;
1578  case IFSTATE_NONE:
1579  /* no \if to elif from */
1580  psql_error("\\elif: no matching \\if\n");
1581  success = false;
1582  break;
1583  }
1584 
1585  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1586 }
static void ignore_boolean_expression(PsqlScanState scan_state)
Definition: command.c:2822
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2901
static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name)
Definition: command.c:2805
static bool success
Definition: pg_basebackup.c:96
void psql_error(const char *fmt,...)
Definition: common.c:220
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2921
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:70
bool conditional_stack_poke(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:82
static backslashResult exec_command_else ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 1596 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().

1598 {
1599  bool success = true;
1600 
1601  switch (conditional_stack_peek(cstack))
1602  {
1603  case IFSTATE_TRUE:
1604 
1605  /*
1606  * Just finished active branch of this \if block. Update saved
1607  * state so we will keep whatever data was put in query_buf by the
1608  * active branch.
1609  */
1610  save_query_text_state(scan_state, cstack, query_buf);
1611 
1612  /* Now skip the \else branch */
1614  break;
1615  case IFSTATE_FALSE:
1616 
1617  /*
1618  * Discard any query text added by the just-skipped branch.
1619  */
1620  discard_query_text(scan_state, cstack, query_buf);
1621 
1622  /*
1623  * We've not found any true \if or \elif expression, so execute
1624  * the \else branch.
1625  */
1627  break;
1628  case IFSTATE_IGNORED:
1629 
1630  /*
1631  * Discard any query text added by the just-skipped branch.
1632  */
1633  discard_query_text(scan_state, cstack, query_buf);
1634 
1635  /*
1636  * Either we previously processed the active branch of this \if,
1637  * or the whole \if block is being skipped. Either way, skip the
1638  * \else branch.
1639  */
1641  break;
1642  case IFSTATE_ELSE_TRUE:
1643  case IFSTATE_ELSE_FALSE:
1644  psql_error("\\else: cannot occur after \\else\n");
1645  success = false;
1646  break;
1647  case IFSTATE_NONE:
1648  /* no \if to else from */
1649  psql_error("\\else: no matching \\if\n");
1650  success = false;
1651  break;
1652  }
1653 
1654  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1655 }
static void save_query_text_state(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2901
static bool success
Definition: pg_basebackup.c:96
void psql_error(const char *fmt,...)
Definition: common.c:220
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2921
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:70
bool conditional_stack_poke(ConditionalStack cstack, ifState new_state)
Definition: conditional.c:82
static backslashResult exec_command_encoding ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1215 of file command.c.

References _psqlSettings::db, _psqlSettings::encoding, printTableOpt::encoding, encoding, free, ignore_slash_options(), NULL, 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().

1216 {
1217  if (active_branch)
1218  {
1219  char *encoding = psql_scan_slash_option(scan_state,
1220  OT_NORMAL, NULL, false);
1221 
1222  if (!encoding)
1223  {
1224  /* show encoding */
1226  }
1227  else
1228  {
1229  /* set encoding */
1230  if (PQsetClientEncoding(pset.db, encoding) == -1)
1231  psql_error("%s: invalid encoding name or conversion procedure not found\n", encoding);
1232  else
1233  {
1234  /* save encoding info into psql internal data */
1237  SetVariable(pset.vars, "ENCODING",
1239  }
1240  free(encoding);
1241  }
1242  }
1243  else
1244  ignore_slash_options(scan_state);
1245 
1246  return PSQL_CMD_SKIP_LINE;
1247 }
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:6157
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:220
int PQsetClientEncoding(PGconn *conn, const char *encoding)
Definition: fe-connect.c:6165
static char * encoding
Definition: initdb.c:122
#define free(a)
Definition: header.h:65
const char * pg_encoding_to_char(int encoding)
Definition: encnames.c:607
#define NULL
Definition: c.h:229
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:2838
int encoding
Definition: settings.h:83
VariableSpace vars
Definition: settings.h:116
static backslashResult exec_command_endif ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 1661 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().

1663 {
1664  bool success = true;
1665 
1666  switch (conditional_stack_peek(cstack))
1667  {
1668  case IFSTATE_TRUE:
1669  case IFSTATE_ELSE_TRUE:
1670  /* Close the \if block, keeping the query text */
1671  success = conditional_stack_pop(cstack);
1672  Assert(success);
1673  break;
1674  case IFSTATE_FALSE:
1675  case IFSTATE_IGNORED:
1676  case IFSTATE_ELSE_FALSE:
1677 
1678  /*
1679  * Discard any query text added by the just-skipped branch.
1680  */
1681  discard_query_text(scan_state, cstack, query_buf);
1682 
1683  /* Close the \if block */
1684  success = conditional_stack_pop(cstack);
1685  Assert(success);
1686  break;
1687  case IFSTATE_NONE:
1688  /* no \if to end */
1689  psql_error("\\endif: no matching \\if\n");
1690  success = false;
1691  break;
1692  }
1693 
1694  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1695 }
bool conditional_stack_pop(ConditionalStack cstack)
Definition: conditional.c:55
static bool success
Definition: pg_basebackup.c:96
void psql_error(const char *fmt,...)
Definition: common.c:220
static void discard_query_text(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf)
Definition: command.c:2921
ifState conditional_stack_peek(ConditionalStack cstack)
Definition: conditional.c:70
#define Assert(condition)
Definition: c.h:676
static backslashResult exec_command_errverbose ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1253 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().

1254 {
1255  if (active_branch)
1256  {
1257  if (pset.last_error_result)
1258  {
1259  char *msg;
1260 
1264  if (msg)
1265  {
1266  psql_error("%s", msg);
1267  PQfreemem(msg);
1268  }
1269  else
1270  puts(_("out of memory"));
1271  }
1272  else
1273  puts(_("There is no previous error."));
1274  }
1275 
1276  return PSQL_CMD_SKIP_LINE;
1277 }
PsqlSettings pset
Definition: startup.c:33
char * PQresultVerboseErrorMessage(const PGresult *res, PGVerbosity verbosity, PGContextVisibility show_context)
Definition: fe-exec.c:2620
void psql_error(const char *fmt,...)
Definition: common.c:220
PGresult * last_error_result
Definition: settings.h:89
#define _(x)
Definition: elog.c:84
void PQfreemem(void *ptr)
Definition: fe-exec.c:3200
static backslashResult exec_command_ev ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf 
)
static

Definition at line 1092 of file command.c.

References _, do_edit(), EditableView, formatPGVersionNumber(), free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), NULL, OT_WHOLE_LINE, printfPQExpBuffer(), pset, PSQL_CMD_ERROR, PSQL_CMD_NEWEDIT, PSQL_CMD_SKIP_LINE, psql_error(), psql_scan_slash_option(), status(), strip_lineno_from_objdesc(), and _psqlSettings::sversion.

Referenced by exec_command().

1094 {
1096 
1097  if (active_branch)
1098  {
1099  char *view = psql_scan_slash_option(scan_state,
1100  OT_WHOLE_LINE, NULL, true);
1101  int lineno = -1;
1102 
1103  if (pset.sversion < 70400)
1104  {
1105  char sverbuf[32];
1106 
1107  psql_error("The server (version %s) does not support editing view definitions.\n",
1109  sverbuf, sizeof(sverbuf)));
1110  status = PSQL_CMD_ERROR;
1111  }
1112  else if (!query_buf)
1113  {
1114  psql_error("no query buffer\n");
1115  status = PSQL_CMD_ERROR;
1116  }
1117  else
1118  {
1119  Oid view_oid = InvalidOid;
1120 
1121  lineno = strip_lineno_from_objdesc(view);
1122  if (lineno == 0)
1123  {
1124  /* error already reported */
1125  status = PSQL_CMD_ERROR;
1126  }
1127  else if (!view)
1128  {
1129  /* set up an empty command to fill in */
1130  printfPQExpBuffer(query_buf,
1131  "CREATE VIEW AS\n"
1132  " SELECT \n"
1133  " -- something...\n");
1134  }
1135  else if (!lookup_object_oid(EditableView, view, &view_oid))
1136  {
1137  /* error already reported */
1138  status = PSQL_CMD_ERROR;
1139  }
1140  else if (!get_create_object_cmd(EditableView, view_oid, query_buf))
1141  {
1142  /* error already reported */
1143  status = PSQL_CMD_ERROR;
1144  }
1145  }
1146 
1147  if (status != PSQL_CMD_ERROR)
1148  {
1149  bool edited = false;
1150 
1151  if (!do_edit(NULL, query_buf, lineno, &edited))
1152  status = PSQL_CMD_ERROR;
1153  else if (!edited)
1154  puts(_("No changes"));
1155  else
1156  status = PSQL_CMD_NEWEDIT;
1157  }
1158 
1159  if (view)
1160  free(view);
1161  }
1162  else
1163  ignore_slash_whole_line(scan_state);
1164 
1165  return status;
1166 }
void printfPQExpBuffer(PQExpBuffer str, const char *fmt,...)
Definition: pqexpbuffer.c:234
PsqlSettings pset
Definition: startup.c:33
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:4489
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:220
static bool do_edit(const char *filename_arg, PQExpBuffer query_buf, int lineno, bool *edited)
Definition: command.c:3447
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
#define NULL
Definition: c.h:229
static int strip_lineno_from_objdesc(char *obj)
Definition: command.c:4722
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:4549
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
#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:2872
static backslashResult exec_command_f ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1283 of file command.c.

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

Referenced by exec_command().

1284 {
1285  bool success = true;
1286 
1287  if (active_branch)
1288  {
1289  char *fname = psql_scan_slash_option(scan_state,
1290  OT_NORMAL, NULL, false);
1291 
1292  success = do_pset("fieldsep", fname, &pset.popt, pset.quiet);
1293  free(fname);
1294  }
1295  else
1296  ignore_slash_options(scan_state);
1297 
1298  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1299 }
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:96
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3745
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
static backslashResult exec_command_g ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1306 of file command.c.

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

Referenced by exec_command().

1307 {
1309 
1310  if (active_branch)
1311  {
1312  char *fname = psql_scan_slash_option(scan_state,
1313  OT_FILEPIPE, NULL, false);
1314 
1315  if (!fname)
1316  pset.gfname = NULL;
1317  else
1318  {
1319  expand_tilde(&fname);
1320  pset.gfname = pg_strdup(fname);
1321  }
1322  free(fname);
1323  if (strcmp(cmd, "gx") == 0)
1324  pset.g_expanded = true;
1325  status = PSQL_CMD_SEND;
1326  }
1327  else
1328  ignore_slash_filepipe(scan_state);
1329 
1330  return status;
1331 }
PsqlSettings pset
Definition: startup.c:33
void expand_tilde(char **filename)
Definition: common.c:2103
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:2855
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
#define NULL
Definition: c.h:229
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static backslashResult exec_command_gexec ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1337 of file command.c.

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

Referenced by exec_command().

1338 {
1340 
1341  if (active_branch)
1342  {
1343  pset.gexec_flag = true;
1344  status = PSQL_CMD_SEND;
1345  }
1346 
1347  return status;
1348 }
PsqlSettings pset
Definition: startup.c:33
bool gexec_flag
Definition: settings.h:96
enum _backslashResult backslashResult
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static backslashResult exec_command_gset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1354 of file command.c.

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

Referenced by exec_command().

1355 {
1357 
1358  if (active_branch)
1359  {
1360  char *prefix = psql_scan_slash_option(scan_state,
1361  OT_NORMAL, NULL, false);
1362 
1363  if (prefix)
1364  pset.gset_prefix = prefix;
1365  else
1366  {
1367  /* we must set a non-NULL prefix to trigger storing */
1368  pset.gset_prefix = pg_strdup("");
1369  }
1370  /* gset_prefix is freed later */
1371  status = PSQL_CMD_SEND;
1372  }
1373  else
1374  ignore_slash_options(scan_state);
1375 
1376  return status;
1377 }
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
#define NULL
Definition: c.h:229
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static backslashResult exec_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1383 of file command.c.

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

Referenced by exec_command().

1384 {
1385  if (active_branch)
1386  {
1387  char *opt = psql_scan_slash_option(scan_state,
1388  OT_WHOLE_LINE, NULL, false);
1389  size_t len;
1390 
1391  /* strip any trailing spaces and semicolons */
1392  if (opt)
1393  {
1394  len = strlen(opt);
1395  while (len > 0 &&
1396  (isspace((unsigned char) opt[len - 1])
1397  || opt[len - 1] == ';'))
1398  opt[--len] = '\0';
1399  }
1400 
1401  helpSQL(opt, pset.popt.topt.pager);
1402  free(opt);
1403  }
1404  else
1405  ignore_slash_whole_line(scan_state);
1406 
1407  return PSQL_CMD_SKIP_LINE;
1408 }
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
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
void helpSQL(const char *topic, unsigned short int pager)
Definition: help.c:441
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2872
static backslashResult exec_command_html ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1414 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().

1415 {
1416  bool success = true;
1417 
1418  if (active_branch)
1419  {
1420  if (pset.popt.topt.format != PRINT_HTML)
1421  success = do_pset("format", "html", &pset.popt, pset.quiet);
1422  else
1423  success = do_pset("format", "aligned", &pset.popt, pset.quiet);
1424  }
1425 
1426  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1427 }
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:96
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3745
printQueryOpt popt
Definition: settings.h:91
static backslashResult exec_command_if ( PsqlScanState  scan_state,
ConditionalStack  cstack,
PQExpBuffer  query_buf 
)
static

Definition at line 1474 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().

1476 {
1477  if (conditional_active(cstack))
1478  {
1479  /*
1480  * First, push a new active stack entry; this ensures that the lexer
1481  * will perform variable substitution and backtick evaluation while
1482  * scanning the expression. (That should happen anyway, since we know
1483  * we're in an active outer branch, but let's be sure.)
1484  */
1486 
1487  /* Remember current query state in case we need to restore later */
1488  save_query_text_state(scan_state, cstack, query_buf);
1489 
1490  /*
1491  * Evaluate the expression; if it's false, change to inactive state.
1492  */
1493  if (!is_true_boolean_expression(scan_state, "\\if expression"))
1495  }
1496  else
1497  {
1498  /*
1499  * We're within an inactive outer branch, so this entire \if block
1500  * will be ignored. We don't want to evaluate the expression, so push
1501  * the "ignored" stack state before scanning it.
1502  */
1504 
1505  /* Remember current query state in case we need to restore later */
1506  save_query_text_state(scan_state, cstack, query_buf);
1507 
1508  ignore_boolean_expression(scan_state);
1509  }
1510 
1511  return PSQL_CMD_SKIP_LINE;
1512 }
static void ignore_boolean_expression(PsqlScanState scan_state)
Definition: command.c:2822
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:2901
static bool is_true_boolean_expression(PsqlScanState scan_state, const char *name)
Definition: command.c:2805
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
static backslashResult exec_command_include ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1433 of file command.c.

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

Referenced by exec_command().

1434 {
1435  bool success = true;
1436 
1437  if (active_branch)
1438  {
1439  char *fname = psql_scan_slash_option(scan_state,
1440  OT_NORMAL, NULL, true);
1441 
1442  if (!fname)
1443  {
1444  psql_error("\\%s: missing required argument\n", cmd);
1445  success = false;
1446  }
1447  else
1448  {
1449  bool include_relative;
1450 
1451  include_relative = (strcmp(cmd, "ir") == 0
1452  || strcmp(cmd, "include_relative") == 0);
1453  expand_tilde(&fname);
1454  success = (process_file(fname, include_relative) == EXIT_SUCCESS);
1455  free(fname);
1456  }
1457  }
1458  else
1459  ignore_slash_options(scan_state);
1460 
1461  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1462 }
#define EXIT_SUCCESS
Definition: settings.h:147
void expand_tilde(char **filename)
Definition: common.c:2103
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
void psql_error(const char *fmt,...)
Definition: common.c:220
int process_file(char *filename, bool use_relative_path)
Definition: command.c:3611
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
static backslashResult exec_command_list ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1701 of file command.c.

References free, ignore_slash_options(), listAllDbs(), NULL, OT_NORMAL, PSQL_CMD_ERROR, PSQL_CMD_SKIP_LINE, psql_scan_slash_option(), and success.

Referenced by exec_command().

1702 {
1703  bool success = true;
1704 
1705  if (active_branch)
1706  {
1707  char *pattern;
1708  bool show_verbose;
1709 
1710  pattern = psql_scan_slash_option(scan_state,
1711  OT_NORMAL, NULL, true);
1712 
1713  show_verbose = strchr(cmd, '+') ? true : false;
1714 
1715  success = listAllDbs(pattern, show_verbose);
1716 
1717  if (pattern)
1718  free(pattern);
1719  }
1720  else
1721  ignore_slash_options(scan_state);
1722 
1723  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1724 }
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:785
static backslashResult exec_command_lo ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1730 of file command.c.

References do_lo_export(), do_lo_import(), do_lo_list(), do_lo_unlink(), expand_tilde(), free, ignore_slash_options(), NULL, 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().

1731 {
1733  bool success = true;
1734 
1735  if (active_branch)
1736  {
1737  char *opt1,
1738  *opt2;
1739 
1740  opt1 = psql_scan_slash_option(scan_state,
1741  OT_NORMAL, NULL, true);
1742  opt2 = psql_scan_slash_option(scan_state,
1743  OT_NORMAL, NULL, true);
1744 
1745  if (strcmp(cmd + 3, "export") == 0)
1746  {
1747  if (!opt2)
1748  {
1749  psql_error("\\%s: missing required argument\n", cmd);
1750  success = false;
1751  }
1752  else
1753  {
1754  expand_tilde(&opt2);
1755  success = do_lo_export(opt1, opt2);
1756  }
1757  }
1758 
1759  else if (strcmp(cmd + 3, "import") == 0)
1760  {
1761  if (!opt1)
1762  {
1763  psql_error("\\%s: missing required argument\n", cmd);
1764  success = false;
1765  }
1766  else
1767  {
1768  expand_tilde(&opt1);
1769  success = do_lo_import(opt1, opt2);
1770  }
1771  }
1772 
1773  else if (strcmp(cmd + 3, "list") == 0)
1774  success = do_lo_list();
1775 
1776  else if (strcmp(cmd + 3, "unlink") == 0)
1777  {
1778  if (!opt1)
1779  {
1780  psql_error("\\%s: missing required argument\n", cmd);
1781  success = false;
1782  }
1783  else
1784  success = do_lo_unlink(opt1);
1785  }
1786 
1787  else
1788  status = PSQL_CMD_UNKNOWN;
1789 
1790  free(opt1);
1791  free(opt2);
1792  }
1793  else
1794  ignore_slash_options(scan_state);
1795 
1796  if (!success)
1797  status = PSQL_CMD_ERROR;
1798 
1799  return status;
1800 }
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:2103
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
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:220
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
#define NULL
Definition: c.h:229
bool do_lo_list(void)
Definition: large_obj.c:273
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static backslashResult exec_command_out ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1806 of file command.c.

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

Referenced by exec_command().

1807 {
1808  bool success = true;
1809 
1810  if (active_branch)
1811  {
1812  char *fname = psql_scan_slash_option(scan_state,
1813  OT_FILEPIPE, NULL, true);
1814 
1815  expand_tilde(&fname);
1816  success = setQFout(fname);
1817  free(fname);
1818  }
1819  else
1820  ignore_slash_filepipe(scan_state);
1821 
1822  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1823 }
void expand_tilde(char **filename)
Definition: common.c:2103
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
static void ignore_slash_filepipe(PsqlScanState scan_state)
Definition: command.c:2855
bool setQFout(const char *fname)
Definition: common.c:84
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static backslashResult exec_command_password ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1855 of file command.c.

References appendStringLiteralConn(), buf, PQExpBufferData::data, _psqlSettings::db, fmtId(), free, ignore_slash_options(), initPQExpBuffer(), NULL, 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().

1856 {
1857  bool success = true;
1858 
1859  if (active_branch)
1860  {
1861  char *opt0 = psql_scan_slash_option(scan_state,
1862  OT_SQLID, NULL, true);
1863  char pw1[100];
1864  char pw2[100];
1865 
1866  simple_prompt("Enter new password: ", pw1, sizeof(pw1), false);
1867  simple_prompt("Enter it again: ", pw2, sizeof(pw2), false);
1868 
1869  if (strcmp(pw1, pw2) != 0)
1870  {
1871  psql_error("Passwords didn't match.\n");
1872  success = false;
1873  }
1874  else
1875  {
1876  char *user;
1877  char *encrypted_password;
1878 
1879  if (opt0)
1880  user = opt0;
1881  else
1882  user = PQuser(pset.db);
1883 
1884  encrypted_password = PQencryptPasswordConn(pset.db, pw1, user, NULL);
1885 
1886  if (!encrypted_password)
1887  {
1888  psql_error("%s", PQerrorMessage(pset.db));
1889  success = false;
1890  }
1891  else
1892  {
1894  PGresult *res;
1895 
1896  initPQExpBuffer(&buf);
1897  printfPQExpBuffer(&buf, "ALTER USER %s PASSWORD ",
1898  fmtId(user));
1899  appendStringLiteralConn(&buf, encrypted_password, pset.db);
1900  res = PSQLexec(buf.data);
1901  termPQExpBuffer(&buf);
1902  if (!res)
1903  success = false;
1904  else
1905  PQclear(res);
1906  PQfreemem(encrypted_password);
1907  }
1908  }
1909 
1910  if (opt0)
1911  free(opt0);
1912  }
1913  else
1914  ignore_slash_options(scan_state);
1915 
1916  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1917 }
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6097
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:5973
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
static char * buf
Definition: pg_test_fsync.c:66
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:220
void PQclear(PGresult *res)
Definition: fe-exec.c:650
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
char * PQencryptPasswordConn(PGconn *conn, const char *passwd, const char *user, const char *algorithm)
Definition: fe-auth.c:1140
void appendStringLiteralConn(PQExpBuffer buf, const char *str, PGconn *conn)
Definition: string_utils.c:298
static char * user
Definition: pg_regress.c:92
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
void PQfreemem(void *ptr)
Definition: fe-exec.c:3200
void initPQExpBuffer(PQExpBuffer str)
Definition: pqexpbuffer.c:89
PGresult * PSQLexec(const char *query)
Definition: common.c:642
static backslashResult exec_command_print ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf,
PQExpBuffer  previous_buf 
)
static

Definition at line 1829 of file command.c.

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

Referenced by exec_command().

1831 {
1832  if (active_branch)
1833  {
1834  /*
1835  * We want to print the same thing \g would execute, but not to change
1836  * the query buffer state; so we can't use copy_previous_query().
1837  * Also, beware of possibility that buffer pointers are NULL.
1838  */
1839  if (query_buf && query_buf->len > 0)
1840  puts(query_buf->data);
1841  else if (previous_buf && previous_buf->len > 0)
1842  puts(previous_buf->data);
1843  else if (!pset.quiet)
1844  puts(_("Query buffer is empty."));
1845  fflush(stdout);
1846  }
1847 
1848  return PSQL_CMD_SKIP_LINE;
1849 }
PsqlSettings pset
Definition: startup.c:33
#define _(x)
Definition: elog.c:84
static backslashResult exec_command_prompt ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 1923 of file command.c.

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

Referenced by exec_command().

1925 {
1926  bool success = true;
1927 
1928  if (active_branch)
1929  {
1930  char *opt,
1931  *prompt_text = NULL;
1932  char *arg1,
1933  *arg2;
1934 
1935  arg1 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
1936  arg2 = psql_scan_slash_option(scan_state, OT_NORMAL, NULL, false);
1937 
1938  if (!arg1)
1939  {
1940  psql_error("\\%s: missing required argument\n", cmd);
1941  success = false;
1942  }
1943  else
1944  {
1945  char *result;
1946 
1947  if (arg2)
1948  {
1949  prompt_text = arg1;
1950  opt = arg2;
1951  }
1952  else
1953  opt = arg1;
1954 
1955  if (!pset.inputfile)
1956  {
1957  result = (char *) pg_malloc(4096);
1958  simple_prompt(prompt_text, result, 4096, true);
1959  }
1960  else
1961  {
1962  if (prompt_text)
1963  {
1964  fputs(prompt_text, stdout);
1965  fflush(stdout);
1966  }
1967  result = gets_fromFile(stdin);
1968  if (!result)
1969  {
1970  psql_error("\\%s: could not read value for variable\n",
1971  cmd);
1972  success = false;
1973  }
1974  }
1975 
1976  if (result &&
1977  !SetVariable(pset.vars, opt, result))
1978  success = false;
1979 
1980  if (result)
1981  free(result);
1982  if (prompt_text)
1983  free(prompt_text);
1984  free(opt);
1985  }
1986  }
1987  else
1988  ignore_slash_options(scan_state);
1989 
1990  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
1991 }
PsqlSettings pset
Definition: startup.c:33
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
return result
Definition: formatting.c:1633
char * inputfile
Definition: settings.h:108
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
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:220
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
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:2838
VariableSpace vars
Definition: settings.h:116
static backslashResult exec_command_pset ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 1997 of file command.c.

References do_pset(), free, i, ignore_slash_options(), NULL, 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().

1998 {
1999  bool success = true;
2000 
2001  if (active_branch)
2002  {
2003  char *opt0 = psql_scan_slash_option(scan_state,
2004  OT_NORMAL, NULL, false);
2005  char *opt1 = psql_scan_slash_option(scan_state,
2006  OT_NORMAL, NULL, false);
2007 
2008  if (!opt0)
2009  {
2010  /* list all variables */
2011 
2012  int i;
2013  static const char *const my_list[] = {
2014  "border", "columns", "expanded", "fieldsep", "fieldsep_zero",
2015  "footer", "format", "linestyle", "null",
2016  "numericlocale", "pager", "pager_min_lines",
2017  "recordsep", "recordsep_zero",
2018  "tableattr", "title", "tuples_only",
2019  "unicode_border_linestyle",
2020  "unicode_column_linestyle",
2021  "unicode_header_linestyle",
2022  NULL
2023  };
2024 
2025  for (i = 0; my_list[i] != NULL; i++)
2026  {
2027  char *val = pset_value_string(my_list[i], &pset.popt);
2028 
2029  printf("%-24s %s\n", my_list[i], val);
2030  free(val);
2031  }
2032 
2033  success = true;
2034  }
2035  else
2036  success = do_pset(opt0, opt1, &pset.popt, pset.quiet);
2037 
2038  free(opt0);
2039  free(opt1);
2040  }
2041  else
2042  ignore_slash_options(scan_state);
2043 
2044  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2045 }
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:96
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:3745
static char * pset_value_string(const char *param, struct printQueryOpt *popt)
Definition: command.c:4238
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
int i
long val
Definition: informix.c:689
static backslashResult exec_command_quit ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2051 of file command.c.

References PSQL_CMD_SKIP_LINE, PSQL_CMD_TERMINATE, and status().

Referenced by exec_command().

2052 {
2054 
2055  if (active_branch)
2056  status = PSQL_CMD_TERMINATE;
2057 
2058  return status;
2059 }
enum _backslashResult backslashResult
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
static backslashResult exec_command_reset ( PsqlScanState  scan_state,
bool  active_branch,
PQExpBuffer  query_buf 
)
static

Definition at line 2065 of file command.c.

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

Referenced by exec_command().

2067 {
2068  if (active_branch)
2069  {
2070  resetPQExpBuffer(query_buf);
2071  psql_scan_reset(scan_state);
2072  if (!pset.quiet)
2073  puts(_("Query buffer reset (cleared)."));
2074  }
2075 
2076  return PSQL_CMD_SKIP_LINE;
2077 }
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
static backslashResult exec_command_s ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2083 of file command.c.

References _, expand_tilde(), free, ignore_slash_options(), NULL, 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().

2084 {
2085  bool success = true;
2086 
2087  if (active_branch)
2088  {
2089  char *fname = psql_scan_slash_option(scan_state,
2090  OT_NORMAL, NULL, true);
2091 
2092  expand_tilde(&fname);
2093  success = printHistory(fname, pset.popt.topt.pager);
2094  if (success && !pset.quiet && fname)
2095  printf(_("Wrote history to file \"%s\".\n"), fname);
2096  if (!fname)
2097  putchar('\n');
2098  free(fname);
2099  }
2100  else
2101  ignore_slash_options(scan_state);
2102 
2103  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2104 }
PsqlSettings pset
Definition: startup.c:33
printTableOpt topt
Definition: print.h:165
void expand_tilde(char **filename)
Definition: common.c:2103
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
unsigned short int pager
Definition: print.h:103
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
#define _(x)
Definition: elog.c:84
bool printHistory(const char *fname, unsigned short int pager)
Definition: input.c:490
static backslashResult exec_command_set ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2110 of file command.c.

References free, ignore_slash_options(), newval, NULL, 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().

2111 {
2112  bool success = true;
2113 
2114  if (active_branch)
2115  {
2116  char *opt0 = psql_scan_slash_option(scan_state,
2117  OT_NORMAL, NULL, false);
2118 
2119  if (!opt0)
2120  {
2121  /* list all variables */
2123  success = true;
2124  }
2125  else
2126  {
2127  /*
2128  * Set variable to the concatenation of the arguments.
2129  */
2130  char *newval;
2131  char *opt;
2132 
2133  opt = psql_scan_slash_option(scan_state,
2134  OT_NORMAL, NULL, false);
2135  newval = pg_strdup(opt ? opt : "");
2136  free(opt);
2137 
2138  while ((opt = psql_scan_slash_option(scan_state,
2139  OT_NORMAL, NULL, false)))
2140  {
2141  newval = pg_realloc(newval, strlen(newval) + strlen(opt) + 1);
2142  strcat(newval, opt);
2143  free(opt);
2144  }
2145 
2146  if (!SetVariable(pset.vars, opt0, newval))
2147  success = false;
2148 
2149  free(newval);
2150  }
2151  free(opt0);
2152  }
2153  else
2154  ignore_slash_options(scan_state);
2155 
2156  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2157 }
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:96
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
#define NULL
Definition: c.h:229
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:2838
VariableSpace vars
Definition: settings.h:116
static backslashResult exec_command_setenv ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2163 of file command.c.

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

Referenced by exec_command().

2165 {
2166  bool success = true;
2167 
2168  if (active_branch)
2169  {
2170  char *envvar = psql_scan_slash_option(scan_state,
2171  OT_NORMAL, NULL, false);
2172  char *envval = psql_scan_slash_option(scan_state,
2173  OT_NORMAL, NULL, false);
2174 
2175  if (!envvar)
2176  {
2177  psql_error("\\%s: missing required argument\n", cmd);
2178  success = false;
2179  }
2180  else if (strchr(envvar, '=') != NULL)
2181  {
2182  psql_error("\\%s: environment variable name must not contain \"=\"\n",
2183  cmd);
2184  success = false;
2185  }
2186  else if (!envval)
2187  {
2188  /* No argument - unset the environment variable */
2189  unsetenv(envvar);
2190  success = true;
2191  }
2192  else
2193  {
2194  /* Set variable to the value of the next argument */
2195  char *newval;
2196 
2197  newval = psprintf("%s=%s", envvar, envval);
2198  putenv(newval);
2199  success = true;
2200 
2201  /*
2202  * Do not free newval here, it will screw up the environment if
2203  * you do. See putenv man page for details. That means we leak a
2204  * bit of memory here, but not enough to worry about.
2205  */
2206  }
2207  free(envvar);
2208  free(envval);
2209  }
2210  else
2211  ignore_slash_options(scan_state);
2212 
2213  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2214 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
#define putenv(x)
Definition: win32.h:411
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
void psql_error(const char *fmt,...)
Definition: common.c:220
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
#define newval
#define unsetenv(x)
Definition: win32.h:412
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
static backslashResult exec_command_sf ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2220 of file command.c.

References ClosePager(), count_lines_in_buf(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), EditableFunction, formatPGVersionNumber(), free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), NULL, 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().

2221 {
2223 
2224  if (active_branch)
2225  {
2226  bool show_linenumbers = (strcmp(cmd, "sf+") == 0);
2227  PQExpBuffer func_buf;
2228  char *func;
2229  Oid foid = InvalidOid;
2230 
2231  func_buf = createPQExpBuffer();
2232  func = psql_scan_slash_option(scan_state,
2233  OT_WHOLE_LINE, NULL, true);
2234  if (pset.sversion < 80400)
2235  {
2236  char sverbuf[32];
2237 
2238  psql_error("The server (version %s) does not support showing function source.\n",
2240  sverbuf, sizeof(sverbuf)));
2241  status = PSQL_CMD_ERROR;
2242  }
2243  else if (!func)
2244  {
2245  psql_error("function name is required\n");
2246  status = PSQL_CMD_ERROR;
2247  }
2248  else if (!lookup_object_oid(EditableFunction, func, &foid))
2249  {
2250  /* error already reported */
2251  status = PSQL_CMD_ERROR;
2252  }
2253  else if (!get_create_object_cmd(EditableFunction, foid, func_buf))
2254  {
2255  /* error already reported */
2256  status = PSQL_CMD_ERROR;
2257  }
2258  else
2259  {
2260  FILE *output;
2261  bool is_pager;
2262 
2263  /* Select output stream: stdout, pager, or file */
2264  if (pset.queryFout == stdout)
2265  {
2266  /* count lines in function to see if pager is needed */
2267  int lineno = count_lines_in_buf(func_buf);
2268 
2269  output = PageOutput(lineno, &(pset.popt.topt));
2270  is_pager = true;
2271  }
2272  else
2273  {
2274  /* use previously set output file, without pager */
2275  output = pset.queryFout;
2276  is_pager = false;
2277  }
2278 
2279  if (show_linenumbers)
2280  {
2281  /*
2282  * lineno "1" should correspond to the first line of the
2283  * function body. We expect that pg_get_functiondef() will
2284  * emit that on a line beginning with "AS ", and that there
2285  * can be no such line before the real start of the function
2286  * body.
2287  */
2288  print_with_linenumbers(output, func_buf->data, "AS ");
2289  }
2290  else
2291  {
2292  /* just send the function definition to output */
2293  fputs(func_buf->data, output);
2294  }
2295 
2296  if (is_pager)
2297  ClosePager(output);
2298  }
2299 
2300  if (func)
2301  free(func);
2302  destroyPQExpBuffer(func_buf);
2303  }
2304  else
2305  ignore_slash_whole_line(scan_state);
2306 
2307  return status;
2308 }
PsqlSettings pset
Definition: startup.c:33
static void output(uint64 loop_count)
void ClosePager(FILE *pagerpipe)
Definition: print.c:2900
printTableOpt topt
Definition: print.h:165
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:4489
static void print_with_linenumbers(FILE *output, char *lines, const char *header_keyword)
Definition: command.c:4807
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:4780
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)
void psql_error(const char *fmt,...)
Definition: common.c:220
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:4549
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
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:2872
static backslashResult exec_command_shell_escape ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2681 of file command.c.

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

Referenced by exec_command().

2682 {
2683  bool success = true;
2684 
2685  if (active_branch)
2686  {
2687  char *opt = psql_scan_slash_option(scan_state,
2688  OT_WHOLE_LINE, NULL, false);
2689 
2690  success = do_shell(opt);
2691  free(opt);
2692  }
2693  else
2694  ignore_slash_whole_line(scan_state);
2695 
2696  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2697 }
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)
static bool success
Definition: pg_basebackup.c:96
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
static bool do_shell(const char *command)
Definition: command.c:4307
static void ignore_slash_whole_line(PsqlScanState scan_state)
Definition: command.c:2872
static backslashResult exec_command_slash_command_help ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2703 of file command.c.

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

Referenced by exec_command().

2704 {
2705  if (active_branch)
2706  {
2707  char *opt0 = psql_scan_slash_option(scan_state,
2708  OT_NORMAL, NULL, false);
2709 
2710  if (!opt0 || strcmp(opt0, "commands") == 0)
2712  else if (strcmp(opt0, "options") == 0)
2714  else if (strcmp(opt0, "variables") == 0)
2716  else
2718 
2719  if (opt0)
2720  free(opt0);
2721  }
2722  else
2723  ignore_slash_options(scan_state);
2724 
2725  return PSQL_CMD_SKIP_LINE;
2726 }
static void usage(void)
Definition: pg_standby.c:503
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:329
unsigned short int pager
Definition: print.h:103
#define free(a)
Definition: header.h:65
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static void ignore_slash_options(PsqlScanState scan_state)
Definition: command.c:2838
void slashUsage(unsigned short int pager)
Definition: help.c:158
static backslashResult exec_command_sv ( PsqlScanState  scan_state,
bool  active_branch,
const char *  cmd 
)
static

Definition at line 2314 of file command.c.

References ClosePager(), count_lines_in_buf(), createPQExpBuffer(), PQExpBufferData::data, destroyPQExpBuffer(), EditableView, formatPGVersionNumber(), free, get_create_object_cmd(), ignore_slash_whole_line(), InvalidOid, lookup_object_oid(), NULL, 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().

2315 {
2317 
2318  if (active_branch)
2319  {
2320  bool show_linenumbers = (strcmp(cmd, "sv+") == 0);
2321  PQExpBuffer view_buf;
2322  char *view;
2323  Oid view_oid = InvalidOid;
2324 
2325  view_buf = createPQExpBuffer();
2326  view = psql_scan_slash_option(scan_state,
2327  OT_WHOLE_LINE, NULL, true);
2328  if (pset.sversion < 70400)
2329  {
2330  char sverbuf[32];
2331 
2332  psql_error("The server (version %s) does not support showing view definitions.\n",
2334  sverbuf, sizeof(sverbuf)));
2335  status = PSQL_CMD_ERROR;
2336  }
2337  else if (!view)
2338  {
2339  psql_error("view name is required\n");
2340  status = PSQL_CMD_ERROR;
2341  }
2342  else if (!lookup_object_oid(EditableView, view, &view_oid))
2343  {
2344  /* error already reported */
2345  status = PSQL_CMD_ERROR;
2346  }
2347  else if (!get_create_object_cmd(EditableView, view_oid, view_buf))
2348  {
2349  /* error already reported */
2350  status = PSQL_CMD_ERROR;
2351  }
2352  else
2353  {
2354  FILE *output;
2355  bool is_pager;
2356 
2357  /* Select output stream: stdout, pager, or file */
2358  if (pset.queryFout == stdout)
2359  {
2360  /* count lines in view to see if pager is needed */
2361  int lineno = count_lines_in_buf(view_buf);
2362 
2363  output = PageOutput(lineno, &(pset.popt.topt));
2364  is_pager = true;
2365  }
2366  else
2367  {
2368  /* use previously set output file, without pager */
2369  output = pset.queryFout;
2370  is_pager = false;
2371  }
2372 
2373  if (show_linenumbers)
2374  {
2375  /* add line numbers, numbering all lines */
2376  print_with_linenumbers(output, view_buf->data, NULL);
2377  }
2378  else
2379  {
2380  /* just send the view definition to output */
2381  fputs(view_buf->data, output);
2382  }
2383 
2384  if (is_pager)
2385  ClosePager(output);
2386  }
2387 
2388  if (view)
2389  free(view);
2390  destroyPQExpBuffer(view_buf);
2391  }
2392  else
2393  ignore_slash_whole_line(scan_state);
2394 
2395  return status;
2396 }
PsqlSettings pset
Definition: startup.c:33
static void output(uint64 loop_count)
void ClosePager(FILE *pagerpipe)
Definition: print.c:2900
printTableOpt topt
Definition: print.h:165
static bool lookup_object_oid(EditableObjectType obj_type, const char *desc, Oid *obj_oid)
Definition: command.c:4489
static void print_with_linenumbers(FILE *output, char *lines, const char *header_keyword)
Definition: command.c:4807
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:4780
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)
void psql_error(const char *fmt,...)
Definition: common.c:220
PQExpBuffer createPQExpBuffer(void)
Definition: pqexpbuffer.c:71
#define InvalidOid
Definition: postgres_ext.h:36
#define free(a)
Definition: header.h:65
enum _backslashResult backslashResult
#define NULL
Definition: c.h:229
printQueryOpt popt
Definition: settings.h:91
static bool get_create_object_cmd(EditableObjectType obj_type, Oid oid, PQExpBuffer buf)
Definition: command.c:4549
static void static void status(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:224
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:2872
static backslashResult exec_command_t ( PsqlScanState  scan_state,
bool  active_branch 
)
static

Definition at line 2402 of file command.c.

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

Referenced by exec_command().

2403 {
2404  bool success = true;
2405 
2406  if (active_branch)
2407  {
2408  char *opt = psql_scan_slash_option(scan_state,
2409  OT_NORMAL, NULL, true);
2410 
2411  success = do_pset("tuples_only", opt, &pset.popt, pset.quiet);
2412  free(opt);
2413  }
2414  else
2415  ignore_slash_options(scan_state);
2416 
2417  return success ? PSQL_CMD_SKIP_LINE : PSQL_CMD_ERROR;
2418 }
PsqlSettings pset
Definition: startup.c:33
char * psql_scan_slash_option(PsqlScanState state, enum slash_option_type type, char *quote, bool semicolon)