1 {-# OPTIONS -cpp -fffi #-}
2 #if __GLASGOW_HASKELL__ < 603
7 -----------------------------------------------------------------------------
9 -- (c) The University of Glasgow, 2004
11 -- runghc program, for invoking from a #! line in a script. For example:
15 -- > main = putStrLn "hello!"
17 -- runghc accepts one flag:
19 -- -f <path> specify the path
21 -- -----------------------------------------------------------------------------
25 import System.Environment
28 import System.Directory
32 import Compat.RawSystem ( rawSystem )
37 ('-':'f' : ghc) : filename : args -> do
38 doIt (dropWhile isSpace ghc) filename args
40 path <- getEnv "PATH" `catch` \e -> return "."
41 ghc <- findBinary "ghc"
42 doIt ghc filename args
44 dieProg "syntax: runghc [-f GHCPATH] FILE ARG..."
46 doIt ghc filename args = do
47 res <- rawSystem ghc ["-e","System.Environment.withArgs ["
48 ++ concat (intersperse "," (map show args))
49 ++ "] Main.main", filename]
52 findBinary :: String -> IO FilePath
53 findBinary binary = do
55 search (parsePath path)
57 search :: [FilePath] -> IO FilePath
58 search [] = dieProg ("cannot find " ++ binary)
60 let path = d ++ '/':binary
61 b <- doesFileExist path
62 if b then return path else search ds
64 parsePath :: String -> [FilePath]
65 parsePath path = split pathSep path
67 #ifdef mingw32_TARGET_OS
73 split :: Char -> String -> [String]
74 split c s = case rest of
76 _:rest -> chunk : split c rest
77 where (chunk, rest) = break (==c) s
80 die msg = do hPutStr stderr msg; exitWith (ExitFailure 1)
82 dieProg :: String -> IO a
83 dieProg msg = do p <- getProgName; die (p ++ ": " ++ msg)