{-# LINE 1 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LANGUAGE CPP #-}
module System.Environment.ExecutablePath
( getExecutablePath
#if !defined(javascript_HOST_ARCH)
, executablePath
#endif
) where
#if defined(javascript_HOST_ARCH)
getExecutablePath :: IO FilePath
getExecutablePath = return "a.jsexe"
#else
{-# LINE 72 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
import Foreign.C
import Foreign.Marshal.Alloc
import Foreign.Ptr
import Foreign.Storable
import System.Posix.Internals
{-# LINE 78 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
getExecutablePath :: IO FilePath
executablePath :: Maybe (IO (Maybe FilePath))
{-# LINE 362 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
foreign import ccall unsafe "getFullProgArgv"
c_getFullProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()
getExecutablePath :: IO FilePath
getExecutablePath =
(Ptr CInt -> IO FilePath) -> IO FilePath
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO FilePath) -> IO FilePath)
-> (Ptr CInt -> IO FilePath) -> IO FilePath
forall a b. (a -> b) -> a -> b
$ \ Ptr CInt
p_argc ->
(Ptr (Ptr CString) -> IO FilePath) -> IO FilePath
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr CString) -> IO FilePath) -> IO FilePath)
-> (Ptr (Ptr CString) -> IO FilePath) -> IO FilePath
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr CString)
p_argv -> do
Ptr CInt -> Ptr (Ptr CString) -> IO ()
c_getFullProgArgv Ptr CInt
p_argc Ptr (Ptr CString)
p_argv
CInt
argc <- Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
p_argc
if CInt
argc CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
> CInt
0
then Ptr (Ptr CString) -> IO (Ptr CString)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CString)
p_argv IO (Ptr CString) -> (Ptr CString -> IO CString) -> IO CString
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Ptr CString -> IO CString
forall a. Storable a => Ptr a -> IO a
peek IO CString -> (CString -> IO FilePath) -> IO FilePath
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= CString -> IO FilePath
peekFilePath
else FilePath -> IO FilePath
forall a. FilePath -> a
errorWithoutStackTrace (FilePath -> IO FilePath) -> FilePath -> IO FilePath
forall a b. (a -> b) -> a -> b
$ FilePath
"getExecutablePath: " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++ FilePath
msg
where msg :: FilePath
msg = FilePath
"no OS specific implementation and program name couldn't be " FilePath -> FilePath -> FilePath
forall a. [a] -> [a] -> [a]
++
FilePath
"found in argv"
executablePath :: Maybe (IO (Maybe FilePath))
executablePath = Maybe (IO (Maybe FilePath))
forall a. Maybe a
Nothing
{-# LINE 384 "libraries/base/System/Environment/ExecutablePath.hsc" #-}
#endif