PostgreSQL Source Code  git master
startup.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "command.h"
#include "common.h"
#include "common/logging.h"
#include "common/string.h"
#include "describe.h"
#include "fe_utils/print.h"
#include "getopt_long.h"
#include "help.h"
#include "input.h"
#include "mainloop.h"
#include "settings.h"
Include dependency graph for startup.c:

Go to the source code of this file.

Data Structures

struct  SimpleActionListCell
 
struct  SimpleActionList
 
struct  adhoc_opts
 

Macros

#define SYSPSQLRC   "psqlrc"
 
#define PSQLRC   ".psqlrc"
 
#define NOPAGER   0
 
#define PARAMS_ARRAY_SIZE   8
 

Typedefs

typedef struct SimpleActionListCell SimpleActionListCell
 
typedef struct SimpleActionList SimpleActionList
 

Enumerations

enum  _actions { ACT_SINGLE_QUERY, ACT_SINGLE_SLASH, ACT_FILE }
 

Functions

static void parse_psql_options (int argc, char *argv[], struct adhoc_opts *options)
 
static void simple_action_list_append (SimpleActionList *list, enum _actions action, const char *val)
 
static void process_psqlrc (char *argv0)
 
static void process_psqlrc_file (char *filename)
 
static void showVersion (void)
 
static void EstablishVariableSpace (void)
 
static void log_pre_callback (void)
 
static void log_locus_callback (const char **filename, uint64 *lineno)
 
int main (int argc, char *argv[])
 
static char * bool_substitute_hook (char *newval)
 
static bool autocommit_hook (const char *newval)
 
static bool on_error_stop_hook (const char *newval)
 
static bool quiet_hook (const char *newval)
 
static bool singleline_hook (const char *newval)
 
static bool singlestep_hook (const char *newval)
 
static char * fetch_count_substitute_hook (char *newval)
 
static bool fetch_count_hook (const char *newval)
 
static bool histfile_hook (const char *newval)
 
static char * histsize_substitute_hook (char *newval)
 
static bool histsize_hook (const char *newval)
 
static char * ignoreeof_substitute_hook (char *newval)
 
static bool ignoreeof_hook (const char *newval)
 
static char * echo_substitute_hook (char *newval)
 
static bool echo_hook (const char *newval)
 
static bool echo_hidden_hook (const char *newval)
 
static bool on_error_rollback_hook (const char *newval)
 
static char * comp_keyword_case_substitute_hook (char *newval)
 
static bool comp_keyword_case_hook (const char *newval)
 
static char * histcontrol_substitute_hook (char *newval)
 
static bool histcontrol_hook (const char *newval)
 
static bool prompt1_hook (const char *newval)
 
static bool prompt2_hook (const char *newval)
 
static bool prompt3_hook (const char *newval)
 
static char * verbosity_substitute_hook (char *newval)
 
static bool verbosity_hook (const char *newval)
 
static char * show_context_substitute_hook (char *newval)
 
static bool show_context_hook (const char *newval)
 
static bool hide_tableam_hook (const char *newval)
 

Variables

PsqlSettings pset
 

Macro Definition Documentation

◆ NOPAGER

#define NOPAGER   0

Definition at line 89 of file startup.c.

Referenced by main(), and parse_psql_options().

◆ PARAMS_ARRAY_SIZE

#define PARAMS_ARRAY_SIZE   8

Referenced by main().

◆ PSQLRC

#define PSQLRC   ".psqlrc"

Definition at line 36 of file startup.c.

Referenced by process_psqlrc().

◆ SYSPSQLRC

#define SYSPSQLRC   "psqlrc"

Definition at line 35 of file startup.c.

Referenced by process_psqlrc().

Typedef Documentation

◆ SimpleActionList

◆ SimpleActionListCell

Enumeration Type Documentation

◆ _actions

enum _actions
Enumerator
ACT_SINGLE_QUERY 
ACT_SINGLE_SLASH 
ACT_FILE 

Definition at line 46 of file startup.c.

Function Documentation

◆ autocommit_hook()

static bool autocommit_hook ( const char *  newval)
static

Definition at line 859 of file startup.c.

References _psqlSettings::autocommit, and ParseVariableBool().

Referenced by EstablishVariableSpace().

860 {
861  return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
862 }
bool autocommit
Definition: settings.h:132
PsqlSettings pset
Definition: startup.c:32
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ bool_substitute_hook()

static char* bool_substitute_hook ( char *  newval)
static

Definition at line 842 of file startup.c.

References newval, pg_free(), and pg_strdup().

Referenced by EstablishVariableSpace().

843 {
844  if (newval == NULL)
845  {
846  /* "\unset FOO" becomes "\set FOO off" */
847  newval = pg_strdup("off");
848  }
849  else if (newval[0] == '\0')
850  {
851  /* "\set FOO" becomes "\set FOO on" */
852  pg_free(newval);
853  newval = pg_strdup("on");
854  }
855  return newval;
856 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval
void pg_free(void *ptr)
Definition: fe_memutils.c:105

◆ comp_keyword_case_hook()

static bool comp_keyword_case_hook ( const char *  newval)
static

Definition at line 1032 of file startup.c.

References Assert, _psqlSettings::comp_case, pg_strcasecmp(), PSQL_COMP_CASE_LOWER, PSQL_COMP_CASE_PRESERVE_LOWER, PSQL_COMP_CASE_PRESERVE_UPPER, PSQL_COMP_CASE_UPPER, and PsqlVarEnumError().

Referenced by EstablishVariableSpace().

1033 {
1034  Assert(newval != NULL); /* else substitute hook messed up */
1035  if (pg_strcasecmp(newval, "preserve-upper") == 0)
1037  else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1039  else if (pg_strcasecmp(newval, "upper") == 0)
1041  else if (pg_strcasecmp(newval, "lower") == 0)
1043  else
1044  {
1045  PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1046  "lower, upper, preserve-lower, preserve-upper");
1047  return false;
1048  }
1049  return true;
1050 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
PSQL_COMP_CASE comp_case
Definition: settings.h:144
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:32
#define Assert(condition)
Definition: c.h:746
#define newval

◆ comp_keyword_case_substitute_hook()

static char* comp_keyword_case_substitute_hook ( char *  newval)
static

Definition at line 1024 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

1025 {
1026  if (newval == NULL)
1027  newval = pg_strdup("preserve-upper");
1028  return newval;
1029 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ echo_hidden_hook()

static bool echo_hidden_hook ( const char *  newval)
static

Definition at line 982 of file startup.c.

References Assert, _psqlSettings::echo_hidden, ParseVariableBool(), pg_strcasecmp(), PSQL_ECHO_HIDDEN_NOEXEC, PSQL_ECHO_HIDDEN_OFF, PSQL_ECHO_HIDDEN_ON, and PsqlVarEnumError().

Referenced by EstablishVariableSpace().

983 {
984  Assert(newval != NULL); /* else substitute hook messed up */
985  if (pg_strcasecmp(newval, "noexec") == 0)
987  else
988  {
989  bool on_off;
990 
991  if (ParseVariableBool(newval, NULL, &on_off))
993  else
994  {
995  PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
996  return false;
997  }
998  }
999  return true;
1000 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:142
PsqlSettings pset
Definition: startup.c:32
#define Assert(condition)
Definition: c.h:746
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ echo_hook()

static bool echo_hook ( const char *  newval)
static

Definition at line 962 of file startup.c.

References Assert, _psqlSettings::echo, pg_strcasecmp(), PSQL_ECHO_ALL, PSQL_ECHO_ERRORS, PSQL_ECHO_NONE, PSQL_ECHO_QUERIES, and PsqlVarEnumError().

Referenced by EstablishVariableSpace().

963 {
964  Assert(newval != NULL); /* else substitute hook messed up */
965  if (pg_strcasecmp(newval, "queries") == 0)
967  else if (pg_strcasecmp(newval, "errors") == 0)
969  else if (pg_strcasecmp(newval, "all") == 0)
971  else if (pg_strcasecmp(newval, "none") == 0)
973  else
974  {
975  PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
976  return false;
977  }
978  return true;
979 }
PSQL_ECHO echo
Definition: settings.h:141
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:32
#define Assert(condition)
Definition: c.h:746
#define newval

◆ echo_substitute_hook()

static char* echo_substitute_hook ( char *  newval)
static

Definition at line 954 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

955 {
956  if (newval == NULL)
957  newval = pg_strdup("none");
958  return newval;
959 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ EstablishVariableSpace()

static void EstablishVariableSpace ( void  )
static

Definition at line 1169 of file startup.c.

References autocommit_hook(), bool_substitute_hook(), comp_keyword_case_hook(), comp_keyword_case_substitute_hook(), CreateVariableSpace(), echo_hidden_hook(), echo_hook(), echo_substitute_hook(), fetch_count_hook(), fetch_count_substitute_hook(), hide_tableam_hook(), histcontrol_hook(), histcontrol_substitute_hook(), histfile_hook(), histsize_hook(), histsize_substitute_hook(), ignoreeof_hook(), ignoreeof_substitute_hook(), on_error_rollback_hook(), on_error_stop_hook(), prompt1_hook(), prompt2_hook(), prompt3_hook(), quiet_hook(), SetVariableHooks(), show_context_hook(), show_context_substitute_hook(), singleline_hook(), singlestep_hook(), _psqlSettings::vars, verbosity_hook(), and verbosity_substitute_hook().

Referenced by main().

1170 {
1172 
1173  SetVariableHooks(pset.vars, "AUTOCOMMIT",
1175  autocommit_hook);
1176  SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1179  SetVariableHooks(pset.vars, "QUIET",
1181  quiet_hook);
1182  SetVariableHooks(pset.vars, "SINGLELINE",
1184  singleline_hook);
1185  SetVariableHooks(pset.vars, "SINGLESTEP",
1187  singlestep_hook);
1188  SetVariableHooks(pset.vars, "FETCH_COUNT",
1191  SetVariableHooks(pset.vars, "HISTFILE",
1192  NULL,
1193  histfile_hook);
1194  SetVariableHooks(pset.vars, "HISTSIZE",
1196  histsize_hook);
1197  SetVariableHooks(pset.vars, "IGNOREEOF",
1199  ignoreeof_hook);
1200  SetVariableHooks(pset.vars, "ECHO",
1202  echo_hook);
1203  SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1206  SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1209  SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1212  SetVariableHooks(pset.vars, "HISTCONTROL",
1215  SetVariableHooks(pset.vars, "PROMPT1",
1216  NULL,
1217  prompt1_hook);
1218  SetVariableHooks(pset.vars, "PROMPT2",
1219  NULL,
1220  prompt2_hook);
1221  SetVariableHooks(pset.vars, "PROMPT3",
1222  NULL,
1223  prompt3_hook);
1224  SetVariableHooks(pset.vars, "VERBOSITY",
1226  verbosity_hook);
1227  SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1230  SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1233 }
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1103
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:865
static bool echo_hook(const char *newval)
Definition: startup.c:962
static bool quiet_hook(const char *newval)
Definition: startup.c:871
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1061
static char * bool_substitute_hook(char *newval)
Definition: startup.c:842
VariableSpace CreateVariableSpace(void)
Definition: variables.c:51
static bool prompt2_hook(const char *newval)
Definition: startup.c:1089
static bool hide_tableam_hook(const char *newval)
Definition: startup.c:1163
static bool singleline_hook(const char *newval)
Definition: startup.c:877
static char * echo_substitute_hook(char *newval)
Definition: startup.c:954
static bool autocommit_hook(const char *newval)
Definition: startup.c:859
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:1024
static bool fetch_count_hook(const char *newval)
Definition: startup.c:897
static bool singlestep_hook(const char *newval)
Definition: startup.c:883
static bool show_context_hook(const char *newval)
Definition: startup.c:1142
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:1003
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1134
static bool histsize_hook(const char *newval)
Definition: startup.c:921
PsqlSettings pset
Definition: startup.c:32
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:913
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition: variables.c:315
static bool comp_keyword_case_hook(const char *newval)
Definition: startup.c:1032
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:1053
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:948
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:927
static bool prompt1_hook(const char *newval)
Definition: startup.c:1082
static bool histfile_hook(const char *newval)
Definition: startup.c:903
static bool verbosity_hook(const char *newval)
Definition: startup.c:1111
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:889
static bool prompt3_hook(const char *newval)
Definition: startup.c:1096
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:982
VariableSpace vars
Definition: settings.h:118

◆ fetch_count_hook()

static bool fetch_count_hook ( const char *  newval)
static

Definition at line 897 of file startup.c.

References _psqlSettings::fetch_count, and ParseVariableNum().

Referenced by EstablishVariableSpace().

898 {
899  return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
900 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
int fetch_count
Definition: settings.h:138
PsqlSettings pset
Definition: startup.c:32
#define newval

◆ fetch_count_substitute_hook()

static char* fetch_count_substitute_hook ( char *  newval)
static

Definition at line 889 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

890 {
891  if (newval == NULL)
892  newval = pg_strdup("0");
893  return newval;
894 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ hide_tableam_hook()

static bool hide_tableam_hook ( const char *  newval)
static

Definition at line 1163 of file startup.c.

References _psqlSettings::hide_tableam, and ParseVariableBool().

Referenced by EstablishVariableSpace().

1164 {
1165  return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1166 }
PsqlSettings pset
Definition: startup.c:32
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
bool hide_tableam
Definition: settings.h:137

◆ histcontrol_hook()

static bool histcontrol_hook ( const char *  newval)
static

Definition at line 1061 of file startup.c.

References Assert, hctl_ignoreboth, hctl_ignoredups, hctl_ignorespace, hctl_none, _psqlSettings::histcontrol, pg_strcasecmp(), and PsqlVarEnumError().

Referenced by EstablishVariableSpace().

1062 {
1063  Assert(newval != NULL); /* else substitute hook messed up */
1064  if (pg_strcasecmp(newval, "ignorespace") == 0)
1066  else if (pg_strcasecmp(newval, "ignoredups") == 0)
1068  else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1070  else if (pg_strcasecmp(newval, "none") == 0)
1072  else
1073  {
1074  PsqlVarEnumError("HISTCONTROL", newval,
1075  "none, ignorespace, ignoredups, ignoreboth");
1076  return false;
1077  }
1078  return true;
1079 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:32
#define Assert(condition)
Definition: c.h:746
#define newval
HistControl histcontrol
Definition: settings.h:145

◆ histcontrol_substitute_hook()

static char* histcontrol_substitute_hook ( char *  newval)
static

Definition at line 1053 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

1054 {
1055  if (newval == NULL)
1056  newval = pg_strdup("none");
1057  return newval;
1058 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ histfile_hook()

static bool histfile_hook ( const char *  newval)
static

Definition at line 903 of file startup.c.

Referenced by EstablishVariableSpace().

904 {
905  /*
906  * Someday we might try to validate the filename, but for now, this is
907  * just a placeholder to ensure HISTFILE is known to tab completion.
908  */
909  return true;
910 }

◆ histsize_hook()

static bool histsize_hook ( const char *  newval)
static

Definition at line 921 of file startup.c.

References _psqlSettings::histsize, and ParseVariableNum().

Referenced by EstablishVariableSpace().

922 {
923  return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
924 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
PsqlSettings pset
Definition: startup.c:32
#define newval

◆ histsize_substitute_hook()

static char* histsize_substitute_hook ( char *  newval)
static

Definition at line 913 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

914 {
915  if (newval == NULL)
916  newval = pg_strdup("500");
917  return newval;
918 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ ignoreeof_hook()

static bool ignoreeof_hook ( const char *  newval)
static

Definition at line 948 of file startup.c.

References _psqlSettings::ignoreeof, and ParseVariableNum().

Referenced by EstablishVariableSpace().

949 {
950  return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
951 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
PsqlSettings pset
Definition: startup.c:32
#define newval

◆ ignoreeof_substitute_hook()

static char* ignoreeof_substitute_hook ( char *  newval)
static

Definition at line 927 of file startup.c.

References newval, ParseVariableNum(), and pg_strdup().

Referenced by EstablishVariableSpace().

928 {
929  int dummy;
930 
931  /*
932  * This tries to mimic the behavior of bash, to wit "If set, the value is
933  * the number of consecutive EOF characters which must be typed as the
934  * first characters on an input line before bash exits. If the variable
935  * exists but does not have a numeric value, or has no value, the default
936  * value is 10. If it does not exist, EOF signifies the end of input to
937  * the shell." Unlike bash, however, we insist on the stored value
938  * actually being a valid integer.
939  */
940  if (newval == NULL)
941  newval = pg_strdup("0");
942  else if (!ParseVariableNum(newval, NULL, &dummy))
943  newval = pg_strdup("10");
944  return newval;
945 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ log_locus_callback()

static void log_locus_callback ( const char **  filename,
uint64 *  lineno 
)
static

Definition at line 99 of file startup.c.

References _psqlSettings::inputfile, and _psqlSettings::lineno.

Referenced by main().

100 {
101  if (pset.inputfile)
102  {
104  *lineno = pset.lineno;
105  }
106  else
107  {
108  *filename = NULL;
109  *lineno = 0;
110  }
111 }
char * inputfile
Definition: settings.h:110
PsqlSettings pset
Definition: startup.c:32
static char * filename
Definition: pg_dumpall.c:91
uint64 lineno
Definition: settings.h:111

◆ log_pre_callback()

static void log_pre_callback ( void  )
static

Definition at line 92 of file startup.c.

References _psqlSettings::queryFout, and generate_unaccent_rules::stdout.

Referenced by main().

93 {
95  fflush(pset.queryFout);
96 }
FILE * queryFout
Definition: settings.h:84
PsqlSettings pset
Definition: startup.c:32

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 119 of file startup.c.

References _, ACT_FILE, ACT_SINGLE_QUERY, ACT_SINGLE_SLASH, SimpleActionListCell::action, adhoc_opts::actions, Assert, printTableOpt::border, conditional_stack_create(), conditional_stack_destroy(), CONNECTION_BAD, connection_warnings(), _psqlSettings::copyStream, CppAsString2, printTableOpt::csvFieldSep, _psqlSettings::cur_cmd_interactive, _psqlSettings::cur_cmd_source, _psqlSettings::db, adhoc_opts::dbname, _psqlSettings::dead_conn, DEFAULT_CSV_FIELD_SEP, DEFAULT_FIELD_SEP, printTableOpt::default_footer, DEFAULT_PROMPT1, DEFAULT_PROMPT2, DEFAULT_PROMPT3, DEFAULT_RECORD_SEP, _psqlSettings::echo, _psqlSettings::encoding, printTableOpt::env_columns, error(), EstablishVariableSpace(), EXIT_BADCONN, EXIT_FAILURE, EXIT_SUCCESS, EXIT_USER, printTableOpt::fieldSep, printTableOpt::format, free, get_progname(), _psqlSettings::getPassword, HandleSlashCmds(), SimpleActionList::head, adhoc_opts::host, initializeInput(), _psqlSettings::last_error_result, adhoc_opts::list_dbs, listAllDbs(), log_locus_callback(), log_pre_callback(), _psqlSettings::logfile, adhoc_opts::logfilename, MainLoop(), SimpleActionListCell::next, adhoc_opts::no_psqlrc, adhoc_opts::no_readline, NOPAGER, NoticeProcessor(), _psqlSettings::notty, _psqlSettings::on_error_stop, printTableOpt::pager, printTableOpt::pager_min_lines, PARAMS_ARRAY_SIZE, parse_psql_options(), password, pg_log_error, pg_log_fatal, PG_LOG_FLAG_TERSE, pg_logging_config(), pg_logging_init(), pg_logging_set_locus_callback(), pg_logging_set_pre_callback(), pg_malloc(), pg_strdup(), PG_TEXTDOMAIN, _psqlSettings::popt, adhoc_opts::port, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQenv2encoding(), PQerrorMessage(), PQfinish(), PQsetNoticeProcessor(), PQstatus(), PQuser(), PRINT_ALIGNED, printf, process_file(), process_psqlrc(), _psqlSettings::progname, psprintf(), PSQL_CMD_ERROR, PSQL_ECHO_ALL, psql_scan_create(), psql_scan_destroy(), psql_scan_set_passthrough(), psql_scan_setup(), psql_setup_cancel_handler(), PSQLexec(), psqlscan_callbacks, _psqlSettings::queryFout, _psqlSettings::queryFoutPipe, _psqlSettings::quiet, printTableOpt::recordSep, refresh_utf8format(), SendQuery(), separator::separator, separator::separator_zero, set_pglocale_pgservice(), setDecimalLocale(), setQFout(), SetVariable(), SetVariableBool(), showVersion(), simple_action_list_append(), simple_prompt(), adhoc_opts::single_txn, standard_strings(), printTableOpt::start_table, generate_unaccent_rules::stdout, printTableOpt::stop_table, success, SyncVariables(), printQueryOpt::topt, TRI_DEFAULT, TRI_NO, TRI_YES, printTableOpt::unicode_border_linestyle, printTableOpt::unicode_column_linestyle, printTableOpt::unicode_header_linestyle, UNICODE_LINESTYLE_SINGLE, usage(), adhoc_opts::username, SimpleActionListCell::val, values, and _psqlSettings::vars.

120 {
121  struct adhoc_opts options;
122  int successResult;
123  char *password = NULL;
124  bool new_pass;
125 
126  pg_logging_init(argv[0]);
129  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
130 
131  if (argc > 1)
132  {
133  if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
134  {
135  usage(NOPAGER);
136  exit(EXIT_SUCCESS);
137  }
138  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
139  {
140  showVersion();
141  exit(EXIT_SUCCESS);
142  }
143  }
144 
145  pset.progname = get_progname(argv[0]);
146 
147  pset.db = NULL;
148  pset.dead_conn = NULL;
152  pset.queryFoutPipe = false;
153  pset.copyStream = NULL;
154  pset.last_error_result = NULL;
155  pset.cur_cmd_source = stdin;
156  pset.cur_cmd_interactive = false;
157 
158  /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
160  pset.popt.topt.border = 1;
161  pset.popt.topt.pager = 1;
163  pset.popt.topt.start_table = true;
164  pset.popt.topt.stop_table = true;
165  pset.popt.topt.default_footer = true;
166 
168  pset.popt.topt.csvFieldSep[1] = '\0';
169 
173 
175 
176  /* We must get COLUMNS here before readline() sets it */
177  pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
178 
179  pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
180 
182 
184 
185  /* Create variables showing psql version number */
186  SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
187  SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
188  SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
189 
190  /* Initialize variables for last error */
191  SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
192  SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
193 
194  /* Default values for variables (that don't match the result of \unset) */
195  SetVariableBool(pset.vars, "AUTOCOMMIT");
196  SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
197  SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
198  SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
199 
200  parse_psql_options(argc, argv, &options);
201 
202  /*
203  * If no action was specified and we're in non-interactive mode, treat it
204  * as if the user had specified "-f -". This lets single-transaction mode
205  * work in this case.
206  */
207  if (options.actions.head == NULL && pset.notty)
208  simple_action_list_append(&options.actions, ACT_FILE, NULL);
209 
210  /* Bail out if -1 was specified but will be ignored. */
211  if (options.single_txn && options.actions.head == NULL)
212  {
213  pg_log_fatal("-1 can only be used in non-interactive mode");
214  exit(EXIT_FAILURE);
215  }
216 
219  {
222  }
225  {
228  }
229 
230  if (pset.getPassword == TRI_YES)
231  {
232  /*
233  * We can't be sure yet of the username that will be used, so don't
234  * offer a potentially wrong one. Typical uses of this option are
235  * noninteractive anyway.
236  */
237  password = simple_prompt("Password: ", false);
238  }
239 
240  /* loop until we have a password if requested by backend */
241  do
242  {
243 #define PARAMS_ARRAY_SIZE 8
244  const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
245  const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
246 
247  keywords[0] = "host";
248  values[0] = options.host;
249  keywords[1] = "port";
250  values[1] = options.port;
251  keywords[2] = "user";
252  values[2] = options.username;
253  keywords[3] = "password";
254  values[3] = password;
255  keywords[4] = "dbname"; /* see do_connect() */
256  values[4] = (options.list_dbs && options.dbname == NULL) ?
257  "postgres" : options.dbname;
258  keywords[5] = "fallback_application_name";
259  values[5] = pset.progname;
260  keywords[6] = "client_encoding";
261  values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
262  keywords[7] = NULL;
263  values[7] = NULL;
264 
265  new_pass = false;
266  pset.db = PQconnectdbParams(keywords, values, true);
267  free(keywords);
268  free(values);
269 
270  if (PQstatus(pset.db) == CONNECTION_BAD &&
272  !password &&
274  {
275  /*
276  * Before closing the old PGconn, extract the user name that was
277  * actually connected with --- it might've come out of a URI or
278  * connstring "database name" rather than options.username.
279  */
280  const char *realusername = PQuser(pset.db);
281  char *password_prompt;
282 
283  if (realusername && realusername[0])
284  password_prompt = psprintf(_("Password for user %s: "),
285  realusername);
286  else
287  password_prompt = pg_strdup(_("Password: "));
288  PQfinish(pset.db);
289 
290  password = simple_prompt(password_prompt, false);
291  free(password_prompt);
292  new_pass = true;
293  }
294  } while (new_pass);
295 
296  if (PQstatus(pset.db) == CONNECTION_BAD)
297  {
298  pg_log_error("could not connect to server: %s", PQerrorMessage(pset.db));
299  PQfinish(pset.db);
300  exit(EXIT_BADCONN);
301  }
302 
304 
306 
307  SyncVariables();
308 
309  if (options.list_dbs)
310  {
311  int success;
312 
313  if (!options.no_psqlrc)
314  process_psqlrc(argv[0]);
315 
316  success = listAllDbs(NULL, false);
317  PQfinish(pset.db);
318  exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
319  }
320 
321  if (options.logfilename)
322  {
323  pset.logfile = fopen(options.logfilename, "a");
324  if (!pset.logfile)
325  {
326  pg_log_fatal("could not open log file \"%s\": %m",
327  options.logfilename);
328  exit(EXIT_FAILURE);
329  }
330  }
331 
332  if (!options.no_psqlrc)
333  process_psqlrc(argv[0]);
334 
335  /*
336  * If any actions were given by user, process them in the order in which
337  * they were specified. Note single_txn is only effective in this mode.
338  */
339  if (options.actions.head != NULL)
340  {
341  PGresult *res;
342  SimpleActionListCell *cell;
343 
344  successResult = EXIT_SUCCESS; /* silence compiler */
345 
346  if (options.single_txn)
347  {
348  if ((res = PSQLexec("BEGIN")) == NULL)
349  {
350  if (pset.on_error_stop)
351  {
352  successResult = EXIT_USER;
353  goto error;
354  }
355  }
356  else
357  PQclear(res);
358  }
359 
360  for (cell = options.actions.head; cell; cell = cell->next)
361  {
362  if (cell->action == ACT_SINGLE_QUERY)
363  {
365 
366  if (pset.echo == PSQL_ECHO_ALL)
367  puts(cell->val);
368 
369  successResult = SendQuery(cell->val)
371  }
372  else if (cell->action == ACT_SINGLE_SLASH)
373  {
374  PsqlScanState scan_state;
375  ConditionalStack cond_stack;
376 
378 
379  if (pset.echo == PSQL_ECHO_ALL)
380  puts(cell->val);
381 
382  scan_state = psql_scan_create(&psqlscan_callbacks);
383  psql_scan_setup(scan_state,
384  cell->val, strlen(cell->val),
386  cond_stack = conditional_stack_create();
387  psql_scan_set_passthrough(scan_state, (void *) cond_stack);
388 
389  successResult = HandleSlashCmds(scan_state,
390  cond_stack,
391  NULL,
392  NULL) != PSQL_CMD_ERROR
394 
395  psql_scan_destroy(scan_state);
396  conditional_stack_destroy(cond_stack);
397  }
398  else if (cell->action == ACT_FILE)
399  {
400  successResult = process_file(cell->val, false);
401  }
402  else
403  {
404  /* should never come here */
405  Assert(false);
406  }
407 
408  if (successResult != EXIT_SUCCESS && pset.on_error_stop)
409  break;
410  }
411 
412  if (options.single_txn)
413  {
414  if ((res = PSQLexec("COMMIT")) == NULL)
415  {
416  if (pset.on_error_stop)
417  {
418  successResult = EXIT_USER;
419  goto error;
420  }
421  }
422  else
423  PQclear(res);
424  }
425 
426 error:
427  ;
428  }
429 
430  /*
431  * or otherwise enter interactive main loop
432  */
433  else
434  {
436  connection_warnings(true);
437  if (!pset.quiet)
438  printf(_("Type \"help\" for help.\n\n"));
439  initializeInput(options.no_readline ? 0 : 1);
440  successResult = MainLoop(stdin);
441  }
442 
443  /* clean up */
444  if (pset.logfile)
445  fclose(pset.logfile);
446  if (pset.db)
447  PQfinish(pset.db);
448  if (pset.dead_conn)
450  setQFout(NULL);
451 
452  return successResult;
453 }
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:461
int env_columns
Definition: print.h:123
#define NOPAGER
Definition: startup.c:89
PSQL_ECHO echo
Definition: settings.h:141
static void process_psqlrc(char *argv0)
Definition: startup.c:755
static void usage(void)
Definition: pg_standby.c:589
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:6669
static void log_pre_callback(void)
Definition: startup.c:92
#define EXIT_SUCCESS
Definition: settings.h:157
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6845
void SyncVariables(void)
Definition: command.c:3575
static void error(void)
Definition: sql-dyntest.c:147
static void EstablishVariableSpace(void)
Definition: startup.c:1169
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
enum _actions action
Definition: startup.c:56
const char * get_progname(const char *argv0)
Definition: path.c:453
#define pg_log_error(...)
Definition: logging.h:80
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
void initializeInput(int flags)
Definition: input.c:346
void pg_logging_init(const char *argv0)
Definition: logging.c:81
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool start_table
Definition: print.h:111
enum printFormat format
Definition: print.h:101
printTableOpt topt
Definition: print.h:169
#define DEFAULT_PROMPT3
Definition: settings.h:28
bool queryFoutPipe
Definition: settings.h:85
void pg_logging_set_pre_callback(void(*cb)(void))
Definition: logging.c:185
FILE * queryFout
Definition: settings.h:84
unicode_linestyle unicode_header_linestyle
Definition: print.h:127
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4174
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:367
#define printf(...)
Definition: port.h:221
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
bool on_error_stop
Definition: settings.h:133
#define DEFAULT_PROMPT2
Definition: settings.h:27
char * username
Definition: oid2name.c:45
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition: startup.c:729
static void log_locus_callback(const char **filename, uint64 *lineno)
Definition: startup.c:99
bool separator_zero
Definition: print.h:96
FILE * copyStream
Definition: settings.h:87
unicode_linestyle unicode_border_linestyle
Definition: print.h:125
static void showVersion(void)
Definition: startup.c:823
static void process_file(const char *filename, int weight)
Definition: pgbench.c:4933
FILE * cur_cmd_source
Definition: settings.h:105
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:647
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:6527
#define EXIT_BADCONN
Definition: settings.h:164
unsigned short int border
Definition: print.h:104
bool cur_cmd_interactive
Definition: settings.h:107
unicode_linestyle unicode_column_linestyle
Definition: print.h:126
const PsqlScanCallbacks psqlscan_callbacks
Definition: mainloop.c:20
#define DEFAULT_RECORD_SEP
Definition: settings.h:16
#define EXIT_USER
Definition: settings.h:166
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * password
Definition: streamutil.c:53
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition: logging.c:191
struct SimpleActionListCell * next
Definition: startup.c:55
char csvFieldSep[2]
Definition: print.h:118
int PQenv2encoding(void)
Definition: fe-misc.c:1250
#define CppAsString2(x)
Definition: c.h:223
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1157
ConditionalStack conditional_stack_create(void)
Definition: conditional.c:18
#define DEFAULT_CSV_FIELD_SEP
Definition: settings.h:14
bool setQFout(const char *fname)
Definition: common.c:85
PsqlSettings pset
Definition: startup.c:32
void psql_scan_destroy(PsqlScanState state)
PGresult * last_error_result
Definition: settings.h:89
FILE * logfile
Definition: settings.h:116
unsigned short int pager
Definition: print.h:106
char * dbname
Definition: oid2name.c:42
void PQclear(PGresult *res)
Definition: fe-exec.c:694
bool stop_table
Definition: print.h:112
bool standard_strings(void)
Definition: common.c:2177
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:116
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:220
int pager_min_lines
Definition: print.h:108
bool default_footer
Definition: print.h:113
#define Assert(condition)
Definition: c.h:746
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
#define PARAMS_ARRAY_SIZE
const char * progname
Definition: settings.h:109
int MainLoop(FILE *source)
Definition: mainloop.c:33
printQueryOpt popt
Definition: settings.h:91
void pg_logging_config(int new_flags)
Definition: logging.c:155
enum trivalue getPassword
Definition: settings.h:104
void setDecimalLocale(void)
Definition: print.c:3509
PGconn * dead_conn
Definition: settings.h:125
#define DEFAULT_PROMPT1
Definition: settings.h:26
void conditional_stack_destroy(ConditionalStack cstack)
Definition: conditional.c:30
static Datum values[MAXATTR]
Definition: bootstrap.c:165
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6703
#define PG_LOG_FLAG_TERSE
Definition: logging.h:64
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:434
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
void psql_setup_cancel_handler(void)
Definition: common.c:266
char * port
Definition: oid2name.c:44
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3559
#define EXIT_FAILURE
Definition: settings.h:161
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
char * separator
Definition: print.h:95
bool SendQuery(const char *query)
Definition: common.c:1183
void connection_warnings(bool in_startup)
Definition: command.c:3452
struct separator recordSep
Definition: print.h:117
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:205
int encoding
Definition: settings.h:83
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:6616
static bool success
Definition: initdb.c:162
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
#define _(x)
Definition: elog.c:88
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:864
PGresult * PSQLexec(const char *query)
Definition: common.c:540
VariableSpace vars
Definition: settings.h:118
#define pg_log_fatal(...)
Definition: logging.h:76

◆ on_error_rollback_hook()

static bool on_error_rollback_hook ( const char *  newval)
static

Definition at line 1003 of file startup.c.

References Assert, _psqlSettings::on_error_rollback, ParseVariableBool(), pg_strcasecmp(), PSQL_ERROR_ROLLBACK_INTERACTIVE, PSQL_ERROR_ROLLBACK_OFF, PSQL_ERROR_ROLLBACK_ON, and PsqlVarEnumError().

Referenced by EstablishVariableSpace().

1004 {
1005  Assert(newval != NULL); /* else substitute hook messed up */
1006  if (pg_strcasecmp(newval, "interactive") == 0)
1008  else
1009  {
1010  bool on_off;
1011 
1012  if (ParseVariableBool(newval, NULL, &on_off))
1014  else
1015  {
1016  PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1017  return false;
1018  }
1019  }
1020  return true;
1021 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:143
PsqlSettings pset
Definition: startup.c:32
#define Assert(condition)
Definition: c.h:746
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ on_error_stop_hook()

static bool on_error_stop_hook ( const char *  newval)
static

Definition at line 865 of file startup.c.

References _psqlSettings::on_error_stop, and ParseVariableBool().

Referenced by EstablishVariableSpace().

866 {
867  return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
868 }
bool on_error_stop
Definition: settings.h:133
PsqlSettings pset
Definition: startup.c:32
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ parse_psql_options()

static void parse_psql_options ( int  argc,
char *  argv[],
struct adhoc_opts options 
)
static

Definition at line 461 of file startup.c.

References _, ACT_FILE, ACT_SINGLE_QUERY, ACT_SINGLE_SLASH, adhoc_opts::actions, adhoc_opts::dbname, DeleteVariable(), do_pset(), EXIT_FAILURE, EXIT_SUCCESS, printTableOpt::expanded, printTableOpt::fieldSep, printTableOpt::format, fprintf, free, getopt_long(), _psqlSettings::getPassword, helpVariables(), adhoc_opts::host, adhoc_opts::list_dbs, adhoc_opts::logfilename, no_argument, adhoc_opts::no_psqlrc, adhoc_opts::no_readline, NOPAGER, optarg, optind, optional_argument, pg_log_fatal, pg_log_warning, pg_strdup(), _psqlSettings::popt, adhoc_opts::port, PRINT_CSV, PRINT_HTML, PRINT_UNALIGNED, _psqlSettings::progname, _psqlSettings::quiet, printTableOpt::recordSep, required_argument, separator::separator, separator::separator_zero, setQFout(), SetVariable(), SetVariableBool(), showVersion(), simple_action_list_append(), adhoc_opts::single_txn, slashUsage(), printTableOpt::tableAttr, printQueryOpt::topt, TRI_NO, TRI_YES, printTableOpt::tuples_only, usage(), adhoc_opts::username, value, and _psqlSettings::vars.

Referenced by main().

462 {
463  static struct option long_options[] =
464  {
465  {"echo-all", no_argument, NULL, 'a'},
466  {"no-align", no_argument, NULL, 'A'},
467  {"command", required_argument, NULL, 'c'},
468  {"dbname", required_argument, NULL, 'd'},
469  {"echo-queries", no_argument, NULL, 'e'},
470  {"echo-errors", no_argument, NULL, 'b'},
471  {"echo-hidden", no_argument, NULL, 'E'},
472  {"file", required_argument, NULL, 'f'},
473  {"field-separator", required_argument, NULL, 'F'},
474  {"field-separator-zero", no_argument, NULL, 'z'},
475  {"host", required_argument, NULL, 'h'},
476  {"html", no_argument, NULL, 'H'},
477  {"list", no_argument, NULL, 'l'},
478  {"log-file", required_argument, NULL, 'L'},
479  {"no-readline", no_argument, NULL, 'n'},
480  {"single-transaction", no_argument, NULL, '1'},
481  {"output", required_argument, NULL, 'o'},
482  {"port", required_argument, NULL, 'p'},
483  {"pset", required_argument, NULL, 'P'},
484  {"quiet", no_argument, NULL, 'q'},
485  {"record-separator", required_argument, NULL, 'R'},
486  {"record-separator-zero", no_argument, NULL, '0'},
487  {"single-step", no_argument, NULL, 's'},
488  {"single-line", no_argument, NULL, 'S'},
489  {"tuples-only", no_argument, NULL, 't'},
490  {"table-attr", required_argument, NULL, 'T'},
491  {"username", required_argument, NULL, 'U'},
492  {"set", required_argument, NULL, 'v'},
493  {"variable", required_argument, NULL, 'v'},
494  {"version", no_argument, NULL, 'V'},
495  {"no-password", no_argument, NULL, 'w'},
496  {"password", no_argument, NULL, 'W'},
497  {"expanded", no_argument, NULL, 'x'},
498  {"no-psqlrc", no_argument, NULL, 'X'},
499  {"help", optional_argument, NULL, 1},
500  {"csv", no_argument, NULL, 2},
501  {NULL, 0, NULL, 0}
502  };
503 
504  int optindex;
505  int c;
506 
507  memset(options, 0, sizeof *options);
508 
509  while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
510  long_options, &optindex)) != -1)
511  {
512  switch (c)
513  {
514  case 'a':
515  SetVariable(pset.vars, "ECHO", "all");
516  break;
517  case 'A':
519  break;
520  case 'b':
521  SetVariable(pset.vars, "ECHO", "errors");
522  break;
523  case 'c':
524  if (optarg[0] == '\\')
527  optarg + 1);
528  else
531  optarg);
532  break;
533  case 'd':
534  options->dbname = pg_strdup(optarg);
535  break;
536  case 'e':
537  SetVariable(pset.vars, "ECHO", "queries");
538  break;
539  case 'E':
540  SetVariableBool(pset.vars, "ECHO_HIDDEN");
541  break;
542  case 'f':
544  ACT_FILE,
545  optarg);
546  break;
547  case 'F':
550  break;
551  case 'h':
552  options->host = pg_strdup(optarg);
553  break;
554  case 'H':
556  break;
557  case 'l':
558  options->list_dbs = true;
559  break;
560  case 'L':
561  options->logfilename = pg_strdup(optarg);
562  break;
563  case 'n':
564  options->no_readline = true;
565  break;
566  case 'o':
567  if (!setQFout(optarg))
568  exit(EXIT_FAILURE);
569  break;
570  case 'p':
571  options->port = pg_strdup(optarg);
572  break;
573  case 'P':
574  {
575  char *value;
576  char *equal_loc;
577  bool result;
578 
579  value = pg_strdup(optarg);
580  equal_loc = strchr(value, '=');
581  if (!equal_loc)
582  result = do_pset(value, NULL, &pset.popt, true);
583  else
584  {
585  *equal_loc = '\0';
586  result = do_pset(value, equal_loc + 1, &pset.popt, true);
587  }
588 
589  if (!result)
590  {
591  pg_log_fatal("could not set printing parameter \"%s\"", value);
592  exit(EXIT_FAILURE);
593  }
594 
595  free(value);
596  break;
597  }
598  case 'q':
599  SetVariableBool(pset.vars, "QUIET");
600  break;
601  case 'R':
604  break;
605  case 's':
606  SetVariableBool(pset.vars, "SINGLESTEP");
607  break;
608  case 'S':
609  SetVariableBool(pset.vars, "SINGLELINE");
610  break;
611  case 't':
612  pset.popt.topt.tuples_only = true;
613  break;
614  case 'T':
616  break;
617  case 'U':
618  options->username = pg_strdup(optarg);
619  break;
620  case 'v':
621  {
622  char *value;
623  char *equal_loc;
624 
625  value = pg_strdup(optarg);
626  equal_loc = strchr(value, '=');
627  if (!equal_loc)
628  {
629  if (!DeleteVariable(pset.vars, value))
630  exit(EXIT_FAILURE); /* error already printed */
631  }
632  else
633  {
634  *equal_loc = '\0';
635  if (!SetVariable(pset.vars, value, equal_loc + 1))
636  exit(EXIT_FAILURE); /* error already printed */
637  }
638 
639  free(value);
640  break;
641  }
642  case 'V':
643  showVersion();
644  exit(EXIT_SUCCESS);
645  case 'w':
647  break;
648  case 'W':
650  break;
651  case 'x':
652  pset.popt.topt.expanded = true;
653  break;
654  case 'X':
655  options->no_psqlrc = true;
656  break;
657  case 'z':
659  break;
660  case '0':
662  break;
663  case '1':
664  options->single_txn = true;
665  break;
666  case '?':
667  if (optind <= argc &&
668  strcmp(argv[optind - 1], "-?") == 0)
669  {
670  /* actual help option given */
671  usage(NOPAGER);
672  exit(EXIT_SUCCESS);
673  }
674  else
675  {
676  /* getopt error (unknown option or missing argument) */
677  goto unknown_option;
678  }
679  break;
680  case 1:
681  {
682  if (!optarg || strcmp(optarg, "options") == 0)
683  usage(NOPAGER);
684  else if (optarg && strcmp(optarg, "commands") == 0)
686  else if (optarg && strcmp(optarg, "variables") == 0)
688  else
689  goto unknown_option;
690 
691  exit(EXIT_SUCCESS);
692  }
693  break;
694  case 2:
696  break;
697  default:
698  unknown_option:
699  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
700  pset.progname);
701  exit(EXIT_FAILURE);
702  break;
703  }
704  }
705 
706  /*
707  * if we still have arguments, use it as the database name and username
708  */
709  while (argc - optind >= 1)
710  {
711  if (!options->dbname)
712  options->dbname = argv[optind];
713  else if (!options->username)
714  options->username = argv[optind];
715  else if (!pset.quiet)
716  pg_log_warning("extra command-line argument \"%s\" ignored",
717  argv[optind]);
718 
719  optind++;
720  }
721 }
#define NOPAGER
Definition: startup.c:89
static void usage(void)
Definition: pg_standby.c:589
SimpleActionList actions
Definition: startup.c:77
char * username
Definition: startup.c:71
#define EXIT_SUCCESS
Definition: settings.h:157
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
enum printFormat format
Definition: print.h:101
printTableOpt topt
Definition: print.h:169
bool no_readline
Definition: startup.c:73
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:367
#define fprintf
Definition: port.h:219
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition: startup.c:729
bool separator_zero
Definition: print.h:96
static void showVersion(void)
Definition: startup.c:823
#define required_argument
Definition: getopt_long.h:25
unsigned short int expanded
Definition: print.h:102
int optind
Definition: getopt.c:50
bool DeleteVariable(VariableSpace space, const char *name)
Definition: variables.c:379
bool tuples_only
Definition: print.h:110
char * c
char * port
Definition: startup.c:70
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:4021
char * tableAttr
Definition: print.h:121
char * dbname
Definition: startup.c:68
char * logfilename
Definition: startup.c:72
#define no_argument
Definition: getopt_long.h:24
void helpVariables(unsigned short int pager)
Definition: help.c:340
bool setQFout(const char *fname)
Definition: common.c:85
PsqlSettings pset
Definition: startup.c:32
static struct @143 value
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:116
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
const char * progname
Definition: settings.h:109
printQueryOpt popt
Definition: settings.h:91
bool single_txn
Definition: startup.c:75
enum trivalue getPassword
Definition: settings.h:104
#define optional_argument
Definition: getopt_long.h:26
bool no_psqlrc
Definition: startup.c:74
bool list_dbs
Definition: startup.c:76
char * host
Definition: startup.c:69
char * optarg
Definition: getopt.c:52
#define EXIT_FAILURE
Definition: settings.h:161
char * separator
Definition: print.h:95
struct separator recordSep
Definition: print.h:117
#define pg_log_warning(...)
Definition: pgfnames.c:24
#define _(x)
Definition: elog.c:88
void slashUsage(unsigned short int pager)
Definition: help.c:160
VariableSpace vars
Definition: settings.h:118
#define pg_log_fatal(...)
Definition: logging.h:76

◆ process_psqlrc()

static void process_psqlrc ( char *  argv0)
static

Definition at line 755 of file startup.c.

References EXIT_FAILURE, expand_tilde(), find_my_exec(), get_etc_path(), get_home_path(), MAXPGPATH, my_exec_path, pg_log_fatal, process_psqlrc_file(), PSQLRC, pstrdup(), snprintf, and SYSPSQLRC.

Referenced by main().

756 {
757  char home[MAXPGPATH];
758  char rc_file[MAXPGPATH];
759  char my_exec_path[MAXPGPATH];
760  char etc_path[MAXPGPATH];
761  char *envrc = getenv("PSQLRC");
762 
763  if (find_my_exec(argv0, my_exec_path) < 0)
764  {
765  pg_log_fatal("could not find own program executable");
766  exit(EXIT_FAILURE);
767  }
768 
769  get_etc_path(my_exec_path, etc_path);
770 
771  snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
772  process_psqlrc_file(rc_file);
773 
774  if (envrc != NULL && strlen(envrc) > 0)
775  {
776  /* might need to free() this */
777  char *envrc_alloc = pstrdup(envrc);
778 
779  expand_tilde(&envrc_alloc);
780  process_psqlrc_file(envrc_alloc);
781  }
782  else if (get_home_path(home))
783  {
784  snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
785  process_psqlrc_file(rc_file);
786  }
787 }
static char * argv0
Definition: pg_ctl.c:97
char * pstrdup(const char *in)
Definition: mcxt.c:1187
#define SYSPSQLRC
Definition: startup.c:35
#define PSQLRC
Definition: startup.c:36
void expand_tilde(char **filename)
Definition: common.c:2222
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:128
char my_exec_path[MAXPGPATH]
Definition: globals.c:72
static void process_psqlrc_file(char *filename)
Definition: startup.c:792
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition: path.c:713
#define EXIT_FAILURE
Definition: settings.h:161
#define snprintf
Definition: port.h:215
bool get_home_path(char *ret_path)
Definition: path.c:807
#define pg_log_fatal(...)
Definition: logging.h:76

◆ process_psqlrc_file()

static void process_psqlrc_file ( char *  filename)
static

Definition at line 792 of file startup.c.

References free, process_file(), and psprintf().

Referenced by process_psqlrc().

793 {
794  char *psqlrc_minor,
795  *psqlrc_major;
796 
797 #if defined(WIN32) && (!defined(__MINGW32__))
798 #define R_OK 4
799 #endif
800 
801  psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
802  psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
803 
804  /* check for minor version first, then major, then no version */
805  if (access(psqlrc_minor, R_OK) == 0)
806  (void) process_file(psqlrc_minor, false);
807  else if (access(psqlrc_major, R_OK) == 0)
808  (void) process_file(psqlrc_major, false);
809  else if (access(filename, R_OK) == 0)
810  (void) process_file(filename, false);
811 
812  free(psqlrc_minor);
813  free(psqlrc_major);
814 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void process_file(const char *filename, int weight)
Definition: pgbench.c:4933
#define free(a)
Definition: header.h:65
static char * filename
Definition: pg_dumpall.c:91

◆ prompt1_hook()

static bool prompt1_hook ( const char *  newval)
static

Definition at line 1082 of file startup.c.

References newval, and _psqlSettings::prompt1.

Referenced by EstablishVariableSpace().

1083 {
1084  pset.prompt1 = newval ? newval : "";
1085  return true;
1086 }
PsqlSettings pset
Definition: startup.c:32
#define newval
const char * prompt1
Definition: settings.h:146

◆ prompt2_hook()

static bool prompt2_hook ( const char *  newval)
static

Definition at line 1089 of file startup.c.

References newval, and _psqlSettings::prompt2.

Referenced by EstablishVariableSpace().

1090 {
1091  pset.prompt2 = newval ? newval : "";
1092  return true;
1093 }
PsqlSettings pset
Definition: startup.c:32
const char * prompt2
Definition: settings.h:147
#define newval

◆ prompt3_hook()

static bool prompt3_hook ( const char *  newval)
static

Definition at line 1096 of file startup.c.

References newval, and _psqlSettings::prompt3.

Referenced by EstablishVariableSpace().

1097 {
1098  pset.prompt3 = newval ? newval : "";
1099  return true;
1100 }
const char * prompt3
Definition: settings.h:148
PsqlSettings pset
Definition: startup.c:32
#define newval

◆ quiet_hook()

static bool quiet_hook ( const char *  newval)
static

Definition at line 871 of file startup.c.

References ParseVariableBool(), and _psqlSettings::quiet.

Referenced by EstablishVariableSpace().

872 {
873  return ParseVariableBool(newval, "QUIET", &pset.quiet);
874 }
PsqlSettings pset
Definition: startup.c:32
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ show_context_hook()

static bool show_context_hook ( const char *  newval)
static

Definition at line 1142 of file startup.c.

References Assert, _psqlSettings::db, pg_strcasecmp(), PQsetErrorContextVisibility(), PQSHOW_CONTEXT_ALWAYS, PQSHOW_CONTEXT_ERRORS, PQSHOW_CONTEXT_NEVER, PsqlVarEnumError(), and _psqlSettings::show_context.

Referenced by EstablishVariableSpace().

1143 {
1144  Assert(newval != NULL); /* else substitute hook messed up */
1145  if (pg_strcasecmp(newval, "never") == 0)
1147  else if (pg_strcasecmp(newval, "errors") == 0)
1149  else if (pg_strcasecmp(newval, "always") == 0)
1151  else
1152  {
1153  PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1154  return false;
1155  }
1156 
1157  if (pset.db)
1159  return true;
1160 }
PGconn * db
Definition: settings.h:82
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PGContextVisibility show_context
Definition: settings.h:150
PsqlSettings pset
Definition: startup.c:32
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:6795
#define Assert(condition)
Definition: c.h:746
#define newval

◆ show_context_substitute_hook()

static char* show_context_substitute_hook ( char *  newval)
static

Definition at line 1134 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

1135 {
1136  if (newval == NULL)
1137  newval = pg_strdup("errors");
1138  return newval;
1139 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

◆ showVersion()

static void showVersion ( void  )
static

Definition at line 823 of file startup.c.

Referenced by main(), and parse_psql_options().

824 {
825  puts("psql (PostgreSQL) " PG_VERSION);
826 }

◆ simple_action_list_append()

static void simple_action_list_append ( SimpleActionList list,
enum _actions  action,
const char *  val 
)
static

Definition at line 729 of file startup.c.

References SimpleActionListCell::action, SimpleActionList::head, SimpleActionListCell::next, pg_malloc(), pg_strdup(), SimpleActionList::tail, and SimpleActionListCell::val.

Referenced by main(), and parse_psql_options().

731 {
732  SimpleActionListCell *cell;
733 
735 
736  cell->next = NULL;
737  cell->action = action;
738  if (val)
739  cell->val = pg_strdup(val);
740  else
741  cell->val = NULL;
742 
743  if (list->tail)
744  list->tail->next = cell;
745  else
746  list->head = cell;
747  list->tail = cell;
748 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
enum _actions action
Definition: startup.c:56
SimpleActionListCell * head
Definition: startup.c:62
SimpleActionListCell * tail
Definition: startup.c:63
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct SimpleActionListCell * next
Definition: startup.c:55
long val
Definition: informix.c:664

◆ singleline_hook()

static bool singleline_hook ( const char *  newval)
static

Definition at line 877 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singleline.

Referenced by EstablishVariableSpace().

878 {
879  return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
880 }
bool singleline
Definition: settings.h:135
PsqlSettings pset
Definition: startup.c:32
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ singlestep_hook()

static bool singlestep_hook ( const char *  newval)
static

Definition at line 883 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singlestep.

Referenced by EstablishVariableSpace().

884 {
885  return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
886 }
bool singlestep
Definition: settings.h:136
PsqlSettings pset
Definition: startup.c:32
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107

◆ verbosity_hook()

static bool verbosity_hook ( const char *  newval)
static

Definition at line 1111 of file startup.c.

References Assert, _psqlSettings::db, pg_strcasecmp(), PQERRORS_DEFAULT, PQERRORS_SQLSTATE, PQERRORS_TERSE, PQERRORS_VERBOSE, PQsetErrorVerbosity(), PsqlVarEnumError(), and _psqlSettings::verbosity.

Referenced by EstablishVariableSpace().

1112 {
1113  Assert(newval != NULL); /* else substitute hook messed up */
1114  if (pg_strcasecmp(newval, "default") == 0)
1116  else if (pg_strcasecmp(newval, "verbose") == 0)
1118  else if (pg_strcasecmp(newval, "terse") == 0)
1120  else if (pg_strcasecmp(newval, "sqlstate") == 0)
1122  else
1123  {
1124  PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1125  return false;
1126  }
1127 
1128  if (pset.db)
1130  return true;
1131 }
PGconn * db
Definition: settings.h:82
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:32
PGVerbosity verbosity
Definition: settings.h:149
#define Assert(condition)
Definition: c.h:746
#define newval
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6783

◆ verbosity_substitute_hook()

static char* verbosity_substitute_hook ( char *  newval)
static

Definition at line 1103 of file startup.c.

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

1104 {
1105  if (newval == NULL)
1106  newval = pg_strdup("default");
1107  return newval;
1108 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define newval

Variable Documentation

◆ pset

Definition at line 32 of file startup.c.

Referenced by AcceptResult(), CheckConnection(), ClearOrSaveResult(), command_no_begin(), connection_warnings(), ConnectionUp(), describeAccessMethods(), describeAggregates(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), DescribeQuery(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_connect(), do_copy(), do_lo_export(), do_lo_import(), do_lo_list(), do_lo_unlink(), do_pset(), do_watch(), echo_hidden_command(), exec_command(), exec_command_a(), exec_command_C(), exec_command_conninfo(), exec_command_crosstabview(), exec_command_echo(), exec_command_ef_ev(), exec_command_encoding(), exec_command_errverbose(), exec_command_f(), exec_command_g(), exec_command_gdesc(), exec_command_gexec(), exec_command_gset(), exec_command_help(), exec_command_html(), exec_command_password(), exec_command_print(), exec_command_prompt(), exec_command_pset(), exec_command_reset(), exec_command_s(), exec_command_set(), exec_command_sf_sv(), exec_command_slash_command_help(), exec_command_t(), exec_command_T(), exec_command_timing(), exec_command_unset(), exec_command_x(), ExecQueryTuples(), ExecQueryUsingCursor(), fail_lo_xact(), finish_lo_xact(), finishInput(), get_create_object_cmd(), get_prompt(), handleCopyIn(), HandleSlashCmds(), helpSQL(), helpVariables(), indexOfColumn(), initializeInput(), is_select_command(), is_superuser(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), lookup_object_oid(), MainLoop(), objectDescription(), parse_slash_copy(), permissionsList(), pg_send_history(), print_lo_result(), printACLColumn(), printCrosstab(), printGSSInfo(), printHistory(), PrintNotifications(), PrintQueryResults(), PrintQueryStatus(), PrintQueryTuples(), PrintResultsInCrosstab(), printSSLInfo(), process_command_g_options(), process_file(), ProcessResult(), psql_get_variable(), PSQLexec(), PSQLexecWatch(), SendQuery(), session_username(), setQFout(), SetResultVariables(), skip_white_space(), slashUsage(), standard_strings(), start_lo_xact(), StoreQueryTuple(), SyncVariables(), UnsyncVariables(), and usage().