55 * Implements the basic DB functions used by the archiver.
66 *
77 * IDENTIFICATION
8- * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.26 2001/09/21 21:58:30 petere Exp $
8+ * $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.27 2001/10/15 16:40:27 momjian Exp $
99 *
1010 * NOTES
1111 *
@@ -49,53 +49,73 @@ static void notice_processor(void *arg, const char *message);
4949 * simple_prompt
5050 *
5151 * Generalized function especially intended for reading in usernames and
52- * password interactively. Reads from stdin.
52+ * password interactively. Reads from /dev/tty or stdin/stderr .
5353 *
5454 * prompt: The prompt to print
5555 * maxlen: How many characters to accept
5656 * echo: Set to false if you want to hide what is entered (for passwords)
5757 *
5858 * Returns a malloc()'ed string with the input (w/o trailing newline).
5959 */
60+ static bool prompt_state ;
61+
6062char *
6163simple_prompt (const char * prompt , int maxlen , bool echo )
6264{
6365 int length ;
6466 char * destination ;
67+ static FILE * termin = NULL , * termout ;
6568
6669#ifdef HAVE_TERMIOS_H
6770 struct termios t_orig ,
6871 t ;
69-
7072#endif
7173
7274 destination = (char * ) malloc (maxlen + 2 );
7375 if (!destination )
7476 return NULL ;
77+
78+ prompt_state = true;
79+
80+ /* initialize the streams */
81+ if (!termin )
82+ {
83+ if ((termin = termout = fopen ("/dev/tty" , "w+" )) == NULL )
84+ {
85+ termin = stdin ;
86+ termout = stderr ;
87+ }
88+ }
89+
7590 if (prompt )
76- fputs (gettext (prompt ), stderr );
91+ {
92+ fputs (gettext (prompt ), termout );
93+ rewind (termout ); /* does flush too */
94+ }
7795
7896#ifdef HAVE_TERMIOS_H
7997 if (!echo )
8098 {
81- tcgetattr (0 , & t );
99+ tcgetattr (fileno ( termin ) , & t );
82100 t_orig = t ;
83101 t .c_lflag &= ~ECHO ;
84- tcsetattr (0 , TCSADRAIN , & t );
102+ tcsetattr (fileno ( termin ) , TCSADRAIN , & t );
85103 }
86104#endif
87105
88- if (fgets (destination , maxlen , stdin ) == NULL )
106+ if (fgets (destination , maxlen , termin ) == NULL )
89107 destination [0 ] = '\0' ;
90108
91109#ifdef HAVE_TERMIOS_H
92110 if (!echo )
93111 {
94- tcsetattr (0 , TCSADRAIN , & t_orig );
95- fputs ("\n" , stderr );
112+ tcsetattr (fileno ( termin ) , TCSADRAIN , & t_orig );
113+ fputs ("\n" , termout );
96114 }
97115#endif
98116
117+ prompt_state = false;
118+
99119 length = strlen (destination );
100120 if (length > 0 && destination [length - 1 ] != '\n' )
101121 {
@@ -105,11 +125,12 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
105125
106126 do
107127 {
108- if (fgets (buf , sizeof (buf ), stdin ) == NULL )
128+ if (fgets (buf , sizeof (buf ), termin ) == NULL )
109129 break ;
110130 buflen = strlen (buf );
111131 } while (buflen > 0 && buf [buflen - 1 ] != '\n' );
112132 }
133+
113134 if (length > 0 && destination [length - 1 ] == '\n' )
114135 /* remove trailing newline */
115136 destination [length - 1 ] = '\0' ;
@@ -118,6 +139,7 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
118139}
119140
120141
142+
121143static int
122144_parse_version (ArchiveHandle * AH , const char * versionString )
123145{
0 commit comments