2003-11-12 00:52:45 +01:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* sprompt.c
|
|
|
|
* simple_prompt() routine
|
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2003-11-12 00:52:45 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2002-07-06 22:12:30 +02:00
|
|
|
*
|
2003-11-12 00:52:45 +01:00
|
|
|
* IDENTIFICATION
|
2010-01-02 17:58:17 +01:00
|
|
|
* $PostgreSQL: pgsql/src/port/sprompt.c,v 1.22 2010/01/02 16:58:13 momjian Exp $
|
2002-07-06 22:12:30 +02:00
|
|
|
*
|
2003-11-12 00:52:45 +01:00
|
|
|
*-------------------------------------------------------------------------
|
2002-07-06 22:12:30 +02:00
|
|
|
*/
|
|
|
|
|
2002-07-06 22:14:58 +02:00
|
|
|
|
2002-07-06 22:12:30 +02:00
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2004-06-24 20:53:48 +02:00
|
|
|
#include "c.h"
|
2002-07-06 22:12:30 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
#include <termios.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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;
|
2006-03-04 05:30:41 +01:00
|
|
|
FILE *termin,
|
|
|
|
*termout;
|
2002-07-06 22:12:30 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
struct termios t_orig,
|
|
|
|
t;
|
2003-07-27 05:32:26 +02:00
|
|
|
#else
|
|
|
|
#ifdef WIN32
|
2004-04-19 19:42:59 +02:00
|
|
|
HANDLE t = NULL;
|
2004-08-29 07:07:03 +02:00
|
|
|
LPDWORD t_orig = NULL;
|
2003-07-27 05:32:26 +02:00
|
|
|
#endif
|
2002-07-06 22:12:30 +02:00
|
|
|
#endif
|
|
|
|
|
2003-03-18 23:09:37 +01:00
|
|
|
destination = (char *) malloc(maxlen + 1);
|
2002-07-06 22:12:30 +02:00
|
|
|
if (!destination)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Do not try to collapse these into one "w+" mode file. Doesn't work on
|
|
|
|
* some platforms (eg, HPUX 10.20).
|
2002-07-06 22:12:30 +02:00
|
|
|
*/
|
2006-03-04 05:30:41 +01:00
|
|
|
termin = fopen(DEVTTY, "r");
|
|
|
|
termout = fopen(DEVTTY, "w");
|
2006-03-05 06:33:12 +01:00
|
|
|
if (!termin || !termout
|
|
|
|
#ifdef WIN32
|
2006-10-04 02:30:14 +02:00
|
|
|
/* See DEVTTY comment for msys */
|
2006-03-05 06:33:12 +01:00
|
|
|
|| (getenv("OSTYPE") && strcmp(getenv("OSTYPE"), "msys") == 0)
|
|
|
|
#endif
|
|
|
|
)
|
2002-07-06 22:12:30 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
2003-07-27 05:32:26 +02:00
|
|
|
#else
|
|
|
|
#ifdef WIN32
|
|
|
|
if (!echo)
|
|
|
|
{
|
|
|
|
/* get a new handle to turn echo off */
|
2003-08-04 02:43:34 +02:00
|
|
|
t_orig = (LPDWORD) malloc(sizeof(DWORD));
|
|
|
|
t = GetStdHandle(STD_INPUT_HANDLE);
|
2003-07-27 05:32:26 +02:00
|
|
|
|
|
|
|
/* save the old configuration first */
|
|
|
|
GetConsoleMode(t, t_orig);
|
|
|
|
|
|
|
|
/* set to the new mode */
|
2003-08-04 02:43:34 +02:00
|
|
|
SetConsoleMode(t, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT);
|
2003-07-27 05:32:26 +02:00
|
|
|
}
|
|
|
|
#endif
|
2002-07-06 22:12:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (prompt)
|
|
|
|
{
|
2005-02-22 05:43:23 +01:00
|
|
|
fputs(_(prompt), termout);
|
2002-07-06 22:12:30 +02:00
|
|
|
fflush(termout);
|
|
|
|
}
|
|
|
|
|
2003-03-18 23:09:37 +01:00
|
|
|
if (fgets(destination, maxlen + 1, termin) == NULL)
|
2002-07-06 22:12:30 +02:00
|
|
|
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);
|
|
|
|
}
|
2003-07-27 05:32:26 +02:00
|
|
|
#else
|
|
|
|
#ifdef WIN32
|
|
|
|
if (!echo)
|
|
|
|
{
|
|
|
|
/* reset to the original console mode */
|
|
|
|
SetConsoleMode(t, *t_orig);
|
|
|
|
fputs("\n", termout);
|
|
|
|
fflush(termout);
|
|
|
|
free(t_orig);
|
|
|
|
}
|
|
|
|
#endif
|
2002-07-06 22:12:30 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if (termin != stdin)
|
|
|
|
{
|
|
|
|
fclose(termin);
|
|
|
|
fclose(termout);
|
|
|
|
}
|
|
|
|
|
|
|
|
return destination;
|
|
|
|
}
|