Move lk_ctx content to private part of library

Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
diff --git a/src/dumpkeys.c b/src/dumpkeys.c
index aece68f..cc7ae36 100644
--- a/src/dumpkeys.c
+++ b/src/dumpkeys.c
@@ -80,7 +80,7 @@
 	char keys_only = 0;
 	char diac_only = 0;
 
-	struct lk_ctx ctx;
+	struct lk_ctx *ctx;
 
 	set_progname(argv[0]);
 
@@ -88,7 +88,10 @@
 	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 @@
 				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 @@
 	}
 
 	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 @@
 	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)
diff --git a/src/libkeymap/Makefile.am b/src/libkeymap/Makefile.am
index 07d14ad..037dade 100644
--- a/src/libkeymap/Makefile.am
+++ b/src/libkeymap/Makefile.am
@@ -41,6 +41,7 @@
 	$(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) \
diff --git a/src/libkeymap/analyze.l b/src/libkeymap/analyze.l
index 887dedd..b6f96fd 100644
--- a/src/libkeymap/analyze.l
+++ b/src/libkeymap/analyze.l
@@ -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"
diff --git a/src/libkeymap/common.c b/src/libkeymap/common.c
index cda0f23..0abe214 100644
--- a/src/libkeymap/common.c
+++ b/src/libkeymap/common.c
@@ -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 @@
 	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 @@
 	    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
index 0000000..d327a1d
--- /dev/null
+++ b/src/libkeymap/contextP.h
@@ -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; 
+
+	/** @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 */
+};
+
+#endif /* LK_DATA_H */
diff --git a/src/libkeymap/dump.c b/src/libkeymap/dump.c
index 8eceb9a..3cab746 100644
--- a/src/libkeymap/dump.c
+++ b/src/libkeymap/dump.c
@@ -14,12 +14,13 @@
 #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
diff --git a/src/libkeymap/kernel.c b/src/libkeymap/kernel.c
index 16fd3b8..9d1f55a 100644
--- a/src/libkeymap/kernel.c
+++ b/src/libkeymap/kernel.c
@@ -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)
 {
diff --git a/src/libkeymap/keymap/common.h b/src/libkeymap/keymap/common.h
index 5857c0e..55deef5 100644
--- a/src/libkeymap/keymap/common.h
+++ b/src/libkeymap/keymap/common.h
@@ -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.
diff --git a/src/libkeymap/keymap/context.h b/src/libkeymap/keymap/context.h
index ffc8300..0a01745 100644
--- a/src/libkeymap/keymap/context.h
+++ b/src/libkeymap/keymap/context.h
@@ -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 @@
 } 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;
+struct lk_ctx;
 
-	/**
-	 * 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 */
-};
-
-#endif /* LK_DATA_H */
+#endif /* LK_CONTEXT_H */
diff --git a/src/libkeymap/kmap.c b/src/libkeymap/kmap.c
index ba3cd7b..ca97ed5 100644
--- a/src/libkeymap/kmap.c
+++ b/src/libkeymap/kmap.c
@@ -6,6 +6,7 @@
 
 #include "keymap.h"
 
+#include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 
diff --git a/src/libkeymap/ksyms.c b/src/libkeymap/ksyms.c
index 05a5de4..0c2d6c1 100644
--- a/src/libkeymap/ksyms.c
+++ b/src/libkeymap/ksyms.c
@@ -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"
diff --git a/src/libkeymap/loadkeys.c b/src/libkeymap/loadkeys.c
index a2a2a7f..8834cc0 100644
--- a/src/libkeymap/loadkeys.c
+++ b/src/libkeymap/loadkeys.c
@@ -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
diff --git a/src/libkeymap/parser.y b/src/libkeymap/parser.y
index 7dcc466..627494a 100644
--- a/src/libkeymap/parser.y
+++ b/src/libkeymap/parser.y
@@ -15,6 +15,7 @@
 #include "nls.h"
 #include "kbd.h"
 
+#include "contextP.h"
 #include "ksyms.h"
 #include "modifiers.h"
 
diff --git a/src/libkeymap/summary.c b/src/libkeymap/summary.c
index 4d4527e..2643934 100644
--- a/src/libkeymap/summary.c
+++ b/src/libkeymap/summary.c
@@ -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)
diff --git a/src/loadkeys.c b/src/loadkeys.c
index 8a247ba..6b23f68 100644
--- a/src/loadkeys.c
+++ b/src/loadkeys.c
@@ -94,7 +94,7 @@
 	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 @@
 
 	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 @@
 			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 @@
 			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 @@
 		}
 	}
 
-	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 @@
 			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 @@
 		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 @@
 			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);