1 -----------------------------------------------------------------------------
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : provisional
9 -- Portability : portable
11 -- Executing an external command.
13 -----------------------------------------------------------------------------
16 ( system, -- :: String -> IO ExitCode
17 rawSystem, -- :: FilePath -> [String] -> IO ExitCode
22 import System.Exit ( ExitCode )
24 #ifdef __GLASGOW_HASKELL__
26 import GHC.IOBase ( ioException, IOException(..), IOErrorType(..) )
34 import System (system)
37 -- ---------------------------------------------------------------------------
41 Computation @system cmd@ returns the exit code
42 produced when the operating system processes the command @cmd@.
44 This computation may fail with
46 * @PermissionDenied@: The process has insufficient privileges to
47 perform the operation.
49 * @ResourceExhausted@: Insufficient resources are available to
50 perform the operation.
52 * @UnsupportedOperation@: The implementation does not support
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.
60 #ifdef __GLASGOW_HASKELL__
61 system :: String -> IO ExitCode
62 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
66 #endif /* __GLASGOW_HASKELL__ */
69 The computation @'rawSystem' cmd args@ runs the operating system command
70 @cmd@ in such a way that it receives as arguments the @args@ strings
71 exactly as given, with no funny escaping or shell meta-syntax expansion.
72 It will therefore behave more portably between operating systems than 'system'.
74 The return codes and possible failures are the same as for 'system'.
76 rawSystem :: String -> [String] -> IO ExitCode
77 #ifdef __GLASGOW_HASKELL__
78 rawSystem cmd args = do
79 p <- runProcess cmd args Nothing Nothing Nothing Nothing Nothing
81 #else /* ! __GLASGOW_HASKELL__ */
82 -- crude fallback implementation: could do much better than this under Unix
83 rawSystem cmd args = system (unwords (map translate (cmd:args)))
85 translate :: String -> String
86 #if defined(mingw32_TARGET_OS)
87 -- copied from System.Process (qv)
88 translate str = '"' : snd (foldr escape (True,"\"") str)
89 where escape '"' (b, str) = (True, '\\' : '"' : str)
90 escape '\\' (True, str) = (True, '\\' : '\\' : str)
91 escape '\\' (False, str) = (False, '\\' : str)
92 escape c (b, str) = (False, c : str)
93 #else /* ! mingw32_TARGET_OS */
94 translate str = '\'' : foldr escape "'" str
95 where escape '\'' cs = '\'' : '\\' : '\'' : '\'' : cs
97 #endif /* ! mingw32_TARGET_OS */
98 #endif /* ! __GLASGOW_HASKELL__ */