[project @ 2003-05-12 10:16:22 by ross]
[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,     -- :: String -> IO ExitCode
19 #endif
20     ) where
21
22 import Prelude
23
24 #ifdef __GLASGOW_HASKELL__
25 import System.Exit
26 import Foreign.C
27 import GHC.IOBase
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 =
65   withCString cmd $ \s -> do
66     status <- throwErrnoIfMinus1 "system" (primSystem s)
67     case status of
68         0  -> return ExitSuccess
69         n  -> return (ExitFailure n)
70
71 foreign import ccall unsafe "systemCmd" primSystem :: CString -> IO Int
72
73 {- | 
74 The same as 'system', but bypasses the shell (GHC only).
75 Will behave more portably between systems,
76 because there is no interpretation of shell metasyntax.
77 -}
78
79 rawSystem :: String -> IO ExitCode
80 rawSystem "" = ioException (IOError Nothing InvalidArgument "rawSystem" "null command" Nothing)
81 rawSystem cmd =
82   withCString cmd $ \s -> do
83     status <- throwErrnoIfMinus1 "rawSystem" (primRawSystem s)
84     case status of
85         0  -> return ExitSuccess
86         n  -> return (ExitFailure n)
87
88 foreign import ccall unsafe "rawSystemCmd" primRawSystem :: CString -> IO Int
89
90 #endif  /* __GLASGOW_HASKELL__ */