2 dnl Copyright (C) 2001-2002, 2004-2005, 2008-2010 Free Software Foundation,
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_MBRTOWC],
10 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
12 AC_REQUIRE([AC_TYPE_MBSTATE_T])
15 AC_CHECK_FUNCS_ONCE([mbrtowc])
16 if test $ac_cv_func_mbrtowc = no; then
19 if test $REPLACE_MBSTATE_T = 1; then
25 case "$gl_cv_func_mbrtowc_null_arg" in
27 *) AC_DEFINE([MBRTOWC_NULL_ARG_BUG], [1],
28 [Define if the mbrtowc function has the NULL string argument bug.])
32 case "$gl_cv_func_mbrtowc_retval" in
34 *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
35 [Define if the mbrtowc function returns a wrong return value.])
39 case "$gl_cv_func_mbrtowc_nul_retval" in
41 *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
42 [Define if the mbrtowc function does not return 0 for a NUL character.])
48 if test $HAVE_MBRTOWC = 0 || test $REPLACE_MBRTOWC = 1; then
55 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
56 dnl redefines the semantics of the given mbstate_t type.
57 dnl Result is REPLACE_MBSTATE_T.
58 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
59 dnl avoid inconsistencies.
61 AC_DEFUN([gl_MBSTATE_T_BROKEN],
63 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
65 AC_REQUIRE([AC_TYPE_MBSTATE_T])
66 AC_CHECK_FUNCS_ONCE([mbsinit])
67 AC_CHECK_FUNCS_ONCE([mbrtowc])
68 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
69 gl_MBRTOWC_INCOMPLETE_STATE
70 gl_MBRTOWC_SANITYCHECK
72 case "$gl_cv_func_mbrtowc_incomplete_state" in
74 *) REPLACE_MBSTATE_T=1 ;;
76 case "$gl_cv_func_mbrtowc_sanitycheck" in
78 *) REPLACE_MBSTATE_T=1 ;;
83 if test $REPLACE_MBSTATE_T = 1; then
88 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
89 dnl incomplete multibyte character.
90 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
92 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
94 AC_REQUIRE([AC_PROG_CC])
95 AC_REQUIRE([gt_LOCALE_JA])
96 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
97 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
98 [gl_cv_func_mbrtowc_incomplete_state],
100 dnl Initial guess, used when cross-compiling or when no suitable locale
104 # Guess no on AIX and OSF/1.
105 osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
106 # Guess yes otherwise.
107 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
110 if test $LOCALE_JA != none; then
117 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
119 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
123 memset (&state, '\0', sizeof (mbstate_t));
124 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
125 if (mbsinit (&state))
130 [gl_cv_func_mbrtowc_incomplete_state=yes],
131 [gl_cv_func_mbrtowc_incomplete_state=no],
137 dnl Test whether mbrtowc works not worse than mbtowc.
138 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
140 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
142 AC_REQUIRE([AC_PROG_CC])
143 AC_REQUIRE([gt_LOCALE_ZH_CN])
144 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
145 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
146 [gl_cv_func_mbrtowc_sanitycheck],
148 dnl Initial guess, used when cross-compiling or when no suitable locale
152 # Guess no on Solaris 8.
153 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
154 # Guess yes otherwise.
155 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
158 if test $LOCALE_ZH_CN != none; then
166 /* This fails on Solaris 8:
167 mbrtowc returns 2, and sets wc to 0x00F0.
168 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
169 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
171 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
175 memset (&state, '\0', sizeof (mbstate_t));
176 if (mbrtowc (&wc, input + 3, 6, &state) != 4
177 && mbtowc (&wc, input + 3, 6) == 4)
182 [gl_cv_func_mbrtowc_sanitycheck=yes],
183 [gl_cv_func_mbrtowc_sanitycheck=no],
189 dnl Test whether mbrtowc supports a NULL string argument correctly.
190 dnl Result is gl_cv_func_mbrtowc_null_arg.
192 AC_DEFUN([gl_MBRTOWC_NULL_ARG],
194 AC_REQUIRE([AC_PROG_CC])
195 AC_REQUIRE([gt_LOCALE_FR_UTF8])
196 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
197 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
198 [gl_cv_func_mbrtowc_null_arg],
200 dnl Initial guess, used when cross-compiling or when no suitable locale
205 osf*) gl_cv_func_mbrtowc_null_arg="guessing no" ;;
206 # Guess yes otherwise.
207 *) gl_cv_func_mbrtowc_null_arg="guessing yes" ;;
210 if test $LOCALE_FR_UTF8 != none; then
217 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
223 memset (&state, '\0', sizeof (mbstate_t));
224 wc = (wchar_t) 0xBADFACE;
225 mbrtowc (&wc, NULL, 5, &state);
226 /* Check that wc was not modified. */
227 if (wc != (wchar_t) 0xBADFACE)
231 }], [gl_cv_func_mbrtowc_null_arg=yes], [gl_cv_func_mbrtowc_null_arg=no], [:])
236 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
237 dnl correctly returns the number of bytes that were needed to complete the
238 dnl character (not the total number of bytes of the multibyte character).
239 dnl Result is gl_cv_func_mbrtowc_retval.
241 AC_DEFUN([gl_MBRTOWC_RETVAL],
243 AC_REQUIRE([AC_PROG_CC])
244 AC_REQUIRE([gt_LOCALE_FR_UTF8])
245 AC_REQUIRE([gt_LOCALE_JA])
246 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
247 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
248 [gl_cv_func_mbrtowc_retval],
250 dnl Initial guess, used when cross-compiling or when no suitable locale
254 # Guess no on HP-UX and Solaris.
255 hpux* | solaris*) gl_cv_func_mbrtowc_retval="guessing no" ;;
256 # Guess yes otherwise.
257 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
260 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none; then
267 /* This fails on Solaris. */
268 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
270 char input[] = "B\303\274\303\237er"; /* "Büßer" */
274 memset (&state, '\0', sizeof (mbstate_t));
275 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
278 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
282 /* This fails on HP-UX 11.11. */
283 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
285 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
289 memset (&state, '\0', sizeof (mbstate_t));
290 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
293 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
299 [gl_cv_func_mbrtowc_retval=yes],
300 [gl_cv_func_mbrtowc_retval=no],
306 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
307 dnl Result is gl_cv_func_mbrtowc_nul_retval.
309 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
311 AC_REQUIRE([AC_PROG_CC])
312 AC_REQUIRE([gt_LOCALE_ZH_CN])
313 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
314 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
315 [gl_cv_func_mbrtowc_nul_retval],
317 dnl Initial guess, used when cross-compiling or when no suitable locale
321 # Guess no on Solaris 8 and 9.
322 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
323 # Guess yes otherwise.
324 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
327 if test $LOCALE_ZH_CN != none; then
334 /* This fails on Solaris 8 and 9. */
335 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
340 memset (&state, '\0', sizeof (mbstate_t));
341 if (mbrtowc (&wc, "", 1, &state) != 0)
346 [gl_cv_func_mbrtowc_nul_retval=yes],
347 [gl_cv_func_mbrtowc_nul_retval=no],
353 # Prerequisites of lib/mbrtowc.c.
354 AC_DEFUN([gl_PREREQ_MBRTOWC], [
361 dnl This override of an autoconf macro can be removed when autoconf 2.60 or
362 dnl newer can be assumed everywhere.
364 m4_if(m4_version_compare(m4_defn([m4_PACKAGE_VERSION]),[2.60]),[-1],[
365 AC_DEFUN([AC_FUNC_MBRTOWC],
367 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
368 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
372 [[#include <wchar.h>]],
377 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
378 gl_cv_func_mbrtowc=yes,
379 gl_cv_func_mbrtowc=no)])
380 if test $gl_cv_func_mbrtowc = yes; then
381 AC_DEFINE([HAVE_MBRTOWC], [1],
382 [Define to 1 if mbrtowc and mbstate_t are properly declared.])