{-# LINE 1 "libraries/unix/System/Posix/Env.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LINE 3 "libraries/unix/System/Posix/Env.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 7 "libraries/unix/System/Posix/Env.hsc" #-}
module System.Posix.Env (
getEnv
, getEnvDefault
, getEnvironmentPrim
, getEnvironment
, setEnvironment
, putEnv
, setEnv
, unsetEnv
, clearEnv
) where
import Foreign.C.Error (throwErrnoIfMinus1_)
import Foreign.C.Types
import Foreign.C.String
import Foreign.Marshal.Array
import Foreign.Ptr
import Foreign.Storable
import Control.Monad
import Data.Maybe (fromMaybe)
import System.Posix.Internals
{-# LINE 50 "libraries/unix/System/Posix/Env.hsc" #-}
getEnv ::
String ->
IO (Maybe String)
getEnv :: String -> IO (Maybe String)
getEnv String
name = do
CString
litstring <- String -> (CString -> IO CString) -> IO CString
forall a. String -> (CString -> IO a) -> IO a
withFilePath String
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 (String -> Maybe String) -> IO String -> IO (Maybe String)
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM String -> Maybe String
forall a. a -> Maybe a
Just (IO String -> IO (Maybe String)) -> IO String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ CString -> IO String
peekFilePath CString
litstring
else Maybe String -> IO (Maybe String)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing
getEnvDefault ::
String ->
String ->
IO String
getEnvDefault :: String -> String -> IO String
getEnvDefault String
name String
fallback = (Maybe String -> String) -> IO (Maybe String) -> IO String
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM (String -> Maybe String -> String
forall a. a -> Maybe a -> a
fromMaybe String
fallback) (String -> IO (Maybe String)
getEnv String
name)
foreign import ccall unsafe "getenv"
c_getenv :: CString -> IO CString
getEnvironmentPrim :: IO [String]
getEnvironmentPrim :: IO [String]
getEnvironmentPrim = do
Ptr CString
c_environ <- IO (Ptr CString)
getCEnviron
if Ptr CString
c_environ Ptr CString -> Ptr CString -> Bool
forall a. Eq a => a -> a -> Bool
== Ptr CString
forall a. Ptr a
nullPtr
then [String] -> IO [String]
forall (m :: * -> *) a. Monad m => a -> m a
return []
else do
[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 String) -> [CString] -> IO [String]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM CString -> IO String
peekFilePath [CString]
arr
getCEnviron :: IO (Ptr CString)
{-# LINE 94 "libraries/unix/System/Posix/Env.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 98 "libraries/unix/System/Posix/Env.hsc" #-}
getEnvironment :: IO [(String,String)]
getEnvironment :: IO [(String, String)]
getEnvironment = do
[String]
env <- IO [String]
getEnvironmentPrim
[(String, String)] -> IO [(String, String)]
forall (m :: * -> *) a. Monad m => a -> m a
return ([(String, String)] -> IO [(String, String)])
-> [(String, String)] -> IO [(String, String)]
forall a b. (a -> b) -> a -> b
$ (String -> (String, String)) -> [String] -> [(String, String)]
forall a b. (a -> b) -> [a] -> [b]
map ((String, String) -> (String, String)
dropEq((String, String) -> (String, String))
-> (String -> (String, String)) -> String -> (String, String)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.((Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
(==) Char
'='))) [String]
env
where
dropEq :: (String, String) -> (String, String)
dropEq (String
x,Char
'=':String
ys) = (String
x,String
ys)
dropEq (String
x,String
_) = String -> (String, String)
forall a. HasCallStack => String -> a
error (String -> (String, String)) -> String -> (String, String)
forall a b. (a -> b) -> a -> b
$ String
"getEnvironment: insane variable " String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
x
setEnvironment ::
[(String,String)] ->
IO ()
setEnvironment :: [(String, String)] -> IO ()
setEnvironment [(String, String)]
env = do
IO ()
clearEnv
[(String, String)] -> ((String, String) -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(String, String)]
env (((String, String) -> IO ()) -> IO ())
-> ((String, String) -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \(String
key,String
value) ->
String -> String -> Bool -> IO ()
setEnv String
key String
value Bool
True
unsetEnv :: String -> IO ()
{-# LINE 126 "libraries/unix/System/Posix/Env.hsc" #-}
unsetEnv :: String -> IO ()
{-# LINE 127 "libraries/unix/System/Posix/Env.hsc" #-}
unsetEnv name = withFilePath name $ \ s ->
throwErrnoIfMinus1_ "unsetenv" (c_unsetenv s)
foreign import capi unsafe "HsUnix.h unsetenv"
c_unsetenv :: CString -> IO CInt
{-# LINE 140 "libraries/unix/System/Posix/Env.hsc" #-}
{-# LINE 143 "libraries/unix/System/Posix/Env.hsc" #-}
putEnv :: String -> IO ()
putEnv :: String -> IO ()
putEnv String
keyvalue = do CString
s <- String -> IO CString
newFilePath String
keyvalue
String -> IO CInt -> IO ()
forall a. (Eq a, Num a) => String -> IO a -> IO ()
throwErrnoIfMinus1_ String
"putenv" (CString -> IO CInt
c_putenv CString
s)
{-# LINE 158 "libraries/unix/System/Posix/Env.hsc" #-}
foreign import ccall unsafe "putenv"
c_putenv :: CString -> IO CInt
setEnv ::
String ->
String ->
Bool ->
IO ()
{-# LINE 175 "libraries/unix/System/Posix/Env.hsc" #-}
setEnv key value ovrwrt = do
withFilePath key $ \ keyP ->
withFilePath value $ \ valueP ->
throwErrnoIfMinus1_ "setenv" $
c_setenv keyP valueP (fromIntegral (fromEnum ovrwrt))
foreign import ccall unsafe "setenv"
c_setenv :: CString -> CString -> CInt -> IO CInt
{-# LINE 191 "libraries/unix/System/Posix/Env.hsc" #-}
clearEnv :: IO ()
clearEnv :: IO ()
{-# LINE 195 "libraries/unix/System/Posix/Env.hsc" #-}
clearEnv = void c_clearenv
foreign import ccall unsafe "clearenv"
c_clearenv :: IO Int
{-# LINE 206 "libraries/unix/System/Posix/Env.hsc" #-}