[project @ 1998-12-02 13:17:09 by simonm]
[ghc-hetmet.git] / ghc / lib / std / System.lhs
index 9fec04d..0aed69e 100644 (file)
@@ -5,15 +5,25 @@
 \section[System]{Module @System@}
 
 \begin{code}
+{-# OPTIONS -#include "cbits/stgio.h" #-}
 module System ( 
     ExitCode(ExitSuccess,ExitFailure),
     getArgs, getProgName, getEnv, system, exitWith
   ) where
 
+#ifdef __HUGS__
+import PreludeBuiltin
+
+indexAddrOffAddr = primIndexAddrOffAddr
+
+unpackCString = unsafeUnpackCString
+
+#else
 import Prelude
 import PrelAddr
 import PrelIOBase      ( IOError(..), IOErrorType(..), constructErrorAndFailWithInfo )
 import PrelPack        ( unpackCString )
+#endif
 
 \end{code}
 
@@ -55,14 +65,25 @@ Computation $getArgs$ returns a list of the program's command
 line arguments (not including the program name).
 
 \begin{code}
+#ifdef __HUGS__
+foreign import stdcall "libHS_cbits.so" "get_prog_argv" primArgv :: Addr
+foreign import stdcall "libHS_cbits.so" "get_prog_argc" primArgc :: Int
+
+getArgs = return (unpackArgv primArgv primArgc)
+#else
 getArgs = return (unpackArgv ``prog_argv'' (``prog_argc''::Int))
+#endif
 \end{code}
 
 Computation $getProgName$ returns the name of the program
 as it was invoked.
 
 \begin{code}
+#ifdef __HUGS__
+getProgName = return (unpackProgName primArgv)
+#else
 getProgName = return (unpackProgName ``prog_argv'')
+#endif
 \end{code}
 
 Computation $getEnv var$ returns the value
@@ -75,12 +96,23 @@ The environment variable does not exist.
 \end{itemize}
 
 \begin{code}
+#ifdef __HUGS__
+foreign import stdcall "libHS_cbits.so" "getenv" primGetEnv :: PrimByteArray -> IO Addr
+
+getEnv name = do
+    litstring <- primGetEnv (primPackString name)
+    if litstring /= nullAddr
+       then primUnpackCString litstring
+        else fail (IOError Nothing NoSuchThing "getEnv"
+                       ("environment variable: " ++ name))
+#else
 getEnv name = do
     litstring <- _ccall_ getenv name
     if litstring /= ``NULL'' 
        then return (unpackCString litstring)
         else fail (IOError Nothing NoSuchThing "getEnv"
                        ("environment variable: " ++ name))
+#endif
 \end{code}
 
 Computation $system cmd$ returns the exit code
@@ -97,14 +129,25 @@ The implementation does not support system calls.
 \end{itemize}
 
 \begin{code}
+#ifdef __HUGS__
+foreign import stdcall "libHS_cbits.so" "systemCmd" primSystem :: PrimByteArray -> IO Int
 system "" = fail (IOError Nothing InvalidArgument "system" "null command")
 system cmd = do
-    status <- _ccall_ systemCmd cmd
+    status <- primSystem (primPackString cmd)
     case status of
         0  -> return ExitSuccess
         -1 -> constructErrorAndFailWithInfo "system" cmd
         n  -> return (ExitFailure n)
 
+#else
+system "" = fail (IOError Nothing InvalidArgument "system" "null command")
+system cmd = do
+    status <- _ccall_ systemCmd cmd
+    case status of
+        0  -> return ExitSuccess
+        -1 -> constructErrorAndFailWithInfo "system" cmd
+        n  -> return (ExitFailure n)
+#endif
 \end{code}
 
 Computation $exitWith code$ terminates the
@@ -112,15 +155,29 @@ program, returning {\em code} to the program's caller.
 Before it terminates, any open or semi-closed handles are first closed.
 
 \begin{code}
+#ifdef __HUGS__
+foreign import stdcall "libHS_cbits.so" "exit" primExit :: Int -> IO ()
+
+exitWith ExitSuccess = do
+    primExit 0
+    fail (IOError Nothing OtherError "exitWith" "exit should not return")
+
+exitWith (ExitFailure n) 
+  | n == 0 = fail (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0")
+  | otherwise = do
+    primExit n
+    fail (IOError Nothing OtherError "exitWith" "exit should not return")
+#else
 exitWith ExitSuccess = do
-    _ccall_ EXIT (0::Int)
+    _ccall_ exit (0::Int)
     fail (IOError Nothing OtherError "exitWith" "exit should not return")
 
 exitWith (ExitFailure n) 
   | n == 0 = fail (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0")
   | otherwise = do
-    _ccall_ EXIT n
+    _ccall_ exit n
     fail (IOError Nothing OtherError "exitWith" "exit should not return")
+#endif
 \end{code}