3 -----------------------------------------------------------------------------
5 -- (c) The University of Glasgow, 2004
7 -- runghc program, for invoking from a #! line in a script. For example:
11 -- > main = putStrLn "hello!"
13 -- runghc accepts one flag:
15 -- -f <path> specify the path
17 -- -----------------------------------------------------------------------------
21 import System.Environment
25 import System.Directory
32 ('-':'f' : ghc) : filename : args -> do
33 doIt (dropWhile isSpace ghc) filename args
35 path <- getEnv "PATH" `catch` \e -> return "."
36 ghc <- findBinary "ghc"
37 doIt ghc filename args
39 dieProg "syntax: runghc [-f GHCPATH] FILE ARG..."
41 doIt ghc filename args = do
42 res <- rawSystem ghc ["-e","System.Environment.withArgs ["
43 ++ concat (intersperse "," (map show args))
44 ++ "] Main.main", filename]
47 findBinary :: String -> IO FilePath
48 findBinary binary = do
50 search (parsePath path)
52 search :: [FilePath] -> IO FilePath
53 search [] = dieProg ("cannot find " ++ binary)
55 let path = d ++ '/':binary
56 b <- doesFileExist path
57 if b then return path else search ds
59 parsePath :: String -> [FilePath]
60 parsePath path = split pathSep path
62 #ifdef mingw32_TARGET_OS
68 split :: Char -> String -> [String]
69 split c s = case rest of
71 _:rest -> chunk : split c rest
72 where (chunk, rest) = break (==c) s
75 die msg = do hPutStr stderr msg; exitWith (ExitFailure 1)
77 dieProg :: String -> IO a
78 dieProg msg = do p <- getProgName; die (p ++ ": " ++ msg)