[project @ 2003-06-19 10:38:15 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 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 The same as 'system', but bypasses the shell (GHC only).
77 Will behave more portably between systems,
78 because there is no interpretation of shell metasyntax.
79 -}
80
81 rawSystem :: FilePath -> [String] -> IO ExitCode
82
83 #ifndef mingw32_TARGET_OS
84
85 rawSystem cmd args =
86   withCString cmd $ \pcmd ->
87     withMany withCString (cmd:args) $ \cstrs ->
88       withArray0 nullPtr cstrs $ \arr -> do
89         status <- throwErrnoIfMinus1 "rawSystem" (c_rawSystem pcmd arr)
90         case status of
91             0  -> return ExitSuccess
92             n  -> return (ExitFailure n)
93
94 foreign import ccall unsafe "rawSystem"
95   c_rawSystem :: CString -> Ptr CString -> IO Int
96
97 #else
98
99 -- On Windows, the command line is passed to the operating system as
100 -- a single string.  Command-line parsing is done by the executable
101 -- itself.
102 rawSystem cmd args = do
103   let cmdline = translate cmd ++ concat (map ((' ':) . translate) args)
104   withCString cmdline $ \pcmdline -> do
105     status <- throwErrnoIfMinus1 "rawSystem" (c_rawSystem pcmdline)
106     case status of
107        0  -> return ExitSuccess
108        n  -> return (ExitFailure n)
109
110 translate :: String -> String
111 translate str = '"' : foldr escape "\"" str
112   where escape '"'  str = '\\' : '"'  : str
113         escape '\\' str = '\\' : '\\' : str
114         escape c    str = c : str
115
116 foreign import ccall unsafe "rawSystem"
117   c_rawSystem :: CString -> IO Int
118
119 #endif
120
121 #endif  /* __GLASGOW_HASKELL__ */