[project @ 2004-09-29 15:50:51 by simonmar]
[ghc-base.git] / System / Cmd.hs
1 -----------------------------------------------------------------------------
2 -- |
3 -- Module      :  System.Cmd
4 -- Copyright   :  (c) The University of Glasgow 2001
5 -- License     :  BSD-style (see the file libraries/base/LICENSE)
6 -- 
7 -- Maintainer  :  libraries@haskell.org
8 -- Stability   :  provisional
9 -- Portability :  portable
10 --
11 -- Executing an external command.
12 --
13 -----------------------------------------------------------------------------
14
15 module System.Cmd
16     ( system,        -- :: String -> IO ExitCode
17 #ifdef __GLASGOW_HASKELL__
18       rawSystem,     -- :: FilePath -> [String] -> IO ExitCode
19 #endif
20     ) where
21
22 import Prelude
23
24 #ifdef __GLASGOW_HASKELL__
25 import System.Process
26 import System.Exit      ( ExitCode )
27 import GHC.IOBase       ( ioException, IOException(..), IOErrorType(..) )
28 #endif
29
30 #ifdef __HUGS__
31 import Hugs.System
32 #endif
33
34 #ifdef __NHC__
35 import System (system)
36 #endif
37
38 -- ---------------------------------------------------------------------------
39 -- system
40
41 {-| 
42 Computation @system cmd@ returns the exit code
43 produced when the operating system processes the command @cmd@.
44
45 This computation may fail with
46
47    * @PermissionDenied@: The process has insufficient privileges to
48      perform the operation.
49
50    * @ResourceExhausted@: Insufficient resources are available to
51      perform the operation.
52
53    * @UnsupportedOperation@: The implementation does not support
54      system calls.
55
56 On Windows, 'system' is implemented using Windows's native system
57 call, which ignores the @SHELL@ environment variable, and always
58 passes the command to the Windows command interpreter (@CMD.EXE@ or
59 @COMMAND.COM@), hence Unixy shell tricks will not work.
60 -}
61 #ifdef __GLASGOW_HASKELL__
62 system :: String -> IO ExitCode
63 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
64 system cmd = do
65   p <- runCommand cmd
66   waitForProcess p
67
68 rawSystem :: String -> [String] -> IO ExitCode
69 rawSystem cmd args = do
70   p <- runProcess cmd args Nothing Nothing Nothing Nothing Nothing
71   waitForProcess p
72 #endif  /* __GLASGOW_HASKELL__ */