summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChoe Hwanjin <choe.hwanjin@gmail.com>2021-09-19 10:48:44 +0900
committerChoe Hwanjin <choe.hwanjin@gmail.com>2021-09-19 12:12:50 +0900
commit4c1efe85ab9ddb5f6ea3e69bdf24e9373d5c402a (patch)
tree6b63d7a38b59f671a2f725f96bd64e16694034b8
parentcf218777a2019753462084a5ceec7ca0457b9f65 (diff)
downloadlibhangul-keyboard-path.tar.gz
Add LIBHANGUL_KEYBOARD_PATH environment variable featuremasterkeyboard-path
LIBHANGUL_KEYBOARD_PATH 환경 변수를 설정하면 그 위치의 키보드 파일을 로딩하게 한다. 테스트할 때에는 시스템에 설치된 키보드 파일이 아니라 빌드 디렉토리의 키보드 파일을 사용해야 의미가 있다. 빌드 디렉토리의 키보드을 로딩할 수 있는 기능을 제공하기위해서 LIBHANGUL_KEYBOARD_PATH 환경 변수를 도입한다. 소스 디렉토리에는 키보드 파일 템플릿만 있다. 완성된 키보드 파일은 빌드 디렉토리에 생성되므로 빌드 디렉토리에서 키보드 파일을 로딩해야 한다. 그러나 거기에는 combination 파일이 없으므로 제대로된 테스트를 위해서는 combination 파일도 복사해주는 룰이 필요하다. AC_CONFIG_LINKS()를 사용하여 소스만 고치고도 테스트 가능하게 한다.
-rw-r--r--configure.ac5
-rw-r--r--hangul/hangulkeyboard.c96
-rw-r--r--test/Makefile.am8
-rw-r--r--test/hangul.c4
-rw-r--r--test/test.c4
5 files changed, 89 insertions, 28 deletions
diff --git a/configure.ac b/configure.ac
index 369f904..d382bb0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -92,6 +92,11 @@ test/Makefile
tools/Makefile
])
+AC_CONFIG_LINKS([
+data/keyboards/hangul-combination-default.xml:data/keyboards/hangul-combination-default.xml
+data/keyboards/hangul-combination-full.xml:data/keyboards/hangul-combination-full.xml
+])
+
AC_OUTPUT
# vim: et
diff --git a/hangul/hangulkeyboard.c b/hangul/hangulkeyboard.c
index ac94cb8..11354d8 100644
--- a/hangul/hangulkeyboard.c
+++ b/hangul/hangulkeyboard.c
@@ -58,7 +58,6 @@
*/
#define LIBHANGUL_KEYBOARD_DIR LIBHANGUL_DATA_DIR "/keyboards"
-//#define LIBHANGUL_KEYBOARD_DIR TOP_SRCDIR "/data/keyboards"
#define HANGUL_KEYBOARD_TABLE_SIZE 0x80
@@ -832,6 +831,62 @@ hangul_keyboard_list_clear()
hangul_keyboards.keyboards = NULL;
}
+static char*
+hangul_keyboard_get_default_keyboard_path()
+{
+ char* keyboard_path = NULL;
+ size_t keyboard_path_len = 1;
+
+ /* default LIBHANGUL_KEYBOARD_PATH is
+ * SYSTEM_KEYBOARD_DIR:USER_KEYBOARD_DIR */
+
+ /* system default dir */
+ const char* system_dir = LIBHANGUL_KEYBOARD_DIR;
+ keyboard_path_len += strlen(system_dir);
+
+ /* user default dir */
+ char* xdg_data_home = getenv("XDG_DATA_HOME");
+ if (xdg_data_home == NULL) {
+ char* home_dir = getenv("HOME");
+ if (home_dir == NULL) {
+ /* no user data dir */
+ keyboard_path = (char*)malloc(keyboard_path_len);
+ if (keyboard_path != NULL) {
+ snprintf(keyboard_path, keyboard_path_len, "%s", system_dir);
+ }
+ } else {
+ const char* subdir = "/.local/share/libhangul/keyboards";
+ keyboard_path_len += 1 + strlen(home_dir) + strlen(subdir);
+ keyboard_path = (char*)malloc(keyboard_path_len);
+ if (keyboard_path != NULL) {
+ snprintf(keyboard_path, keyboard_path_len, "%s:%s%s", system_dir, home_dir, subdir);
+ }
+ }
+ } else {
+ const char* subdir = "/libhangul/keyboards";
+ keyboard_path_len += 1 + strlen(xdg_data_home) + strlen(subdir);
+ keyboard_path = (char*)malloc(keyboard_path_len);
+ if (keyboard_path != NULL) {
+ snprintf(keyboard_path, keyboard_path_len, "%s:%s%s", system_dir, xdg_data_home, subdir);
+ }
+ }
+
+ return keyboard_path;
+}
+
+static char*
+hangul_keyboard_get_keyboard_path()
+{
+ char* keyboard_path = getenv("LIBHANGUL_KEYBOARD_PATH");
+ if (keyboard_path == NULL) {
+ keyboard_path = hangul_keyboard_get_default_keyboard_path();
+ } else {
+ keyboard_path = strdup(keyboard_path);
+ }
+
+ return keyboard_path;
+}
+
int
hangul_keyboard_list_init()
{
@@ -847,35 +902,24 @@ hangul_keyboard_list_init()
* builtin 키보드는 하위 호환을 위해 남겨둔다. */
hangul_builtin_keyboard_count = 0;
- unsigned n = 0;
/* libhangul data dir에서 keyboard 로딩 */
- n += hangul_keyboard_list_load_dir(LIBHANGUL_KEYBOARD_DIR);
+ char* libhangul_keyboard_path = hangul_keyboard_get_keyboard_path();
- /* 유저의 개별 키보드 파일 로딩 */
- char* user_data_dir = NULL;
- char* xdg_data_home = getenv("XDG_DATA_HOME");
- if (xdg_data_home == NULL) {
- char* home_dir = getenv("HOME");
- if (home_dir != NULL) {
- const char* subdir = "/.local/share/libhangul/keyboards";
- size_t len = strlen(home_dir) + strlen(subdir) + 1;
- user_data_dir = (char*)malloc(len);
- if (user_data_dir != NULL) {
- snprintf(user_data_dir, len, "%s%s", home_dir, subdir);
- }
- }
- } else {
- const char* subdir = "/libhangul/keyboards";
- size_t len = strlen(xdg_data_home) + strlen(subdir) + 1;
- user_data_dir = (char*)malloc(len);
- if (user_data_dir != NULL) {
- snprintf(user_data_dir, len, "%s%s", xdg_data_home, subdir);
+ unsigned n = 0;
+
+ char* dir = libhangul_keyboard_path;
+ while (dir != NULL && dir[0] != '\0') {
+ char* next = strchr(dir, ':');
+ if (next != NULL) {
+ next[0] = '\0';
+ ++next;
}
+
+ n += hangul_keyboard_list_load_dir(dir);
+ dir = next;
}
- if (user_data_dir != NULL) {
- n += hangul_keyboard_list_load_dir(user_data_dir);
- free(user_data_dir);
- }
+
+ free(libhangul_keyboard_path);
if (n == 0)
return 1;
diff --git a/test/Makefile.am b/test/Makefile.am
index 6364f50..071d0a2 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -1,7 +1,7 @@
noinst_PROGRAMS = hangul hanja
-hangul_CFLAGS =
+hangul_CFLAGS = -DTEST_LIBHANGUL_KEYBOARD_PATH=\"${abs_top_builddir}/data/keyboards\"
hangul_SOURCES = hangul.c
hangul_LDADD = ../hangul/libhangul.la $(LTLIBINTL) $(LTLIBICONV)
@@ -12,5 +12,9 @@ hanja_LDADD = ../hangul/libhangul.la $(LTLIBINTL)
TESTS = test
check_PROGRAMS = test
test_SOURCES = test.c ../hangul/hangul.h
-test_CFLAGS = $(CHECK_CFLAGS) -DTEST_SOURCE_DIR=\"$(abs_srcdir)\"
+test_CFLAGS = \
+ $(CHECK_CFLAGS) \
+ -DTEST_SOURCE_DIR=\"$(abs_srcdir)\" \
+ -DTEST_LIBHANGUL_KEYBOARD_PATH=\"${abs_top_builddir}/data/keyboards\" \
+ $(NULL)
test_LDADD = $(CHECK_LIBS) ../hangul/libhangul.la $(LTLIBINTL)
diff --git a/test/hangul.c b/test/hangul.c
index 797c14d..a019f06 100644
--- a/test/hangul.c
+++ b/test/hangul.c
@@ -68,6 +68,10 @@ main(int argc, char *argv[])
keyboard = argv[1];
}
+ char* keyboard_path = getenv("LIBHANGUL_KEYBOARD_PATH");
+ if (keyboard_path == NULL)
+ setenv("LIBHANGUL_KEYBOARD_PATH", TEST_LIBHANGUL_KEYBOARD_PATH, 1);
+
hangul_init();
hic = hangul_ic_new(keyboard);
diff --git a/test/test.c b/test/test.c
index c64fd6e..2a0b383 100644
--- a/test/test.c
+++ b/test/test.c
@@ -613,6 +613,10 @@ Suite* libhangul_suite()
int main()
{
+ char* keyboard_path = getenv("LIBHANGUL_KEYBOARD_PATH");
+ if (keyboard_path == NULL)
+ setenv("LIBHANGUL_KEYBOARD_PATH", TEST_LIBHANGUL_KEYBOARD_PATH, 1);
+
hangul_init();
int number_failed;