[project @ 2002-09-06 14:35:42 by simonmar]
[ghc-hetmet.git] / ghc / compiler / ghci / InteractiveUI.hs
index 62c89a7..14208e1 100644 (file)
@@ -1,6 +1,6 @@
 {-# OPTIONS -#include "Linker.h" -#include "SchedAPI.h" #-}
 -----------------------------------------------------------------------------
--- $Id: InteractiveUI.hs,v 1.128 2002/07/02 16:27:38 simonmar Exp $
+-- $Id: InteractiveUI.hs,v 1.133 2002/09/06 14:35:44 simonmar Exp $
 --
 -- GHC Interactive User Interface
 --
@@ -38,7 +38,6 @@ import TyCon          ( tyConName, tyConClass_maybe, isPrimTyCon, DataConDetails(..) )
 import FieldLabel      ( fieldLabelTyCon )
 import SrcLoc          ( isGoodSrcLoc )
 import Module          ( moduleName )
-import NameEnv         ( nameEnvElts )
 import Name            ( Name, isHomePackageName, nameSrcLoc, nameOccName,
                          NamedThing(..) )
 import OccName         ( isSymOcc )
@@ -50,31 +49,35 @@ import Panic                ( GhcException(..), showGhcException )
 import Config
 
 #ifndef mingw32_TARGET_OS
-import Posix
+import System.Posix
 #endif
 
-import Exception
-import Dynamic
 #if HAVE_READLINE_HEADERS && HAVE_READLINE_LIBS
-import Readline 
+import System.Console.Readline as Readline
 #endif
-import Concurrent
-import IOExts
-import SystemExts
+
+--import SystemExts
+
+import Control.Exception as Exception
+import Data.Dynamic
+import Control.Concurrent
 
 import Numeric
-import List
-import System
-import CPUTime
-import Directory
-import IO
-import Char
-import Monad
+import Data.List
+import System.Cmd
+import System.CPUTime
+import System.Environment
+import System.Directory
+import System.IO as IO
+import Data.Char
+import Control.Monad as Monad
 
-import GlaExts         ( unsafeCoerce# )
+import GHC.Exts                ( unsafeCoerce# )
 
 import Foreign         ( nullPtr )
-import CString         ( CString, peekCString, withCString )
+import Foreign.C.String        ( CString, peekCString, withCString )
+import Data.IORef      ( IORef, newIORef, readIORef, writeIORef )
+
 
 -----------------------------------------------------------------------------
 
@@ -164,7 +167,10 @@ interactiveUI cmstate paths cmdline_libs = do
    (cmstate, maybe_hval) 
        <- cmCompileExpr cmstate dflags "IO.hSetBuffering IO.stdout IO.NoBuffering Prelude.>> IO.hSetBuffering IO.stderr IO.NoBuffering"
    case maybe_hval of
-       Just hval -> unsafeCoerce# hval :: IO ()
+       Just hval -> do
+               let action = unsafeCoerce# hval :: IO ()
+               action -- do it now
+               writeIORef turn_off_buffering action -- and save it for later
        _ -> panic "interactiveUI:buffering"
 
    (cmstate, maybe_hval)
@@ -243,8 +249,14 @@ runGHCi paths dflags = do
        loadModule (unwords paths)
 
   -- enter the interactive loop
+#if defined(mingw32_TARGET_OS)
+   -- always show prompt, since hIsTerminalDevice returns True for Consoles
+   -- only, which we may or may not be running under (cf. Emacs sub-shells.)
+  interactiveLoop True
+#else
   is_tty <- io (hIsTerminalDevice stdin)
   interactiveLoop is_tty
+#endif
 
   -- and finally, exit
   io $ do when (verbosity dflags > 0) $ putStrLn "Leaving GHCi."
@@ -262,7 +274,7 @@ interactiveLoop is_tty = do
        then readlineLoop
        else fileLoop stdin False  -- turn off prompt for non-TTY input
 #else
-  fileLoop stdin True
+  fileLoop stdin is_tty
 #endif
 
 
@@ -932,6 +944,7 @@ data GHCiOption
 
 GLOBAL_VAR(flush_stdout, error "no flush_stdout", IO ())
 GLOBAL_VAR(flush_stderr, error "no flush_stdout", IO ())
+GLOBAL_VAR(turn_off_buffering, error "no flush_stdout", IO ())
 
 newtype GHCi a = GHCi { unGHCi :: IORef GHCiState -> IO a }
 
@@ -1242,4 +1255,12 @@ maybePutStrLn dflags s | verbosity dflags > 0 = putStrLn s
 -----------------------------------------------------------------------------
 -- reverting CAFs
        
-foreign import revertCAFs :: IO ()     -- make it "safe", just in case
+revertCAFs :: IO ()
+revertCAFs = do
+  rts_revertCAFs
+  Monad.join (readIORef turn_off_buffering)
+       -- have to do this again, because we just reverted
+       -- stdout, stderr & stdin to their defaults.
+
+foreign import ccall "revertCAFs" rts_revertCAFs  :: IO ()  
+       -- make it "safe", just in case