[project @ 2004-05-10 09:22:59 by malcolm]
[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 Foreign
26 import Foreign.C
27 import System.Exit
28 import GHC.IOBase
29 #include "config.h"
30 #endif
31
32 #ifdef __HUGS__
33 import Hugs.System
34 #endif
35
36 #ifdef __NHC__
37 import System (system)
38 #endif
39
40 -- ---------------------------------------------------------------------------
41 -- system
42
43 {-| 
44 Computation @system cmd@ returns the exit code
45 produced when the operating system processes the command @cmd@.
46
47 This computation may fail with
48
49    * @PermissionDenied@: The process has insufficient privileges to
50      perform the operation.
51
52    * @ResourceExhausted@: Insufficient resources are available to
53      perform the operation.
54
55    * @UnsupportedOperation@: The implementation does not support
56      system calls.
57
58 On Windows, 'system' is implemented using Windows's native system
59 call, which ignores the @SHELL@ environment variable, and always
60 passes the command to the Windows command interpreter (@CMD.EXE@ or
61 @COMMAND.COM@), hence Unixy shell tricks will not work.
62 -}
63 #ifdef __GLASGOW_HASKELL__
64 system :: String -> IO ExitCode
65 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
66 system cmd =
67   withCString cmd $ \s -> do
68     status <- throwErrnoIfMinus1 "system" (primSystem s)
69     case status of
70         0  -> return ExitSuccess
71         n  -> return (ExitFailure n)
72
73 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int
74
75 -- ---------------------------------------------------------------------------
76 -- rawSystem
77
78 -- rawSystem is in a separate file, so we can #include it various places.
79 #include "RawSystem.hs-inc"
80
81 #endif  /* __GLASGOW_HASKELL__ */