diff --git a/uppsrc/Core/Stream.cpp b/uppsrc/Core/Stream.cpp index 6b36acdcc..41d9c1ab4 100644 --- a/uppsrc/Core/Stream.cpp +++ b/uppsrc/Core/Stream.cpp @@ -2,6 +2,7 @@ #ifdef PLATFORM_POSIX #include +#include #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) { diff --git a/uppsrc/Core/Stream.h b/uppsrc/Core/Stream.h index 4c3d813ad..a0c325b25 100644 --- a/uppsrc/Core/Stream.h +++ b/uppsrc/Core/Stream.h @@ -622,6 +622,9 @@ void CoutUTF8(); Stream& Cout(); Stream& Cerr(); String ReadStdIn(); +String ReadSecret(); +void EnableEcho(bool b = true); +void DisableEcho(); #endif Stream& NilStream(); diff --git a/uppsrc/Core/src.tpp/StreamFn$en-us.tpp b/uppsrc/Core/src.tpp/StreamFn$en-us.tpp index f1ef51a13..17aff8433 100644 --- a/uppsrc/Core/src.tpp/StreamFn$en-us.tpp +++ b/uppsrc/Core/src.tpp/StreamFn$en-us.tpp @@ -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.&]