f5c85ecb7449e56779d639df05114ac67df2de87
[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 #ifndef __HUGS__
18       rawSystem,     -- :: String -> IO ExitCode
19 #endif
20     ) where
21
22 import Prelude
23
24 import System.Exit
25 #ifndef __HUGS__
26 import Foreign.C
27 #endif
28
29 #ifdef __GLASGOW_HASKELL__
30 import GHC.IOBase
31 #endif
32
33 #ifdef __HUGS__
34 import Hugs.System
35 #endif
36
37 -- ---------------------------------------------------------------------------
38 -- system
39
40 {-| 
41 Computation @system cmd@ returns the exit code
42 produced when the operating system processes the command @cmd@.
43
44 This computation may fail with
45
46    * @PermissionDenied@: The process has insufficient privileges to
47      perform the operation.
48
49    * @ResourceExhausted@: Insufficient resources are available to
50      perform the operation.
51
52    * @UnsupportedOperation@: The implementation does not support
53      system calls.
54
55 On Windows, 'system' is implemented using Windows's native system
56 call, which ignores the @SHELL@ environment variable, and always
57 passes the command to the Windows command interpreter (@CMD.EXE@ or
58 @COMMAND.COM@), hence Unixy shell tricks will not work.
59 -}
60 #ifndef __HUGS__
61 system :: String -> IO ExitCode
62 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
63 system cmd =
64   withCString cmd $ \s -> do
65     status <- throwErrnoIfMinus1 "system" (primSystem s)
66     case status of
67         0  -> return ExitSuccess
68         n  -> return (ExitFailure n)
69
70 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int
71
72 {- | 
73 The same as 'system', but bypasses the shell.  Will behave more portably between
74 systems, because there is no interpretation of shell metasyntax.
75 -}
76
77 rawSystem :: String -> IO ExitCode
78 rawSystem "" = ioException (IOError Nothing InvalidArgument "rawSystem" "null command" Nothing)
79 rawSystem cmd =
80   withCString cmd $ \s -> do
81     status <- throwErrnoIfMinus1 "rawSystem" (primRawSystem s)
82     case status of
83         0  -> return ExitSuccess
84         n  -> return (ExitFailure n)
85
86 foreign import ccall unsafe "rawSystemCmd" primRawSystem :: CString -> IO Int
87
88 #endif  /* __HUGS__ */