aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBruce Momjian <bruce@momjian.us>2002-07-06 20:12:30 +0000
committerBruce Momjian <bruce@momjian.us>2002-07-06 20:12:30 +0000
commit7015111a19eba481f3abf3f8d6149cd417bf6de5 (patch)
treeaa7dc3eaf73cc2961bf15f1f2a9ead5c219e1ee9 /src
parentd66f172f4b75df4fccae02612a181f1161e0865d (diff)
downloadpostgresql-7015111a19eba481f3abf3f8d6149cd417bf6de5.tar.gz
postgresql-7015111a19eba481f3abf3f8d6149cd417bf6de5.zip
Move simple_prompt() into its own file to be shared with psql and pg_dump.
Diffstat (limited to 'src')
-rw-r--r--src/bin/pg_dump/Makefile6
-rw-r--r--src/bin/pg_dump/pg_backup_db.c110
-rw-r--r--src/bin/pg_dump/pg_dump.h5
-rw-r--r--src/bin/pg_dump/sprompt.c121
-rw-r--r--src/bin/psql/Makefile4
-rw-r--r--src/bin/psql/common.c116
-rw-r--r--src/bin/psql/common.h5
-rw-r--r--src/bin/psql/sprompt.c121
8 files changed, 258 insertions, 230 deletions
diff --git a/src/bin/pg_dump/Makefile b/src/bin/pg_dump/Makefile
index cebb113e1c5..3df5ff6d4a0 100644
--- a/src/bin/pg_dump/Makefile
+++ b/src/bin/pg_dump/Makefile
@@ -5,7 +5,7 @@
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
-# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.33 2002/06/20 20:29:41 momjian Exp $
+# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.34 2002/07/06 20:12:30 momjian Exp $
#
#-------------------------------------------------------------------------
@@ -13,8 +13,8 @@ subdir = src/bin/pg_dump
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
-OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o pg_backup_files.o \
- pg_backup_null.o pg_backup_tar.o
+OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \
+ pg_backup_files.o pg_backup_null.o pg_backup_tar.o sprompt.o
ifdef STRDUP
OBJS+=$(top_builddir)/src/utils/strdup.o
diff --git a/src/bin/pg_dump/pg_backup_db.c b/src/bin/pg_dump/pg_backup_db.c
index f9fcaf7f59c..a50f71bcaae 100644
--- a/src/bin/pg_dump/pg_backup_db.c
+++ b/src/bin/pg_dump/pg_backup_db.c
@@ -5,7 +5,7 @@
* Implements the basic DB functions used by the archiver.
*
* IDENTIFICATION
- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.34 2002/07/04 15:35:07 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.35 2002/07/06 20:12:30 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -37,114 +37,6 @@ static char *_sendSQLLine(ArchiveHandle *AH, char *qry, char *eos);
static char *_sendCopyLine(ArchiveHandle *AH, char *qry, char *eos);
-/*
- * simple_prompt --- borrowed from psql
- *
- * Generalized function especially intended for reading in usernames and
- * password interactively. Reads from /dev/tty or stdin/stderr.
- *
- * prompt: The prompt to print
- * maxlen: How many characters to accept
- * echo: Set to false if you want to hide what is entered (for passwords)
- *
- * Returns a malloc()'ed string with the input (w/o trailing newline).
- */
-static bool prompt_state = false;
-
-char *
-simple_prompt(const char *prompt, int maxlen, bool echo)
-{
- int length;
- char *destination;
- FILE *termin,
- *termout;
-
-#ifdef HAVE_TERMIOS_H
- struct termios t_orig,
- t;
-#endif
-
- destination = (char *) malloc(maxlen + 2);
- if (!destination)
- return NULL;
-
- prompt_state = true; /* disable SIGINT */
-
- /*
- * Do not try to collapse these into one "w+" mode file. Doesn't work
- * on some platforms (eg, HPUX 10.20).
- */
- termin = fopen("/dev/tty", "r");
- termout = fopen("/dev/tty", "w");
- if (!termin || !termout)
- {
- if (termin)
- fclose(termin);
- if (termout)
- fclose(termout);
- termin = stdin;
- termout = stderr;
- }
-
-#ifdef HAVE_TERMIOS_H
- if (!echo)
- {
- tcgetattr(fileno(termin), &t);
- t_orig = t;
- t.c_lflag &= ~ECHO;
- tcsetattr(fileno(termin), TCSAFLUSH, &t);
- }
-#endif
-
- if (prompt)
- {
- fputs(gettext(prompt), termout);
- fflush(termout);
- }
-
- if (fgets(destination, maxlen, termin) == NULL)
- destination[0] = '\0';
-
- length = strlen(destination);
- if (length > 0 && destination[length - 1] != '\n')
- {
- /* eat rest of the line */
- char buf[128];
- int buflen;
-
- do
- {
- if (fgets(buf, sizeof(buf), termin) == NULL)
- break;
- buflen = strlen(buf);
- } while (buflen > 0 && buf[buflen - 1] != '\n');
- }
-
- if (length > 0 && destination[length - 1] == '\n')
- /* remove trailing newline */
- destination[length - 1] = '\0';
-
-#ifdef HAVE_TERMIOS_H
- if (!echo)
- {
- tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
- fputs("\n", termout);
- fflush(termout);
- }
-#endif
-
- if (termin != stdin)
- {
- fclose(termin);
- fclose(termout);
- }
-
- prompt_state = false; /* SIGINT okay again */
-
- return destination;
-}
-
-
static int
_parse_version(ArchiveHandle *AH, const char *versionString)
{
diff --git a/src/bin/pg_dump/pg_dump.h b/src/bin/pg_dump/pg_dump.h
index 3483fe2c0f6..17491b027e5 100644
--- a/src/bin/pg_dump/pg_dump.h
+++ b/src/bin/pg_dump/pg_dump.h
@@ -6,7 +6,7 @@
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
- * $Id: pg_dump.h,v 1.89 2002/07/02 05:49:52 momjian Exp $
+ * $Id: pg_dump.h,v 1.90 2002/07/06 20:12:30 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@@ -209,4 +209,7 @@ extern void dumpTables(Archive *fout, TableInfo tblinfo[], int numTables,
const bool schemaOnly, const bool dataOnly);
extern void dumpIndexes(Archive *fout, TableInfo *tbinfo, int numTables);
+/* sprompt.h */
+extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
+
#endif /* PG_DUMP_H */
diff --git a/src/bin/pg_dump/sprompt.c b/src/bin/pg_dump/sprompt.c
new file mode 100644
index 00000000000..6bc0d986a10
--- /dev/null
+++ b/src/bin/pg_dump/sprompt.c
@@ -0,0 +1,121 @@
+/*
+ * psql - the PostgreSQL interactive terminal
+ *
+ * Copyright 2000 by PostgreSQL Global Development Group
+ *
+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/sprompt.c,v 1.1 2002/07/06 20:12:30 momjian Exp $
+ */
+
+/*
+ * simple_prompt
+ *
+ * Generalized function especially intended for reading in usernames and
+ * password interactively. Reads from /dev/tty or stdin/stderr.
+ *
+ * prompt: The prompt to print
+ * maxlen: How many characters to accept
+ * echo: Set to false if you want to hide what is entered (for passwords)
+ *
+ * Returns a malloc()'ed string with the input (w/o trailing newline).
+ */
+#include "postgres_fe.h"
+
+#ifdef HAVE_TERMIOS_H
+#include <termios.h>
+#endif
+
+bool prompt_state = false;
+extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
+
+char *
+simple_prompt(const char *prompt, int maxlen, bool echo)
+{
+ int length;
+ char *destination;
+ FILE *termin,
+ *termout;
+
+#ifdef HAVE_TERMIOS_H
+ struct termios t_orig,
+ t;
+#endif
+
+ destination = (char *) malloc(maxlen + 2);
+ if (!destination)
+ return NULL;
+
+ prompt_state = true; /* disable SIGINT */
+
+ /*
+ * Do not try to collapse these into one "w+" mode file. Doesn't work
+ * on some platforms (eg, HPUX 10.20).
+ */
+ termin = fopen("/dev/tty", "r");
+ termout = fopen("/dev/tty", "w");
+ if (!termin || !termout)
+ {
+ if (termin)
+ fclose(termin);
+ if (termout)
+ fclose(termout);
+ termin = stdin;
+ termout = stderr;
+ }
+
+#ifdef HAVE_TERMIOS_H
+ if (!echo)
+ {
+ tcgetattr(fileno(termin), &t);
+ t_orig = t;
+ t.c_lflag &= ~ECHO;
+ tcsetattr(fileno(termin), TCSAFLUSH, &t);
+ }
+#endif
+
+ if (prompt)
+ {
+ fputs(gettext(prompt), termout);
+ fflush(termout);
+ }
+
+ if (fgets(destination, maxlen, termin) == NULL)
+ destination[0] = '\0';
+
+ length = strlen(destination);
+ if (length > 0 && destination[length - 1] != '\n')
+ {
+ /* eat rest of the line */
+ char buf[128];
+ int buflen;
+
+ do
+ {
+ if (fgets(buf, sizeof(buf), termin) == NULL)
+ break;
+ buflen = strlen(buf);
+ } while (buflen > 0 && buf[buflen - 1] != '\n');
+ }
+
+ if (length > 0 && destination[length - 1] == '\n')
+ /* remove trailing newline */
+ destination[length - 1] = '\0';
+
+#ifdef HAVE_TERMIOS_H
+ if (!echo)
+ {
+ tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
+ fputs("\n", termout);
+ fflush(termout);
+ }
+#endif
+
+ if (termin != stdin)
+ {
+ fclose(termin);
+ fclose(termout);
+ }
+
+ prompt_state = false; /* SIGINT okay again */
+
+ return destination;
+}
diff --git a/src/bin/psql/Makefile b/src/bin/psql/Makefile
index 7ab5a906566..8a7416a89c2 100644
--- a/src/bin/psql/Makefile
+++ b/src/bin/psql/Makefile
@@ -5,7 +5,7 @@
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
-# $Header: /cvsroot/pgsql/src/bin/psql/Makefile,v 1.32 2002/06/20 20:29:42 momjian Exp $
+# $Header: /cvsroot/pgsql/src/bin/psql/Makefile,v 1.33 2002/07/06 20:12:30 momjian Exp $
#
#-------------------------------------------------------------------------
@@ -19,7 +19,7 @@ override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
OBJS=command.o common.o help.o input.o stringutils.o mainloop.o \
copy.o startup.o prompt.o variables.o large_obj.o print.o describe.o \
- tab-complete.o mbprint.o
+ sprompt.o tab-complete.o mbprint.o
all: submake psql
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index bd2c87fa7f6..1b1e4626609 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
- * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.40 2002/03/06 06:10:31 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/common.c,v 1.41 2002/07/06 20:12:30 momjian Exp $
*/
#include "postgres_fe.h"
@@ -12,9 +12,6 @@
#include <errno.h>
#include <stdarg.h>
#include <sys/time.h>
-#ifdef HAVE_TERMIOS_H
-#include <termios.h>
-#endif
#ifndef HAVE_STRDUP
#include <strdup.h>
#endif
@@ -37,6 +34,7 @@
#include "print.h"
#include "mainloop.h"
+extern bool prompt_state;
/*
* "Safe" wrapper around strdup()
@@ -159,115 +157,6 @@ NoticeProcessor(void *arg, const char *message)
/*
- * simple_prompt
- *
- * Generalized function especially intended for reading in usernames and
- * password interactively. Reads from /dev/tty or stdin/stderr.
- *
- * prompt: The prompt to print
- * maxlen: How many characters to accept
- * echo: Set to false if you want to hide what is entered (for passwords)
- *
- * Returns a malloc()'ed string with the input (w/o trailing newline).
- */
-static bool prompt_state = false;
-
-char *
-simple_prompt(const char *prompt, int maxlen, bool echo)
-{
- int length;
- char *destination;
- FILE *termin,
- *termout;
-
-#ifdef HAVE_TERMIOS_H
- struct termios t_orig,
- t;
-#endif
-
- destination = (char *) malloc(maxlen + 2);
- if (!destination)
- return NULL;
-
- prompt_state = true; /* disable SIGINT */
-
- /*
- * Do not try to collapse these into one "w+" mode file. Doesn't work
- * on some platforms (eg, HPUX 10.20).
- */
- termin = fopen("/dev/tty", "r");
- termout = fopen("/dev/tty", "w");
- if (!termin || !termout)
- {
- if (termin)
- fclose(termin);
- if (termout)
- fclose(termout);
- termin = stdin;
- termout = stderr;
- }
-
-#ifdef HAVE_TERMIOS_H
- if (!echo)
- {
- tcgetattr(fileno(termin), &t);
- t_orig = t;
- t.c_lflag &= ~ECHO;
- tcsetattr(fileno(termin), TCSAFLUSH, &t);
- }
-#endif
-
- if (prompt)
- {
- fputs(gettext(prompt), termout);
- fflush(termout);
- }
-
- if (fgets(destination, maxlen, termin) == NULL)
- destination[0] = '\0';
-
- length = strlen(destination);
- if (length > 0 && destination[length - 1] != '\n')
- {
- /* eat rest of the line */
- char buf[128];
- int buflen;
-
- do
- {
- if (fgets(buf, sizeof(buf), termin) == NULL)
- break;
- buflen = strlen(buf);
- } while (buflen > 0 && buf[buflen - 1] != '\n');
- }
-
- if (length > 0 && destination[length - 1] == '\n')
- /* remove trailing newline */
- destination[length - 1] = '\0';
-
-#ifdef HAVE_TERMIOS_H
- if (!echo)
- {
- tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
- fputs("\n", termout);
- fflush(termout);
- }
-#endif
-
- if (termin != stdin)
- {
- fclose(termin);
- fclose(termout);
- }
-
- prompt_state = false; /* SIGINT okay again */
-
- return destination;
-}
-
-
-
-/*
* Code to support query cancellation
*
* Before we start a query, we enable a SIGINT signal catcher that sends a
@@ -276,7 +165,6 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
* so. We use write() to print to stdout because it's better to use simple
* facilities in a signal handler.
*/
-
PGconn *cancelConn;
volatile bool cancel_pressed;
diff --git a/src/bin/psql/common.h b/src/bin/psql/common.h
index a148fcce0a0..f11a7981278 100644
--- a/src/bin/psql/common.h
+++ b/src/bin/psql/common.h
@@ -3,7 +3,7 @@
*
* Copyright 2000 by PostgreSQL Global Development Group
*
- * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.17 2001/11/05 17:46:31 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/common.h,v 1.18 2002/07/06 20:12:30 momjian Exp $
*/
#ifndef COMMON_H
#define COMMON_H
@@ -37,4 +37,7 @@ extern PGresult *PSQLexec(const char *query);
extern bool SendQuery(const char *query);
+/* sprompt.h */
+extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
+
#endif /* COMMON_H */
diff --git a/src/bin/psql/sprompt.c b/src/bin/psql/sprompt.c
new file mode 100644
index 00000000000..c9c2ab5cfc1
--- /dev/null
+++ b/src/bin/psql/sprompt.c
@@ -0,0 +1,121 @@
+/*
+ * psql - the PostgreSQL interactive terminal
+ *
+ * Copyright 2000 by PostgreSQL Global Development Group
+ *
+ * $Header: /cvsroot/pgsql/src/bin/psql/Attic/sprompt.c,v 1.1 2002/07/06 20:12:30 momjian Exp $
+ */
+
+/*
+ * simple_prompt
+ *
+ * Generalized function especially intended for reading in usernames and
+ * password interactively. Reads from /dev/tty or stdin/stderr.
+ *
+ * prompt: The prompt to print
+ * maxlen: How many characters to accept
+ * echo: Set to false if you want to hide what is entered (for passwords)
+ *
+ * Returns a malloc()'ed string with the input (w/o trailing newline).
+ */
+#include "postgres_fe.h"
+
+#ifdef HAVE_TERMIOS_H
+#include <termios.h>
+#endif
+
+bool prompt_state = false;
+extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
+
+char *
+simple_prompt(const char *prompt, int maxlen, bool echo)
+{
+ int length;
+ char *destination;
+ FILE *termin,
+ *termout;
+
+#ifdef HAVE_TERMIOS_H
+ struct termios t_orig,
+ t;
+#endif
+
+ destination = (char *) malloc(maxlen + 2);
+ if (!destination)
+ return NULL;
+
+ prompt_state = true; /* disable SIGINT */
+
+ /*
+ * Do not try to collapse these into one "w+" mode file. Doesn't work
+ * on some platforms (eg, HPUX 10.20).
+ */
+ termin = fopen("/dev/tty", "r");
+ termout = fopen("/dev/tty", "w");
+ if (!termin || !termout)
+ {
+ if (termin)
+ fclose(termin);
+ if (termout)
+ fclose(termout);
+ termin = stdin;
+ termout = stderr;
+ }
+
+#ifdef HAVE_TERMIOS_H
+ if (!echo)
+ {
+ tcgetattr(fileno(termin), &t);
+ t_orig = t;
+ t.c_lflag &= ~ECHO;
+ tcsetattr(fileno(termin), TCSAFLUSH, &t);
+ }
+#endif
+
+ if (prompt)
+ {
+ fputs(gettext(prompt), termout);
+ fflush(termout);
+ }
+
+ if (fgets(destination, maxlen, termin) == NULL)
+ destination[0] = '\0';
+
+ length = strlen(destination);
+ if (length > 0 && destination[length - 1] != '\n')
+ {
+ /* eat rest of the line */
+ char buf[128];
+ int buflen;
+
+ do
+ {
+ if (fgets(buf, sizeof(buf), termin) == NULL)
+ break;
+ buflen = strlen(buf);
+ } while (buflen > 0 && buf[buflen - 1] != '\n');
+ }
+
+ if (length > 0 && destination[length - 1] == '\n')
+ /* remove trailing newline */
+ destination[length - 1] = '\0';
+
+#ifdef HAVE_TERMIOS_H
+ if (!echo)
+ {
+ tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
+ fputs("\n", termout);
+ fflush(termout);
+ }
+#endif
+
+ if (termin != stdin)
+ {
+ fclose(termin);
+ fclose(termout);
+ }
+
+ prompt_state = false; /* SIGINT okay again */
+
+ return destination;
+}