2 dnl Copyright (C) 2001-2002, 2004-2005, 2008 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_MBRTOWC],
9 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
11 AC_REQUIRE([AC_TYPE_MBSTATE_T])
13 if test $REPLACE_MBSTATE_T = 1; then
16 AC_CHECK_FUNCS_ONCE([mbrtowc])
17 if test $ac_cv_func_mbrtowc = no; then
20 if test $HAVE_MBRTOWC != 0 && test $REPLACE_MBRTOWC != 1; then
24 case "$gl_cv_func_mbrtowc_null_arg" in
26 *) AC_DEFINE([MBRTOWC_NULL_ARG_BUG], [1],
27 [Define if the mbrtowc function has the NULL string argument bug.])
31 case "$gl_cv_func_mbrtowc_retval" in
33 *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
34 [Define if the mbrtowc function returns a wrong return value.])
38 case "$gl_cv_func_mbrtowc_nul_retval" in
40 *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
41 [Define if the mbrtowc function does not return 0 for a NUL character.])
46 if test $HAVE_MBRTOWC = 0 || test $REPLACE_MBRTOWC = 1; then
53 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
54 dnl redefines the semantics of the given mbstate_t type.
55 dnl Result is REPLACE_MBSTATE_T.
56 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
57 dnl avoid inconsistencies.
59 AC_DEFUN([gl_MBSTATE_T_BROKEN],
61 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
63 AC_REQUIRE([AC_TYPE_MBSTATE_T])
64 AC_CHECK_FUNCS_ONCE([mbsinit])
65 AC_CHECK_FUNCS_ONCE([mbrtowc])
66 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
67 gl_MBRTOWC_INCOMPLETE_STATE
68 case "$gl_cv_func_mbrtowc_incomplete_state" in
69 *yes) REPLACE_MBSTATE_T=0 ;;
70 *) REPLACE_MBSTATE_T=1 ;;
75 if test $REPLACE_MBSTATE_T = 1; then
80 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
81 dnl incomplete multibyte character.
82 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
84 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
86 AC_REQUIRE([AC_PROG_CC])
87 AC_REQUIRE([gt_LOCALE_JA])
88 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
89 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
90 [gl_cv_func_mbrtowc_incomplete_state],
92 dnl Initial guess, used when cross-compiling or when no suitable locale
96 # Guess no on AIX and OSF/1.
97 osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
98 # Guess yes otherwise.
99 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
102 if test $LOCALE_JA != none; then
109 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
111 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
116 memset (&state, '\0', sizeof (mbstate_t));
117 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
118 if (mbsinit (&state))
123 [gl_cv_func_mbrtowc_incomplete_state=yes],
124 [gl_cv_func_mbrtowc_incomplete_state=no],
130 dnl Test whether mbrtowc supports a NULL string argument correctly.
131 dnl Result is gl_cv_func_mbrtowc_null_arg.
133 AC_DEFUN([gl_MBRTOWC_NULL_ARG],
135 AC_REQUIRE([AC_PROG_CC])
136 AC_REQUIRE([gt_LOCALE_FR_UTF8])
137 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
138 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
139 [gl_cv_func_mbrtowc_null_arg],
141 dnl Initial guess, used when cross-compiling or when no suitable locale
146 osf*) gl_cv_func_mbrtowc_null_arg="guessing no" ;;
147 # Guess yes otherwise.
148 *) gl_cv_func_mbrtowc_null_arg="guessing yes" ;;
151 if test $LOCALE_FR_UTF8 != none; then
158 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
164 memset (&state, '\0', sizeof (mbstate_t));
165 wc = (wchar_t) 0xBADFACE;
166 mbrtowc (&wc, NULL, 5, &state);
167 /* Check that wc was not modified. */
168 if (wc != (wchar_t) 0xBADFACE)
172 }], [gl_cv_func_mbrtowc_null_arg=yes], [gl_cv_func_mbrtowc_null_arg=no], [])
177 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
178 dnl correctly returns the number of bytes that were needed to complete the
179 dnl character (not the total number of bytes of the multibyte character).
180 dnl Result is gl_cv_func_mbrtowc_retval.
182 AC_DEFUN([gl_MBRTOWC_RETVAL],
184 AC_REQUIRE([AC_PROG_CC])
185 AC_REQUIRE([gt_LOCALE_FR_UTF8])
186 AC_REQUIRE([gt_LOCALE_JA])
187 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
188 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
189 [gl_cv_func_mbrtowc_retval],
191 dnl Initial guess, used when cross-compiling or when no suitable locale
195 # Guess no on HP-UX and Solaris.
196 hpux* | solaris*) gl_cv_func_mbrtowc_retval="guessing no" ;;
197 # Guess yes otherwise.
198 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
201 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none; then
208 /* This fails on Solaris. */
209 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
211 char input[] = "B\303\274\303\237er"; /* "Büßer" */
215 memset (&state, '\0', sizeof (mbstate_t));
216 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
219 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
223 /* This fails on HP-UX 11.11. */
224 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
226 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
230 memset (&state, '\0', sizeof (mbstate_t));
231 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
234 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
240 [gl_cv_func_mbrtowc_retval=yes],
241 [gl_cv_func_mbrtowc_retval=no],
247 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
248 dnl Result is gl_cv_func_mbrtowc_nul_retval.
250 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
252 AC_REQUIRE([AC_PROG_CC])
253 AC_REQUIRE([gt_LOCALE_ZH_CN])
254 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
255 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
256 [gl_cv_func_mbrtowc_nul_retval],
258 dnl Initial guess, used when cross-compiling or when no suitable locale
262 # Guess no on Solaris 9.
263 solaris2.9) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
264 # Guess yes otherwise.
265 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
268 if test $LOCALE_ZH_CN != none; then
275 /* This fails on Solaris 9. */
276 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
281 memset (&state, '\0', sizeof (mbstate_t));
282 if (mbrtowc (&wc, "", 1, &state) != 0)
287 [gl_cv_func_mbrtowc_nul_retval=yes],
288 [gl_cv_func_mbrtowc_nul_retval=no],
294 # Prerequisites of lib/mbrtowc.c.
295 AC_DEFUN([gl_PREREQ_MBRTOWC], [
302 dnl This override of an autoconf macro can be removed when autoconf 2.60 or
303 dnl newer can be assumed everywhere.
305 m4_if(m4_version_compare(m4_defn([m4_PACKAGE_VERSION]),[2.60]),[-1],[
306 AC_DEFUN([AC_FUNC_MBRTOWC],
308 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
309 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
313 [[#include <wchar.h>]],
318 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
319 gl_cv_func_mbrtowc=yes,
320 gl_cv_func_mbrtowc=no)])
321 if test $gl_cv_func_mbrtowc = yes; then
322 AC_DEFINE([HAVE_MBRTOWC], 1,
323 [Define to 1 if mbrtowc and mbstate_t are properly declared.])