|
| 1 | +{-# LANGUAGE CApiFFI #-} |
| 2 | + |
| 3 | +----------------------------------------------------------------------------- |
| 4 | +-- | |
| 5 | +-- Module : System.Posix.Env.PosixString |
| 6 | +-- Copyright : (c) The University of Glasgow 2002 |
| 7 | +-- License : BSD-style (see the file libraries/base/LICENSE) |
| 8 | +-- |
| 9 | +-- Maintainer : libraries@haskell.org |
| 10 | +-- Stability : provisional |
| 11 | +-- Portability : non-portable (requires POSIX) |
| 12 | +-- |
| 13 | +-- POSIX environment support |
| 14 | +-- |
| 15 | +----------------------------------------------------------------------------- |
| 16 | + |
| 17 | +module System.Posix.Env.PosixString ( |
| 18 | + -- * Environment Variables |
| 19 | + getEnv |
| 20 | + , getEnvDefault |
| 21 | + , getEnvironmentPrim |
| 22 | + , getEnvironment |
| 23 | + , setEnvironment |
| 24 | + , putEnv |
| 25 | + , setEnv |
| 26 | + , unsetEnv |
| 27 | + , clearEnv |
| 28 | + |
| 29 | + -- * Program arguments |
| 30 | + , getArgs |
| 31 | +) where |
| 32 | + |
| 33 | +#include "HsUnix.h" |
| 34 | + |
| 35 | +import Control.Monad |
| 36 | +import Foreign |
| 37 | +import Foreign.C |
| 38 | +import Data.Maybe ( fromMaybe ) |
| 39 | + |
| 40 | +import GHC.IO.Encoding.UTF8 ( mkUTF8 ) |
| 41 | +import GHC.IO.Encoding.Failure ( CodingFailureMode(..) ) |
| 42 | +import System.Posix.Env ( clearEnv ) |
| 43 | +import System.OsPath.Posix |
| 44 | +import System.OsString.Internal.Types |
| 45 | +import qualified System.OsPath.Data.ByteString.Short as B |
| 46 | +import Data.ByteString.Short.Internal ( copyToPtr ) |
| 47 | + |
| 48 | +-- |'getEnv' looks up a variable in the environment. |
| 49 | + |
| 50 | +getEnv :: |
| 51 | + PosixString {- ^ variable name -} -> |
| 52 | + IO (Maybe PosixString) {- ^ variable value -} |
| 53 | +getEnv (PS name) = do |
| 54 | + litstring <- B.useAsCString name c_getenv |
| 55 | + if litstring /= nullPtr |
| 56 | + then (Just . PS) <$> B.packCString litstring |
| 57 | + else return Nothing |
| 58 | + |
| 59 | +-- |'getEnvDefault' is a wrapper around 'getEnv' where the |
| 60 | +-- programmer can specify a fallback as the second argument, which will be |
| 61 | +-- used if the variable is not found in the environment. |
| 62 | + |
| 63 | +getEnvDefault :: |
| 64 | + PosixString {- ^ variable name -} -> |
| 65 | + PosixString {- ^ fallback value -} -> |
| 66 | + IO PosixString {- ^ variable value or fallback value -} |
| 67 | +getEnvDefault name fallback = fromMaybe fallback <$> getEnv name |
| 68 | + |
| 69 | +foreign import ccall unsafe "getenv" |
| 70 | + c_getenv :: CString -> IO CString |
| 71 | + |
| 72 | +getEnvironmentPrim :: IO [PosixString] |
| 73 | +getEnvironmentPrim = do |
| 74 | + c_environ <- getCEnviron |
| 75 | + arr <- peekArray0 nullPtr c_environ |
| 76 | + mapM (fmap PS . B.packCString) arr |
| 77 | + |
| 78 | +getCEnviron :: IO (Ptr CString) |
| 79 | +#if HAVE__NSGETENVIRON |
| 80 | +-- You should not access @char **environ@ directly on Darwin in a bundle/shared library. |
| 81 | +-- See #2458 and http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man7/environ.7.html |
| 82 | +getCEnviron = nsGetEnviron >>= peek |
| 83 | + |
| 84 | +foreign import ccall unsafe "_NSGetEnviron" |
| 85 | + nsGetEnviron :: IO (Ptr (Ptr CString)) |
| 86 | +#else |
| 87 | +getCEnviron = peek c_environ_p |
| 88 | + |
| 89 | +foreign import ccall unsafe "&environ" |
| 90 | + c_environ_p :: Ptr (Ptr CString) |
| 91 | +#endif |
| 92 | + |
| 93 | +-- |'getEnvironment' retrieves the entire environment as a |
| 94 | +-- list of @(key,value)@ pairs. |
| 95 | + |
| 96 | +getEnvironment :: IO [(PosixString,PosixString)] {- ^ @[(key,value)]@ -} |
| 97 | +getEnvironment = do |
| 98 | + env <- getEnvironmentPrim |
| 99 | + return $ map (dropEq . (B.break ((==) _equal)) . getPosixString) env |
| 100 | + where |
| 101 | + dropEq (x,y) |
| 102 | + | B.head y == _equal = (PS x, PS (B.tail y)) |
| 103 | + | otherwise = error $ "getEnvironment: insane variable " ++ _toStr x |
| 104 | + |
| 105 | +-- |'setEnvironment' resets the entire environment to the given list of |
| 106 | +-- @(key,value)@ pairs. |
| 107 | +setEnvironment :: |
| 108 | + [(PosixString,PosixString)] {- ^ @[(key,value)]@ -} -> |
| 109 | + IO () |
| 110 | +setEnvironment env = do |
| 111 | + clearEnv |
| 112 | + forM_ env $ \(key,value) -> |
| 113 | + setEnv key value True {-overwrite-} |
| 114 | + |
| 115 | +-- |The 'unsetEnv' function deletes all instances of the variable name |
| 116 | +-- from the environment. |
| 117 | + |
| 118 | +unsetEnv :: PosixString {- ^ variable name -} -> IO () |
| 119 | +#if HAVE_UNSETENV |
| 120 | +# if !UNSETENV_RETURNS_VOID |
| 121 | +unsetEnv (PS name) = B.useAsCString name $ \ s -> |
| 122 | + throwErrnoIfMinus1_ "unsetenv" (c_unsetenv s) |
| 123 | + |
| 124 | +-- POSIX.1-2001 compliant unsetenv(3) |
| 125 | +foreign import capi unsafe "HsUnix.h unsetenv" |
| 126 | + c_unsetenv :: CString -> IO CInt |
| 127 | +# else |
| 128 | +unsetEnv name = B.useAsCString name c_unsetenv |
| 129 | + |
| 130 | +-- pre-POSIX unsetenv(3) returning @void@ |
| 131 | +foreign import capi unsafe "HsUnix.h unsetenv" |
| 132 | + c_unsetenv :: CString -> IO () |
| 133 | +# endif |
| 134 | +#else |
| 135 | +unsetEnv name = putEnv (name <> PosixString (B.pack "=")) |
| 136 | +#endif |
| 137 | + |
| 138 | +-- |'putEnv' function takes an argument of the form @name=value@ |
| 139 | +-- and is equivalent to @setEnv(key,value,True{-overwrite-})@. |
| 140 | +putEnv :: PosixString {- ^ "key=value" -} -> IO () |
| 141 | +putEnv (PS sbs) = do |
| 142 | + buf <- mallocBytes (l+1) |
| 143 | + copyToPtr sbs 0 buf (fromIntegral l) |
| 144 | + pokeByteOff buf l (0::Word8) |
| 145 | + throwErrnoIfMinus1_ "putenv" (c_putenv buf) |
| 146 | + where l = B.length sbs |
| 147 | + |
| 148 | + |
| 149 | +foreign import ccall unsafe "putenv" |
| 150 | + c_putenv :: CString -> IO CInt |
| 151 | + |
| 152 | +{- |The 'setEnv' function inserts or resets the environment variable name in |
| 153 | + the current environment list. If the variable @name@ does not exist in the |
| 154 | + list, it is inserted with the given value. If the variable does exist, |
| 155 | + the argument @overwrite@ is tested; if @overwrite@ is @False@, the variable is |
| 156 | + not reset, otherwise it is reset to the given value. |
| 157 | +-} |
| 158 | + |
| 159 | +setEnv :: |
| 160 | + PosixString {- ^ variable name -} -> |
| 161 | + PosixString {- ^ variable value -} -> |
| 162 | + Bool {- ^ overwrite -} -> |
| 163 | + IO () |
| 164 | +#ifdef HAVE_SETENV |
| 165 | +setEnv (PS key) (PS value) ovrwrt = do |
| 166 | + B.useAsCString key $ \ keyP -> |
| 167 | + B.useAsCString value $ \ valueP -> |
| 168 | + throwErrnoIfMinus1_ "setenv" $ |
| 169 | + c_setenv keyP valueP (fromIntegral (fromEnum ovrwrt)) |
| 170 | + |
| 171 | +foreign import ccall unsafe "setenv" |
| 172 | + c_setenv :: CString -> CString -> CInt -> IO CInt |
| 173 | +#else |
| 174 | +setEnv key value True = putEnv (key++"="++value) |
| 175 | +setEnv key value False = do |
| 176 | + res <- getEnv key |
| 177 | + case res of |
| 178 | + Just _ -> return () |
| 179 | + Nothing -> putEnv (key++"="++value) |
| 180 | +#endif |
| 181 | + |
| 182 | +-- | Computation 'getArgs' returns a list of the program's command |
| 183 | +-- line arguments (not including the program name), as 'PosixString's. |
| 184 | +-- |
| 185 | +-- Unlike 'System.Environment.getArgs', this function does no Unicode |
| 186 | +-- decoding of the arguments; you get the exact bytes that were passed |
| 187 | +-- to the program by the OS. To interpret the arguments as text, some |
| 188 | +-- Unicode decoding should be applied. |
| 189 | +-- |
| 190 | +getArgs :: IO [PosixString] |
| 191 | +getArgs = |
| 192 | + alloca $ \ p_argc -> |
| 193 | + alloca $ \ p_argv -> do |
| 194 | + getProgArgv p_argc p_argv |
| 195 | + p <- fromIntegral <$> peek p_argc |
| 196 | + argv <- peek p_argv |
| 197 | + peekArray (p - 1) (advancePtr argv 1) >>= mapM (fmap PS . B.packCString) |
| 198 | + |
| 199 | +foreign import ccall unsafe "getProgArgv" |
| 200 | + getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO () |
| 201 | + |
| 202 | +_equal :: Word8 |
| 203 | +_equal = 0x3d |
| 204 | + |
| 205 | +_toStr :: B.ShortByteString -> String |
| 206 | +_toStr = either (error . show) id . decodeWith (mkUTF8 TransliterateCodingFailure) . PosixString |
0 commit comments