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