2 dnl Copyright (C) 2001-2002, 2004-2005, 2008-2011 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
26 case "$gl_cv_func_mbrtowc_null_arg1" in
28 *) AC_DEFINE([MBRTOWC_NULL_ARG1_BUG], [1],
29 [Define if the mbrtowc function has the NULL pwc argument bug.])
33 case "$gl_cv_func_mbrtowc_null_arg2" in
35 *) AC_DEFINE([MBRTOWC_NULL_ARG2_BUG], [1],
36 [Define if the mbrtowc function has the NULL string argument bug.])
40 case "$gl_cv_func_mbrtowc_retval" in
42 *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
43 [Define if the mbrtowc function returns a wrong return value.])
47 case "$gl_cv_func_mbrtowc_nul_retval" in
49 *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
50 [Define if the mbrtowc function does not return 0 for a NUL character.])
56 if test $HAVE_MBRTOWC = 0 || test $REPLACE_MBRTOWC = 1; then
62 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
63 dnl redefines the semantics of the given mbstate_t type.
64 dnl Result is REPLACE_MBSTATE_T.
65 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
66 dnl avoid inconsistencies.
68 AC_DEFUN([gl_MBSTATE_T_BROKEN],
70 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
72 AC_REQUIRE([AC_TYPE_MBSTATE_T])
73 AC_CHECK_FUNCS_ONCE([mbsinit])
74 AC_CHECK_FUNCS_ONCE([mbrtowc])
75 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
76 gl_MBRTOWC_INCOMPLETE_STATE
77 gl_MBRTOWC_SANITYCHECK
79 case "$gl_cv_func_mbrtowc_incomplete_state" in
81 *) REPLACE_MBSTATE_T=1 ;;
83 case "$gl_cv_func_mbrtowc_sanitycheck" in
85 *) REPLACE_MBSTATE_T=1 ;;
92 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
93 dnl incomplete multibyte character.
94 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
96 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
98 AC_REQUIRE([AC_PROG_CC])
99 AC_REQUIRE([gt_LOCALE_JA])
100 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
101 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
102 [gl_cv_func_mbrtowc_incomplete_state],
104 dnl Initial guess, used when cross-compiling or when no suitable locale
108 # Guess no on AIX and OSF/1.
109 aix* | osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
110 # Guess yes otherwise.
111 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
114 if test $LOCALE_JA != none; then
119 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
121 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
122 included before <wchar.h>. */
129 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
131 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
135 memset (&state, '\0', sizeof (mbstate_t));
136 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
137 if (mbsinit (&state))
142 [gl_cv_func_mbrtowc_incomplete_state=yes],
143 [gl_cv_func_mbrtowc_incomplete_state=no],
149 dnl Test whether mbrtowc works not worse than mbtowc.
150 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
152 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
154 AC_REQUIRE([AC_PROG_CC])
155 AC_REQUIRE([gt_LOCALE_ZH_CN])
156 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
157 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
158 [gl_cv_func_mbrtowc_sanitycheck],
160 dnl Initial guess, used when cross-compiling or when no suitable locale
164 # Guess no on Solaris 8.
165 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
166 # Guess yes otherwise.
167 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
170 if 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>. */
186 /* This fails on Solaris 8:
187 mbrtowc returns 2, and sets wc to 0x00F0.
188 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
189 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
191 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
195 memset (&state, '\0', sizeof (mbstate_t));
196 if (mbrtowc (&wc, input + 3, 6, &state) != 4
197 && mbtowc (&wc, input + 3, 6) == 4)
202 [gl_cv_func_mbrtowc_sanitycheck=yes],
203 [gl_cv_func_mbrtowc_sanitycheck=no],
209 dnl Test whether mbrtowc supports a NULL pwc argument correctly.
210 dnl Result is gl_cv_func_mbrtowc_null_arg1.
212 AC_DEFUN([gl_MBRTOWC_NULL_ARG1],
214 AC_REQUIRE([AC_PROG_CC])
215 AC_REQUIRE([gt_LOCALE_FR_UTF8])
216 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
217 AC_CACHE_CHECK([whether mbrtowc handles a NULL pwc argument],
218 [gl_cv_func_mbrtowc_null_arg1],
220 dnl Initial guess, used when cross-compiling or when no suitable locale
224 # Guess no on Solaris.
225 solaris*) gl_cv_func_mbrtowc_null_arg1="guessing no" ;;
226 # Guess yes otherwise.
227 *) gl_cv_func_mbrtowc_null_arg1="guessing yes" ;;
230 if test $LOCALE_FR_UTF8 != none; then
236 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
238 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
239 included before <wchar.h>. */
248 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
250 char input[] = "\303\237er";
255 memset (&state, '\0', sizeof (mbstate_t));
256 wc = (wchar_t) 0xBADFACE;
257 ret = mbrtowc (&wc, input, 5, &state);
260 if (!mbsinit (&state))
263 memset (&state, '\0', sizeof (mbstate_t));
264 ret = mbrtowc (NULL, input, 5, &state);
265 if (ret != 2) /* Solaris 7 fails here: ret is -1. */
267 if (!mbsinit (&state))
272 [gl_cv_func_mbrtowc_null_arg1=yes],
273 [gl_cv_func_mbrtowc_null_arg1=no],
279 dnl Test whether mbrtowc supports a NULL string argument correctly.
280 dnl Result is gl_cv_func_mbrtowc_null_arg2.
282 AC_DEFUN([gl_MBRTOWC_NULL_ARG2],
284 AC_REQUIRE([AC_PROG_CC])
285 AC_REQUIRE([gt_LOCALE_FR_UTF8])
286 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
287 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
288 [gl_cv_func_mbrtowc_null_arg2],
290 dnl Initial guess, used when cross-compiling or when no suitable locale
295 osf*) gl_cv_func_mbrtowc_null_arg2="guessing no" ;;
296 # Guess yes otherwise.
297 *) gl_cv_func_mbrtowc_null_arg2="guessing yes" ;;
300 if test $LOCALE_FR_UTF8 != none; then
305 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
307 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
308 included before <wchar.h>. */
315 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
321 memset (&state, '\0', sizeof (mbstate_t));
322 wc = (wchar_t) 0xBADFACE;
323 mbrtowc (&wc, NULL, 5, &state);
324 /* Check that wc was not modified. */
325 if (wc != (wchar_t) 0xBADFACE)
330 [gl_cv_func_mbrtowc_null_arg2=yes],
331 [gl_cv_func_mbrtowc_null_arg2=no],
337 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
338 dnl correctly returns the number of bytes that were needed to complete the
339 dnl character (not the total number of bytes of the multibyte character).
340 dnl Result is gl_cv_func_mbrtowc_retval.
342 AC_DEFUN([gl_MBRTOWC_RETVAL],
344 AC_REQUIRE([AC_PROG_CC])
345 AC_REQUIRE([gt_LOCALE_FR_UTF8])
346 AC_REQUIRE([gt_LOCALE_JA])
347 AC_REQUIRE([AC_CANONICAL_HOST])
348 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
349 [gl_cv_func_mbrtowc_retval],
351 dnl Initial guess, used when cross-compiling or when no suitable locale
355 # Guess no on HP-UX, Solaris, native Windows.
356 hpux* | solaris* | mingw*) gl_cv_func_mbrtowc_retval="guessing no" ;;
357 # Guess yes otherwise.
358 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
361 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none \
362 || { case "$host_os" in mingw*) true;; *) false;; esac; }; then
367 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
369 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
370 included before <wchar.h>. */
378 int found_some_locale = 0;
379 /* This fails on Solaris. */
380 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
382 char input[] = "B\303\274\303\237er"; /* "Büßer" */
386 memset (&state, '\0', sizeof (mbstate_t));
387 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
390 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
393 found_some_locale = 1;
395 /* This fails on HP-UX 11.11. */
396 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
398 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
402 memset (&state, '\0', sizeof (mbstate_t));
403 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
406 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
409 found_some_locale = 1;
411 /* This fails on native Windows. */
412 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
414 char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
418 memset (&state, '\0', sizeof (mbstate_t));
419 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
422 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
425 found_some_locale = 1;
427 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
429 char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
433 memset (&state, '\0', sizeof (mbstate_t));
434 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
437 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
440 found_some_locale = 1;
442 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
444 char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
448 memset (&state, '\0', sizeof (mbstate_t));
449 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
452 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
455 found_some_locale = 1;
457 return (found_some_locale ? result : 77);
459 [gl_cv_func_mbrtowc_retval=yes],
460 [if test $? != 77; then
461 gl_cv_func_mbrtowc_retval=no
469 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
470 dnl Result is gl_cv_func_mbrtowc_nul_retval.
472 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
474 AC_REQUIRE([AC_PROG_CC])
475 AC_REQUIRE([gt_LOCALE_ZH_CN])
476 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
477 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
478 [gl_cv_func_mbrtowc_nul_retval],
480 dnl Initial guess, used when cross-compiling or when no suitable locale
484 # Guess no on Solaris 8 and 9.
485 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
486 # Guess yes otherwise.
487 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
490 if test $LOCALE_ZH_CN != none; then
495 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
497 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
498 included before <wchar.h>. */
505 /* This fails on Solaris 8 and 9. */
506 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
511 memset (&state, '\0', sizeof (mbstate_t));
512 if (mbrtowc (&wc, "", 1, &state) != 0)
517 [gl_cv_func_mbrtowc_nul_retval=yes],
518 [gl_cv_func_mbrtowc_nul_retval=no],
524 # Prerequisites of lib/mbrtowc.c.
525 AC_DEFUN([gl_PREREQ_MBRTOWC], [
532 dnl This is an override of an autoconf macro.
534 AC_DEFUN([AC_FUNC_MBRTOWC],
536 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
537 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
541 [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
542 included before <wchar.h>.
543 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
544 must be included before <wchar.h>. */
548 #include <wchar.h>]],
553 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
554 gl_cv_func_mbrtowc=yes,
555 gl_cv_func_mbrtowc=no)])
556 if test $gl_cv_func_mbrtowc = yes; then
557 AC_DEFINE([HAVE_MBRTOWC], [1],
558 [Define to 1 if mbrtowc and mbstate_t are properly declared.])