PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
startup.c File Reference
#include "postgres_fe.h"
#include <sys/types.h>
#include <unistd.h>
#include "getopt_long.h"
#include <locale.h>
#include "command.h"
#include "common.h"
#include "describe.h"
#include "help.h"
#include "input.h"
#include "mainloop.h"
#include "fe_utils/print.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)
 
int main (int argc, char *argv[])
 
static char * bool_substitute_hook (char *newval)
 
static bool autocommit_hook (const char *newval)
 
static bool on_error_stop_hook (const char *newval)
 
static bool quiet_hook (const char *newval)
 
static bool singleline_hook (const char *newval)
 
static bool singlestep_hook (const char *newval)
 
static char * fetch_count_substitute_hook (char *newval)
 
static bool fetch_count_hook (const char *newval)
 
static bool histfile_hook (const char *newval)
 
static char * histsize_substitute_hook (char *newval)
 
static bool histsize_hook (const char *newval)
 
static char * ignoreeof_substitute_hook (char *newval)
 
static bool ignoreeof_hook (const char *newval)
 
static char * echo_substitute_hook (char *newval)
 
static bool echo_hook (const char *newval)
 
static bool echo_hidden_hook (const char *newval)
 
static bool on_error_rollback_hook (const char *newval)
 
static char * comp_keyword_case_substitute_hook (char *newval)
 
static bool comp_keyword_case_hook (const char *newval)
 
static char * histcontrol_substitute_hook (char *newval)
 
static bool histcontrol_hook (const char *newval)
 
static bool prompt1_hook (const char *newval)
 
static bool prompt2_hook (const char *newval)
 
static bool prompt3_hook (const char *newval)
 
static char * verbosity_substitute_hook (char *newval)
 
static bool verbosity_hook (const char *newval)
 
static char * show_context_substitute_hook (char *newval)
 
static bool show_context_hook (const char *newval)
 

Variables

PsqlSettings pset
 

Macro Definition Documentation

#define NOPAGER   0

Definition at line 94 of file startup.c.

Referenced by main(), and parse_psql_options().

#define PARAMS_ARRAY_SIZE   8

Referenced by main().

#define PSQLRC   ".psqlrc"

Definition at line 41 of file startup.c.

Referenced by process_psqlrc().

#define SYSPSQLRC   "psqlrc"

Definition at line 40 of file startup.c.

Referenced by process_psqlrc().

Typedef Documentation

Enumeration Type Documentation

enum _actions
Enumerator
ACT_SINGLE_QUERY 
ACT_SINGLE_SLASH 
ACT_FILE 

Definition at line 51 of file startup.c.

Function Documentation

static bool autocommit_hook ( const char *  newval)
static

Definition at line 802 of file startup.c.

References _psqlSettings::autocommit, and ParseVariableBool().

Referenced by EstablishVariableSpace().

803 {
804  return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
805 }
bool autocommit
Definition: settings.h:122
PsqlSettings pset
Definition: startup.c:37
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static char* bool_substitute_hook ( char *  newval)
static

Definition at line 785 of file startup.c.

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

Referenced by EstablishVariableSpace().

786 {
787  if (newval == NULL)
788  {
789  /* "\unset FOO" becomes "\set FOO off" */
790  newval = pg_strdup("off");
791  }
792  else if (newval[0] == '\0')
793  {
794  /* "\set FOO" becomes "\set FOO on" */
795  pg_free(newval);
796  newval = pg_strdup("on");
797  }
798  return newval;
799 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
void pg_free(void *ptr)
Definition: fe_memutils.c:105
static bool comp_keyword_case_hook ( const char *  newval)
static

Definition at line 975 of file startup.c.

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

Referenced by EstablishVariableSpace().

976 {
977  Assert(newval != NULL); /* else substitute hook messed up */
978  if (pg_strcasecmp(newval, "preserve-upper") == 0)
980  else if (pg_strcasecmp(newval, "preserve-lower") == 0)
982  else if (pg_strcasecmp(newval, "upper") == 0)
984  else if (pg_strcasecmp(newval, "lower") == 0)
986  else
987  {
988  PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
989  "lower, upper, preserve-lower, preserve-upper");
990  return false;
991  }
992  return true;
993 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
PSQL_COMP_CASE comp_case
Definition: settings.h:133
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:37
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
static char* comp_keyword_case_substitute_hook ( char *  newval)
static

Definition at line 967 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

968 {
969  if (newval == NULL)
970  newval = pg_strdup("preserve-upper");
971  return newval;
972 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
static bool echo_hidden_hook ( const char *  newval)
static

Definition at line 925 of file startup.c.

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

Referenced by EstablishVariableSpace().

926 {
927  Assert(newval != NULL); /* else substitute hook messed up */
928  if (pg_strcasecmp(newval, "noexec") == 0)
930  else
931  {
932  bool on_off;
933 
934  if (ParseVariableBool(newval, NULL, &on_off))
936  else
937  {
938  PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
939  return false;
940  }
941  }
942  return true;
943 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PSQL_ECHO_HIDDEN echo_hidden
Definition: settings.h:131
PsqlSettings pset
Definition: startup.c:37
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static bool echo_hook ( const char *  newval)
static

Definition at line 905 of file startup.c.

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

Referenced by EstablishVariableSpace().

906 {
907  Assert(newval != NULL); /* else substitute hook messed up */
908  if (pg_strcasecmp(newval, "queries") == 0)
910  else if (pg_strcasecmp(newval, "errors") == 0)
912  else if (pg_strcasecmp(newval, "all") == 0)
914  else if (pg_strcasecmp(newval, "none") == 0)
916  else
917  {
918  PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
919  return false;
920  }
921  return true;
922 }
PSQL_ECHO echo
Definition: settings.h:130
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:37
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
static char* echo_substitute_hook ( char *  newval)
static

Definition at line 897 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

898 {
899  if (newval == NULL)
900  newval = pg_strdup("none");
901  return newval;
902 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
static void EstablishVariableSpace ( void  )
static

Definition at line 1105 of file startup.c.

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

Referenced by main().

1106 {
1108 
1109  SetVariableHooks(pset.vars, "AUTOCOMMIT",
1111  autocommit_hook);
1112  SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1115  SetVariableHooks(pset.vars, "QUIET",
1117  quiet_hook);
1118  SetVariableHooks(pset.vars, "SINGLELINE",
1120  singleline_hook);
1121  SetVariableHooks(pset.vars, "SINGLESTEP",
1123  singlestep_hook);
1124  SetVariableHooks(pset.vars, "FETCH_COUNT",
1127  SetVariableHooks(pset.vars, "HISTFILE",
1128  NULL,
1129  histfile_hook);
1130  SetVariableHooks(pset.vars, "HISTSIZE",
1132  histsize_hook);
1133  SetVariableHooks(pset.vars, "IGNOREEOF",
1135  ignoreeof_hook);
1136  SetVariableHooks(pset.vars, "ECHO",
1138  echo_hook);
1139  SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1142  SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1145  SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1148  SetVariableHooks(pset.vars, "HISTCONTROL",
1151  SetVariableHooks(pset.vars, "PROMPT1",
1152  NULL,
1153  prompt1_hook);
1154  SetVariableHooks(pset.vars, "PROMPT2",
1155  NULL,
1156  prompt2_hook);
1157  SetVariableHooks(pset.vars, "PROMPT3",
1158  NULL,
1159  prompt3_hook);
1160  SetVariableHooks(pset.vars, "VERBOSITY",
1162  verbosity_hook);
1163  SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1166 }
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1046
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:808
static bool echo_hook(const char *newval)
Definition: startup.c:905
static bool quiet_hook(const char *newval)
Definition: startup.c:814
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1004
static char * bool_substitute_hook(char *newval)
Definition: startup.c:785
VariableSpace CreateVariableSpace(void)
Definition: variables.c:51
static bool prompt2_hook(const char *newval)
Definition: startup.c:1032
static bool singleline_hook(const char *newval)
Definition: startup.c:820
static char * echo_substitute_hook(char *newval)
Definition: startup.c:897
static bool autocommit_hook(const char *newval)
Definition: startup.c:802
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:967
static bool fetch_count_hook(const char *newval)
Definition: startup.c:840
static bool singlestep_hook(const char *newval)
Definition: startup.c:826
static bool show_context_hook(const char *newval)
Definition: startup.c:1083
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:946
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1075
static bool histsize_hook(const char *newval)
Definition: startup.c:864
PsqlSettings pset
Definition: startup.c:37
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:856
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition: variables.c:315
#define NULL
Definition: c.h:226
static bool comp_keyword_case_hook(const char *newval)
Definition: startup.c:975
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:996
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:891
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:870
static bool prompt1_hook(const char *newval)
Definition: startup.c:1025
static bool histfile_hook(const char *newval)
Definition: startup.c:846
static bool verbosity_hook(const char *newval)
Definition: startup.c:1054
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:832
static bool prompt3_hook(const char *newval)
Definition: startup.c:1039
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:925
VariableSpace vars
Definition: settings.h:115
static bool fetch_count_hook ( const char *  newval)
static

Definition at line 840 of file startup.c.

References _psqlSettings::fetch_count, and ParseVariableNum().

Referenced by EstablishVariableSpace().

841 {
842  return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
843 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
int fetch_count
Definition: settings.h:127
PsqlSettings pset
Definition: startup.c:37
#define newval
static char* fetch_count_substitute_hook ( char *  newval)
static

Definition at line 832 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

833 {
834  if (newval == NULL)
835  newval = pg_strdup("0");
836  return newval;
837 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
static bool histcontrol_hook ( const char *  newval)
static

Definition at line 1004 of file startup.c.

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

Referenced by EstablishVariableSpace().

1005 {
1006  Assert(newval != NULL); /* else substitute hook messed up */
1007  if (pg_strcasecmp(newval, "ignorespace") == 0)
1009  else if (pg_strcasecmp(newval, "ignoredups") == 0)
1011  else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1013  else if (pg_strcasecmp(newval, "none") == 0)
1015  else
1016  {
1017  PsqlVarEnumError("HISTCONTROL", newval,
1018  "none, ignorespace, ignoredups, ignoreboth");
1019  return false;
1020  }
1021  return true;
1022 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:37
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
HistControl histcontrol
Definition: settings.h:134
static char* histcontrol_substitute_hook ( char *  newval)
static

Definition at line 996 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

997 {
998  if (newval == NULL)
999  newval = pg_strdup("none");
1000  return newval;
1001 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
static bool histfile_hook ( const char *  newval)
static

Definition at line 846 of file startup.c.

Referenced by EstablishVariableSpace().

847 {
848  /*
849  * Someday we might try to validate the filename, but for now, this is
850  * just a placeholder to ensure HISTFILE is known to tab completion.
851  */
852  return true;
853 }
static bool histsize_hook ( const char *  newval)
static

Definition at line 864 of file startup.c.

References _psqlSettings::histsize, and ParseVariableNum().

Referenced by EstablishVariableSpace().

865 {
866  return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
867 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
PsqlSettings pset
Definition: startup.c:37
#define newval
static char* histsize_substitute_hook ( char *  newval)
static

Definition at line 856 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

857 {
858  if (newval == NULL)
859  newval = pg_strdup("500");
860  return newval;
861 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
static bool ignoreeof_hook ( const char *  newval)
static

Definition at line 891 of file startup.c.

References _psqlSettings::ignoreeof, and ParseVariableNum().

Referenced by EstablishVariableSpace().

892 {
893  return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
894 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
PsqlSettings pset
Definition: startup.c:37
#define newval
static char* ignoreeof_substitute_hook ( char *  newval)
static

Definition at line 870 of file startup.c.

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

Referenced by EstablishVariableSpace().

871 {
872  int dummy;
873 
874  /*
875  * This tries to mimic the behavior of bash, to wit "If set, the value is
876  * the number of consecutive EOF characters which must be typed as the
877  * first characters on an input line before bash exits. If the variable
878  * exists but does not have a numeric value, or has no value, the default
879  * value is 10. If it does not exist, EOF signifies the end of input to
880  * the shell." Unlike bash, however, we insist on the stored value
881  * actually being a valid integer.
882  */
883  if (newval == NULL)
884  newval = pg_strdup("0");
885  else if (!ParseVariableNum(newval, NULL, &dummy))
886  newval = pg_strdup("10");
887  return newval;
888 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
int main ( int  argc,
char *  argv[] 
)

Definition at line 102 of file startup.c.

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

103 {
104  struct adhoc_opts options;
105  int successResult;
106  bool have_password = false;
107  char password[100];
108  char *password_prompt = NULL;
109  bool new_pass;
110 
111  set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("psql"));
112 
113  if (argc > 1)
114  {
115  if ((strcmp(argv[1], "-?") == 0) || (argc == 2 && (strcmp(argv[1], "--help") == 0)))
116  {
117  usage(NOPAGER);
118  exit(EXIT_SUCCESS);
119  }
120  if (strcmp(argv[1], "--version") == 0 || strcmp(argv[1], "-V") == 0)
121  {
122  showVersion();
123  exit(EXIT_SUCCESS);
124  }
125  }
126 
127 #ifdef WIN32
128  setvbuf(stderr, NULL, _IONBF, 0);
129 #endif
130 
131  pset.progname = get_progname(argv[0]);
132 
133  pset.db = NULL;
136  pset.queryFout = stdout;
137  pset.queryFoutPipe = false;
138  pset.copyStream = NULL;
140  pset.cur_cmd_source = stdin;
141  pset.cur_cmd_interactive = false;
142 
143  /* We rely on unmentioned fields of pset.popt to start out 0/false/NULL */
145  pset.popt.topt.border = 1;
146  pset.popt.topt.pager = 1;
148  pset.popt.topt.start_table = true;
149  pset.popt.topt.stop_table = true;
150  pset.popt.topt.default_footer = true;
151 
155 
157 
158  /* We must get COLUMNS here before readline() sets it */
159  pset.popt.topt.env_columns = getenv("COLUMNS") ? atoi(getenv("COLUMNS")) : 0;
160 
161  pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
162 
164 
166 
167  SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
168 
169  /* Default values for variables (that don't match the result of \unset) */
170  SetVariableBool(pset.vars, "AUTOCOMMIT");
171  SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
172  SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
173  SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
174 
175  parse_psql_options(argc, argv, &options);
176 
177  /*
178  * If no action was specified and we're in non-interactive mode, treat it
179  * as if the user had specified "-f -". This lets single-transaction mode
180  * work in this case.
181  */
182  if (options.actions.head == NULL && pset.notty)
184 
185  /* Bail out if -1 was specified but will be ignored. */
186  if (options.single_txn && options.actions.head == NULL)
187  {
188  fprintf(stderr, _("%s: -1 can only be used in non-interactive mode\n"), pset.progname);
189  exit(EXIT_FAILURE);
190  }
191 
194  {
197  }
200  {
203  }
204 
205  if (options.username == NULL)
206  password_prompt = pg_strdup(_("Password: "));
207  else
208  password_prompt = psprintf(_("Password for user %s: "),
209  options.username);
210 
211  if (pset.getPassword == TRI_YES)
212  {
213  simple_prompt(password_prompt, password, sizeof(password), false);
214  have_password = true;
215  }
216 
217  /* loop until we have a password if requested by backend */
218  do
219  {
220 #define PARAMS_ARRAY_SIZE 8
221  const char **keywords = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*keywords));
222  const char **values = pg_malloc(PARAMS_ARRAY_SIZE * sizeof(*values));
223 
224  keywords[0] = "host";
225  values[0] = options.host;
226  keywords[1] = "port";
227  values[1] = options.port;
228  keywords[2] = "user";
229  values[2] = options.username;
230  keywords[3] = "password";
231  values[3] = have_password ? password : NULL;
232  keywords[4] = "dbname"; /* see do_connect() */
233  values[4] = (options.list_dbs && options.dbname == NULL) ?
234  "postgres" : options.dbname;
235  keywords[5] = "fallback_application_name";
236  values[5] = pset.progname;
237  keywords[6] = "client_encoding";
238  values[6] = (pset.notty || getenv("PGCLIENTENCODING")) ? NULL : "auto";
239  keywords[7] = NULL;
240  values[7] = NULL;
241 
242  new_pass = false;
243  pset.db = PQconnectdbParams(keywords, values, true);
244  free(keywords);
245  free(values);
246 
247  if (PQstatus(pset.db) == CONNECTION_BAD &&
249  !have_password &&
251  {
252  PQfinish(pset.db);
253  simple_prompt(password_prompt, password, sizeof(password), false);
254  have_password = true;
255  new_pass = true;
256  }
257  } while (new_pass);
258 
259  free(password_prompt);
260 
261  if (PQstatus(pset.db) == CONNECTION_BAD)
262  {
263  fprintf(stderr, "%s: %s", pset.progname, PQerrorMessage(pset.db));
264  PQfinish(pset.db);
265  exit(EXIT_BADCONN);
266  }
267 
269 
271 
272  SyncVariables();
273 
274  if (options.list_dbs)
275  {
276  int success;
277 
278  if (!options.no_psqlrc)
279  process_psqlrc(argv[0]);
280 
281  success = listAllDbs(NULL, false);
282  PQfinish(pset.db);
283  exit(success ? EXIT_SUCCESS : EXIT_FAILURE);
284  }
285 
286  if (options.logfilename)
287  {
288  pset.logfile = fopen(options.logfilename, "a");
289  if (!pset.logfile)
290  {
291  fprintf(stderr, _("%s: could not open log file \"%s\": %s\n"),
292  pset.progname, options.logfilename, strerror(errno));
293  exit(EXIT_FAILURE);
294  }
295  }
296 
297  if (!options.no_psqlrc)
298  process_psqlrc(argv[0]);
299 
300  /*
301  * If any actions were given by user, process them in the order in which
302  * they were specified. Note single_txn is only effective in this mode.
303  */
304  if (options.actions.head != NULL)
305  {
306  PGresult *res;
307  SimpleActionListCell *cell;
308 
309  successResult = EXIT_SUCCESS; /* silence compiler */
310 
311  if (options.single_txn)
312  {
313  if ((res = PSQLexec("BEGIN")) == NULL)
314  {
315  if (pset.on_error_stop)
316  {
317  successResult = EXIT_USER;
318  goto error;
319  }
320  }
321  else
322  PQclear(res);
323  }
324 
325  for (cell = options.actions.head; cell; cell = cell->next)
326  {
327  if (cell->action == ACT_SINGLE_QUERY)
328  {
329  if (pset.echo == PSQL_ECHO_ALL)
330  puts(cell->val);
331 
332  successResult = SendQuery(cell->val)
334  }
335  else if (cell->action == ACT_SINGLE_SLASH)
336  {
337  PsqlScanState scan_state;
338 
339  if (pset.echo == PSQL_ECHO_ALL)
340  puts(cell->val);
341 
342  scan_state = psql_scan_create(&psqlscan_callbacks);
343  psql_scan_setup(scan_state,
344  cell->val, strlen(cell->val),
346 
347  successResult = HandleSlashCmds(scan_state, NULL) != PSQL_CMD_ERROR
349 
350  psql_scan_destroy(scan_state);
351  }
352  else if (cell->action == ACT_FILE)
353  {
354  successResult = process_file(cell->val, false);
355  }
356  else
357  {
358  /* should never come here */
359  Assert(false);
360  }
361 
362  if (successResult != EXIT_SUCCESS && pset.on_error_stop)
363  break;
364  }
365 
366  if (options.single_txn)
367  {
368  if ((res = PSQLexec("COMMIT")) == NULL)
369  {
370  if (pset.on_error_stop)
371  {
372  successResult = EXIT_USER;
373  goto error;
374  }
375  }
376  else
377  PQclear(res);
378  }
379 
380 error:
381  ;
382  }
383 
384  /*
385  * or otherwise enter interactive main loop
386  */
387  else
388  {
389  connection_warnings(true);
390  if (!pset.quiet)
391  printf(_("Type \"help\" for help.\n\n"));
392  initializeInput(options.no_readline ? 0 : 1);
393  successResult = MainLoop(stdin);
394  }
395 
396  /* clean up */
397  if (pset.logfile)
398  fclose(pset.logfile);
399  PQfinish(pset.db);
400  setQFout(NULL);
401 
402  return successResult;
403 }
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:411
int env_columns
Definition: print.h:119
#define NOPAGER
Definition: startup.c:94
PSQL_ECHO echo
Definition: settings.h:130
static void process_psqlrc(char *argv0)
Definition: startup.c:698
static void usage(void)
Definition: pg_standby.c:503
static char password[100]
Definition: streamutil.c:44
PGconn * db
Definition: settings.h:82
char * PQerrorMessage(const PGconn *conn)
Definition: fe-connect.c:5960
#define EXIT_SUCCESS
Definition: settings.h:146
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6136
void SyncVariables(void)
Definition: command.c:2153
static void error(void)
Definition: sql-dyntest.c:147
static void EstablishVariableSpace(void)
Definition: startup.c:1105
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
enum _actions action
Definition: startup.c:61
const char * get_progname(const char *argv0)
Definition: path.c:453
PsqlScanState psql_scan_create(const PsqlScanCallbacks *callbacks)
void initializeInput(int flags)
Definition: input.c:346
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
bool start_table
Definition: print.h:108
enum printFormat format
Definition: print.h:98
printTableOpt topt
Definition: print.h:165
#define DEFAULT_PROMPT3
Definition: settings.h:28
bool queryFoutPipe
Definition: settings.h:85
FILE * queryFout
Definition: settings.h:84
unicode_linestyle unicode_header_linestyle
Definition: print.h:123
void PQfinish(PGconn *conn)
Definition: fe-connect.c:3517
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:367
static void setup_cancel_handler(void)
Definition: parallel.c:622
bool on_error_stop
Definition: settings.h:123
#define DEFAULT_PROMPT2
Definition: settings.h:27
char * username
Definition: oid2name.c:40
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition: startup.c:672
bool separator_zero
Definition: print.h:93
FILE * copyStream
Definition: settings.h:87
unicode_linestyle unicode_border_linestyle
Definition: print.h:121
static void showVersion(void)
Definition: startup.c:766
static void process_file(const char *filename, int weight)
Definition: pgbench.c:3324
FILE * cur_cmd_source
Definition: settings.h:102
PGconn * PQconnectdbParams(const char *const *keywords, const char *const *values, int expand_dbname)
Definition: fe-connect.c:471
#define EXIT_BADCONN
Definition: settings.h:153
unsigned short int border
Definition: print.h:101
bool cur_cmd_interactive
Definition: settings.h:104
unicode_linestyle unicode_column_linestyle
Definition: print.h:122
static bool success
Definition: pg_basebackup.c:99
const PsqlScanCallbacks psqlscan_callbacks
Definition: mainloop.c:21
void simple_prompt(const char *prompt, char *destination, size_t destlen, bool echo)
Definition: sprompt.c:37
#define DEFAULT_RECORD_SEP
Definition: settings.h:16
#define EXIT_USER
Definition: settings.h:155
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct SimpleActionListCell * next
Definition: startup.c:60
int PQenv2encoding(void)
Definition: fe-misc.c:1206
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
bool setQFout(const char *fname)
Definition: common.c:84
PsqlSettings pset
Definition: startup.c:37
void psql_scan_destroy(PsqlScanState state)
PGresult * last_error_result
Definition: settings.h:89
FILE * logfile
Definition: settings.h:113
unsigned short int pager
Definition: print.h:103
char * dbname
Definition: oid2name.c:37
void PQclear(PGresult *res)
Definition: fe-exec.c:650
bool stop_table
Definition: print.h:109
bool standard_strings(void)
Definition: common.c:2008
#define free(a)
Definition: header.h:60
struct separator fieldSep
Definition: print.h:113
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:198
static bool have_password
Definition: streamutil.c:43
int pager_min_lines
Definition: print.h:105
bool default_footer
Definition: print.h:110
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
#define PARAMS_ARRAY_SIZE
const char * progname
Definition: settings.h:106
int MainLoop(FILE *source)
Definition: mainloop.c:35
printQueryOpt popt
Definition: settings.h:91
enum trivalue getPassword
Definition: settings.h:101
void setDecimalLocale(void)
Definition: print.c:3377
#define DEFAULT_PROMPT1
Definition: settings.h:26
backslashResult HandleSlashCmds(PsqlScanState scan_state, PQExpBuffer query_buf)
Definition: command.c:111
static Datum values[MAXATTR]
Definition: bootstrap.c:162
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:5994
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
char * port
Definition: oid2name.c:39
const char * strerror(int errnum)
Definition: strerror.c:19
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3427
#define EXIT_FAILURE
Definition: settings.h:150
void psql_scan_setup(PsqlScanState state, const char *line, int line_len, int encoding, bool std_strings)
char * separator
Definition: print.h:92
bool SendQuery(const char *query)
Definition: common.c:1183
void connection_warnings(bool in_startup)
Definition: command.c:2046
bool notty
Definition: settings.h:99
struct separator recordSep
Definition: print.h:114
int encoding
Definition: settings.h:83
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5907
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
#define _(x)
Definition: elog.c:84
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:777
PGresult * PSQLexec(const char *query)
Definition: common.c:599
VariableSpace vars
Definition: settings.h:115
static bool on_error_rollback_hook ( const char *  newval)
static

Definition at line 946 of file startup.c.

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

Referenced by EstablishVariableSpace().

947 {
948  Assert(newval != NULL); /* else substitute hook messed up */
949  if (pg_strcasecmp(newval, "interactive") == 0)
951  else
952  {
953  bool on_off;
954 
955  if (ParseVariableBool(newval, NULL, &on_off))
957  else
958  {
959  PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
960  return false;
961  }
962  }
963  return true;
964 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PSQL_ERROR_ROLLBACK on_error_rollback
Definition: settings.h:132
PsqlSettings pset
Definition: startup.c:37
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static bool on_error_stop_hook ( const char *  newval)
static

Definition at line 808 of file startup.c.

References _psqlSettings::on_error_stop, and ParseVariableBool().

Referenced by EstablishVariableSpace().

809 {
810  return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
811 }
bool on_error_stop
Definition: settings.h:123
PsqlSettings pset
Definition: startup.c:37
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static void parse_psql_options ( int  argc,
char *  argv[],
struct adhoc_opts options 
)
static

Definition at line 411 of file startup.c.

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

Referenced by main().

412 {
413  static struct option long_options[] =
414  {
415  {"echo-all", no_argument, NULL, 'a'},
416  {"no-align", no_argument, NULL, 'A'},
417  {"command", required_argument, NULL, 'c'},
418  {"dbname", required_argument, NULL, 'd'},
419  {"echo-queries", no_argument, NULL, 'e'},
420  {"echo-errors", no_argument, NULL, 'b'},
421  {"echo-hidden", no_argument, NULL, 'E'},
422  {"file", required_argument, NULL, 'f'},
423  {"field-separator", required_argument, NULL, 'F'},
424  {"field-separator-zero", no_argument, NULL, 'z'},
425  {"host", required_argument, NULL, 'h'},
426  {"html", no_argument, NULL, 'H'},
427  {"list", no_argument, NULL, 'l'},
428  {"log-file", required_argument, NULL, 'L'},
429  {"no-readline", no_argument, NULL, 'n'},
430  {"single-transaction", no_argument, NULL, '1'},
431  {"output", required_argument, NULL, 'o'},
432  {"port", required_argument, NULL, 'p'},
433  {"pset", required_argument, NULL, 'P'},
434  {"quiet", no_argument, NULL, 'q'},
435  {"record-separator", required_argument, NULL, 'R'},
436  {"record-separator-zero", no_argument, NULL, '0'},
437  {"single-step", no_argument, NULL, 's'},
438  {"single-line", no_argument, NULL, 'S'},
439  {"tuples-only", no_argument, NULL, 't'},
440  {"table-attr", required_argument, NULL, 'T'},
441  {"username", required_argument, NULL, 'U'},
442  {"set", required_argument, NULL, 'v'},
443  {"variable", required_argument, NULL, 'v'},
444  {"version", no_argument, NULL, 'V'},
445  {"no-password", no_argument, NULL, 'w'},
446  {"password", no_argument, NULL, 'W'},
447  {"expanded", no_argument, NULL, 'x'},
448  {"no-psqlrc", no_argument, NULL, 'X'},
449  {"help", optional_argument, NULL, 1},
450  {NULL, 0, NULL, 0}
451  };
452 
453  int optindex;
454  int c;
455 
456  memset(options, 0, sizeof *options);
457 
458  while ((c = getopt_long(argc, argv, "aAbc:d:eEf:F:h:HlL:no:p:P:qR:sStT:U:v:VwWxXz?01",
459  long_options, &optindex)) != -1)
460  {
461  switch (c)
462  {
463  case 'a':
464  SetVariable(pset.vars, "ECHO", "all");
465  break;
466  case 'A':
468  break;
469  case 'b':
470  SetVariable(pset.vars, "ECHO", "errors");
471  break;
472  case 'c':
473  if (optarg[0] == '\\')
476  optarg + 1);
477  else
480  optarg);
481  break;
482  case 'd':
483  options->dbname = pg_strdup(optarg);
484  break;
485  case 'e':
486  SetVariable(pset.vars, "ECHO", "queries");
487  break;
488  case 'E':
489  SetVariableBool(pset.vars, "ECHO_HIDDEN");
490  break;
491  case 'f':
493  ACT_FILE,
494  optarg);
495  break;
496  case 'F':
499  break;
500  case 'h':
501  options->host = pg_strdup(optarg);
502  break;
503  case 'H':
505  break;
506  case 'l':
507  options->list_dbs = true;
508  break;
509  case 'L':
510  options->logfilename = pg_strdup(optarg);
511  break;
512  case 'n':
513  options->no_readline = true;
514  break;
515  case 'o':
516  if (!setQFout(optarg))
517  exit(EXIT_FAILURE);
518  break;
519  case 'p':
520  options->port = pg_strdup(optarg);
521  break;
522  case 'P':
523  {
524  char *value;
525  char *equal_loc;
526  bool result;
527 
528  value = pg_strdup(optarg);
529  equal_loc = strchr(value, '=');
530  if (!equal_loc)
531  result = do_pset(value, NULL, &pset.popt, true);
532  else
533  {
534  *equal_loc = '\0';
535  result = do_pset(value, equal_loc + 1, &pset.popt, true);
536  }
537 
538  if (!result)
539  {
540  fprintf(stderr, _("%s: could not set printing parameter \"%s\"\n"), pset.progname, value);
541  exit(EXIT_FAILURE);
542  }
543 
544  free(value);
545  break;
546  }
547  case 'q':
548  SetVariableBool(pset.vars, "QUIET");
549  break;
550  case 'R':
553  break;
554  case 's':
555  SetVariableBool(pset.vars, "SINGLESTEP");
556  break;
557  case 'S':
558  SetVariableBool(pset.vars, "SINGLELINE");
559  break;
560  case 't':
561  pset.popt.topt.tuples_only = true;
562  break;
563  case 'T':
565  break;
566  case 'U':
567  options->username = pg_strdup(optarg);
568  break;
569  case 'v':
570  {
571  char *value;
572  char *equal_loc;
573 
574  value = pg_strdup(optarg);
575  equal_loc = strchr(value, '=');
576  if (!equal_loc)
577  {
578  if (!DeleteVariable(pset.vars, value))
579  exit(EXIT_FAILURE); /* error already printed */
580  }
581  else
582  {
583  *equal_loc = '\0';
584  if (!SetVariable(pset.vars, value, equal_loc + 1))
585  exit(EXIT_FAILURE); /* error already printed */
586  }
587 
588  free(value);
589  break;
590  }
591  case 'V':
592  showVersion();
593  exit(EXIT_SUCCESS);
594  case 'w':
596  break;
597  case 'W':
599  break;
600  case 'x':
601  pset.popt.topt.expanded = true;
602  break;
603  case 'X':
604  options->no_psqlrc = true;
605  break;
606  case 'z':
608  break;
609  case '0':
611  break;
612  case '1':
613  options->single_txn = true;
614  break;
615  case '?':
616  /* Actual help option given */
617  if (strcmp(argv[optind - 1], "-?") == 0)
618  {
619  usage(NOPAGER);
620  exit(EXIT_SUCCESS);
621  }
622  /* unknown option reported by getopt */
623  else
624  goto unknown_option;
625  break;
626  case 1:
627  {
628  if (!optarg || strcmp(optarg, "options") == 0)
629  usage(NOPAGER);
630  else if (optarg && strcmp(optarg, "commands") == 0)
632  else if (optarg && strcmp(optarg, "variables") == 0)
634  else
635  goto unknown_option;
636 
637  exit(EXIT_SUCCESS);
638  }
639  break;
640  default:
641  unknown_option:
642  fprintf(stderr, _("Try \"%s --help\" for more information.\n"),
643  pset.progname);
644  exit(EXIT_FAILURE);
645  break;
646  }
647  }
648 
649  /*
650  * if we still have arguments, use it as the database name and username
651  */
652  while (argc - optind >= 1)
653  {
654  if (!options->dbname)
655  options->dbname = argv[optind];
656  else if (!options->username)
657  options->username = argv[optind];
658  else if (!pset.quiet)
659  fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
660  pset.progname, argv[optind]);
661 
662  optind++;
663  }
664 }
#define NOPAGER
Definition: startup.c:94
static void usage(void)
Definition: pg_standby.c:503
static struct @76 value
SimpleActionList actions
Definition: startup.c:82
char * username
Definition: startup.c:76
#define EXIT_SUCCESS
Definition: settings.h:146
int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex)
Definition: getopt_long.c:57
enum printFormat format
Definition: print.h:98
printTableOpt topt
Definition: print.h:165
bool no_readline
Definition: startup.c:78
bool SetVariableBool(VariableSpace space, const char *name)
Definition: variables.c:367
static void simple_action_list_append(SimpleActionList *list, enum _actions action, const char *val)
Definition: startup.c:672
bool separator_zero
Definition: print.h:93
static void showVersion(void)
Definition: startup.c:766
#define required_argument
Definition: getopt_long.h:25
unsigned short int expanded
Definition: print.h:99
int optind
Definition: getopt.c:51
bool DeleteVariable(VariableSpace space, const char *name)
Definition: variables.c:379
bool tuples_only
Definition: print.h:107
char * c
char * port
Definition: startup.c:75
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
bool do_pset(const char *param, const char *value, printQueryOpt *popt, bool quiet)
Definition: command.c:2560
char * tableAttr
Definition: print.h:117
char * dbname
Definition: startup.c:73
char * logfilename
Definition: startup.c:77
#define no_argument
Definition: getopt_long.h:24
void helpVariables(unsigned short int pager)
Definition: help.c:320
bool setQFout(const char *fname)
Definition: common.c:84
PsqlSettings pset
Definition: startup.c:37
#define free(a)
Definition: header.h:60
struct separator fieldSep
Definition: print.h:113
#define NULL
Definition: c.h:226
bool SetVariable(VariableSpace space, const char *name, const char *value)
Definition: variables.c:211
const char * progname
Definition: settings.h:106
printQueryOpt popt
Definition: settings.h:91
bool single_txn
Definition: startup.c:80
enum trivalue getPassword
Definition: settings.h:101
#define optional_argument
Definition: getopt_long.h:26
bool no_psqlrc
Definition: startup.c:79
bool list_dbs
Definition: startup.c:81
char * host
Definition: startup.c:74
char * optarg
Definition: getopt.c:53
#define EXIT_FAILURE
Definition: settings.h:150
char * separator
Definition: print.h:92
struct separator recordSep
Definition: print.h:114
#define _(x)
Definition: elog.c:84
void slashUsage(unsigned short int pager)
Definition: help.c:159
VariableSpace vars
Definition: settings.h:115
static void process_psqlrc ( char *  argv0)
static

Definition at line 698 of file startup.c.

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

Referenced by main().

699 {
700  char home[MAXPGPATH];
701  char rc_file[MAXPGPATH];
702  char my_exec_path[MAXPGPATH];
703  char etc_path[MAXPGPATH];
704  char *envrc = getenv("PSQLRC");
705 
706  if (find_my_exec(argv0, my_exec_path) < 0)
707  {
708  fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
709  exit(EXIT_FAILURE);
710  }
711 
712  get_etc_path(my_exec_path, etc_path);
713 
714  snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
715  process_psqlrc_file(rc_file);
716 
717  if (envrc != NULL && strlen(envrc) > 0)
718  {
719  /* might need to free() this */
720  char *envrc_alloc = pstrdup(envrc);
721 
722  expand_tilde(&envrc_alloc);
723  process_psqlrc_file(envrc_alloc);
724  }
725  else if (get_home_path(home))
726  {
727  snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
728  process_psqlrc_file(rc_file);
729  }
730 }
static char * argv0
Definition: pg_ctl.c:92
char * pstrdup(const char *in)
Definition: mcxt.c:1165
#define SYSPSQLRC
Definition: startup.c:40
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PSQLRC
Definition: startup.c:41
void expand_tilde(char **filename)
Definition: common.c:2053
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:119
char my_exec_path[MAXPGPATH]
Definition: globals.c:63
static void process_psqlrc_file(char *filename)
Definition: startup.c:735
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition: path.c:713
#define NULL
Definition: c.h:226
#define EXIT_FAILURE
Definition: settings.h:150
#define _(x)
Definition: elog.c:84
bool get_home_path(char *ret_path)
Definition: path.c:807
static void process_psqlrc_file ( char *  filename)
static

Definition at line 735 of file startup.c.

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

Referenced by process_psqlrc().

736 {
737  char *psqlrc_minor,
738  *psqlrc_major;
739 
740 #if defined(WIN32) && (!defined(__MINGW32__))
741 #define R_OK 4
742 #endif
743 
744  psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
745  psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
746 
747  /* check for minor version first, then major, then no version */
748  if (access(psqlrc_minor, R_OK) == 0)
749  (void) process_file(psqlrc_minor, false);
750  else if (access(psqlrc_major, R_OK) == 0)
751  (void) process_file(psqlrc_major, false);
752  else if (access(filename, R_OK) == 0)
753  (void) process_file(filename, false);
754 
755  free(psqlrc_minor);
756  free(psqlrc_major);
757 }
char * psprintf(const char *fmt,...)
Definition: psprintf.c:46
static void process_file(const char *filename, int weight)
Definition: pgbench.c:3324
#define free(a)
Definition: header.h:60
static char * filename
Definition: pg_dumpall.c:84
static bool prompt1_hook ( const char *  newval)
static

Definition at line 1025 of file startup.c.

References _psqlSettings::prompt1.

Referenced by EstablishVariableSpace().

1026 {
1027  pset.prompt1 = newval ? newval : "";
1028  return true;
1029 }
PsqlSettings pset
Definition: startup.c:37
#define newval
const char * prompt1
Definition: settings.h:135
static bool prompt2_hook ( const char *  newval)
static

Definition at line 1032 of file startup.c.

References _psqlSettings::prompt2.

Referenced by EstablishVariableSpace().

1033 {
1034  pset.prompt2 = newval ? newval : "";
1035  return true;
1036 }
PsqlSettings pset
Definition: startup.c:37
const char * prompt2
Definition: settings.h:136
#define newval
static bool prompt3_hook ( const char *  newval)
static

Definition at line 1039 of file startup.c.

References _psqlSettings::prompt3.

Referenced by EstablishVariableSpace().

1040 {
1041  pset.prompt3 = newval ? newval : "";
1042  return true;
1043 }
const char * prompt3
Definition: settings.h:137
PsqlSettings pset
Definition: startup.c:37
#define newval
static bool quiet_hook ( const char *  newval)
static

Definition at line 814 of file startup.c.

References ParseVariableBool(), and _psqlSettings::quiet.

Referenced by EstablishVariableSpace().

815 {
816  return ParseVariableBool(newval, "QUIET", &pset.quiet);
817 }
PsqlSettings pset
Definition: startup.c:37
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static bool show_context_hook ( const char *  newval)
static

Definition at line 1083 of file startup.c.

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

Referenced by EstablishVariableSpace().

1084 {
1085  Assert(newval != NULL); /* else substitute hook messed up */
1086  if (pg_strcasecmp(newval, "never") == 0)
1088  else if (pg_strcasecmp(newval, "errors") == 0)
1090  else if (pg_strcasecmp(newval, "always") == 0)
1092  else
1093  {
1094  PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1095  return false;
1096  }
1097 
1098  if (pset.db)
1100  return true;
1101 }
PGconn * db
Definition: settings.h:82
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PGContextVisibility show_context
Definition: settings.h:139
PsqlSettings pset
Definition: startup.c:37
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:6086
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
static char* show_context_substitute_hook ( char *  newval)
static

Definition at line 1075 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1076 {
1077  if (newval == NULL)
1078  newval = pg_strdup("errors");
1079  return newval;
1080 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval
static void showVersion ( void  )
static

Definition at line 766 of file startup.c.

Referenced by main(), and parse_psql_options().

767 {
768  puts("psql (PostgreSQL) " PG_VERSION);
769 }
static void simple_action_list_append ( SimpleActionList list,
enum _actions  action,
const char *  val 
)
static

Definition at line 672 of file startup.c.

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

Referenced by main(), and parse_psql_options().

674 {
675  SimpleActionListCell *cell;
676 
678 
679  cell->next = NULL;
680  cell->action = action;
681  if (val)
682  cell->val = pg_strdup(val);
683  else
684  cell->val = NULL;
685 
686  if (list->tail)
687  list->tail->next = cell;
688  else
689  list->head = cell;
690  list->tail = cell;
691 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
enum _actions action
Definition: startup.c:61
SimpleActionListCell * head
Definition: startup.c:67
SimpleActionListCell * tail
Definition: startup.c:68
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct SimpleActionListCell * next
Definition: startup.c:60
#define NULL
Definition: c.h:226
long val
Definition: informix.c:689
static bool singleline_hook ( const char *  newval)
static

Definition at line 820 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singleline.

Referenced by EstablishVariableSpace().

821 {
822  return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
823 }
bool singleline
Definition: settings.h:125
PsqlSettings pset
Definition: startup.c:37
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static bool singlestep_hook ( const char *  newval)
static

Definition at line 826 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singlestep.

Referenced by EstablishVariableSpace().

827 {
828  return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
829 }
bool singlestep
Definition: settings.h:126
PsqlSettings pset
Definition: startup.c:37
#define newval
bool ParseVariableBool(const char *value, const char *name, bool *result)
Definition: variables.c:107
static bool verbosity_hook ( const char *  newval)
static

Definition at line 1054 of file startup.c.

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

Referenced by EstablishVariableSpace().

1055 {
1056  Assert(newval != NULL); /* else substitute hook messed up */
1057  if (pg_strcasecmp(newval, "default") == 0)
1059  else if (pg_strcasecmp(newval, "terse") == 0)
1061  else if (pg_strcasecmp(newval, "verbose") == 0)
1063  else
1064  {
1065  PsqlVarEnumError("VERBOSITY", newval, "default, terse, verbose");
1066  return false;
1067  }
1068 
1069  if (pset.db)
1071  return true;
1072 }
PGconn * db
Definition: settings.h:82
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:37
PGVerbosity verbosity
Definition: settings.h:138
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:671
#define newval
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6074
static char* verbosity_substitute_hook ( char *  newval)
static

Definition at line 1046 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1047 {
1048  if (newval == NULL)
1049  newval = pg_strdup("default");
1050  return newval;
1051 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:226
#define newval

Variable Documentation

Definition at line 37 of file startup.c.

Referenced by AcceptResult(), CheckConnection(), ClearOrSaveResult(), command_no_begin(), connection_warnings(), ConnectionUp(), describeAccessMethods(), describeAggregates(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describePublications(), describeRoles(), describeSubscriptions(), describeTableDetails(), describeTablespaces(), describeTypes(), do_connect(), do_copy(), do_lo_export(), do_lo_import(), do_lo_list(), do_lo_unlink(), do_pset(), do_watch(), echo_hidden_command(), exec_command(), ExecQueryTuples(), ExecQueryUsingCursor(), fail_lo_xact(), finish_lo_xact(), finishInput(), get_create_object_cmd(), get_prompt(), handleCopyIn(), HandleSlashCmds(), helpSQL(), helpVariables(), indexOfColumn(), initializeInput(), is_select_command(), is_superuser(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtensionContents(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listOneExtensionContents(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSConfigsVerbose(), listTSDictionaries(), listTSParsers(), listTSParsersVerbose(), listTSTemplates(), listUserMappings(), lookup_object_oid(), MainLoop(), objectDescription(), parse_slash_copy(), permissionsList(), pg_send_history(), print_lo_result(), printACLColumn(), printCrosstab(), printHistory(), PrintNotifications(), PrintQueryResults(), PrintQueryStatus(), PrintQueryTuples(), PrintResultsInCrosstab(), printSSLInfo(), process_file(), ProcessResult(), psql_error(), psql_get_variable(), PSQLexec(), PSQLexecWatch(), SendQuery(), session_username(), SetCancelConn(), setQFout(), skip_white_space(), slashUsage(), standard_strings(), start_lo_xact(), StoreQueryTuple(), SyncVariables(), UnsyncVariables(), and usage().