]> git.draconx.ca Git - dxcommon.git/blob - snippet/test-nls.at
DX_C_ALIGNAS: Work around bash-5 parsing bug.
[dxcommon.git] / snippet / test-nls.at
1 # Copyright © 2023 Nick Bowler
2 #
3 # Helper macros for finding a locale using a specific character set.
4 #
5 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
6 # This is free software: you are free to do what the fuck you want to.
7 # There is NO WARRANTY, to the extent permitted by law.
8
9 # TEST_UTF8_LOCALE([variable], [action-if-found], [action-if-not-found])
10 #
11 # Try to find a supported UTF-8 locale.  If any is found, the given shell
12 # variable is set to the name of one such locale, and action-if-found is
13 # expanded.
14 #
15 # If no suitable locale is found, then variable is set to the empty string
16 # and action-if-not-found is expanded.  If no action is specified, the
17 # default action in this scenario is to skip the current test group.
18 #
19 # Since m4sh overrides LC_ALL, it is recommended to use the locale by
20 # setting LC_ALL to the detected value when running a program.
21 m4_defun([TEST_UTF8_LOCALE],
22 [AS_VAR_SET([$1], [`_TEST_UTF8_LOCALE`])
23 AS_VAR_IF([$1], [""], [m4_default([$3], [AT_SKIP_IF([:])])], [$2])])
24
25 m4_defun([_TEST_UTF8_LOCALE], [m4_require([_TEST_NLS_SETUP])dnl
26 test_find_locale_charmap '[[Uu][Tt][Ff]-*8]'])
27
28 # Output shell function definitions to implement locale detection.
29 #
30 # - test_check_locale_charmap name regex
31 #
32 #   Returns success if setting LC_ALL to the given locale name results in a
33 #   charmap string that matches the given regex (as interpreted by 'grep').
34 #
35 # - test_find_locale_charmap regex
36 #
37 #   Try to find any locale for which test_check_locale_charmap is successful
38 #   with the given regex.  If a matching locale is found, it is printed to
39 #   standard output.
40 #
41 #   By default, the user's own LC_CTYPE is preferred.  Otherwise, the first
42 #   match in the output of "locale -a" is chosen, unless that starts with
43 #   "C" in which case we prefer the next one.
44
45 m4_defun_once([_TEST_NLS_SETUP], [m4_divert_push([HEADER-COPYRIGHT])
46 # save user locale setting before m4sh clobbers it
47 _orig_LC_ALL=$LC_ALL
48
49 m4_divert([PREPARE_TESTS])
50 test_check_locale_charmap () {
51   LC_ALL=$[1] locale -ck charmap 2>&AS_MESSAGE_LOG_FD | grep "$[2]" >/dev/null 2>&1
52 }
53
54 test_find_locale_charmap () {
55   save_LC_ALL=$LC_ALL
56   LC_ALL=$_orig_LC_ALL
57   re=$[1]
58
59   init_ctype=`locale 2>&AS_MESSAGE_LOG_FD | $AWK -F= '$[1] == "LC_CTYPE" {
60     $[1] = ""; sub(/^ "/, ""); sub(/"$/, ""); print
61   }'`
62
63   LC_ALL=$save_LC_ALL found_locale=
64   if test_check_locale_charmap "$init_ctype" "$re"; then
65     found_locale=$init_ctype
66   fi
67
68   case $found_locale in
69   C*|"")
70     set x `locale -a | grep "$re"`; shift
71     for arg; do
72       if test_check_locale_charmap "$arg" "$re"; then
73         found_locale=$arg;
74         case $found_locale in
75         C*) :;;
76         *) break ;;
77         esac
78       fi
79     done
80     ;;
81   esac
82
83   test ${found_locale+y} && {
84     AS_ECHO(["found matching locale $found_locale"]) >&AS_MESSAGE_LOG_FD
85     LC_ALL=$found_locale locale >&AS_MESSAGE_LOG_FD 2>&1
86     AS_ECHO(["$found_locale"])
87   }
88 }
89 m4_divert_pop([PREPARE_TESTS])
90 ])