From 24a56cca308621a5a585f73b22e5421461318b3d Mon Sep 17 00:00:00 2001 From: erw7 Date: Wed, 6 Mar 2019 14:59:28 +0900 Subject: [PATCH 1/2] Fix environment variable on Windows Since uv_os_setenv uses SetEnvironmentVariableW, _wenviron is no updated. As a result, inconsistency occurs in completion of environment variable names. Change to use GetEnvironmentStaringsW instead of _wenviron to solve it. --- src/nvim/os/env.c | 46 ++++++++++++++++++---------- test/functional/ui/wildmode_spec.lua | 16 ++++++++++ 2 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index e7bfbc8240..35b56f9539 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -151,25 +151,39 @@ 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++; + + // GetEnvironmentStrings return environment block of the following format: + // + // var1=value1\0var2=value2\0...varN=valueN\0\0 + // + char *name = NULL; + size_t current_index = 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) diff --git a/test/functional/ui/wildmode_spec.lua b/test/functional/ui/wildmode_spec.lua index 7cd09fb222..8a56699cca 100644 --- a/test/functional/ui/wildmode_spec.lua +++ b/test/functional/ui/wildmode_spec.lua @@ -201,6 +201,22 @@ describe('command line completion', function() ]]) end) + it('completes env var names', function() + clear() + screen:attach() + command('let $XTEST_1 = "foo" | let $XTEST_2 = "bar"') + command('set wildmode=full') + command('set wildmenu') + feed(':!echo $XTEST_') + screen:expect([[ + | + {1:~ }| + {1:~ }| + {2:XTEST_1}{3: XTEST_2 }| + :!echo $XTEST_1^ | + ]]) + end) + it('completes (multibyte) env var names #9655', function() clear({env={ ['XTEST_1AaあB']='foo', From c9264e6d524b3c2ac1a1388d5627f9b0c717cbc7 Mon Sep 17 00:00:00 2001 From: erw7 Date: Thu, 7 Mar 2019 13:41:40 +0900 Subject: [PATCH 2/2] Fix os.getenv of lua on Windows Change to use os_getenv instead of getenv because environment variable set by uv_os_setenv can not be get with getenv. --- src/nvim/lua/executor.c | 25 +++++++++++++++++++++++++ src/nvim/os/env.h | 7 +++++++ test/functional/lua/overrides_spec.lua | 16 ++++++++++++++++ 3 files changed, 48 insertions(+) create mode 100644 src/nvim/os/env.h diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 93069893cf..d62b5e2c21 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/env.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")); @@ -337,6 +349,19 @@ int nlua_debug(lua_State *lstate) return 0; } +#ifdef WIN32 +/// os.getenv implementation: On Windows, uv_os_setenv does not update _environ, +/// so we need to use os_getenv instead of getenv. Therefore we will apply a +/// monkey patch here. +/// +/// @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.h b/src/nvim/os/env.h new file mode 100644 index 0000000000..e0abd87220 --- /dev/null +++ b/src/nvim/os/env.h @@ -0,0 +1,7 @@ +#ifndef NVIM_OS_ENV_H +#define NVIM_OS_ENV_H + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "os/env.h.generated.h" +#endif +#endif // NVIM_OS_ENV_H diff --git a/test/functional/lua/overrides_spec.lua b/test/functional/lua/overrides_spec.lua index 007d40874f..e9d7f3355a 100644 --- a/test/functional/lua/overrides_spec.lua +++ b/test/functional/lua/overrides_spec.lua @@ -300,3 +300,19 @@ describe('package.path/package.cpath', function() eq(new_paths_str, eval_lua('package.path'):sub(1, #new_paths_str)) end) end) + +describe('os.getenv', function() + it('returns nothing for not set env var', function() + eq(NIL, funcs.luaeval('os.getenv("XTEST_1")')) + end) + it('returns env var set by the parent process', function() + local value = 'foo' + clear({env = {['XTEST_1']=value}}) + eq(value, funcs.luaeval('os.getenv("XTEST_1")')) + end) + it('returns env var set by let', function() + local value = 'foo' + meths.command('let $XTEST_1 = "'..value..'"') + eq(value, funcs.luaeval('os.getenv("XTEST_1")')) + end) +end)