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

References _psqlSettings::autocommit, and ParseVariableBool().

Referenced by EstablishVariableSpace().

806 {
807  return ParseVariableBool(newval, "AUTOCOMMIT", &pset.autocommit);
808 }
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 788 of file startup.c.

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

Referenced by EstablishVariableSpace().

789 {
790  if (newval == NULL)
791  {
792  /* "\unset FOO" becomes "\set FOO off" */
793  newval = pg_strdup("off");
794  }
795  else if (newval[0] == '\0')
796  {
797  /* "\set FOO" becomes "\set FOO on" */
798  pg_free(newval);
799  newval = pg_strdup("on");
800  }
801  return newval;
802 }
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 978 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().

979 {
980  Assert(newval != NULL); /* else substitute hook messed up */
981  if (pg_strcasecmp(newval, "preserve-upper") == 0)
983  else if (pg_strcasecmp(newval, "preserve-lower") == 0)
985  else if (pg_strcasecmp(newval, "upper") == 0)
987  else if (pg_strcasecmp(newval, "lower") == 0)
989  else
990  {
991  PsqlVarEnumError("COMP_KEYWORD_CASE", newval,
992  "lower, upper, preserve-lower, preserve-upper");
993  return false;
994  }
995  return true;
996 }
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 970 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

971 {
972  if (newval == NULL)
973  newval = pg_strdup("preserve-upper");
974  return newval;
975 }
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 928 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().

929 {
930  Assert(newval != NULL); /* else substitute hook messed up */
931  if (pg_strcasecmp(newval, "noexec") == 0)
933  else
934  {
935  bool on_off;
936 
937  if (ParseVariableBool(newval, NULL, &on_off))
939  else
940  {
941  PsqlVarEnumError("ECHO_HIDDEN", newval, "on, off, noexec");
942  return false;
943  }
944  }
945  return true;
946 }
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 908 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().

909 {
910  Assert(newval != NULL); /* else substitute hook messed up */
911  if (pg_strcasecmp(newval, "queries") == 0)
913  else if (pg_strcasecmp(newval, "errors") == 0)
915  else if (pg_strcasecmp(newval, "all") == 0)
917  else if (pg_strcasecmp(newval, "none") == 0)
919  else
920  {
921  PsqlVarEnumError("ECHO", newval, "none, errors, queries, all");
922  return false;
923  }
924  return true;
925 }
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 900 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

901 {
902  if (newval == NULL)
903  newval = pg_strdup("none");
904  return newval;
905 }
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 1108 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().

1109 {
1111 
1112  SetVariableHooks(pset.vars, "AUTOCOMMIT",
1114  autocommit_hook);
1115  SetVariableHooks(pset.vars, "ON_ERROR_STOP",
1118  SetVariableHooks(pset.vars, "QUIET",
1120  quiet_hook);
1121  SetVariableHooks(pset.vars, "SINGLELINE",
1123  singleline_hook);
1124  SetVariableHooks(pset.vars, "SINGLESTEP",
1126  singlestep_hook);
1127  SetVariableHooks(pset.vars, "FETCH_COUNT",
1130  SetVariableHooks(pset.vars, "HISTFILE",
1131  NULL,
1132  histfile_hook);
1133  SetVariableHooks(pset.vars, "HISTSIZE",
1135  histsize_hook);
1136  SetVariableHooks(pset.vars, "IGNOREEOF",
1138  ignoreeof_hook);
1139  SetVariableHooks(pset.vars, "ECHO",
1141  echo_hook);
1142  SetVariableHooks(pset.vars, "ECHO_HIDDEN",
1145  SetVariableHooks(pset.vars, "ON_ERROR_ROLLBACK",
1148  SetVariableHooks(pset.vars, "COMP_KEYWORD_CASE",
1151  SetVariableHooks(pset.vars, "HISTCONTROL",
1154  SetVariableHooks(pset.vars, "PROMPT1",
1155  NULL,
1156  prompt1_hook);
1157  SetVariableHooks(pset.vars, "PROMPT2",
1158  NULL,
1159  prompt2_hook);
1160  SetVariableHooks(pset.vars, "PROMPT3",
1161  NULL,
1162  prompt3_hook);
1163  SetVariableHooks(pset.vars, "VERBOSITY",
1165  verbosity_hook);
1166  SetVariableHooks(pset.vars, "SHOW_CONTEXT",
1169 }
static char * verbosity_substitute_hook(char *newval)
Definition: startup.c:1049
static bool on_error_stop_hook(const char *newval)
Definition: startup.c:811
static bool echo_hook(const char *newval)
Definition: startup.c:908
static bool quiet_hook(const char *newval)
Definition: startup.c:817
static bool histcontrol_hook(const char *newval)
Definition: startup.c:1007
static char * bool_substitute_hook(char *newval)
Definition: startup.c:788
VariableSpace CreateVariableSpace(void)
Definition: variables.c:51
static bool prompt2_hook(const char *newval)
Definition: startup.c:1035
static bool singleline_hook(const char *newval)
Definition: startup.c:823
static char * echo_substitute_hook(char *newval)
Definition: startup.c:900
static bool autocommit_hook(const char *newval)
Definition: startup.c:805
static char * comp_keyword_case_substitute_hook(char *newval)
Definition: startup.c:970
static bool fetch_count_hook(const char *newval)
Definition: startup.c:843
static bool singlestep_hook(const char *newval)
Definition: startup.c:829
static bool show_context_hook(const char *newval)
Definition: startup.c:1086
static bool on_error_rollback_hook(const char *newval)
Definition: startup.c:949
static char * show_context_substitute_hook(char *newval)
Definition: startup.c:1078
static bool histsize_hook(const char *newval)
Definition: startup.c:867
PsqlSettings pset
Definition: startup.c:33
static char * histsize_substitute_hook(char *newval)
Definition: startup.c:859
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:978
static char * histcontrol_substitute_hook(char *newval)
Definition: startup.c:999
static bool ignoreeof_hook(const char *newval)
Definition: startup.c:894
static char * ignoreeof_substitute_hook(char *newval)
Definition: startup.c:873
static bool prompt1_hook(const char *newval)
Definition: startup.c:1028
static bool histfile_hook(const char *newval)
Definition: startup.c:849
static bool verbosity_hook(const char *newval)
Definition: startup.c:1057
static char * fetch_count_substitute_hook(char *newval)
Definition: startup.c:835
static bool prompt3_hook(const char *newval)
Definition: startup.c:1042
static bool echo_hidden_hook(const char *newval)
Definition: startup.c:928
VariableSpace vars
Definition: settings.h:116
static bool fetch_count_hook ( const char *  newval)
static

Definition at line 843 of file startup.c.

References _psqlSettings::fetch_count, and ParseVariableNum().

Referenced by EstablishVariableSpace().

844 {
845  return ParseVariableNum(newval, "FETCH_COUNT", &pset.fetch_count);
846 }
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 835 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

836 {
837  if (newval == NULL)
838  newval = pg_strdup("0");
839  return newval;
840 }
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 1007 of file startup.c.

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

Referenced by EstablishVariableSpace().

1008 {
1009  Assert(newval != NULL); /* else substitute hook messed up */
1010  if (pg_strcasecmp(newval, "ignorespace") == 0)
1012  else if (pg_strcasecmp(newval, "ignoredups") == 0)
1014  else if (pg_strcasecmp(newval, "ignoreboth") == 0)
1016  else if (pg_strcasecmp(newval, "none") == 0)
1018  else
1019  {
1020  PsqlVarEnumError("HISTCONTROL", newval,
1021  "none, ignorespace, ignoredups, ignoreboth");
1022  return false;
1023  }
1024  return true;
1025 }
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 999 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1000 {
1001  if (newval == NULL)
1002  newval = pg_strdup("none");
1003  return newval;
1004 }
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 849 of file startup.c.

Referenced by EstablishVariableSpace().

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

Definition at line 867 of file startup.c.

References _psqlSettings::histsize, and ParseVariableNum().

Referenced by EstablishVariableSpace().

868 {
869  return ParseVariableNum(newval, "HISTSIZE", &pset.histsize);
870 }
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 859 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

860 {
861  if (newval == NULL)
862  newval = pg_strdup("500");
863  return newval;
864 }
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 894 of file startup.c.

References _psqlSettings::ignoreeof, and ParseVariableNum().

Referenced by EstablishVariableSpace().

895 {
896  return ParseVariableNum(newval, "IGNOREEOF", &pset.ignoreeof);
897 }
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 873 of file startup.c.

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

Referenced by EstablishVariableSpace().

874 {
875  int dummy;
876 
877  /*
878  * This tries to mimic the behavior of bash, to wit "If set, the value is
879  * the number of consecutive EOF characters which must be typed as the
880  * first characters on an input line before bash exits. If the variable
881  * exists but does not have a numeric value, or has no value, the default
882  * value is 10. If it does not exist, EOF signifies the end of input to
883  * the shell." Unlike bash, however, we insist on the stored value
884  * actually being a valid integer.
885  */
886  if (newval == NULL)
887  newval = pg_strdup("0");
888  else if (!ParseVariableNum(newval, NULL, &dummy))
889  newval = pg_strdup("10");
890  return newval;
891 }
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, conditional_stack_create(), conditional_stack_destroy(), 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_set_passthrough(), 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  ConditionalStack cond_stack;
335 
336  if (pset.echo == PSQL_ECHO_ALL)
337  puts(cell->val);
338 
339  scan_state = psql_scan_create(&psqlscan_callbacks);
340  psql_scan_setup(scan_state,
341  cell->val, strlen(cell->val),
343  cond_stack = conditional_stack_create();
344  psql_scan_set_passthrough(scan_state, (void *) cond_stack);
345 
346  successResult = HandleSlashCmds(scan_state,
347  cond_stack,
348  NULL,
349  NULL) != PSQL_CMD_ERROR
351 
352  psql_scan_destroy(scan_state);
353  conditional_stack_destroy(cond_stack);
354  }
355  else if (cell->action == ACT_FILE)
356  {
357  successResult = process_file(cell->val, false);
358  }
359  else
360  {
361  /* should never come here */
362  Assert(false);
363  }
364 
365  if (successResult != EXIT_SUCCESS && pset.on_error_stop)
366  break;
367  }
368 
369  if (options.single_txn)
370  {
371  if ((res = PSQLexec("COMMIT")) == NULL)
372  {
373  if (pset.on_error_stop)
374  {
375  successResult = EXIT_USER;
376  goto error;
377  }
378  }
379  else
380  PQclear(res);
381  }
382 
383 error:
384  ;
385  }
386 
387  /*
388  * or otherwise enter interactive main loop
389  */
390  else
391  {
392  connection_warnings(true);
393  if (!pset.quiet)
394  printf(_("Type \"help\" for help.\n\n"));
395  initializeInput(options.no_readline ? 0 : 1);
396  successResult = MainLoop(stdin);
397  }
398 
399  /* clean up */
400  if (pset.logfile)
401  fclose(pset.logfile);
402  PQfinish(pset.db);
403  setQFout(NULL);
404 
405  return successResult;
406 }
static void parse_psql_options(int argc, char *argv[], struct adhoc_opts *options)
Definition: startup.c:414
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:701
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:6014
#define EXIT_SUCCESS
Definition: settings.h:147
PQnoticeProcessor PQsetNoticeProcessor(PGconn *conn, PQnoticeProcessor proc, void *arg)
Definition: fe-connect.c:6190
void SyncVariables(void)
Definition: command.c:3330
static void error(void)
Definition: sql-dyntest.c:147
static void EstablishVariableSpace(void)
Definition: startup.c:1108
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:3547
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:675
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:769
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:510
#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:1201
#define PG_TEXTDOMAIN(domain)
Definition: c.h:1012
ConditionalStack conditional_stack_create(void)
Definition: conditional.c:16
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:2058
#define free(a)
Definition: header.h:65
struct separator fieldSep
Definition: print.h:113
void NoticeProcessor(void *arg, const char *message)
Definition: common.c:241
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
void conditional_stack_destroy(ConditionalStack cstack)
Definition: conditional.c:28
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int PQconnectionNeedsPassword(const PGconn *conn)
Definition: fe-connect.c:6048
void set_pglocale_pgservice(const char *argv0, const char *app)
Definition: exec.c:550
void psql_scan_set_passthrough(PsqlScanState state, void *passthrough)
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:1230
void connection_warnings(bool in_startup)
Definition: command.c:3223
struct separator recordSep
Definition: print.h:114
backslashResult HandleSlashCmds(PsqlScanState scan_state, ConditionalStack cstack, PQExpBuffer query_buf, PQExpBuffer previous_buf)
Definition: command.c:202
int encoding
Definition: settings.h:83
ConnStatusType PQstatus(const PGconn *conn)
Definition: fe-connect.c:5961
#define DEFAULT_FIELD_SEP
Definition: settings.h:15
#define _(x)
Definition: elog.c:84
bool listAllDbs(const char *pattern, bool verbose)
Definition: describe.c:780
PGresult * PSQLexec(const char *query)
Definition: common.c:642
VariableSpace vars
Definition: settings.h:116
static bool on_error_rollback_hook ( const char *  newval)
static

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

950 {
951  Assert(newval != NULL); /* else substitute hook messed up */
952  if (pg_strcasecmp(newval, "interactive") == 0)
954  else
955  {
956  bool on_off;
957 
958  if (ParseVariableBool(newval, NULL, &on_off))
960  else
961  {
962  PsqlVarEnumError("ON_ERROR_ROLLBACK", newval, "on, off, interactive");
963  return false;
964  }
965  }
966  return true;
967 }
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 811 of file startup.c.

References _psqlSettings::on_error_stop, and ParseVariableBool().

Referenced by EstablishVariableSpace().

812 {
813  return ParseVariableBool(newval, "ON_ERROR_STOP", &pset.on_error_stop);
814 }
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 414 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().

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

702 {
703  char home[MAXPGPATH];
704  char rc_file[MAXPGPATH];
705  char my_exec_path[MAXPGPATH];
706  char etc_path[MAXPGPATH];
707  char *envrc = getenv("PSQLRC");
708 
709  if (find_my_exec(argv0, my_exec_path) < 0)
710  {
711  fprintf(stderr, _("%s: could not find own program executable\n"), argv0);
712  exit(EXIT_FAILURE);
713  }
714 
715  get_etc_path(my_exec_path, etc_path);
716 
717  snprintf(rc_file, MAXPGPATH, "%s/%s", etc_path, SYSPSQLRC);
718  process_psqlrc_file(rc_file);
719 
720  if (envrc != NULL && strlen(envrc) > 0)
721  {
722  /* might need to free() this */
723  char *envrc_alloc = pstrdup(envrc);
724 
725  expand_tilde(&envrc_alloc);
726  process_psqlrc_file(envrc_alloc);
727  }
728  else if (get_home_path(home))
729  {
730  snprintf(rc_file, MAXPGPATH, "%s/%s", home, PSQLRC);
731  process_psqlrc_file(rc_file);
732  }
733 }
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:2103
#define MAXPGPATH
int find_my_exec(const char *argv0, char *retpath)
Definition: exec.c:119
char my_exec_path[MAXPGPATH]
Definition: globals.c:64
static void process_psqlrc_file(char *filename)
Definition: startup.c:738
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 738 of file startup.c.

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

Referenced by process_psqlrc().

739 {
740  char *psqlrc_minor,
741  *psqlrc_major;
742 
743 #if defined(WIN32) && (!defined(__MINGW32__))
744 #define R_OK 4
745 #endif
746 
747  psqlrc_minor = psprintf("%s-%s", filename, PG_VERSION);
748  psqlrc_major = psprintf("%s-%s", filename, PG_MAJORVERSION);
749 
750  /* check for minor version first, then major, then no version */
751  if (access(psqlrc_minor, R_OK) == 0)
752  (void) process_file(psqlrc_minor, false);
753  else if (access(psqlrc_major, R_OK) == 0)
754  (void) process_file(psqlrc_major, false);
755  else if (access(filename, R_OK) == 0)
756  (void) process_file(filename, false);
757 
758  free(psqlrc_minor);
759  free(psqlrc_major);
760 }
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:89
static bool prompt1_hook ( const char *  newval)
static

Definition at line 1028 of file startup.c.

References _psqlSettings::prompt1.

Referenced by EstablishVariableSpace().

1029 {
1030  pset.prompt1 = newval ? newval : "";
1031  return true;
1032 }
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 1035 of file startup.c.

References _psqlSettings::prompt2.

Referenced by EstablishVariableSpace().

1036 {
1037  pset.prompt2 = newval ? newval : "";
1038  return true;
1039 }
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 1042 of file startup.c.

References _psqlSettings::prompt3.

Referenced by EstablishVariableSpace().

1043 {
1044  pset.prompt3 = newval ? newval : "";
1045  return true;
1046 }
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 817 of file startup.c.

References ParseVariableBool(), and _psqlSettings::quiet.

Referenced by EstablishVariableSpace().

818 {
819  return ParseVariableBool(newval, "QUIET", &pset.quiet);
820 }
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 1086 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().

1087 {
1088  Assert(newval != NULL); /* else substitute hook messed up */
1089  if (pg_strcasecmp(newval, "never") == 0)
1091  else if (pg_strcasecmp(newval, "errors") == 0)
1093  else if (pg_strcasecmp(newval, "always") == 0)
1095  else
1096  {
1097  PsqlVarEnumError("SHOW_CONTEXT", newval, "never, errors, always");
1098  return false;
1099  }
1100 
1101  if (pset.db)
1103  return true;
1104 }
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:6140
#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 1078 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1079 {
1080  if (newval == NULL)
1081  newval = pg_strdup("errors");
1082  return newval;
1083 }
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 769 of file startup.c.

Referenced by main(), and parse_psql_options().

770 {
771  puts("psql (PostgreSQL) " PG_VERSION);
772 }
static void simple_action_list_append ( SimpleActionList list,
enum _actions  action,
const char *  val 
)
static

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

677 {
678  SimpleActionListCell *cell;
679 
681 
682  cell->next = NULL;
683  cell->action = action;
684  if (val)
685  cell->val = pg_strdup(val);
686  else
687  cell->val = NULL;
688 
689  if (list->tail)
690  list->tail->next = cell;
691  else
692  list->head = cell;
693  list->tail = cell;
694 }
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 823 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singleline.

Referenced by EstablishVariableSpace().

824 {
825  return ParseVariableBool(newval, "SINGLELINE", &pset.singleline);
826 }
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 829 of file startup.c.

References ParseVariableBool(), and _psqlSettings::singlestep.

Referenced by EstablishVariableSpace().

830 {
831  return ParseVariableBool(newval, "SINGLESTEP", &pset.singlestep);
832 }
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 1057 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().

1058 {
1059  Assert(newval != NULL); /* else substitute hook messed up */
1060  if (pg_strcasecmp(newval, "default") == 0)
1062  else if (pg_strcasecmp(newval, "terse") == 0)
1064  else if (pg_strcasecmp(newval, "verbose") == 0)
1066  else
1067  {
1068  PsqlVarEnumError("VERBOSITY", newval, "default, terse, verbose");
1069  return false;
1070  }
1071 
1072  if (pset.db)
1074  return true;
1075 }
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:6128
static char* verbosity_substitute_hook ( char *  newval)
static

Definition at line 1049 of file startup.c.

References newval, NULL, and pg_strdup().

Referenced by EstablishVariableSpace().

1050 {
1051  if (newval == NULL)
1052  newval = pg_strdup("default");
1053  return newval;
1054 }
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(), exec_command_a(), exec_command_C(), exec_command_conninfo(), exec_command_crosstabview(), exec_command_echo(), exec_command_ef(), exec_command_encoding(), exec_command_errverbose(), exec_command_ev(), exec_command_f(), exec_command_g(), exec_command_gexec(), exec_command_gset(), exec_command_help(), exec_command_html(), exec_command_password(), exec_command_print(), exec_command_prompt(), exec_command_pset(), exec_command_reset(), exec_command_s(), exec_command_set(), exec_command_sf(), exec_command_slash_command_help(), exec_command_sv(), exec_command_t(), exec_command_T(), exec_command_timing(), exec_command_unset(), exec_command_x(), 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().