\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}
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
\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
\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
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}