Hide syms array
authorAlexey Gladkov <gladkov.alexey@gmail.com>
Sun, 15 Jun 2014 23:08:05 +0000 (03:08 +0400)
committerAlexey Gladkov <gladkov.alexey@gmail.com>
Sun, 15 Jun 2014 23:08:05 +0000 (03:08 +0400)
Broke backward compatibility: lk_dump_symbols.

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
src/dumpkeys.c
src/libkeymap/dump.c
src/libkeymap/keymap/dump.h
src/libkeymap/ksyms.c
src/libkeymap/ksyms.h
src/libkeymap/loadkeys.c
src/libkeymap/parser.y
src/libkeymap/summary.c

index cc7ae36..736ca35 100644 (file)
@@ -168,7 +168,7 @@ main (int argc, char *argv[]) {
                if (long_info) {
                        printf(_("Symbols recognized by %s:\n(numeric value, symbol)\n\n"),
                                progname);
-                       lk_dump_symbols(stdout);
+                       lk_dump_symbols(ctx, stdout);
                }
                exit(EXIT_SUCCESS);
        }
index 3cab746..20f6a84 100644 (file)
@@ -248,7 +248,7 @@ lk_dump_funcs(struct lk_ctx *ctx, FILE *fd)
                if (!ptr)
                        continue;
 
-               fprintf(fd, "string %s = \"", syms[KT_FN].table[i]);
+               fprintf(fd, "string %s = \"", get_sym(ctx, KT_FN, i));
 
                for (; *ptr; ptr++) {
                        if (*ptr == '"' || *ptr == '\\') {
@@ -352,8 +352,7 @@ print_mod(FILE *fd, int x)
 static void
 print_keysym(struct lk_ctx *ctx, FILE *fd, int code, char numeric)
 {
-       unsigned int t;
-       int v;
+       unsigned int t, v;
        const char *p;
        int plus;
 
@@ -373,11 +372,11 @@ print_keysym(struct lk_ctx *ctx, FILE *fd, int code, char numeric)
                fprintf(fd, "+");
                plus++;
        }
-       if (!numeric && t < syms_size && v < syms[t].size &&
-           (p = syms[t].table[v])[0])
+       if (!numeric && t < syms_size && v < get_sym_size(ctx, t) &&
+           (p = get_sym(ctx, t, v))[0])
                fprintf(fd, "%-*s", 16 - plus, p);
-       else if (!numeric && t == KT_META && v < 128 && v < syms[0].size &&
-                (p = syms[0].table[v])[0])
+       else if (!numeric && t == KT_META && v < 128 && v < get_sym_size(ctx, KT_LATIN) &&
+                (p = get_sym(ctx, KT_LATIN, v))[0])
                fprintf(fd, "Meta_%-11s", p);
        else
                fprintf(fd, "0x%04x         %s", code, plus ? "" : " ");
index a23bb7f..8057e3a 100644 (file)
@@ -98,6 +98,8 @@ void lk_dump_diacs(struct lk_ctx *ctx, FILE *fd);
  */
 char *lk_code_to_ksym(struct lk_ctx *ctx, int code);
 
+char *lk_get_sym(struct lk_ctx *ctx, unsigned int ktype, unsigned int index);
+
 /**
  * Converts a string to a numeric representation of the character.
  * @param ctx is a keymap library context.
@@ -109,6 +111,6 @@ int lk_ksym_to_unicode(struct lk_ctx *ctx, const char *code);
 
 int lk_get_kmapinfo(struct lk_ctx *ctx, struct kmapinfo *res);
 void lk_dump_summary(struct lk_ctx *ctx, FILE *fd, int console);
-void lk_dump_symbols(FILE *fd);
+void lk_dump_symbols(struct lk_ctx *ctx, FILE *fd);
 
 #endif /* LK_DUMP_H */
index 1290203..22b1336 100644 (file)
@@ -140,6 +140,38 @@ lk_set_charset(struct lk_ctx *ctx, const char *charset) {
        return 1;
 }
 
+unsigned int
+get_sym_size(struct lk_ctx *ctx, unsigned int ktype)
+{
+       if (ktype >= syms_size) {
+               ERR(ctx, _("unable to get symbol by wrong type: %d"), ktype);
+               return 0;
+       }
+
+       return syms[ktype].size;
+}
+
+const char *
+get_sym(struct lk_ctx *ctx, unsigned int ktype, unsigned int index)
+{
+       if (!get_sym_size(ctx, ktype))
+               return NULL;
+
+       if (index >= syms[ktype].size) {
+               ERR(ctx, _("unable to get symbol of %d type by wrong index: %d"), ktype, index);
+               return NULL;
+       }
+
+       return syms[ktype].table[index];
+}
+
+char *
+lk_get_sym(struct lk_ctx *ctx, unsigned int ktype, unsigned int index)
+{
+       const char *ksym = get_sym(ctx, ktype, index);
+       return (ksym ? strdup(ksym) : NULL);
+}
+
 const char *
 codetoksym(struct lk_ctx *ctx, int code) {
        unsigned int i;
@@ -151,7 +183,7 @@ codetoksym(struct lk_ctx *ctx, int code) {
 
        if (code < 0x1000) {    /* "traditional" keysym */
                if (code < 0x80)
-                       return iso646_syms[code];
+                       return get_sym(ctx, KT_LATIN, code);
 
                if (KTYP(code) == KT_META)
                        return NULL;
@@ -160,7 +192,7 @@ codetoksym(struct lk_ctx *ctx, int code) {
                        code = K(KT_LATIN, KVAL(code));
 
                if (KTYP(code) > KT_LATIN)
-                       return syms[KTYP(code)].table[KVAL(code)];
+                       return get_sym(ctx, KTYP(code), KVAL(code));
 
                i = ctx->charset;
                while (1) {
@@ -185,7 +217,7 @@ codetoksym(struct lk_ctx *ctx, int code) {
                code ^= 0xf000;
 
                if (code < 0x80)
-                       return iso646_syms[code];
+                       return get_sym(ctx, KT_LATIN, code);
 
                i = ctx->charset;
                while (1) {
@@ -213,20 +245,20 @@ codetoksym(struct lk_ctx *ctx, int code) {
 char *
 lk_code_to_ksym(struct lk_ctx *ctx, int code)
 {
-       const char *sym;
+       const char *s;
 
-       sym = codetoksym(ctx, code);
-       if (!sym)
+       s = codetoksym(ctx, code);
+       if (!s)
                return NULL;
 
-       return strdup(sym);
+       return strdup(s);
 }
 
 /* Functions for loadkeys. */
 
 static int
 kt_latin(struct lk_ctx *ctx, const char *s, int direction) {
-       int i, max;
+       unsigned int i, max;
 
        if (ctx->charset) {
                sym *p = (sym *) charsets[ctx->charset].charnames;
@@ -240,7 +272,7 @@ kt_latin(struct lk_ctx *ctx, const char *s, int direction) {
        max = (direction == TO_UNICODE ? 128 : syms[KT_LATIN].size);
 
        for (i = 0; i < max; i++) {
-               if (!strcmp(s, syms[KT_LATIN].table[i]))
+               if (!strcmp(s, get_sym(ctx, KT_LATIN, i)))
                        return K(KT_LATIN, i);
        }
 
@@ -249,8 +281,8 @@ kt_latin(struct lk_ctx *ctx, const char *s, int direction) {
 
 int
 ksymtocode(struct lk_ctx *ctx, const char *s, int direction) {
-       unsigned int i;
-       int n, j;
+       unsigned int i, j;
+       int n;
        int keycode;
        sym *p;
 
@@ -276,7 +308,7 @@ ksymtocode(struct lk_ctx *ctx, const char *s, int direction) {
 
        for (i = 1; i < syms_size; i++) {
                for (j = 0; j < syms[i].size; j++) {
-                       if (!strcmp(s,syms[i].table[j]))
+                       if (!strcmp(s, get_sym(ctx, i, j)))
                                return K(i, j);
                }
        }
index 42a9f96..84e851e 100644 (file)
@@ -10,11 +10,9 @@ typedef struct {
 
 typedef struct {
        const char * const *table;
-       const int size;
+       const unsigned int size;
 } syms_entry;
 
-extern syms_entry const syms[];
-
 struct syn {
        const char *synonym;
        const char *official_name;
@@ -32,6 +30,9 @@ extern const unsigned int syn_size;
 #define TO_8BIT 0
 #define TO_UNICODE 1
 
+const char *get_sym(struct lk_ctx *ctx, unsigned int ktype, unsigned int index);
+unsigned int get_sym_size(struct lk_ctx *ctx, unsigned int ktype);
+
 const char *codetoksym(struct lk_ctx *ctx, int code);
 int ksymtocode(struct lk_ctx *ctx, const char *s, int direction);
 int convert_code(struct lk_ctx *ctx, int code, int direction);
index 01a556b..93ee428 100644 (file)
@@ -159,7 +159,7 @@ deffuncs(struct lk_ctx *ctx, int fd)
                                if (s == NULL)
                                        return -1;
                                ERR(ctx, _("failed to bind string '%s' to function %s"),
-                                       s, syms[KT_FN].table[kbs.kb_func]);
+                                       s, get_sym(ctx, KT_FN, kbs.kb_func));
                                free(s);
                        } else {
                                ct++;
@@ -169,7 +169,7 @@ deffuncs(struct lk_ctx *ctx, int fd)
 
                        if (ioctl(fd, KDSKBSENT, (unsigned long)&kbs)) {
                                ERR(ctx, _("failed to clear string %s"),
-                                       syms[KT_FN].table[kbs.kb_func]);
+                                       get_sym(ctx, KT_FN, kbs.kb_func));
                        } else {
                                ct++;
                        }
index 17d953a..a656119 100644 (file)
@@ -252,7 +252,7 @@ strline             : STRING LITERAL EQUALS STRLITERAL EOL
 
                                if (KTYP($2) != KT_FN) {
                                        ERR(ctx, _("'%s' is not a function key symbol"),
-                                               syms[KTYP($2)].table[KVAL($2)]);
+                                               get_sym(ctx, KTYP($2), KVAL($2)));
                                        YYERROR;
                                }
 
index 316459a..ae36446 100644 (file)
@@ -115,22 +115,21 @@ lk_dump_summary(struct lk_ctx *ctx, FILE *fd, int console)
 }
 
 void
-lk_dump_symbols(FILE *fd)
+lk_dump_symbols(struct lk_ctx *ctx, FILE *fd)
 {
-       unsigned int t;
+       unsigned int t, v;
        modifier_t *mod;
-       int v;
        const char *p;
 
        for (t = 0; t < syms_size; t++) {
-           if (syms[t].size) {
-               for (v = 0; v < syms[t].size; v++) {
-                       if ((p = syms[t].table[v])[0])
+           if (get_sym_size(ctx, t)) {
+               for (v = 0; v < get_sym_size(ctx, t); v++) {
+                       if ((p = get_sym(ctx, t, v))[0])
                                fprintf(fd, "0x%04x\t%s\n", K(t, v), p);
                }
            } else if (t == KT_META) {
-               for (v = 0; v < syms[0].size && v < 128; v++) {
-                       if ((p = syms[0].table[v])[0])
+               for (v = 0; v < get_sym_size(ctx, KT_LATIN) && v < 128; v++) {
+                       if ((p = get_sym(ctx, KT_LATIN, v))[0])
                                fprintf(fd, "0x%04x\tMeta_%s\n", K(t, v), p);
                }
            }