Disable echo on Windows

This commit is contained in:
Frank Denis 2015-06-08 00:35:41 +02:00
parent a97739eb35
commit 46448bdb07

View file

@ -14,6 +14,8 @@
# include <poll.h>
# include <termios.h>
# include <unistd.h>
#elif defined(_WIN32)
# include <windows.h>
#endif
#include "get_line.h"
@ -28,34 +30,56 @@
static void
disable_echo(void)
{
# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
struct termios p;
fpurge(stdin);
fflush(stdout);
fflush(stderr);
if (!isatty(0) || tcgetattr(0, &p) != 0) {
return;
# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
{
struct termios p;
if (!isatty(0) || tcgetattr(0, &p) != 0) {
return;
}
p.c_lflag &= ~ECHO;
tcsetattr(0, TCSAFLUSH, &p);
}
# elif defined(_WIN32)
{
HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(handle, &mode);
SetConsoleMode(handle, mode & ~ENABLE_ECHO_INPUT);
}
p.c_lflag &= ~ECHO;
tcsetattr(0, TCSAFLUSH, &p);
# endif
}
static void
enable_echo(void)
{
# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
struct termios p;
fpurge(stdin);
fflush(stdout);
fflush(stderr);
if (!isatty(0) || tcgetattr(0, &p) != 0) {
return;
# if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
{
struct termios p;
if (!isatty(0) || tcgetattr(0, &p) != 0) {
return;
}
p.c_lflag |= ECHO;
tcsetattr(0, TCSAFLUSH, &p);
}
# elif defined(_WIN32)
{
HANDLE handle = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(handle, &mode);
SetConsoleMode(handle, mode | ENABLE_ECHO_INPUT);
}
p.c_lflag |= ECHO;
tcsetattr(0, TCSAFLUSH, &p);
# endif
}