-- -fno-cse is needed for GLOBAL_VAR's to behave properly
{-# OPTIONS -#include "Linker.h" #-}
+{-# OPTIONS_GHC -fno-warn-name-shadowing #-}
-----------------------------------------------------------------------------
--
-- GHC Interactive User Interface
import FastString
import Encoding
+#if __GLASGOW_HASKELL__ < 611
+import Foreign.C
+import Encoding
+#endif
+
#ifndef mingw32_HOST_OS
import System.Posix hiding (getEnv)
#else
import Text.Printf
import Foreign
import GHC.Exts ( unsafeCoerce# )
+
+#if __GLASGOW_HASKELL__ >= 611
+import GHC.IO.Exception ( IOErrorType(InvalidArgument) )
+#else
import GHC.IOBase ( IOErrorType(InvalidArgument) )
+#endif
+
import GHC.TopHandler
import Data.IORef ( IORef, readIORef, writeIORef )
Nothing ->
do
-- enter the interactive loop
- runGHCiInput $ runCommands $ haskelineLoop show_prompt
+ runGHCiInput $ runCommands $ nextInputLine show_prompt is_tty
Just exprs -> do
-- just evaluate the expression we were given
enqueueCommands exprs
setLogAction
f
--- TODO really bad name
-haskelineLoop :: Bool -> InputT GHCi (Maybe String)
-haskelineLoop show_prompt = do
+nextInputLine :: Bool -> Bool -> InputT GHCi (Maybe String)
+nextInputLine show_prompt is_tty
+ | is_tty = do
prompt <- if show_prompt then lift mkPrompt else return ""
- l <- getInputLine prompt
- return l
-
+ getInputLine prompt
+ | otherwise = do
+ when show_prompt $ lift mkPrompt >>= liftIO . putStr
+ fileLoop stdin
-- NOTE: We only read .ghci files if they are owned by the current user,
-- and aren't world writable. Otherwise, we could be accidentally
fileLoop :: MonadIO m => Handle -> InputT m (Maybe String)
fileLoop hdl = do
- l <- liftIO $ IO.try (BS.hGetLine hdl)
+ l <- liftIO $ IO.try $ hGetLine hdl
case l of
Left e | isEOFError e -> return Nothing
| InvalidArgument <- etype -> return Nothing
-- this can happen if the user closed stdin, or
-- perhaps did getContents which closes stdin at
-- EOF.
- Right l -> fmap Just (Encoding.decode l)
+ Right l -> return (Just l)
mkPrompt :: GHCi String
mkPrompt = do