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 <unistd.h>
#include "getopt_long.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 90 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 37 of file startup.c.

Referenced by process_psqlrc().

#define SYSPSQLRC   "psqlrc"

Definition at line 36 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 47 of file startup.c.

Function Documentation

static bool autocommit_hook ( const char *  newval)
static

Definition at line 798 of file startup.c.

References _psqlSettings::autocommit, and ParseVariableBool().

Referenced by EstablishVariableSpace().

799 {
800  return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
801 }
bool autocommit
Definition: settings.h:123
PsqlSettings pset
Definition: startup.c:33
#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 781 of file startup.c.

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

Referenced by EstablishVariableSpace().

782 {
783  if (newval == NULL)
784  {
785  /* "\unset FOO" becomes "\set FOO off" */
786  newval = pg_strdup("off");
787  }
788  else if (newval[0] == '\0')
789  {
790  /* "\set FOO" becomes "\set FOO on" */
791  pg_free(newval);
792  newval = pg_strdup("on");
793  }
794  return newval;
795 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#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 971 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().

972 {
973  Assert(newval != NULL); /* else substitute hook messed up */
974  if (pg_strcasecmp(newval, "preserve-upper") == 0)
976  else if (pg_strcasecmp(newval, "preserve-lower") == 0)
978  else if (pg_strcasecmp(newval, "upper") == 0)
980  else if (pg_strcasecmp(newval, "lower") == 0)
982  else
983  {
984  PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
985  "lower, upper, preserve-lower, preserve-upper");
986  return false;
987  }
988  return true;
989 }
void PsqlVarEnumError(const char *name, const char *value, const char *suggestions)
Definition: variables.c:391
PSQL_COMP_CASE comp_case
Definition: settings.h:134
int pg_strcasecmp(const char *s1, const char *s2)
Definition: pgstrcasecmp.c:36
PsqlSettings pset
Definition: startup.c:33
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define newval
static char* comp_keyword_case_substitute_hook ( char *  newval)
static

Definition at line 963 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

964 {
965  if (newval == NULL)
966  newval = pg_strdup("preserve-upper");
967  return newval;
968 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval
static bool echo_hidden_hook ( const char *  newval)
static

Definition at line 921 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().

922 {
923  Assert(newval != NULL); /* else substitute hook messed up */
924  if (pg_strcasecmp(newval, "noexec") == 0)
926  else
927  {
928  bool on_off;
929 
930  if (ParseVariableBool(newval, NULL, &on_off))
932  else
933  {
934  PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
935  return false;
936  }
937  }
938  return true;
939 }
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:132
PsqlSettings pset
Definition: startup.c:33
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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 901 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().

902 {
903  Assert(newval != NULL); /* else substitute hook messed up */
904  if (pg_strcasecmp(newval, "queries") == 0)
906  else if (pg_strcasecmp(newval, "errors") == 0)
908  else if (pg_strcasecmp(newval, "all") == 0)
910  else if (pg_strcasecmp(newval, "none") == 0)
912  else
913  {
914  PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
915  return false;
916  }
917  return true;
918 }
PSQL_ECHO echo
Definition: settings.h:131
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:33
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define newval
static char* echo_substitute_hook ( char *  newval)
static

Definition at line 893 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

894 {
895  if (newval == NULL)
896  newval = pg_strdup("none");
897  return newval;
898 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval
static void EstablishVariableSpace ( void  )
static

Definition at line 1101 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().

1102 {
1104 
1105  SetVariableHooks(pset.vars, "AUTOCOMMIT",
1107  autocommit_hook);
1108  SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1111  SetVariableHooks(pset.vars, "QUIET",
1113  quiet_hook);
1114  SetVariableHooks(pset.vars, "SINGLELINE",
1116  singleline_hook);
1117  SetVariableHooks(pset.vars, "SINGLESTEP",
1119  singlestep_hook);
1120  SetVariableHooks(pset.vars, "FETCH_COUNT",
1123  SetVariableHooks(pset.vars, "HISTFILE",
1124  NULL,
1125  histfile_hook);
1126  SetVariableHooks(pset.vars, "HISTSIZE",
1128  histsize_hook);
1129  SetVariableHooks(pset.vars, "IGNOREEOF",
1131  ignoreeof_hook);
1132  SetVariableHooks(pset.vars, "ECHO",
1134  echo_hook);
1135  SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1138  SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1141  SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1144  SetVariableHooks(pset.vars, "HISTCONTROL",
1147  SetVariableHooks(pset.vars, "PROMPT1",
1148  NULL,
1149  prompt1_hook);
1150  SetVariableHooks(pset.vars, "PROMPT2",
1151  NULL,
1152  prompt2_hook);
1153  SetVariableHooks(pset.vars, "PROMPT3",
1154  NULL,
1155  prompt3_hook);
1156  SetVariableHooks(pset.vars, "VERBOSITY",
1158  verbosity_hook);
1159  SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1162 }
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1042
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:804
static bool echo_hook(const char *newval)
Definition: startup.c:901
static bool quiet_hook(const char *newval)
Definition: startup.c:810
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1000
static char * bool_substitute_hook(char *newval)
Definition: startup.c:781
VariableSpace CreateVariableSpace(void)
Definition: variables.c:51
static bool prompt2_hook(const char *newval)
Definition: startup.c:1028
static bool singleline_hook(const char *newval)
Definition: startup.c:816
static char * echo_substitute_hook(char *newval)
Definition: startup.c:893
static bool autocommit_hook(const char *newval)
Definition: startup.c:798
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:963
static bool fetch_count_hook(const char *newval)
Definition: startup.c:836
static bool singlestep_hook(const char *newval)
Definition: startup.c:822
static bool show_context_hook(const char *newval)
Definition: startup.c:1079
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:942
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1071
static bool histsize_hook(const char *newval)
Definition: startup.c:860
PsqlSettings pset
Definition: startup.c:33
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:852
void SetVariableHooks(VariableSpace space, const char *name, VariableSubstituteHook shook, VariableAssignHook ahook)
Definition: variables.c:315
#define NULL
Definition: c.h:229
static bool comp_keyword_case_hook(const char *newval)
Definition: startup.c:971
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:992
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:887
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:866
static bool prompt1_hook(const char *newval)
Definition: startup.c:1021
static bool histfile_hook(const char *newval)
Definition: startup.c:842
static bool verbosity_hook(const char *newval)
Definition: startup.c:1050
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:828
static bool prompt3_hook(const char *newval)
Definition: startup.c:1035
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:921
VariableSpace vars
Definition: settings.h:116
static bool fetch_count_hook ( const char *  newval)
static

Definition at line 836 of file startup.c.

References _psqlSettings::fetch_count, and ParseVariableNum().

Referenced by EstablishVariableSpace().

837 {
838  return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
839 }
bool ParseVariableNum(const char *value, const char *name, int *result)
Definition: variables.c:156
int fetch_count
Definition: settings.h:128
PsqlSettings pset
Definition: startup.c:33
#define newval
static char* fetch_count_substitute_hook ( char *  newval)
static

Definition at line 828 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

829 {
830  if (newval == NULL)
831  newval = pg_strdup("0");
832  return newval;
833 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval
static bool histcontrol_hook ( const char *  newval)
static

Definition at line 1000 of file startup.c.

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

Referenced by EstablishVariableSpace().

1001 {
1002  Assert(newval != NULL); /* else substitute hook messed up */
1003  if (pg_strcasecmp(newval, "ignorespace") == 0)
1005  else if (pg_strcasecmp(newval, "ignoredups") == 0)
1007  else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1009  else if (pg_strcasecmp(newval, "none") == 0)
1011  else
1012  {
1013  PsqlVarEnumError("HISTCONTROL", newval,
1014  "none, ignorespace, ignoredups, ignoreboth");
1015  return false;
1016  }
1017  return true;
1018 }
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:33
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define newval
HistControl histcontrol
Definition: settings.h:135
static char* histcontrol_substitute_hook ( char *  newval)
static

Definition at line 992 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

993 {
994  if (newval == NULL)
995  newval = pg_strdup("none");
996  return newval;
997 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval
static bool histfile_hook ( const char *  newval)
static

Definition at line 842 of file startup.c.

Referenced by EstablishVariableSpace().

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

Definition at line 860 of file startup.c.

References _psqlSettings::histsize, and ParseVariableNum().

Referenced by EstablishVariableSpace().

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

Definition at line 852 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

853 {
854  if (newval == NULL)
855  newval = pg_strdup("500");
856  return newval;
857 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval
static bool ignoreeof_hook ( const char *  newval)
static

Definition at line 887 of file startup.c.

References _psqlSettings::ignoreeof, and ParseVariableNum().

Referenced by EstablishVariableSpace().

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

Definition at line 866 of file startup.c.

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

Referenced by EstablishVariableSpace().

867 {
868  int dummy;
869 
870  /*
871  * This tries to mimic the behavior of bash, to wit "If set, the value is
872  * the number of consecutive EOF characters which must be typed as the
873  * first characters on an input line before bash exits. If the variable
874  * exists but does not have a numeric value, or has no value, the default
875  * value is 10. If it does not exist, EOF signifies the end of input to
876  * the shell." Unlike bash, however, we insist on the stored value
877  * actually being a valid integer.
878  */
879  if (newval == NULL)
880  newval = pg_strdup("0");
881  else if (!ParseVariableNum(newval, NULL, &dummy))
882  newval = pg_strdup("10");
883  return newval;
884 }
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:229
#define newval
int main ( int  argc,
char *  argv[] 
)

Definition at line 98 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.

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

Definition at line 942 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().

943 {
944  Assert(newval != NULL); /* else substitute hook messed up */
945  if (pg_strcasecmp(newval, "interactive") == 0)
947  else
948  {
949  bool on_off;
950 
951  if (ParseVariableBool(newval, NULL, &on_off))
953  else
954  {
955  PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
956  return false;
957  }
958  }
959  return true;
960 }
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:133
PsqlSettings pset
Definition: startup.c:33
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#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 804 of file startup.c.

References _psqlSettings::on_error_stop, and ParseVariableBool().

Referenced by EstablishVariableSpace().

805 {
806  return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
807 }
bool on_error_stop
Definition: settings.h:124
PsqlSettings pset
Definition: startup.c:33
#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 407 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, result, 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().

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

Definition at line 694 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().

695 {
696  char home[MAXPGPATH];
697  char rc_file[MAXPGPATH];
698  char my_exec_path[MAXPGPATH];
699  char etc_path[MAXPGPATH];
700  char *envrc = getenv("PSQLRC");
701 
702  if (find_my_exec(argv0, my_exec_path) < 0)
703  {
704  fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
705  exit(EXIT_FAILURE);
706  }
707 
708  get_etc_path(my_exec_path, etc_path);
709 
710  snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
711  process_psqlrc_file(rc_file);
712 
713  if (envrc != NULL && strlen(envrc) > 0)
714  {
715  /* might need to free() this */
716  char *envrc_alloc = pstrdup(envrc);
717 
718  expand_tilde(&envrc_alloc);
719  process_psqlrc_file(envrc_alloc);
720  }
721  else if (get_home_path(home))
722  {
723  snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
724  process_psqlrc_file(rc_file);
725  }
726 }
static char * argv0
Definition: pg_ctl.c:89
char * pstrdup(const char *in)
Definition: mcxt.c:1077
#define SYSPSQLRC
Definition: startup.c:36
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
#define PSQLRC
Definition: startup.c:37
void expand_tilde(char **filename)
Definition: common.c:2064
#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:731
void get_etc_path(const char *my_exec_path, char *ret_path)
Definition: path.c:713
#define NULL
Definition: c.h:229
#define EXIT_FAILURE
Definition: settings.h:151
#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 731 of file startup.c.

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

Referenced by process_psqlrc().

732 {
733  char *psqlrc_minor,
734  *psqlrc_major;
735 
736 #if defined(WIN32) && (!defined(__MINGW32__))
737 #define R_OK 4
738 #endif
739 
740  psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
741  psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
742 
743  /* check for minor version first, then major, then no version */
744  if (access(psqlrc_minor, R_OK) == 0)
745  (void) process_file(psqlrc_minor, false);
746  else if (access(psqlrc_major, R_OK) == 0)
747  (void) process_file(psqlrc_major, false);
748  else if (access(filename, R_OK) == 0)
749  (void) process_file(filename, false);
750 
751  free(psqlrc_minor);
752  free(psqlrc_major);
753 }
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:65
static char * filename
Definition: pg_dumpall.c:87
static bool prompt1_hook ( const char *  newval)
static

Definition at line 1021 of file startup.c.

References _psqlSettings::prompt1.

Referenced by EstablishVariableSpace().

1022 {
1023  pset.prompt1 = newval ? newval : "";
1024  return true;
1025 }
PsqlSettings pset
Definition: startup.c:33
#define newval
const char * prompt1
Definition: settings.h:136
static bool prompt2_hook ( const char *  newval)
static

Definition at line 1028 of file startup.c.

References _psqlSettings::prompt2.

Referenced by EstablishVariableSpace().

1029 {
1030  pset.prompt2 = newval ? newval : "";
1031  return true;
1032 }
PsqlSettings pset
Definition: startup.c:33
const char * prompt2
Definition: settings.h:137
#define newval
static bool prompt3_hook ( const char *  newval)
static

Definition at line 1035 of file startup.c.

References _psqlSettings::prompt3.

Referenced by EstablishVariableSpace().

1036 {
1037  pset.prompt3 = newval ? newval : "";
1038  return true;
1039 }
const char * prompt3
Definition: settings.h:138
PsqlSettings pset
Definition: startup.c:33
#define newval
static bool quiet_hook ( const char *  newval)
static

Definition at line 810 of file startup.c.

References ParseVariableBool(), and _psqlSettings::quiet.

Referenced by EstablishVariableSpace().

811 {
812  return ParseVariableBool(newval, "QUIET", &pset.quiet);
813 }
PsqlSettings pset
Definition: startup.c:33
#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 1079 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().

1080 {
1081  Assert(newval != NULL); /* else substitute hook messed up */
1082  if (pg_strcasecmp(newval, "never") == 0)
1084  else if (pg_strcasecmp(newval, "errors") == 0)
1086  else if (pg_strcasecmp(newval, "always") == 0)
1088  else
1089  {
1090  PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1091  return false;
1092  }
1093 
1094  if (pset.db)
1096  return true;
1097 }
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:140
PsqlSettings pset
Definition: startup.c:33
PGContextVisibility PQsetErrorContextVisibility(PGconn *conn, PGContextVisibility show_context)
Definition: fe-connect.c:6137
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define newval
static char* show_context_substitute_hook ( char *  newval)
static

Definition at line 1071 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1072 {
1073  if (newval == NULL)
1074  newval = pg_strdup("errors");
1075  return newval;
1076 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval
static void showVersion ( void  )
static

Definition at line 762 of file startup.c.

Referenced by main(), and parse_psql_options().

763 {
764  puts("psql (PostgreSQL) " PG_VERSION);
765 }
static void simple_action_list_append ( SimpleActionList list,
enum _actions  action,
const char *  val 
)
static

Definition at line 668 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().

670 {
671  SimpleActionListCell *cell;
672 
674 
675  cell->next = NULL;
676  cell->action = action;
677  if (val)
678  cell->val = pg_strdup(val);
679  else
680  cell->val = NULL;
681 
682  if (list->tail)
683  list->tail->next = cell;
684  else
685  list->head = cell;
686  list->tail = cell;
687 }
void * pg_malloc(size_t size)
Definition: fe_memutils.c:47
enum _actions action
Definition: startup.c:57
SimpleActionListCell * head
Definition: startup.c:63
SimpleActionListCell * tail
Definition: startup.c:64
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
struct SimpleActionListCell * next
Definition: startup.c:56
#define NULL
Definition: c.h:229
long val
Definition: informix.c:689
static bool singleline_hook ( const char *  newval)
static

Definition at line 816 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singleline.

Referenced by EstablishVariableSpace().

817 {
818  return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
819 }
bool singleline
Definition: settings.h:126
PsqlSettings pset
Definition: startup.c:33
#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 822 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singlestep.

Referenced by EstablishVariableSpace().

823 {
824  return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
825 }
bool singlestep
Definition: settings.h:127
PsqlSettings pset
Definition: startup.c:33
#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 1050 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().

1051 {
1052  Assert(newval != NULL); /* else substitute hook messed up */
1053  if (pg_strcasecmp(newval, "default") == 0)
1055  else if (pg_strcasecmp(newval, "terse") == 0)
1057  else if (pg_strcasecmp(newval, "verbose") == 0)
1059  else
1060  {
1061  PsqlVarEnumError("VERBOSITY", newval, "default, terse, verbose");
1062  return false;
1063  }
1064 
1065  if (pset.db)
1067  return true;
1068 }
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:33
PGVerbosity verbosity
Definition: settings.h:139
#define NULL
Definition: c.h:229
#define Assert(condition)
Definition: c.h:675
#define newval
PGVerbosity PQsetErrorVerbosity(PGconn *conn, PGVerbosity verbosity)
Definition: fe-connect.c:6125
static char* verbosity_substitute_hook ( char *  newval)
static

Definition at line 1042 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1043 {
1044  if (newval == NULL)
1045  newval = pg_strdup("default");
1046  return newval;
1047 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
#define NULL
Definition: c.h:229
#define newval

Variable Documentation

Definition at line 33 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().