{-# LINE 1 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Trustworthy #-}
{-# LINE 4 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
{-# OPTIONS_GHC -fno-warn-trustworthy-safe #-}
{-# LINE 6 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
module System.Posix.Env.ByteString (
getEnv
, getEnvDefault
, getEnvironmentPrim
, getEnvironment
, putEnv
, setEnv
, unsetEnv
, getArgs
) where
import Foreign
import Foreign.C
import Control.Monad ( liftM )
import Data.Maybe ( fromMaybe )
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Data.ByteString (ByteString)
getEnv ::
ByteString ->
IO (Maybe ByteString)
getEnv :: ByteString -> IO (Maybe ByteString)
getEnv ByteString
name = do
CString
litstring <- ByteString -> (CString -> IO CString) -> IO CString
forall a. ByteString -> (CString -> IO a) -> IO a
B.useAsCString ByteString
name CString -> IO CString
c_getenv
if CString
litstring CString -> CString -> Bool
forall a. Eq a => a -> a -> Bool
/= CString
forall a. Ptr a
nullPtr
then (ByteString -> Maybe ByteString)
-> IO ByteString -> IO (Maybe ByteString)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM ByteString -> Maybe ByteString
forall a. a -> Maybe a
Just (IO ByteString -> IO (Maybe ByteString))
-> IO ByteString -> IO (Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ CString -> IO ByteString
B.packCString CString
litstring
else Maybe ByteString -> IO (Maybe ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe ByteString
forall a. Maybe a
Nothing
getEnvDefault ::
ByteString ->
ByteString ->
IO ByteString
getEnvDefault :: ByteString -> ByteString -> IO ByteString
getEnvDefault ByteString
name ByteString
fallback = (Maybe ByteString -> ByteString)
-> IO (Maybe ByteString) -> IO ByteString
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ByteString
fallback) (ByteString -> IO (Maybe ByteString)
getEnv ByteString
name)
foreign import ccall unsafe "getenv"
c_getenv :: CString -> IO CString
getEnvironmentPrim :: IO [ByteString]
getEnvironmentPrim :: IO [ByteString]
getEnvironmentPrim = do
Ptr CString
c_environ <- IO (Ptr CString)
getCEnviron
[CString]
arr <- CString -> Ptr CString -> IO [CString]
forall a. (Storable a, Eq a) => a -> Ptr a -> IO [a]
peekArray0 CString
forall a. Ptr a
nullPtr Ptr CString
c_environ
(CString -> IO ByteString) -> [CString] -> IO [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM CString -> IO ByteString
B.packCString [CString]
arr
getCEnviron :: IO (Ptr CString)
{-# LINE 85 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
getCEnviron :: IO (Ptr CString)
getCEnviron = Ptr (Ptr CString) -> IO (Ptr CString)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CString)
c_environ_p
foreign import ccall unsafe "&environ"
c_environ_p :: Ptr (Ptr CString)
{-# LINE 90 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
getEnvironment :: IO [(ByteString,ByteString)]
getEnvironment :: IO [(ByteString, ByteString)]
getEnvironment = do
[ByteString]
env <- IO [ByteString]
getEnvironmentPrim
[(ByteString, ByteString)] -> IO [(ByteString, ByteString)]
forall (m :: * -> *) a. Monad m => a -> m a
return ([(ByteString, ByteString)] -> IO [(ByteString, ByteString)])
-> [(ByteString, ByteString)] -> IO [(ByteString, ByteString)]
forall a b. (a -> b) -> a -> b
$ (ByteString -> (ByteString, ByteString))
-> [ByteString] -> [(ByteString, ByteString)]
forall a b. (a -> b) -> [a] -> [b]
map ((ByteString, ByteString) -> (ByteString, ByteString)
dropEq((ByteString, ByteString) -> (ByteString, ByteString))
-> (ByteString -> (ByteString, ByteString))
-> ByteString
-> (ByteString, ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((Char -> Bool) -> ByteString -> (ByteString, ByteString)
BC.break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
(==) Char
'='))) [ByteString]
env
where
dropEq :: (ByteString, ByteString) -> (ByteString, ByteString)
dropEq (ByteString
x,ByteString
y)
| ByteString -> Char
BC.head ByteString
y Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'=' = (ByteString
x,ByteString -> ByteString
B.tail ByteString
y)
| Bool
otherwise = [Char] -> (ByteString, ByteString)
forall a. HasCallStack => [Char] -> a
error ([Char] -> (ByteString, ByteString))
-> [Char] -> (ByteString, ByteString)
forall a b. (a -> b) -> a -> b
$ [Char]
"getEnvironment: insane variable " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ ByteString -> [Char]
BC.unpack ByteString
x
unsetEnv :: ByteString -> IO ()
{-# LINE 108 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
unsetEnv :: ByteString -> IO ()
{-# LINE 109 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
unsetEnv name = B.useAsCString name $ \ s ->
throwErrnoIfMinus1_ "unsetenv" (c_unsetenv s)
foreign import capi unsafe "HsUnix.h unsetenv"
c_unsetenv :: CString -> IO CInt
{-# LINE 122 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
{-# LINE 125 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
putEnv :: ByteString -> IO ()
putEnv :: ByteString -> IO ()
putEnv ByteString
keyvalue = ByteString -> (CString -> IO ()) -> IO ()
forall a. ByteString -> (CString -> IO a) -> IO a
B.useAsCString ByteString
keyvalue ((CString -> IO ()) -> IO ()) -> (CString -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \CString
s ->
[Char] -> IO CInt -> IO ()
forall a. (Eq a, Num a) => [Char] -> IO a -> IO ()
throwErrnoIfMinus1_ [Char]
"putenv" (CString -> IO CInt
c_putenv CString
s)
foreign import ccall unsafe "putenv"
c_putenv :: CString -> IO CInt
setEnv ::
ByteString ->
ByteString ->
Bool ->
IO ()
{-# LINE 149 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
setEnv key value ovrwrt = do
B.useAsCString key $ \ keyP ->
B.useAsCString value $ \ valueP ->
throwErrnoIfMinus1_ "setenv" $
c_setenv keyP valueP (fromIntegral (fromEnum ovrwrt))
foreign import ccall unsafe "setenv"
c_setenv :: CString -> CString -> CInt -> IO CInt
{-# LINE 165 "libraries/unix/System/Posix/Env/ByteString.hsc" #-}
getArgs :: IO [ByteString]
getArgs :: IO [ByteString]
getArgs =
(Ptr CInt -> IO [ByteString]) -> IO [ByteString]
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr CInt -> IO [ByteString]) -> IO [ByteString])
-> (Ptr CInt -> IO [ByteString]) -> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ \ Ptr CInt
p_argc ->
(Ptr (Ptr CString) -> IO [ByteString]) -> IO [ByteString]
forall a b. Storable a => (Ptr a -> IO b) -> IO b
alloca ((Ptr (Ptr CString) -> IO [ByteString]) -> IO [ByteString])
-> (Ptr (Ptr CString) -> IO [ByteString]) -> IO [ByteString]
forall a b. (a -> b) -> a -> b
$ \ Ptr (Ptr CString)
p_argv -> do
Ptr CInt -> Ptr (Ptr CString) -> IO ()
getProgArgv Ptr CInt
p_argc Ptr (Ptr CString)
p_argv
Int
p <- CInt -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (CInt -> Int) -> IO CInt -> IO Int
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
`liftM` Ptr CInt -> IO CInt
forall a. Storable a => Ptr a -> IO a
peek Ptr CInt
p_argc
Ptr CString
argv <- Ptr (Ptr CString) -> IO (Ptr CString)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CString)
p_argv
Int -> Ptr CString -> IO [CString]
forall a. Storable a => Int -> Ptr a -> IO [a]
peekArray (Int
p Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) (Ptr CString -> Int -> Ptr CString
forall a. Storable a => Ptr a -> Int -> Ptr a
advancePtr Ptr CString
argv Int
1) IO [CString] -> ([CString] -> IO [ByteString]) -> IO [ByteString]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (CString -> IO ByteString) -> [CString] -> IO [ByteString]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM CString -> IO ByteString
B.packCString
foreign import ccall unsafe "getProgArgv"
getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO ()