PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
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 880 of file startup.c.

881{
882 return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
883}
PsqlSettings pset
Definition: startup.c:32
#define newval
bool autocommit
Definition: settings.h:159
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 863 of file startup.c.

864{
865 if (newval == NULL)
866 {
867 /* "\unset FOO" becomes "\set FOO off" */
868 newval = pg_strdup("off");
869 }
870 else if (newval[0] == '\0')
871 {
872 /* "\set FOO" becomes "\set FOO on" */
874 newval = pg_strdup("on");
875 }
876 return newval;
877}
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 1053 of file startup.c.

1054{
1055 Assert(newval != NULL); /* else substitute hook messed up */
1056 if (pg_strcasecmp(newval, "preserve-upper") == 0)
1058 else if (pg_strcasecmp(newval, "preserve-lower") == 0)
1060 else if (pg_strcasecmp(newval, "upper") == 0)
1062 else if (pg_strcasecmp(newval, "lower") == 0)
1064 else
1065 {
1066 PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
1067 "lower, upper, preserve-lower, preserve-upper");
1068 return false;
1069 }
1070 return true;
1071}
Assert(PointerIsAligned(start, uint64))
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:172
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 1045 of file startup.c.

1046{
1047 if (newval == NULL)
1048 newval = pg_strdup("preserve-upper");
1049 return newval;
1050}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ echo_hidden_hook()

static bool echo_hidden_hook ( const char *  newval)
static

Definition at line 1003 of file startup.c.

1004{
1005 Assert(newval != NULL); /* else substitute hook messed up */
1006 if (pg_strcasecmp(newval, "noexec") == 0)
1008 else
1009 {
1010 bool on_off;
1011
1012 if (ParseVariableBool(newval, NULL, &on_off))
1014 else
1015 {
1016 PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
1017 return false;
1018 }
1019 }
1020 return true;
1021}
@ 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:170

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

984{
985 Assert(newval != NULL); /* else substitute hook messed up */
986 if (pg_strcasecmp(newval, "queries") == 0)
988 else if (pg_strcasecmp(newval, "errors") == 0)
990 else if (pg_strcasecmp(newval, "all") == 0)
992 else if (pg_strcasecmp(newval, "none") == 0)
994 else
995 {
996 PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
997 return false;
998 }
999 return true;
1000}
@ 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:169

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

976{
977 if (newval == NULL)
978 newval = pg_strdup("none");
979 return newval;
980}

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

1204{
1206
1207 SetVariableHooks(pset.vars, "AUTOCOMMIT",
1210 SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1213 SetVariableHooks(pset.vars, "QUIET",
1215 quiet_hook);
1216 SetVariableHooks(pset.vars, "SINGLELINE",
1219 SetVariableHooks(pset.vars, "SINGLESTEP",
1222 SetVariableHooks(pset.vars, "FETCH_COUNT",
1225 SetVariableHooks(pset.vars, "HISTFILE",
1226 NULL,
1228 SetVariableHooks(pset.vars, "HISTSIZE",
1231 SetVariableHooks(pset.vars, "IGNOREEOF",
1234 SetVariableHooks(pset.vars, "ECHO",
1236 echo_hook);
1237 SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1240 SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1243 SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1246 SetVariableHooks(pset.vars, "HISTCONTROL",
1249 SetVariableHooks(pset.vars, "PROMPT1",
1250 NULL,
1251 prompt1_hook);
1252 SetVariableHooks(pset.vars, "PROMPT2",
1253 NULL,
1254 prompt2_hook);
1255 SetVariableHooks(pset.vars, "PROMPT3",
1256 NULL,
1257 prompt3_hook);
1258 SetVariableHooks(pset.vars, "VERBOSITY",
1261 SetVariableHooks(pset.vars, "SHOW_ALL_RESULTS",
1264 SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1267 SetVariableHooks(pset.vars, "HIDE_TOAST_COMPRESSION",
1270 SetVariableHooks(pset.vars, "HIDE_TABLEAM",
1273}
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1124
static bool show_context_hook(const char *newval)
Definition: startup.c:1169
static bool hide_tableam_hook(const char *newval)
Definition: startup.c:1197
static bool quiet_hook(const char *newval)
Definition: startup.c:892
static bool singleline_hook(const char *newval)
Definition: startup.c:898
static bool prompt2_hook(const char *newval)
Definition: startup.c:1110
static bool comp_keyword_case_hook(const char *newval)
Definition: startup.c:1053
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1082
static bool prompt1_hook(const char *newval)
Definition: startup.c:1103
static bool verbosity_hook(const char *newval)
Definition: startup.c:1132
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:948
static bool show_all_results_hook(const char *newval)
Definition: startup.c:1155
static bool fetch_count_hook(const char *newval)
Definition: startup.c:918
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:910
static bool histsize_hook(const char *newval)
Definition: startup.c:942
static char * echo_substitute_hook(char *newval)
Definition: startup.c:975
static bool hide_compression_hook(const char *newval)
Definition: startup.c:1190
static char * bool_substitute_hook(char *newval)
Definition: startup.c:863
static bool singlestep_hook(const char *newval)
Definition: startup.c:904
static bool histfile_hook(const char *newval)
Definition: startup.c:924
static bool prompt3_hook(const char *newval)
Definition: startup.c:1117
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:1024
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:934
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:886
static bool autocommit_hook(const char *newval)
Definition: startup.c:880
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1161
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:969
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:1003
static bool echo_hook(const char *newval)
Definition: startup.c:983
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:1074
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:1045
VariableSpace vars
Definition: settings.h:145
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 918 of file startup.c.

919{
920 return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
921}
int fetch_count
Definition: settings.h:166
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 910 of file startup.c.

911{
912 if (newval == NULL)
913 newval = pg_strdup("0");
914 return newval;
915}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ hide_compression_hook()

static bool hide_compression_hook ( const char *  newval)
static

Definition at line 1190 of file startup.c.

1191{
1192 return ParseVariableBool(newval, "HIDE_TOAST_COMPRESSION",
1194}
bool hide_compression
Definition: settings.h:164

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

1198{
1199 return ParseVariableBool(newval, "HIDE_TABLEAM", &pset.hide_tableam);
1200}
bool hide_tableam
Definition: settings.h:165

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

Referenced by EstablishVariableSpace().

◆ histcontrol_hook()

static bool histcontrol_hook ( const char *  newval)
static

Definition at line 1082 of file startup.c.

1083{
1084 Assert(newval != NULL); /* else substitute hook messed up */
1085 if (pg_strcasecmp(newval, "ignorespace") == 0)
1087 else if (pg_strcasecmp(newval, "ignoredups") == 0)
1089 else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1091 else if (pg_strcasecmp(newval, "none") == 0)
1093 else
1094 {
1095 PsqlVarEnumError("HISTCONTROL", newval,
1096 "none, ignorespace, ignoredups, ignoreboth");
1097 return false;
1098 }
1099 return true;
1100}
@ hctl_ignoredups
Definition: settings.h:84
@ hctl_ignoreboth
Definition: settings.h:85
@ hctl_none
Definition: settings.h:82
@ hctl_ignorespace
Definition: settings.h:83
HistControl histcontrol
Definition: settings.h:173

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

1075{
1076 if (newval == NULL)
1077 newval = pg_strdup("none");
1078 return newval;
1079}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ histfile_hook()

static bool histfile_hook ( const char *  newval)
static

Definition at line 924 of file startup.c.

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

Referenced by EstablishVariableSpace().

◆ histsize_hook()

static bool histsize_hook ( const char *  newval)
static

Definition at line 942 of file startup.c.

943{
944 return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
945}

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

Referenced by EstablishVariableSpace().

◆ histsize_substitute_hook()

static char * histsize_substitute_hook ( char *  newval)
static

Definition at line 934 of file startup.c.

935{
936 if (newval == NULL)
937 newval = pg_strdup("500");
938 return newval;
939}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ ignoreeof_hook()

static bool ignoreeof_hook ( const char *  newval)
static

Definition at line 969 of file startup.c.

970{
971 return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
972}

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

Referenced by EstablishVariableSpace().

◆ ignoreeof_substitute_hook()

static char * ignoreeof_substitute_hook ( char *  newval)
static

Definition at line 948 of file startup.c.

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

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:123
uint64 lineno
Definition: settings.h:138
char * inputfile
Definition: settings.h:137

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.

References 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);
143 exit(EXIT_SUCCESS);
144 }
145 if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
146 {
147 showVersion();
148 exit(EXIT_SUCCESS);
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 /* Initialize pipeline variables */
209 SetVariable(pset.vars, "PIPELINE_SYNC_COUNT", "0");
210 SetVariable(pset.vars, "PIPELINE_COMMAND_COUNT", "0");
211 SetVariable(pset.vars, "PIPELINE_RESULT_COUNT", "0");
212
213 parse_psql_options(argc, argv, &options);
214
215 /*
216 * If no action was specified and we're in non-interactive mode, treat it
217 * as if the user had specified "-f -". This lets single-transaction mode
218 * work in this case.
219 */
220 if (options.actions.head == NULL && pset.notty)
222
223 /* Bail out if -1 was specified but will be ignored. */
224 if (options.single_txn && options.actions.head == NULL)
225 pg_fatal("-1 can only be used in non-interactive mode");
226
229 {
232 }
235 {
238 }
239
240 if (pset.getPassword == TRI_YES)
241 {
242 /*
243 * We can't be sure yet of the username that will be used, so don't
244 * offer a potentially wrong one. Typical uses of this option are
245 * noninteractive anyway. (Note: since we've not yet set up our
246 * cancel handler, there's no need to use simple_prompt_extended.)
247 */
248 password = simple_prompt("Password: ", false);
249 }
250
251 /* loop until we have a password if requested by backend */
252 do
253 {
254#define PARAMS_ARRAY_SIZE 8
255 const char **keywords = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
256 const char **values = pg_malloc_array(const char *, PARAMS_ARRAY_SIZE);
257
258 keywords[0] = "host";
259 values[0] = options.host;
260 keywords[1] = "port";
261 values[1] = options.port;
262 keywords[2] = "user";
264 keywords[3] = "password";
265 values[3] = password;
266 keywords[4] = "dbname"; /* see do_connect() */
267 values[4] = (options.list_dbs && options.dbname == NULL) ?
268 "postgres" : options.dbname;
269 keywords[5] = "fallback_application_name";
270 values[5] = pset.progname;
271 keywords[6] = "client_encoding";
272 values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
273 keywords[7] = NULL;
274 values[7] = NULL;
275
276 new_pass = false;
278 free(keywords);
279 free(values);
280
281 if (PQstatus(pset.db) == CONNECTION_BAD &&
283 !password &&
285 {
286 /*
287 * Before closing the old PGconn, extract the user name that was
288 * actually connected with --- it might've come out of a URI or
289 * connstring "database name" rather than options.username.
290 */
291 const char *realusername = PQuser(pset.db);
292 char *password_prompt;
293
294 if (realusername && realusername[0])
295 password_prompt = psprintf(_("Password for user %s: "),
296 realusername);
297 else
298 password_prompt = pg_strdup(_("Password: "));
300
301 password = simple_prompt(password_prompt, false);
302 free(password_prompt);
303 new_pass = true;
304 }
305 } while (new_pass);
306
308 {
311 exit(EXIT_BADCONN);
312 }
313
315
316#ifndef WIN32
317
318 /*
319 * do_watch() needs signal handlers installed (otherwise sigwait() will
320 * filter them out on some platforms), but doesn't need them to do
321 * anything, and they shouldn't ever run (unless perhaps a stray SIGALRM
322 * arrives due to a race when do_watch() cancels an itimer).
323 */
326#endif
327
329
331
332 if (options.list_dbs)
333 {
334 int success;
335
336 if (!options.no_psqlrc)
337 process_psqlrc(argv[0]);
338
339 success = listAllDbs(NULL, false);
342 }
343
344 if (options.logfilename)
345 {
346 pset.logfile = fopen(options.logfilename, "a");
347 if (!pset.logfile)
348 pg_fatal("could not open log file \"%s\": %m",
349 options.logfilename);
350 }
351
352 if (!options.no_psqlrc)
353 process_psqlrc(argv[0]);
354
355 /*
356 * If any actions were given by user, process them in the order in which
357 * they were specified. Note single_txn is only effective in this mode.
358 */
359 if (options.actions.head != NULL)
360 {
361 PGresult *res;
363
364 successResult = EXIT_SUCCESS; /* silence compiler */
365
366 if (options.single_txn)
367 {
368 if ((res = PSQLexec("BEGIN")) == NULL)
369 {
371 {
372 successResult = EXIT_USER;
373 goto error;
374 }
375 }
376 else
377 PQclear(res);
378 }
379
380 for (cell = options.actions.head; cell; cell = cell->next)
381 {
382 if (cell->action == ACT_SINGLE_QUERY)
383 {
385
386 if (pset.echo == PSQL_ECHO_ALL)
387 puts(cell->val);
388
389 successResult = SendQuery(cell->val)
391 }
392 else if (cell->action == ACT_SINGLE_SLASH)
393 {
394 PsqlScanState scan_state;
395 ConditionalStack cond_stack;
396
398
399 if (pset.echo == PSQL_ECHO_ALL)
400 puts(cell->val);
401
403 psql_scan_setup(scan_state,
404 cell->val, strlen(cell->val),
406 cond_stack = conditional_stack_create();
407 psql_scan_set_passthrough(scan_state, cond_stack);
408
409 successResult = HandleSlashCmds(scan_state,
410 cond_stack,
411 NULL,
412 NULL) != PSQL_CMD_ERROR
414
415 psql_scan_destroy(scan_state);
416 conditional_stack_destroy(cond_stack);
417 }
418 else if (cell->action == ACT_FILE)
419 {
420 successResult = process_file(cell->val, false);
421 }
422 else
423 {
424 /* should never come here */
425 Assert(false);
426 }
427
428 if (successResult != EXIT_SUCCESS && pset.on_error_stop)
429 break;
430 }
431
432 if (options.single_txn)
433 {
434 /*
435 * Rollback the contents of the single transaction if the caller
436 * has set ON_ERROR_STOP and one of the steps has failed. This
437 * check needs to match the one done a couple of lines above.
438 */
439 res = PSQLexec((successResult != EXIT_SUCCESS && pset.on_error_stop) ?
440 "ROLLBACK" : "COMMIT");
441 if (res == NULL)
442 {
444 {
445 successResult = EXIT_USER;
446 goto error;
447 }
448 }
449 else
450 PQclear(res);
451 }
452
453error:
454 ;
455 }
456
457 /*
458 * or otherwise enter interactive main loop
459 */
460 else
461 {
464 if (!pset.quiet)
465 printf(_("Type \"help\" for help.\n\n"));
466 initializeInput(options.no_readline ? 0 : 1);
467 successResult = MainLoop(stdin);
468 }
469
470 /* clean up */
471 if (pset.logfile)
472 fclose(pset.logfile);
473 if (pset.db)
475 if (pset.dead_conn)
477 setQFout(NULL);
478
479 return successResult;
480}
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:2448
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:115
static void process_psqlrc(char *argv0)
Definition: startup.c:779
static void EstablishVariableSpace(void)
Definition: startup.c:1203
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:753
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:488
static void showVersion(void)
Definition: startup.c:844
static Datum values[MAXATTR]
Definition: bootstrap.c:151
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1185
#define CppAsString2(x)
Definition: c.h:363
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:224
void SyncVariables(void)
Definition: command.c:4428
void connection_warnings(bool in_startup)
Definition: command.c:4300
@ 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:945
#define _(x)
Definition: elog.c:90
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:7606
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:7490
void PQfinish(PGconn *conn)
Definition: fe-connect.c:5224
char * PQuser(const PGconn *conn)
Definition: fe-connect.c:7397
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:7734
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:7553
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:737
void PQclear(PGresult *res)
Definition: fe-exec.c:721
int PQenv2encoding(void)
Definition: fe-misc.c:1262
#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:84
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:6114
#define pqsignal
Definition: port.h:521
const char * get_progname(const char *argv0)
Definition: path.c:652
#define printf(...)
Definition: port.h:245
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:186
#define EXIT_FAILURE
Definition: settings.h:190
#define EXIT_BADCONN
Definition: settings.h:193
#define DEFAULT_PROMPT2
Definition: settings.h:27
#define EXIT_USER
Definition: settings.h:195
#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:51
struct SimpleActionListCell * next
Definition: startup.c:55
enum _actions action
Definition: startup.c:56
printQueryOpt popt
Definition: settings.h:106
int encoding
Definition: settings.h:98
FILE * logfile
Definition: settings.h:143
bool on_error_stop
Definition: settings.h:160
PGconn * dead_conn
Definition: settings.h:152
PGresult * last_error_result
Definition: settings.h:104
FILE * copyStream
Definition: settings.h:102
PGconn * db
Definition: settings.h:97
bool queryFoutPipe
Definition: settings.h:100
enum trivalue getPassword
Definition: settings.h:131
bool cur_cmd_interactive
Definition: settings.h:134
FILE * cur_cmd_source
Definition: settings.h:132
const char * progname
Definition: settings.h:136
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_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(), 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 1024 of file startup.c.

1025{
1026 Assert(newval != NULL); /* else substitute hook messed up */
1027 if (pg_strcasecmp(newval, "interactive") == 0)
1029 else
1030 {
1031 bool on_off;
1032
1033 if (ParseVariableBool(newval, NULL, &on_off))
1035 else
1036 {
1037 PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
1038 return false;
1039 }
1040 }
1041 return true;
1042}
@ 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:171

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

887{
888 return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
889}

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

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

780{
781 char home[MAXPGPATH];
782 char rc_file[MAXPGPATH];
784 char etc_path[MAXPGPATH];
785 char *envrc = getenv("PSQLRC");
786
788 pg_fatal("could not find own program executable");
789
790 get_etc_path(my_exec_path, etc_path);
791
792 snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
793 process_psqlrc_file(rc_file);
794
795 if (envrc != NULL && strlen(envrc) > 0)
796 {
797 /* might need to free() this */
798 char *envrc_alloc = pstrdup(envrc);
799
800 expand_tilde(&envrc_alloc);
801 process_psqlrc_file(envrc_alloc);
802 }
803 else if (get_home_path(home))
804 {
805 snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
806 process_psqlrc_file(rc_file);
807 }
808}
void expand_tilde(char **filename)
Definition: common.c:2489
#define SYSPSQLRC
Definition: startup.c:35
#define PSQLRC
Definition: startup.c:36
static void process_psqlrc_file(char *filename)
Definition: startup.c:813
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:1699
#define MAXPGPATH
static char * argv0
Definition: pg_ctl.c:93
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:239

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

814{
815 char *psqlrc_minor,
816 *psqlrc_major;
817
818#if defined(WIN32) && (!defined(__MINGW32__))
819#define R_OK 4
820#endif
821
822 psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
823 psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
824
825 /* check for minor version first, then major, then no version */
826 if (access(psqlrc_minor, R_OK) == 0)
827 (void) process_file(psqlrc_minor, false);
828 else if (access(psqlrc_major, R_OK) == 0)
829 (void) process_file(psqlrc_major, false);
830 else if (access(filename, R_OK) == 0)
831 (void) process_file(filename, false);
832
833 free(psqlrc_minor);
834 free(psqlrc_major);
835}
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 1103 of file startup.c.

1104{
1105 pset.prompt1 = newval ? newval : "";
1106 return true;
1107}
const char * prompt1
Definition: settings.h:174

References newval, _psqlSettings::prompt1, and pset.

Referenced by EstablishVariableSpace().

◆ prompt2_hook()

static bool prompt2_hook ( const char *  newval)
static

Definition at line 1110 of file startup.c.

1111{
1112 pset.prompt2 = newval ? newval : "";
1113 return true;
1114}
const char * prompt2
Definition: settings.h:175

References newval, _psqlSettings::prompt2, and pset.

Referenced by EstablishVariableSpace().

◆ prompt3_hook()

static bool prompt3_hook ( const char *  newval)
static

Definition at line 1117 of file startup.c.

1118{
1119 pset.prompt3 = newval ? newval : "";
1120 return true;
1121}
const char * prompt3
Definition: settings.h:176

References newval, _psqlSettings::prompt3, and pset.

Referenced by EstablishVariableSpace().

◆ quiet_hook()

static bool quiet_hook ( const char *  newval)
static

Definition at line 892 of file startup.c.

893{
894 return ParseVariableBool(newval, "QUIET", &pset.quiet);
895}

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

1156{
1157 return ParseVariableBool(newval, "SHOW_ALL_RESULTS", &pset.show_all_results);
1158}
bool show_all_results
Definition: settings.h:178

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

1170{
1171 Assert(newval != NULL); /* else substitute hook messed up */
1172 if (pg_strcasecmp(newval, "never") == 0)
1174 else if (pg_strcasecmp(newval, "errors") == 0)
1176 else if (pg_strcasecmp(newval, "always") == 0)
1178 else
1179 {
1180 PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1181 return false;
1182 }
1183
1184 if (pset.db)
1186 return true;
1187}
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:7705
@ PQSHOW_CONTEXT_NEVER
Definition: libpq-fe.h:163
@ PQSHOW_CONTEXT_ALWAYS
Definition: libpq-fe.h:165
@ PQSHOW_CONTEXT_ERRORS
Definition: libpq-fe.h:164
PGContextVisibility show_context
Definition: settings.h:179

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

1162{
1163 if (newval == NULL)
1164 newval = pg_strdup("errors");
1165 return newval;
1166}

References newval, and pg_strdup().

Referenced by EstablishVariableSpace().

◆ showVersion()

static void showVersion ( void  )
static

Definition at line 844 of file startup.c.

845{
846 puts("psql (PostgreSQL) " PG_VERSION);
847}

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

755{
757
759
760 cell->next = NULL;
761 cell->action = action;
762 if (val)
763 cell->val = pg_strdup(val);
764 else
765 cell->val = NULL;
766
767 if (list->tail)
768 list->tail->next = cell;
769 else
770 list->head = cell;
771 list->tail = cell;
772}
#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 898 of file startup.c.

899{
900 return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
901}
bool singleline
Definition: settings.h:162

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

Referenced by EstablishVariableSpace().

◆ singlestep_hook()

static bool singlestep_hook ( const char *  newval)
static

Definition at line 904 of file startup.c.

905{
906 return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
907}
bool singlestep
Definition: settings.h:163

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

Referenced by EstablishVariableSpace().

◆ verbosity_hook()

static bool verbosity_hook ( const char *  newval)
static

Definition at line 1132 of file startup.c.

1133{
1134 Assert(newval != NULL); /* else substitute hook messed up */
1135 if (pg_strcasecmp(newval, "default") == 0)
1137 else if (pg_strcasecmp(newval, "verbose") == 0)
1139 else if (pg_strcasecmp(newval, "terse") == 0)
1141 else if (pg_strcasecmp(newval, "sqlstate") == 0)
1143 else
1144 {
1145 PsqlVarEnumError("VERBOSITY", newval, "default, verbose, terse, sqlstate");
1146 return false;
1147 }
1148
1149 if (pset.db)
1151 return true;
1152}
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:7693
@ PQERRORS_VERBOSE
Definition: libpq-fe.h:157
@ PQERRORS_DEFAULT
Definition: libpq-fe.h:156
@ PQERRORS_TERSE
Definition: libpq-fe.h:155
@ PQERRORS_SQLSTATE
Definition: libpq-fe.h:158
PGVerbosity verbosity
Definition: settings.h:177

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

1125{
1126 if (newval == NULL)
1127 newval = pg_strdup("default");
1128 return newval;
1129}

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(), 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(), 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_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_write(), 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(), 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(), and verbosity_hook().