]> git.draconx.ca Git - dxcommon.git/blobdiff - snippet/test-nls.at
help_print_optstring: Test fullwidth/halfwidth character output.
[dxcommon.git] / snippet / test-nls.at
diff --git a/snippet/test-nls.at b/snippet/test-nls.at
new file mode 100644 (file)
index 0000000..103cb5b
--- /dev/null
@@ -0,0 +1,77 @@
+# Copyright © 2023 Nick Bowler
+#
+# Helper macros for finding a locale using a specific character set.
+#
+# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+# This is free software: you are free to do what the fuck you want to.
+# There is NO WARRANTY, to the extent permitted by law.
+
+# TEST_UTF8_LOCALE([variable], [action-if-found], [action-if-not-found])
+#
+# Try to find a supported UTF-8 locale.  If any is found, the given shell
+# variable is set to the name of one such locale, and action-if-found is
+# expanded.
+#
+# If no suitable locale is found, then variable is set to the empty string
+# and action-if-not-found is expanded.  If no action is specified, the
+# default action in this scenario is to skip the current test group.
+#
+# Since m4sh overrides LC_ALL, it is recommended to use the locale by
+# setting LC_ALL to the detected value when running a program.
+m4_defun([TEST_UTF8_LOCALE],
+[AS_VAR_SET([$1], [`_TEST_UTF8_LOCALE`])
+AS_VAR_IF([$1], [""], [m4_default([$3], [AT_SKIP_IF([:])])], [$2])])
+
+m4_defun([_TEST_UTF8_LOCALE], [m4_require([_TEST_NLS_SETUP])dnl
+test_find_locale_charmap '[[Uu][Tt][Ff]-*8]'])
+
+# Output shell function definitions to implement locale detection.
+#
+# - test_check_locale_charmap name regex
+#
+#   Returns success if setting LC_ALL to the given locale name results in a
+#   charmap string that matches the given regex (as interpreted by 'grep').
+#
+# - test_find_locale_charmap regex
+#
+#   Try to find any locale for which test_check_locale_charmap is successful
+#   with the given regex.  If a matching locale is found, it is printed to
+#   standard output.
+#
+#   By default, the user's own LC_CTYPE is preferred.  Otherwise, the first
+#   match in the output of "locale -a" is chosen.
+
+m4_defun_once([_TEST_NLS_SETUP], [m4_divert_push([HEADER-COPYRIGHT])
+# save user locale setting before m4sh clobbers it
+_orig_LC_ALL=$LC_ALL
+
+m4_divert([PREPARE_TESTS])
+test_check_locale_charmap () {
+  LC_ALL=$[1] locale -ck charmap 2>&AS_MESSAGE_LOG_FD | grep "$[2]" >/dev/null 2>&1
+}
+
+test_find_locale_charmap () {
+  save_LC_ALL=$LC_ALL
+  LC_ALL=$_orig_LC_ALL
+  re=$[1]
+
+  init_ctype=`locale 2>&AS_MESSAGE_LOG_FD | $AWK -F= '$[1] == "LC_CTYPE" {
+    $[1] = ""; sub(/^ "/, ""); sub(/"$/, ""); print
+  }'`
+
+  LC_ALL=$save_LC_ALL
+  if test_check_locale_charmap "$init_ctype" "$re"; then
+    AS_ECHO(["$init_ctype"])
+    return;
+  fi
+
+  set x `locale -a | grep "$re"`; shift
+  for arg; do
+    if test_check_locale_charmap "$arg" "$re"; then
+      AS_ECHO(["$arg"])
+      return;
+    fi
+  done
+}
+m4_divert_pop([PREPARE_TESTS])
+])