Commit e7f7ce78 authored by Edward Z. Yang's avatar Edward Z. Yang
Browse files

More accurate isatty test for MinGW.

parent 3e3cfe97
......@@ -374,7 +374,12 @@ foreign import ccall safe "fdReady"
-- Terminal-related stuff
isTerminal :: FD -> IO Bool
isTerminal fd = c_isatty (fdFD fd) >>= return.toBool
isTerminal fd =
#if defined(mingw32_HOST_OS)
is_console (fdFD fd) >>= return.toBool
#else
c_isatty (fdFD fd) >>= return.toBool
#endif
setEcho :: FD -> Bool -> IO ()
setEcho fd on = System.Posix.Internals.setEcho (fdFD fd) on
......
......@@ -311,6 +311,9 @@ foreign import ccall unsafe "consUtils.h set_console_echo__"
foreign import ccall unsafe "consUtils.h get_console_echo__"
get_console_echo :: CInt -> IO CInt
foreign import ccall unsafe "consUtils.h is_console__"
is_console :: CInt -> IO CInt
#endif
-- ---------------------------------------------------------------------------
......
......@@ -14,6 +14,29 @@
#define _get_osfhandle get_osfhandle
#endif
int is_console__(int fd) {
DWORD st;
HANDLE h;
if (!_isatty(fd)) {
/* TTY must be a character device */
return 0;
}
h = get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
/* Broken handle can't be terminal */
return 0;
}
if (GetConsoleMode(h, &st) == INVALID_HANDLE_VALUE) {
/* GetConsoleMode appears to fail when it's not a TTY. In
particular, it's what most of our terminal functions
assume works, so if it doesn't work for all intents
and purposes we're not dealing with a terminal. */
return 0;
}
return 1;
}
int
set_console_buffering__(int fd, int cooked)
{
......
......@@ -5,6 +5,7 @@
*/
#ifndef __CONSUTILS_H__
#define __CONSUTILS_H__
extern int is_console__(int fd);
extern int set_console_buffering__(int fd, int cooked);
extern int set_console_echo__(int fd, int on);
extern int get_console_echo__(int fd);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment