Commit 5a60bdaf authored by bbr's avatar bbr
Browse files

set environment variables from within programs

- implemented using global states
parent 97c0bbcc
......@@ -49,12 +49,6 @@ prim_sleep = ioFunc1 (\t->system ("sleep "++show (t::Integer)) >> return ())
prim_exitWith :: Curry a => C_Int -> Result (C_IO a)
prim_exitWith e _ = C_IO (\ _ -> exitWith (fromCurry e) >>= return . IOVal)
prim_setEnviron :: C_String -> C_String -> Result (C_IO T0)
prim_setEnviron = error "setEnviron not implemented yet"
prim_unsetEnviron :: C_String -> Result (C_IO T0)
prim_unsetEnviron = error "unsetEnviron not implemented yet"
evalSpace :: a -> Result a
evalSpace = error "evalSpace is a no-no"
......
[ForFunction "getCPUTime",ForFunction "getElapsedTime",ForFunction "getArgs",ForFunction "prim_getEnviron",ForFunction "getHostname",ForFunction "getPID",ForFunction "getProgName",ForFunction "prim_system",ForFunction "prim_sleep",ForFunction "prim_exitWith"]
\ No newline at end of file
[ForFunction "getCPUTime"
,ForFunction "getElapsedTime"
,ForFunction "getArgs"
,ForFunction "prim_getEnviron"
,ForFunction "getHostname"
,ForFunction "getPID"
,ForFunction "getProgName"
,ForFunction "prim_system"
,ForFunction "prim_sleep"
,ForFunction "prim_exitWith"]
......@@ -11,6 +11,8 @@ module System(getCPUTime,getElapsedTime,
system,exitWith,
sleep,evalTime,evalSpace) where
import Global
--- Returns the current cpu time of the process in milliseconds.
getCPUTime :: IO Int
......@@ -31,11 +33,17 @@ getArgs external
--- The empty string is returned for undefined environment variables.
getEnviron :: String -> IO String
getEnviron evar = prim_getEnviron $## evar
getEnviron evar = do
envs <- readGlobal environ
maybe (prim_getEnviron $## evar) return (lookup evar envs)
prim_getEnviron :: String -> IO String
prim_getEnviron external
--- internal state of environment variables set via setEnviron
environ :: Global [(String,String)]
environ = global [] Temporary
--- Set an environment variable to a value.
--- The new value will be passed to subsequent shell commands
--- (see <code>system</code>) and visible to subsequent calls to
......@@ -43,19 +51,17 @@ prim_getEnviron external
--- of the process that started the program execution).
setEnviron :: String -> String -> IO ()
setEnviron evar val = (prim_setEnviron $## val) $## evar
prim_setEnviron :: String -> String -> IO ()
prim_setEnviron external
setEnviron evar val = do
envs <- readGlobal environ
writeGlobal environ ((evar,val) : filter ((/=evar) . fst) envs)
--- Removes an environment variable that has been set by
--- <code>setEnviron</code>.
unsetEnviron :: String -> IO ()
unsetEnviron evar = prim_unsetEnviron $## evar
prim_unsetEnviron :: String -> IO ()
prim_unsetEnviron external
unsetEnviron evar = do
envs <- readGlobal environ
writeGlobal environ (filter ((/=evar) . fst) envs)
--- Returns the hostname of the machine running this process.
......@@ -79,7 +85,11 @@ getProgName external
--- An exit status of zero means successful execution.
system :: String -> IO Int
system cmd = prim_system $## cmd
system cmd = do
envs <- readGlobal environ
prim_system $## (concatMap set envs ++ cmd)
where
set (var,val) = "export "++var++"="++val++"; "
prim_system :: String -> IO Int
prim_system external
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment