[project @ 2000-07-07 11:03:57 by simonmar]
[ghc-hetmet.git] / ghc / lib / std / System.lhs
index f09a617..c96e2b9 100644 (file)
@@ -1,5 +1,7 @@
+% -----------------------------------------------------------------------------
+% $Id: System.lhs,v 1.26 2000/07/07 11:03:58 simonmar Exp $
 %
-% (c) The AQUA Project, Glasgow University, 1994-1999
+% (c) The University of Glasgow, 1994-2000
 %
 
 \section[System]{Module @System@}
@@ -16,20 +18,17 @@ module System
     , exitWith      -- :: ExitCode -> IO a
     , exitFailure   -- :: IO a
   ) where
+\end{code}
 
-#ifdef __HUGS__
-import PreludeBuiltin
-
-indexAddrOffAddr = primIndexAddrOffAddr
-
-unpackCString = unsafeUnpackCString
 
-#else
+#ifndef __HUGS__
+\begin{code}
 import Prelude
 import PrelAddr
-import PrelIOBase      ( IOError(..), IOErrorType(..), constructErrorAndFailWithInfo, stToIO )
+import PrelIOBase      ( IOException(..), ioException, 
+                         IOErrorType(..), constructErrorAndFailWithInfo, stToIO )
 import PrelPack        ( unpackCString, unpackCStringST, packString )
-import PrelArr         ( ByteArray )
+import PrelByteArr     ( ByteArray )
 
 type PrimByteArray  = ByteArray Int
 
@@ -38,7 +37,6 @@ primUnpackCString s = stToIO ( unpackCStringST s )
 
 primPackString :: String -> PrimByteArray
 primPackString s    = packString s
-#endif
 
 \end{code}
 
@@ -95,8 +93,8 @@ getEnv name = do
     litstring <- primGetEnv (primPackString name)
     if litstring /= nullAddr
        then primUnpackCString litstring
-        else ioError (IOError Nothing NoSuchThing "getEnv"
-                       ("environment variable: " ++ name))
+        else ioException (IOError Nothing NoSuchThing "getEnv"
+                           ("environment variable: " ++ name))
 
 foreign import ccall "libHS_cbits.so" "getenv" unsafe primGetEnv :: PrimByteArray -> IO Addr
 \end{code}
@@ -116,7 +114,7 @@ The implementation does not support system calls.
 
 \begin{code}
 system                 :: String -> IO ExitCode
-system "" = ioError (IOError Nothing InvalidArgument "system" "null command")
+system "" = ioException (IOError Nothing InvalidArgument "system" "null command")
 system cmd = do
     status <- primSystem (primPackString cmd)
     case status of
@@ -134,14 +132,16 @@ Before it terminates, any open or semi-closed handles are first closed.
 exitWith               :: ExitCode -> IO a
 exitWith ExitSuccess = do
     primExit 0
-    ioError (IOError Nothing OtherError "exitWith" "exit should not return")
+    ioException (IOError Nothing OtherError "exitWith" "exit should not return")
 
 exitWith (ExitFailure n) 
-  | n == 0 = ioError (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0")
+  | n == 0 = ioException (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0")
   | otherwise = do
     primExit n
-    ioError (IOError Nothing OtherError "exitWith" "exit should not return")
+    ioException (IOError Nothing OtherError "exitWith" "exit should not return")
 
+-- NOTE: shutdownHaskellAndExit must be called "safe", because it *can*
+-- re-enter Haskell land through finalizers.
 foreign import ccall "shutdownHaskellAndExit" primExit :: Int -> IO ()
 
 exitFailure :: IO a
@@ -159,18 +159,17 @@ exitFailure = exitWith (ExitFailure 1)
 type CHAR_STAR_STAR    = Addr  -- this is all a  HACK
 type CHAR_STAR         = Addr
 
-unpackArgv     :: CHAR_STAR_STAR -> Int -> [String] -- argv[1 .. argc-1]
-unpackProgName :: CHAR_STAR_STAR        -> String   -- argv[0]
-
+unpackArgv :: CHAR_STAR_STAR -> Int -> [String] -- argv[1 .. argc-1]
 unpackArgv argv argc = unpack 1
   where
-    unpack :: Int -> [String]
-    unpack n
-      = if (n >= argc)
-       then ([] :: [String])
-       else case (indexAddrOffAddr argv n) of { item ->
-            unpackCString item : unpack (n + 1) }
+   unpack :: Int -> [String]
+   unpack n
+     | n >= argc = []
+     | otherwise =
+        case (indexAddrOffAddr argv n) of 
+          item -> unpackCString item : unpack (n + 1)
 
+unpackProgName :: CHAR_STAR_STAR        -> String   -- argv[0]
 unpackProgName argv
   = case (indexAddrOffAddr argv 0) of { prog ->
     de_slash [] (unpackCString prog) }
@@ -181,3 +180,90 @@ unpackProgName argv
     de_slash _acc ('/':xs) = de_slash []      xs
     de_slash  acc (x:xs)   = de_slash (x:acc) xs
 \end{code}
+
+#else
+
+\begin{code}
+-----------------------------------------------------------------------------
+-- Standard Library: System operations
+--
+-- Warning: the implementation of these functions in Hugs 98 is very weak.
+-- The functions themselves are best suited to uses in compiled programs,
+-- and not to use in an interpreter-based environment like Hugs.
+--
+-- Suitable for use with Hugs 98
+-----------------------------------------------------------------------------
+import PrelPrim ( primGetRawArgs
+               , primGetEnv
+               , prelCleanupAfterRunAction
+               , copy_String_to_cstring
+               , readIORef
+               , nh_stderr
+               , nh_stdout
+               , nh_stdin 
+               , nh_exitwith 
+               , nh_flush
+               , nh_close
+               , nh_system
+               , nh_free
+               , nh_getPID
+               )
+
+
+data ExitCode = ExitSuccess | ExitFailure Int
+                deriving (Eq, Ord, Read, Show)
+
+getArgs                     :: IO [String]
+getArgs                      = primGetRawArgs >>= \rawargs ->
+                               return (tail rawargs)
+
+getProgName                 :: IO String
+getProgName                  = primGetRawArgs >>= \rawargs ->
+                               return (head rawargs)
+
+getEnv                      :: String -> IO String
+getEnv                       = primGetEnv
+
+exitFailure                :: IO a
+exitFailure                 = exitWith (ExitFailure 1)
+
+toExitCode                  :: Int -> ExitCode
+toExitCode 0                 = ExitSuccess
+toExitCode n                 = ExitFailure n
+
+fromExitCode                :: ExitCode -> Int
+fromExitCode ExitSuccess     = 0
+fromExitCode (ExitFailure n) = n
+
+-- see comment in Prelude.hs near primRunIO_hugs_toplevel
+exitWith :: ExitCode -> IO a
+exitWith c
+   = do cleanup_action <- readIORef prelCleanupAfterRunAction
+        case cleanup_action of
+           Just xx -> xx
+           Nothing -> return ()
+        nh_stderr >>= nh_flush
+        nh_stdout >>= nh_flush
+        nh_stdin  >>= nh_close
+        nh_exitwith (fromExitCode c)
+        (ioException . IOError) "System.exitWith: should not return"
+
+system :: String -> IO ExitCode
+system cmd
+   | null cmd
+   = (ioException.IOError) "System.system: null command"
+   | otherwise
+   = do str    <- copy_String_to_cstring cmd
+        status <- nh_system str
+        nh_free str
+        case status of
+           0  -> return ExitSuccess
+           n  -> return (ExitFailure n)
+
+getPID :: IO Int
+getPID
+   = nh_getPID
+
+-----------------------------------------------------------------------------
+\end{code}
+#endif