Remove static arrays that is used internally for the parser
authorAlexey Gladkov <gladkov.alexey@gmail.com>
Wed, 29 May 2013 10:57:27 +0000 (14:57 +0400)
committerAlexey Gladkov <gladkov.alexey@gmail.com>
Wed, 29 May 2013 10:57:27 +0000 (14:57 +0400)
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
src/libkeymap/analyze.l
src/libkeymap/array.c
src/libkeymap/common.c
src/libkeymap/keymap/array.h
src/libkeymap/keymap/data.h
src/libkeymap/kmap.c
src/libkeymap/parser.y

index 05cf574..e21673f 100644 (file)
@@ -345,7 +345,7 @@ To                      to|To|TO
 {Comment}.*/{Eol}      ; /* do nothing */
 {Equals}               {
                                yy_push_state(RVALUE, yyscanner);
-                               yyextra->rvalct = 0;
+                               lk_array_empty(yyextra->key_line);
                                return(EQUALS);
                        }
 {String}               {
index 659f91a..5cf9dec 100644 (file)
@@ -35,6 +35,18 @@ lk_array_free(struct lk_array *a)
 }
 
 int
+lk_array_empty(struct lk_array *a)
+{
+       if (!a)
+               return -EINVAL;
+
+       memset(a->array, 0, (a->memb * a->total));
+       a->count = 0;
+
+       return 0;
+}
+
+int
 lk_array_exist(struct lk_array *a, unsigned int i)
 {
        char *s;
@@ -120,3 +132,17 @@ lk_array_unset(struct lk_array *a, unsigned int i)
 
        return 0;
 }
+
+int
+lk_array_append(struct lk_array *a, const void *e)
+{
+       int ret = array_resize(a, a->count);
+
+       if (ret < 0)
+               return ret;
+
+       memcpy(a->array + (a->memb * a->count), e, a->memb);
+       a->count++;
+
+       return 0;
+}
index be0ffdf..33795fe 100644 (file)
@@ -105,16 +105,18 @@ lk_init(struct keymap *kmap)
        lk_set_log_priority(kmap, LOG_ERR);
 
        kmap->keymap = malloc(sizeof(struct lk_array));
-       if (!(kmap->keymap)) {
-               ERR(kmap, "out of memory");
-               return -1;
-       }
+       kmap->key_constant = malloc(sizeof(struct lk_array));
+       kmap->key_line = malloc(sizeof(struct lk_array));
 
-       if (lk_array_init(kmap->keymap, sizeof(void *), 0) < 0) {
+       if (!(kmap->keymap) || !(kmap->key_constant) || !(kmap->key_line)) {
                ERR(kmap, "out of memory");
                return -1;
        }
 
+       lk_array_init(kmap->keymap, sizeof(void*), 0);
+       lk_array_init(kmap->key_constant, sizeof(char), 0);
+       lk_array_init(kmap->key_line, sizeof(int), 0);
+
        return 0;
 }
 
@@ -149,5 +151,17 @@ lk_free(struct keymap *kmap)
                kmap->keymap = NULL;
        }
 
+       if (kmap->key_constant) {
+               lk_array_free(kmap->key_constant);
+               free(kmap->key_constant);
+               kmap->key_constant = NULL;
+       }
+
+       if (kmap->key_line) {
+               lk_array_free(kmap->key_line);
+               free(kmap->key_line);
+               kmap->key_line = NULL;
+       }
+
        return 0;
 }
index ee6144a..2f19726 100644 (file)
@@ -11,6 +11,10 @@ struct lk_array {
 int lk_array_init(struct lk_array *a, size_t memb, size_t size);
 int lk_array_free(struct lk_array *a);
 
+int lk_array_empty(struct lk_array *a);
+
+int lk_array_append(struct lk_array *a, const void *e);
+
 int   lk_array_set(struct lk_array *a, unsigned int i, const void *e);
 void *lk_array_get(struct lk_array *a, unsigned int i);
 void *lk_array_get_ptr(struct lk_array *a, unsigned int i);
index cc435cb..4edcde9 100644 (file)
@@ -43,14 +43,16 @@ struct keymap {
        accent_entry accent_table[MAX_DIACR];
        unsigned int accent_table_size;    
 
-       char key_is_constant[NR_KEYS];
+       struct lk_array *key_constant;
 
-       int mod;                     /* Line by line modifiers */
-       int key_buf[MAX_NR_KEYMAPS]; /* Key definitions on one line */
+       /* Key definitions on one line */
+       struct lk_array *key_line;
+
+       /* Line by line modifiers */
+       int mod;
 
        unsigned int charset;
 
-       int rvalct;
        lkfile_t *stack[MAX_INCLUDE_DEPTH];
 
        int log_priority;
index 8dfa683..8b3681d 100644 (file)
@@ -308,10 +308,12 @@ lk_add_constants(struct keymap *kmap)
                        r0++;
        }
 
-       for (i = 0; i < NR_KEYS; i++) {
+       for (i = 0; i < kmap->key_constant->total; i++) {
+               char *constant;
                u_short key;
 
-               if (!kmap->key_is_constant[i])
+               constant = lk_array_get(kmap->key_constant, i);
+               if (!constant || !(*constant))
                        continue;
 
                if (!lk_map_exist(kmap, r0)) {
index d77c7b8..a51a682 100644 (file)
@@ -309,14 +309,15 @@ modifier  : SHIFT         { kmap->mod |= M_SHIFT; }
                ;
 fullline       : KEYCODE NUMBER EQUALS rvalue0 EOL
                        {
-                               unsigned int j;
-                               int i, keycode;
+                               unsigned int j, i, keycode;
+                               int *val;
 
-                               if (kmap->rvalct == 1) {
+                               if (kmap->key_line->count == 1) {
+                                       char one = 1;
                                        /* Some files do not have a keymaps line, and
                                         * we have to wait until all input has been read
                                         * before we know which maps to fill. */
-                                       kmap->key_is_constant[$2] = 1;
+                                       lk_array_set(kmap->key_constant, $2, &one);
 
                                        /* On the other hand, we now have include files,
                                         * and it should be possible to override lines
@@ -337,10 +338,13 @@ fullline  : KEYCODE NUMBER EQUALS rvalue0 EOL
                                                if (!lk_map_exist(kmap, j))
                                                        continue;
 
-                                               if (kmap->rvalct != 1 || i == 0) {
-                                                       keycode = (i < kmap->rvalct)
-                                                               ? kmap->key_buf[i]
-                                                               : K_HOLE;
+                                               if (kmap->key_line->count != 1 || i == 0) {
+                                                       keycode = K_HOLE;
+
+                                                       if (i < kmap->key_line->count) {
+                                                               val = lk_array_get(kmap->key_line, i);
+                                                               keycode = *val;
+                                                       }
 
                                                        if (lk_add_key(kmap, j, $2, keycode) < 0)
                                                                YYERROR;
@@ -348,14 +352,16 @@ fullline  : KEYCODE NUMBER EQUALS rvalue0 EOL
                                                i++;
                                        }
 
-                                       if (i < kmap->rvalct) {
+                                       if (i < kmap->key_line->count) {
                                                ERR(kmap, _("too many (%d) entries on one line"),
-                                                       kmap->rvalct);
+                                                       kmap->key_line->count);
                                                YYERROR;
                                        }
                                } else {
-                                       for (i = 0; i < kmap->rvalct; i++) {
-                                               if (lk_add_key(kmap, i, $2, kmap->key_buf[i]) < 0)
+                                       for (i = 0; i < kmap->key_line->count; i++) {
+                                               val = lk_array_get(kmap->key_line, i);
+
+                                               if (lk_add_key(kmap, i, $2, *val) < 0)
                                                        YYERROR;
                                        }
                                }
@@ -367,11 +373,8 @@ rvalue0            :
                ;
 rvalue1                : rvalue
                        {
-                               if (kmap->rvalct >= MAX_NR_KEYMAPS) {
-                                       ERR(kmap, _("too many key definitions on one line"));
-                                       YYERROR;
-                               }
-                               kmap->key_buf[kmap->rvalct++] = $1;
+                               int val = $1;
+                               lk_array_append(kmap->key_line, &val);
                        }
                ;
 rvalue         : NUMBER        { $$ = convert_code(kmap, $1, TO_AUTO);         }