Remove the exception for tizen common in .spec file
[platform/upstream/libxkbcommon.git] / src / context.c
index 923060e..bcec16c 100644 (file)
  * Author: Daniel Stone <daniel@fooishbar.org>
  */
 
+#include "config.h"
+
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <ctype.h>
 #include <errno.h>
-#include <unistd.h>
 
 #include "xkbcommon/xkbcommon.h"
 #include "utils.h"
 #include "context.h"
 
-struct xkb_context {
-    int refcnt;
-
-    ATTR_PRINTF(3, 0) void (*log_fn)(struct xkb_context *ctx,
-                                     enum xkb_log_level level,
-                                     const char *fmt, va_list args);
-    enum xkb_log_level log_level;
-    int log_verbosity;
-    void *user_data;
-
-    struct xkb_rule_names names_dflt;
-
-    darray(char *) includes;
-    darray(char *) failed_includes;
-
-    struct atom_table *atom_table;
-
-    /* Buffer for the *Text() functions. */
-    char text_buffer[2048];
-    size_t text_next;
-
-    unsigned int use_environment_names : 1;
-};
 
 /**
  * Append one directory to the context's include path.
@@ -65,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);
@@ -73,48 +50,86 @@ xkb_context_include_path_append(struct xkb_context *ctx, const char *path)
         goto err;
 
     err = stat(path, &stat_buf);
-    if (err != 0)
+    if (err != 0) {
+        err = errno;
         goto err;
-    if (!S_ISDIR(stat_buf.st_mode))
+    }
+    if (!S_ISDIR(stat_buf.st_mode)) {
+        err = ENOTDIR;
         goto err;
+    }
 
-#if defined(HAVE_EACCESS)
-    if (eaccess(path, R_OK | X_OK) != 0)
+    if (!check_eaccess(path, R_OK | X_OK)) {
+        err = EACCES;
         goto err;
-#elif defined(HAVE_EUIDACCESS)
-    if (euidaccess(path, R_OK | X_OK) != 0)
-        goto err;
-#endif
+    }
 
     darray_append(ctx->includes, tmp);
+    log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID, "Include path added: %s\n", tmp);
+
     return 1;
 
 err:
     darray_append(ctx->failed_includes, tmp);
+    log_dbg(ctx, XKB_LOG_MESSAGE_NO_ID,
+            "Include path failed: %s (%s)\n", tmp, strerror(err));
     return 0;
 }
 
+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 = xkb_context_getenv(ctx, "XKB_CONFIG_ROOT");
+    return root ? root : DFLT_XKB_CONFIG_ROOT;
+}
+
 /**
  * Append the default include directories to the context.
  */
 XKB_EXPORT int
 xkb_context_include_path_append_default(struct xkb_context *ctx)
 {
-    const char *home;
+    const char *home, *xdg, *root, *extra;
     char *user_path;
-    int err;
     int ret = 0;
 
-    ret |= xkb_context_include_path_append(ctx, DFLT_XKB_CONFIG_ROOT);
+    home = xkb_context_getenv(ctx, "HOME");
+
+    xdg = xkb_context_getenv(ctx, "XDG_CONFIG_HOME");
+    if (xdg != NULL) {
+        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/ */
+        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) {
+        user_path = asprintf_safe("%s/.xkb", home);
+        if (user_path) {
+            ret |= xkb_context_include_path_append(ctx, user_path);
+            free(user_path);
+        }
+    }
 
-    home = getenv("HOME");
-    if (!home)
-        return ret;
-    err = asprintf(&user_path, "%s/.xkb", home);
-    if (err <= 0)
-        return ret;
-    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);
 
     return ret;
 }
@@ -155,12 +170,6 @@ xkb_context_num_include_paths(struct xkb_context *ctx)
     return darray_size(ctx->includes);
 }
 
-unsigned int
-xkb_context_num_failed_include_paths(struct xkb_context *ctx)
-{
-    return darray_size(ctx->failed_includes);
-}
-
 /**
  * Returns the given entry in the context's include path, or NULL if an
  * invalid index is passed.
@@ -174,16 +183,6 @@ xkb_context_include_path_get(struct xkb_context *ctx, unsigned int idx)
     return darray_item(ctx->includes, idx);
 }
 
-const char *
-xkb_context_failed_include_path_get(struct xkb_context *ctx,
-                                    unsigned int idx)
-{
-    if (idx >= xkb_context_num_failed_include_paths(ctx))
-        return NULL;
-
-    return darray_item(ctx->failed_includes, idx);
-}
-
 /**
  * Take a new reference on the context.
  */
@@ -204,6 +203,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);
@@ -214,15 +214,15 @@ log_level_to_prefix(enum xkb_log_level level)
 {
     switch (level) {
     case XKB_LOG_LEVEL_DEBUG:
-        return "Debug:";
+        return "xkbcommon: DEBUG: ";
     case XKB_LOG_LEVEL_INFO:
-        return "Info:";
+        return "xkbcommon: INFO: ";
     case XKB_LOG_LEVEL_WARNING:
-        return "Warning:";
+        return "xkbcommon: WARNING: ";
     case XKB_LOG_LEVEL_ERROR:
-        return "Error:";
+        return "xkbcommon: ERROR: ";
     case XKB_LOG_LEVEL_CRITICAL:
-        return "Critical:";
+        return "xkbcommon: CRITICAL: ";
     default:
         return NULL;
     }
@@ -235,7 +235,7 @@ default_log_fn(struct xkb_context *ctx, enum xkb_log_level level,
     const char *prefix = log_level_to_prefix(level);
 
     if (prefix)
-        fprintf(stderr, "%-10s", prefix);
+        fprintf(stderr, "%s", prefix);
     vfprintf(stderr, fmt, args);
 }
 
@@ -246,7 +246,7 @@ log_level(const char *level) {
 
     errno = 0;
     lvl = strtol(level, &endptr, 10);
-    if (errno == 0 && (endptr[0] == '\0' || isspace(endptr[0])))
+    if (errno == 0 && (endptr[0] == '\0' || is_space(endptr[0])))
         return lvl;
     if (istreq_prefix("crit", level))
         return XKB_LOG_LEVEL_CRITICAL;
@@ -275,14 +275,6 @@ log_verbosity(const char *verbosity) {
     return 0;
 }
 
-#ifndef DEFAULT_XKB_VARIANT
-#define DEFAULT_XKB_VARIANT NULL
-#endif
-
-#ifndef DEFAULT_XKB_OPTIONS
-#define DEFAULT_XKB_OPTIONS NULL
-#endif
-
 /**
  * Create a new context.
  */
@@ -299,74 +291,36 @@ 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 = getenv("XKB_LOG_LEVEL");
+    env = xkb_context_getenv(ctx, "XKB_LOG_LEVEL");
     if (env)
         xkb_context_set_log_level(ctx, log_level(env));
 
-    env = getenv("XKB_LOG_VERBOSITY");
+    env = xkb_context_getenv(ctx, "XKB_LOG_VERBOSITY");
     if (env)
         xkb_context_set_log_verbosity(ctx, log_verbosity(env));
 
     if (!(flags & XKB_CONTEXT_NO_DEFAULT_INCLUDES) &&
         !xkb_context_include_path_append_default(ctx)) {
-        log_err(ctx, "failed to add default include path %s\n",
+        log_err(ctx, XKB_LOG_MESSAGE_NO_ID,
+                "failed to add default include path %s\n",
                 DFLT_XKB_CONFIG_ROOT);
         xkb_context_unref(ctx);
         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;
     }
 
-    return ctx;
-}
-
-xkb_atom_t
-xkb_atom_lookup(struct xkb_context *ctx, const char *string)
-{
-    return atom_lookup(ctx->atom_table, string, strlen(string));
-}
-
-xkb_atom_t
-xkb_atom_intern(struct xkb_context *ctx, const char *string)
-{
-    return atom_intern(ctx->atom_table, string, strlen(string), false);
-}
-
-xkb_atom_t
-xkb_atom_steal(struct xkb_context *ctx, char *string)
-{
-    return atom_intern(ctx->atom_table, string, strlen(string), true);
-}
-
-char *
-xkb_atom_strdup(struct xkb_context *ctx, xkb_atom_t atom)
-{
-    return atom_strdup(ctx->atom_table, atom);
-}
-
-const char *
-xkb_atom_text(struct xkb_context *ctx, xkb_atom_t atom)
-{
-    return atom_text(ctx->atom_table, atom);
-}
-
-void
-xkb_log(struct xkb_context *ctx, enum xkb_log_level level,
-        const char *fmt, ...)
-{
-    va_list args;
+    ctx->x11_atom_cache = NULL;
 
-    va_start(args, fmt);
-    ctx->log_fn(ctx, level, fmt, args);
-    va_end(args);
+    return ctx;
 }
 
 XKB_EXPORT void
@@ -415,78 +369,3 @@ xkb_context_set_user_data(struct xkb_context *ctx, void *user_data)
 {
     ctx->user_data = user_data;
 }
-
-char *
-xkb_context_get_buffer(struct xkb_context *ctx, size_t size)
-{
-    char *rtrn;
-
-    if (size >= sizeof(ctx->text_buffer))
-        return NULL;
-
-    if (sizeof(ctx->text_buffer) - ctx->text_next <= size)
-        ctx->text_next = 0;
-
-    rtrn = &ctx->text_buffer[ctx->text_next];
-    ctx->text_next += size;
-
-    return rtrn;
-}
-
-const char *
-xkb_context_get_default_rules(struct xkb_context *ctx)
-{
-    const char *env = NULL;
-
-    if (ctx->use_environment_names)
-        env = getenv("XKB_DEFAULT_RULES");
-
-    return env ? env : DEFAULT_XKB_RULES;
-}
-
-const char *
-xkb_context_get_default_model(struct xkb_context *ctx)
-{
-    const char *env = NULL;
-
-    if (ctx->use_environment_names)
-        env = getenv("XKB_DEFAULT_MODEL");
-
-    return env ? env : DEFAULT_XKB_MODEL;
-}
-
-const char *
-xkb_context_get_default_layout(struct xkb_context *ctx)
-{
-    const char *env = NULL;
-
-    if (ctx->use_environment_names)
-        env = getenv("XKB_DEFAULT_LAYOUT");
-
-    return env ? env : DEFAULT_XKB_LAYOUT;
-}
-
-const char *
-xkb_context_get_default_variant(struct xkb_context *ctx)
-{
-    const char *env = NULL;
-    const char *layout = getenv("XKB_DEFAULT_VARIANT");
-
-    /* We don't want to inherit the variant if they haven't also set a
-     * layout, since they're so closely paired. */
-    if (layout && ctx->use_environment_names)
-        env = getenv("XKB_DEFAULT_VARIANT");
-
-    return env ? env : DEFAULT_XKB_VARIANT;
-}
-
-const char *
-xkb_context_get_default_options(struct xkb_context *ctx)
-{
-    const char *env = NULL;
-
-    if (ctx->use_environment_names)
-        env = getenv("XKB_DEFAULT_OPTIONS");
-
-    return env ? env : DEFAULT_XKB_OPTIONS;
-}