|
| 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 | + |
| 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 | + , putEnv |
| 24 | + , setEnv |
| 25 | + , unsetEnv |
| 26 | + |
| 27 | + -- * Program arguments |
| 28 | + , getArgs |
| 29 | +) where |
| 30 | + |
| 31 | +#include "HsUnix.h" |
| 32 | + |
| 33 | +import Foreign |
| 34 | +import Foreign.C |
| 35 | +import Data.Maybe ( fromMaybe ) |
| 36 | + |
| 37 | +import System.AbstractFilePath.Data.ByteString.Short.Decode (decodeUtf8With, lenientDecode) |
| 38 | +import System.OsString.Internal.Types |
| 39 | +import qualified Data.ByteString.Short as B |
| 40 | + |
| 41 | +-- |'getEnv' looks up a variable in the environment. |
| 42 | + |
| 43 | +getEnv :: |
| 44 | + PosixString {- ^ variable name -} -> |
| 45 | + IO (Maybe PosixString) {- ^ variable value -} |
| 46 | +getEnv (PS name) = do |
| 47 | + litstring <- B.useAsCString name c_getenv |
| 48 | + if litstring /= nullPtr |
| 49 | + then (Just . PS) <$> B.packCString litstring |
| 50 | + else return Nothing |
| 51 | + |
| 52 | +-- |'getEnvDefault' is a wrapper around 'getEnv' where the |
| 53 | +-- programmer can specify a fallback as the second argument, which will be |
| 54 | +-- used if the variable is not found in the environment. |
| 55 | + |
| 56 | +getEnvDefault :: |
| 57 | + PosixString {- ^ variable name -} -> |
| 58 | + PosixString {- ^ fallback value -} -> |
| 59 | + IO PosixString {- ^ variable value or fallback value -} |
| 60 | +getEnvDefault name fallback = fromMaybe fallback <$> getEnv name |
| 61 | + |
| 62 | +foreign import ccall unsafe "getenv" |
| 63 | + c_getenv :: CString -> IO CString |
| 64 | + |
| 65 | +getEnvironmentPrim :: IO [PosixString] |
| 66 | +getEnvironmentPrim = do |
| 67 | + c_environ <- getCEnviron |
| 68 | + arr <- peekArray0 nullPtr c_environ |
| 69 | + mapM (fmap PS . B.packCString) arr |
| 70 | + |
| 71 | +getCEnviron :: IO (Ptr CString) |
| 72 | +#if HAVE__NSGETENVIRON |
| 73 | +-- You should not access @char **environ@ directly on Darwin in a bundle/shared library. |
| 74 | +-- See #2458 and http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man7/environ.7.html |
| 75 | +getCEnviron = nsGetEnviron >>= peek |
| 76 | + |
| 77 | +foreign import ccall unsafe "_NSGetEnviron" |
| 78 | + nsGetEnviron :: IO (Ptr (Ptr CString)) |
| 79 | +#else |
| 80 | +getCEnviron = peek c_environ_p |
| 81 | + |
| 82 | +foreign import ccall unsafe "&environ" |
| 83 | + c_environ_p :: Ptr (Ptr CString) |
| 84 | +#endif |
| 85 | + |
| 86 | +-- |'getEnvironment' retrieves the entire environment as a |
| 87 | +-- list of @(key,value)@ pairs. |
| 88 | + |
| 89 | +getEnvironment :: IO [(PosixString,PosixString)] {- ^ @[(key,value)]@ -} |
| 90 | +getEnvironment = do |
| 91 | + env <- getEnvironmentPrim |
| 92 | + return $ map (dropEq . (B.break ((==) _equal)) . unPFP) env |
| 93 | + where |
| 94 | + dropEq (x,y) |
| 95 | + | B.head y == _equal = (PS x, PS (B.tail y)) |
| 96 | + | otherwise = error $ "getEnvironment: insane variable " ++ _toStr x |
| 97 | + |
| 98 | +-- |The 'unsetEnv' function deletes all instances of the variable name |
| 99 | +-- from the environment. |
| 100 | + |
| 101 | +unsetEnv :: PosixString {- ^ variable name -} -> IO () |
| 102 | +#if HAVE_UNSETENV |
| 103 | +# if !UNSETENV_RETURNS_VOID |
| 104 | +unsetEnv (PS name) = B.useAsCString name $ \ s -> |
| 105 | + throwErrnoIfMinus1_ "unsetenv" (c_unsetenv s) |
| 106 | + |
| 107 | +-- POSIX.1-2001 compliant unsetenv(3) |
| 108 | +foreign import capi unsafe "HsUnix.h unsetenv" |
| 109 | + c_unsetenv :: CString -> IO CInt |
| 110 | +# else |
| 111 | +unsetEnv name = B.useAsCString name c_unsetenv |
| 112 | + |
| 113 | +-- pre-POSIX unsetenv(3) returning @void@ |
| 114 | +foreign import capi unsafe "HsUnix.h unsetenv" |
| 115 | + c_unsetenv :: CString -> IO () |
| 116 | +# endif |
| 117 | +#else |
| 118 | +unsetEnv name = putEnv (name ++ "=") |
| 119 | +#endif |
| 120 | + |
| 121 | +-- |'putEnv' function takes an argument of the form @name=value@ |
| 122 | +-- and is equivalent to @setEnv(key,value,True{-overwrite-})@. |
| 123 | + |
| 124 | +putEnv :: PosixString {- ^ "key=value" -} -> IO () |
| 125 | +putEnv (PS keyvalue) = B.useAsCString keyvalue $ \s -> |
| 126 | + throwErrnoIfMinus1_ "putenv" (c_putenv s) |
| 127 | + |
| 128 | +foreign import ccall unsafe "putenv" |
| 129 | + c_putenv :: CString -> IO CInt |
| 130 | + |
| 131 | +{- |The 'setEnv' function inserts or resets the environment variable name in |
| 132 | + the current environment list. If the variable @name@ does not exist in the |
| 133 | + list, it is inserted with the given value. If the variable does exist, |
| 134 | + the argument @overwrite@ is tested; if @overwrite@ is @False@, the variable is |
| 135 | + not reset, otherwise it is reset to the given value. |
| 136 | +-} |
| 137 | + |
| 138 | +setEnv :: |
| 139 | + PosixString {- ^ variable name -} -> |
| 140 | + PosixString {- ^ variable value -} -> |
| 141 | + Bool {- ^ overwrite -} -> |
| 142 | + IO () |
| 143 | +#ifdef HAVE_SETENV |
| 144 | +setEnv (PS key) (PS value) ovrwrt = do |
| 145 | + B.useAsCString key $ \ keyP -> |
| 146 | + B.useAsCString value $ \ valueP -> |
| 147 | + throwErrnoIfMinus1_ "setenv" $ |
| 148 | + c_setenv keyP valueP (fromIntegral (fromEnum ovrwrt)) |
| 149 | + |
| 150 | +foreign import ccall unsafe "setenv" |
| 151 | + c_setenv :: CString -> CString -> CInt -> IO CInt |
| 152 | +#else |
| 153 | +setEnv key value True = putEnv (key++"="++value) |
| 154 | +setEnv key value False = do |
| 155 | + res <- getEnv key |
| 156 | + case res of |
| 157 | + Just _ -> return () |
| 158 | + Nothing -> putEnv (key++"="++value) |
| 159 | +#endif |
| 160 | + |
| 161 | +-- | Computation 'getArgs' returns a list of the program's command |
| 162 | +-- line arguments (not including the program name), as 'PosixString's. |
| 163 | +-- |
| 164 | +-- Unlike 'System.Environment.getArgs', this function does no Unicode |
| 165 | +-- decoding of the arguments; you get the exact bytes that were passed |
| 166 | +-- to the program by the OS. To interpret the arguments as text, some |
| 167 | +-- Unicode decoding should be applied. |
| 168 | +-- |
| 169 | +getArgs :: IO [PosixString] |
| 170 | +getArgs = |
| 171 | + alloca $ \ p_argc -> |
| 172 | + alloca $ \ p_argv -> do |
| 173 | + getProgArgv p_argc p_argv |
| 174 | + p <- fromIntegral <$> peek p_argc |
| 175 | + argv <- peek p_argv |
| 176 | + peekArray (p - 1) (advancePtr argv 1) >>= mapM (fmap PS . B.packCString) |
| 177 | + |
| 178 | +foreign import ccall unsafe "getProgArgv" |
| 179 | + getProgArgv :: Ptr CInt -> Ptr (Ptr CString) -> IO () |
| 180 | + |
| 181 | +_equal :: Word8 |
| 182 | +_equal = 0x3d |
| 183 | + |
| 184 | +_toStr :: B.ShortByteString -> String |
| 185 | +_toStr = decodeUtf8With lenientDecode |
| 186 | + |
0 commit comments