| 1 |
# mbrtoc32.m4 serial 17 |
| 2 |
dnl Copyright (C) 2014-2023 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. |
| 6 |
|
| 7 |
AC_DEFUN([gl_FUNC_MBRTOC32], |
| 8 |
[ |
| 9 |
AC_REQUIRE([gl_UCHAR_H_DEFAULTS]) |
| 10 |
|
| 11 |
AC_REQUIRE([AC_TYPE_MBSTATE_T]) |
| 12 |
dnl Determine REPLACE_MBSTATE_T, from which GNULIB_defined_mbstate_t is |
| 13 |
dnl determined. It describes how our overridden mbrtowc is implemented. |
| 14 |
dnl We then implement mbrtoc32 accordingly. |
| 15 |
AC_REQUIRE([gl_MBSTATE_T_BROKEN]) |
| 16 |
|
| 17 |
AC_REQUIRE([gl_TYPE_CHAR32_T]) |
| 18 |
AC_REQUIRE([gl_MBRTOC32_SANITYCHECK]) |
| 19 |
|
| 20 |
AC_REQUIRE([gl_CHECK_FUNC_MBRTOC32]) |
| 21 |
if test $gl_cv_func_mbrtoc32 = no; then |
| 22 |
HAVE_MBRTOC32=0 |
| 23 |
else |
| 24 |
if test $GNULIBHEADERS_OVERRIDE_CHAR32_T = 1 || test $REPLACE_MBSTATE_T = 1; then |
| 25 |
REPLACE_MBRTOC32=1 |
| 26 |
else |
| 27 |
gl_MBRTOC32_EMPTY_INPUT |
| 28 |
gl_MBRTOC32_C_LOCALE |
| 29 |
case "$gl_cv_func_mbrtoc32_empty_input" in |
| 30 |
*yes) ;; |
| 31 |
*) AC_DEFINE([MBRTOC32_EMPTY_INPUT_BUG], [1], |
| 32 |
[Define if the mbrtoc32 function does not return (size_t) -2 for empty input.]) |
| 33 |
REPLACE_MBRTOC32=1 |
| 34 |
;; |
| 35 |
esac |
| 36 |
case "$gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ" in |
| 37 |
*yes) ;; |
| 38 |
*) AC_DEFINE([MBRTOC32_IN_C_LOCALE_MAYBE_EILSEQ], [1], |
| 39 |
[Define if the mbrtoc32 function may signal encoding errors in the C locale.]) |
| 40 |
REPLACE_MBRTOC32=1 |
| 41 |
;; |
| 42 |
esac |
| 43 |
fi |
| 44 |
if test $HAVE_WORKING_MBRTOC32 = 0; then |
| 45 |
REPLACE_MBRTOC32=1 |
| 46 |
fi |
| 47 |
fi |
| 48 |
]) |
| 49 |
|
| 50 |
AC_DEFUN([gl_CHECK_FUNC_MBRTOC32], |
| 51 |
[ |
| 52 |
dnl Cf. gl_CHECK_FUNCS_ANDROID |
| 53 |
AC_CHECK_DECL([mbrtoc32], , , |
| 54 |
[[#ifdef __HAIKU__ |
| 55 |
#include <stdint.h> |
| 56 |
#endif |
| 57 |
#include <uchar.h> |
| 58 |
]]) |
| 59 |
if test $ac_cv_have_decl_mbrtoc32 = yes; then |
| 60 |
dnl We can't use AC_CHECK_FUNC here, because mbrtoc32() is defined as a |
| 61 |
dnl static inline function on Haiku 2020. |
| 62 |
AC_CACHE_CHECK([for mbrtoc32], [gl_cv_func_mbrtoc32], |
| 63 |
[AC_LINK_IFELSE( |
| 64 |
[AC_LANG_PROGRAM( |
| 65 |
[[#include <stdlib.h> |
| 66 |
#ifdef __HAIKU__ |
| 67 |
#include <stdint.h> |
| 68 |
#endif |
| 69 |
#include <uchar.h> |
| 70 |
]], |
| 71 |
[[char32_t c; |
| 72 |
return mbrtoc32 (&c, "", 1, NULL) == 0; |
| 73 |
]]) |
| 74 |
], |
| 75 |
[gl_cv_func_mbrtoc32=yes], |
| 76 |
[gl_cv_func_mbrtoc32=no]) |
| 77 |
]) |
| 78 |
else |
| 79 |
gl_cv_func_mbrtoc32=no |
| 80 |
fi |
| 81 |
]) |
| 82 |
|
| 83 |
dnl Test whether mbrtoc32 returns the correct value on empty input. |
| 84 |
|
| 85 |
AC_DEFUN([gl_MBRTOC32_EMPTY_INPUT], |
| 86 |
[ |
| 87 |
AC_REQUIRE([AC_PROG_CC]) |
| 88 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 89 |
AC_CACHE_CHECK([whether mbrtoc32 works on empty input], |
| 90 |
[gl_cv_func_mbrtoc32_empty_input], |
| 91 |
[ |
| 92 |
AC_RUN_IFELSE( |
| 93 |
[AC_LANG_SOURCE([[ |
| 94 |
#ifdef __HAIKU__ |
| 95 |
#include <stdint.h> |
| 96 |
#endif |
| 97 |
#include <uchar.h> |
| 98 |
static char32_t wc; |
| 99 |
static mbstate_t mbs; |
| 100 |
int |
| 101 |
main (void) |
| 102 |
{ |
| 103 |
return mbrtoc32 (&wc, "", 0, &mbs) != (size_t) -2; |
| 104 |
}]])], |
| 105 |
[gl_cv_func_mbrtoc32_empty_input=yes], |
| 106 |
[gl_cv_func_mbrtoc32_empty_input=no], |
| 107 |
[case "$host_os" in |
| 108 |
# Guess no on glibc systems. |
| 109 |
*-gnu* | gnu*) gl_cv_func_mbrtoc32_empty_input="guessing no" ;; |
| 110 |
# Guess no on Android. |
| 111 |
linux*-android*) gl_cv_func_mbrtoc32_empty_input="guessing no" ;; |
| 112 |
# Guess no on native Windows. |
| 113 |
mingw*) gl_cv_func_mbrtoc32_empty_input="guessing no" ;; |
| 114 |
*) gl_cv_func_mbrtoc32_empty_input="guessing yes" ;; |
| 115 |
esac |
| 116 |
]) |
| 117 |
]) |
| 118 |
]) |
| 119 |
|
| 120 |
dnl <https://pubs.opengroup.org/onlinepubs/9699919799/functions/mbrtowc.html> |
| 121 |
dnl POSIX:2018 says regarding mbrtowc: "In the POSIX locale an [EILSEQ] error |
| 122 |
dnl cannot occur since all byte values are valid characters." It is reasonable |
| 123 |
dnl to expect mbrtoc32 to behave in the same way. |
| 124 |
|
| 125 |
AC_DEFUN([gl_MBRTOC32_C_LOCALE], |
| 126 |
[ |
| 127 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 128 |
AC_CACHE_CHECK([whether the C locale is free of encoding errors], |
| 129 |
[gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ], |
| 130 |
[AC_RUN_IFELSE( |
| 131 |
[AC_LANG_PROGRAM( |
| 132 |
[[#include <limits.h> |
| 133 |
#include <locale.h> |
| 134 |
#ifdef __HAIKU__ |
| 135 |
#include <stdint.h> |
| 136 |
#endif |
| 137 |
#include <uchar.h> |
| 138 |
]], [[ |
| 139 |
int i; |
| 140 |
char *locale = setlocale (LC_ALL, "C"); |
| 141 |
if (! locale) |
| 142 |
return 2; |
| 143 |
for (i = CHAR_MIN; i <= CHAR_MAX; i++) |
| 144 |
{ |
| 145 |
char c = i; |
| 146 |
char32_t wc; |
| 147 |
mbstate_t mbs = { 0, }; |
| 148 |
size_t ss = mbrtoc32 (&wc, &c, 1, &mbs); |
| 149 |
if (1 < ss) |
| 150 |
return 3; |
| 151 |
} |
| 152 |
return 0; |
| 153 |
]])], |
| 154 |
[gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ=yes], |
| 155 |
[gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ=no], |
| 156 |
[case "$host_os" in |
| 157 |
# Guess yes on native Windows. |
| 158 |
mingw*) gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ="guessing yes" ;; |
| 159 |
*) gl_cv_func_mbrtoc32_C_locale_sans_EILSEQ="$gl_cross_guess_normal" ;; |
| 160 |
esac |
| 161 |
]) |
| 162 |
]) |
| 163 |
]) |
| 164 |
|
| 165 |
dnl Test whether mbrtoc32 works not worse than mbrtowc. |
| 166 |
dnl Result is HAVE_WORKING_MBRTOC32. |
| 167 |
|
| 168 |
AC_DEFUN([gl_MBRTOC32_SANITYCHECK], |
| 169 |
[ |
| 170 |
AC_REQUIRE([AC_PROG_CC]) |
| 171 |
AC_REQUIRE([gl_TYPE_CHAR32_T]) |
| 172 |
AC_REQUIRE([gl_CHECK_FUNC_MBRTOC32]) |
| 173 |
AC_REQUIRE([gt_LOCALE_FR]) |
| 174 |
AC_REQUIRE([gt_LOCALE_ZH_CN]) |
| 175 |
AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles |
| 176 |
if test $GNULIBHEADERS_OVERRIDE_CHAR32_T = 1 || test $gl_cv_func_mbrtoc32 = no; then |
| 177 |
HAVE_WORKING_MBRTOC32=0 |
| 178 |
else |
| 179 |
AC_CACHE_CHECK([whether mbrtoc32 works as well as mbrtowc], |
| 180 |
[gl_cv_func_mbrtoc32_sanitycheck], |
| 181 |
[ |
| 182 |
dnl Initial guess, used when cross-compiling or when no suitable locale |
| 183 |
dnl is present. |
| 184 |
changequote(,)dnl |
| 185 |
case "$host_os" in |
| 186 |
# Guess no on FreeBSD, Solaris, native Windows. |
| 187 |
freebsd* | midnightbsd* | solaris* | mingw*) |
| 188 |
gl_cv_func_mbrtoc32_sanitycheck="guessing no" |
| 189 |
;; |
| 190 |
# Guess yes otherwise. |
| 191 |
*) |
| 192 |
gl_cv_func_mbrtoc32_sanitycheck="guessing yes" |
| 193 |
;; |
| 194 |
esac |
| 195 |
changequote([,])dnl |
| 196 |
if test $LOCALE_FR != none || test $LOCALE_ZH_CN != none; then |
| 197 |
AC_RUN_IFELSE( |
| 198 |
[AC_LANG_SOURCE([[ |
| 199 |
#include <locale.h> |
| 200 |
#include <stdlib.h> |
| 201 |
#include <string.h> |
| 202 |
#include <wchar.h> |
| 203 |
#ifdef __HAIKU__ |
| 204 |
#include <stdint.h> |
| 205 |
#endif |
| 206 |
#include <uchar.h> |
| 207 |
int main () |
| 208 |
{ |
| 209 |
int result = 0; |
| 210 |
/* This fails on native Windows: |
| 211 |
mbrtoc32 returns (size_t)-1. |
| 212 |
mbrtowc returns 1 (correct). */ |
| 213 |
if (strcmp ("$LOCALE_FR", "none") != 0 |
| 214 |
&& setlocale (LC_ALL, "$LOCALE_FR") != NULL) |
| 215 |
{ |
| 216 |
mbstate_t state; |
| 217 |
wchar_t wc = (wchar_t) 0xBADFACE; |
| 218 |
memset (&state, '\0', sizeof (mbstate_t)); |
| 219 |
if (mbrtowc (&wc, "\374", 1, &state) == 1) |
| 220 |
{ |
| 221 |
char32_t c32 = (wchar_t) 0xBADFACE; |
| 222 |
memset (&state, '\0', sizeof (mbstate_t)); |
| 223 |
if (mbrtoc32 (&c32, "\374", 1, &state) != 1) |
| 224 |
result |= 1; |
| 225 |
} |
| 226 |
} |
| 227 |
/* This fails on FreeBSD 13.0 and Solaris 11.4: |
| 228 |
mbrtoc32 returns (size_t)-2 or (size_t)-1. |
| 229 |
mbrtowc returns 4 (correct). */ |
| 230 |
if (strcmp ("$LOCALE_ZH_CN", "none") != 0 |
| 231 |
&& setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL) |
| 232 |
{ |
| 233 |
mbstate_t state; |
| 234 |
wchar_t wc = (wchar_t) 0xBADFACE; |
| 235 |
memset (&state, '\0', sizeof (mbstate_t)); |
| 236 |
if (mbrtowc (&wc, "\224\071\375\067", 4, &state) == 4) |
| 237 |
{ |
| 238 |
char32_t c32 = (wchar_t) 0xBADFACE; |
| 239 |
memset (&state, '\0', sizeof (mbstate_t)); |
| 240 |
if (mbrtoc32 (&c32, "\224\071\375\067", 4, &state) != 4) |
| 241 |
result |= 2; |
| 242 |
} |
| 243 |
} |
| 244 |
return result; |
| 245 |
}]])], |
| 246 |
[gl_cv_func_mbrtoc32_sanitycheck=yes], |
| 247 |
[gl_cv_func_mbrtoc32_sanitycheck=no], |
| 248 |
[:]) |
| 249 |
fi |
| 250 |
]) |
| 251 |
case "$gl_cv_func_mbrtoc32_sanitycheck" in |
| 252 |
*yes) |
| 253 |
HAVE_WORKING_MBRTOC32=1 |
| 254 |
AC_DEFINE([HAVE_WORKING_MBRTOC32], [1], |
| 255 |
[Define if the mbrtoc32 function basically works.]) |
| 256 |
;; |
| 257 |
*) HAVE_WORKING_MBRTOC32=0 ;; |
| 258 |
esac |
| 259 |
fi |
| 260 |
AC_SUBST([HAVE_WORKING_MBRTOC32]) |
| 261 |
]) |
| 262 |
|
| 263 |
# Prerequisites of lib/mbrtoc32.c and lib/lc-charset-dispatch.c. |
| 264 |
AC_DEFUN([gl_PREREQ_MBRTOC32], [ |
| 265 |
: |
| 266 |
]) |