aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/startup.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/startup.c')
-rw-r--r--src/bin/psql/startup.c202
1 files changed, 171 insertions, 31 deletions
diff --git a/src/bin/psql/startup.c b/src/bin/psql/startup.c
index 1a937e1eb47..8d3409bd194 100644
--- a/src/bin/psql/startup.c
+++ b/src/bin/psql/startup.c
@@ -3,7 +3,7 @@
*
* Copyright (c) 2000-2006, PostgreSQL Global Development Group
*
- * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.135 2006/07/14 14:52:26 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/psql/startup.c,v 1.136 2006/08/29 15:19:51 tgl Exp $
*/
#include "postgres_fe.h"
@@ -84,14 +84,14 @@ static void parse_psql_options(int argc, char *argv[],
static void process_psqlrc(char *argv0);
static void process_psqlrc_file(char *filename);
static void showVersion(void);
+static void EstablishVariableSpace(void);
#ifdef USE_SSL
static void printSSLInfo(void);
#endif
#ifdef WIN32
-static void
- checkWin32Codepage(void);
+static void checkWin32Codepage(void);
#endif
/*
@@ -134,34 +134,19 @@ main(int argc, char *argv[])
pset.progname = get_progname(argv[0]);
+ pset.db = NULL;
setDecimalLocale();
+ pset.encoding = PQenv2encoding();
+ pset.queryFout = stdout;
+ pset.queryFoutPipe = false;
pset.cur_cmd_source = stdin;
pset.cur_cmd_interactive = false;
- pset.encoding = PQenv2encoding();
- pset.vars = CreateVariableSpace();
- if (!pset.vars)
- {
- fprintf(stderr, _("%s: out of memory\n"), pset.progname);
- exit(EXIT_FAILURE);
- }
pset.popt.topt.format = PRINT_ALIGNED;
- pset.queryFout = stdout;
pset.popt.topt.border = 1;
pset.popt.topt.pager = 1;
pset.popt.default_footer = true;
- SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
-
- /* Default values for variables */
- SetVariableBool(pset.vars, "AUTOCOMMIT");
- SetVariable(pset.vars, "VERBOSITY", "default");
- SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
- SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
- SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
-
- pset.verbosity = PQERRORS_DEFAULT;
-
pset.notty = (!isatty(fileno(stdin)) || !isatty(fileno(stdout)));
/* This is obsolete and should be removed sometime. */
@@ -171,6 +156,17 @@ main(int argc, char *argv[])
pset.getPassword = false;
#endif
+ EstablishVariableSpace();
+
+ SetVariable(pset.vars, "VERSION", PG_VERSION_STR);
+
+ /* Default values for variables */
+ SetVariableBool(pset.vars, "AUTOCOMMIT");
+ SetVariable(pset.vars, "VERBOSITY", "default");
+ SetVariable(pset.vars, "PROMPT1", DEFAULT_PROMPT1);
+ SetVariable(pset.vars, "PROMPT2", DEFAULT_PROMPT2);
+ SetVariable(pset.vars, "PROMPT3", DEFAULT_PROMPT3);
+
parse_psql_options(argc, argv, &options);
if (!pset.popt.topt.fieldSep)
@@ -239,9 +235,6 @@ main(int argc, char *argv[])
SyncVariables();
- /* Grab the backend server version */
- pset.sversion = PQserverVersion(pset.db);
-
if (options.action == ACT_LIST_DB)
{
int success = listAllDbs(false);
@@ -280,7 +273,7 @@ main(int argc, char *argv[])
{
PsqlScanState scan_state;
- if (VariableEquals(pset.vars, "ECHO", "all"))
+ if (pset.echo == PSQL_ECHO_ALL)
puts(options.action_string);
scan_state = psql_scan_create();
@@ -299,7 +292,7 @@ main(int argc, char *argv[])
*/
else if (options.action == ACT_SINGLE_QUERY)
{
- if (VariableEquals(pset.vars, "ECHO", "all"))
+ if (pset.echo == PSQL_ECHO_ALL)
puts(options.action_string);
successResult = SendQuery(options.action_string)
@@ -314,7 +307,7 @@ main(int argc, char *argv[])
if (!options.no_psqlrc)
process_psqlrc(argv[0]);
- if (!QUIET() && !pset.notty)
+ if (!pset.quiet && !pset.notty)
{
int client_ver = parse_version(PG_VERSION);
@@ -644,14 +637,14 @@ parse_psql_options(int argc, char *argv[], struct adhoc_opts * options)
options->dbname = argv[optind];
else if (!options->username)
options->username = argv[optind];
- else if (!QUIET())
+ else if (!pset.quiet)
fprintf(stderr, _("%s: warning: extra command-line argument \"%s\" ignored\n"),
pset.progname, argv[optind]);
optind++;
}
- if (used_old_u_option && !QUIET())
+ if (used_old_u_option && !pset.quiet)
fprintf(stderr, _("%s: Warning: The -u option is deprecated. Use -U.\n"), pset.progname);
}
@@ -743,7 +736,6 @@ printSSLInfo(void)
#endif
-
/*
* checkWin32Codepage
*
@@ -768,3 +760,151 @@ checkWin32Codepage(void)
}
#endif
+
+
+/*
+ * Assign hooks for psql variables.
+ *
+ * This isn't an amazingly good place for them, but neither is anywhere else.
+ */
+
+static void
+autocommit_hook(const char *newval)
+{
+ pset.autocommit = ParseVariableBool(newval);
+}
+
+static void
+on_error_stop_hook(const char *newval)
+{
+ pset.on_error_stop = ParseVariableBool(newval);
+}
+
+static void
+quiet_hook(const char *newval)
+{
+ pset.quiet = ParseVariableBool(newval);
+}
+
+static void
+singleline_hook(const char *newval)
+{
+ pset.singleline = ParseVariableBool(newval);
+}
+
+static void
+singlestep_hook(const char *newval)
+{
+ pset.singlestep = ParseVariableBool(newval);
+}
+
+static void
+echo_hook(const char *newval)
+{
+ if (newval == NULL)
+ pset.echo = PSQL_ECHO_NONE;
+ else if (strcmp(newval, "queries") == 0)
+ pset.echo = PSQL_ECHO_QUERIES;
+ else if (strcmp(newval, "all") == 0)
+ pset.echo = PSQL_ECHO_ALL;
+ else
+ pset.echo = PSQL_ECHO_NONE;
+}
+
+static void
+echo_hidden_hook(const char *newval)
+{
+ if (newval == NULL)
+ pset.echo_hidden = PSQL_ECHO_HIDDEN_OFF;
+ else if (strcmp(newval, "noexec") == 0)
+ pset.echo_hidden = PSQL_ECHO_HIDDEN_NOEXEC;
+ else if (pg_strcasecmp(newval, "off") == 0)
+ pset.echo_hidden = PSQL_ECHO_HIDDEN_OFF;
+ else
+ pset.echo_hidden = PSQL_ECHO_HIDDEN_ON;
+}
+
+static void
+on_error_rollback_hook(const char *newval)
+{
+ if (newval == NULL)
+ pset.on_error_rollback = PSQL_ERROR_ROLLBACK_OFF;
+ else if (pg_strcasecmp(newval, "interactive") == 0)
+ pset.on_error_rollback = PSQL_ERROR_ROLLBACK_INTERACTIVE;
+ else if (pg_strcasecmp(newval, "off") == 0)
+ pset.on_error_rollback = PSQL_ERROR_ROLLBACK_OFF;
+ else
+ pset.on_error_rollback = PSQL_ERROR_ROLLBACK_ON;
+}
+
+static void
+histcontrol_hook(const char *newval)
+{
+ if (newval == NULL)
+ pset.histcontrol = hctl_none;
+ else if (strcmp(newval, "ignorespace") == 0)
+ pset.histcontrol = hctl_ignorespace;
+ else if (strcmp(newval, "ignoredups") == 0)
+ pset.histcontrol = hctl_ignoredups;
+ else if (strcmp(newval, "ignoreboth") == 0)
+ pset.histcontrol = hctl_ignoreboth;
+ else
+ pset.histcontrol = hctl_none;
+}
+
+static void
+prompt1_hook(const char *newval)
+{
+ pset.prompt1 = newval ? newval : "";
+}
+
+static void
+prompt2_hook(const char *newval)
+{
+ pset.prompt2 = newval ? newval : "";
+}
+
+static void
+prompt3_hook(const char *newval)
+{
+ pset.prompt3 = newval ? newval : "";
+}
+
+static void
+verbosity_hook(const char *newval)
+{
+ if (newval == NULL)
+ pset.verbosity = PQERRORS_DEFAULT;
+ else if (strcmp(newval, "default") == 0)
+ pset.verbosity = PQERRORS_DEFAULT;
+ else if (strcmp(newval, "terse") == 0)
+ pset.verbosity = PQERRORS_TERSE;
+ else if (strcmp(newval, "verbose") == 0)
+ pset.verbosity = PQERRORS_VERBOSE;
+ else
+ pset.verbosity = PQERRORS_DEFAULT;
+
+ if (pset.db)
+ PQsetErrorVerbosity(pset.db, pset.verbosity);
+}
+
+
+static void
+EstablishVariableSpace(void)
+{
+ pset.vars = CreateVariableSpace();
+
+ SetVariableAssignHook(pset.vars, "AUTOCOMMIT", autocommit_hook);
+ SetVariableAssignHook(pset.vars, "ON_ERROR_STOP", on_error_stop_hook);
+ SetVariableAssignHook(pset.vars, "QUIET", quiet_hook);
+ SetVariableAssignHook(pset.vars, "SINGLELINE", singleline_hook);
+ SetVariableAssignHook(pset.vars, "SINGLESTEP", singlestep_hook);
+ SetVariableAssignHook(pset.vars, "ECHO", echo_hook);
+ SetVariableAssignHook(pset.vars, "ECHO_HIDDEN", echo_hidden_hook);
+ SetVariableAssignHook(pset.vars, "ON_ERROR_ROLLBACK", on_error_rollback_hook);
+ SetVariableAssignHook(pset.vars, "HISTCONTROL", histcontrol_hook);
+ SetVariableAssignHook(pset.vars, "PROMPT1", prompt1_hook);
+ SetVariableAssignHook(pset.vars, "PROMPT2", prompt2_hook);
+ SetVariableAssignHook(pset.vars, "PROMPT3", prompt3_hook);
+ SetVariableAssignHook(pset.vars, "VERBOSITY", verbosity_hook);
+}