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.])
58 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
59 dnl redefines the semantics of the given mbstate_t type.
60 dnl Result is REPLACE_MBSTATE_T.
61 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
62 dnl avoid inconsistencies.
64 AC_DEFUN([gl_MBSTATE_T_BROKEN],
66 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
68 AC_REQUIRE([AC_TYPE_MBSTATE_T])
69 AC_CHECK_FUNCS_ONCE([mbsinit])
70 AC_CHECK_FUNCS_ONCE([mbrtowc])
71 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
72 gl_MBRTOWC_INCOMPLETE_STATE
73 gl_MBRTOWC_SANITYCHECK
75 case "$gl_cv_func_mbrtowc_incomplete_state" in
77 *) REPLACE_MBSTATE_T=1 ;;
79 case "$gl_cv_func_mbrtowc_sanitycheck" in
81 *) REPLACE_MBSTATE_T=1 ;;
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
115 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
117 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
118 included before <wchar.h>. */
125 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
127 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
131 memset (&state, '\0', sizeof (mbstate_t));
132 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
133 if (mbsinit (&state))
138 [gl_cv_func_mbrtowc_incomplete_state=yes],
139 [gl_cv_func_mbrtowc_incomplete_state=no],
145 dnl Test whether mbrtowc works not worse than mbtowc.
146 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
148 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
150 AC_REQUIRE([AC_PROG_CC])
151 AC_REQUIRE([gt_LOCALE_ZH_CN])
152 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
153 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
154 [gl_cv_func_mbrtowc_sanitycheck],
156 dnl Initial guess, used when cross-compiling or when no suitable locale
160 # Guess no on Solaris 8.
161 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
162 # Guess yes otherwise.
163 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
166 if test $LOCALE_ZH_CN != none; then
172 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
174 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
175 included before <wchar.h>. */
182 /* This fails on Solaris 8:
183 mbrtowc returns 2, and sets wc to 0x00F0.
184 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
185 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
187 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
191 memset (&state, '\0', sizeof (mbstate_t));
192 if (mbrtowc (&wc, input + 3, 6, &state) != 4
193 && mbtowc (&wc, input + 3, 6) == 4)
198 [gl_cv_func_mbrtowc_sanitycheck=yes],
199 [gl_cv_func_mbrtowc_sanitycheck=no],
205 dnl Test whether mbrtowc supports a NULL pwc argument correctly.
206 dnl Result is gl_cv_func_mbrtowc_null_arg1.
208 AC_DEFUN([gl_MBRTOWC_NULL_ARG1],
210 AC_REQUIRE([AC_PROG_CC])
211 AC_REQUIRE([gt_LOCALE_FR_UTF8])
212 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
213 AC_CACHE_CHECK([whether mbrtowc handles a NULL pwc argument],
214 [gl_cv_func_mbrtowc_null_arg1],
216 dnl Initial guess, used when cross-compiling or when no suitable locale
220 # Guess no on Solaris.
221 solaris*) gl_cv_func_mbrtowc_null_arg1="guessing no" ;;
222 # Guess yes otherwise.
223 *) gl_cv_func_mbrtowc_null_arg1="guessing yes" ;;
226 if test $LOCALE_FR_UTF8 != none; then
232 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
234 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
235 included before <wchar.h>. */
244 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
246 char input[] = "\303\237er";
251 memset (&state, '\0', sizeof (mbstate_t));
252 wc = (wchar_t) 0xBADFACE;
253 ret = mbrtowc (&wc, input, 5, &state);
256 if (!mbsinit (&state))
259 memset (&state, '\0', sizeof (mbstate_t));
260 ret = mbrtowc (NULL, input, 5, &state);
261 if (ret != 2) /* Solaris 7 fails here: ret is -1. */
263 if (!mbsinit (&state))
268 [gl_cv_func_mbrtowc_null_arg1=yes],
269 [gl_cv_func_mbrtowc_null_arg1=no],
275 dnl Test whether mbrtowc supports a NULL string argument correctly.
276 dnl Result is gl_cv_func_mbrtowc_null_arg2.
278 AC_DEFUN([gl_MBRTOWC_NULL_ARG2],
280 AC_REQUIRE([AC_PROG_CC])
281 AC_REQUIRE([gt_LOCALE_FR_UTF8])
282 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
283 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
284 [gl_cv_func_mbrtowc_null_arg2],
286 dnl Initial guess, used when cross-compiling or when no suitable locale
291 osf*) gl_cv_func_mbrtowc_null_arg2="guessing no" ;;
292 # Guess yes otherwise.
293 *) gl_cv_func_mbrtowc_null_arg2="guessing yes" ;;
296 if test $LOCALE_FR_UTF8 != none; then
301 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
303 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
304 included before <wchar.h>. */
311 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
317 memset (&state, '\0', sizeof (mbstate_t));
318 wc = (wchar_t) 0xBADFACE;
319 mbrtowc (&wc, NULL, 5, &state);
320 /* Check that wc was not modified. */
321 if (wc != (wchar_t) 0xBADFACE)
326 [gl_cv_func_mbrtowc_null_arg2=yes],
327 [gl_cv_func_mbrtowc_null_arg2=no],
333 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
334 dnl correctly returns the number of bytes that were needed to complete the
335 dnl character (not the total number of bytes of the multibyte character).
336 dnl Result is gl_cv_func_mbrtowc_retval.
338 AC_DEFUN([gl_MBRTOWC_RETVAL],
340 AC_REQUIRE([AC_PROG_CC])
341 AC_REQUIRE([gt_LOCALE_FR_UTF8])
342 AC_REQUIRE([gt_LOCALE_JA])
343 AC_REQUIRE([AC_CANONICAL_HOST])
344 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
345 [gl_cv_func_mbrtowc_retval],
347 dnl Initial guess, used when cross-compiling or when no suitable locale
351 # Guess no on HP-UX, Solaris, native Windows.
352 hpux* | solaris* | mingw*) gl_cv_func_mbrtowc_retval="guessing no" ;;
353 # Guess yes otherwise.
354 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
357 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none \
358 || { case "$host_os" in mingw*) true;; *) false;; esac; }; then
363 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
365 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
366 included before <wchar.h>. */
374 int found_some_locale = 0;
375 /* This fails on Solaris. */
376 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
378 char input[] = "B\303\274\303\237er"; /* "Büßer" */
382 memset (&state, '\0', sizeof (mbstate_t));
383 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
386 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
389 found_some_locale = 1;
391 /* This fails on HP-UX 11.11. */
392 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
394 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
398 memset (&state, '\0', sizeof (mbstate_t));
399 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
402 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
405 found_some_locale = 1;
407 /* This fails on native Windows. */
408 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
410 char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
414 memset (&state, '\0', sizeof (mbstate_t));
415 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
418 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
421 found_some_locale = 1;
423 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
425 char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
429 memset (&state, '\0', sizeof (mbstate_t));
430 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
433 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
436 found_some_locale = 1;
438 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
440 char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
444 memset (&state, '\0', sizeof (mbstate_t));
445 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
448 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
451 found_some_locale = 1;
453 return (found_some_locale ? result : 77);
455 [gl_cv_func_mbrtowc_retval=yes],
456 [if test $? != 77; then
457 gl_cv_func_mbrtowc_retval=no
465 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
466 dnl Result is gl_cv_func_mbrtowc_nul_retval.
468 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
470 AC_REQUIRE([AC_PROG_CC])
471 AC_REQUIRE([gt_LOCALE_ZH_CN])
472 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
473 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
474 [gl_cv_func_mbrtowc_nul_retval],
476 dnl Initial guess, used when cross-compiling or when no suitable locale
480 # Guess no on Solaris 8 and 9.
481 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
482 # Guess yes otherwise.
483 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
486 if test $LOCALE_ZH_CN != none; then
491 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
493 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
494 included before <wchar.h>. */
501 /* This fails on Solaris 8 and 9. */
502 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
507 memset (&state, '\0', sizeof (mbstate_t));
508 if (mbrtowc (&wc, "", 1, &state) != 0)
513 [gl_cv_func_mbrtowc_nul_retval=yes],
514 [gl_cv_func_mbrtowc_nul_retval=no],
520 # Prerequisites of lib/mbrtowc.c.
521 AC_DEFUN([gl_PREREQ_MBRTOWC], [
528 dnl This is an override of an autoconf macro.
530 AC_DEFUN([AC_FUNC_MBRTOWC],
532 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
533 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
537 [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
538 included before <wchar.h>.
539 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
540 must be included before <wchar.h>. */
544 #include <wchar.h>]],
549 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
550 gl_cv_func_mbrtowc=yes,
551 gl_cv_func_mbrtowc=no)])
552 if test $gl_cv_func_mbrtowc = yes; then
553 AC_DEFINE([HAVE_MBRTOWC], [1],
554 [Define to 1 if mbrtowc and mbstate_t are properly declared.])