Allow runghc to take input from stdin, just like Ruby & Python
authorSimon Marlow <simonmar@microsoft.com>
Thu, 7 Feb 2008 14:58:30 +0000 (14:58 +0000)
committerSimon Marlow <simonmar@microsoft.com>
Thu, 7 Feb 2008 14:58:30 +0000 (14:58 +0000)
utils/runghc/runghc.hs

index 244c98f..458861e 100644 (file)
@@ -27,13 +27,14 @@ import System.IO
 import Data.List
 import System.Exit
 import Data.Char
 import Data.List
 import System.Exit
 import Data.Char
+import System.Directory ( removeFile )
+import Control.Exception  ( bracket )
+import System.Directory ( findExecutable, getTemporaryDirectory )
 
 #ifdef USING_COMPAT
 import Compat.RawSystem ( rawSystem )
 
 #ifdef USING_COMPAT
 import Compat.RawSystem ( rawSystem )
-import Compat.Directory ( findExecutable )
 #else
 import System.Cmd       ( rawSystem )
 #else
 import System.Cmd       ( rawSystem )
-import System.Directory ( findExecutable )
 #endif
 
 main :: IO ()
 #endif
 
 main :: IO ()
@@ -59,7 +60,17 @@ doIt :: String -> [String] -> IO ()
 doIt ghc args = do
     let (ghc_args, rest) = getGhcArgs args
     case rest of
 doIt ghc args = do
     let (ghc_args, rest) = getGhcArgs args
     case rest of
-        [] -> dieProg usage
+        [] -> do
+           -- behave like typical perl, python, ruby interpreters:      
+           -- read from stdin
+           tmpdir <- getTemporaryDirectory
+           bracket
+             (openTempFile tmpdir "runghcXXXX.hs")
+             (\(filename,_) -> removeFile filename)
+             $ \(filename,h) -> do
+                 getContents >>= hPutStr h
+                 hClose h
+                 doIt ghc (ghc_args ++ [filename])
         filename : prog_args -> do
             let c1 = ":set prog " ++ show filename
                 c2 = ":main " ++ show prog_args
         filename : prog_args -> do
             let c1 = ":set prog " ++ show filename
                 c2 = ":main " ++ show prog_args
@@ -90,6 +101,6 @@ dieProg msg = do
     hPutStrLn stderr (p ++ ": " ++ msg)
     exitWith (ExitFailure 1)
 
     hPutStrLn stderr (p ++ ": " ++ msg)
     exitWith (ExitFailure 1)
 
-usage :: String
-usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."
+-- usage :: String
+-- usage = "syntax: runghc [-f GHC-PATH | --] [GHC-ARGS] [--] FILE ARG..."