@@ -25,6 +25,7 @@ import System.IO.Error (IOError, isDoesNotExistError)
2525
2626import qualified Foreign.Lua as Lua
2727import qualified System.Directory as Directory
28+ import qualified System.Environment as Env
2829import qualified System.IO.Temp as Temp
2930
3031-- | Pushes the @text@ module to the lua stack.
@@ -33,8 +34,11 @@ pushModule = do
3334 Lua. newtable
3435 addFunction " chdir" chdir
3536 addFunction " currentdir" currentdir
37+ addFunction " env" env
38+ addFunction " getenv" getenv
3639 addFunction " ls" ls
3740 addFunction " pwd" currentdir
41+ addFunction " setenv" setenv
3842 addFunction " tmpdirname" tmpdirname
3943 addFunction " with_tmpdir" with_tmpdir
4044 return 1
@@ -135,6 +139,23 @@ chdir fp = ioToLua $ Directory.setCurrentDirectory fp
135139currentdir :: Lua FilePath
136140currentdir = ioToLua Directory. getCurrentDirectory
137141
142+ -- | Retrieve the entire environment
143+ env :: Lua NumResults
144+ env = do
145+ kvs <- ioToLua Env. getEnvironment
146+ let addValue (k, v) = Lua. push k *> Lua. push v *> Lua. rawset (- 3 )
147+ Lua. newtable
148+ mapM_ addValue kvs
149+ return (NumResults 1 )
150+
151+ -- | Returns the value of an environment variable
152+ getenv :: String -> Lua (Optional String )
153+ getenv name = ioToLua (Optional <$> Env. lookupEnv name)
154+
155+ -- | Set the specified environment variable to a new value.
156+ setenv :: String -> String -> Lua ()
157+ setenv name value = ioToLua (Env. setEnv name value)
158+
138159-- | Convert a System IO operation to a Lua operation.
139160ioToLua :: IO a -> Lua a
140161ioToLua action = do
0 commit comments