[project @ 2004-12-03 14:08:07 by ross]
[packages/random.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, -- :: FilePath -> [String] -> IO ExitCode
18 ) where
19
20 import Prelude
21
22 import System.Exit ( ExitCode )
23
24 #ifdef __GLASGOW_HASKELL__
25 import System.Process
26 import GHC.IOBase ( ioException, IOException(..), IOErrorType(..) )
27 #endif
28
29 #ifdef __HUGS__
30 import Hugs.System
31 #endif
32
33 #ifdef __NHC__
34 import System (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 #ifdef __GLASGOW_HASKELL__
61 system :: String -> IO ExitCode
62 system "" = ioException (IOError Nothing InvalidArgument "system" "null command" Nothing)
63 system cmd = do
64 p <- runCommand cmd
65 waitForProcess p
66 #endif /* __GLASGOW_HASKELL__ */
67
68 {-|
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'.
73
74 The return codes and possible failures are the same as for 'system'.
75 -}
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
80 waitForProcess p
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)))
84
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
96 escape c cs = c : cs
97 #endif /* ! mingw32_TARGET_OS */
98 #endif /* ! __GLASGOW_HASKELL__ */