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 aix* | 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
118 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
120 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
124 memset (&state, '\0', sizeof (mbstate_t));
125 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
126 if (mbsinit (&state))
131 [gl_cv_func_mbrtowc_incomplete_state=yes],
132 [gl_cv_func_mbrtowc_incomplete_state=no],
138 dnl Test whether mbrtowc works not worse than mbtowc.
139 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
141 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
143 AC_REQUIRE([AC_PROG_CC])
144 AC_REQUIRE([gt_LOCALE_ZH_CN])
145 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
146 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
147 [gl_cv_func_mbrtowc_sanitycheck],
149 dnl Initial guess, used when cross-compiling or when no suitable locale
153 # Guess no on Solaris 8.
154 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
155 # Guess yes otherwise.
156 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
159 if test $LOCALE_ZH_CN != none; then
168 /* This fails on Solaris 8:
169 mbrtowc returns 2, and sets wc to 0x00F0.
170 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
171 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
173 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
177 memset (&state, '\0', sizeof (mbstate_t));
178 if (mbrtowc (&wc, input + 3, 6, &state) != 4
179 && mbtowc (&wc, input + 3, 6) == 4)
184 [gl_cv_func_mbrtowc_sanitycheck=yes],
185 [gl_cv_func_mbrtowc_sanitycheck=no],
191 dnl Test whether mbrtowc supports a NULL string argument correctly.
192 dnl Result is gl_cv_func_mbrtowc_null_arg.
194 AC_DEFUN([gl_MBRTOWC_NULL_ARG],
196 AC_REQUIRE([AC_PROG_CC])
197 AC_REQUIRE([gt_LOCALE_FR_UTF8])
198 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
199 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
200 [gl_cv_func_mbrtowc_null_arg],
202 dnl Initial guess, used when cross-compiling or when no suitable locale
207 osf*) gl_cv_func_mbrtowc_null_arg="guessing no" ;;
208 # Guess yes otherwise.
209 *) gl_cv_func_mbrtowc_null_arg="guessing yes" ;;
212 if test $LOCALE_FR_UTF8 != none; then
220 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
226 memset (&state, '\0', sizeof (mbstate_t));
227 wc = (wchar_t) 0xBADFACE;
228 mbrtowc (&wc, NULL, 5, &state);
229 /* Check that wc was not modified. */
230 if (wc != (wchar_t) 0xBADFACE)
235 [gl_cv_func_mbrtowc_null_arg=yes],
236 [gl_cv_func_mbrtowc_null_arg=no],
242 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
243 dnl correctly returns the number of bytes that were needed to complete the
244 dnl character (not the total number of bytes of the multibyte character).
245 dnl Result is gl_cv_func_mbrtowc_retval.
247 AC_DEFUN([gl_MBRTOWC_RETVAL],
249 AC_REQUIRE([AC_PROG_CC])
250 AC_REQUIRE([gt_LOCALE_FR_UTF8])
251 AC_REQUIRE([gt_LOCALE_JA])
252 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
253 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
254 [gl_cv_func_mbrtowc_retval],
256 dnl Initial guess, used when cross-compiling or when no suitable locale
260 # Guess no on HP-UX and Solaris.
261 hpux* | solaris*) gl_cv_func_mbrtowc_retval="guessing no" ;;
262 # Guess yes otherwise.
263 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
266 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none; then
275 /* This fails on Solaris. */
276 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
278 char input[] = "B\303\274\303\237er"; /* "Büßer" */
282 memset (&state, '\0', sizeof (mbstate_t));
283 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
286 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
290 /* This fails on HP-UX 11.11. */
291 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
293 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
297 memset (&state, '\0', sizeof (mbstate_t));
298 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
301 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
307 [gl_cv_func_mbrtowc_retval=yes],
308 [gl_cv_func_mbrtowc_retval=no],
314 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
315 dnl Result is gl_cv_func_mbrtowc_nul_retval.
317 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
319 AC_REQUIRE([AC_PROG_CC])
320 AC_REQUIRE([gt_LOCALE_ZH_CN])
321 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
322 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
323 [gl_cv_func_mbrtowc_nul_retval],
325 dnl Initial guess, used when cross-compiling or when no suitable locale
329 # Guess no on Solaris 8 and 9.
330 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
331 # Guess yes otherwise.
332 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
335 if test $LOCALE_ZH_CN != none; then
343 /* This fails on Solaris 8 and 9. */
344 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
349 memset (&state, '\0', sizeof (mbstate_t));
350 if (mbrtowc (&wc, "", 1, &state) != 0)
355 [gl_cv_func_mbrtowc_nul_retval=yes],
356 [gl_cv_func_mbrtowc_nul_retval=no],
362 # Prerequisites of lib/mbrtowc.c.
363 AC_DEFUN([gl_PREREQ_MBRTOWC], [
370 dnl This override of an autoconf macro can be removed when autoconf 2.60 or
371 dnl newer can be assumed everywhere.
373 m4_if(m4_version_compare(m4_defn([m4_PACKAGE_VERSION]),[2.60]),[-1],[
374 AC_DEFUN([AC_FUNC_MBRTOWC],
376 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
377 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
381 [[#include <wchar.h>]],
386 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
387 gl_cv_func_mbrtowc=yes,
388 gl_cv_func_mbrtowc=no)])
389 if test $gl_cv_func_mbrtowc = yes; then
390 AC_DEFINE([HAVE_MBRTOWC], [1],
391 [Define to 1 if mbrtowc and mbstate_t are properly declared.])