diff options
author | Josh Rahm <joshuarahm@gmail.com> | 2023-11-30 20:35:25 +0000 |
---|---|---|
committer | Josh Rahm <joshuarahm@gmail.com> | 2023-11-30 20:35:25 +0000 |
commit | 1b7b916b7631ddf73c38e3a0070d64e4636cb2f3 (patch) | |
tree | cd08258054db80bb9a11b1061bb091c70b76926a /src/nvim/os/env.c | |
parent | eaa89c11d0f8aefbb512de769c6c82f61a8baca3 (diff) | |
parent | 4a8bf24ac690004aedf5540fa440e788459e5e34 (diff) | |
download | rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.gz rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.tar.bz2 rneovim-1b7b916b7631ddf73c38e3a0070d64e4636cb2f3.zip |
Merge remote-tracking branch 'upstream/master' into aucmd_textputpostaucmd_textputpost
Diffstat (limited to 'src/nvim/os/env.c')
-rw-r--r-- | src/nvim/os/env.c | 81 |
1 files changed, 26 insertions, 55 deletions
diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index 0611de14aa..8620c79069 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - // Environment inspection #include <assert.h> @@ -12,29 +9,33 @@ #include <uv.h> #include "auto/config.h" -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/buffer_defs.h" #include "nvim/charset.h" #include "nvim/cmdexpand.h" #include "nvim/eval.h" -#include "nvim/ex_cmds_defs.h" +#include "nvim/func_attr.h" #include "nvim/gettext.h" #include "nvim/globals.h" #include "nvim/log.h" -#include "nvim/macros.h" -#include "nvim/map.h" +#include "nvim/macros_defs.h" +#include "nvim/map_defs.h" #include "nvim/memory.h" #include "nvim/message.h" -#include "nvim/option_defs.h" +#include "nvim/option_vars.h" +#include "nvim/os/fs.h" #include "nvim/os/os.h" #include "nvim/path.h" #include "nvim/strings.h" -#include "nvim/types.h" #include "nvim/version.h" -#include "nvim/vim.h" +#include "nvim/vim_defs.h" #ifdef MSWIN -# include "nvim/mbyte.h" // for utf8_to_utf16, utf16_to_utf8 +# include "nvim/mbyte.h" +#endif + +#ifdef BACKSLASH_IN_FILENAME +# include "nvim/fileio.h" #endif #ifdef HAVE__NSGETENVIRON @@ -48,22 +49,6 @@ // Because `uv_os_getenv` requires allocating, we must manage a map to maintain // the behavior of `os_getenv`. static PMap(cstr_t) envmap = MAP_INIT; -static uv_mutex_t mutex; - -void env_init(void) -{ - uv_mutex_init(&mutex); -} - -void os_env_var_lock(void) -{ - uv_mutex_lock(&mutex); -} - -void os_env_var_unlock(void) -{ - uv_mutex_unlock(&mutex); -} /// Like getenv(), but returns NULL if the variable is empty. /// @see os_env_exists @@ -75,9 +60,8 @@ const char *os_getenv(const char *name) if (name[0] == '\0') { return NULL; } - uv_mutex_lock(&mutex); int r = 0; - if (pmap_has(cstr_t)(&envmap, name) + if (map_has(cstr_t, &envmap, name) && !!(e = (char *)pmap_get(cstr_t)(&envmap, name))) { if (e[0] != '\0') { // Found non-empty cached env var. @@ -101,8 +85,6 @@ const char *os_getenv(const char *name) } pmap_put(cstr_t)(&envmap, xstrdup(name), e); end: - // Must do this before ELOG, log.c may call os_setenv. - uv_mutex_unlock(&mutex); if (r != 0 && r != UV_ENOENT && r != UV_UNKNOWN) { ELOG("uv_os_getenv(%s) failed: %d %s", name, r, uv_err_name(r)); } @@ -154,7 +136,6 @@ int os_setenv(const char *name, const char *value, int overwrite) return 0; } #endif - uv_mutex_lock(&mutex); int r; #ifdef MSWIN // libintl uses getenv() for LC_ALL/LANG/etc., so we must use _putenv_s(). @@ -169,8 +150,6 @@ int os_setenv(const char *name, const char *value, int overwrite) // Destroy the old map item. Do this AFTER uv_os_setenv(), because `value` // could be a previous os_getenv() result. pmap_del2(&envmap, name); - // Must do this before ELOG, log.c may call os_setenv. - uv_mutex_unlock(&mutex); if (r != 0) { ELOG("uv_os_setenv(%s) failed: %d %s", name, r, uv_err_name(r)); } @@ -184,11 +163,8 @@ int os_unsetenv(const char *name) if (name[0] == '\0') { return -1; } - uv_mutex_lock(&mutex); pmap_del2(&envmap, name); int r = uv_os_unsetenv(name); - // Must do this before ELOG, log.c may call os_setenv. - uv_mutex_unlock(&mutex); if (r != 0) { ELOG("uv_os_unsetenv(%s) failed: %d %s", name, r, uv_err_name(r)); } @@ -315,12 +291,11 @@ char *os_getenvname_at_index(size_t index) // Some Windows env vars start with =, so skip over that to find the // separator between name/value - const char * const end = strchr(utf8_str + (utf8_str[0] == '=' ? 1 : 0), - '='); + const char *const end = strchr(utf8_str + (utf8_str[0] == '=' ? 1 : 0), '='); assert(end != NULL); ptrdiff_t len = end - utf8_str; assert(len > 0); - name = xstrndup(utf8_str, (size_t)len); + name = xmemdupz(utf8_str, (size_t)len); xfree(utf8_str); break; } @@ -351,7 +326,7 @@ char *os_getenvname_at_index(size_t index) assert(end != NULL); ptrdiff_t len = end - str; assert(len > 0); - return xstrndup(str, (size_t)len); + return xmemdupz(str, (size_t)len); #endif } @@ -490,7 +465,7 @@ void init_homedir(void) // links. Don't do it when we can't return. if (os_dirname(os_buf, MAXPATHL) == OK && os_chdir(os_buf) == 0) { if (!os_chdir(var) && os_dirname(IObuff, IOSIZE) == OK) { - var = (char *)IObuff; + var = IObuff; } if (os_chdir(os_buf) != 0) { emsg(_(e_prev_dir)); @@ -515,10 +490,8 @@ static char *os_homedir(void) { homedir_buf[0] = NUL; size_t homedir_size = MAXPATHL; - uv_mutex_lock(&mutex); // http://docs.libuv.org/en/v1.x/misc.html#c.uv_os_homedir int ret_value = uv_os_homedir(homedir_buf, &homedir_size); - uv_mutex_unlock(&mutex); if (ret_value == 0 && homedir_size < MAXPATHL) { return homedir_buf; } @@ -599,7 +572,7 @@ void expand_env_esc(char *restrict srcp, char *restrict dst, int dstlen, bool es if (src[0] == '`' && src[1] == '=') { var = src; src += 2; - (void)skip_expr(&src); + (void)skip_expr(&src, NULL); if (*src == '`') { src++; } @@ -841,7 +814,7 @@ const void *vim_env_iter(const char delim, const char *const val, const void *co const char **const dir, size_t *const len) FUNC_ATTR_NONNULL_ARG(2, 4, 5) FUNC_ATTR_WARN_UNUSED_RESULT { - const char *varval = (const char *)iter; + const char *varval = iter; if (varval == NULL) { varval = val; } @@ -872,7 +845,7 @@ const void *vim_env_iter_rev(const char delim, const char *const val, const void const char **const dir, size_t *const len) FUNC_ATTR_NONNULL_ARG(2, 4, 5) FUNC_ATTR_WARN_UNUSED_RESULT { - const char *varend = (const char *)iter; + const char *varend = iter; if (varend == NULL) { varend = val + strlen(val) - 1; } @@ -956,10 +929,8 @@ char *vim_getenv(const char *name) // Find runtime path relative to the nvim binary: ../share/nvim/runtime if (vim_path == NULL) { vim_get_prefix_from_exepath(exe_name); - if (append_path(exe_name, - "share" _PATHSEPSTR "nvim" _PATHSEPSTR "runtime" _PATHSEPSTR, - MAXPATHL) == OK) { - vim_path = exe_name; // -V507 + if (append_path(exe_name, "share/nvim/runtime/", MAXPATHL) == OK) { + vim_path = exe_name; } } @@ -985,7 +956,7 @@ char *vim_getenv(const char *name) // check that the result is a directory name assert(vim_path_end >= vim_path); - vim_path = xstrndup(vim_path, (size_t)(vim_path_end - vim_path)); + vim_path = xmemdupz(vim_path, (size_t)(vim_path_end - vim_path)); if (!os_isdir(vim_path)) { xfree(vim_path); @@ -1056,7 +1027,7 @@ size_t home_replace(const buf_T *const buf, const char *src, char *const dst, si } if (buf != NULL && buf->b_help) { - const size_t dlen = xstrlcpy(dst, path_tail((char *)src), dstlen); + const size_t dlen = xstrlcpy(dst, path_tail(src), dstlen); return MIN(dlen, dstlen - 1); } @@ -1094,7 +1065,7 @@ size_t home_replace(const buf_T *const buf, const char *src, char *const dst, si } if (!one) { - src = skipwhite((char *)src); + src = skipwhite(src); } char *dst_p = dst; while (*src && dstlen > 0) { @@ -1107,7 +1078,7 @@ size_t home_replace(const buf_T *const buf, const char *src, char *const dst, si // er's home directory)). char *p = homedir; size_t len = dirlen; - for (;;) { + while (true) { if (len && path_fnamencmp(src, p, len) == 0 && (vim_ispathsep(src[len]) |