[project @ 1999-10-29 01:16:48 by andy]
[ghc-hetmet.git] / ghc / lib / std / System.lhs
index f09a617..0080df6 100644 (file)
@@ -16,15 +16,11 @@ 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 )
@@ -38,7 +34,6 @@ primUnpackCString s = stToIO ( unpackCStringST s )
 
 primPackString :: String -> PrimByteArray
 primPackString s    = packString s
-#endif
 
 \end{code}
 
@@ -159,18 +154,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 +175,51 @@ 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
+-----------------------------------------------------------------------------
+
+data ExitCode = ExitSuccess | ExitFailure Int
+                deriving (Eq, Ord, Read, Show)
+
+getArgs                     :: IO [String]
+getArgs                      = primGetRawArgs >>= \rawargs ->
+                               return (drop 1 (dropWhile (/= "--") rawargs))
+
+getProgName                 :: IO String
+getProgName                  = primGetRawArgs >>= \rawargs ->
+                               return (head rawargs)
+
+getEnv                      :: String -> IO String
+getEnv                       = primGetEnv
+
+system                      :: String -> IO ExitCode
+system s                     = error "System.system unimplemented"
+
+exitWith                    :: ExitCode -> IO a
+exitWith c                   = error "System.exitWith unimplemented"
+
+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
+
+-----------------------------------------------------------------------------
+\end{code}
+#endif