1 {-# LANGUAGE CPP, ForeignFunctionInterface #-}
3 module GHC.Environment (getFullArgs) where
10 import GHC.IO (finally)
13 -- Ignore the arguments to hs_init on Windows for the sake of Unicode compat
14 getFullArgs :: IO [String]
16 p_arg_string <- c_GetCommandLine
17 alloca $ \p_argc -> do
18 p_argv <- c_CommandLineToArgv p_arg_string p_argc
20 then throwGetLastError "getFullArgs"
21 else flip finally (c_LocalFree p_argv) $ do
23 p_argvs <- peekArray (fromIntegral argc) p_argv
24 mapM peekCWString p_argvs
26 foreign import stdcall unsafe "windows.h GetCommandLineW"
27 c_GetCommandLine :: IO (Ptr CWString)
29 foreign import stdcall unsafe "windows.h CommandLineToArgvW"
30 c_CommandLineToArgv :: Ptr CWString -> Ptr CInt -> IO (Ptr CWString)
32 foreign import stdcall unsafe "Windows.h LocalFree"
33 c_LocalFree :: Ptr a -> IO (Ptr a)
37 import GHC.IO.Encoding
38 import qualified GHC.Foreign as GHC
40 getFullArgs :: IO [String]
43 alloca $ \ p_argv -> do
44 getFullProgArgv p_argc p_argv
45 p <- fromIntegral `liftM` peek p_argc
47 peekArray (p - 1) (advancePtr argv 1) >>= mapM (GHC.peekCString fileSystemEncoding)
49 foreign import ccall unsafe "getFullProgArgv"
50 getFullProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()