Move simple_prompt() into its own file to be shared with psql and pg_dump.
authorBruce Momjian <bruce@momjian.us>
Sat, 6 Jul 2002 20:12:30 +0000 (20:12 +0000)
committerBruce Momjian <bruce@momjian.us>
Sat, 6 Jul 2002 20:12:30 +0000 (20:12 +0000)
src/bin/pg_dump/Makefile
src/bin/pg_dump/pg_backup_db.c
src/bin/pg_dump/pg_dump.h
src/bin/pg_dump/sprompt.c [new file with mode: 0644]
src/bin/psql/Makefile
src/bin/psql/common.c
src/bin/psql/common.h
src/bin/psql/sprompt.c [new file with mode: 0644]

index cebb113e1c54edf64e1a5710c6b9118fad0dac97..3df5ff6d4a0988d9418d07fe2746715799796329 100644 (file)
@@ -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
index f9fcaf7f59c2a5f0a2c50ba9684698dcd911333f..a50f71bcaaeca5a24854f3360cd69e8ff7069651 100644 (file)
@@ -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)
 {
index 3483fe2c0f6d2d8f698a5e106f06f94f5beffffc..17491b027e57d0cdacc114585b9b356e80c85055 100644 (file)
@@ -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 (file)
index 0000000..6bc0d98
--- /dev/null
@@ -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;
+}
index 7ab5a906566e28bed0645630c087a0f2db06f123..8a7416a89c23a7b9b55d88ca068fd1f5a17645d9 100644 (file)
@@ -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
 
index bd2c87fa7f6e35d8ed32adaee6c6b82f5ebe88b1..1b1e462660995fd3dfdd3686ab8c6bfb94a14408 100644 (file)
@@ -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()
@@ -158,115 +156,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
  *
@@ -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;
 
index a148fcce0a0275edf466ae3265325676214413a1..f11a7981278207838699856be6e0e8395594f576 100644 (file)
@@ -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 (file)
index 0000000..c9c2ab5
--- /dev/null
@@ -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;
+}