[project @ 2002-07-18 22:01:07 by sof]
authorsof <unknown>
Thu, 18 Jul 2002 22:01:07 +0000 (22:01 +0000)
committersof <unknown>
Thu, 18 Jul 2002 22:01:07 +0000 (22:01 +0000)
helper functions for mucking about with Win32 consoles

cbits/Makefile
cbits/consUtils.c [new file with mode: 0644]
include/consUtils.h [new file with mode: 0644]

index 5eda815..c110e30 100644 (file)
@@ -1,4 +1,4 @@
-# $Id: Makefile,v 1.7 2002/02/14 15:14:02 simonmar Exp $
+# $Id: Makefile,v 1.8 2002/07/18 22:01:07 sof Exp $
 
 TOP = ../..
 include $(TOP)/mk/boilerplate.mk
@@ -13,6 +13,10 @@ endif
 
 EXCLUDED_SRCS += ilxstubs.c
 
+ifneq "$(TARGETPLATFORM)" "i386-unknown-mingw32"
+EXCLUDED_SRCS += consUtils.c
+endif
+
 SRC_CC_OPTS += -Wall -DCOMPILING_STDLIB 
 SRC_CC_OPTS += -I$(GHC_INCLUDE_DIR) -I$(GHC_RUNTIME_DIR) -I../include
 
diff --git a/cbits/consUtils.c b/cbits/consUtils.c
new file mode 100644 (file)
index 0000000..fd51440
--- /dev/null
@@ -0,0 +1,67 @@
+/* 
+ * (c) The University of Glasgow 2002
+ *
+ * Win32 Console API support
+ */
+#include "config.h"
+#if defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS)
+/* to the end */
+
+#include "consUtils.h"
+#include <windows.h>
+#include <io.h>
+
+#if defined(cygwin32_TARGET_OS)
+#define _get_osfhandle get_osfhandle
+#endif
+
+int
+set_console_buffering__(int fd, int cooked)
+{
+    HANDLE h;
+    DWORD  st;
+    /* According to GetConsoleMode() docs, it is not possible to
+       leave ECHO_INPUT enabled without also having LINE_INPUT,
+       so we have to turn both off here. */
+    DWORD flgs = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
+    
+    if ( (h = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE ) {
+       if ( GetConsoleMode(h,&st) &&
+            SetConsoleMode(h, cooked ? (st | ENABLE_LINE_INPUT) : st & ~flgs)  ) {
+           return 0;
+       }
+    }
+    return -1;
+}
+
+int
+set_console_echo__(int fd, int on)
+{
+    HANDLE h;
+    DWORD  st;
+    DWORD flgs = ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT;
+    
+    if ( (h = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE ) {
+       if ( GetConsoleMode(h,&st) && 
+            SetConsoleMode(h,( on ? (st | flgs) : (st & ~ENABLE_ECHO_INPUT))) ) {
+           return 0;
+       }
+    }
+    return -1;
+}
+
+int
+get_console_echo__(int fd)
+{
+    HANDLE h;
+    DWORD  st;
+    
+    if ( (h = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE ) {
+       if ( GetConsoleMode(h,&st) ) {
+           return (st & ENABLE_ECHO_INPUT ? 1 : 0);
+       }
+    }
+    return -1;
+}
+
+#endif /* defined(mingw32_TARGET_OS) || defined(cygwin32_TARGET_OS) */
diff --git a/include/consUtils.h b/include/consUtils.h
new file mode 100644 (file)
index 0000000..e6a04e8
--- /dev/null
@@ -0,0 +1,11 @@
+/* 
+ * (c) The University of Glasgow, 2000-2002
+ *
+ * Win32 Console API helpers.
+ */
+#ifndef __CONSUTILS_H__
+#define __CONSUTILS_H__
+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);
+#endif