dash/src/compat/stdin.cpp
Wladimir J. van der Laan d29f7cc99a
Merge #17084: build: Fix #include sys/poll.h to just poll.h (without sys/)
4de0bde7bc546638cbbbb8bc4eb0d840d81cb953 build: Fix #include sys/poll.h to just poll.h (without sys/) (Daki Carnhof)

Pull request description:

  http://pubs.opengroup.org/onlinepubs/009695399/functions/poll.html
  http://man7.org/linux/man-pages/man2/poll.2.html

ACKs for top commit:
  Empact:
    ACK 4de0bde7bc546638cbbbb8bc4eb0d840d81cb953

Tree-SHA512: 01c22a62b5bc327b3a46f721312af2283f4e09cb314bc7a3f82dbc1f4eda6f018a394559c4370fee895532e768b8a9152783979d61cb8a0ed86de069f7c150fb
2022-04-02 16:30:54 +05:30

73 lines
1.6 KiB
C++

// 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 <config/dash-config.h>
#endif
#include <cstdio> // for fileno(), stdin
#ifdef WIN32
#include <windows.h> // for SetStdinEcho()
#include <io.h> // for isatty()
#else
#include <termios.h> // for SetStdinEcho()
#include <unistd.h> // for SetStdinEcho(), isatty()
#include <poll.h> // for StdinReady()
#endif
#include <compat/stdin.h>
// 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); }