-- 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
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
-- ---------------------------------------------------------------------------
#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)
{
*/
#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);