Fix typo in ensure-stable-doc-urls.py
[platform/upstream/libxkbcommon.git] / src / context.c
index 768fe5c..b8b214b 100644 (file)
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <errno.h>
-#ifdef _MSC_VER
-# include <direct.h>
-# include <io.h>
-# ifndef S_ISDIR
-#  define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
-# endif
-#else
-# include <unistd.h>
-#endif
 
 #include "xkbcommon/xkbcommon.h"
 #include "utils.h"
 #include "context.h"
 
+
 /**
  * Append one directory to the context's include path.
  */
@@ -50,7 +42,7 @@ XKB_EXPORT int
 xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
 {
     struct stat stat_buf;
-    int err;
+    int err = ENOMEM;
     char *tmp;
 
     tmp = strdup(path);
@@ -84,9 +76,16 @@ err:
 }
 
 const char *
+xkb_context_include_path_get_extra_path(struct xkb_context *ctx)
+{
+    const char *extra = xkb_context_getenv(ctx, "XKB_CONFIG_EXTRA_PATH");
+    return extra ? extra : DFLT_XKB_CONFIG_EXTRA_PATH;
+}
+
+const char *
 xkb_context_include_path_get_system_path(struct xkb_context *ctx)
 {
-    const char *root = secure_getenv("XKB_CONFIG_ROOT");
+    const char *root = xkb_context_getenv(ctx, "XKB_CONFIG_ROOT");
     return root ? root : DFLT_XKB_CONFIG_ROOT;
 }
 
@@ -96,37 +95,38 @@ xkb_context_include_path_get_system_path(struct xkb_context *ctx)
 XKB_EXPORT int
 xkb_context_include_path_append_default(struct xkb_context *ctx)
 {
-    const char *home, *xdg, *root;
+    const char *home, *xdg, *root, *extra;
     char *user_path;
-    int err;
     int ret = 0;
 
-    home = secure_getenv("HOME");
+    home = xkb_context_getenv(ctx, "HOME");
 
-    xdg = secure_getenv("XDG_CONFIG_HOME");
+    xdg = xkb_context_getenv(ctx, "XDG_CONFIG_HOME");
     if (xdg != NULL) {
-        err = asprintf(&user_path, "%s/xkb", xdg);
-        if (err >= 0) {
+        user_path = asprintf_safe("%s/xkb", xdg);
+        if (user_path) {
             ret |= xkb_context_include_path_append(ctx, user_path);
             free(user_path);
         }
     } else if (home != NULL) {
         /* XDG_CONFIG_HOME fallback is $HOME/.config/ */
-        err = asprintf(&user_path, "%s/.config/xkb", home);
-        if (err >= 0) {
+        user_path = asprintf_safe("%s/.config/xkb", home);
+        if (user_path) {
             ret |= xkb_context_include_path_append(ctx, user_path);
             free(user_path);
         }
     }
 
     if (home != NULL) {
-        err = asprintf(&user_path, "%s/.xkb", home);
-        if (err >= 0) {
+        user_path = asprintf_safe("%s/.xkb", home);
+        if (user_path) {
             ret |= xkb_context_include_path_append(ctx, user_path);
             free(user_path);
         }
     }
 
+    extra = xkb_context_include_path_get_extra_path(ctx);
+    ret |= xkb_context_include_path_append(ctx, extra);
     root = xkb_context_include_path_get_system_path(ctx);
     ret |= xkb_context_include_path_append(ctx, root);
 
@@ -202,6 +202,7 @@ xkb_context_unref(struct xkb_context *ctx)
     if (!ctx || --ctx->refcnt > 0)
         return;
 
+    free(ctx->x11_atom_cache);
     xkb_context_include_path_clear(ctx);
     atom_table_free(ctx->atom_table);
     free(ctx);
@@ -289,13 +290,15 @@ xkb_context_new(enum xkb_context_flags flags)
     ctx->log_fn = default_log_fn;
     ctx->log_level = XKB_LOG_LEVEL_ERROR;
     ctx->log_verbosity = 0;
+    ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
+    ctx->use_secure_getenv = !(flags & XKB_CONTEXT_NO_SECURE_GETENV);
 
     /* Environment overwrites defaults. */
-    env = secure_getenv("XKB_LOG_LEVEL");
+    env = xkb_context_getenv(ctx, "XKB_LOG_LEVEL");
     if (env)
         xkb_context_set_log_level(ctx, log_level(env));
 
-    env = secure_getenv("XKB_LOG_VERBOSITY");
+    env = xkb_context_getenv(ctx, "XKB_LOG_VERBOSITY");
     if (env)
         xkb_context_set_log_verbosity(ctx, log_verbosity(env));
 
@@ -307,14 +310,14 @@ xkb_context_new(enum xkb_context_flags flags)
         return NULL;
     }
 
-    ctx->use_environment_names = !(flags & XKB_CONTEXT_NO_ENVIRONMENT_NAMES);
-
     ctx->atom_table = atom_table_new();
     if (!ctx->atom_table) {
         xkb_context_unref(ctx);
         return NULL;
     }
 
+    ctx->x11_atom_cache = NULL;
+
     return ctx;
 }