From 0da503e9475fea5644168544668581796bf66334 Mon Sep 17 00:00:00 2001 From: Karl-Johan Alm Date: Thu, 19 Jul 2018 23:48:38 +0900 Subject: [PATCH] add stdin helpers for password input support --- src/Makefile.am | 2 ++ src/compat/stdin.cpp | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/compat/stdin.h | 18 +++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/compat/stdin.cpp create mode 100644 src/compat/stdin.h diff --git a/src/Makefile.am b/src/Makefile.am index 1ef62a656..d25f7ebe8 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -520,6 +520,8 @@ endif libbitcoin_cli_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES) libbitcoin_cli_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS) libbitcoin_cli_a_SOURCES = \ + compat/stdin.h \ + compat/stdin.cpp \ rpc/client.cpp \ $(BITCOIN_CORE_H) diff --git a/src/compat/stdin.cpp b/src/compat/stdin.cpp new file mode 100644 index 000000000..4f2ba1e9c --- /dev/null +++ b/src/compat/stdin.cpp @@ -0,0 +1,72 @@ +// Copyright (c) 2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#if defined(HAVE_CONFIG_H) +#include +#endif + +#include // for fileno(), stdin + +#ifdef WIN32 +#include // for SetStdinEcho() +#include // for isatty() +#else +#include // for SetStdinEcho() +#include // for SetStdinEcho(), isatty() +#include // for StdinReady() +#endif + +#include + +// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin +void SetStdinEcho(bool enable) +{ +#ifdef 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 + struct termios tty; + tcgetattr(STDIN_FILENO, &tty); + if (!enable) { + tty.c_lflag &= ~ECHO; + } else { + tty.c_lflag |= ECHO; + } + (void)tcsetattr(STDIN_FILENO, TCSANOW, &tty); +#endif +} + +bool StdinTerminal() +{ +#ifdef WIN32 + return _isatty(_fileno(stdin)); +#else + return isatty(fileno(stdin)); +#endif +} + +bool StdinReady() +{ + if (!StdinTerminal()) { + return true; + } +#ifdef WIN32 + return false; +#else + struct pollfd fds; + fds.fd = 0; /* this is STDIN */ + fds.events = POLLIN; + return poll(&fds, 1, 0) == 1; +#endif +} + +NoechoInst::NoechoInst() { SetStdinEcho(false); } +NoechoInst::~NoechoInst() { SetStdinEcho(true); } diff --git a/src/compat/stdin.h b/src/compat/stdin.h new file mode 100644 index 000000000..468fe4d6a --- /dev/null +++ b/src/compat/stdin.h @@ -0,0 +1,18 @@ +// Copyright (c) 2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef BITCOIN_COMPAT_STDIN_H +#define BITCOIN_COMPAT_STDIN_H + +struct NoechoInst { + NoechoInst(); + ~NoechoInst(); +}; + +#define NO_STDIN_ECHO() NoechoInst _no_echo + +bool StdinTerminal(); +bool StdinReady(); + +#endif // BITCOIN_COMPAT_STDIN_H