Tweak runghc
[ghc-hetmet.git] / utils / runghc / runghc.hs
1 {-# OPTIONS -cpp -fffi #-}
2 #if __GLASGOW_HASKELL__ < 603
3 #include "config.h"
4 #else
5 #include "ghcconfig.h"
6 #endif
7 -----------------------------------------------------------------------------
8 --
9 -- (c) The University of Glasgow, 2004
10 --
11 -- runghc program, for invoking from a #! line in a script.  For example:
12 --
13 --   script.lhs:
14 --      #! /usr/bin/runghc
15 --      > main = putStrLn "hello!"
16 --
17 -- runghc accepts one flag:
18 --
19 --      -f <path>    specify the path
20 --
21 -- -----------------------------------------------------------------------------
22
23 module Main (main) where
24
25 import System.Environment
26 import System.IO
27 import Data.List
28 import System.Exit
29 import Data.Char
30
31 #ifdef USING_COMPAT
32 import Compat.RawSystem ( rawSystem )
33 import Compat.Directory ( findExecutable )
34 #else
35 import System.Cmd       ( rawSystem )
36 import System.Directory ( findExecutable )
37 #endif
38
39 main :: IO ()
40 main = do
41     args <- getArgs
42     case getGhcLoc args of
43         (Just ghc, args') -> doIt ghc args'
44         (Nothing, args') -> do
45             mb_ghc <- findExecutable "ghc"
46             case mb_ghc of
47                 Nothing  -> dieProg ("cannot find ghc")
48                 Just ghc -> doIt ghc args'
49
50 getGhcLoc :: [String] -> (Maybe FilePath, [String])
51 getGhcLoc ("-f" : ghc : args) = (Just ghc, args)
52 getGhcLoc (('-' : 'f' : ghc) : args) = (Just ghc, args)
53 -- If you need the first GHC flag to be a -f flag then you can pass --
54 -- first
55 getGhcLoc ("--" : args) = (Nothing, args)
56 getGhcLoc args = (Nothing, args)
57
58 doIt :: String -> [String] -> IO ()
59 doIt ghc args = do
60     let (ghc_args, rest) = getGhcArgs args
61     case rest of
62         [] -> dieProg usage
63         filename : prog_args -> do
64             let c1 = ":set prog " ++ show filename
65                 c2 = ":main " ++ show prog_args
66             res <- rawSystem ghc (["-ignore-dot-ghci"] ++ ghc_args ++
67                                   [ "-e", c1, "-e", c2, filename])
68             exitWith res
69
70 getGhcArgs :: [String] -> ([String], [String])
71 getGhcArgs args
72  = let (ghcArgs, otherArgs) = case break pastArgs args of
73                               (xs, "--":ys) -> (xs, ys)
74                               (xs, ys)      -> (xs, ys)
75    in (map unescape ghcArgs, otherArgs)
76     where unescape ('-':'-':'g':'h':'c':'-':'a':'r':'g':'=':arg) = arg
77           unescape arg = arg
78
79 pastArgs :: String -> Bool
80 -- You can use -- to mark the end of the flags, in case you need to use
81 -- a file called -foo.hs for some reason. You almost certainly shouldn't,
82 -- though.
83 pastArgs "--" = True
84 pastArgs ('-':_) = False
85 pastArgs _       = True
86
87 dieProg :: String -> IO a
88 dieProg msg = do
89     p <- getProgName
90     hPutStrLn stderr (p ++ ": " ++ msg)
91     exitWith (ExitFailure 1)
92
93 usage :: String
94 usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
95