2 dnl Copyright (C) 2014-2020 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
7 AC_DEFUN([gl_FUNC_MBRTOC32],
9 AC_REQUIRE([gl_UCHAR_H_DEFAULTS])
11 AC_REQUIRE([AC_TYPE_MBSTATE_T])
14 AC_REQUIRE([gl_TYPE_CHAR32_T])
15 AC_REQUIRE([gl_MBRTOC32_SANITYCHECK])
17 AC_REQUIRE([gl_CHECK_FUNC_MBRTOC32])
18 if test $gl_cv_func_mbrtoc32 = no; then
21 if test $GNULIB_OVERRIDES_CHAR32_T = 1 || test $REPLACE_MBSTATE_T = 1; then
24 gl_MBRTOC32_EMPTY_INPUT
26 case "$gl_cv_func_mbrtoc32_empty_input" in
28 *) AC_DEFINE([MBRTOC32_EMPTY_INPUT_BUG], [1],
29 [Define if the mbrtoc32 function does not return (size_t) -2 for empty input.])
33 case "$gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ" in
35 *) AC_DEFINE([MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ], [1],
36 [Define if the mbrtoc32 function may signal encoding errors in the C locale.])
41 if test $HAVE_WORKING_MBRTOC32 = 0; then
47 dnl We can't use AC_CHECK_FUNC here, because mbrtoc32() is defined as a
48 dnl static inline function on Haiku 2020.
49 AC_DEFUN([gl_CHECK_FUNC_MBRTOC32],
51 AC_CACHE_CHECK([for mbrtoc32], [gl_cv_func_mbrtoc32],
58 return mbrtoc32 (&c, "", 1, NULL) == 0;
61 [gl_cv_func_mbrtoc32=yes],
62 [gl_cv_func_mbrtoc32=no])
66 AC_DEFUN([gl_MBRTOC32_EMPTY_INPUT],
68 AC_REQUIRE([AC_PROG_CC])
69 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
70 AC_CACHE_CHECK([whether mbrtoc32 works on empty input],
71 [gl_cv_func_mbrtoc32_empty_input],
73 dnl Initial guess, used when cross-compiling or when no suitable locale
77 # Guess no on glibc systems.
78 *-gnu* | gnu*) gl_cv_func_mbrtoc32_empty_input="guessing no" ;;
79 *) gl_cv_func_mbrtoc32_empty_input="guessing yes" ;;
90 return mbrtoc32 (&wc, "", 0, &mbs) != (size_t) -2;
92 [gl_cv_func_mbrtoc32_empty_input=yes],
93 [gl_cv_func_mbrtoc32_empty_input=no],
98 AC_DEFUN([gl_MBRTOC32_C_LOCALE],
100 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
101 AC_CACHE_CHECK([whether the C locale is free of encoding errors],
102 [gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ],
104 dnl Initial guess, used when cross-compiling or when no suitable locale
106 gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ="$gl_cross_guess_normal"
110 [[#include <limits.h>
115 char *locale = setlocale (LC_ALL, "C");
118 for (i = CHAR_MIN; i <= CHAR_MAX; i++)
122 mbstate_t mbs = { 0, };
123 size_t ss = mbrtoc32 (&wc, &c, 1, &mbs);
129 [gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ=yes],
130 [gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ=no],
132 # Guess yes on native Windows.
133 mingw*) gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ="guessing yes" ;;
139 dnl Test whether mbrtoc32 works not worse than mbrtowc.
140 dnl Result is HAVE_WORKING_MBRTOC32.
142 AC_DEFUN([gl_MBRTOC32_SANITYCHECK],
144 AC_REQUIRE([AC_PROG_CC])
145 AC_REQUIRE([gl_TYPE_CHAR32_T])
146 AC_REQUIRE([gl_CHECK_FUNC_MBRTOC32])
147 AC_REQUIRE([gt_LOCALE_FR])
148 AC_REQUIRE([gt_LOCALE_ZH_CN])
149 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
150 if test $GNULIB_OVERRIDES_CHAR32_T = 1 || test $gl_cv_func_mbrtoc32 = no; then
151 HAVE_WORKING_MBRTOC32=0
153 AC_CACHE_CHECK([whether mbrtoc32 works as well as mbrtowc],
154 [gl_cv_func_mbrtoc32_sanitycheck],
156 dnl Initial guess, used when cross-compiling or when no suitable locale
160 # Guess no on FreeBSD, Solaris, native Windows.
161 freebsd* | solaris* | mingw*)
162 gl_cv_func_mbrtoc32_sanitycheck="guessing no"
164 # Guess yes otherwise.
166 gl_cv_func_mbrtoc32_sanitycheck="guessing yes"
170 if test $LOCALE_FR != none || test $LOCALE_ZH_CN != none; then
176 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
178 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
179 included before <wchar.h>. */
188 /* This fails on native Windows:
189 mbrtoc32 returns (size_t)-1.
190 mbrtowc returns 1 (correct). */
191 if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
194 wchar_t wc = (wchar_t) 0xBADFACE;
195 memset (&state, '\0', sizeof (mbstate_t));
196 if (mbrtowc (&wc, "\374", 1, &state) == 1)
198 char32_t c32 = (wchar_t) 0xBADFACE;
199 memset (&state, '\0', sizeof (mbstate_t));
200 if (mbrtoc32 (&c32, "\374", 1, &state) != 1)
204 /* This fails on FreeBSD 12 and Solaris 11.4:
205 mbrtoc32 returns (size_t)-2 or (size_t)-1.
206 mbrtowc returns 4 (correct). */
207 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
210 wchar_t wc = (wchar_t) 0xBADFACE;
211 memset (&state, '\0', sizeof (mbstate_t));
212 if (mbrtowc (&wc, "\224\071\375\067", 4, &state) == 4)
214 char32_t c32 = (wchar_t) 0xBADFACE;
215 memset (&state, '\0', sizeof (mbstate_t));
216 if (mbrtoc32 (&c32, "\224\071\375\067", 4, &state) != 4)
222 [gl_cv_func_mbrtoc32_sanitycheck=yes],
223 [gl_cv_func_mbrtoc32_sanitycheck=no],
227 case "$gl_cv_func_mbrtoc32_sanitycheck" in
229 HAVE_WORKING_MBRTOC32=1
230 AC_DEFINE([HAVE_WORKING_MBRTOC32], [1],
231 [Define if the mbrtoc32 function basically works.])
233 *) HAVE_WORKING_MBRTOC32=0 ;;
236 AC_SUBST([HAVE_WORKING_MBRTOC32])
239 # Prerequisites of lib/mbrtoc32.c and lib/lc-charset-dispatch.c.
240 AC_DEFUN([gl_PREREQ_MBRTOC32], [