[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / lib / haskell-1.3 / LibSystem.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1995
3 %
4 \section[LibSystem]{Haskell 1.3 System Interaction}
5
6 \begin{code}
7 module LibSystem where
8
9 import PreludeGlaST
10 import PreludeIOError
11 import PreludeDialogueIO  ( unpackArgv, unpackProgName )
12
13 data ExitCode = ExitSuccess 
14               | ExitFailure Int
15 {- mattson -} deriving (Eq, Ord, Text)
16
17 \end{code}
18
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).
25
26 \begin{code}
27 getArgs :: IO [String] 
28 getArgs = return (unpackArgv ``prog_argv'' (``prog_argc''::Int))
29 \end{code}
30
31 Computation $getArgs$ returns a list of the program's command
32 line arguments (not including the program name).
33
34 \begin{code}
35 getProgName :: IO String
36 getProgName = return (unpackProgName ``prog_argv'')
37 \end{code}
38
39 Computation $getProgName$ returns the name of the program
40 as it was invoked.
41
42 \begin{code}
43 getEnv :: String -> IO String
44 getEnv name = 
45     _ccall_ getenv name                             `thenPrimIO` \ litstring ->
46     if litstring /= ``NULL'' then
47         return (_unpackPS (_packCString litstring)) -- cheaper than it looks
48     else
49         failWith (NoSuchThing ("environment variable: " ++ name))
50 \end{code}
51
52 Computation $getEnv var$ returns the value
53 of the environment variable {\em var}.  
54
55 This computation may fail with
56 \begin{itemize}
57 \item $NoSuchThing$
58 The environment variable does not exist.
59 \end{itemize}
60
61 \begin{code}
62 system :: String -> IO ExitCode
63 system "" = failWith (InvalidArgument "null command")
64 system cmd = 
65     _ccall_ systemCmd cmd                           `thenPrimIO` \ status ->
66     case status of
67         0  -> return ExitSuccess
68         -1 -> _constructError                       `thenPrimIO` \ ioError ->
69               failWith ioError
70         n  -> return (ExitFailure n)
71 \end{code}
72
73 Computation $system cmd$ returns the exit code
74 produced when the operating system processes the command {\em cmd}.
75
76 This computation may fail with
77 \begin{itemize}
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.
84 \end{itemize}
85
86 \begin{code}
87 exitWith :: ExitCode -> IO a
88 exitWith ExitSuccess = 
89     _ccall_ EXIT (0::Int)                           `thenPrimIO` \ () ->
90     failWith (OtherError13 "exit should not return")
91
92 exitWith (ExitFailure n) 
93   | n == 0 = failWith (InvalidArgument "ExitFailure 0")
94   | otherwise = 
95     _ccall_ EXIT n                                  `thenPrimIO` \ () ->
96     failWith (OtherError13 "exit should not return")
97 \end{code}
98
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.
102
103