summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-04-27 00:02:50 +0800
committerruki <[email protected]>2019-04-26 18:17:00 +0800
commit6e34dd3a9f72eb9c7743b1eebb10f7fbc5996025 (patch)
treef89a7bc61fb1fa692651867ba6ed627f441035f3
parent695a5e99906bbf0c4f11b68c9339222af3ebfb7d (diff)
add os.getenvs
-rw-r--r--core/src/xmake/machine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/getenvs.c85
-rw-r--r--xmake/core/base/os.lua17
-rw-r--r--xmake/core/sandbox/modules/os.lua5
5 files changed, 108 insertions, 2 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 9c9cccec9..2ca2227c5 100644
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -75,6 +75,7 @@ tb_int_t xm_os_rename(lua_State* lua);
tb_int_t xm_os_exists(lua_State* lua);
tb_int_t xm_os_setenv(lua_State* lua);
tb_int_t xm_os_getenv(lua_State* lua);
+tb_int_t xm_os_getenvs(lua_State* lua);
tb_int_t xm_os_readlink(lua_State* lua);
tb_int_t xm_os_filesize(lua_State* lua);
tb_int_t xm_os_emptydir(lua_State* lua);
@@ -167,6 +168,7 @@ static luaL_Reg const g_os_functions[] =
, { "exists", xm_os_exists }
, { "setenv", xm_os_setenv }
, { "getenv", xm_os_getenv }
+, { "getenvs", xm_os_getenvs }
, { "readlink", xm_os_readlink }
, { "emptydir", xm_os_emptydir }
, { "strerror", xm_os_strerror }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index fb6c1b76d..7589e4ccf 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -35,6 +35,7 @@ xmake_C_FILES += \
os/exists \
os/setenv \
os/getenv \
+ os/getenvs \
os/readlink \
os/emptydir \
os/strerror \
diff --git a/core/src/xmake/os/getenvs.c b/core/src/xmake/os/getenvs.c
new file mode 100644
index 000000000..81e0b68c7
--- /dev/null
+++ b/core/src/xmake/os/getenvs.c
@@ -0,0 +1,85 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015 - 2019, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file getenvs.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "getenvs"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * macros
+ */
+
+// the separator
+#ifdef TB_CONFIG_OS_WINDOWS
+# define XM_OS_ENV_SEP ';'
+#else
+# define XM_OS_ENV_SEP ':'
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * globals
+ */
+
+// the user environment
+#ifndef TB_CONFIG_OS_WINDOWS
+extern tb_char_t** environ;
+#endif
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_getenvs(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // init table
+ lua_newtable(lua);
+
+#ifdef TB_CONFIG_OS_WINDOWS
+#else
+ tb_char_t const** p = (tb_char_t const**)environ;
+ if (p)
+ {
+ tb_int_t i = 0;
+ while (*p)
+ {
+ tb_size_t n = tb_strlen(*p);
+ if (n)
+ {
+ lua_pushstring(lua, *p);
+ lua_rawseti(lua, -2, i++);
+ }
+ p++;
+ }
+ }
+#endif
+
+ // ok
+ return 1;
+}
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index b69dd1754..ee0404971 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -36,6 +36,7 @@ os._mkdir = os._mkdir or os.mkdir
os._rmdir = os._rmdir or os.rmdir
os._tmpdir = os._tmpdir or os.tmpdir
os._setenv = os._setenv or os.setenv
+os._getenvs = os._getenvs or os.getenvs
os._readlink = os._readlink or os.readlink
-- copy single file or directory
@@ -899,6 +900,22 @@ function os.fscase()
return os._FSCASE
end
+-- get all current environment variables
+function os.getenvs()
+ local envs = {}
+ for _, line in ipairs(os._getenvs()) do
+ local p = line:find('=', 1, true)
+ if p then
+ local key = line:sub(1, p - 1):trim()
+ local values = line:sub(p + 1):trim()
+ if #key > 0 then
+ envs[key] = values
+ end
+ end
+ end
+ return envs
+end
+
-- set values to environment variable
function os.setenv(name, ...)
local seperator = os.host() == "windows" and ';' or ':'
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index 20048bff9..55c59d6d5 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -49,8 +49,9 @@ sandbox_os.nuldev = os.nuldev
sandbox_os.getenv = os.getenv
sandbox_os.setenv = os.setenv
sandbox_os.addenv = os.addenv
-sandbox_os.setenvp = os.setenvp
-sandbox_os.addenvp = os.addenvp
+sandbox_os.setenvp = os.setenvp
+sandbox_os.addenvp = os.addenvp
+sandbox_os.getenvs = os.getenvs
sandbox_os.pbpaste = os.pbpaste
sandbox_os.pbcopy = os.pbcopy
sandbox_os.emptydir = os.emptydir