1 -----------------------------------------------------------------------------
2 -- $Id: InteractiveUI.hs,v 1.6 2000/11/20 16:51:35 simonmar Exp $
4 -- GHC Interactive User Interface
6 -- (c) The GHC Team 2000
8 -----------------------------------------------------------------------------
10 module InteractiveUI (interactiveUI) where
12 #include "HsVersions.h"
34 -----------------------------------------------------------------------------
37 \ _____ __ __ ____ _________________________________________________\n\
38 \(| || || (| |) GHC Interactive, version 5.00 \n\
39 \|| __ ||___|| || () For Haskell 98. \n\
40 \|| |) ||---|| || || http://www.haskell.org/ghc \n\
41 \|| || || || || (| Bug reports to: glasgow-haskell-bugs@haskell.org \n\
42 \(|___|| || || (|__|) \\\\______________________________________________________\n"
44 commands :: [(String, String -> GHCi ())]
46 ("cd", changeDirectory),
50 ("reload", reloadModule),
56 shortHelpText = "use :? for help.\n"
59 \ <expr> evaluate <expr>\n\
60 \ :cd <dir> change directory to <dir>\n\
61 \ :help display this list of commands\n\
62 \ :? display this list of commands\n\
63 \ :load <filename> load a module (and it dependents)\n\
64 \ :reload reload the current program\n\
65 \ :set <opetion> ... set options\n\
66 \ :type <expr> show the type of <expr>\n\
68 \ :!<command> run the shell command <command>\n\
71 interactiveUI :: CmState -> IO ()
73 hPutStrLn stdout ghciWelcomeMsg
75 hSetBuffering stdout NoBuffering
77 -- link in the available packages
78 pkgs <- getPackageInfo
79 linkPackages (reverse pkgs)
84 _ <- (unGHCi uiLoop) GHCiState{ modules = [],
85 current_module = Nothing,
94 l <- io (readline (mkPrompt (current_module st) ++ "> "))
96 l <- io (hGetLine stdin)
108 mkPrompt Nothing = ""
109 mkPrompt (Just mod_name) = moduleNameUserString mod_name
111 -- Top level exception handler, just prints out the exception
114 ghciHandle ( \other_exception ->io (putStrLn (show other_exception) )) $
117 PhaseFailed phase code ->
118 io ( putStrLn ("Phase " ++ phase ++ " failed (code "
119 ++ show code ++ ")"))
120 Interrupted -> io (putStrLn "Interrupted.")
121 _ -> io (putStrLn (show (dyn :: BarfKind)))
125 doCommand (':' : command) = specialCommand command
128 case current_module st of
129 Nothing -> throwDyn (OtherError "no module context in which to run the expression")
131 dflags <- io (readIORef v_DynFlags)
132 (st, maybe_hvalue) <-
133 io (cmGetExpr (cmstate st) dflags mod expr)
136 Just hv -> io (cmRunExpr hv)
138 let (mod,'.':str) = break (=='.') expr
139 case cmLookupSymbol (mkOrig varName (mkModuleName mod) (_PK_ str)) (cmstate st) of
140 Nothing -> io (putStrLn "nothing.")
145 specialCommand ('!':str) = shellEscape (dropWhile isSpace str)
146 specialCommand str = do
147 let (cmd,rest) = break isSpace str
148 case [ (s,f) | (s,f) <- commands, prefixMatch cmd s ] of
149 [] -> io $ hPutStr stdout ("uknown command `:" ++ cmd ++ "'\n"
151 [(_,f)] -> f (dropWhile isSpace rest)
152 cs -> io $ hPutStrLn stdout ("prefix " ++ cmd ++
153 " matches multiple commands (" ++
154 foldr1 (\a b -> a ++ ',':b) (map fst cs)
157 noArgs c = io (hPutStrLn stdout ("command `:" ++ c ++ "' takes no arguments"))
159 -----------------------------------------------------------------------------
162 help :: String -> GHCi ()
163 help _ = io (putStr helpText)
165 changeDirectory :: String -> GHCi ()
166 changeDirectory = io . setCurrentDirectory
168 loadModule :: String -> GHCi ()
170 state <- getGHCiState
171 (new_cmstate, ok, mods) <- io (cmLoadModule (cmstate state) path)
173 let new_state = GHCiState {
174 cmstate = new_cmstate,
176 current_module = case mods of
178 xs -> Just (last xs),
181 setGHCiState new_state
184 | null mods = text "none."
186 punctuate comma (map (text.moduleNameUserString) mods)) <> text "."
189 io (putStrLn (showSDoc (text "Failed, modules loaded: " <> mod_commas)))
191 io (putStrLn (showSDoc (text "Ok, modules loaded: " <> mod_commas)))
193 reloadModule :: String -> GHCi ()
195 state <- getGHCiState
197 Nothing -> io (putStr "no current target\n")
198 Just path -> do (new_cmstate, ok, mod)
199 <- io (cmLoadModule (cmstate state) path)
200 setGHCiState state{cmstate=new_cmstate}
201 reloadModule _ = noArgs ":reload"
203 -- set options in the interpreter. Syntax is exactly the same as the
204 -- ghc command line, except that certain options aren't available (-C,
207 -- This is pretty fragile: most options won't work as expected. ToDo:
208 -- figure out which ones & disallow them.
209 setOptions :: String -> GHCi ()
211 io (do leftovers <- processArgs static_flags (words str) []
212 dyn_flags <- readIORef v_InitDynFlags
213 writeIORef v_DynFlags dyn_flags
214 leftovers <- processArgs dynamic_flags leftovers []
215 dyn_flags <- readIORef v_DynFlags
216 writeIORef v_InitDynFlags dyn_flags
217 if (not (null leftovers))
218 then throwDyn (OtherError ("unrecognised flags: " ++
223 typeOfExpr :: String -> GHCi ()
224 typeOfExpr = panic "typeOfExpr"
226 quit :: String -> GHCi ()
229 shellEscape :: String -> GHCi ()
230 shellEscape str = io (system str >> return ())
232 -----------------------------------------------------------------------------
235 data GHCiState = GHCiState
237 modules :: [ModuleName],
238 current_module :: Maybe ModuleName,
239 target :: Maybe FilePath,
243 newtype GHCi a = GHCi { unGHCi :: GHCiState -> IO (GHCiState, a) }
245 instance Monad GHCi where
246 (GHCi m) >>= k = GHCi $ \s -> m s >>= \(s,a) -> unGHCi (k a) s
247 return a = GHCi $ \s -> return (s,a)
249 getGHCiState = GHCi $ \s -> return (s,s)
250 setGHCiState s = GHCi $ \_ -> return (s,())
252 io m = GHCi $ \s -> m >>= \a -> return (s,a)
254 ghciHandle h (GHCi m) = GHCi $ \s ->
255 Exception.catch (m s) (\e -> unGHCi (h e) s)
256 ghciHandleDyn h (GHCi m) = GHCi $ \s ->
257 Exception.catchDyn (m s) (\e -> unGHCi (h e) s)
259 -----------------------------------------------------------------------------
262 linkPackages :: [Package] -> IO ()
263 linkPackages pkgs = mapM_ linkPackage pkgs
265 linkPackage :: Package -> IO ()
266 -- ignore rts and gmp for now (ToDo; better?)
267 linkPackage pkg | name pkg `elem` ["rts", "gmp"] = return ()
269 putStr ("Loading package " ++ name pkg ++ " ... ")
270 let dirs = library_dirs pkg
271 let objs = map (++".o") (hs_libraries pkg ++ extra_libraries pkg)
272 mapM (linkOneObj dirs) objs
273 putStr "resolving ... "
277 linkOneObj dirs obj = do
278 filename <- findFile dirs obj
281 findFile [] obj = throwDyn (OtherError ("can't find " ++ obj))
282 findFile (d:ds) obj = do
283 let path = d ++ '/':obj
284 b <- doesFileExist path
285 if b then return path else findFile ds obj