From 0b03ddb21bc968f869896e92762d45b4f1461337 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 18 May 2024 15:20:22 -0500 Subject: [PATCH] More work on setting locale for extended locale support. --- src/core/os.c | 56 +++++++++++++++++++++++++++++---------------------- 1 file changed, 32 insertions(+), 24 deletions(-) diff --git a/src/core/os.c b/src/core/os.c index e9dab3e1..b642d735 100644 --- a/src/core/os.c +++ b/src/core/os.c @@ -43,14 +43,22 @@ #include #endif -#if defined(JANET_BSD) || defined(JANET_APPLE) +#if defined(__FreeBSD__) || defined(__DragonFly__) || defined(JANET_APPLE) +/* It seems only some bsds use this header for xlocale */ #include +#define JANET_EXTENDED_LOCALE #else #include #endif #ifdef JANET_LINUX #include +#define JANET_EXTENDED_LOCALE +#endif + +/* OpenBSD works here with extended locale support, just in the usual headers */ +#if defined(__OpenBSD__) +#define JANET_EXTENDED_LOCALE #endif #ifdef JANET_WINDOWS @@ -1904,28 +1912,7 @@ JANET_CORE_FN(os_setlocale, janet_arity(argc, 1, 2); const char *locale_name = janet_optcstring(argv, argc, 1, NULL); int category_int = 0; -#ifdef JANET_WINDOWS - if (janet_keyeq(argv[0], "all")) { - category_int = LC_ALL; - } else if (janet_keyeq(argv[0], "collate")) { - category_int = LC_COLLATE; - } else if (janet_keyeq(argv[0], "ctype")) { - category_int = LC_CTYPE; - } else if (janet_keyeq(argv[0], "monetary")) { - category_int = LC_MONETARY; - } else if (janet_keyeq(argv[0], "numeric")) { - category_int = LC_NUMERIC; - } else if (janet_keyeq(argv[0], "time")) { - category_int = LC_TIME; - } else { - janet_panicf("expected one of :all, :collate, :ctype, :monetary, :numeric, or :time, got %v", argv[0]); - } - const char *old = setlocale(category_int, locale_name); - if (old == NULL) { - janet_panicf("failed to set locale - %s", strerror(errno)); - } - return janet_wrap_nil(); -#else +#ifdef JANET_EXTENDED_LOCALE if (janet_keyeq(argv[0], "all")) { category_int = LC_ALL_MASK; } else if (janet_keyeq(argv[0], "collate")) { @@ -1959,6 +1946,27 @@ JANET_CORE_FN(os_setlocale, freelocale(old_locale); } return janet_wrap_nil(); +#else + if (janet_keyeq(argv[0], "all")) { + category_int = LC_ALL; + } else if (janet_keyeq(argv[0], "collate")) { + category_int = LC_COLLATE; + } else if (janet_keyeq(argv[0], "ctype")) { + category_int = LC_CTYPE; + } else if (janet_keyeq(argv[0], "monetary")) { + category_int = LC_MONETARY; + } else if (janet_keyeq(argv[0], "numeric")) { + category_int = LC_NUMERIC; + } else if (janet_keyeq(argv[0], "time")) { + category_int = LC_TIME; + } else { + janet_panicf("expected one of :all, :collate, :ctype, :monetary, :numeric, or :time, got %v", argv[0]); + } + const char *old = setlocale(category_int, locale_name); + if (old == NULL) { + janet_panicf("failed to set locale - %s", strerror(errno)); + } + return janet_wrap_nil(); #endif } @@ -2816,7 +2824,7 @@ void janet_lib_os(JanetTable *env) { #endif JANET_REG_END }; -#ifdef JANET_WINDOWS +#if defined(JANET_WINDOWS) && !defined(JANET_REDUCED_OS) _configthreadlocale(_ENABLE_PER_THREAD_LOCALE); #endif janet_core_cfuns_ext(env, NULL, os_cfuns);