diff options
author | Bram Moolenaar <Bram@vim.org> | 2019-05-09 14:52:41 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2019-05-09 14:52:41 +0200 |
commit | 691ddeefb545d8488e5a495af61caba2e57b3de9 (patch) | |
tree | 4fc05bf8d9ad408b60d1c127b541869ac0f5f8ed /src | |
parent | 68cbb14bae1013702270b25e886b5ee09e07575a (diff) | |
download | vim-git-691ddeefb545d8488e5a495af61caba2e57b3de9.tar.gz |
patch 8.1.1305: there is no easy way to manipulate environment variablesv8.1.1305
Problem: There is no easy way to manipulate environment variables.
Solution: Add environ(), getenv() and setenv(). (Yasuhiro Matsumoto,
closes #2875)
Diffstat (limited to 'src')
-rw-r--r-- | src/evalfunc.c | 97 | ||||
-rw-r--r-- | src/testdir/Make_all.mak | 2 | ||||
-rw-r--r-- | src/testdir/test_environ.vim | 44 | ||||
-rw-r--r-- | src/version.c | 2 |
4 files changed, 145 insertions, 0 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index eaefccf62..267683268 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -137,6 +137,7 @@ static void f_did_filetype(typval_T *argvars, typval_T *rettv); static void f_diff_filler(typval_T *argvars, typval_T *rettv); static void f_diff_hlID(typval_T *argvars, typval_T *rettv); static void f_empty(typval_T *argvars, typval_T *rettv); +static void f_environ(typval_T *argvars, typval_T *rettv); static void f_escape(typval_T *argvars, typval_T *rettv); static void f_eval(typval_T *argvars, typval_T *rettv); static void f_eventhandler(typval_T *argvars, typval_T *rettv); @@ -187,6 +188,7 @@ static void f_getcmdpos(typval_T *argvars, typval_T *rettv); static void f_getcmdtype(typval_T *argvars, typval_T *rettv); static void f_getcmdwintype(typval_T *argvars, typval_T *rettv); static void f_getcwd(typval_T *argvars, typval_T *rettv); +static void f_getenv(typval_T *argvars, typval_T *rettv); static void f_getfontname(typval_T *argvars, typval_T *rettv); static void f_getfperm(typval_T *argvars, typval_T *rettv); static void f_getfsize(typval_T *argvars, typval_T *rettv); @@ -365,6 +367,7 @@ static void f_setbufline(typval_T *argvars, typval_T *rettv); static void f_setbufvar(typval_T *argvars, typval_T *rettv); static void f_setcharsearch(typval_T *argvars, typval_T *rettv); static void f_setcmdpos(typval_T *argvars, typval_T *rettv); +static void f_setenv(typval_T *argvars, typval_T *rettv); static void f_setfperm(typval_T *argvars, typval_T *rettv); static void f_setline(typval_T *argvars, typval_T *rettv); static void f_setloclist(typval_T *argvars, typval_T *rettv); @@ -629,6 +632,7 @@ static struct fst {"diff_filler", 1, 1, f_diff_filler}, {"diff_hlID", 2, 2, f_diff_hlID}, {"empty", 1, 1, f_empty}, + {"environ", 0, 0, f_environ}, {"escape", 2, 2, f_escape}, {"eval", 1, 1, f_eval}, {"eventhandler", 0, 0, f_eventhandler}, @@ -681,6 +685,7 @@ static struct fst #endif {"getcurpos", 0, 0, f_getcurpos}, {"getcwd", 0, 2, f_getcwd}, + {"getenv", 1, 1, f_getenv}, {"getfontname", 0, 1, f_getfontname}, {"getfperm", 1, 1, f_getfperm}, {"getfsize", 1, 1, f_getfsize}, @@ -873,6 +878,7 @@ static struct fst {"setbufvar", 3, 3, f_setbufvar}, {"setcharsearch", 1, 1, f_setcharsearch}, {"setcmdpos", 1, 1, f_setcmdpos}, + {"setenv", 2, 2, f_setenv}, {"setfperm", 2, 2, f_setfperm}, {"setline", 2, 2, f_setline}, {"setloclist", 2, 4, f_setloclist}, @@ -3340,6 +3346,59 @@ f_empty(typval_T *argvars, typval_T *rettv) } /* + * "environ()" function + */ + static void +f_environ(typval_T *argvars UNUSED, typval_T *rettv) +{ +#if !defined(AMIGA) + int i = 0; + char_u *entry, *value; +# ifdef MSWIN + extern wchar_t **_wenviron; +# else + extern char **environ; +# endif + + if (rettv_dict_alloc(rettv) != OK) + return; + +# ifdef MSWIN + if (*_wenviron == NULL) + return; +# else + if (*environ == NULL) + return; +# endif + + for (i = 0; ; ++i) + { +# ifdef MSWIN + short_u *p; + + if ((p = (short_u *)_wenviron[i]) == NULL) + return; + entry = utf16_to_enc(p, NULL); +# else + if ((entry = (char_u *)environ[i]) == NULL) + return; + entry = vim_strsave(entry); +# endif + if (entry == NULL) // out of memory + return; + if ((value = vim_strchr(entry, '=')) == NULL) + { + vim_free(entry); + continue; + } + *value++ = NUL; + dict_add_string(rettv->vval.v_dict, (char *)entry, value); + vim_free(entry); + } +#endif +} + +/* * "escape({string}, {chars})" function */ static void @@ -5261,6 +5320,27 @@ f_getcwd(typval_T *argvars, typval_T *rettv) } /* + * "getenv()" function + */ + static void +f_getenv(typval_T *argvars, typval_T *rettv) +{ + int mustfree = FALSE; + char_u *p = vim_getenv(tv_get_string(&argvars[0]), &mustfree); + + if (p == NULL) + { + rettv->v_type = VAR_SPECIAL; + rettv->vval.v_number = VVAL_NULL; + return; + } + if (!mustfree) + p = vim_strsave(p); + rettv->vval.v_string = p; + rettv->v_type = VAR_STRING; +} + +/* * "getfontname()" function */ static void @@ -11425,6 +11505,23 @@ f_setcmdpos(typval_T *argvars, typval_T *rettv) } /* + * "setenv()" function + */ + static void +f_setenv(typval_T *argvars, typval_T *rettv UNUSED) +{ + char_u namebuf[NUMBUFLEN]; + char_u valbuf[NUMBUFLEN]; + char_u *name = tv_get_string_buf(&argvars[0], namebuf); + + if (argvars[1].v_type == VAR_SPECIAL + && argvars[1].vval.v_number == VVAL_NULL) + vim_unsetenv(name); + else + vim_setenv(name, tv_get_string_buf(&argvars[1], valbuf)); +} + +/* * "setfperm({fname}, {mode})" function */ static void diff --git a/src/testdir/Make_all.mak b/src/testdir/Make_all.mak index 6b966e6fe..1f50bd8cf 100644 --- a/src/testdir/Make_all.mak +++ b/src/testdir/Make_all.mak @@ -104,6 +104,7 @@ NEW_TESTS = \ test_erasebackword \ test_escaped_glob \ test_eval_stuff \ + test_environ \ test_ex_equal \ test_ex_undo \ test_ex_z \ @@ -320,6 +321,7 @@ NEW_TESTS_RES = \ test_digraph.res \ test_display.res \ test_edit.res \ + test_environ.res \ test_erasebackword.res \ test_escaped_glob.res \ test_eval_stuff.res \ diff --git a/src/testdir/test_environ.vim b/src/testdir/test_environ.vim new file mode 100644 index 000000000..094c4ce36 --- /dev/null +++ b/src/testdir/test_environ.vim @@ -0,0 +1,44 @@ +scriptencoding utf-8 + +func Test_environ() + unlet! $TESTENV + call assert_equal(0, has_key(environ(), 'TESTENV')) + let $TESTENV = 'foo' + call assert_equal(1, has_key(environ(), 'TESTENV')) + let $TESTENV = 'こんにちわ' + call assert_equal('こんにちわ', environ()['TESTENV']) +endfunc + +func Test_getenv() + unlet! $TESTENV + call assert_equal(v:null, getenv('TESTENV')) + let $TESTENV = 'foo' + call assert_equal('foo', getenv('TESTENV')) +endfunc + +func Test_setenv() + unlet! $TESTENV + call setenv('TEST ENV', 'foo') + call assert_equal('foo', getenv('TEST ENV')) + call setenv('TEST ENV', v:null) + call assert_equal(v:null, getenv('TEST ENV')) +endfunc + +func Test_external_env() + call setenv('FOO', 'HelloWorld') + if has('win32') + let result = system('echo %FOO%') + else + let result = system('echo $FOO') + endif + let result = substitute(result, '[ \r\n]', '', 'g') + call assert_equal('HelloWorld', result) + + call setenv('FOO', v:null) + if has('win32') + let result = system('set | grep ^FOO=') + else + let result = system('env | grep ^FOO=') + endif + call assert_equal('', result) +endfunc diff --git a/src/version.c b/src/version.c index 7715410bd..182a43a53 100644 --- a/src/version.c +++ b/src/version.c @@ -768,6 +768,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1305, +/**/ 1304, /**/ 1303, |