Move lk_ctx content to private part of library
authorAlexey Gladkov <gladkov.alexey@gmail.com>
Wed, 24 Jul 2013 14:28:13 +0000 (18:28 +0400)
committerAlexey Gladkov <gladkov.alexey@gmail.com>
Sat, 27 Jul 2013 21:29:15 +0000 (01:29 +0400)
Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
24 files changed:
src/dumpkeys.c
src/libkeymap/Makefile.am
src/libkeymap/analyze.l
src/libkeymap/common.c
src/libkeymap/contextP.h [new file with mode: 0644]
src/libkeymap/dump.c
src/libkeymap/kernel.c
src/libkeymap/keymap/common.h
src/libkeymap/keymap/context.h
src/libkeymap/keymap/dump.h
src/libkeymap/kmap.c
src/libkeymap/ksyms.c
src/libkeymap/loadkeys.c
src/libkeymap/parser.y
src/libkeymap/summary.c
src/loadkeys.c
tests/libkeymap-bkeymap.c
tests/libkeymap-charset.c
tests/libkeymap-dumpkeys.c
tests/libkeymap-init.c
tests/libkeymap-keys.c
tests/libkeymap-kmap.c
tests/libkeymap-mktable.c
tests/libkeymap-parse.c

index aece68f..cc7ae36 100644 (file)
@@ -80,7 +80,7 @@ main (int argc, char *argv[]) {
        char keys_only = 0;
        char diac_only = 0;
 
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
        set_progname(argv[0]);
 
@@ -88,7 +88,10 @@ main (int argc, char *argv[]) {
        bindtextdomain(PACKAGE_NAME, LOCALEDIR);
        textdomain(PACKAGE_NAME);
 
-       lk_init(&ctx);
+       ctx = lk_init();
+       if (!ctx) {
+               exit(EXIT_FAILURE);
+       }
 
        while ((c = getopt_long(argc, argv,
                short_opts, long_opts, NULL)) != -1) {
@@ -122,10 +125,10 @@ main (int argc, char *argv[]) {
                                diac_only = 1;
                                break;
                        case 'v':
-                               lk_set_log_priority(&ctx, LOG_INFO);
+                               lk_set_log_priority(ctx, LOG_INFO);
                                break;
                        case 'c':
-                               if ((lk_set_charset(&ctx, optarg)) != 0) {
+                               if ((lk_set_charset(ctx, optarg)) != 0) {
                                        fprintf(stderr, _("unknown charset %s - ignoring charset request\n"),
                                                optarg);
                                        usage();
@@ -153,14 +156,14 @@ main (int argc, char *argv[]) {
        }
 
        if (kbd_mode == K_UNICODE) {
-               lk_set_parser_flags(&ctx, LK_FLAG_PREFER_UNICODE);
+               lk_set_parser_flags(ctx, LK_FLAG_PREFER_UNICODE);
        }
 
-       if ((rc = lk_kernel_keymap(&ctx, fd)) < 0)
+       if ((rc = lk_kernel_keymap(ctx, fd)) < 0)
                goto fail;
 
        if (short_info || long_info) {
-               lk_dump_summary(&ctx, stdout, fd);
+               lk_dump_summary(ctx, stdout, fd);
 
                if (long_info) {
                        printf(_("Symbols recognized by %s:\n(numeric value, symbol)\n\n"),
@@ -174,16 +177,16 @@ main (int argc, char *argv[]) {
        if (!diac_only) {
 #endif
        if (!funcs_only) {
-               lk_dump_keymap(&ctx, stdout, table, numeric);
+               lk_dump_keymap(ctx, stdout, table, numeric);
        }
 #ifdef KDGKBDIACR
        }
 
        if (!funcs_only && !keys_only)
-               lk_dump_diacs(&ctx, stdout);
+               lk_dump_diacs(ctx, stdout);
 #endif
 
- fail: lk_free(&ctx);
+ fail: lk_free(ctx);
        close(fd);
 
        if (rc < 0)
index 07d14ad..037dade 100644 (file)
@@ -41,6 +41,7 @@ libkeymap_la_SOURCES = \
        $(headers) \
        array.c \
        findfile.c common.c kernel.c dump.c kmap.c summary.c loadkeys.c \
+       contextP.h \
        parser.y parser.h analyze.l analyze.h \
        modifiers.c modifiers.h \
        ksyms.c ksyms.h $(ksyms_headers) \
index 887dedd..b6f96fd 100644 (file)
@@ -2,9 +2,10 @@
 #include <stdlib.h>
 #include <unistd.h> /* readlink */
 
-#include "ksyms.h"
 #include "nls.h"
 #include "kbd.h"
+#include "contextP.h"
+#include "ksyms.h"
 #include "paths.h"
 
 #include "parser.h"
index cda0f23..0abe214 100644 (file)
@@ -2,9 +2,11 @@
 #include <stdlib.h>
 #include <stdarg.h>
 
+#include "keymap.h"
+
 #include "kbd.h"
 #include "nls.h"
-#include "keymap.h"
+#include "contextP.h"
 
 void __attribute__ ((format (printf, 6, 7)))
 lk_log(struct lk_ctx *ctx, int priority,
@@ -136,11 +138,14 @@ init_array(struct lk_ctx *ctx, struct lk_array **arr, size_t size)
        return 0;
 }
 
-int
-lk_init(struct lk_ctx *ctx)
+struct lk_ctx *
+lk_init(void)
 {
+       struct lk_ctx *ctx;
+
+       ctx = malloc(sizeof(struct lk_ctx));
        if (!ctx)
-               return -1;
+               return NULL;
 
        memset(ctx, 0, sizeof(struct lk_ctx));
 
@@ -151,10 +156,12 @@ lk_init(struct lk_ctx *ctx)
            init_array(ctx, &ctx->func_table,   sizeof(void*)) < 0 ||
            init_array(ctx, &ctx->accent_table, sizeof(void*)) < 0 ||
            init_array(ctx, &ctx->key_constant, sizeof(char))  < 0 ||
-           init_array(ctx, &ctx->key_line,     sizeof(int))   < 0)
-               return -1;
+           init_array(ctx, &ctx->key_line,     sizeof(int))   < 0) {
+               lk_free(ctx);
+               return NULL;
+       }
 
-       return 0;
+       return ctx;
 }
 
 
diff --git a/src/libkeymap/contextP.h b/src/libkeymap/contextP.h
new file mode 100644 (file)
index 0000000..e3798e3
--- /dev/null
@@ -0,0 +1,77 @@
+#ifndef LK_CONTEXTP_H
+#define LK_CONTEXTP_H
+
+#include "keymap.h"
+
+/**
+ * @brief Copy of struct kbdiacruc.
+ */
+struct kb_diacr {
+       unsigned int diacr, base, result;
+};
+
+/**
+ * @brief The maximum number of include levels.
+ */
+#define MAX_INCLUDE_DEPTH 20
+
+/**
+ * @brief Opaque object representing the library context.
+ */
+struct lk_ctx {
+       /**
+        * Parser flags that are set outside the library.
+        */
+       lk_flags flags;
+
+       /**
+        * Keywords used in keymap files.
+        */
+       lk_keywords keywords;
+
+       /**
+        * Key translation table (keycode to action code).
+        */
+       struct lk_array *keymap;
+
+       /**
+        * Function key string entry.
+        */
+       struct lk_array *func_table;
+
+       /**
+        * Accent table.
+        */
+       struct lk_array *accent_table;
+
+       /**
+        * User defined logging function.
+        */
+       void (*log_fn)(void *data, int priority,
+                      const char *file, int line, const char *fn,
+                      const char *format, va_list args);
+
+       /**
+        * The data passed to the @ref log_fn logging function as the first argument.
+        */
+       void *log_data;
+
+       /**
+        * Logging priority used by @ref log_fn logging function.
+        */
+       int log_priority;
+
+       /**
+        * User defined charset.
+        */
+       unsigned int charset;
+
+       /* Fields used by keymap parser */
+
+       struct lk_array *key_constant;
+       struct lk_array *key_line;
+       int mod;
+       lkfile_t *stack[MAX_INCLUDE_DEPTH];
+};
+
+#endif /* LK_CONTEXTP_H */
index 8eceb9a..3cab746 100644 (file)
 #include <ctype.h>
 #include <unistd.h>
 
+#include "keymap.h"
+
+#include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 #include "nls.h"
 
-#include "keymap.h"
-
 #define U(x) ((x) ^ 0xf000)
 
 static void
index 16fd3b8..9d1f55a 100644 (file)
@@ -8,12 +8,13 @@
  */
 #include <string.h>
 #include <errno.h>
-
 #include <sys/ioctl.h>
 
-#include "nls.h"
 #include "keymap.h"
 
+#include "nls.h"
+#include "contextP.h"
+
 int
 lk_kernel_keys(struct lk_ctx *ctx, int fd)
 {
index 5857c0e..55deef5 100644 (file)
@@ -8,11 +8,10 @@
 #include <keymap/context.h>
 
 /** Initializes the structures necessary to read and/or parse keymap.
- * @param ctx is a keymap library context.
  *
- * @return 0 on success, -1 on error.
+ * @return a pointer to keymap library context or NULL.
  */
-int lk_init(struct lk_ctx *ctx);
+struct lk_ctx *lk_init(void);
 
 /** Free keymap resources.
  * @param ctx is a keymap library context.
index ffc8300..0a01745 100644 (file)
@@ -1,5 +1,5 @@
-#ifndef LK_DATA_H
-#define LK_DATA_H
+#ifndef LK_CONTEXT_H
+#define LK_CONTEXT_H
 
 #include <linux/kd.h>
 #include <linux/keyboard.h>
@@ -7,13 +7,6 @@
 #include <keymap/array.h>
 
 /**
- * @brief Copy of struct kbdiacruc.
- */
-struct kb_diacr {
-       unsigned int diacr, base, result;
-};
-
-/**
  * @brief Parser flags that are set outside the library.
  */
 typedef enum {
@@ -34,67 +27,8 @@ typedef enum {
 } lk_keywords;
 
 /**
- * @brief The maximum number of include levels.
- */
-#define MAX_INCLUDE_DEPTH 20
-
-/**
  * @brief Opaque object representing the library context.
  */
-struct lk_ctx {
-       /**
-        * Parser flags that are set outside the library.
-        */
-       lk_flags flags;
-
-       /**
-        * Keywords used in keymap files.
-        */
-       lk_keywords keywords;
-
-       /**
-        * Key translation table (keycode to action code).
-        */
-       struct lk_array *keymap;
-
-       /**
-        * Function key string entry.
-        */
-       struct lk_array *func_table;
-
-       /**
-        * Accent table.
-        */
-       struct lk_array *accent_table; 
-
-       /** @protected
-        * User defined logging function.
-        */
-       void (*log_fn)(void *data, int priority,
-                      const char *file, int line, const char *fn,
-                      const char *format, va_list args);
-
-       /** @protected
-        * The data passed to the @ref log_fn logging function as the first argument.
-        */
-       void *log_data;
-
-       /** @protected
-        * Logging priority used by @ref log_fn logging function.
-        */
-       int log_priority;
-
-       /** @protected
-        * User defined charset.
-        */
-       unsigned int charset;
-
-       /* Fields used by keymap parser */
-
-       struct lk_array *key_constant;      /**< @private */
-       struct lk_array *key_line;          /**< @private */
-       int mod;                            /**< @private */
-       lkfile_t *stack[MAX_INCLUDE_DEPTH]; /**< @private */
-};
+struct lk_ctx;
 
-#endif /* LK_DATA_H */
+#endif /* LK_CONTEXT_H */
index 26d5a45..d3c926f 100644 (file)
@@ -16,6 +16,18 @@ typedef enum {
 } lk_table_shape;
 
 /**
+ * @brief General information about the keymap.
+ */
+struct kmapinfo {
+       lk_flags    flags;           /**< Parser flags that are set outside the library */
+       lk_keywords keywords;        /**< Keywords used in keymap files */
+       size_t      keymaps;         /**< Number of keymaps in actual use */
+       size_t      keymaps_alloced; /**< Number of keymaps dynamically allocated */
+       size_t      functions;       /**< Number of function keys */
+       size_t      composes;        /**< Number of compose definitions in actual use */
+};
+
+/**
  * Outputs a keymap in binary format.
  * @param ctx is a keymap library context.
  * @param fd is a FILE pointer for output.
@@ -73,6 +85,7 @@ void lk_dump_funcs(struct lk_ctx *ctx, FILE *fd);
  */
 void lk_dump_diacs(struct lk_ctx *ctx, FILE *fd);
 
+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);
 
index ba3cd7b..ca97ed5 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "keymap.h"
 
+#include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 
index 05a5de4..0c2d6c1 100644 (file)
@@ -2,11 +2,13 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include "ksyms.h"
-#include "nls.h"
 
 #include "keymap.h"
 
+#include "contextP.h"
+#include "ksyms.h"
+#include "nls.h"
+
 #include "syms.cp1250.h"
 #include "syms.ethiopic.h"
 #include "syms.iso8859_15.h"
index a2a2a7f..8834cc0 100644 (file)
@@ -6,9 +6,11 @@
 #include <linux/keyboard.h>
 #include <unistd.h>
 
+#include "keymap.h"
+
 #include "nls.h"
 #include "kbd.h"
-#include "keymap.h"
+#include "contextP.h"
 #include "ksyms.h"
 
 static int
index 7dcc466..627494a 100644 (file)
@@ -15,6 +15,7 @@
 #include "nls.h"
 #include "kbd.h"
 
+#include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 
index 4d4527e..d85778d 100644 (file)
@@ -8,14 +8,15 @@
  */
 #include <string.h>
 #include <errno.h>
-
 #include <sys/ioctl.h>
 
+#include "keymap.h"
+
+#include "nls.h"
+#include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 
-#include "nls.h"
-#include "keymap.h"
 
 static char
 valid_type(int fd, int t)
@@ -53,27 +54,51 @@ maximum_val(int fd, int t)
        return i - 1;
 }
 
+int
+lk_get_kmapinfo(struct lk_ctx *ctx, struct kmapinfo *res)
+{
+       int i;
+
+       if (!ctx)
+               return -1;
+
+       res->flags     = ctx->flags;
+       res->keywords  = ctx->keywords;
+       res->keymaps   = ctx->keymap->count;
+       res->functions = ctx->func_table->count;
+       res->composes  = ctx->accent_table->count;
+
+       res->keymaps_alloced = 0;
+
+       for (i = 0; i < MAX_NR_KEYMAPS; i++) {
+               if (lk_get_key(ctx, i, 0) == K_ALLOCATED) {
+                       res->keymaps_alloced++;
+               }
+       }
+
+       return 0;
+}
+
 #define NR_TYPES 15
 
 void
 lk_dump_summary(struct lk_ctx *ctx, FILE *fd, int console)
 {
-       int i, allocct = 0;
+       int i;
+       struct kmapinfo info;
 
-       for (i = 0; i < MAX_NR_KEYMAPS; i++) {
-               if (lk_get_key(ctx, i, 0) == K_ALLOCATED)
-                       allocct++;
-       }
+       if (lk_get_kmapinfo(ctx, &info) < 0)
+               return;
 
        fprintf(fd, _("keycode range supported by kernel:           1 - %d\n"),
                NR_KEYS - 1);
        fprintf(fd, _("max number of actions bindable to a key:         %d\n"),
                MAX_NR_KEYMAPS);
        fprintf(fd, _("number of keymaps in actual use:                 %u\n"),
-               (unsigned int) ctx->keymap->count);
+               (unsigned int) info.keymaps);
 
-       if (allocct)
-               fprintf(fd, _("of which %d dynamically allocated\n"), allocct);
+       fprintf(fd, _("of which %u dynamically allocated\n"),
+               (unsigned int) info.keymaps_alloced);
 
        fprintf(fd, _("ranges of action codes supported by kernel:\n"));
 
@@ -86,7 +111,7 @@ lk_dump_summary(struct lk_ctx *ctx, FILE *fd, int console)
        fprintf(fd, _("max nr of compose definitions: %d\n"),
                MAX_DIACR);
        fprintf(fd, _("nr of compose definitions in actual use: %u\n"),
-               (unsigned int) ctx->accent_table->count);
+               (unsigned int) info.composes);
 }
 
 void
index 8a247ba..6b23f68 100644 (file)
@@ -94,7 +94,7 @@ main(int argc, char *argv[])
        const char *const *dirpath;
        const char *dirpath2[] = { 0, 0 };
 
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
        lk_flags flags = 0;
 
        int c, i, rc = -1;
@@ -111,7 +111,10 @@ main(int argc, char *argv[])
 
        progname = set_progname(argv[0]);
 
-       lk_init(&ctx);
+       ctx = lk_init();
+       if (!ctx) {
+               exit(EXIT_FAILURE);
+       }
 
        while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
                switch (c) {
@@ -122,7 +125,7 @@ main(int argc, char *argv[])
                        options |= OPT_B;
                        break;
                case 'c':
-                       ctx.flags |= LK_FLAG_CLEAR_COMPOSE;
+                       flags |= LK_FLAG_CLEAR_COMPOSE;
                        break;
                case 'C':
                        console = optarg;
@@ -142,10 +145,10 @@ main(int argc, char *argv[])
                        flags |= LK_FLAG_PREFER_UNICODE;
                        break;
                case 'q':
-                       lk_set_log_priority(&ctx, LOG_ERR);
+                       lk_set_log_priority(ctx, LOG_ERR);
                        break;
                case 'v':
-                       lk_set_log_priority(&ctx, LOG_INFO);
+                       lk_set_log_priority(ctx, LOG_INFO);
                        break;
                case 'V':
                        fprintf(stdout, _("%s from %s\n"), progname, PACKAGE_STRING);
@@ -196,7 +199,7 @@ main(int argc, char *argv[])
                }
        }
 
-       lk_set_parser_flags(&ctx, flags);
+       lk_set_parser_flags(ctx, flags);
 
        dirpath = dirpath1;
        if ((ev = getenv("LOADKEYS_KEYMAP_PATH")) != NULL) {
@@ -212,7 +215,7 @@ main(int argc, char *argv[])
                        exit(EXIT_FAILURE);
                }
 
-               if ((rc = lk_parse_keymap(&ctx, &f)) == -1)
+               if ((rc = lk_parse_keymap(ctx, &f)) == -1)
                        goto fail;
 
 
@@ -220,7 +223,7 @@ main(int argc, char *argv[])
                f.fd = stdin;
                strcpy(f.pathname, "<stdin>");
 
-               if ((rc = lk_parse_keymap(&ctx, &f)) == -1)
+               if ((rc = lk_parse_keymap(ctx, &f)) == -1)
                        goto fail;
        }
 
@@ -234,19 +237,19 @@ main(int argc, char *argv[])
                        goto fail;
                }
 
-               if ((rc = lk_parse_keymap(&ctx, &f)) == -1)
+               if ((rc = lk_parse_keymap(ctx, &f)) == -1)
                        goto fail;
        }
 
        if (options & OPT_B) {
-               rc = lk_dump_bkeymap(&ctx, stdout);
+               rc = lk_dump_bkeymap(ctx, stdout);
        } else if (options & OPT_M) {
-               rc = lk_dump_ctable(&ctx, stdout);
+               rc = lk_dump_ctable(ctx, stdout);
        } else {
-               rc = lk_load_keymap(&ctx, fd, kbd_mode);
+               rc = lk_load_keymap(ctx, fd, kbd_mode);
        }
 
- fail: lk_free(&ctx);
+ fail: lk_free(ctx);
        lk_fpclose(&f);
        close(fd);
 
index 1e10291..84aa4b6 100644 (file)
@@ -5,18 +5,18 @@
 
 int main(int argc, char **argv)
 {
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
        lkfile_t f;
 
-       lk_init(&ctx);
+       ctx = lk_init();
 
        f.pipe = 0;
        strcpy(f.pathname, argv[1]);
        f.fd = fopen( argv[1], "r");
 
-       lk_parse_keymap(&ctx, &f);
-       lk_dump_bkeymap(&ctx, stdout);
+       lk_parse_keymap(ctx, &f);
+       lk_dump_bkeymap(ctx, stdout);
 
-       lk_free(&ctx);
+       lk_free(ctx);
        return 0;
 }
index 4cc6fb1..4804afb 100644 (file)
@@ -8,21 +8,22 @@ START_TEST(test0)
 {
        char *s;
        lkfile_t f;
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       struct lk_ctx *ctx;
+
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "charset-keymap0.map");
        f.fd = fopen(DATADIR "/charset-keymap0.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
-       s = lk_get_charset(&ctx);
+       s = lk_get_charset(ctx);
 
        fail_if(strcmp(s, "iso-8859-2"), "Unable to parse charset");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -30,47 +31,22 @@ START_TEST(test1)
 {
        char *s;
        lkfile_t f;
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
-
-       f.pipe = 0;
-       strcpy(f.pathname, "null");
-       f.fd = fopen("/dev/null", "r");
+       struct lk_ctx *ctx;
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
-
-       s = lk_get_charset(&ctx);
-
-       fail_if(s != NULL, "Wrong charset");
-
-       lk_free(&ctx);
-}
-END_TEST
-
-START_TEST(test2)
-{
-       char *s;
-       lkfile_t f;
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "null");
        f.fd = fopen("/dev/null", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
-       fail_if(lk_get_charset(NULL), "Wrong charset index");
+       s = lk_get_charset(ctx);
 
-       ctx.charset = -1;
-       fail_if(lk_get_charset(&ctx), "Wrong charset index");
-
-       ctx.charset = 255;
-       fail_if(lk_get_charset(&ctx), "Wrong charset index");
+       fail_if(s != NULL, "Wrong charset");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -82,7 +58,6 @@ libkeymap_suite(void)
 
        tcase_add_test(tc_core, test0);
        tcase_add_test(tc_core, test1);
-       tcase_add_test(tc_core, test2);
 
        suite_add_tcase(s, tc_core);
        return s;
index 330c1f6..6d7ec2c 100644 (file)
@@ -7,7 +7,7 @@ int main(int argc, char **argv)
 {
        lk_table_shape table;
        char numeric;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
        lkfile_t f;
 
        if (argc == 1) {
@@ -22,17 +22,17 @@ int main(int argc, char **argv)
 
        numeric = (!strcasecmp(argv[3], "TRUE")) ? 1 : 0;
 
-       lk_init(&ctx);
-       ctx.flags |= LK_FLAG_PREFER_UNICODE;
+       ctx = lk_init();
+       lk_set_parser_flags(ctx, LK_FLAG_PREFER_UNICODE);
 
        f.pipe = 0;
        strcpy(f.pathname, argv[1]);
        f.fd = fopen( argv[1], "r");
 
-       lk_parse_keymap(&ctx, &f);
-       lk_dump_keymap(&ctx, stdout, table, numeric);
-       lk_dump_diacs(&ctx, stdout);
+       lk_parse_keymap(ctx, &f);
+       lk_dump_keymap(ctx, stdout, table, numeric);
+       lk_dump_diacs(ctx, stdout);
 
-       lk_free(&ctx);
+       lk_free(ctx);
        return 0;
 }
index e46d205..a384c53 100644 (file)
@@ -6,36 +6,33 @@
 
 START_TEST(test_create_0)
 {
-       struct lk_ctx ctx;
-       fail_unless(lk_init(&ctx) == 0, 
-               "Unable to initialize structure by valid pointer");
-}
-END_TEST
+       struct lk_ctx *ctx;
 
-START_TEST(test_create_1)
-{
-       fail_if(lk_init(NULL) == 0, 
-               "Possible to initialize structure by NULL");
+       ctx = lk_init();
+
+       fail_if(ctx == NULL,
+               "Unable to initialize structure by valid pointer");
+       lk_free(ctx);
 }
 END_TEST
 
 START_TEST(test_free_0)
 {
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       fail_unless(lk_init(&ctx) == 0,
+       ctx = lk_init();
+
+       fail_if(ctx == NULL,
                "Unable to initialize structure by valid pointer");
 
-       fail_unless(lk_free(&ctx) == 0, 
+       fail_unless(lk_free(ctx) == 0,
                "Unable to free by valid pointer");
-
-       lk_free(&ctx);
 }
 END_TEST
 
 START_TEST(test_free_1)
 {
-       fail_if(lk_free(NULL) == 0, 
+       fail_if(lk_free(NULL) == 0,
                "Possible to free NULL pointer");
 }
 END_TEST
@@ -48,7 +45,6 @@ libkeymap_suite(void)
        TCase *tc_core = tcase_create(NULL);
 
        tcase_add_test(tc_core, test_create_0);
-       tcase_add_test(tc_core, test_create_1);
        tcase_add_test(tc_core, test_free_0);
        tcase_add_test(tc_core, test_free_1);
 
index 0d92708..cbd9477 100644 (file)
@@ -6,54 +6,56 @@
 
 START_TEST(test_add_key_0)
 {
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       struct lk_ctx *ctx;
 
-       fail_if(lk_add_key(&ctx, 0, NR_KEYS + 1, 0) != 0,
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
+
+       fail_if(lk_add_key(ctx, 0, NR_KEYS + 1, 0) != 0,
                "Unable to use index > NR_KEYS");
 
-       fail_if(lk_add_key(&ctx, MAX_NR_KEYMAPS + 1, 0, 0) != 0,
+       fail_if(lk_add_key(ctx, MAX_NR_KEYMAPS + 1, 0, 0) != 0,
                "Unable to use table > MAX_NR_KEYMAPS");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
 START_TEST(test_add_key_1)
 {
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       struct lk_ctx *ctx;
+
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
-       fail_unless(lk_add_key(&ctx, 0, 0, 0) == 0,
+       fail_unless(lk_add_key(ctx, 0, 0, 0) == 0,
                "Unable to add keycode = 0");
 
-       fail_unless(lk_add_key(&ctx, 0, 0, 16) == 0,
+       fail_unless(lk_add_key(ctx, 0, 0, 16) == 0,
                "Unable to add keycode = 16");
 
-       fail_unless(lk_add_key(&ctx, 1, 1, K_HOLE) == 0,
+       fail_unless(lk_add_key(ctx, 1, 1, K_HOLE) == 0,
                "Unable to add keycode = K_HOLE");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
 START_TEST(test_add_key_2)
 {
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       struct lk_ctx *ctx;
 
-       ctx.flags |= LK_KEYWORD_ALTISMETA;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
+       lk_set_parser_flags(ctx, LK_KEYWORD_ALTISMETA);
 
-       fail_unless(lk_add_key(&ctx, 0, 0, 16) == 0,
+       fail_unless(lk_add_key(ctx, 0, 0, 16) == 0,
                "Unable to add keycode");
 
-       fail_unless(lk_get_key(&ctx, 0, 0) == 16,
+       fail_unless(lk_get_key(ctx, 0, 0) == 16,
                "Unable to get keycode");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -72,10 +74,10 @@ START_TEST(test_add_func_0)
                "\033[6~",  0,          0,          0,          0
        };
        int i;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        for (i = 0; i < 30; i++) {
                struct kbsentry ke;
@@ -88,28 +90,29 @@ START_TEST(test_add_func_0)
                ke.kb_string[sizeof(ke.kb_string) - 1] = 0;
                ke.kb_func = i;
 
-               fail_if(lk_add_func(&ctx, ke) == -1,
+               fail_if(lk_add_func(ctx, ke) == -1,
                        "Unable to add function");
        }
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
 START_TEST(test_add_diacr_0)
 {
        int i = MAX_DIACR + 10;
-       struct lk_ctx ctx;
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       struct lk_ctx *ctx;
+
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        while (i > 0) {
-               fail_if(lk_add_diacr(&ctx, 0, 0, 0) != 0,
+               fail_if(lk_add_diacr(ctx, 0, 0, 0) != 0,
                        "Unable to add diacr");
                i--;
        }
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
index 89bccae..a5791f5 100644 (file)
@@ -6,47 +6,52 @@
 
 START_TEST(test_add_map_border)
 {
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
-       fail_unless(lk_add_map(&ctx, MAX_NR_KEYMAPS) == 0,
+       fail_unless(lk_add_map(ctx, MAX_NR_KEYMAPS) == 0,
                "Unable to define map == MAX_NR_KEYMAPS");
 
-       fail_unless(lk_add_map(&ctx, MAX_NR_KEYMAPS*2) == 0,
+       fail_unless(lk_add_map(ctx, MAX_NR_KEYMAPS*2) == 0,
                "Unable to define map == MAX_NR_KEYMAPS*2");
 
-       fail_unless(lk_add_map(&ctx, 0) == 0,
+       fail_unless(lk_add_map(ctx, 0) == 0,
                "Unable to define map");
 
-       fail_unless(lk_add_map(&ctx, 0) == 0,
+       fail_unless(lk_add_map(ctx, 0) == 0,
                "Unable to define map");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
 START_TEST(test_add_map_0)
 {
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
+       struct kmapinfo info;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
-       fail_if(lk_add_map(&ctx, 0) != 0, "Unable to define map");
-       fail_if(ctx.keymap->count != 1, "Wrong keymap number");
+       fail_if(lk_add_map(ctx, 0) != 0, "Unable to define map");
+       lk_get_kmapinfo(ctx, &info);
+       fail_if(info.keymaps != 1, "Wrong keymap number");
 
-       fail_if(lk_add_map(&ctx, 0) != 0, "Unable to define map");
-       fail_if(ctx.keymap->count != 1, "Wrong keymap number");
+       fail_if(lk_add_map(ctx, 0) != 0, "Unable to define map");
+       lk_get_kmapinfo(ctx, &info);
+       fail_if(info.keymaps != 1, "Wrong keymap number");
                
-       fail_if(lk_add_map(&ctx, 1) != 0, "Unable to define map");
-       fail_if(ctx.keymap->count != 2, "Wrong keymap number");
+       fail_if(lk_add_map(ctx, 1) != 0, "Unable to define map");
+       lk_get_kmapinfo(ctx, &info);
+       fail_if(info.keymaps != 2, "Wrong keymap number");
 
-       fail_if(lk_add_map(&ctx, 2) != 0, "Unable to define map");
-       fail_if(ctx.keymap->count != 3, "Wrong keymap number");
+       fail_if(lk_add_map(ctx, 2) != 0, "Unable to define map");
+       lk_get_kmapinfo(ctx, &info);
+       fail_if(info.keymaps != 3, "Wrong keymap number");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
index 5c831f9..2fb3691 100644 (file)
@@ -5,18 +5,18 @@
 
 int main(int argc, char **argv)
 {
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
        lkfile_t f;
 
-       lk_init(&ctx);
+       ctx = lk_init();
 
        f.pipe = 0;
        strcpy(f.pathname, argv[1]);
        f.fd = fopen( argv[1], "r");
 
-       lk_parse_keymap(&ctx, &f);
-       lk_dump_ctable(&ctx, stdout);
+       lk_parse_keymap(ctx, &f);
+       lk_dump_ctable(ctx, stdout);
 
-       lk_free(&ctx);
+       lk_free(ctx);
        return 0;
 }
index b04655a..e3aa910 100644 (file)
@@ -16,36 +16,36 @@ START_TEST(test_parse_0)
 {
        int c;
        lkfile_t f;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap0.map");
        f.fd = fopen(DATADIR "/keymap0.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
-       c = lk_get_key(&ctx, 0, 16);
+       c = lk_get_key(ctx, 0, 16);
        fail_if(KVAL(c) != 'q', "Unable to get keycode 16");
 
-       c = lk_get_key(&ctx, 0, 17);
+       c = lk_get_key(ctx, 0, 17);
        fail_if(KVAL(c) != 'w', "Unable to get keycode 17");
 
-       c = lk_get_key(&ctx, 0, 18);
+       c = lk_get_key(ctx, 0, 18);
        fail_if(KVAL(c) != 'e', "Unable to get keycode 18");
 
-       c = lk_get_key(&ctx, 0, 19);
+       c = lk_get_key(ctx, 0, 19);
        fail_if(KVAL(c) != 'r', "Unable to get keycode 19");
 
-       c = lk_get_key(&ctx, 0, 20);
+       c = lk_get_key(ctx, 0, 20);
        fail_if(KVAL(c) != 't', "Unable to get keycode 20");
 
-       c = lk_get_key(&ctx, 0, 21);
+       c = lk_get_key(ctx, 0, 21);
        fail_if(KVAL(c) != 'y', "Unable to get keycode 21");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -53,24 +53,24 @@ START_TEST(test_parse_1)
 {
        int c;
        lkfile_t f;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap1.map");
        f.fd = fopen(DATADIR "/keymap1.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
-       c = lk_get_key(&ctx, 0, 16);
+       c = lk_get_key(ctx, 0, 16);
        fail_if(KVAL(c) != 'q', "Unable to get keycode");
 
-       c = lk_get_key(&ctx, 1, 16);
+       c = lk_get_key(ctx, 1, 16);
        fail_if(KVAL(c) != 'Q', "Unable to get keycode");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -78,24 +78,24 @@ START_TEST(test_parse_2)
 {
        int i = 0;
        lkfile_t f;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap2.map");
        f.fd = fopen(DATADIR "/keymap2.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
        while (i < MAX_NR_KEYMAPS) {
-               int c = lk_get_key(&ctx, i, 17);
+               int c = lk_get_key(ctx, i, 17);
                fail_if(KVAL(c) != 'x', "Unable to get keycode");
                i++;
        }
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -104,23 +104,23 @@ START_TEST(test_parse_3)
        int i;
        char str[] = "qwertyuiopasdfghjklzxcvbnm";
        lkfile_t f;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap3.map");
        f.fd = fopen(DATADIR "/keymap3.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
        for (i = 0; i < 26; i++) {
-               int c = lk_get_key(&ctx, i, 17);
+               int c = lk_get_key(ctx, i, 17);
                fail_if(KVAL(c) != str[i], "Unable to get keycode");
        }
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -128,27 +128,27 @@ START_TEST(test_parse_4)
 {
        int c;
        lkfile_t f;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap4.map");
        f.fd = fopen(DATADIR "/keymap4.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
-       c = lk_get_key(&ctx, 0, 16);
+       c = lk_get_key(ctx, 0, 16);
        fail_if(KVAL(c) != 'q', "Unable to get keycode");
 
-       c = lk_get_key(&ctx, 0, 17);
+       c = lk_get_key(ctx, 0, 17);
        fail_if(KVAL(c) != 'w', "Include40.map failed");
 
-       c = lk_get_key(&ctx, 0, 18);
+       c = lk_get_key(ctx, 0, 18);
        fail_if(KVAL(c) != 'e', "Include41.map failed");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -157,25 +157,25 @@ START_TEST(test_parse_5)
        int i;
        lkfile_t f;
        struct kbsentry kbs;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap5.map");
        f.fd = fopen(DATADIR "/keymap5.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
        for(i = 0; i < MAX_NR_FUNC; i++) {
                kbs.kb_func = i;
                kbs.kb_string[0] = 0;
-               fail_if(lk_get_func(&ctx, &kbs) != 0,
+               fail_if(lk_get_func(ctx, &kbs) != 0,
                        "Unable to get func %d", i);
        }
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST
 
@@ -183,30 +183,30 @@ START_TEST(test_parse_6)
 {
        lkfile_t f;
        struct kbsentry kbs;
-       struct lk_ctx ctx;
+       struct lk_ctx *ctx;
 
-       lk_init(&ctx);
-       ctx.log_fn = NULL;
+       ctx = lk_init();
+       lk_set_log_fn(ctx, NULL, NULL);
 
        f.pipe = 0;
        strcpy(f.pathname, "keymap6.map");
        f.fd = fopen(DATADIR "/keymap6.map", "r");
 
-       fail_if(lk_parse_keymap(&ctx, &f) != 0, "Unable to parse keymap");
+       fail_if(lk_parse_keymap(ctx, &f) != 0, "Unable to parse keymap");
 
        kbs.kb_func = 0;
        kbs.kb_string[0] = 0;
-       fail_if(lk_get_func(&ctx, &kbs) != 0, "Unable to get func 0");
+       fail_if(lk_get_func(ctx, &kbs) != 0, "Unable to get func 0");
 
        kbs.kb_func = 1;
        kbs.kb_string[0] = 0;
-       fail_if(lk_get_func(&ctx, &kbs) != 0, "Unable to get func 1");
+       fail_if(lk_get_func(ctx, &kbs) != 0, "Unable to get func 1");
 
        kbs.kb_func = 2;
        kbs.kb_string[0] = 0;
-       fail_if(lk_get_func(&ctx, &kbs) != -1, "Possible to get not alloced func");
+       fail_if(lk_get_func(ctx, &kbs) != -1, "Possible to get not alloced func");
 
-       lk_free(&ctx);
+       lk_free(ctx);
 }
 END_TEST