PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "portability/instr_time.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 charbool_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 charfetch_count_substitute_hook (char *newval)
 
static bool fetch_count_hook (const char *newval)
 
static bool histfile_hook (const char *newval)
 
static charhistsize_substitute_hook (char *newval)
 
static bool histsize_hook (const char *newval)
 
static charwatch_interval_substitute_hook (char *newval)
 
static bool watch_interval_hook (const char *newval)
 
static charignoreeof_substitute_hook (char *newval)
 
static bool ignoreeof_hook (const char *newval)
 
static charecho_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 charcomp_keyword_case_substitute_hook (char *newval)
 
static bool comp_keyword_case_hook (const char *newval)
 
static charhistcontrol_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 charverbosity_substitute_hook (char *newval)
 
static bool verbosity_hook (const char *newval)
 
static bool show_all_results_hook (const char *newval)
 
static charshow_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 90 of file startup.c.

◆ PARAMS_ARRAY_SIZE

#define PARAMS_ARRAY_SIZE   8

◆ PSQLRC

#define PSQLRC   ".psqlrc"

Definition at line 37 of file startup.c.

◆ SYSPSQLRC

#define SYSPSQLRC   "psqlrc"

Definition at line 36 of file startup.c.

Typedef Documentation

◆ SimpleActionList

◆ SimpleActionListCell

Enumeration Type Documentation

◆ _actions

Enumerator
ACT_SINGLE_QUERY 
ACT_SINGLE_SLASH 
ACT_FILE 

Definition at line 47 of file startup.c.

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

Function Documentation

◆ autocommit_hook()

static bool autocommit_hook ( const char newval)
static

Definition at line 884 of file startup.c.

885{
886 return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
887}
PsqlSettings pset
Definition startup.c:33
#define newval
bool autocommit
Definition settings.h:165
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition variables.c:109

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

Referenced by EstablishVariableSpace().

◆ bool_substitute_hook()

static char * bool_substitute_hook ( char newval)
static

Definition at line 867 of file startup.c.

868{
869 if (newval == NULL)
870 {
871 /* "\unset FOO" becomes "\set FOO off" */
872 newval = pg_strdup("off");
873 }
874 else if (newval[0] == '\0')
875 {
876 /* "\set FOO" becomes "\set FOO on" */
878 newval = pg_strdup("on");
879 }
880 return newval;
881}
char * pg_strdup(const char *in)
Definition fe_memutils.c:85
void pg_free(void *ptr)
static int fb(int x)

References fb(), 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 1072 of file startup.c.

1073{
1074 Assert(newval != NULL); /* else substitute hook messed up */
1075 if (pg_strcasecmp(newval, "preserve-upper") == 0)
1077 else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1079 else if (pg_strcasecmp(newval, "upper") == 0)
1081 else if (pg_strcasecmp(newval, "lower") == 0)
1083 else
1084 {
1085 PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1086 "lower, upper, preserve-lower, preserve-upper");
1087 return false;
1088 }
1089 return true;
1090}
#define Assert(condition)
Definition c.h:943
int pg_strcasecmp(const char *s1, const char *s2)
@ PSQL_COMP_CASE_PRESERVE_LOWER
Definition settings.h:66
@ PSQL_COMP_CASE_LOWER
Definition settings.h:68
@ PSQL_COMP_CASE_PRESERVE_UPPER
Definition settings.h:65
@ PSQL_COMP_CASE_UPPER
Definition settings.h:67
PSQL_COMP_CASE comp_case
Definition settings.h:179
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition variables.c:486

References Assert, _psqlSettings::comp_case, fb(), 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 1064 of file startup.c.

1065{
1066 if (newval == NULL)
1067 newval = pg_strdup("preserve-upper");
1068 return newval;
1069}

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

Referenced by EstablishVariableSpace().

◆ echo_hidden_hook()

static bool echo_hidden_hook ( const char newval)
static

Definition at line 1022 of file startup.c.

1023{
1024 Assert(newval != NULL); /* else substitute hook messed up */
1025 if (pg_strcasecmp(newval, "noexec") == 0)
1027 else
1028 {
1029 bool on_off;
1030
1033 else
1034 {
1035 PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1036 return false;
1037 }
1038 }
1039 return true;
1040}
@ PSQL_ECHO_HIDDEN_NOEXEC
Definition settings.h:53
@ PSQL_ECHO_HIDDEN_OFF
Definition settings.h:51
@ PSQL_ECHO_HIDDEN_ON
Definition settings.h:52
PSQL_ECHO_HIDDEN echo_hidden
Definition settings.h:177

References Assert, _psqlSettings::echo_hidden, fb(), 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 1002 of file startup.c.

1003{
1004 Assert(newval != NULL); /* else substitute hook messed up */
1005 if (pg_strcasecmp(newval, "queries") == 0)
1007 else if (pg_strcasecmp(newval, "errors") == 0)
1009 else if (pg_strcasecmp(newval, "all") == 0)
1011 else if (pg_strcasecmp(newval, "none") == 0)
1013 else
1014 {
1015 PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
1016 return false;
1017 }
1018 return true;
1019}
@ PSQL_ECHO_ALL
Definition settings.h:46
@ PSQL_ECHO_ERRORS
Definition settings.h:45
@ PSQL_ECHO_NONE
Definition settings.h:43
@ PSQL_ECHO_QUERIES
Definition settings.h:44
PSQL_ECHO echo
Definition settings.h:176

References Assert, _psqlSettings::echo, fb(), 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 994 of file startup.c.

995{
996 if (newval == NULL)
997 newval = pg_strdup("none");
998 return newval;
999}

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

Referenced by EstablishVariableSpace().

◆ empty_signal_handler()

static void empty_signal_handler ( SIGNAL_ARGS  )
static

Definition at line 116 of file startup.c.

117{
118}

Referenced by main().

◆ EstablishVariableSpace()

static void EstablishVariableSpace ( void  )
static

Definition at line 1222 of file startup.c.

1223{
1225
1226 SetVariableHooks(pset.vars, "AUTOCOMMIT",
1229 SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1232 SetVariableHooks(pset.vars, "QUIET",
1234 quiet_hook);
1235 SetVariableHooks(pset.vars, "SINGLELINE",
1238 SetVariableHooks(pset.vars, "SINGLESTEP",
1241 SetVariableHooks(pset.vars, "FETCH_COUNT",
1244 SetVariableHooks(pset.vars, "HISTFILE",
1245 NULL,
1247 SetVariableHooks(pset.vars, "HISTSIZE",
1250 SetVariableHooks(pset.vars, "IGNOREEOF",
1253 SetVariableHooks(pset.vars, "ECHO",
1255 echo_hook);
1256 SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1259 SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1262 SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1265 SetVariableHooks(pset.vars, "HISTCONTROL",
1268 SetVariableHooks(pset.vars, "PROMPT1",
1269 NULL,
1270 prompt1_hook);
1271 SetVariableHooks(pset.vars, "PROMPT2",
1272 NULL,
1273 prompt2_hook);
1274 SetVariableHooks(pset.vars, "PROMPT3",
1275 NULL,
1276 prompt3_hook);
1277 SetVariableHooks(pset.vars, "VERBOSITY",
1280 SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
1283 SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1286 SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1289 SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1292 SetVariableHooks(pset.vars, "WATCH_INTERVAL",
1295}
static char * verbosity_substitute_hook(char *newval)
Definition startup.c:1143
static bool show_context_hook(const char *newval)
Definition startup.c:1188
static bool hide_tableam_hook(const char *newval)
Definition startup.c:1216
static bool quiet_hook(const char *newval)
Definition startup.c:896
static bool singleline_hook(const char *newval)
Definition startup.c:902
static bool prompt2_hook(const char *newval)
Definition startup.c:1129
static bool comp_keyword_case_hook(const char *newval)
Definition startup.c:1072
static bool histcontrol_hook(const char *newval)
Definition startup.c:1101
static bool prompt1_hook(const char *newval)
Definition startup.c:1122
static bool verbosity_hook(const char *newval)
Definition startup.c:1151
static char * ignoreeof_substitute_hook(char *newval)
Definition startup.c:967
static bool show_all_results_hook(const char *newval)
Definition startup.c:1174
static bool fetch_count_hook(const char *newval)
Definition startup.c:922
static char * fetch_count_substitute_hook(char *newval)
Definition startup.c:914
static bool histsize_hook(const char *newval)
Definition startup.c:946
static char * echo_substitute_hook(char *newval)
Definition startup.c:994
static char * watch_interval_substitute_hook(char *newval)
Definition startup.c:952
static bool hide_compression_hook(const char *newval)
Definition startup.c:1209
static char * bool_substitute_hook(char *newval)
Definition startup.c:867
static bool singlestep_hook(const char *newval)
Definition startup.c:908
static bool histfile_hook(const char *newval)
Definition startup.c:928
static bool prompt3_hook(const char *newval)
Definition startup.c:1136
static bool on_error_rollback_hook(const char *newval)
Definition startup.c:1043
static char * histsize_substitute_hook(char *newval)
Definition startup.c:938
static bool on_error_stop_hook(const char *newval)
Definition startup.c:890
static bool autocommit_hook(const char *newval)
Definition startup.c:884
static char * show_context_substitute_hook(char *newval)
Definition startup.c:1180
static bool watch_interval_hook(const char *newval)
Definition startup.c:960
static bool ignoreeof_hook(const char *newval)
Definition startup.c:988
static bool echo_hidden_hook(const char *newval)
Definition startup.c:1022
static bool echo_hook(const char *newval)
Definition startup.c:1002
static char * histcontrol_substitute_hook(char *newval)
Definition startup.c:1093
static char * comp_keyword_case_substitute_hook(char *newval)
Definition startup.c:1064
VariableSpace vars
Definition settings.h:151
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition variables.c:384
VariableSpace CreateVariableSpace(void)
Definition variables.c:53

References autocommit_hook(), bool_substitute_hook(), comp_keyword_case_hook(), comp_keyword_case_substitute_hook(), CreateVariableSpace(), echo_hidden_hook(), echo_hook(), echo_substitute_hook(), fb(), 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(), verbosity_substitute_hook(), watch_interval_hook(), and watch_interval_substitute_hook().

Referenced by main().

◆ fetch_count_hook()

static bool fetch_count_hook ( const char newval)
static

Definition at line 922 of file startup.c.

923{
924 return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
925}
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition variables.c:158

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 914 of file startup.c.

915{
916 if (newval == NULL)
917 newval = pg_strdup("0");
918 return newval;
919}

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

Referenced by EstablishVariableSpace().

◆ hide_compression_hook()

static bool hide_compression_hook ( const char newval)
static

Definition at line 1209 of file startup.c.

1210{
1211 return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1213}
bool hide_compression
Definition settings.h:170

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 1216 of file startup.c.

1217{
1218 return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1219}
bool hide_tableam
Definition settings.h:171

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

Referenced by EstablishVariableSpace().

◆ histcontrol_hook()

static bool histcontrol_hook ( const char newval)
static

Definition at line 1101 of file startup.c.

1102{
1103 Assert(newval != NULL); /* else substitute hook messed up */
1104 if (pg_strcasecmp(newval, "ignorespace") == 0)
1106 else if (pg_strcasecmp(newval, "ignoredups") == 0)
1108 else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1110 else if (pg_strcasecmp(newval, "none") == 0)
1112 else
1113 {
1114 PsqlVarEnumError("HISTCONTROL", newval,
1115 "none, ignorespace, ignoredups, ignoreboth");
1116 return false;
1117 }
1118 return true;
1119}
@ hctl_ignoredups
Definition settings.h:90
@ hctl_ignoreboth
Definition settings.h:91
@ hctl_none
Definition settings.h:88
@ hctl_ignorespace
Definition settings.h:89
HistControl histcontrol
Definition settings.h:180

References Assert, fb(), 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 1093 of file startup.c.

1094{
1095 if (newval == NULL)
1096 newval = pg_strdup("none");
1097 return newval;
1098}

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

Referenced by EstablishVariableSpace().

◆ histfile_hook()

static bool histfile_hook ( const char newval)
static

Definition at line 928 of file startup.c.

929{
930 /*
931 * Someday we might try to validate the filename, but for now, this is
932 * just a placeholder to ensure HISTFILE is known to tab completion.
933 */
934 return true;
935}

Referenced by EstablishVariableSpace().

◆ histsize_hook()

static bool histsize_hook ( const char newval)
static

Definition at line 946 of file startup.c.

947{
948 return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
949}

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

Referenced by EstablishVariableSpace().

◆ histsize_substitute_hook()

static char * histsize_substitute_hook ( char newval)
static

Definition at line 938 of file startup.c.

939{
940 if (newval == NULL)
941 newval = pg_strdup("500");
942 return newval;
943}

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

Referenced by EstablishVariableSpace().

◆ ignoreeof_hook()

static bool ignoreeof_hook ( const char newval)
static

Definition at line 988 of file startup.c.

989{
990 return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
991}

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

Referenced by EstablishVariableSpace().

◆ ignoreeof_substitute_hook()

static char * ignoreeof_substitute_hook ( char newval)
static

Definition at line 967 of file startup.c.

968{
969 int dummy;
970
971 /*
972 * This tries to mimic the behavior of bash, to wit "If set, the value is
973 * the number of consecutive EOF characters which must be typed as the
974 * first characters on an input line before bash exits. If the variable
975 * exists but does not have a numeric value, or has no value, the default
976 * value is 10. If it does not exist, EOF signifies the end of input to
977 * the shell." Unlike bash, however, we insist on the stored value
978 * actually being a valid integer.
979 */
980 if (newval == NULL)
981 newval = pg_strdup("0");
982 else if (!ParseVariableNum(newval, NULL, &dummy))
983 newval = pg_strdup("10");
984 return newval;
985}

References fb(), 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 100 of file startup.c.

101{
102 if (pset.inputfile)
103 {
105 *lineno = pset.lineno;
106 }
107 else
108 {
109 *filename = NULL;
110 *lineno = 0;
111 }
112}
static char * filename
Definition pg_dumpall.c:133
uint64 lineno
Definition settings.h:144
char * inputfile
Definition settings.h:143

References fb(), filename, _psqlSettings::inputfile, _psqlSettings::lineno, and pset.

◆ log_pre_callback()

static void log_pre_callback ( void  )
static

Definition at line 93 of file startup.c.

94{
95 if (pset.queryFout && pset.queryFout != stdout)
97}
FILE * queryFout
Definition settings.h:105

References fb(), pset, and _psqlSettings::queryFout.

Referenced by main().

◆ main()

int main ( int  argc,
char argv[] 
)

Definition at line 127 of file startup.c.

128{
129 struct adhoc_opts options;
130 int successResult;
131 char *password = NULL;
132 bool new_pass;
133
134 pg_logging_init(argv[0]);
137 set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
138
139 if (argc > 1)
140 {
141 if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
142 {
143 usage(NOPAGER);
145 }
146 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
147 {
148 showVersion();
150 }
151 }
152
153 pset.progname = get_progname(argv[0]);
154
155 pset.db = NULL;
160 pset.queryFoutPipe = false;
165
166 /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
168 pset.popt.topt.border = 1;
169 pset.popt.topt.pager = 1;
171 pset.popt.topt.start_table = true;
172 pset.popt.topt.stop_table = true;
174
176 pset.popt.topt.csvFieldSep[1] = '\0';
177
181
183
184 /* We must get COLUMNS here before readline() sets it */
185 pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
186
187 pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
188
190
192
193 /* Create variables showing psql version number */
194 SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
195 SetVariable(pset.vars, "VERSION_NAME", PG_VERSION);
197
198 /* Initialize variables for last error */
199 SetVariable(pset.vars, "LAST_ERROR_MESSAGE", "");
200 SetVariable(pset.vars, "LAST_ERROR_SQLSTATE", "00000");
201
202 /* Default values for variables (that don't match the result of \unset) */
203 SetVariableBool(pset.vars, "AUTOCOMMIT");
207 SetVariableBool(pset.vars, "SHOW_ALL_RESULTS");
208
209 /* Initialize pipeline variables */
210 SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0");
211 SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0");
212 SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0");
213
214 parse_psql_options(argc, argv, &options);
215
216 /*
217 * If no action was specified and we're in non-interactive mode, treat it
218 * as if the user had specified "-f -". This lets single-transaction mode
219 * work in this case.
220 */
221 if (options.actions.head == NULL && pset.notty)
223
224 /* Bail out if -1 was specified but will be ignored. */
225 if (options.single_txn && options.actions.head == NULL)
226 pg_fatal("-1 can only be used in non-interactive mode");
227
230 {
233 }
236 {
239 }
240
241 if (pset.getPassword == TRI_YES)
242 {
243 /*
244 * We can't be sure yet of the username that will be used, so don't
245 * offer a potentially wrong one. Typical uses of this option are
246 * noninteractive anyway. (Note: since we've not yet set up our
247 * cancel handler, there's no need to use simple_prompt_extended.)
248 */
249 password = simple_prompt("Password: ", false);
250 }
251
252 /* loop until we have a password if requested by backend */
253 do
254 {
255#define PARAMS_ARRAY_SIZE 8
256 const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
257 const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
258
259 keywords[0] = "host";
260 values[0] = options.host;
261 keywords[1] = "port";
262 values[1] = options.port;
263 keywords[2] = "user";
265 keywords[3] = "password";
266 values[3] = password;
267 keywords[4] = "dbname"; /* see do_connect() */
268 values[4] = (options.list_dbs && options.dbname == NULL) ?
269 "postgres" : options.dbname;
270 keywords[5] = "fallback_application_name";
271 values[5] = pset.progname;
272 keywords[6] = "client_encoding";
273 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
274 keywords[7] = NULL;
275 values[7] = NULL;
276
277 new_pass = false;
279 free(keywords);
280 free(values);
281
282 if (PQstatus(pset.db) == CONNECTION_BAD &&
284 !password &&
286 {
287 /*
288 * Before closing the old PGconn, extract the user name that was
289 * actually connected with --- it might've come out of a URI or
290 * connstring "database name" rather than options.username.
291 */
292 const char *realusername = PQuser(pset.db);
293 char *password_prompt;
294
295 if (realusername && realusername[0])
296 password_prompt = psprintf(_("Password for user %s: "),
298 else
299 password_prompt = pg_strdup(_("Password: "));
301
304 new_pass = true;
305 }
306 } while (new_pass);
307
309 {
313 }
314
316
317#ifndef WIN32
318
319 /*
320 * do_watch() needs signal handlers installed (otherwise sigwait() will
321 * filter them out on some platforms), but doesn't need them to do
322 * anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
323 * arrives due to a race when do_watch() cancels an itimer).
324 */
327#endif
328
330
331 /* initialize timing infrastructure (required for INSTR_* calls) */
333
335
336 if (options.list_dbs)
337 {
338 int success;
339
340 if (!options.no_psqlrc)
341 process_psqlrc(argv[0]);
342
343 success = listAllDbs(NULL, false);
346 }
347
348 if (options.logfilename)
349 {
350 pset.logfile = fopen(options.logfilename, "a");
351 if (!pset.logfile)
352 pg_fatal("could not open log file \"%s\": %m",
353 options.logfilename);
354 }
355
356 if (!options.no_psqlrc)
357 process_psqlrc(argv[0]);
358
359 /*
360 * If any actions were given by user, process them in the order in which
361 * they were specified. Note single_txn is only effective in this mode.
362 */
363 if (options.actions.head != NULL)
364 {
365 PGresult *res;
367
368 successResult = EXIT_SUCCESS; /* silence compiler */
369
370 if (options.single_txn)
371 {
372 if ((res = PSQLexec("BEGIN")) == NULL)
373 {
375 {
377 goto error;
378 }
379 }
380 else
381 PQclear(res);
382 }
383
384 for (cell = options.actions.head; cell; cell = cell->next)
385 {
386 if (cell->action == ACT_SINGLE_QUERY)
387 {
389
390 if (pset.echo == PSQL_ECHO_ALL)
391 puts(cell->val);
392
395 }
396 else if (cell->action == ACT_SINGLE_SLASH)
397 {
400
402
403 if (pset.echo == PSQL_ECHO_ALL)
404 puts(cell->val);
405
408 cell->val, strlen(cell->val),
412
415 NULL,
418
421 }
422 else if (cell->action == ACT_FILE)
423 {
424 successResult = process_file(cell->val, false);
425 }
426 else
427 {
428 /* should never come here */
429 Assert(false);
430 }
431
433 break;
434 }
435
436 if (options.single_txn)
437 {
438 /*
439 * Rollback the contents of the single transaction if the caller
440 * has set ON_ERROR_STOP and one of the steps has failed. This
441 * check needs to match the one done a couple of lines above.
442 */
444 "ROLLBACK" : "COMMIT");
445 if (res == NULL)
446 {
448 {
450 goto error;
451 }
452 }
453 else
454 PQclear(res);
455 }
456
457error:
458 ;
459 }
460
461 /*
462 * or otherwise enter interactive main loop
463 */
464 else
465 {
468 if (!pset.quiet)
469 printf(_("Type \"help\" for help.\n\n"));
470 initializeInput(options.no_readline ? 0 : 1);
472 }
473
474 /* clean up */
475 if (pset.logfile)
477 if (pset.db)
479 if (pset.dead_conn)
481 setQFout(NULL);
482
483 return successResult;
484}
PGresult * PSQLexec(const char *query)
Definition common.c:655
void psql_setup_cancel_handler(void)
Definition common.c:325
void NoticeProcessor(void *arg, const char *message)
Definition common.c:279
bool standard_strings(void)
Definition common.c:2500
bool setQFout(const char *fname)
Definition common.c:144
bool SendQuery(const char *query)
Definition common.c:1118
static void empty_signal_handler(SIGNAL_ARGS)
Definition startup.c:116
static void process_psqlrc(char *argv0)
Definition startup.c:783
static void EstablishVariableSpace(void)
Definition startup.c:1222
static void log_pre_callback(void)
Definition startup.c:93
#define NOPAGER
Definition startup.c:90
#define PARAMS_ARRAY_SIZE
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition startup.c:757
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition startup.c:492
static void showVersion(void)
Definition startup.c:848
static Datum values[MAXATTR]
Definition bootstrap.c:190
#define PG_TEXTDOMAIN(domain)
Definition c.h:1303
#define CppAsString2(x)
Definition c.h:506
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition command.c:231
void SyncVariables(void)
Definition command.c:4571
void connection_warnings(bool in_startup)
Definition command.c:4443
@ PSQL_CMD_ERROR
Definition command.h:22
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition exec.c:430
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:953
#define _(x)
Definition elog.c:96
int PQconnectionNeedsPassword(const PGconn *conn)
ConnStatusType PQstatus(const PGconn *conn)
void PQfinish(PGconn *conn)
char * PQuser(const PGconn *conn)
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
char * PQerrorMessage(const PGconn *conn)
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition fe-connect.c:775
int PQenv2encoding(void)
Definition fe-misc.c:1285
#define pg_malloc_array(type, count)
Definition fe_memutils.h:56
void refresh_utf8format(const printTableOpt *opt)
Definition print.c:3889
void setDecimalLocale(void)
Definition print.c:3839
@ UNICODE_LINESTYLE_SINGLE
Definition print.h:101
@ PRINT_ALIGNED
Definition print.h:31
static bool success
Definition initdb.c:188
void initializeInput(int flags)
Definition input.c:344
void pg_initialize_timing(void)
Definition instr_time.c:82
static const JsonPathKeyword keywords[]
#define PQclear
@ CONNECTION_BAD
Definition libpq-fe.h:91
void pg_logging_init(const char *argv0)
Definition logging.c:85
void pg_logging_set_locus_callback(void(*cb)(const char **filename, uint64 *lineno))
Definition logging.c:204
static void(* log_locus_callback)(const char **, uint64 *)
Definition logging.c:27
void pg_logging_config(int new_flags)
Definition logging.c:168
void pg_logging_set_pre_callback(void(*cb)(void))
Definition logging.c:198
#define pg_log_error(...)
Definition logging.h:108
#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
static void usage(void)
#define pg_fatal(...)
static void process_file(const char *filename, int weight)
Definition pgbench.c:6173
#define pqsignal
Definition port.h:547
const char * get_progname(const char *argv0)
Definition path.c:652
#define printf(...)
Definition port.h:266
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
void psql_scan_destroy(PsqlScanState state)
Definition psqlscan.l:1034
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
Definition psqlscan.l:1013
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
Definition psqlscan.l:1053
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
Definition psqlscan.l:1071
#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:193
#define EXIT_FAILURE
Definition settings.h:197
#define EXIT_BADCONN
Definition settings.h:200
#define DEFAULT_PROMPT2
Definition settings.h:27
#define EXIT_USER
Definition settings.h:202
#define DEFAULT_CSV_FIELD_SEP
Definition settings.h:14
#define free(a)
char * simple_prompt(const char *prompt, bool echo)
Definition sprompt.c:38
static void error(void)
static char * password
Definition streamutil.c:51
char * dbname
Definition streamutil.c:49
struct SimpleActionListCell * next
Definition startup.c:56
enum _actions action
Definition startup.c:57
printQueryOpt popt
Definition settings.h:112
FILE * logfile
Definition settings.h:149
bool on_error_stop
Definition settings.h:166
PGconn * dead_conn
Definition settings.h:158
PGresult * last_error_result
Definition settings.h:110
FILE * copyStream
Definition settings.h:108
PGconn * db
Definition settings.h:103
bool queryFoutPipe
Definition settings.h:106
enum trivalue getPassword
Definition settings.h:137
bool cur_cmd_interactive
Definition settings.h:140
FILE * cur_cmd_source
Definition settings.h:138
const char * progname
Definition settings.h:142
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
@ 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:462
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition variables.c:281
#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_BADCONN, EXIT_FAILURE, EXIT_SUCCESS, EXIT_USER, fb(), 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_initialize_timing(), 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(), 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, 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 1043 of file startup.c.

1044{
1045 Assert(newval != NULL); /* else substitute hook messed up */
1046 if (pg_strcasecmp(newval, "interactive") == 0)
1048 else
1049 {
1050 bool on_off;
1051
1054 else
1055 {
1056 PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1057 return false;
1058 }
1059 }
1060 return true;
1061}
@ PSQL_ERROR_ROLLBACK_INTERACTIVE
Definition settings.h:59
@ PSQL_ERROR_ROLLBACK_ON
Definition settings.h:60
@ PSQL_ERROR_ROLLBACK_OFF
Definition settings.h:58
PSQL_ERROR_ROLLBACK on_error_rollback
Definition settings.h:178

References Assert, fb(), 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 890 of file startup.c.

891{
892 return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
893}

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 492 of file startup.c.

493{
494 static struct option long_options[] =
495 {
496 {"echo-all", no_argument, NULL, 'a'},
497 {"no-align", no_argument, NULL, 'A'},
498 {"command", required_argument, NULL, 'c'},
499 {"dbname", required_argument, NULL, 'd'},
500 {"echo-queries", no_argument, NULL, 'e'},
501 {"echo-errors", no_argument, NULL, 'b'},
502 {"echo-hidden", no_argument, NULL, 'E'},
503 {"file", required_argument, NULL, 'f'},
504 {"field-separator", required_argument, NULL, 'F'},
505 {"field-separator-zero", no_argument, NULL, 'z'},
506 {"host", required_argument, NULL, 'h'},
507 {"html", no_argument, NULL, 'H'},
508 {"list", no_argument, NULL, 'l'},
509 {"log-file", required_argument, NULL, 'L'},
510 {"no-readline", no_argument, NULL, 'n'},
511 {"single-transaction", no_argument, NULL, '1'},
512 {"output", required_argument, NULL, 'o'},
513 {"port", required_argument, NULL, 'p'},
514 {"pset", required_argument, NULL, 'P'},
515 {"quiet", no_argument, NULL, 'q'},
516 {"record-separator", required_argument, NULL, 'R'},
517 {"record-separator-zero", no_argument, NULL, '0'},
518 {"single-step", no_argument, NULL, 's'},
519 {"single-line", no_argument, NULL, 'S'},
520 {"tuples-only", no_argument, NULL, 't'},
521 {"table-attr", required_argument, NULL, 'T'},
522 {"username", required_argument, NULL, 'U'},
523 {"set", required_argument, NULL, 'v'},
524 {"variable", required_argument, NULL, 'v'},
525 {"version", no_argument, NULL, 'V'},
526 {"no-password", no_argument, NULL, 'w'},
527 {"password", no_argument, NULL, 'W'},
528 {"expanded", no_argument, NULL, 'x'},
529 {"no-psqlrc", no_argument, NULL, 'X'},
530 {"help", optional_argument, NULL, 1},
531 {"csv", no_argument, NULL, 2},
532 {NULL, 0, NULL, 0}
533 };
534
535 int optindex;
536 int c;
537
538 memset(options, 0, sizeof *options);
539
540 while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
541 long_options, &optindex)) != -1)
542 {
543 switch (c)
544 {
545 case 'a':
546 SetVariable(pset.vars, "ECHO", "all");
547 break;
548 case 'A':
550 break;
551 case 'b':
552 SetVariable(pset.vars, "ECHO", "errors");
553 break;
554 case 'c':
555 if (optarg[0] == '\\')
558 optarg + 1);
559 else
562 optarg);
563 break;
564 case 'd':
566 break;
567 case 'e':
568 SetVariable(pset.vars, "ECHO", "queries");
569 break;
570 case 'E':
571 SetVariableBool(pset.vars, "ECHO_HIDDEN");
572 break;
573 case 'f':
575 ACT_FILE,
576 optarg);
577 break;
578 case 'F':
581 break;
582 case 'h':
583 options->host = pg_strdup(optarg);
584 break;
585 case 'H':
587 break;
588 case 'l':
589 options->list_dbs = true;
590 break;
591 case 'L':
592 options->logfilename = pg_strdup(optarg);
593 break;
594 case 'n':
595 options->no_readline = true;
596 break;
597 case 'o':
598 if (!setQFout(optarg))
600 break;
601 case 'p':
603 break;
604 case 'P':
605 {
606 char *value;
607 char *equal_loc;
608 bool result;
609
611 equal_loc = strchr(value, '=');
612 if (!equal_loc)
613 result = do_pset(value, NULL, &pset.popt, true);
614 else
615 {
616 *equal_loc = '\0';
617 result = do_pset(value, equal_loc + 1, &pset.popt, true);
618 }
619
620 if (!result)
621 pg_fatal("could not set printing parameter \"%s\"", value);
622
623 free(value);
624 break;
625 }
626 case 'q':
627 SetVariableBool(pset.vars, "QUIET");
628 break;
629 case 'R':
632 break;
633 case 's':
634 SetVariableBool(pset.vars, "SINGLESTEP");
635 break;
636 case 'S':
637 SetVariableBool(pset.vars, "SINGLELINE");
638 break;
639 case 't':
640 pset.popt.topt.tuples_only = true;
641 break;
642 case 'T':
644 break;
645 case 'U':
647 break;
648 case 'v':
649 {
650 char *value;
651 char *equal_loc;
652
654 equal_loc = strchr(value, '=');
655 if (!equal_loc)
656 {
658 exit(EXIT_FAILURE); /* error already printed */
659 }
660 else
661 {
662 *equal_loc = '\0';
663 if (!SetVariable(pset.vars, value, equal_loc + 1))
664 exit(EXIT_FAILURE); /* error already printed */
665 }
666
667 free(value);
668 break;
669 }
670 case 'V':
671 showVersion();
673 case 'w':
675 break;
676 case 'W':
678 break;
679 case 'x':
680 pset.popt.topt.expanded = true;
681 break;
682 case 'X':
683 options->no_psqlrc = true;
684 break;
685 case 'z':
687 break;
688 case '0':
690 break;
691 case '1':
692 options->single_txn = true;
693 break;
694 case '?':
695 if (optind <= argc &&
696 strcmp(argv[optind - 1], "-?") == 0)
697 {
698 /* actual help option given */
699 usage(NOPAGER);
701 }
702 else
703 {
704 /* getopt error (unknown option or missing argument) */
705 goto unknown_option;
706 }
707 break;
708 case 1:
709 {
710 if (!optarg || strcmp(optarg, "options") == 0)
711 usage(NOPAGER);
712 else if (optarg && strcmp(optarg, "commands") == 0)
714 else if (optarg && strcmp(optarg, "variables") == 0)
716 else
717 goto unknown_option;
718
720 }
721 break;
722 case 2:
724 break;
725 default:
727 /* getopt_long already emitted a complaint */
728 pg_log_error_hint("Try \"%s --help\" for more information.",
729 pset.progname);
731 }
732 }
733
734 /*
735 * if we still have arguments, use it as the database name and username
736 */
737 while (argc - optind >= 1)
738 {
739 if (!options->dbname)
740 options->dbname = argv[optind];
741 else if (!options->username)
742 options->username = argv[optind];
743 else if (!pset.quiet)
744 pg_log_warning("extra command-line argument \"%s\" ignored",
745 argv[optind]);
746
747 optind++;
748 }
749}
uint32 result
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition command.c:5078
@ 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:370
void slashUsage(unsigned short int pager)
Definition help.c:148
static struct @177 value
#define pg_log_error_hint(...)
Definition logging.h:114
PGDLLIMPORT int optind
Definition getopt.c:47
PGDLLIMPORT char * optarg
Definition getopt.c:49
#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:474

References ACT_FILE, ACT_SINGLE_QUERY, ACT_SINGLE_SLASH, options::dbname, DeleteVariable(), do_pset(), EXIT_FAILURE, EXIT_SUCCESS, printTableOpt::expanded, fb(), 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, result, 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 783 of file startup.c.

784{
785 char home[MAXPGPATH];
786 char rc_file[MAXPGPATH];
788 char etc_path[MAXPGPATH];
789 char *envrc = getenv("PSQLRC");
790
792 pg_fatal("could not find own program executable");
793
795
798
799 if (envrc != NULL && strlen(envrc) > 0)
800 {
801 /* might need to free() this */
802 char *envrc_alloc = pstrdup(envrc);
803
806 }
807 else if (get_home_path(home))
808 {
809 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
811 }
812}
void expand_tilde(char **filename)
Definition common.c:2576
#define SYSPSQLRC
Definition startup.c:36
#define PSQLRC
Definition startup.c:37
static void process_psqlrc_file(char *filename)
Definition startup.c:817
int find_my_exec(const char *argv0, char *retpath)
Definition exec.c:161
char my_exec_path[MAXPGPATH]
Definition globals.c:83
char * pstrdup(const char *in)
Definition mcxt.c:1781
#define MAXPGPATH
static char * argv0
Definition pg_ctl.c:94
bool get_home_path(char *ret_path)
Definition path.c:1005
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition path.c:911
#define snprintf
Definition port.h:260

References argv0, expand_tilde(), fb(), 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 817 of file startup.c.

818{
819 char *psqlrc_minor,
821
822#if defined(WIN32) && (!defined(__MINGW32__))
823#define R_OK 4
824#endif
825
828
829 /* check for minor version first, then major, then no version */
830 if (access(psqlrc_minor, R_OK) == 0)
832 else if (access(psqlrc_major, R_OK) == 0)
833 (void) process_file(psqlrc_major, false);
834 else if (access(filename, R_OK) == 0)
835 (void) process_file(filename, false);
836
839}
short access

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

Referenced by process_psqlrc().

◆ prompt1_hook()

static bool prompt1_hook ( const char newval)
static

Definition at line 1122 of file startup.c.

1123{
1124 pset.prompt1 = newval ? newval : "";
1125 return true;
1126}
const char * prompt1
Definition settings.h:181

References newval, _psqlSettings::prompt1, and pset.

Referenced by EstablishVariableSpace().

◆ prompt2_hook()

static bool prompt2_hook ( const char newval)
static

Definition at line 1129 of file startup.c.

1130{
1131 pset.prompt2 = newval ? newval : "";
1132 return true;
1133}
const char * prompt2
Definition settings.h:182

References newval, _psqlSettings::prompt2, and pset.

Referenced by EstablishVariableSpace().

◆ prompt3_hook()

static bool prompt3_hook ( const char newval)
static

Definition at line 1136 of file startup.c.

1137{
1138 pset.prompt3 = newval ? newval : "";
1139 return true;
1140}
const char * prompt3
Definition settings.h:183

References newval, _psqlSettings::prompt3, and pset.

Referenced by EstablishVariableSpace().

◆ quiet_hook()

static bool quiet_hook ( const char newval)
static

Definition at line 896 of file startup.c.

897{
898 return ParseVariableBool(newval, "QUIET", &pset.quiet);
899}

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 1174 of file startup.c.

1175{
1176 return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
1177}
bool show_all_results
Definition settings.h:185

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 1188 of file startup.c.

1189{
1190 Assert(newval != NULL); /* else substitute hook messed up */
1191 if (pg_strcasecmp(newval, "never") == 0)
1193 else if (pg_strcasecmp(newval, "errors") == 0)
1195 else if (pg_strcasecmp(newval, "always") == 0)
1197 else
1198 {
1199 PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1200 return false;
1201 }
1202
1203 if (pset.db)
1205 return true;
1206}
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
@ PQSHOW_CONTEXT_NEVER
Definition libpq-fe.h:170
@ PQSHOW_CONTEXT_ALWAYS
Definition libpq-fe.h:172
@ PQSHOW_CONTEXT_ERRORS
Definition libpq-fe.h:171
PGContextVisibility show_context
Definition settings.h:186

References Assert, _psqlSettings::db, fb(), 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 1180 of file startup.c.

1181{
1182 if (newval == NULL)
1183 newval = pg_strdup("errors");
1184 return newval;
1185}

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

Referenced by EstablishVariableSpace().

◆ showVersion()

static void showVersion ( void  )
static

Definition at line 848 of file startup.c.

849{
850 puts("psql (PostgreSQL) " PG_VERSION);
851}

References fb().

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 757 of file startup.c.

759{
761
763
764 cell->next = NULL;
765 cell->action = action;
766 if (val)
767 cell->val = pg_strdup(val);
768 else
769 cell->val = NULL;
770
771 if (list->tail)
772 list->tail->next = cell;
773 else
774 list->head = cell;
775 list->tail = cell;
776}
#define pg_malloc_object(type)
Definition fe_memutils.h:50
long val
Definition informix.c:689

References SimpleActionListCell::action, fb(), 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 902 of file startup.c.

903{
904 return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
905}
bool singleline
Definition settings.h:168

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

Referenced by EstablishVariableSpace().

◆ singlestep_hook()

static bool singlestep_hook ( const char newval)
static

Definition at line 908 of file startup.c.

909{
910 return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
911}
bool singlestep
Definition settings.h:169

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

Referenced by EstablishVariableSpace().

◆ verbosity_hook()

static bool verbosity_hook ( const char newval)
static

Definition at line 1151 of file startup.c.

1152{
1153 Assert(newval != NULL); /* else substitute hook messed up */
1154 if (pg_strcasecmp(newval, "default") == 0)
1156 else if (pg_strcasecmp(newval, "verbose") == 0)
1158 else if (pg_strcasecmp(newval, "terse") == 0)
1160 else if (pg_strcasecmp(newval, "sqlstate") == 0)
1162 else
1163 {
1164 PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1165 return false;
1166 }
1167
1168 if (pset.db)
1170 return true;
1171}
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
@ PQERRORS_VERBOSE
Definition libpq-fe.h:164
@ PQERRORS_DEFAULT
Definition libpq-fe.h:163
@ PQERRORS_TERSE
Definition libpq-fe.h:162
@ PQERRORS_SQLSTATE
Definition libpq-fe.h:165
PGVerbosity verbosity
Definition settings.h:184

References Assert, _psqlSettings::db, fb(), 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 1143 of file startup.c.

1144{
1145 if (newval == NULL)
1146 newval = pg_strdup("default");
1147 return newval;
1148}

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

Referenced by EstablishVariableSpace().

◆ watch_interval_hook()

static bool watch_interval_hook ( const char newval)
static

Definition at line 960 of file startup.c.

961{
962 return ParseVariableDouble(newval, "WATCH_INTERVAL", &pset.watch_interval,
964}
#define DEFAULT_WATCH_INTERVAL_MAX
Definition settings.h:35
double watch_interval
Definition settings.h:175
bool ParseVariableDouble(const char *value, const char *name, double *result, double min, double max)
Definition variables.c:195

References DEFAULT_WATCH_INTERVAL_MAX, newval, ParseVariableDouble(), pset, and _psqlSettings::watch_interval.

Referenced by EstablishVariableSpace().

◆ watch_interval_substitute_hook()

static char * watch_interval_substitute_hook ( char newval)
static

Definition at line 952 of file startup.c.

953{
954 if (newval == NULL)
956 return newval;
957}
#define DEFAULT_WATCH_INTERVAL
Definition settings.h:30

References DEFAULT_WATCH_INTERVAL, fb(), newval, and pg_strdup().

Referenced by EstablishVariableSpace().

Variable Documentation

◆ pset

Definition at line 33 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(), discardAbortedPipelineResults(), 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_prepared(), exec_command_conninfo(), exec_command_crosstabview(), exec_command_d(), exec_command_echo(), exec_command_edit(), exec_command_encoding(), exec_command_endpipeline(), exec_command_errverbose(), exec_command_f(), exec_command_flush(), exec_command_flushrequest(), exec_command_g(), exec_command_gdesc(), exec_command_getenv(), exec_command_getresults(), 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_sendpipeline(), exec_command_set(), exec_command_sf_sv(), exec_command_slash_command_help(), exec_command_startpipeline(), exec_command_syncpipeline(), exec_command_t(), exec_command_T(), exec_command_timing(), exec_command_unset(), exec_command_watch(), exec_command_write(), exec_command_x(), exec_command_z(), ExecQueryAndProcessResults(), ExecQueryTuples(), fail_lo_xact(), fetch_count_hook(), finish_lo_xact(), finishInput(), get_conninfo_value(), 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(), pipelineReset(), 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(), SetPipelineVariables(), 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(), verbosity_hook(), and watch_interval_hook().