summaryrefslogtreecommitdiff
path: root/gnulib-m4/mbrtoc32.m4
blob: 4da0712bd4ca1af0f49945f247bd280c18a20ac4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# mbrtoc32.m4
# serial 21
dnl Copyright (C) 2014-2024 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_MBRTOC32],
[
  AC_REQUIRE([gl_UCHAR_H_DEFAULTS])

  AC_REQUIRE([AC_TYPE_MBSTATE_T])
  dnl Determine REPLACE_MBSTATE_T, from which GNULIB_defined_mbstate_t is
  dnl determined.  It describes how our overridden mbrtowc is implemented.
  dnl We then implement mbrtoc32 accordingly.
  AC_REQUIRE([gl_MBSTATE_T_BROKEN])

  AC_REQUIRE([gl_TYPE_CHAR32_T])
  AC_REQUIRE([gl_MBRTOC32_SANITYCHECK])

  AC_REQUIRE([gl_CHECK_FUNC_MBRTOC32])
  if test $gl_cv_func_mbrtoc32 = no; then
    HAVE_MBRTOC32=0
  else
    if test $GNULIBHEADERS_OVERRIDE_CHAR32_T = 1 || test $REPLACE_MBSTATE_T = 1; then
      REPLACE_MBRTOC32=1
    else
      gl_MBRTOC32_EMPTY_INPUT
      gl_MBRTOC32_C_LOCALE
      gl_MBRTOC32_UTF8_LOCALE
      case "$gl_cv_func_mbrtoc32_empty_input" in
        *yes) ;;
        *) AC_DEFINE([MBRTOC32_EMPTY_INPUT_BUG], [1],
             [Define if the mbrtoc32 function does not return (size_t) -2 for empty input.])
           REPLACE_MBRTOC32=1
           ;;
      esac
      case "$gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ" in
        *yes) ;;
        *) AC_DEFINE([MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ], [1],
             [Define if the mbrtoc32 function may signal encoding errors in the C locale.])
           REPLACE_MBRTOC32=1
           ;;
      esac
      case "$gl_cv_func_mbrtoc32_utf8_locale_works" in
        *yes) ;;
        *) AC_DEFINE([MBRTOC32_MULTIBYTE_LOCALE_BUG], [1],
             [Define if the mbrtoc32 function does not accept the input bytes one-by-one.])
           REPLACE_MBRTOC32=1
           dnl Our replacement mbrtoc32 can handle UTF-8, but not GB18030.
           LOCALE_ZH_CN=none
           ;;
      esac
    fi
    if test $HAVE_WORKING_MBRTOC32 = 0; then
      REPLACE_MBRTOC32=1
    fi
  fi
])

AC_DEFUN([gl_CHECK_FUNC_MBRTOC32],
[
  dnl Cf. gl_CHECK_FUNCS_ANDROID
  AC_CHECK_DECL([mbrtoc32], , ,
    [[#ifdef __HAIKU__
       #include <stdint.h>
      #endif
      #include <uchar.h>
    ]])
  if test $ac_cv_have_decl_mbrtoc32 = yes; then
    dnl We can't use AC_CHECK_FUNC here, because mbrtoc32() is defined as a
    dnl static inline function on Haiku 2020.
    AC_CACHE_CHECK([for mbrtoc32], [gl_cv_func_mbrtoc32],
      [AC_LINK_IFELSE(
         [AC_LANG_PROGRAM(
            [[#include <stdlib.h>
              #ifdef __HAIKU__
               #include <stdint.h>
              #endif
              #include <uchar.h>
            ]],
            [[char32_t c;
              return mbrtoc32 (&c, "", 1, NULL) == 0;
            ]])
         ],
         [gl_cv_func_mbrtoc32=yes],
         [gl_cv_func_mbrtoc32=no])
      ])
  else
    gl_cv_func_mbrtoc32=no
  fi
])

dnl Test whether mbrtoc32 returns the correct value on empty input.

AC_DEFUN([gl_MBRTOC32_EMPTY_INPUT],
[
  AC_REQUIRE([AC_PROG_CC])
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  AC_CACHE_CHECK([whether mbrtoc32 works on empty input],
    [gl_cv_func_mbrtoc32_empty_input],
    [
      AC_RUN_IFELSE(
        [AC_LANG_SOURCE([[
           #ifdef __HAIKU__
            #include <stdint.h>
           #endif
           #include <uchar.h>
           static char32_t wc;
           static mbstate_t mbs;
           int
           main (void)
           {
             return mbrtoc32 (&wc, "", 0, &mbs) != (size_t) -2;
           }]])],
        [gl_cv_func_mbrtoc32_empty_input=yes],
        [gl_cv_func_mbrtoc32_empty_input=no],
        [case "$host_os" in
                              # Guess no on glibc systems.
           *-gnu* | gnu*)     gl_cv_func_mbrtoc32_empty_input="guessing no" ;;
                              # Guess no on Android.
           linux*-android*)   gl_cv_func_mbrtoc32_empty_input="guessing no" ;;
                              # Guess no on native Windows.
           mingw* | windows*) gl_cv_func_mbrtoc32_empty_input="guessing no" ;;
           *)                 gl_cv_func_mbrtoc32_empty_input="guessing yes" ;;
         esac
        ])
    ])
])

dnl <https://pubs.opengroup.org/onlinepubs/9699919799/functions/mbrtowc.html>
dnl POSIX:2018 says regarding mbrtowc: "In the POSIX locale an [EILSEQ] error
dnl cannot occur since all byte values are valid characters."  It is reasonable
dnl to expect mbrtoc32 to behave in the same way.

AC_DEFUN([gl_MBRTOC32_C_LOCALE],
[
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  AC_CACHE_CHECK([whether the C locale is free of encoding errors],
    [gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ],
    [AC_RUN_IFELSE(
       [AC_LANG_PROGRAM(
          [[#include <limits.h>
            #include <locale.h>
            #ifdef __HAIKU__
             #include <stdint.h>
            #endif
            #include <uchar.h>
          ]], [[
            int i;
            char *locale = setlocale (LC_ALL, "C");
            if (! locale)
              return 2;
            for (i = CHAR_MIN; i <= CHAR_MAX; i++)
              {
                char c = i;
                char32_t wc;
                mbstate_t mbs = { 0, };
                size_t ss = mbrtoc32 (&wc, &c, 1, &mbs);
                if (1 < ss)
                  return 3;
              }
            return 0;
          ]])],
       [gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ=yes],
       [gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ=no],
       [case "$host_os" in
                             # Guess yes on native Windows.
          mingw* | windows*) gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ="guessing yes" ;;
          *)                 gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ="$gl_cross_guess_normal" ;;
        esac
       ])
    ])
])

dnl Test whether mbrtoc32 works when it's fed the bytes one-by-one in an UTF-8
dnl locale.

AC_DEFUN([gl_MBRTOC32_UTF8_LOCALE],
[
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  AC_CACHE_CHECK([whether mbrtoc32 works in an UTF-8 locale],
    [gl_cv_func_mbrtoc32_utf8_locale_works],
    [AC_RUN_IFELSE(
       [AC_LANG_PROGRAM(
          [[#include <locale.h>
            #ifdef __HAIKU__
             #include <stdint.h>
            #endif
            #include <uchar.h>
          ]], [[
            char *locale = setlocale (LC_ALL, "en_US.UTF-8");
            if (locale)
              {
                /* This test fails on Cygwin 3.5.3.  */
                mbstate_t state = { 0, };
                char32_t uc = 0xDEADBEEF;
                /* \360\237\220\203 = U+0001F403 */
                if (mbrtoc32 (&uc, "\360", 1, &state) != (size_t)-2)
                  return 1;
                if (mbrtoc32 (&uc, "\237", 1, &state) != (size_t)-2)
                  return 2;
                if (mbrtoc32 (&uc, "\220", 1, &state) != (size_t)-2)
                  return 3;
                if (mbrtoc32 (&uc, "\203", 1, &state) != 1)
                  return 4;
                if (uc != 0x0001F403)
                  return 5;
              }
            return 0;
          ]])],
       [gl_cv_func_mbrtoc32_utf8_locale_works=yes],
       [gl_cv_func_mbrtoc32_utf8_locale_works=no],
       [case "$host_os" in
                   # Guess no on Cygwin.
          cygwin*) gl_cv_func_mbrtoc32_utf8_locale_works="guessing no" ;;
          *)       gl_cv_func_mbrtoc32_utf8_locale_works="$gl_cross_guess_normal" ;;
        esac
       ])
    ])
])

dnl Test whether mbrtoc32 works not worse than mbrtowc.
dnl Result is HAVE_WORKING_MBRTOC32.

AC_DEFUN([gl_MBRTOC32_SANITYCHECK],
[
  AC_REQUIRE([AC_PROG_CC])
  AC_REQUIRE([gl_TYPE_CHAR32_T])
  AC_REQUIRE([gl_CHECK_FUNC_MBRTOC32])
  AC_REQUIRE([gt_LOCALE_FR])
  AC_REQUIRE([gt_LOCALE_ZH_CN])
  AC_REQUIRE([AC_CANONICAL_HOST])
  if test $GNULIBHEADERS_OVERRIDE_CHAR32_T = 1 || test $gl_cv_func_mbrtoc32 = no; then
    HAVE_WORKING_MBRTOC32=0
  else
    AC_CACHE_CHECK([whether mbrtoc32 works as well as mbrtowc],
      [gl_cv_func_mbrtoc32_sanitycheck],
      [
        dnl Initial guess, used when cross-compiling or when no suitable locale
        dnl is present.
changequote(,)dnl
        case "$host_os" in
          # Guess no on FreeBSD, Solaris, native Windows.
          freebsd* | midnightbsd* | solaris* | mingw* | windows*)
            gl_cv_func_mbrtoc32_sanitycheck="guessing no"
            ;;
          # Guess yes otherwise.
          *)
            gl_cv_func_mbrtoc32_sanitycheck="guessing yes"
            ;;
        esac
changequote([,])dnl
        if test $LOCALE_FR != none || test $LOCALE_ZH_CN != none; then
          AC_RUN_IFELSE(
            [AC_LANG_SOURCE([[
#include <locale.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#ifdef __HAIKU__
 #include <stdint.h>
#endif
#include <uchar.h>
int main ()
{
  int result = 0;
  /* This fails on native Windows:
     mbrtoc32 returns (size_t)-1.
     mbrtowc returns 1 (correct).  */
  if (strcmp ("$LOCALE_FR", "none") != 0
      && setlocale (LC_ALL, "$LOCALE_FR") != NULL)
    {
      mbstate_t state;
      wchar_t wc = (wchar_t) 0xBADFACE;
      memset (&state, '\0', sizeof (mbstate_t));
      if (mbrtowc (&wc, "\374", 1, &state) == 1)
        {
          char32_t c32 = (wchar_t) 0xBADFACE;
          memset (&state, '\0', sizeof (mbstate_t));
          if (mbrtoc32 (&c32, "\374", 1, &state) != 1)
            result |= 1;
        }
    }
  /* This fails on FreeBSD 13.0 and Solaris 11.4:
     mbrtoc32 returns (size_t)-2 or (size_t)-1.
     mbrtowc returns 4 (correct).  */
  if (strcmp ("$LOCALE_ZH_CN", "none") != 0
      && setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
    {
      mbstate_t state;
      wchar_t wc = (wchar_t) 0xBADFACE;
      memset (&state, '\0', sizeof (mbstate_t));
      if (mbrtowc (&wc, "\224\071\375\067", 4, &state) == 4)
        {
          char32_t c32 = (wchar_t) 0xBADFACE;
          memset (&state, '\0', sizeof (mbstate_t));
          if (mbrtoc32 (&c32, "\224\071\375\067", 4, &state) != 4)
            result |= 2;
        }
    }
  return result;
}]])],
            [gl_cv_func_mbrtoc32_sanitycheck=yes],
            [gl_cv_func_mbrtoc32_sanitycheck=no],
            [:])
        fi
      ])
    case "$gl_cv_func_mbrtoc32_sanitycheck" in
      *yes)
        HAVE_WORKING_MBRTOC32=1
        AC_DEFINE([HAVE_WORKING_MBRTOC32], [1],
          [Define if the mbrtoc32 function basically works.])
        ;;
      *) HAVE_WORKING_MBRTOC32=0 ;;
    esac
  fi
  AC_SUBST([HAVE_WORKING_MBRTOC32])
])

# Prerequisites of lib/mbrtoc32.c and lib/lc-charset-dispatch.c.
AC_DEFUN([gl_PREREQ_MBRTOC32], [
  AC_REQUIRE([gl_C32RTOMB_SANITYCHECK])
  :
])