Branch data Line data Source code
1 : : // Copyright (c) 2018-present 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 : : #include <compat/stdin.h>
6 : :
7 : : #include <cstdio>
8 : :
9 : : #ifdef WIN32
10 : : #include <windows.h>
11 : : #include <io.h>
12 : : #else
13 : : #include <termios.h>
14 : : #include <unistd.h>
15 : : #include <poll.h>
16 : : #endif
17 : :
18 : : // https://stackoverflow.com/questions/1413445/reading-a-password-from-stdcin
19 : 0 : void SetStdinEcho(bool enable)
20 : : {
21 [ # # ]: 0 : if (!StdinTerminal()) {
22 : : return;
23 : : }
24 : : #ifdef WIN32
25 : : HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
26 : : DWORD mode;
27 : : if (!GetConsoleMode(hStdin, &mode)) {
28 : : fputs("GetConsoleMode failed\n", stderr);
29 : : return;
30 : : }
31 : : if (!enable) {
32 : : mode &= ~ENABLE_ECHO_INPUT;
33 : : } else {
34 : : mode |= ENABLE_ECHO_INPUT;
35 : : }
36 : : if (!SetConsoleMode(hStdin, mode)) {
37 : : fputs("SetConsoleMode failed\n", stderr);
38 : : }
39 : : #else
40 : 0 : struct termios tty;
41 [ # # ]: 0 : if (tcgetattr(STDIN_FILENO, &tty) != 0) {
42 : 0 : fputs("tcgetattr failed\n", stderr);
43 : 0 : return;
44 : : }
45 [ # # ]: 0 : if (!enable) {
46 : 0 : tty.c_lflag &= static_cast<decltype(tty.c_lflag)>(~ECHO);
47 : : } else {
48 : 0 : tty.c_lflag |= ECHO;
49 : : }
50 [ # # ]: 0 : if (tcsetattr(STDIN_FILENO, TCSANOW, &tty) != 0) {
51 : 0 : fputs("tcsetattr failed\n", stderr);
52 : : }
53 : : #endif
54 : : }
55 : :
56 : 0 : bool StdinTerminal()
57 : : {
58 : : #ifdef WIN32
59 : : return _isatty(_fileno(stdin));
60 : : #else
61 : 0 : return isatty(fileno(stdin));
62 : : #endif
63 : : }
64 : :
65 : 0 : bool StdinReady()
66 : : {
67 [ # # ]: 0 : if (!StdinTerminal()) {
68 : : return true;
69 : : }
70 : : #ifdef WIN32
71 : : return false;
72 : : #else
73 : 0 : struct pollfd fds;
74 : 0 : fds.fd = STDIN_FILENO;
75 : 0 : fds.events = POLLIN;
76 : 0 : return poll(&fds, 1, 0) == 1;
77 : : #endif
78 : : }
79 : :
80 : 0 : NoechoInst::NoechoInst() { SetStdinEcho(false); }
81 : 0 : NoechoInst::~NoechoInst() { SetStdinEcho(true); }
|