2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
4 \section[LibSystem]{Haskell 1.3 System Interaction}
11 import PreludeDialogueIO ( unpackArgv, unpackProgName )
13 data ExitCode = ExitSuccess
15 {- mattson -} deriving (Eq, Ord, Text)
19 The $ExitCode$ type defines the exit codes that a program
20 can return. $ExitSuccess$ indicates successful termination;
21 and $ExitFailure code$ indicates program failure
22 with value {\em code}. The exact interpretation of {\em code}
23 is operating-system dependent. In particular, some values of
24 {\em code} may be prohibited (e.g. 0 on a POSIX-compliant system).
27 getArgs :: IO [String]
28 getArgs = return (unpackArgv ``prog_argv'' (``prog_argc''::Int))
31 Computation $getArgs$ returns a list of the program's command
32 line arguments (not including the program name).
35 getProgName :: IO String
36 getProgName = return (unpackProgName ``prog_argv'')
39 Computation $getProgName$ returns the name of the program
43 getEnv :: String -> IO String
45 _ccall_ getenv name `thenPrimIO` \ litstring ->
46 if litstring /= ``NULL'' then
47 return (_unpackPS (_packCString litstring)) -- cheaper than it looks
49 failWith (NoSuchThing ("environment variable: " ++ name))
52 Computation $getEnv var$ returns the value
53 of the environment variable {\em var}.
55 This computation may fail with
58 The environment variable does not exist.
62 system :: String -> IO ExitCode
63 system "" = failWith (InvalidArgument "null command")
65 _ccall_ systemCmd cmd `thenPrimIO` \ status ->
67 0 -> return ExitSuccess
68 -1 -> _constructError `thenPrimIO` \ ioError ->
70 n -> return (ExitFailure n)
73 Computation $system cmd$ returns the exit code
74 produced when the operating system processes the command {\em cmd}.
76 This computation may fail with
78 \item $PermissionDenied$
79 The process has insufficient privileges to perform the operation.
80 \item $ResourceExhausted$
81 Insufficient resources are available to perform the operation.
82 \item $UnsupportedOperation$
83 The implementation does not support system calls.
87 exitWith :: ExitCode -> IO a
88 exitWith ExitSuccess =
89 _ccall_ EXIT (0::Int) `thenPrimIO` \ () ->
90 failWith (OtherError13 "exit should not return")
92 exitWith (ExitFailure n)
93 | n == 0 = failWith (InvalidArgument "ExitFailure 0")
95 _ccall_ EXIT n `thenPrimIO` \ () ->
96 failWith (OtherError13 "exit should not return")
99 Computation $exitWith code$ terminates the
100 program, returning {\em code} to the program's caller.
101 Before it terminates, any open or semi-closed handles are first closed.