From: sof Date: Thu, 18 Jul 2002 22:01:07 +0000 (+0000) Subject: [project @ 2002-07-18 22:01:07 by sof] X-Git-Tag: nhc98-1-18-release~939 X-Git-Url: http://git.megacz.com/?a=commitdiff_plain;h=2f71a076d6210da9fe91641a8bf9787d13c58ee5;p=ghc-base.git [project @ 2002-07-18 22:01:07 by sof] helper functions for mucking about with Win32 consoles --- diff --git a/cbits/Makefile b/cbits/Makefile index 5eda815..c110e30 100644 --- a/cbits/Makefile +++ b/cbits/Makefile @@ -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 index 0000000..fd51440 --- /dev/null +++ b/cbits/consUtils.c @@ -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 +#include + +#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 index 0000000..e6a04e8 --- /dev/null +++ b/include/consUtils.h @@ -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