Unbuffered character IO under Windows XP?

module Main(main) where import System.IO main = do b1 <- hGetBuffering stdin print b1 b2 <- hGetBuffering stdout print b2 -- not sure if these help, or are needed hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering b1 <- hGetBuffering stdin print b1 b2 <- hGetBuffering stdout print b2 putStr "0" c <- getChar -- echoes during input by default putStr "1" -- want this output w/o hitting Enter hFlush stdout -- adding this does not help putStrLn [c] {------------------- Output: E:\ghcTest>ghc --version The Glorious Glasgow Haskell Compilation System, version 6.8.1 E:\ghcTest>ghc --make main [1 of 1] Compiling Main ( main.hs, main.o ) Linking main.exe ... E:\ghcTest>main LineBuffering LineBuffering NoBuffering NoBuffering 0a 1a E:\ghcTest> ----------------- Question: Is it possible to have unbuffered character IO under Windows XP? I would like to be able to type a single character and have the processing and IO continue without having to hit Enter. I.e., rather than this (had to hit Enter after typing the 'a' in '0a'): 0a 1a I would like to have this (without having to hit Enter after typing the char): 0a1a I have tried a few combinations of hSetBuffering and put/get Str/Char functions, without success. NOTE: I need to run under Windows XP, non-administrator account. This test was run using the normal XP shell cmd.exe. Thanks much in advance. -- Peter --------}

I have this problem as well and would love to hear if there is an answer.
I think there are some windows terminal settings that the GHC runtime should
twiddle when you change the buffering state on stdin, but I don't know
exactly what is involved.
-- ryan
On 12/28/07, Peter Schmitz
module Main(main) where import System.IO
main = do b1 <- hGetBuffering stdin print b1 b2 <- hGetBuffering stdout print b2
-- not sure if these help, or are needed hSetBuffering stdin NoBuffering hSetBuffering stdout NoBuffering
b1 <- hGetBuffering stdin print b1 b2 <- hGetBuffering stdout print b2
putStr "0" c <- getChar -- echoes during input by default putStr "1" -- want this output w/o hitting Enter hFlush stdout -- adding this does not help putStrLn [c]
{------------------- Output: E:\ghcTest>ghc --version The Glorious Glasgow Haskell Compilation System, version 6.8.1
E:\ghcTest>ghc --make main [1 of 1] Compiling Main ( main.hs, main.o ) Linking main.exe ...
E:\ghcTest>main LineBuffering LineBuffering NoBuffering NoBuffering 0a 1a
E:\ghcTest> ----------------- Question: Is it possible to have unbuffered character IO under Windows XP?
I would like to be able to type a single character and have the processing and IO continue without having to hit Enter.
I.e., rather than this (had to hit Enter after typing the 'a' in '0a'): 0a 1a
I would like to have this (without having to hit Enter after typing the char): 0a1a
I have tried a few combinations of hSetBuffering and put/get Str/Char functions, without success.
NOTE: I need to run under Windows XP, non-administrator account. This test was run using the normal XP shell cmd.exe. Thanks much in advance. -- Peter --------}
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

Hello Ryan, Saturday, December 29, 2007, 7:14:25 AM, you wrote:
Is it possible to have unbuffered character IO under Windows XP?
I have this problem as well and would love to hear if there is an answer.
once i had a hard day trying to figure out how to to the same in Unix. in Windows, it was easy for me :))) now my program contains: -- |Ask decryption password using hidden input ask_decryption_password = syncUI $ do withoutEcho $ do putStr "\n Enter decryption password:" hFlush stdout getHiddenLine -- |Input line not echoing it to the screen getHiddenLine = go "" where go s = do c <- getHiddenChar case c of '\r' -> do putStrLn ""; return s '\n' -> do putStrLn ""; return s c -> go (s++[c]) -- please note that FREEARC_WIN is specific to my program #ifdef FREEARC_WIN -- |Switch console into the hidden input mode withoutEcho = id -- |Input char without echoing it getHiddenChar = liftM (chr.fromEnum) c_getch foreign import ccall unsafe "conio.h getch" c_getch :: IO CChar #else -- |Input char without echoing it getHiddenChar = getChar -- |Switch console into the hidden input mode withoutEcho action = do let setAttr attr = setTerminalAttributes stdInput attr Immediately disableEcho = do origAttr <- getTerminalAttributes stdInput setAttr$ origAttr.$ flip withMode ProcessInput .$ flip withoutMode EnableEcho .$ flip withMode KeyboardInterrupts .$ flip withoutMode IgnoreBreak .$ flip withMode InterruptOnBreak return origAttr -- bracketCtrlBreak disableEcho setAttr (\_ -> action) #endif -- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com
participants (3)
-
Bulat Ziganshin
-
Peter Schmitz
-
Ryan Ingram