2 * (c) The University of Glasgow 2002
4 * Win32 Console API support
6 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32) || defined(__CYGWIN__)
13 #if defined(__CYGWIN__)
14 #define _get_osfhandle get_osfhandle
17 int is_console__(int fd) {
21 /* TTY must be a character device */
24 h = (HANDLE)_get_osfhandle(fd);
25 if (h == INVALID_HANDLE_VALUE) {
26 /* Broken handle can't be terminal */
29 if (!GetConsoleMode(h, &st)) {
30 /* GetConsoleMode appears to fail when it's not a TTY. In
31 particular, it's what most of our terminal functions
32 assume works, so if it doesn't work for all intents
33 and purposes we're not dealing with a terminal. */
41 set_console_buffering__(int fd, int cooked)
45 /* According to GetConsoleMode() docs, it is not possible to
46 leave ECHO_INPUT enabled without also having LINE_INPUT,
47 so we have to turn both off here. */
48 DWORD flgs = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
50 if ( (h = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE ) {
51 if ( GetConsoleMode(h,&st) &&
52 SetConsoleMode(h, cooked ? (st | ENABLE_LINE_INPUT) : st & ~flgs) ) {
60 set_console_echo__(int fd, int on)
64 DWORD flgs = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
66 if ( (h = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE ) {
67 if ( GetConsoleMode(h,&st) &&
68 SetConsoleMode(h,( on ? (st | flgs) : (st & ~ENABLE_ECHO_INPUT))) ) {
76 get_console_echo__(int fd)
81 if ( (h = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE ) {
82 if ( GetConsoleMode(h,&st) ) {
83 return (st & ENABLE_ECHO_INPUT ? 1 : 0);
90 flush_input_console__(int fd)
92 HANDLE h = (HANDLE)_get_osfhandle(fd);
94 if ( h != INVALID_HANDLE_VALUE ) {
95 /* If the 'fd' isn't connected to a console; treat the flush
99 if ( !GetConsoleMode(h,&unused) &&
100 GetLastError() == ERROR_INVALID_HANDLE ) {
103 if ( FlushConsoleInputBuffer(h) ) {
107 /* ToDo: translate GetLastError() into something errno-friendly */
111 #endif /* defined(__MINGW32__) || ... */