Permalink
Browse files

Add a cross-platform setenv implementation.

This is a simplified version of Simon Hengel's patch for the 'base' library [1]
(without unsetEnv and the unix dependency). Tested on Windows and Linux.

[1] http://thread.gmane.org/gmane.comp.lang.haskell.libraries/18001
  • Loading branch information...
1 parent 330f08c commit c70913b6e953d649cbf9e58cd7ae73df6056eb69 @23Skidoo committed Nov 17, 2012
Showing with 97 additions and 0 deletions.
  1. +96 −0 cabal-install/Distribution/Compat/SetEnv.hs
  2. +1 −0 cabal-install/cabal-install.cabal
@@ -0,0 +1,96 @@
+{-# LANGUAGE CPP, ForeignFunctionInterface #-}
+
+-----------------------------------------------------------------------------
+-- |
+-- Module : Distribution.Compat.SetEnv
+-- Copyright : (c) Simon Hengel 2012
+-- License : BSD-style (see the file LICENSE)
+--
+-- Maintainer : cabal-devel@haskell.org
+-- Stability : provisional
+-- Portability : portable
+--
+-- A cross-platform library for setting environment variables.
+--
+-----------------------------------------------------------------------------
+
+module Distribution.Compat.SetEnv (
+ setEnv
+) where
+
+#ifdef __GLASGOW_HASKELL__
+#ifdef mingw32_HOST_OS
+import GHC.Windows
+import Foreign.Safe
+import Foreign.C
+import Control.Monad
+#else
+import Foreign.C.Types
+import Foreign.C.String
+import Foreign.C.Error (throwErrnoIfMinus1_)
+#endif /* mingw32_HOST_OS */
+
+#if __GLASGOW_HASKELL__ > 611
+import System.Posix.Internals ( withFilePath )
+#else
+withFilePath :: FilePath -> (CString -> IO a) -> IO a
+withFilePath = withCString
+#endif /* __GLASGOW_HASKELL__ > 611 */
+
+#ifdef mingw32_HOST_OS
+# if defined(i386_HOST_ARCH)
+# define WINDOWS_CCONV stdcall
+# elif defined(x86_64_HOST_ARCH)
+# define WINDOWS_CCONV ccall
+# else
+# error Unknown mingw32 arch
+# endif
+
+foreign import WINDOWS_CCONV unsafe "windows.h GetLastError"
+ c_GetLastError:: IO DWORD
+
+eRROR_ENVVAR_NOT_FOUND :: DWORD
+eRROR_ENVVAR_NOT_FOUND = 203
+
+#endif /* mingw32_HOST_OS */
+#endif /* __GLASGOW_HASKELL__ */
+
+-- | @setEnv name value@ sets the specified environment variable to @value@.
+--
+-- Throws `Control.Exception.IOException` if either @name@ or @value@ is the
+-- empty string or contains an equals sign.
+setEnv :: String -> String -> IO ()
+setEnv key value_
+ | null value = error "Distribuiton.Compat.setEnv: empty string"
+ | otherwise = setEnv_ key value
+ where
+ -- NOTE: Anything that follows NUL is ignored on both POSIX and Windows. We
+ -- still strip it manually so that the null check above succeds if a value
+ -- starts with NUL.
+ value = takeWhile (/= '\NUL') value_
+
+setEnv_ :: String -> String -> IO ()
+#ifdef __GLASGOW_HASKELL__
+
+#ifdef mingw32_HOST_OS
+setEnv_ key value = withCWString key $ \k -> withCWString value $ \v -> do
+ success <- c_SetEnvironmentVariable k v
+ unless success (throwGetLastError "setEnv")
+
+foreign import WINDOWS_CCONV unsafe "windows.h SetEnvironmentVariableW"
+ c_SetEnvironmentVariable :: LPTSTR -> LPTSTR -> IO Bool
+#else
+setEnv_ key value = do
+ withFilePath key $ \ keyP ->
+ withFilePath value $ \ valueP ->
+ throwErrnoIfMinus1_ "setenv" $
+ c_setenv keyP valueP (fromIntegral (fromEnum True))
+
+foreign import ccall unsafe "setenv"
+ c_setenv :: CString -> CString -> CInt -> IO CInt
+#endif /* mingw32_HOST_OS */
+
+#else
+-- setEnv is a no-op on non-GHC compilers since we depend on GHC.Windows.
+setEnv_ _key _value = return ()
+#endif /* __GLASGOW_HASKELL__ */
@@ -109,6 +109,7 @@ Executable cabal
Distribution.Client.Win32SelfUpgrade
Distribution.Compat.Exception
Distribution.Compat.FilePerms
+ Distribution.Compat.SetEnv
Distribution.Compat.Time
Paths_cabal_install

0 comments on commit c70913b

Please sign in to comment.