diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2019-03-16 20:28:52 +0100 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2019-03-16 20:28:52 +0100 |
commit | 11a481f711ee2d58c1157e9917779ea424ba3a45 (patch) | |
tree | 69e0ee5348f621a1dc62e40c853ab3764f812505 /src | |
parent | 8d00393d0cc89511867861dc8ac5cc7b068f9f69 (diff) | |
parent | c9264e6d524b3c2ac1a1388d5627f9b0c717cbc7 (diff) | |
download | rneovim-11a481f711ee2d58c1157e9917779ea424ba3a45.tar.gz rneovim-11a481f711ee2d58c1157e9917779ea424ba3a45.tar.bz2 rneovim-11a481f711ee2d58c1157e9917779ea424ba3a45.zip |
Merge #9686 'win/Lua: monkey-patch os.getenv()'
fixes #9681
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/lua/executor.c | 27 | ||||
-rw-r--r-- | src/nvim/os/env.c | 43 |
2 files changed, 53 insertions, 17 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 93069893cf..72b97736fc 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -24,6 +24,10 @@ #include "nvim/undo.h" #include "nvim/ascii.h" +#ifdef WIN32 +#include "nvim/os/os.h" +#endif + #include "nvim/lua/executor.h" #include "nvim/lua/converter.h" @@ -118,6 +122,14 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_setfield(lstate, -2, "debug"); lua_pop(lstate, 1); +#ifdef WIN32 + // os.getenv + lua_getglobal(lstate, "os"); + lua_pushcfunction(lstate, &nlua_getenv); + lua_setfield(lstate, -2, "getenv"); + lua_pop(lstate, 1); +#endif + // vim if (luaL_dostring(lstate, (char *)&vim_module[0])) { nlua_error(lstate, _("E5106: Error while creating vim module: %.*s")); @@ -297,7 +309,7 @@ nlua_print_error: return 0; } -/// debug.debug implementation: interaction with user while debugging +/// debug.debug: interaction with user while debugging. /// /// @param lstate Lua interpreter state. int nlua_debug(lua_State *lstate) @@ -337,6 +349,19 @@ int nlua_debug(lua_State *lstate) return 0; } +#ifdef WIN32 +/// os.getenv: override os.getenv to maintain coherency. #9681 +/// +/// uv_os_setenv uses SetEnvironmentVariableW which does not update _environ. +/// +/// @param lstate Lua interpreter state. +static int nlua_getenv(lua_State *lstate) +{ + lua_pushstring(lstate, os_getenv(luaL_checkstring(lstate, 1))); + return 1; +} +#endif + /// Evaluate lua string /// /// Used for luaeval(). diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index e7bfbc8240..7d1021962c 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -151,25 +151,36 @@ int os_unsetenv(const char *name) char *os_getenvname_at_index(size_t index) { #ifdef _WIN32 - // Check if index is inside the environ array and is not the last element. - for (size_t i = 0; i <= index; i++) { - if (_wenviron[i] == NULL) { - return NULL; - } - } - wchar_t *utf16_str = _wenviron[index]; - char *utf8_str; - int conversion_result = utf16_to_utf8(utf16_str, &utf8_str); - if (conversion_result != 0) { - EMSG2("utf16_to_utf8 failed: %d", conversion_result); + wchar_t *env = GetEnvironmentStringsW(); + if (!env) { return NULL; } - size_t namesize = 0; - while (utf8_str[namesize] != '=' && utf8_str[namesize] != NUL) { - namesize++; + char *name = NULL; + size_t current_index = 0; + // GetEnvironmentStringsW() result has this format: + // var1=value1\0var2=value2\0...varN=valueN\0\0 + for (wchar_t *it = env; *it != L'\0' || *(it + 1) != L'\0'; it++) { + if (index == current_index) { + char *utf8_str; + int conversion_result = utf16_to_utf8(it, &utf8_str); + if (conversion_result != 0) { + EMSG2("utf16_to_utf8 failed: %d", conversion_result); + break; + } + size_t namesize = 0; + while (utf8_str[namesize] != '=' && utf8_str[namesize] != NUL) { + namesize++; + } + name = (char *)vim_strnsave((char_u *)utf8_str, namesize); + xfree(utf8_str); + break; + } + if (*it == L'\0') { + current_index++; + } } - char *name = (char *)vim_strnsave((char_u *)utf8_str, namesize); - xfree(utf8_str); + + FreeEnvironmentStringsW(env); return name; #else # if defined(HAVE__NSGETENVIRON) |