Skip to content

Commit f0908e6

Browse files
committed
patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var
Problem: MS-Windows: cannot use multi-byte chars in environment var. Solution: Use the wide API. (Ken Takata, closes #4008)
1 parent 2d04a91 commit f0908e6

3 files changed

Lines changed: 54 additions & 44 deletions

File tree

src/misc1.c

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4301,41 +4301,46 @@ expand_env_esc(
43014301
char_u *
43024302
vim_getenv(char_u *name, int *mustfree)
43034303
{
4304-
char_u *p;
4304+
char_u *p = NULL;
43054305
char_u *pend;
43064306
int vimruntime;
4307+
#ifdef MSWIN
4308+
WCHAR *wn, *wp;
43074309

4308-
#if defined(MSWIN)
4309-
/* use "C:/" when $HOME is not set */
4310+
// use "C:/" when $HOME is not set
43104311
if (STRCMP(name, "HOME") == 0)
43114312
return homedir;
4312-
#endif
43134313

4314-
p = mch_getenv(name);
4315-
if (p != NULL && *p == NUL) /* empty is the same as not set */
4316-
p = NULL;
4314+
// Use Wide function
4315+
wn = enc_to_utf16(name, NULL);
4316+
if (wn == NULL)
4317+
return NULL;
43174318

4318-
if (p != NULL)
4319+
wp = _wgetenv(wn);
4320+
vim_free(wn);
4321+
4322+
if (wp != NULL && *wp == NUL) // empty is the same as not set
4323+
wp = NULL;
4324+
4325+
if (wp != NULL)
43194326
{
4320-
#if defined(MSWIN)
4321-
if (enc_utf8)
4322-
{
4323-
int len;
4324-
char_u *pp = NULL;
4327+
p = utf16_to_enc(wp, NULL);
4328+
if (p == NULL)
4329+
return NULL;
43254330

4326-
/* Convert from active codepage to UTF-8. Other conversions are
4327-
* not done, because they would fail for non-ASCII characters. */
4328-
acp_to_enc(p, (int)STRLEN(p), &pp, &len);
4329-
if (pp != NULL)
4330-
{
4331-
p = pp;
4332-
*mustfree = TRUE;
4333-
}
4334-
}
4335-
#endif
4331+
*mustfree = TRUE;
43364332
return p;
43374333
}
4334+
#else
4335+
p = mch_getenv(name);
4336+
if (p != NULL && *p == NUL) // empty is the same as not set
4337+
p = NULL;
4338+
4339+
if (p != NULL)
4340+
return p;
4341+
#endif
43384342

4343+
// handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
43394344
vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
43404345
if (!vimruntime && STRCMP(name, "VIM") != 0)
43414346
return NULL;
@@ -4350,8 +4355,25 @@ vim_getenv(char_u *name, int *mustfree)
43504355
#endif
43514356
)
43524357
{
4358+
#ifdef MSWIN
4359+
// Use Wide function
4360+
wp = _wgetenv(L"VIM");
4361+
if (wp != NULL && *wp == NUL) // empty is the same as not set
4362+
wp = NULL;
4363+
if (wp != NULL)
4364+
{
4365+
char_u *q = utf16_to_enc(wp, NULL);
4366+
if (q != NULL)
4367+
{
4368+
p = vim_version_dir(q);
4369+
*mustfree = TRUE;
4370+
if (p == NULL)
4371+
p = q;
4372+
}
4373+
}
4374+
#else
43534375
p = mch_getenv((char_u *)"VIM");
4354-
if (p != NULL && *p == NUL) /* empty is the same as not set */
4376+
if (p != NULL && *p == NUL) // empty is the same as not set
43554377
p = NULL;
43564378
if (p != NULL)
43574379
{
@@ -4360,27 +4382,8 @@ vim_getenv(char_u *name, int *mustfree)
43604382
*mustfree = TRUE;
43614383
else
43624384
p = mch_getenv((char_u *)"VIM");
4363-
4364-
#if defined(MSWIN)
4365-
if (enc_utf8)
4366-
{
4367-
int len;
4368-
char_u *pp = NULL;
4369-
4370-
/* Convert from active codepage to UTF-8. Other conversions
4371-
* are not done, because they would fail for non-ASCII
4372-
* characters. */
4373-
acp_to_enc(p, (int)STRLEN(p), &pp, &len);
4374-
if (pp != NULL)
4375-
{
4376-
if (*mustfree)
4377-
vim_free(p);
4378-
p = pp;
4379-
*mustfree = TRUE;
4380-
}
4381-
}
4382-
#endif
43834385
}
4386+
#endif
43844387
}
43854388

43864389
/*

src/testdir/test_let.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,8 @@ func Test_let_varg_fail()
146146
call assert_fails('call s:set_varg8(1)', 'E742:')
147147
call s:set_varg9([0])
148148
endfunction
149+
150+
func Test_let_utf8_environment()
151+
let $a = 'ĀĒĪŌŪあいうえお'
152+
call assert_equal('ĀĒĪŌŪあいうえお', $a)
153+
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,8 @@ static char *(features[]) =
775775

776776
static int included_patches[] =
777777
{ /* Add new patch number below this line */
778+
/**/
779+
1091,
778780
/**/
779781
1090,
780782
/**/

0 commit comments

Comments
 (0)