summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2015-12-22 21:18:24 +0800
committerruki <[email protected]>2015-12-22 21:18:24 +0800
commitcf5d1176c75430619a8d47beb2744c5f0418705c (patch)
tree92209658eb6b8e5df1a06a3c6fee0a27d38316e7
parenta1b27b37d0032904adafbeb55f3986af00fdb2e6 (diff)
add getenv for lua
-rwxr-xr-xcore/src/xmake/machine.c2
-rwxr-xr-xcore/src/xmake/makefile1
-rwxr-xr-xcore/src/xmake/os/getenv.c95
-rwxr-xr-xcore/src/xmake/os/setenv.c12
4 files changed, 98 insertions, 12 deletions
diff --git a/core/src/xmake/machine.c b/core/src/xmake/machine.c
index 7e5cae462..5935cab50 100755
--- a/core/src/xmake/machine.c
+++ b/core/src/xmake/machine.c
@@ -65,6 +65,7 @@ tb_int_t xm_os_cpfile(lua_State* lua);
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_strerror(lua_State* lua);
// the path functions
@@ -99,6 +100,7 @@ static luaL_Reg const g_os_functions[] =
, { "rename", xm_os_rename }
, { "exists", xm_os_exists }
, { "setenv", xm_os_setenv }
+, { "getenv", xm_os_getenv }
, { "strerror", xm_os_strerror }
, { tb_null, tb_null }
};
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 098ad95f3..ca2449d0b 100755
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -29,6 +29,7 @@ xmake_C_FILES += \
os/rename \
os/exists \
os/setenv \
+ os/getenv \
os/strerror \
path/relative \
path/absolute \
diff --git a/core/src/xmake/os/getenv.c b/core/src/xmake/os/getenv.c
new file mode 100755
index 000000000..571b9d437
--- /dev/null
+++ b/core/src/xmake/os/getenv.c
@@ -0,0 +1,95 @@
+/*!The Automatic Cross-platform Build Tool
+ *
+ * XMake is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * XMake is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with XMake;
+ * If not, see <a href="http://www.gnu.org/licenses/"> http://www.gnu.org/licenses/</a>
+ *
+ * Copyright (C) 2009 - 2015, ruki All rights reserved.
+ *
+ * @author ruki
+ * @file getenv.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "getenv"
+#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
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+tb_int_t xm_os_getenv(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ // get the name
+ tb_char_t const* name = luaL_checkstring(lua, 1);
+ tb_check_return_val(name, 0);
+
+ // init values
+ tb_string_t values;
+ if (!tb_string_init(&values)) return 0;
+
+ // init environment
+ tb_environment_ref_t environment = tb_environment_init();
+ if (environment)
+ {
+ // load variable
+ if (tb_environment_load(environment, name))
+ {
+ // make values
+ tb_bool_t is_first = tb_true;
+ tb_for_all_if (tb_char_t const*, value, environment, value)
+ {
+ // append separator
+ if (!is_first) tb_string_chrcat(&values, XM_OS_ENV_SEP);
+ else is_first = tb_false;
+
+ // append value
+ tb_string_cstrcat(&values, value);
+ }
+ }
+
+ // exit environment
+ tb_environment_exit(environment);
+ }
+
+ // save result
+ if (tb_string_size(&values)) lua_pushstring(lua, tb_string_cstr(&values));
+ else lua_pushnil(lua);
+
+ // exit values
+ tb_string_exit(&values);
+
+ // ok
+ return 1;
+}
diff --git a/core/src/xmake/os/setenv.c b/core/src/xmake/os/setenv.c
index 7e3661f36..13fb991e2 100755
--- a/core/src/xmake/os/setenv.c
+++ b/core/src/xmake/os/setenv.c
@@ -31,9 +31,6 @@
* includes
*/
#include "prefix.h"
-#ifdef TB_CONFIG_OS_WINDOWS
-# include <stdio.h>
-#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* macros
@@ -60,14 +57,6 @@ tb_int_t xm_os_setenv(lua_State* lua)
tb_char_t const* value = luaL_checklstring(lua, 2, &value_size);
tb_check_return_val(name, 0);
-#ifdef TB_CONFIG_OS_WINDOWS
- /* @note
- *
- * we must use setenv for matching the function getenv of luajit,
- * because SetEnvironmentVariable and getenv are not compatible.
- */
- lua_pushboolean(lua, setenv(name, value));
-#else
// find the first separator position
tb_char_t const* p = value? tb_strchr(value, XM_OS_ENV_SEP) : tb_null;
if (p)
@@ -130,7 +119,6 @@ tb_int_t xm_os_setenv(lua_State* lua)
// done os.setenv(name, value)
lua_pushboolean(lua, tb_environment_set_one(name, value));
}
-#endif
// ok
return 1;