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)
 
static void empty_signal_handler (SIGNAL_ARGS)
 
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 bool show_all_results_hook (const char *newval)
 
static char * show_context_substitute_hook (char *newval)
 
static bool show_context_hook (const char *newval)
 
static bool hide_compression_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.

◆ PARAMS_ARRAY_SIZE

#define PARAMS_ARRAY_SIZE   8

◆ PSQLRC

#define PSQLRC   ".psqlrc"

Definition at line 36 of file startup.c.

◆ SYSPSQLRC

#define SYSPSQLRC   "psqlrc"

Definition at line 35 of file startup.c.

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.

47{
51};
@ ACT_SINGLE_SLASH
Definition: startup.c:49
@ ACT_FILE
Definition: startup.c:50
@ ACT_SINGLE_QUERY
Definition: startup.c:48

Function Documentation

◆ autocommit_hook()

static bool autocommit_hook ( const char *  newval)
static

Definition at line 875 of file startup.c.

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

References _psqlSettings::autocommit, newval, ParseVariableBool(), and pset.

Referenced by EstablishVariableSpace().

◆ bool_substitute_hook()

static char * bool_substitute_hook ( char *  newval)
static

Definition at line 858 of file startup.c.

859{
860 if (newval == NULL)
861 {
862 /* "\unset FOO" becomes "\set FOO off" */
863 newval = pg_strdup("off");
864 }
865 else if (newval[0] == '\0')
866 {
867 /* "\set FOO" becomes "\set FOO on" */
869 newval = pg_strdup("on");
870 }
871 return newval;
872}
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void pg_free(void *ptr)
Definition: fe_memutils.c:105

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

Referenced by EstablishVariableSpace().

◆ comp_keyword_case_hook()

static bool comp_keyword_case_hook ( const char *  newval)
static

Definition at line 1048 of file startup.c.

1049{
1050 Assert(newval != NULL); /* else substitute hook messed up */
1051 if (pg_strcasecmp(newval, "preserve-upper") == 0)
1053 else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1055 else if (pg_strcasecmp(newval, "upper") == 0)
1057 else if (pg_strcasecmp(newval, "lower") == 0)
1059 else
1060 {
1061 PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1062 "lower, upper, preserve-lower, preserve-upper");
1063 return false;
1064 }
1065 return true;
1066}
#define Assert(condition)
Definition: c.h:815
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
@ PSQL_COMP_CASE_PRESERVE_LOWER
Definition: settings.h:60
@ PSQL_COMP_CASE_LOWER
Definition: settings.h:62
@ PSQL_COMP_CASE_PRESERVE_UPPER
Definition: settings.h:59
@ PSQL_COMP_CASE_UPPER
Definition: settings.h:61
PSQL_COMP_CASE comp_case
Definition: settings.h:160
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:416

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

Referenced by EstablishVariableSpace().

◆ comp_keyword_case_substitute_hook()

static char * comp_keyword_case_substitute_hook ( char *  newval)
static

Definition at line 1040 of file startup.c.

1041{
1042 if (newval == NULL)
1043 newval = pg_strdup("preserve-upper");
1044 return newval;
1045}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ echo_hidden_hook()

static bool echo_hidden_hook ( const char *  newval)
static

Definition at line 998 of file startup.c.

999{
1000 Assert(newval != NULL); /* else substitute hook messed up */
1001 if (pg_strcasecmp(newval, "noexec") == 0)
1003 else
1004 {
1005 bool on_off;
1006
1007 if (ParseVariableBool(newval, NULL, &on_off))
1009 else
1010 {
1011 PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1012 return false;
1013 }
1014 }
1015 return true;
1016}
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition: settings.h:47
@ PSQL_ECHO_HIDDEN_OFF
Definition: settings.h:45
@ PSQL_ECHO_HIDDEN_ON
Definition: settings.h:46
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:158

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

Referenced by EstablishVariableSpace().

◆ echo_hook()

static bool echo_hook ( const char *  newval)
static

Definition at line 978 of file startup.c.

979{
980 Assert(newval != NULL); /* else substitute hook messed up */
981 if (pg_strcasecmp(newval, "queries") == 0)
983 else if (pg_strcasecmp(newval, "errors") == 0)
985 else if (pg_strcasecmp(newval, "all") == 0)
987 else if (pg_strcasecmp(newval, "none") == 0)
989 else
990 {
991 PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
992 return false;
993 }
994 return true;
995}
@ PSQL_ECHO_ALL
Definition: settings.h:40
@ PSQL_ECHO_ERRORS
Definition: settings.h:39
@ PSQL_ECHO_NONE
Definition: settings.h:37
@ PSQL_ECHO_QUERIES
Definition: settings.h:38
PSQL_ECHO echo
Definition: settings.h:157

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

Referenced by EstablishVariableSpace().

◆ echo_substitute_hook()

static char * echo_substitute_hook ( char *  newval)
static

Definition at line 970 of file startup.c.

971{
972 if (newval == NULL)
973 newval = pg_strdup("none");
974 return newval;
975}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ empty_signal_handler()

static void empty_signal_handler ( SIGNAL_ARGS  )
static

Definition at line 115 of file startup.c.

116{
117}

Referenced by main().

◆ EstablishVariableSpace()

static void EstablishVariableSpace ( void  )
static

Definition at line 1198 of file startup.c.

1199{
1201
1202 SetVariableHooks(pset.vars, "AUTOCOMMIT",
1205 SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1208 SetVariableHooks(pset.vars, "QUIET",
1210 quiet_hook);
1211 SetVariableHooks(pset.vars, "SINGLELINE",
1214 SetVariableHooks(pset.vars, "SINGLESTEP",
1217 SetVariableHooks(pset.vars, "FETCH_COUNT",
1220 SetVariableHooks(pset.vars, "HISTFILE",
1221 NULL,
1223 SetVariableHooks(pset.vars, "HISTSIZE",
1226 SetVariableHooks(pset.vars, "IGNOREEOF",
1229 SetVariableHooks(pset.vars, "ECHO",
1231 echo_hook);
1232 SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1235 SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1238 SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1241 SetVariableHooks(pset.vars, "HISTCONTROL",
1244 SetVariableHooks(pset.vars, "PROMPT1",
1245 NULL,
1246 prompt1_hook);
1247 SetVariableHooks(pset.vars, "PROMPT2",
1248 NULL,
1249 prompt2_hook);
1250 SetVariableHooks(pset.vars, "PROMPT3",
1251 NULL,
1252 prompt3_hook);
1253 SetVariableHooks(pset.vars, "VERBOSITY",
1256 SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
1259 SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1262 SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1265 SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1268}
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1119
static bool show_context_hook(const char *newval)
Definition: startup.c:1164
static bool hide_tableam_hook(const char *newval)
Definition: startup.c:1192
static bool quiet_hook(const char *newval)
Definition: startup.c:887
static bool singleline_hook(const char *newval)
Definition: startup.c:893
static bool prompt2_hook(const char *newval)
Definition: startup.c:1105
static bool comp_keyword_case_hook(const char *newval)
Definition: startup.c:1048
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1077
static bool prompt1_hook(const char *newval)
Definition: startup.c:1098
static bool verbosity_hook(const char *newval)
Definition: startup.c:1127
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:943
static bool show_all_results_hook(const char *newval)
Definition: startup.c:1150
static bool fetch_count_hook(const char *newval)
Definition: startup.c:913
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:905
static bool histsize_hook(const char *newval)
Definition: startup.c:937
static char * echo_substitute_hook(char *newval)
Definition: startup.c:970
static bool hide_compression_hook(const char *newval)
Definition: startup.c:1185
static char * bool_substitute_hook(char *newval)
Definition: startup.c:858
static bool singlestep_hook(const char *newval)
Definition: startup.c:899
static bool histfile_hook(const char *newval)
Definition: startup.c:919
static bool prompt3_hook(const char *newval)
Definition: startup.c:1112
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:1019
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:929
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:881
static bool autocommit_hook(const char *newval)
Definition: startup.c:875
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1156
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:964
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:998
static bool echo_hook(const char *newval)
Definition: startup.c:978
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:1069
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:1040
VariableSpace vars
Definition: settings.h:133
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition: variables.c:314
VariableSpace CreateVariableSpace(void)
Definition: variables.c:51

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_compression_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(), pset, quiet_hook(), SetVariableHooks(), show_all_results_hook(), show_context_hook(), show_context_substitute_hook(), singleline_hook(), singlestep_hook(), _psqlSettings::vars, verbosity_hook(), and verbosity_substitute_hook().

Referenced by main().

◆ fetch_count_hook()

static bool fetch_count_hook ( const char *  newval)
static

Definition at line 913 of file startup.c.

914{
915 return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
916}
int fetch_count
Definition: settings.h:154
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156

References _psqlSettings::fetch_count, newval, ParseVariableNum(), and pset.

Referenced by EstablishVariableSpace().

◆ fetch_count_substitute_hook()

static char * fetch_count_substitute_hook ( char *  newval)
static

Definition at line 905 of file startup.c.

906{
907 if (newval == NULL)
908 newval = pg_strdup("0");
909 return newval;
910}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ hide_compression_hook()

static bool hide_compression_hook ( const char *  newval)
static

Definition at line 1185 of file startup.c.

1186{
1187 return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1189}
bool hide_compression
Definition: settings.h:152

References _psqlSettings::hide_compression, newval, ParseVariableBool(), and pset.

Referenced by EstablishVariableSpace().

◆ hide_tableam_hook()

static bool hide_tableam_hook ( const char *  newval)
static

Definition at line 1192 of file startup.c.

1193{
1194 return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1195}
bool hide_tableam
Definition: settings.h:153

References _psqlSettings::hide_tableam, newval, ParseVariableBool(), and pset.

Referenced by EstablishVariableSpace().

◆ histcontrol_hook()

static bool histcontrol_hook ( const char *  newval)
static

Definition at line 1077 of file startup.c.

1078{
1079 Assert(newval != NULL); /* else substitute hook messed up */
1080 if (pg_strcasecmp(newval, "ignorespace") == 0)
1082 else if (pg_strcasecmp(newval, "ignoredups") == 0)
1084 else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1086 else if (pg_strcasecmp(newval, "none") == 0)
1088 else
1089 {
1090 PsqlVarEnumError("HISTCONTROL", newval,
1091 "none, ignorespace, ignoredups, ignoreboth");
1092 return false;
1093 }
1094 return true;
1095}
@ hctl_ignoredups
Definition: settings.h:78
@ hctl_ignoreboth
Definition: settings.h:79
@ hctl_none
Definition: settings.h:76
@ hctl_ignorespace
Definition: settings.h:77
HistControl histcontrol
Definition: settings.h:161

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

Referenced by EstablishVariableSpace().

◆ histcontrol_substitute_hook()

static char * histcontrol_substitute_hook ( char *  newval)
static

Definition at line 1069 of file startup.c.

1070{
1071 if (newval == NULL)
1072 newval = pg_strdup("none");
1073 return newval;
1074}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ histfile_hook()

static bool histfile_hook ( const char *  newval)
static

Definition at line 919 of file startup.c.

920{
921 /*
922 * Someday we might try to validate the filename, but for now, this is
923 * just a placeholder to ensure HISTFILE is known to tab completion.
924 */
925 return true;
926}

Referenced by EstablishVariableSpace().

◆ histsize_hook()

static bool histsize_hook ( const char *  newval)
static

Definition at line 937 of file startup.c.

938{
939 return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
940}

References _psqlSettings::histsize, newval, ParseVariableNum(), and pset.

Referenced by EstablishVariableSpace().

◆ histsize_substitute_hook()

static char * histsize_substitute_hook ( char *  newval)
static

Definition at line 929 of file startup.c.

930{
931 if (newval == NULL)
932 newval = pg_strdup("500");
933 return newval;
934}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ ignoreeof_hook()

static bool ignoreeof_hook ( const char *  newval)
static

Definition at line 964 of file startup.c.

965{
966 return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
967}

References _psqlSettings::ignoreeof, newval, ParseVariableNum(), and pset.

Referenced by EstablishVariableSpace().

◆ ignoreeof_substitute_hook()

static char * ignoreeof_substitute_hook ( char *  newval)
static

Definition at line 943 of file startup.c.

944{
945 int dummy;
946
947 /*
948 * This tries to mimic the behavior of bash, to wit "If set, the value is
949 * the number of consecutive EOF characters which must be typed as the
950 * first characters on an input line before bash exits. If the variable
951 * exists but does not have a numeric value, or has no value, the default
952 * value is 10. If it does not exist, EOF signifies the end of input to
953 * the shell." Unlike bash, however, we insist on the stored value
954 * actually being a valid integer.
955 */
956 if (newval == NULL)
957 newval = pg_strdup("0");
958 else if (!ParseVariableNum(newval, NULL, &dummy))
959 newval = pg_strdup("10");
960 return newval;
961}

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

Referenced by EstablishVariableSpace().

◆ log_locus_callback()

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

Definition at line 99 of file startup.c.

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

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

Referenced by main().

◆ log_pre_callback()

static void log_pre_callback ( void  )
static

Definition at line 92 of file startup.c.

93{
96}
static void const char fflush(stdout)
FILE * queryFout
Definition: settings.h:93

References fflush(), pset, _psqlSettings::queryFout, and generate_unaccent_rules::stdout.

Referenced by main().

◆ main()

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

Definition at line 126 of file startup.c.

127{
128 struct adhoc_opts options;
129 int successResult;
130 char *password = NULL;
131 bool new_pass;
132
133 pg_logging_init(argv[0]);
136 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
137
138 if (argc > 1)
139 {
140 if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
141 {
142 usage(NOPAGER);
144 }
145 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
146 {
147 showVersion();
149 }
150 }
151
152 pset.progname = get_progname(argv[0]);
153
154 pset.db = NULL;
155 pset.dead_conn = NULL;
159 pset.queryFoutPipe = false;
160 pset.copyStream = NULL;
161 pset.last_error_result = NULL;
162 pset.cur_cmd_source = stdin;
164
165 /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
167 pset.popt.topt.border = 1;
168 pset.popt.topt.pager = 1;
170 pset.popt.topt.start_table = true;
171 pset.popt.topt.stop_table = true;
173
175 pset.popt.topt.csvFieldSep[1] = '\0';
176
180
182
183 /* We must get COLUMNS here before readline() sets it */
184 pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
185
186 pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
187
189
191
192 /* Create variables showing psql version number */
193 SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
194 SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
195 SetVariable(pset.vars, "VERSION_NUM", CppAsString2(PG_VERSION_NUM));
196
197 /* Initialize variables for last error */
198 SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
199 SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
200
201 /* Default values for variables (that don't match the result of \unset) */
202 SetVariableBool(pset.vars, "AUTOCOMMIT");
206 SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
207
208 parse_psql_options(argc, argv, &options);
209
210 /*
211 * If no action was specified and we're in non-interactive mode, treat it
212 * as if the user had specified "-f -". This lets single-transaction mode
213 * work in this case.
214 */
215 if (options.actions.head == NULL && pset.notty)
217
218 /* Bail out if -1 was specified but will be ignored. */
219 if (options.single_txn && options.actions.head == NULL)
220 pg_fatal("-1 can only be used in non-interactive mode");
221
224 {
227 }
230 {
233 }
234
235 if (pset.getPassword == TRI_YES)
236 {
237 /*
238 * We can't be sure yet of the username that will be used, so don't
239 * offer a potentially wrong one. Typical uses of this option are
240 * noninteractive anyway. (Note: since we've not yet set up our
241 * cancel handler, there's no need to use simple_prompt_extended.)
242 */
243 password = simple_prompt("Password: ", false);
244 }
245
246 /* loop until we have a password if requested by backend */
247 do
248 {
249#define PARAMS_ARRAY_SIZE 8
250 const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
251 const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
252
253 keywords[0] = "host";
254 values[0] = options.host;
255 keywords[1] = "port";
256 values[1] = options.port;
257 keywords[2] = "user";
259 keywords[3] = "password";
260 values[3] = password;
261 keywords[4] = "dbname"; /* see do_connect() */
262 values[4] = (options.list_dbs && options.dbname == NULL) ?
263 "postgres" : options.dbname;
264 keywords[5] = "fallback_application_name";
265 values[5] = pset.progname;
266 keywords[6] = "client_encoding";
267 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
268 keywords[7] = NULL;
269 values[7] = NULL;
270
271 new_pass = false;
273 free(keywords);
274 free(values);
275
276 if (PQstatus(pset.db) == CONNECTION_BAD &&
278 !password &&
280 {
281 /*
282 * Before closing the old PGconn, extract the user name that was
283 * actually connected with --- it might've come out of a URI or
284 * connstring "database name" rather than options.username.
285 */
286 const char *realusername = PQuser(pset.db);
287 char *password_prompt;
288
289 if (realusername && realusername[0])
290 password_prompt = psprintf(_("Password for user %s: "),
291 realusername);
292 else
293 password_prompt = pg_strdup(_("Password: "));
295
296 password = simple_prompt(password_prompt, false);
297 free(password_prompt);
298 new_pass = true;
299 }
300 } while (new_pass);
301
303 {
307 }
308
310
311#ifndef WIN32
312
313 /*
314 * do_watch() needs signal handlers installed (otherwise sigwait() will
315 * filter them out on some platforms), but doesn't need them to do
316 * anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
317 * arrives due to a race when do_watch() cancels an itimer).
318 */
321#endif
322
324
326
327 if (options.list_dbs)
328 {
329 int success;
330
331 if (!options.no_psqlrc)
332 process_psqlrc(argv[0]);
333
334 success = listAllDbs(NULL, false);
337 }
338
339 if (options.logfilename)
340 {
341 pset.logfile = fopen(options.logfilename, "a");
342 if (!pset.logfile)
343 pg_fatal("could not open log file \"%s\": %m",
344 options.logfilename);
345 }
346
347 if (!options.no_psqlrc)
348 process_psqlrc(argv[0]);
349
350 /*
351 * If any actions were given by user, process them in the order in which
352 * they were specified. Note single_txn is only effective in this mode.
353 */
354 if (options.actions.head != NULL)
355 {
356 PGresult *res;
358
359 successResult = EXIT_SUCCESS; /* silence compiler */
360
361 if (options.single_txn)
362 {
363 if ((res = PSQLexec("BEGIN")) == NULL)
364 {
366 {
367 successResult = EXIT_USER;
368 goto error;
369 }
370 }
371 else
372 PQclear(res);
373 }
374
375 for (cell = options.actions.head; cell; cell = cell->next)
376 {
377 if (cell->action == ACT_SINGLE_QUERY)
378 {
380
381 if (pset.echo == PSQL_ECHO_ALL)
382 puts(cell->val);
383
384 successResult = SendQuery(cell->val)
386 }
387 else if (cell->action == ACT_SINGLE_SLASH)
388 {
389 PsqlScanState scan_state;
390 ConditionalStack cond_stack;
391
393
394 if (pset.echo == PSQL_ECHO_ALL)
395 puts(cell->val);
396
398 psql_scan_setup(scan_state,
399 cell->val, strlen(cell->val),
401 cond_stack = conditional_stack_create();
402 psql_scan_set_passthrough(scan_state, cond_stack);
403
404 successResult = HandleSlashCmds(scan_state,
405 cond_stack,
406 NULL,
407 NULL) != PSQL_CMD_ERROR
409
410 psql_scan_destroy(scan_state);
411 conditional_stack_destroy(cond_stack);
412 }
413 else if (cell->action == ACT_FILE)
414 {
415 successResult = process_file(cell->val, false);
416 }
417 else
418 {
419 /* should never come here */
420 Assert(false);
421 }
422
423 if (successResult != EXIT_SUCCESS && pset.on_error_stop)
424 break;
425 }
426
427 if (options.single_txn)
428 {
429 /*
430 * Rollback the contents of the single transaction if the caller
431 * has set ON_ERROR_STOP and one of the steps has failed. This
432 * check needs to match the one done a couple of lines above.
433 */
434 res = PSQLexec((successResult != EXIT_SUCCESS && pset.on_error_stop) ?
435 "ROLLBACK" : "COMMIT");
436 if (res == NULL)
437 {
439 {
440 successResult = EXIT_USER;
441 goto error;
442 }
443 }
444 else
445 PQclear(res);
446 }
447
448error:
449 ;
450 }
451
452 /*
453 * or otherwise enter interactive main loop
454 */
455 else
456 {
459 if (!pset.quiet)
460 printf(_("Type \"help\" for help.\n\n"));
461 initializeInput(options.no_readline ? 0 : 1);
462 successResult = MainLoop(stdin);
463 }
464
465 /* clean up */
466 if (pset.logfile)
467 fclose(pset.logfile);
468 if (pset.db)
470 if (pset.dead_conn)
472 setQFout(NULL);
473
474 return successResult;
475}
PGresult * PSQLexec(const char *query)
Definition: common.c:620
void psql_setup_cancel_handler(void)
Definition: common.c:313
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:267
bool standard_strings(void)
Definition: common.c:2154
bool setQFout(const char *fname)
Definition: common.c:132
bool SendQuery(const char *query)
Definition: common.c:1082
static void empty_signal_handler(SIGNAL_ARGS)
Definition: startup.c:115
static void process_psqlrc(char *argv0)
Definition: startup.c:774
static void EstablishVariableSpace(void)
Definition: startup.c:1198
static void log_pre_callback(void)
Definition: startup.c:92
#define NOPAGER
Definition: startup.c:89
#define PARAMS_ARRAY_SIZE
static void log_locus_callback(const char **filename, uint64 *lineno)
Definition: startup.c:99
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition: startup.c:748
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:483
static void showVersion(void)
Definition: startup.c:839
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1171
#define CppAsString2(x)
Definition: c.h:349
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:218
void SyncVariables(void)
Definition: command.c:4128
void connection_warnings(bool in_startup)
Definition: command.c:4000
@ PSQL_CMD_ERROR
Definition: command.h:22
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:429
ConditionalStack conditional_stack_create(void)
Definition: conditional.c:18
void conditional_stack_destroy(ConditionalStack cstack)
Definition: conditional.c:43
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:944
#define _(x)
Definition: elog.c:90
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7319
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7205
void PQfinish(PGconn *conn)
Definition: fe-connect.c:4939
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7112
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7447
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7268
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:698
int PQenv2encoding(void)
Definition: fe-misc.c:1253
#define pg_malloc_array(type, count)
Definition: fe_memutils.h:56
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3691
void setDecimalLocale(void)
Definition: print.c:3641
@ UNICODE_LINESTYLE_SINGLE
Definition: print.h:101
@ PRINT_ALIGNED
Definition: print.h:31
#define free(a)
Definition: header.h:65
static bool success
Definition: initdb.c:186
void initializeInput(int flags)
Definition: input.c:344
static const JsonPathKeyword keywords[]
@ CONNECTION_BAD
Definition: libpq-fe.h:82
exit(1)
void pg_logging_init(const char *argv0)
Definition: logging.c:83
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition: logging.c:202
void pg_logging_config(int new_flags)
Definition: logging.c:166
void pg_logging_set_pre_callback(void(*cb)(void))
Definition: logging.c:196
#define pg_log_error(...)
Definition: logging.h:106
#define PG_LOG_FLAG_TERSE
Definition: logging.h:86
const PsqlScanCallbacks psqlscan_callbacks
Definition: mainloop.c:20
int MainLoop(FILE *source)
Definition: mainloop.c:33
#define pg_fatal(...)
static void process_file(const char *filename, int weight)
Definition: pgbench.c:6088
#define pqsignal
Definition: port.h:520
const char * get_progname(const char *argv0)
Definition: path.c:575
#define printf(...)
Definition: port.h:244
char * psprintf(const char *fmt,...)
Definition: psprintf.c:43
void psql_scan_destroy(PsqlScanState state)
Definition: psqlscan.l:1022
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
Definition: psqlscan.l:1001
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
Definition: psqlscan.l:1041
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
Definition: psqlscan.l:1059
#define DEFAULT_PROMPT1
Definition: settings.h:26
#define DEFAULT_PROMPT3
Definition: settings.h:28
#define DEFAULT_RECORD_SEP
Definition: settings.h:16
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
#define EXIT_SUCCESS
Definition: settings.h:174
#define EXIT_FAILURE
Definition: settings.h:178
#define EXIT_BADCONN
Definition: settings.h:181
#define DEFAULT_PROMPT2
Definition: settings.h:27
#define EXIT_USER
Definition: settings.h:183
#define DEFAULT_CSV_FIELD_SEP
Definition: settings.h:14
char * simple_prompt(const char *prompt, bool echo)
Definition: sprompt.c:38
static void error(void)
Definition: sql-dyntest.c:147
static char * password
Definition: streamutil.c:52
struct SimpleActionListCell * next
Definition: startup.c:55
enum _actions action
Definition: startup.c:56
printQueryOpt popt
Definition: settings.h:100
int encoding
Definition: settings.h:92
FILE * logfile
Definition: settings.h:131
bool on_error_stop
Definition: settings.h:148
PGconn * dead_conn
Definition: settings.h:140
PGresult * last_error_result
Definition: settings.h:98
FILE * copyStream
Definition: settings.h:96
PGconn * db
Definition: settings.h:91
bool queryFoutPipe
Definition: settings.h:94
enum trivalue getPassword
Definition: settings.h:119
bool cur_cmd_interactive
Definition: settings.h:122
FILE * cur_cmd_source
Definition: settings.h:120
const char * progname
Definition: settings.h:124
char * username
Definition: oid2name.c:45
char * dbname
Definition: oid2name.c:42
char * port
Definition: oid2name.c:44
printTableOpt topt
Definition: print.h:185
bool start_table
Definition: print.h:127
unicode_linestyle unicode_border_linestyle
Definition: print.h:141
enum printFormat format
Definition: print.h:113
struct separator fieldSep
Definition: print.h:132
struct separator recordSep
Definition: print.h:133
char csvFieldSep[2]
Definition: print.h:134
bool default_footer
Definition: print.h:129
int pager_min_lines
Definition: print.h:124
unsigned short int pager
Definition: print.h:122
unsigned short int border
Definition: print.h:120
unicode_linestyle unicode_header_linestyle
Definition: print.h:143
int env_columns
Definition: print.h:139
unicode_linestyle unicode_column_linestyle
Definition: print.h:142
bool stop_table
Definition: print.h:128
bool separator_zero
Definition: print.h:108
char * separator
Definition: print.h:107
static void usage(const char *progname)
Definition: vacuumlo.c:414
@ TRI_YES
Definition: vacuumlo.c:38
@ TRI_DEFAULT
Definition: vacuumlo.c:36
@ TRI_NO
Definition: vacuumlo.c:37
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:392
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
#define SIGCHLD
Definition: win32_port.h:168
#define SIGALRM
Definition: win32_port.h:164

References _, ACT_FILE, ACT_SINGLE_QUERY, ACT_SINGLE_SLASH, SimpleActionListCell::action, 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, options::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, empty_signal_handler(), _psqlSettings::encoding, printTableOpt::env_columns, error(), EstablishVariableSpace(), exit(), EXIT_BADCONN, EXIT_FAILURE, EXIT_SUCCESS, EXIT_USER, printTableOpt::fieldSep, printTableOpt::format, free, get_progname(), _psqlSettings::getPassword, HandleSlashCmds(), initializeInput(), keywords, _psqlSettings::last_error_result, listAllDbs(), log_locus_callback(), log_pre_callback(), _psqlSettings::logfile, MainLoop(), SimpleActionListCell::next, NOPAGER, NoticeProcessor(), _psqlSettings::notty, _psqlSettings::on_error_stop, printTableOpt::pager, printTableOpt::pager_min_lines, PARAMS_ARRAY_SIZE, parse_psql_options(), password, pg_fatal, pg_log_error, PG_LOG_FLAG_TERSE, pg_logging_config(), pg_logging_init(), pg_logging_set_locus_callback(), pg_logging_set_pre_callback(), pg_malloc_array, pg_strdup(), PG_TEXTDOMAIN, _psqlSettings::popt, options::port, PQclear(), PQconnectdbParams(), PQconnectionNeedsPassword(), PQenv2encoding(), PQerrorMessage(), PQfinish(), PQsetNoticeProcessor(), pqsignal, PQstatus(), PQuser(), PRINT_ALIGNED, printf, process_file(), process_psqlrc(), _psqlSettings::progname, pset, 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(), res, SendQuery(), separator::separator, separator::separator_zero, set_pglocale_pgservice(), setDecimalLocale(), setQFout(), SetVariable(), SetVariableBool(), showVersion(), SIGALRM, SIGCHLD, simple_action_list_append(), simple_prompt(), 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(), options::username, SimpleActionListCell::val, values, and _psqlSettings::vars.

◆ on_error_rollback_hook()

static bool on_error_rollback_hook ( const char *  newval)
static

Definition at line 1019 of file startup.c.

1020{
1021 Assert(newval != NULL); /* else substitute hook messed up */
1022 if (pg_strcasecmp(newval, "interactive") == 0)
1024 else
1025 {
1026 bool on_off;
1027
1028 if (ParseVariableBool(newval, NULL, &on_off))
1030 else
1031 {
1032 PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1033 return false;
1034 }
1035 }
1036 return true;
1037}
@ PSQL_ERROR_ROLLBACK_INTERACTIVE
Definition: settings.h:53
@ PSQL_ERROR_ROLLBACK_ON
Definition: settings.h:54
@ PSQL_ERROR_ROLLBACK_OFF
Definition: settings.h:52
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:159

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

Referenced by EstablishVariableSpace().

◆ on_error_stop_hook()

static bool on_error_stop_hook ( const char *  newval)
static

Definition at line 881 of file startup.c.

882{
883 return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
884}

References newval, _psqlSettings::on_error_stop, ParseVariableBool(), and pset.

Referenced by EstablishVariableSpace().

◆ parse_psql_options()

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

Definition at line 483 of file startup.c.

484{
485 static struct option long_options[] =
486 {
487 {"echo-all", no_argument, NULL, 'a'},
488 {"no-align", no_argument, NULL, 'A'},
489 {"command", required_argument, NULL, 'c'},
490 {"dbname", required_argument, NULL, 'd'},
491 {"echo-queries", no_argument, NULL, 'e'},
492 {"echo-errors", no_argument, NULL, 'b'},
493 {"echo-hidden", no_argument, NULL, 'E'},
494 {"file", required_argument, NULL, 'f'},
495 {"field-separator", required_argument, NULL, 'F'},
496 {"field-separator-zero", no_argument, NULL, 'z'},
497 {"host", required_argument, NULL, 'h'},
498 {"html", no_argument, NULL, 'H'},
499 {"list", no_argument, NULL, 'l'},
500 {"log-file", required_argument, NULL, 'L'},
501 {"no-readline", no_argument, NULL, 'n'},
502 {"single-transaction", no_argument, NULL, '1'},
503 {"output", required_argument, NULL, 'o'},
504 {"port", required_argument, NULL, 'p'},
505 {"pset", required_argument, NULL, 'P'},
506 {"quiet", no_argument, NULL, 'q'},
507 {"record-separator", required_argument, NULL, 'R'},
508 {"record-separator-zero", no_argument, NULL, '0'},
509 {"single-step", no_argument, NULL, 's'},
510 {"single-line", no_argument, NULL, 'S'},
511 {"tuples-only", no_argument, NULL, 't'},
512 {"table-attr", required_argument, NULL, 'T'},
513 {"username", required_argument, NULL, 'U'},
514 {"set", required_argument, NULL, 'v'},
515 {"variable", required_argument, NULL, 'v'},
516 {"version", no_argument, NULL, 'V'},
517 {"no-password", no_argument, NULL, 'w'},
518 {"password", no_argument, NULL, 'W'},
519 {"expanded", no_argument, NULL, 'x'},
520 {"no-psqlrc", no_argument, NULL, 'X'},
521 {"help", optional_argument, NULL, 1},
522 {"csv", no_argument, NULL, 2},
523 {NULL, 0, NULL, 0}
524 };
525
526 int optindex;
527 int c;
528
529 memset(options, 0, sizeof *options);
530
531 while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
532 long_options, &optindex)) != -1)
533 {
534 switch (c)
535 {
536 case 'a':
537 SetVariable(pset.vars, "ECHO", "all");
538 break;
539 case 'A':
541 break;
542 case 'b':
543 SetVariable(pset.vars, "ECHO", "errors");
544 break;
545 case 'c':
546 if (optarg[0] == '\\')
549 optarg + 1);
550 else
553 optarg);
554 break;
555 case 'd':
557 break;
558 case 'e':
559 SetVariable(pset.vars, "ECHO", "queries");
560 break;
561 case 'E':
562 SetVariableBool(pset.vars, "ECHO_HIDDEN");
563 break;
564 case 'f':
566 ACT_FILE,
567 optarg);
568 break;
569 case 'F':
572 break;
573 case 'h':
574 options->host = pg_strdup(optarg);
575 break;
576 case 'H':
578 break;
579 case 'l':
580 options->list_dbs = true;
581 break;
582 case 'L':
583 options->logfilename = pg_strdup(optarg);
584 break;
585 case 'n':
586 options->no_readline = true;
587 break;
588 case 'o':
589 if (!setQFout(optarg))
591 break;
592 case 'p':
594 break;
595 case 'P':
596 {
597 char *value;
598 char *equal_loc;
599 bool result;
600
602 equal_loc = strchr(value, '=');
603 if (!equal_loc)
604 result = do_pset(value, NULL, &pset.popt, true);
605 else
606 {
607 *equal_loc = '\0';
608 result = do_pset(value, equal_loc + 1, &pset.popt, true);
609 }
610
611 if (!result)
612 pg_fatal("could not set printing parameter \"%s\"", value);
613
614 free(value);
615 break;
616 }
617 case 'q':
618 SetVariableBool(pset.vars, "QUIET");
619 break;
620 case 'R':
623 break;
624 case 's':
625 SetVariableBool(pset.vars, "SINGLESTEP");
626 break;
627 case 'S':
628 SetVariableBool(pset.vars, "SINGLELINE");
629 break;
630 case 't':
631 pset.popt.topt.tuples_only = true;
632 break;
633 case 'T':
635 break;
636 case 'U':
638 break;
639 case 'v':
640 {
641 char *value;
642 char *equal_loc;
643
645 equal_loc = strchr(value, '=');
646 if (!equal_loc)
647 {
649 exit(EXIT_FAILURE); /* error already printed */
650 }
651 else
652 {
653 *equal_loc = '\0';
654 if (!SetVariable(pset.vars, value, equal_loc + 1))
655 exit(EXIT_FAILURE); /* error already printed */
656 }
657
658 free(value);
659 break;
660 }
661 case 'V':
662 showVersion();
664 case 'w':
666 break;
667 case 'W':
669 break;
670 case 'x':
671 pset.popt.topt.expanded = true;
672 break;
673 case 'X':
674 options->no_psqlrc = true;
675 break;
676 case 'z':
678 break;
679 case '0':
681 break;
682 case '1':
683 options->single_txn = true;
684 break;
685 case '?':
686 if (optind <= argc &&
687 strcmp(argv[optind - 1], "-?") == 0)
688 {
689 /* actual help option given */
690 usage(NOPAGER);
692 }
693 else
694 {
695 /* getopt error (unknown option or missing argument) */
696 goto unknown_option;
697 }
698 break;
699 case 1:
700 {
701 if (!optarg || strcmp(optarg, "options") == 0)
702 usage(NOPAGER);
703 else if (optarg && strcmp(optarg, "commands") == 0)
705 else if (optarg && strcmp(optarg, "variables") == 0)
707 else
708 goto unknown_option;
709
711 }
712 break;
713 case 2:
715 break;
716 default:
717 unknown_option:
718 /* getopt_long already emitted a complaint */
719 pg_log_error_hint("Try \"%s --help\" for more information.",
720 pset.progname);
722 }
723 }
724
725 /*
726 * if we still have arguments, use it as the database name and username
727 */
728 while (argc - optind >= 1)
729 {
730 if (!options->dbname)
731 options->dbname = argv[optind];
732 else if (!options->username)
733 options->username = argv[optind];
734 else if (!pset.quiet)
735 pg_log_warning("extra command-line argument \"%s\" ignored",
736 argv[optind]);
737
738 optind++;
739 }
740}
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:4621
@ PRINT_CSV
Definition: print.h:33
@ PRINT_UNALIGNED
Definition: print.h:38
@ PRINT_HTML
Definition: print.h:34
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:60
#define no_argument
Definition: getopt_long.h:25
#define required_argument
Definition: getopt_long.h:26
#define optional_argument
Definition: getopt_long.h:27
void helpVariables(unsigned short int pager)
Definition: help.c:358
void slashUsage(unsigned short int pager)
Definition: help.c:148
static struct @162 value
#define pg_log_error_hint(...)
Definition: logging.h:112
PGDLLIMPORT int optind
Definition: getopt.c:51
PGDLLIMPORT char * optarg
Definition: getopt.c:53
#define pg_log_warning(...)
Definition: pgfnames.c:24
char * c
unsigned short int expanded
Definition: print.h:114
bool tuples_only
Definition: print.h:126
char * tableAttr
Definition: print.h:137
bool DeleteVariable(VariableSpace space, const char *name)
Definition: variables.c:404

References ACT_FILE, ACT_SINGLE_QUERY, ACT_SINGLE_SLASH, options::dbname, DeleteVariable(), do_pset(), exit(), EXIT_FAILURE, EXIT_SUCCESS, printTableOpt::expanded, printTableOpt::fieldSep, printTableOpt::format, free, getopt_long(), _psqlSettings::getPassword, helpVariables(), no_argument, NOPAGER, optarg, optind, optional_argument, pg_fatal, pg_log_error_hint, pg_log_warning, pg_strdup(), _psqlSettings::popt, options::port, PRINT_CSV, PRINT_HTML, PRINT_UNALIGNED, _psqlSettings::progname, pset, _psqlSettings::quiet, printTableOpt::recordSep, required_argument, separator::separator, separator::separator_zero, setQFout(), SetVariable(), SetVariableBool(), showVersion(), simple_action_list_append(), slashUsage(), printTableOpt::tableAttr, printQueryOpt::topt, TRI_NO, TRI_YES, printTableOpt::tuples_only, usage(), options::username, value, and _psqlSettings::vars.

Referenced by main().

◆ process_psqlrc()

static void process_psqlrc ( char *  argv0)
static

Definition at line 774 of file startup.c.

775{
776 char home[MAXPGPATH];
777 char rc_file[MAXPGPATH];
779 char etc_path[MAXPGPATH];
780 char *envrc = getenv("PSQLRC");
781
783 pg_fatal("could not find own program executable");
784
785 get_etc_path(my_exec_path, etc_path);
786
787 snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
788 process_psqlrc_file(rc_file);
789
790 if (envrc != NULL && strlen(envrc) > 0)
791 {
792 /* might need to free() this */
793 char *envrc_alloc = pstrdup(envrc);
794
795 expand_tilde(&envrc_alloc);
796 process_psqlrc_file(envrc_alloc);
797 }
798 else if (get_home_path(home))
799 {
800 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
801 process_psqlrc_file(rc_file);
802 }
803}
void expand_tilde(char **filename)
Definition: common.c:2195
#define SYSPSQLRC
Definition: startup.c:35
#define PSQLRC
Definition: startup.c:36
static void process_psqlrc_file(char *filename)
Definition: startup.c:808
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:160
char my_exec_path[MAXPGPATH]
Definition: globals.c:80
char * pstrdup(const char *in)
Definition: mcxt.c:1696
#define MAXPGPATH
static char * argv0
Definition: pg_ctl.c:93
bool get_home_path(char *ret_path)
Definition: path.c:928
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition: path.c:834
#define snprintf
Definition: port.h:238

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

Referenced by main().

◆ process_psqlrc_file()

static void process_psqlrc_file ( char *  filename)
static

Definition at line 808 of file startup.c.

809{
810 char *psqlrc_minor,
811 *psqlrc_major;
812
813#if defined(WIN32) && (!defined(__MINGW32__))
814#define R_OK 4
815#endif
816
817 psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
818 psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
819
820 /* check for minor version first, then major, then no version */
821 if (access(psqlrc_minor, R_OK) == 0)
822 (void) process_file(psqlrc_minor, false);
823 else if (access(psqlrc_major, R_OK) == 0)
824 (void) process_file(psqlrc_major, false);
825 else if (access(filename, R_OK) == 0)
826 (void) process_file(filename, false);
827
828 free(psqlrc_minor);
829 free(psqlrc_major);
830}
short access
Definition: preproc-type.c:36

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

Referenced by process_psqlrc().

◆ prompt1_hook()

static bool prompt1_hook ( const char *  newval)
static

Definition at line 1098 of file startup.c.

1099{
1100 pset.prompt1 = newval ? newval : "";
1101 return true;
1102}
const char * prompt1
Definition: settings.h:162

References newval, _psqlSettings::prompt1, and pset.

Referenced by EstablishVariableSpace().

◆ prompt2_hook()

static bool prompt2_hook ( const char *  newval)
static

Definition at line 1105 of file startup.c.

1106{
1107 pset.prompt2 = newval ? newval : "";
1108 return true;
1109}
const char * prompt2
Definition: settings.h:163

References newval, _psqlSettings::prompt2, and pset.

Referenced by EstablishVariableSpace().

◆ prompt3_hook()

static bool prompt3_hook ( const char *  newval)
static

Definition at line 1112 of file startup.c.

1113{
1114 pset.prompt3 = newval ? newval : "";
1115 return true;
1116}
const char * prompt3
Definition: settings.h:164

References newval, _psqlSettings::prompt3, and pset.

Referenced by EstablishVariableSpace().

◆ quiet_hook()

static bool quiet_hook ( const char *  newval)
static

Definition at line 887 of file startup.c.

888{
889 return ParseVariableBool(newval, "QUIET", &pset.quiet);
890}

References newval, ParseVariableBool(), pset, and _psqlSettings::quiet.

Referenced by EstablishVariableSpace().

◆ show_all_results_hook()

static bool show_all_results_hook ( const char *  newval)
static

Definition at line 1150 of file startup.c.

1151{
1152 return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
1153}
bool show_all_results
Definition: settings.h:166

References newval, ParseVariableBool(), pset, and _psqlSettings::show_all_results.

Referenced by EstablishVariableSpace().

◆ show_context_hook()

static bool show_context_hook ( const char *  newval)
static

Definition at line 1164 of file startup.c.

1165{
1166 Assert(newval != NULL); /* else substitute hook messed up */
1167 if (pg_strcasecmp(newval, "never") == 0)
1169 else if (pg_strcasecmp(newval, "errors") == 0)
1171 else if (pg_strcasecmp(newval, "always") == 0)
1173 else
1174 {
1175 PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1176 return false;
1177 }
1178
1179 if (pset.db)
1181 return true;
1182}
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7418
@ PQSHOW_CONTEXT_NEVER
Definition: libpq-fe.h:159
@ PQSHOW_CONTEXT_ALWAYS
Definition: libpq-fe.h:161
@ PQSHOW_CONTEXT_ERRORS
Definition: libpq-fe.h:160
PGContextVisibility show_context
Definition: settings.h:167

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

Referenced by EstablishVariableSpace().

◆ show_context_substitute_hook()

static char * show_context_substitute_hook ( char *  newval)
static

Definition at line 1156 of file startup.c.

1157{
1158 if (newval == NULL)
1159 newval = pg_strdup("errors");
1160 return newval;
1161}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ showVersion()

static void showVersion ( void  )
static

Definition at line 839 of file startup.c.

840{
841 puts("psql (PostgreSQL) " PG_VERSION);
842}

Referenced by main(), and parse_psql_options().

◆ simple_action_list_append()

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

Definition at line 748 of file startup.c.

750{
752
754
755 cell->next = NULL;
756 cell->action = action;
757 if (val)
758 cell->val = pg_strdup(val);
759 else
760 cell->val = NULL;
761
762 if (list->tail)
763 list->tail->next = cell;
764 else
765 list->head = cell;
766 list->tail = cell;
767}
#define pg_malloc_object(type)
Definition: fe_memutils.h:50
long val
Definition: informix.c:689

References generate_unaccent_rules::action, SimpleActionListCell::action, sort-test::list, SimpleActionListCell::next, pg_malloc_object, pg_strdup(), SimpleActionListCell::val, and val.

Referenced by main(), and parse_psql_options().

◆ singleline_hook()

static bool singleline_hook ( const char *  newval)
static

Definition at line 893 of file startup.c.

894{
895 return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
896}
bool singleline
Definition: settings.h:150

References newval, ParseVariableBool(), pset, and _psqlSettings::singleline.

Referenced by EstablishVariableSpace().

◆ singlestep_hook()

static bool singlestep_hook ( const char *  newval)
static

Definition at line 899 of file startup.c.

900{
901 return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
902}
bool singlestep
Definition: settings.h:151

References newval, ParseVariableBool(), pset, and _psqlSettings::singlestep.

Referenced by EstablishVariableSpace().

◆ verbosity_hook()

static bool verbosity_hook ( const char *  newval)
static

Definition at line 1127 of file startup.c.

1128{
1129 Assert(newval != NULL); /* else substitute hook messed up */
1130 if (pg_strcasecmp(newval, "default") == 0)
1132 else if (pg_strcasecmp(newval, "verbose") == 0)
1134 else if (pg_strcasecmp(newval, "terse") == 0)
1136 else if (pg_strcasecmp(newval, "sqlstate") == 0)
1138 else
1139 {
1140 PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1141 return false;
1142 }
1143
1144 if (pset.db)
1146 return true;
1147}
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7406
@ PQERRORS_VERBOSE
Definition: libpq-fe.h:153
@ PQERRORS_DEFAULT
Definition: libpq-fe.h:152
@ PQERRORS_TERSE
Definition: libpq-fe.h:151
@ PQERRORS_SQLSTATE
Definition: libpq-fe.h:154
PGVerbosity verbosity
Definition: settings.h:165

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

Referenced by EstablishVariableSpace().

◆ verbosity_substitute_hook()

static char * verbosity_substitute_hook ( char *  newval)
static

Definition at line 1119 of file startup.c.

1120{
1121 if (newval == NULL)
1122 newval = pg_strdup("default");
1123 return newval;
1124}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

Variable Documentation

◆ pset

Definition at line 32 of file startup.c.

Referenced by AcceptResult(), autocommit_hook(), CheckConnection(), clean_extended_state(), ClearOrSaveAllResults(), ClearOrSaveResult(), command_no_begin(), comp_keyword_case_hook(), connection_warnings(), ConnectionUp(), describeAccessMethods(), describeAggregates(), describeConfigurationParameters(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), DescribeQuery(), describeRoleGrants(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_connect(), do_copy(), do_lo_export(), do_lo_import(), do_lo_unlink(), do_pset(), do_watch(), echo_hidden_command(), echo_hidden_hook(), echo_hook(), EstablishVariableSpace(), exec_command(), exec_command_a(), exec_command_bind(), exec_command_bind_named(), exec_command_C(), exec_command_close(), exec_command_conninfo(), exec_command_crosstabview(), exec_command_d(), exec_command_echo(), exec_command_encoding(), exec_command_errverbose(), exec_command_f(), exec_command_g(), exec_command_gdesc(), exec_command_getenv(), exec_command_gexec(), exec_command_gset(), exec_command_help(), exec_command_html(), exec_command_list(), exec_command_lo(), exec_command_parse(), 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(), exec_command_z(), ExecQueryAndProcessResults(), ExecQueryTuples(), fail_lo_xact(), fetch_count_hook(), finish_lo_xact(), finishInput(), get_create_object_cmd(), get_prompt(), handleCopyIn(), HandleCopyResult(), HandleSlashCmds(), helpSQL(), helpVariables(), hide_compression_hook(), hide_tableam_hook(), histcontrol_hook(), histsize_hook(), ignoreeof_hook(), indexOfColumn(), initializeInput(), is_superuser(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtendedStats(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listLargeObjects(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), log_locus_callback(), log_pre_callback(), lookup_object_oid(), main(), MainLoop(), objectDescription(), on_error_rollback_hook(), on_error_stop_hook(), parse_psql_options(), parse_slash_copy(), permissionsList(), pg_send_history(), print_lo_result(), printCrosstab(), printGSSInfo(), printHistory(), PrintNotifications(), PrintQueryResult(), PrintQueryStatus(), PrintQueryTuples(), PrintResultInCrosstab(), printSSLInfo(), process_command_g_options(), process_file(), prompt1_hook(), prompt2_hook(), prompt3_hook(), psql_get_variable(), PSQLexec(), PSQLexecWatch(), quiet_hook(), SendQuery(), session_username(), setQFout(), SetResultVariables(), SetShellResultVariables(), SetupGOutput(), show_all_results_hook(), show_context_hook(), singleline_hook(), singlestep_hook(), skip_white_space(), slashUsage(), standard_strings(), start_lo_xact(), StoreQueryTuple(), SyncVariables(), UnsyncVariables(), usage(), validateSQLNamePattern(), and verbosity_hook().