Skip to content

Commit

Permalink
feat(cpp-client): utility function to read a password from stdin (dee…
Browse files Browse the repository at this point in the history
  • Loading branch information
jcferretti authored Jul 25, 2024
1 parent 215f3d7 commit 6b24b18
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ std::string Basename(std::string_view path);
*/
[[nodiscard]] std::optional<std::string> GetEnv(const std::string& envname);

/**
* Enables or disables echo for stdin.
* @param enable true to enable, false to disable
*/
void SetStdinEcho(bool enable);

/**
* Reads a password from stdin up to pressing 'Enter', without echoing the characters typed.
* @return the password read
*/
std::string ReadPasswordFromStdinNoEcho();

template <class T> [[nodiscard]] std::string
TypeName(const T& t) {
return demangle(typeid(t).name());
Expand Down
10 changes: 10 additions & 0 deletions cpp-client/deephaven/dhcore/src/utility/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static_assert(FMT_VERSION >= 100000);
namespace deephaven::dhcore::utility {

namespace {

const char kEncodeLookup[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const char kPadCharacter = '=';
} // namespace
Expand Down Expand Up @@ -167,4 +168,13 @@ std::string demangle(const char* name) {
std::string ObjectId(const std::string &class_short_name, void *this_ptr) {
return fmt::format("{}({})", class_short_name, this_ptr);
}

std::string ReadPasswordFromStdinNoEcho() {
SetStdinEcho(false);
std::string password;
std::cin >> password;
SetStdinEcho(true);
return password;
}

} // namespace deephaven::dhcore::utility
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#if defined(__unix__)
#include <netdb.h>
#include <termios.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/socket.h>
Expand All @@ -17,6 +18,7 @@
#endif

namespace deephaven::dhcore::utility {

std::string GetTidAsString() {
#if defined(__linux__)
const pid_t tid = syscall(__NR_gettid); // this is more portable than gettid().
Expand Down Expand Up @@ -89,4 +91,32 @@ std::optional<std::string> GetEnv(const std::string& envname) {
#error "Unsupported configuration"
#endif
}

// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
void SetStdinEcho(const bool enable) {
#if defined(__unix__)
struct termios tty;
tcgetattr(STDIN_FILENO, &tty);
if( !enable )
tty.c_lflag &= ~ECHO;
else
tty.c_lflag |= ECHO;

(void) tcsetattr(STDIN_FILENO, TCSANOW, &tty);
#elif defined(_WIN32)
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);

if( !enable )
mode &= ~ENABLE_ECHO_INPUT;
else
mode |= ENABLE_ECHO_INPUT;

SetConsoleMode(hStdin, mode );
#else
#error "Unsupported configuration"
#endif
}

} // namespace deephaven::dhcore::utility

0 comments on commit 6b24b18

Please sign in to comment.