5
5
* Implements the basic DB functions used by the archiver.
6
6
*
7
7
* 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 $
9
9
*
10
10
* NOTES
11
11
*
@@ -49,53 +49,73 @@ static void notice_processor(void *arg, const char *message);
49
49
* simple_prompt
50
50
*
51
51
* 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 .
53
53
*
54
54
* prompt: The prompt to print
55
55
* maxlen: How many characters to accept
56
56
* echo: Set to false if you want to hide what is entered (for passwords)
57
57
*
58
58
* Returns a malloc()'ed string with the input (w/o trailing newline).
59
59
*/
60
+ static bool prompt_state ;
61
+
60
62
char *
61
63
simple_prompt (const char * prompt , int maxlen , bool echo )
62
64
{
63
65
int length ;
64
66
char * destination ;
67
+ static FILE * termin = NULL , * termout ;
65
68
66
69
#ifdef HAVE_TERMIOS_H
67
70
struct termios t_orig ,
68
71
t ;
69
-
70
72
#endif
71
73
72
74
destination = (char * ) malloc (maxlen + 2 );
73
75
if (!destination )
74
76
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
+
75
90
if (prompt )
76
- fputs (gettext (prompt ), stderr );
91
+ {
92
+ fputs (gettext (prompt ), termout );
93
+ rewind (termout ); /* does flush too */
94
+ }
77
95
78
96
#ifdef HAVE_TERMIOS_H
79
97
if (!echo )
80
98
{
81
- tcgetattr (0 , & t );
99
+ tcgetattr (fileno ( termin ) , & t );
82
100
t_orig = t ;
83
101
t .c_lflag &= ~ECHO ;
84
- tcsetattr (0 , TCSADRAIN , & t );
102
+ tcsetattr (fileno ( termin ) , TCSADRAIN , & t );
85
103
}
86
104
#endif
87
105
88
- if (fgets (destination , maxlen , stdin ) == NULL )
106
+ if (fgets (destination , maxlen , termin ) == NULL )
89
107
destination [0 ] = '\0' ;
90
108
91
109
#ifdef HAVE_TERMIOS_H
92
110
if (!echo )
93
111
{
94
- tcsetattr (0 , TCSADRAIN , & t_orig );
95
- fputs ("\n" , stderr );
112
+ tcsetattr (fileno ( termin ) , TCSADRAIN , & t_orig );
113
+ fputs ("\n" , termout );
96
114
}
97
115
#endif
98
116
117
+ prompt_state = false;
118
+
99
119
length = strlen (destination );
100
120
if (length > 0 && destination [length - 1 ] != '\n' )
101
121
{
@@ -105,11 +125,12 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
105
125
106
126
do
107
127
{
108
- if (fgets (buf , sizeof (buf ), stdin ) == NULL )
128
+ if (fgets (buf , sizeof (buf ), termin ) == NULL )
109
129
break ;
110
130
buflen = strlen (buf );
111
131
} while (buflen > 0 && buf [buflen - 1 ] != '\n' );
112
132
}
133
+
113
134
if (length > 0 && destination [length - 1 ] == '\n' )
114
135
/* remove trailing newline */
115
136
destination [length - 1 ] = '\0' ;
@@ -118,6 +139,7 @@ simple_prompt(const char *prompt, int maxlen, bool echo)
118
139
}
119
140
120
141
142
+
121
143
static int
122
144
_parse_version (ArchiveHandle * AH , const char * versionString )
123
145
{
0 commit comments