aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/os/stdpaths.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/os/stdpaths.c')
-rw-r--r--src/nvim/os/stdpaths.c61
1 files changed, 46 insertions, 15 deletions
diff --git a/src/nvim/os/stdpaths.c b/src/nvim/os/stdpaths.c
index 81ceb919c4..f6503dfdb0 100644
--- a/src/nvim/os/stdpaths.c
+++ b/src/nvim/os/stdpaths.c
@@ -1,3 +1,6 @@
+// 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
+
#include <stdbool.h>
#include "nvim/os/stdpaths_defs.h"
@@ -16,20 +19,29 @@ static const char *xdg_env_vars[] = {
[kXDGDataDirs] = "XDG_DATA_DIRS",
};
+#ifdef WIN32
+static const char *const xdg_defaults_env_vars[] = {
+ [kXDGConfigHome] = "LOCALAPPDATA",
+ [kXDGDataHome] = "LOCALAPPDATA",
+ [kXDGCacheHome] = "TEMP",
+ [kXDGRuntimeDir] = NULL,
+ [kXDGConfigDirs] = NULL,
+ [kXDGDataDirs] = NULL,
+};
+#endif
+
/// Defaults for XDGVarType values
///
/// Used in case environment variables contain nothing. Need to be expanded.
static const char *const xdg_defaults[] = {
#ifdef WIN32
- // Windows
- [kXDGConfigHome] = "$LOCALAPPDATA",
- [kXDGDataHome] = "$LOCALAPPDATA",
- [kXDGCacheHome] = "$TEMP",
+ [kXDGConfigHome] = "~\\AppData\\Local",
+ [kXDGDataHome] = "~\\AppData\\Local",
+ [kXDGCacheHome] = "~\\AppData\\Local\\Temp",
[kXDGRuntimeDir] = NULL,
[kXDGConfigDirs] = NULL,
[kXDGDataDirs] = NULL,
#else
- // Linux, BSD, CYGWIN, Apple
[kXDGConfigHome] = "~/.config",
[kXDGDataHome] = "~/.local/share",
[kXDGCacheHome] = "~/.cache",
@@ -50,12 +62,19 @@ char *stdpaths_get_xdg_var(const XDGVarType idx)
const char *const env = xdg_env_vars[idx];
const char *const fallback = xdg_defaults[idx];
- const char *const env_val = os_getenv(env);
+ const char *env_val = os_getenv(env);
+
+#ifdef WIN32
+ if (env_val == NULL && xdg_defaults_env_vars[idx] != NULL) {
+ env_val = os_getenv(xdg_defaults_env_vars[idx]);
+ }
+#endif
+
char *ret = NULL;
if (env_val != NULL) {
ret = xstrdup(env_val);
} else if (fallback) {
- ret = (char *) expand_env_save((char_u *)fallback);
+ ret = (char *)expand_env_save((char_u *)fallback);
}
return ret;
@@ -69,7 +88,7 @@ char *stdpaths_get_xdg_var(const XDGVarType idx)
///
/// In WIN32 get_xdg_home(kXDGDataHome) returns `{xdg_directory}/nvim-data` to
/// avoid storing configuration and data files in the same path.
-static char *get_xdg_home(const XDGVarType idx)
+char *get_xdg_home(const XDGVarType idx)
FUNC_ATTR_WARN_UNUSED_RESULT
{
char *dir = stdpaths_get_xdg_var(idx);
@@ -100,18 +119,30 @@ char *stdpaths_user_conf_subpath(const char *fname)
///
/// @param[in] fname New component of the path.
/// @param[in] trailing_pathseps Amount of trailing path separators to add.
+/// @param[in] escape_commas If true, all commas will be escaped.
///
-/// @return [allocated] `$XDG_DATA_HOME/nvim/{fname}`
+/// @return [allocated] `$XDG_DATA_HOME/nvim/{fname}`.
char *stdpaths_user_data_subpath(const char *fname,
- const size_t trailing_pathseps)
+ const size_t trailing_pathseps,
+ const bool escape_commas)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_RET
{
char *ret = concat_fnames_realloc(get_xdg_home(kXDGDataHome), fname, true);
- if (trailing_pathseps) {
- const size_t len = strlen(ret);
- ret = xrealloc(ret, len + trailing_pathseps + 1);
- memset(ret + len, PATHSEP, trailing_pathseps);
- ret[len + trailing_pathseps] = NUL;
+ const size_t len = strlen(ret);
+ const size_t numcommas = (escape_commas ? memcnt(ret, ',', len) : 0);
+ if (numcommas || trailing_pathseps) {
+ ret = xrealloc(ret, len + trailing_pathseps + numcommas + 1);
+ for (size_t i = 0 ; i < len + numcommas ; i++) {
+ if (ret[i] == ',') {
+ memmove(ret + i + 1, ret + i, len - i + numcommas);
+ ret[i] = '\\';
+ i++;
+ }
+ }
+ if (trailing_pathseps) {
+ memset(ret + len + numcommas, PATHSEP, trailing_pathseps);
+ }
+ ret[len + trailing_pathseps + numcommas] = NUL;
}
return ret;
}