Improve list fusion for [n::Integer..m]
[packages/base.git] / GHC / Environment.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP #-}
3
4 module GHC.Environment (getFullArgs) where
5
6 import Prelude
7 import Foreign
8 import Foreign.C
9
10 #ifdef mingw32_HOST_OS
11 import GHC.IO (finally)
12 import GHC.Windows
13
14 # if defined(i386_HOST_ARCH)
15 # define WINDOWS_CCONV stdcall
16 # elif defined(x86_64_HOST_ARCH)
17 # define WINDOWS_CCONV ccall
18 # else
19 # error Unknown mingw32 arch
20 # endif
21
22 -- Ignore the arguments to hs_init on Windows for the sake of Unicode compat
23 getFullArgs :: IO [String]
24 getFullArgs = do
25 p_arg_string <- c_GetCommandLine
26 alloca $ \p_argc -> do
27 p_argv <- c_CommandLineToArgv p_arg_string p_argc
28 if p_argv == nullPtr
29 then throwGetLastError "getFullArgs"
30 else flip finally (c_LocalFree p_argv) $ do
31 argc <- peek p_argc
32 p_argvs <- peekArray (fromIntegral argc) p_argv
33 mapM peekCWString p_argvs
34
35 foreign import WINDOWS_CCONV unsafe "windows.h GetCommandLineW"
36 c_GetCommandLine :: IO (Ptr CWString)
37
38 foreign import WINDOWS_CCONV unsafe "windows.h CommandLineToArgvW"
39 c_CommandLineToArgv :: Ptr CWString -> Ptr CInt -> IO (Ptr CWString)
40
41 foreign import WINDOWS_CCONV unsafe "Windows.h LocalFree"
42 c_LocalFree :: Ptr a -> IO (Ptr a)
43 #else
44 import Control.Monad
45
46 import GHC.IO.Encoding
47 import qualified GHC.Foreign as GHC
48
49 getFullArgs :: IO [String]
50 getFullArgs =
51 alloca $ \ p_argc ->
52 alloca $ \ p_argv -> do
53 getFullProgArgv p_argc p_argv
54 p <- fromIntegral `liftM` peek p_argc
55 argv <- peek p_argv
56 enc <- getFileSystemEncoding
57 peekArray (p - 1) (advancePtr argv 1) >>= mapM (GHC.peekCString enc)
58
59 foreign import ccall unsafe "getFullProgArgv"
60 getFullProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
61 #endif