Skip to content

Commit 7015111

Browse files
committed
Move simple_prompt() into its own file to be shared with psql and pg_dump.
1 parent d66f172 commit 7015111

File tree

8 files changed

+258
-230
lines changed

8 files changed

+258
-230
lines changed

src/bin/pg_dump/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
66
# Portions Copyright (c) 1994, Regents of the University of California
77
#
8-
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.33 2002/06/20 20:29:41 momjian Exp $
8+
# $Header: /cvsroot/pgsql/src/bin/pg_dump/Makefile,v 1.34 2002/07/06 20:12:30 momjian Exp $
99
#
1010
#-------------------------------------------------------------------------
1111

1212
subdir = src/bin/pg_dump
1313
top_builddir = ../../..
1414
include $(top_builddir)/src/Makefile.global
1515

16-
OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o pg_backup_files.o \
17-
pg_backup_null.o pg_backup_tar.o
16+
OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \
17+
pg_backup_files.o pg_backup_null.o pg_backup_tar.o sprompt.o
1818

1919
ifdef STRDUP
2020
OBJS+=$(top_builddir)/src/utils/strdup.o

src/bin/pg_dump/pg_backup_db.c

Lines changed: 1 addition & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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.34 2002/07/04 15:35:07 momjian Exp $
8+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/pg_backup_db.c,v 1.35 2002/07/06 20:12:30 momjian Exp $
99
*
1010
*-------------------------------------------------------------------------
1111
*/
@@ -37,114 +37,6 @@ static char *_sendSQLLine(ArchiveHandle *AH, char *qry, char *eos);
3737
static char *_sendCopyLine(ArchiveHandle *AH, char *qry, char *eos);
3838

3939

40-
/*
41-
* simple_prompt --- borrowed from psql
42-
*
43-
* Generalized function especially intended for reading in usernames and
44-
* password interactively. Reads from /dev/tty or stdin/stderr.
45-
*
46-
* prompt: The prompt to print
47-
* maxlen: How many characters to accept
48-
* echo: Set to false if you want to hide what is entered (for passwords)
49-
*
50-
* Returns a malloc()'ed string with the input (w/o trailing newline).
51-
*/
52-
static bool prompt_state = false;
53-
54-
char *
55-
simple_prompt(const char *prompt, int maxlen, bool echo)
56-
{
57-
int length;
58-
char *destination;
59-
FILE *termin,
60-
*termout;
61-
62-
#ifdef HAVE_TERMIOS_H
63-
struct termios t_orig,
64-
t;
65-
#endif
66-
67-
destination = (char *) malloc(maxlen + 2);
68-
if (!destination)
69-
return NULL;
70-
71-
prompt_state = true; /* disable SIGINT */
72-
73-
/*
74-
* Do not try to collapse these into one "w+" mode file. Doesn't work
75-
* on some platforms (eg, HPUX 10.20).
76-
*/
77-
termin = fopen("/dev/tty", "r");
78-
termout = fopen("/dev/tty", "w");
79-
if (!termin || !termout)
80-
{
81-
if (termin)
82-
fclose(termin);
83-
if (termout)
84-
fclose(termout);
85-
termin = stdin;
86-
termout = stderr;
87-
}
88-
89-
#ifdef HAVE_TERMIOS_H
90-
if (!echo)
91-
{
92-
tcgetattr(fileno(termin), &t);
93-
t_orig = t;
94-
t.c_lflag &= ~ECHO;
95-
tcsetattr(fileno(termin), TCSAFLUSH, &t);
96-
}
97-
#endif
98-
99-
if (prompt)
100-
{
101-
fputs(gettext(prompt), termout);
102-
fflush(termout);
103-
}
104-
105-
if (fgets(destination, maxlen, termin) == NULL)
106-
destination[0] = '\0';
107-
108-
length = strlen(destination);
109-
if (length > 0 && destination[length - 1] != '\n')
110-
{
111-
/* eat rest of the line */
112-
char buf[128];
113-
int buflen;
114-
115-
do
116-
{
117-
if (fgets(buf, sizeof(buf), termin) == NULL)
118-
break;
119-
buflen = strlen(buf);
120-
} while (buflen > 0 && buf[buflen - 1] != '\n');
121-
}
122-
123-
if (length > 0 && destination[length - 1] == '\n')
124-
/* remove trailing newline */
125-
destination[length - 1] = '\0';
126-
127-
#ifdef HAVE_TERMIOS_H
128-
if (!echo)
129-
{
130-
tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
131-
fputs("\n", termout);
132-
fflush(termout);
133-
}
134-
#endif
135-
136-
if (termin != stdin)
137-
{
138-
fclose(termin);
139-
fclose(termout);
140-
}
141-
142-
prompt_state = false; /* SIGINT okay again */
143-
144-
return destination;
145-
}
146-
147-
14840
static int
14941
_parse_version(ArchiveHandle *AH, const char *versionString)
15042
{

src/bin/pg_dump/pg_dump.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
9-
* $Id: pg_dump.h,v 1.89 2002/07/02 05:49:52 momjian Exp $
9+
* $Id: pg_dump.h,v 1.90 2002/07/06 20:12:30 momjian Exp $
1010
*
1111
*-------------------------------------------------------------------------
1212
*/
@@ -209,4 +209,7 @@ extern void dumpTables(Archive *fout, TableInfo tblinfo[], int numTables,
209209
const bool schemaOnly, const bool dataOnly);
210210
extern void dumpIndexes(Archive *fout, TableInfo *tbinfo, int numTables);
211211

212+
/* sprompt.h */
213+
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
214+
212215
#endif /* PG_DUMP_H */

src/bin/pg_dump/sprompt.c

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* psql - the PostgreSQL interactive terminal
3+
*
4+
* Copyright 2000 by PostgreSQL Global Development Group
5+
*
6+
* $Header: /cvsroot/pgsql/src/bin/pg_dump/Attic/sprompt.c,v 1.1 2002/07/06 20:12:30 momjian Exp $
7+
*/
8+
9+
/*
10+
* simple_prompt
11+
*
12+
* Generalized function especially intended for reading in usernames and
13+
* password interactively. Reads from /dev/tty or stdin/stderr.
14+
*
15+
* prompt: The prompt to print
16+
* maxlen: How many characters to accept
17+
* echo: Set to false if you want to hide what is entered (for passwords)
18+
*
19+
* Returns a malloc()'ed string with the input (w/o trailing newline).
20+
*/
21+
#include "postgres_fe.h"
22+
23+
#ifdef HAVE_TERMIOS_H
24+
#include <termios.h>
25+
#endif
26+
27+
bool prompt_state = false;
28+
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
29+
30+
char *
31+
simple_prompt(const char *prompt, int maxlen, bool echo)
32+
{
33+
int length;
34+
char *destination;
35+
FILE *termin,
36+
*termout;
37+
38+
#ifdef HAVE_TERMIOS_H
39+
struct termios t_orig,
40+
t;
41+
#endif
42+
43+
destination = (char *) malloc(maxlen + 2);
44+
if (!destination)
45+
return NULL;
46+
47+
prompt_state = true; /* disable SIGINT */
48+
49+
/*
50+
* Do not try to collapse these into one "w+" mode file. Doesn't work
51+
* on some platforms (eg, HPUX 10.20).
52+
*/
53+
termin = fopen("/dev/tty", "r");
54+
termout = fopen("/dev/tty", "w");
55+
if (!termin || !termout)
56+
{
57+
if (termin)
58+
fclose(termin);
59+
if (termout)
60+
fclose(termout);
61+
termin = stdin;
62+
termout = stderr;
63+
}
64+
65+
#ifdef HAVE_TERMIOS_H
66+
if (!echo)
67+
{
68+
tcgetattr(fileno(termin), &t);
69+
t_orig = t;
70+
t.c_lflag &= ~ECHO;
71+
tcsetattr(fileno(termin), TCSAFLUSH, &t);
72+
}
73+
#endif
74+
75+
if (prompt)
76+
{
77+
fputs(gettext(prompt), termout);
78+
fflush(termout);
79+
}
80+
81+
if (fgets(destination, maxlen, termin) == NULL)
82+
destination[0] = '\0';
83+
84+
length = strlen(destination);
85+
if (length > 0 && destination[length - 1] != '\n')
86+
{
87+
/* eat rest of the line */
88+
char buf[128];
89+
int buflen;
90+
91+
do
92+
{
93+
if (fgets(buf, sizeof(buf), termin) == NULL)
94+
break;
95+
buflen = strlen(buf);
96+
} while (buflen > 0 && buf[buflen - 1] != '\n');
97+
}
98+
99+
if (length > 0 && destination[length - 1] == '\n')
100+
/* remove trailing newline */
101+
destination[length - 1] = '\0';
102+
103+
#ifdef HAVE_TERMIOS_H
104+
if (!echo)
105+
{
106+
tcsetattr(fileno(termin), TCSAFLUSH, &t_orig);
107+
fputs("\n", termout);
108+
fflush(termout);
109+
}
110+
#endif
111+
112+
if (termin != stdin)
113+
{
114+
fclose(termin);
115+
fclose(termout);
116+
}
117+
118+
prompt_state = false; /* SIGINT okay again */
119+
120+
return destination;
121+
}

src/bin/psql/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
66
# Portions Copyright (c) 1994, Regents of the University of California
77
#
8-
# $Header: /cvsroot/pgsql/src/bin/psql/Makefile,v 1.32 2002/06/20 20:29:42 momjian Exp $
8+
# $Header: /cvsroot/pgsql/src/bin/psql/Makefile,v 1.33 2002/07/06 20:12:30 momjian Exp $
99
#
1010
#-------------------------------------------------------------------------
1111

@@ -19,7 +19,7 @@ override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
1919

2020
OBJS=command.o common.o help.o input.o stringutils.o mainloop.o \
2121
copy.o startup.o prompt.o variables.o large_obj.o print.o describe.o \
22-
tab-complete.o mbprint.o
22+
sprompt.o tab-complete.o mbprint.o
2323

2424
all: submake psql
2525

0 commit comments

Comments
 (0)