comparison src/misc1.c @ 16172:6b0836727cf3 v8.1.1091

patch 8.1.1091: MS-Windows: cannot use multi-byte chars in environment var commit https://github.com/vim/vim/commit/f0908e6fe18943ad4453d7d6772fa43049aff4bc Author: Bram Moolenaar <Bram@vim.org> Date: Sat Mar 30 20:11:50 2019 +0100 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 https://github.com/vim/vim/issues/4008)
author Bram Moolenaar <Bram@vim.org>
date Sat, 30 Mar 2019 20:15:05 +0100
parents cd5c83115ec6
children 973070a30381
comparison
equal deleted inserted replaced
16171:99532bf924ff 16172:6b0836727cf3
4299 * initialized to FALSE by the caller. 4299 * initialized to FALSE by the caller.
4300 */ 4300 */
4301 char_u * 4301 char_u *
4302 vim_getenv(char_u *name, int *mustfree) 4302 vim_getenv(char_u *name, int *mustfree)
4303 { 4303 {
4304 char_u *p; 4304 char_u *p = NULL;
4305 char_u *pend; 4305 char_u *pend;
4306 int vimruntime; 4306 int vimruntime;
4307 4307 #ifdef MSWIN
4308 #if defined(MSWIN) 4308 WCHAR *wn, *wp;
4309 /* use "C:/" when $HOME is not set */ 4309
4310 // use "C:/" when $HOME is not set
4310 if (STRCMP(name, "HOME") == 0) 4311 if (STRCMP(name, "HOME") == 0)
4311 return homedir; 4312 return homedir;
4312 #endif 4313
4313 4314 // Use Wide function
4315 wn = enc_to_utf16(name, NULL);
4316 if (wn == NULL)
4317 return NULL;
4318
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)
4326 {
4327 p = utf16_to_enc(wp, NULL);
4328 if (p == NULL)
4329 return NULL;
4330
4331 *mustfree = TRUE;
4332 return p;
4333 }
4334 #else
4314 p = mch_getenv(name); 4335 p = mch_getenv(name);
4315 if (p != NULL && *p == NUL) /* empty is the same as not set */ 4336 if (p != NULL && *p == NUL) // empty is the same as not set
4316 p = NULL; 4337 p = NULL;
4317 4338
4318 if (p != NULL) 4339 if (p != NULL)
4319 {
4320 #if defined(MSWIN)
4321 if (enc_utf8)
4322 {
4323 int len;
4324 char_u *pp = NULL;
4325
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
4336 return p; 4340 return p;
4337 } 4341 #endif
4338 4342
4343 // handling $VIMRUNTIME and $VIM is below, bail out if it's another name.
4339 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0); 4344 vimruntime = (STRCMP(name, "VIMRUNTIME") == 0);
4340 if (!vimruntime && STRCMP(name, "VIM") != 0) 4345 if (!vimruntime && STRCMP(name, "VIM") != 0)
4341 return NULL; 4346 return NULL;
4342 4347
4343 /* 4348 /*
4348 #ifdef HAVE_PATHDEF 4353 #ifdef HAVE_PATHDEF
4349 && *default_vimruntime_dir == NUL 4354 && *default_vimruntime_dir == NUL
4350 #endif 4355 #endif
4351 ) 4356 )
4352 { 4357 {
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
4353 p = mch_getenv((char_u *)"VIM"); 4375 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
4355 p = NULL; 4377 p = NULL;
4356 if (p != NULL) 4378 if (p != NULL)
4357 { 4379 {
4358 p = vim_version_dir(p); 4380 p = vim_version_dir(p);
4359 if (p != NULL) 4381 if (p != NULL)
4360 *mustfree = TRUE; 4382 *mustfree = TRUE;
4361 else 4383 else
4362 p = mch_getenv((char_u *)"VIM"); 4384 p = mch_getenv((char_u *)"VIM");
4363 4385 }
4364 #if defined(MSWIN) 4386 #endif
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
4383 }
4384 } 4387 }
4385 4388
4386 /* 4389 /*
4387 * When expanding $VIM or $VIMRUNTIME fails, try using: 4390 * When expanding $VIM or $VIMRUNTIME fails, try using:
4388 * - the directory name from 'helpfile' (unless it contains '$') 4391 * - the directory name from 'helpfile' (unless it contains '$')