Move simple_prompt() into its own file to be shared with psql and pg_dump.
This commit is contained in:
parent
d66f172f4b
commit
7015111a19
@ -5,7 +5,7 @@
|
|||||||
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
# Portions Copyright (c) 1994, Regents of the University of California
|
# 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 = ../../..
|
top_builddir = ../../..
|
||||||
include $(top_builddir)/src/Makefile.global
|
include $(top_builddir)/src/Makefile.global
|
||||||
|
|
||||||
OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o pg_backup_files.o \
|
OBJS= pg_backup_archiver.o pg_backup_db.o pg_backup_custom.o \
|
||||||
pg_backup_null.o pg_backup_tar.o
|
pg_backup_files.o pg_backup_null.o pg_backup_tar.o sprompt.o
|
||||||
|
|
||||||
ifdef STRDUP
|
ifdef STRDUP
|
||||||
OBJS+=$(top_builddir)/src/utils/strdup.o
|
OBJS+=$(top_builddir)/src/utils/strdup.o
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
* Implements the basic DB functions used by the archiver.
|
* Implements the basic DB functions used by the archiver.
|
||||||
*
|
*
|
||||||
* IDENTIFICATION
|
* 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);
|
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
|
static int
|
||||||
_parse_version(ArchiveHandle *AH, const char *versionString)
|
_parse_version(ArchiveHandle *AH, const char *versionString)
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
* Portions Copyright (c) 1994, Regents of the University of California
|
* 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);
|
const bool schemaOnly, const bool dataOnly);
|
||||||
extern void dumpIndexes(Archive *fout, TableInfo *tbinfo, int numTables);
|
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 */
|
#endif /* PG_DUMP_H */
|
||||||
|
121
src/bin/pg_dump/sprompt.c
Normal file
121
src/bin/pg_dump/sprompt.c
Normal file
@ -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;
|
||||||
|
}
|
@ -5,7 +5,7 @@
|
|||||||
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
# Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
|
||||||
# Portions Copyright (c) 1994, Regents of the University of California
|
# 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 \
|
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 \
|
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
|
all: submake psql
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright 2000 by PostgreSQL Global Development Group
|
* 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"
|
#include "postgres_fe.h"
|
||||||
|
|
||||||
@ -12,9 +12,6 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#ifdef HAVE_TERMIOS_H
|
|
||||||
#include <termios.h>
|
|
||||||
#endif
|
|
||||||
#ifndef HAVE_STRDUP
|
#ifndef HAVE_STRDUP
|
||||||
#include <strdup.h>
|
#include <strdup.h>
|
||||||
#endif
|
#endif
|
||||||
@ -37,6 +34,7 @@
|
|||||||
#include "print.h"
|
#include "print.h"
|
||||||
#include "mainloop.h"
|
#include "mainloop.h"
|
||||||
|
|
||||||
|
extern bool prompt_state;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* "Safe" wrapper around strdup()
|
* "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
|
* 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
|
* so. We use write() to print to stdout because it's better to use simple
|
||||||
* facilities in a signal handler.
|
* facilities in a signal handler.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
PGconn *cancelConn;
|
PGconn *cancelConn;
|
||||||
volatile bool cancel_pressed;
|
volatile bool cancel_pressed;
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
*
|
*
|
||||||
* Copyright 2000 by PostgreSQL Global Development Group
|
* 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
|
#ifndef COMMON_H
|
||||||
#define COMMON_H
|
#define COMMON_H
|
||||||
@ -37,4 +37,7 @@ extern PGresult *PSQLexec(const char *query);
|
|||||||
|
|
||||||
extern bool SendQuery(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 */
|
#endif /* COMMON_H */
|
||||||
|
121
src/bin/psql/sprompt.c
Normal file
121
src/bin/psql/sprompt.c
Normal file
@ -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;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user