Core: ReadSecret (thanks Oblivion)

git-svn-id: svn://ultimatepp.org/upp/trunk@11238 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2017-07-15 09:05:42 +00:00
parent 5bdca6e31e
commit 28efdebbd1
3 changed files with 43 additions and 0 deletions

View file

@ -2,6 +2,7 @@
#ifdef PLATFORM_POSIX
#include <sys/mman.h>
#include <termios.h>
#endif
namespace Upp {
@ -1304,6 +1305,38 @@ String ReadStdIn()
}
}
String ReadSecret()
{
DisableEcho();
String s = ReadStdIn();
EnableEcho();
return s;
}
void EnableEcho(bool b)
{
#ifdef PLATFORM_POSIX
termios t;
tcgetattr(STDIN_FILENO, &t);
if(b) t.c_lflag |= ECHO;
else t.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &t);
#elif PLATFORM_WIN32
HANDLE h = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(h, &mode);
if(b) mode |= ENABLE_ECHO_INPUT;
else mode &= ~ENABLE_ECHO_INPUT;
SetConsoleMode(h, mode);
#endif
}
void DisableEcho()
{
EnableEcho(false);
}
// ---------------------------------------------------------------------------
String LoadStream(Stream& in) {

View file

@ -622,6 +622,9 @@ void CoutUTF8();
Stream& Cout();
Stream& Cerr();
String ReadStdIn();
String ReadSecret();
void EnableEcho(bool b = true);
void DisableEcho();
#endif
Stream& NilStream();

View file

@ -62,6 +62,13 @@ Data written to this stream are displayed as characters in console.&]
[s7;%% [*/ Return value]-|Console input.&]
[s3; &]
[s4; &]
[s5;:Upp`:`:ReadSecret`(`): [_^Upp`:`:String^ String]_[* ReadSecret]()&]
[s2;%% Reads one line of input data from the console without echoing.
This function is useful for reading passwords, secret phrases,
etc. from the console.&]
[s7;%% [*/ Return value]-|Console input.&]
[s3; &]
[s4; &]
[s5;:NilStream`(`): [_^Stream^ Stream][@(0.0.255) `&]_[* NilStream]()&]
[s2;%% Returns special stream that is always in IsEof state and simply
discards all data written to it.&]