[project @ 2000-04-10 14:28:14 by sewardj]
[ghc-hetmet.git] / ghc / lib / std / System.lhs
index dee3c3d..0404492 100644 (file)
@@ -16,20 +16,16 @@ 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 PrelPack        ( unpackCString, unpackCStringST, packString )
-import PrelArr         ( ByteArray )
+import PrelByteArr     ( ByteArray )
 
 type PrimByteArray  = ByteArray Int
 
@@ -38,7 +34,6 @@ primUnpackCString s = stToIO ( unpackCStringST s )
 
 primPackString :: String -> PrimByteArray
 primPackString s    = packString s
-#endif
 
 \end{code}
 
@@ -180,3 +175,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)
+        (ioError.IOError) "System.exitWith: should not return"
+
+system :: String -> IO ExitCode
+system cmd
+   | null cmd
+   = (ioError.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