ghc-6 messing with terminal settings?

Simon Marlow simonmar at microsoft.com
Fri Jul 2 05:58:43 EDT 2004


On 02 July 2004 10:13, Malcolm Wallace wrote:

> OK, here's a strange bug.  I have a program (hat-trail) that uses
> System.system("stty -icanon -echo") together with resetting the
> buffering of I/O, in order to get so-called character-break mode on
> the terminal.  Once the program is finished, it calls
> System.system("stty icanon echo") to restore the terminal to normal. 
> Under ghc-5.x and nhc98, this works just fine.  But under ghc-6.x,
> the terminal remains in character mode as if the final reset had not
> taken place. 
> 
> Having investigated somewhat (using strace to determine the order of
> system calls), it turns out that ghc-6 is very sensitive to the order
> in which operations are called.  Here is a cut-down demo program:
> 
>     import System; import Char; import IO
>     main = do system ("stty -icanon -echo")
>               hSetBuffering stdin NoBuffering
>               hSetBuffering stdout NoBuffering
>               getChars
>               system ("stty icanon echo")
>               putStrLn "Done"
>     getChars = do c <- getChar
>                   putChar (toUpper c)
>                   if c=='q' then return () else getChars
> 
> Running it leaves the terminal in char mode.  But if you swap the
> order of operations so that the call to stty comes *after* setting
> the buffering, the program then works correctly.
> 
> It would appear that ghc-6 is being careful to save the current state
> of the terminal at the point of hSetBuffering, and to restore it on
> exit, something ghc-5 did not do.  But paradoxically, this "safer"
> choice caused things to mess up, instead of getting better!

Your diagnosis is spot on.  

I suggest you just remove the 'stty' commands in that code (which don't
work on Windows, incedentally), and it will do what you want.  Better
even: GHC will save and restore the terminal settings, whereas your
program always sets the terminal to icanon/echo on exit.

Cheers,
	Simon



More information about the Glasgow-haskell-bugs mailing list