2 % (c) The AQUA Project, Glasgow University, 1994-1996
5 \section[System]{Module @System@}
9 ExitCode(ExitSuccess,ExitFailure),
10 getArgs, getProgName, getEnv, system, exitWith
15 import PrelIOBase ( IOError(..), IOErrorType(..), constructErrorAndFailWithInfo )
16 import PrelPack ( unpackCString )
20 %*********************************************************
22 \subsection{The @ExitCode@ type}
24 %*********************************************************
26 The $ExitCode$ type defines the exit codes that a program
27 can return. $ExitSuccess$ indicates successful termination;
28 and $ExitFailure code$ indicates program failure
29 with value {\em code}. The exact interpretation of {\em code}
30 is operating-system dependent. In particular, some values of
31 {\em code} may be prohibited (e.g. 0 on a POSIX-compliant system).
34 data ExitCode = ExitSuccess | ExitFailure Int
35 deriving (Eq, Ord, Read, Show)
40 %*********************************************************
42 \subsection{Other functions}
44 %*********************************************************
47 getArgs :: IO [String]
48 getProgName :: IO String
49 getEnv :: String -> IO String
50 system :: String -> IO ExitCode
51 exitWith :: ExitCode -> IO a
54 Computation $getArgs$ returns a list of the program's command
55 line arguments (not including the program name).
58 getArgs = return (unpackArgv ``prog_argv'' (``prog_argc''::Int))
61 Computation $getProgName$ returns the name of the program
65 getProgName = return (unpackProgName ``prog_argv'')
68 Computation $getEnv var$ returns the value
69 of the environment variable {\em var}.
71 This computation may fail with
74 The environment variable does not exist.
79 litstring <- _ccall_ getenv name
80 if litstring /= ``NULL''
81 then return (unpackCString litstring)
82 else fail (IOError Nothing NoSuchThing "getEnv"
83 ("environment variable: " ++ name))
86 Computation $system cmd$ returns the exit code
87 produced when the operating system processes the command {\em cmd}.
89 This computation may fail with
91 \item $PermissionDenied$
92 The process has insufficient privileges to perform the operation.
93 \item $ResourceExhausted$
94 Insufficient resources are available to perform the operation.
95 \item $UnsupportedOperation$
96 The implementation does not support system calls.
100 system "" = fail (IOError Nothing InvalidArgument "system" "null command")
102 status <- _ccall_ systemCmd cmd
104 0 -> return ExitSuccess
105 -1 -> constructErrorAndFailWithInfo "system" cmd
106 n -> return (ExitFailure n)
110 Computation $exitWith code$ terminates the
111 program, returning {\em code} to the program's caller.
112 Before it terminates, any open or semi-closed handles are first closed.
115 exitWith ExitSuccess = do
116 _ccall_ EXIT (0::Int)
117 fail (IOError Nothing OtherError "exitWith" "exit should not return")
119 exitWith (ExitFailure n)
120 | n == 0 = fail (IOError Nothing InvalidArgument "exitWith" "ExitFailure 0")
123 fail (IOError Nothing OtherError "exitWith" "exit should not return")
127 %*********************************************************
129 \subsection{Local utilities}
131 %*********************************************************
134 type CHAR_STAR_STAR = Addr -- this is all a HACK
135 type CHAR_STAR = Addr
137 unpackArgv :: CHAR_STAR_STAR -> Int -> [String] -- argv[1 .. argc-1]
138 unpackProgName :: CHAR_STAR_STAR -> String -- argv[0]
140 unpackArgv argv argc = unpack 1
142 unpack :: Int -> [String]
145 then ([] :: [String])
146 else case (indexAddrOffAddr argv n) of { item ->
147 unpackCString item : unpack (n + 1) }
150 = case (indexAddrOffAddr argv 0) of { prog ->
151 de_slash [] (unpackCString prog) }
153 -- re-start accumulating at every '/'
154 de_slash :: String -> String -> String
155 de_slash acc [] = reverse acc
156 de_slash acc ('/':xs) = de_slash [] xs
157 de_slash acc (x:xs) = de_slash (x:acc) xs