Skip to content

Commit 0da503e

Browse files
committed
add stdin helpers for password input support
1 parent b0e268d commit 0da503e

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ endif
520520
libbitcoin_cli_a_CPPFLAGS = $(AM_CPPFLAGS) $(BITCOIN_INCLUDES)
521521
libbitcoin_cli_a_CXXFLAGS = $(AM_CXXFLAGS) $(PIE_FLAGS)
522522
libbitcoin_cli_a_SOURCES = \
523+
compat/stdin.h \
524+
compat/stdin.cpp \
523525
rpc/client.cpp \
524526
$(BITCOIN_CORE_H)
525527

src/compat/stdin.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#if defined(HAVE_CONFIG_H)
6+
#include <config/bitcoin-config.h>
7+
#endif
8+
9+
#include <cstdio> // for fileno(), stdin
10+
11+
#ifdef WIN32
12+
#include <windows.h> // for SetStdinEcho()
13+
#include <io.h> // for isatty()
14+
#else
15+
#include <termios.h> // for SetStdinEcho()
16+
#include <unistd.h> // for SetStdinEcho(), isatty()
17+
#include <sys/poll.h> // for StdinReady()
18+
#endif
19+
20+
#include <compat/stdin.h>
21+
22+
// https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
23+
void SetStdinEcho(bool enable)
24+
{
25+
#ifdef WIN32
26+
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
27+
DWORD mode;
28+
GetConsoleMode(hStdin, &mode);
29+
if (!enable) {
30+
mode &= ~ENABLE_ECHO_INPUT;
31+
} else {
32+
mode |= ENABLE_ECHO_INPUT;
33+
}
34+
SetConsoleMode(hStdin, mode);
35+
#else
36+
struct termios tty;
37+
tcgetattr(STDIN_FILENO, &tty);
38+
if (!enable) {
39+
tty.c_lflag &= ~ECHO;
40+
} else {
41+
tty.c_lflag |= ECHO;
42+
}
43+
(void)tcsetattr(STDIN_FILENO, TCSANOW, &tty);
44+
#endif
45+
}
46+
47+
bool StdinTerminal()
48+
{
49+
#ifdef WIN32
50+
return _isatty(_fileno(stdin));
51+
#else
52+
return isatty(fileno(stdin));
53+
#endif
54+
}
55+
56+
bool StdinReady()
57+
{
58+
if (!StdinTerminal()) {
59+
return true;
60+
}
61+
#ifdef WIN32
62+
return false;
63+
#else
64+
struct pollfd fds;
65+
fds.fd = 0; /* this is STDIN */
66+
fds.events = POLLIN;
67+
return poll(&fds, 1, 0) == 1;
68+
#endif
69+
}
70+
71+
NoechoInst::NoechoInst() { SetStdinEcho(false); }
72+
NoechoInst::~NoechoInst() { SetStdinEcho(true); }

src/compat/stdin.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) 2018 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_COMPAT_STDIN_H
6+
#define BITCOIN_COMPAT_STDIN_H
7+
8+
struct NoechoInst {
9+
NoechoInst();
10+
~NoechoInst();
11+
};
12+
13+
#define NO_STDIN_ECHO() NoechoInst _no_echo
14+
15+
bool StdinTerminal();
16+
bool StdinReady();
17+
18+
#endif // BITCOIN_COMPAT_STDIN_H

0 commit comments

Comments
 (0)