Imported Upstream version 2.0.90
[platform/upstream/kbd.git] / src / libkeymap / kmap.c
index 8bfc3be..a87314c 100644 (file)
@@ -1,34 +1,36 @@
+#include "config.h"
+
 #include <stdlib.h>
 #include <string.h>
 
-#include "nls.h"
-#include "kbd.h"
-
 #include "keymap.h"
 
+#include "libcommon.h"
 #include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 
-int
-lk_map_exists(struct lk_ctx *ctx, unsigned int k_table)
+int lk_map_exists(struct lk_ctx *ctx, int k_table)
 {
        return (lk_array_get_ptr(ctx->keymap, k_table) != NULL);
 }
 
-int
-lk_get_keys_total(struct lk_ctx *ctx, unsigned int k_table)
+int lk_get_keys_total(struct lk_ctx *ctx, int k_table)
 {
        struct lk_array *map;
        map = lk_array_get_ptr(ctx->keymap, k_table);
        if (!map) {
                return 0;
        }
-       return map->total;
+
+       if (map->total >= NR_KEYS) {
+               return -1;
+       }
+
+       return (int) map->total;
 }
 
-int
-lk_key_exists(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
+int lk_key_exists(struct lk_ctx *ctx, int k_table, int k_index)
 {
        struct lk_array *map;
        unsigned int *key;
@@ -46,8 +48,7 @@ lk_key_exists(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
        return (*key > 0);
 }
 
-int
-lk_add_map(struct lk_ctx *ctx, unsigned int k_table)
+int lk_add_map(struct lk_ctx *ctx, int k_table)
 {
        struct lk_array *keys;
 
@@ -72,11 +73,10 @@ lk_add_map(struct lk_ctx *ctx, unsigned int k_table)
        return 0;
 }
 
-int
-lk_get_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
+int lk_get_key(struct lk_ctx *ctx, int k_table, int k_index)
 {
        struct lk_array *map;
-       unsigned int *key;
+       int *key;
 
        map = lk_array_get_ptr(ctx->keymap, k_table);
        if (!map) {
@@ -89,11 +89,10 @@ lk_get_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
                return K_HOLE;
        }
 
-       return (*key)-1;
+       return (*key) - 1;
 }
 
-int
-lk_del_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
+int lk_del_key(struct lk_ctx *ctx, int k_table, int k_index)
 {
        struct lk_array *map;
 
@@ -108,29 +107,25 @@ lk_del_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index)
 
        if (lk_array_unset(map, k_index) < 0) {
                ERR(ctx, _("unable to unset key %d for table %d"),
-                       k_index, k_table);
+                   k_index, k_table);
                return -1;
        }
 
        return 0;
 }
 
-int
-lk_add_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index, int keycode)
+int lk_add_key(struct lk_ctx *ctx, int k_table, int k_index, int keycode)
 {
        struct lk_array *map;
-       unsigned int code = keycode + 1;
+       int code = keycode + 1;
 
        if (keycode == CODE_FOR_UNKNOWN_KSYM) {
-               /* is safer not to be silent in this case, 
+               /* is safer not to be silent in this case,
                 * it can be caused by coding errors as well. */
                ERR(ctx, _("lk_add_key called with bad keycode %d"), keycode);
                return -1;
        }
 
-       if (!k_index && keycode == K_NOSUCHMAP)
-               return 0;
-
        map = lk_array_get_ptr(ctx->keymap, k_table);
        if (!map) {
                if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
@@ -151,14 +146,14 @@ lk_add_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index, int k
 
        if (lk_array_set(map, k_index, &code) < 0) {
                ERR(ctx, _("unable to set key %d for table %d"),
-                       k_index, k_table);
+                   k_index, k_table);
                return -1;
        }
 
        if (ctx->keywords & LK_KEYWORD_ALTISMETA) {
-               unsigned int alttable = k_table | M_ALT;
-               int type = KTYP(keycode);
-               int val = KVAL(keycode);
+               int alttable = k_table | M_ALT;
+               int type     = KTYP(keycode);
+               int val      = KVAL(keycode);
 
                if (alttable != k_table && lk_map_exists(ctx, alttable) &&
                    !lk_key_exists(ctx, alttable, k_index) &&
@@ -172,94 +167,18 @@ lk_add_key(struct lk_ctx *ctx, unsigned int k_table, unsigned int k_index, int k
        return 0;
 }
 
-int
-lk_get_func(struct lk_ctx *ctx, struct kbsentry *kbs)
-{
-       char *s;
-
-       s = lk_array_get_ptr(ctx->func_table, kbs->kb_func);
-       if (!s) {
-               ERR(ctx, _("func %d not allocated"), kbs->kb_func);
-               return -1;
-       }
-
-       strncpy((char *)kbs->kb_string, s, sizeof(kbs->kb_string));
-       kbs->kb_string[sizeof(kbs->kb_string) - 1] = 0;
-
-       return 0;
-}
-
-
-int
-lk_add_func(struct lk_ctx *ctx, struct kbsentry kbs)
-{
-       char *s;
-
-       s = lk_array_get_ptr(ctx->func_table, kbs.kb_func);
-       if (s)
-               free(s);
-
-       s = strdup((char *)kbs.kb_string);
-
-       if (lk_array_set(ctx->func_table, kbs.kb_func, &s) < 0) {
-               free(s);
-               ERR(ctx, _("out of memory"));
-               return -1;
-       }
-
-       return 0;
-}
-
-int
-lk_add_diacr(struct lk_ctx *ctx, struct lk_kbdiacr *dcr)
-{
-       struct lk_kbdiacr *ptr;
-
-       ptr = malloc(sizeof(struct lk_kbdiacr));
-       if (!ptr) {
-               ERR(ctx, _("out of memory"));
-               return -1;
-       }
-
-       ptr->diacr  = dcr->diacr;
-       ptr->base   = dcr->base;
-       ptr->result = dcr->result;
-
-       lk_array_append(ctx->accent_table, &ptr);
-
-       return 0;
-}
-
-int
-lk_add_compose(struct lk_ctx *ctx, struct lk_kbdiacr *dcr)
-{
-       struct lk_kbdiacr dcr0;
-       int direction = TO_8BIT;
-
-#ifdef KDSKBDIACRUC
-       if (ctx->flags & LK_FLAG_PREFER_UNICODE)
-               direction = TO_UNICODE;
-#endif
-
-       dcr0.diacr  = convert_code(ctx, dcr->diacr,  direction);
-       dcr0.base   = convert_code(ctx, dcr->base,   direction);
-       dcr0.result = convert_code(ctx, dcr->result, direction);
-
-       return lk_add_diacr(ctx, &dcr0);
-}
-
 static int
-do_constant_key(struct lk_ctx *ctx, int i, u_short key)
+do_constant_key(struct lk_ctx *ctx, int i, int key)
 {
-       int typ, val;
-       unsigned int j;
+       int j, typ, val;
 
        typ = KTYP(key);
        val = KVAL(key);
 
        if ((typ == KT_LATIN || typ == KT_LETTER) &&
            ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'))) {
-               u_short defs[16];
+               int defs[16];
+
                defs[0] = K(KT_LETTER, val);
                defs[1] = K(KT_LETTER, val ^ 32);
                defs[2] = defs[0];
@@ -299,10 +218,9 @@ do_constant_key(struct lk_ctx *ctx, int i, u_short key)
        return 0;
 }
 
-int
-lk_add_constants(struct lk_ctx *ctx)
+int lk_add_constants(struct lk_ctx *ctx)
 {
-       unsigned int i, r0 = 0;
+       int i, r0 = 0;
 
        if (ctx->keywords & LK_KEYWORD_KEYMAPS) {
                while (r0 < ctx->keymap->total && !lk_map_exists(ctx, r0))
@@ -311,7 +229,7 @@ lk_add_constants(struct lk_ctx *ctx)
 
        for (i = 0; i < ctx->key_constant->total; i++) {
                char *constant;
-               u_short key;
+               int key;
 
                constant = lk_array_get(ctx->key_constant, i);
                if (!constant || !(*constant))