-{-
%
-% (c) The GRASP/AQUA Project, Glasgow University, 1995
+% (c) The AQUA Project, Glasgow University, 1994-1996
%
-\section[LibSystem]{Haskell 1.3 System Interaction}
--}
+
+\section[System]{Module @System@}
+
+\begin{code}
module System (
ExitCode(ExitSuccess,ExitFailure),
- getArgs, getProgName, getEnv, system, exitWith ) where
+ getArgs, getProgName, getEnv, system, exitWith
+ ) where
+
+import Foreign ( Addr )
+import IOBase ( IOError(..), thenIO_Prim, constructErrorAndFail )
+import ArrBase ( indexAddrOffAddr )
+import PackedString ( unpackCString )
+\end{code}
-import GHCio
-import GHCps ( unpackPS, packCString )
-import GHCbase ( indexAddrOffAddr, Addr )
+%*********************************************************
+%* *
+\subsection{The @ExitCode@ type}
+%* *
+%*********************************************************
-{-
The $ExitCode$ type defines the exit codes that a program
can return. $ExitSuccess$ indicates successful termination;
and $ExitFailure code$ indicates program failure
with value {\em code}. The exact interpretation of {\em code}
is operating-system dependent. In particular, some values of
{\em code} may be prohibited (e.g. 0 on a POSIX-compliant system).
--}
+\begin{code}
data ExitCode = ExitSuccess | ExitFailure Int
deriving (Eq, Ord, Read, Show)
+\end{code}
+
+%*********************************************************
+%* *
+\subsection{Other functions}
+%* *
+%*********************************************************
+
+\begin{code}
getArgs :: IO [String]
getProgName :: IO String
getEnv :: String -> IO String
system :: String -> IO ExitCode
exitWith :: ExitCode -> IO a
+\end{code}
-{-
Computation $getArgs$ returns a list of the program's command
line arguments (not including the program name).
--}
+
+\begin{code}
getArgs = return (unpackArgv ``prog_argv'' (``prog_argc''::Int))
+\end{code}
-{-
Computation $getProgName$ returns the name of the program
as it was invoked.
--}
+
+\begin{code}
getProgName = return (unpackProgName ``prog_argv'')
+\end{code}
-{-
Computation $getEnv var$ returns the value
of the environment variable {\em var}.
\item $NoSuchThing$
The environment variable does not exist.
\end{itemize}
--}
+
+\begin{code}
getEnv name =
- _ccall_ getenv name `stThen` \ litstring ->
+ _ccall_ getenv name `thenIO_Prim` \ litstring ->
if litstring /= ``NULL'' then
- return (unpackPS (packCString litstring)) -- cheaper than it looks
+ return (unpackCString litstring)
else
fail (NoSuchThing ("environment variable: " ++ name))
+\end{code}
-{-
Computation $system cmd$ returns the exit code
produced when the operating system processes the command {\em cmd}.
\item $UnsupportedOperation$
The implementation does not support system calls.
\end{itemize}
--}
+
+\begin{code}
system "" = fail (InvalidArgument "null command")
system cmd =
- _ccall_ systemCmd cmd `stThen` \ status ->
+ _ccall_ systemCmd cmd `thenIO_Prim` \ status ->
case status of
0 -> return ExitSuccess
-1 -> constructErrorAndFail "system"
n -> return (ExitFailure n)
-{-
+\end{code}
+
Computation $exitWith code$ terminates the
program, returning {\em code} to the program's caller.
Before it terminates, any open or semi-closed handles are first closed.
--}
+
+\begin{code}
exitWith ExitSuccess =
- _ccall_ EXIT (0::Int) `stThen` \ () ->
+ _ccall_ EXIT (0::Int) `thenIO_Prim` \ () ->
fail (OtherError "exit should not return")
exitWith (ExitFailure n)
| n == 0 = fail (InvalidArgument "ExitFailure 0")
| otherwise =
- _ccall_ EXIT n `stThen` \ () ->
+ _ccall_ EXIT n `thenIO_Prim` \ () ->
fail (OtherError "exit should not return")
+\end{code}
+
-------------------------------------------
--- like unpackCString ...
+%*********************************************************
+%* *
+\subsection{Local utilities}
+%* *
+%*********************************************************
+\begin{code}
type CHAR_STAR_STAR = Addr -- this is all a HACK
type CHAR_STAR = Addr
= if (n >= argc)
then ([] :: [String])
else case (indexAddrOffAddr argv n) of { item ->
- unpackPS (packCString item) : unpack (n + 1) }
+ unpackCString item : unpack (n + 1) }
unpackProgName argv
= case (indexAddrOffAddr argv 0) of { prog ->
- de_slash [] (unpackPS (packCString prog)) }
+ de_slash [] (unpackCString prog) }
where
-- re-start accumulating at every '/'
de_slash :: String -> String -> String
de_slash acc [] = reverse acc
de_slash acc ('/':xs) = de_slash [] xs
de_slash acc (x:xs) = de_slash (x:acc) xs
+\end{code}