2 dnl Copyright (C) 2001-2002, 2004-2005, 2008-2013 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
18 AC_CHECK_DECLS([mbrtowc],,, [[
19 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
21 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
22 included before <wchar.h>. */
28 if test $ac_cv_have_decl_mbrtowc = yes; then
29 dnl On Minix 3.1.8, the system's <wchar.h> declares mbrtowc() although
30 dnl it does not have the function. Avoid a collision with gnulib's
35 if test $REPLACE_MBSTATE_T = 1; then
42 case "$gl_cv_func_mbrtowc_null_arg1" in
44 *) AC_DEFINE([MBRTOWC_NULL_ARG1_BUG], [1],
45 [Define if the mbrtowc function has the NULL pwc argument bug.])
49 case "$gl_cv_func_mbrtowc_null_arg2" in
51 *) AC_DEFINE([MBRTOWC_NULL_ARG2_BUG], [1],
52 [Define if the mbrtowc function has the NULL string argument bug.])
56 case "$gl_cv_func_mbrtowc_retval" in
58 *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
59 [Define if the mbrtowc function returns a wrong return value.])
63 case "$gl_cv_func_mbrtowc_nul_retval" in
65 *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
66 [Define if the mbrtowc function does not return 0 for a NUL character.])
74 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
75 dnl redefines the semantics of the given mbstate_t type.
76 dnl Result is REPLACE_MBSTATE_T.
77 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
78 dnl avoid inconsistencies.
80 AC_DEFUN([gl_MBSTATE_T_BROKEN],
82 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
84 AC_REQUIRE([AC_TYPE_MBSTATE_T])
85 AC_CHECK_FUNCS_ONCE([mbsinit])
86 AC_CHECK_FUNCS_ONCE([mbrtowc])
87 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
88 gl_MBRTOWC_INCOMPLETE_STATE
89 gl_MBRTOWC_SANITYCHECK
91 case "$gl_cv_func_mbrtowc_incomplete_state" in
93 *) REPLACE_MBSTATE_T=1 ;;
95 case "$gl_cv_func_mbrtowc_sanitycheck" in
97 *) REPLACE_MBSTATE_T=1 ;;
104 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
105 dnl incomplete multibyte character.
106 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
108 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
110 AC_REQUIRE([AC_PROG_CC])
111 AC_REQUIRE([gt_LOCALE_JA])
112 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
113 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
114 [gl_cv_func_mbrtowc_incomplete_state],
116 dnl Initial guess, used when cross-compiling or when no suitable locale
120 # Guess no on AIX and OSF/1.
121 aix* | osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
122 # Guess yes otherwise.
123 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
126 if test $LOCALE_JA != none; then
131 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
133 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
134 included before <wchar.h>. */
141 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
143 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
147 memset (&state, '\0', sizeof (mbstate_t));
148 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
149 if (mbsinit (&state))
154 [gl_cv_func_mbrtowc_incomplete_state=yes],
155 [gl_cv_func_mbrtowc_incomplete_state=no],
161 dnl Test whether mbrtowc works not worse than mbtowc.
162 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
164 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
166 AC_REQUIRE([AC_PROG_CC])
167 AC_REQUIRE([gt_LOCALE_ZH_CN])
168 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
169 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
170 [gl_cv_func_mbrtowc_sanitycheck],
172 dnl Initial guess, used when cross-compiling or when no suitable locale
176 # Guess no on Solaris 8.
177 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
178 # Guess yes otherwise.
179 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
182 if test $LOCALE_ZH_CN != none; then
188 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
190 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
191 included before <wchar.h>. */
198 /* This fails on Solaris 8:
199 mbrtowc returns 2, and sets wc to 0x00F0.
200 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
201 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
203 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
207 memset (&state, '\0', sizeof (mbstate_t));
208 if (mbrtowc (&wc, input + 3, 6, &state) != 4
209 && mbtowc (&wc, input + 3, 6) == 4)
214 [gl_cv_func_mbrtowc_sanitycheck=yes],
215 [gl_cv_func_mbrtowc_sanitycheck=no],
221 dnl Test whether mbrtowc supports a NULL pwc argument correctly.
222 dnl Result is gl_cv_func_mbrtowc_null_arg1.
224 AC_DEFUN([gl_MBRTOWC_NULL_ARG1],
226 AC_REQUIRE([AC_PROG_CC])
227 AC_REQUIRE([gt_LOCALE_FR_UTF8])
228 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
229 AC_CACHE_CHECK([whether mbrtowc handles a NULL pwc argument],
230 [gl_cv_func_mbrtowc_null_arg1],
232 dnl Initial guess, used when cross-compiling or when no suitable locale
236 # Guess no on Solaris.
237 solaris*) gl_cv_func_mbrtowc_null_arg1="guessing no" ;;
238 # Guess yes otherwise.
239 *) gl_cv_func_mbrtowc_null_arg1="guessing yes" ;;
242 if test $LOCALE_FR_UTF8 != none; then
248 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
250 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
251 included before <wchar.h>. */
260 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
262 char input[] = "\303\237er";
267 memset (&state, '\0', sizeof (mbstate_t));
268 wc = (wchar_t) 0xBADFACE;
269 ret = mbrtowc (&wc, input, 5, &state);
272 if (!mbsinit (&state))
275 memset (&state, '\0', sizeof (mbstate_t));
276 ret = mbrtowc (NULL, input, 5, &state);
277 if (ret != 2) /* Solaris 7 fails here: ret is -1. */
279 if (!mbsinit (&state))
284 [gl_cv_func_mbrtowc_null_arg1=yes],
285 [gl_cv_func_mbrtowc_null_arg1=no],
291 dnl Test whether mbrtowc supports a NULL string argument correctly.
292 dnl Result is gl_cv_func_mbrtowc_null_arg2.
294 AC_DEFUN([gl_MBRTOWC_NULL_ARG2],
296 AC_REQUIRE([AC_PROG_CC])
297 AC_REQUIRE([gt_LOCALE_FR_UTF8])
298 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
299 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
300 [gl_cv_func_mbrtowc_null_arg2],
302 dnl Initial guess, used when cross-compiling or when no suitable locale
307 osf*) gl_cv_func_mbrtowc_null_arg2="guessing no" ;;
308 # Guess yes otherwise.
309 *) gl_cv_func_mbrtowc_null_arg2="guessing yes" ;;
312 if test $LOCALE_FR_UTF8 != none; then
317 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
319 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
320 included before <wchar.h>. */
327 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
333 memset (&state, '\0', sizeof (mbstate_t));
334 wc = (wchar_t) 0xBADFACE;
335 mbrtowc (&wc, NULL, 5, &state);
336 /* Check that wc was not modified. */
337 if (wc != (wchar_t) 0xBADFACE)
342 [gl_cv_func_mbrtowc_null_arg2=yes],
343 [gl_cv_func_mbrtowc_null_arg2=no],
349 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
350 dnl correctly returns the number of bytes that were needed to complete the
351 dnl character (not the total number of bytes of the multibyte character).
352 dnl Result is gl_cv_func_mbrtowc_retval.
354 AC_DEFUN([gl_MBRTOWC_RETVAL],
356 AC_REQUIRE([AC_PROG_CC])
357 AC_REQUIRE([gt_LOCALE_FR_UTF8])
358 AC_REQUIRE([gt_LOCALE_JA])
359 AC_REQUIRE([AC_CANONICAL_HOST])
360 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
361 [gl_cv_func_mbrtowc_retval],
363 dnl Initial guess, used when cross-compiling or when no suitable locale
367 # Guess no on HP-UX, Solaris, native Windows.
368 hpux* | solaris* | mingw*) gl_cv_func_mbrtowc_retval="guessing no" ;;
369 # Guess yes otherwise.
370 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
373 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none \
374 || { case "$host_os" in mingw*) true;; *) false;; esac; }; then
379 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
381 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
382 included before <wchar.h>. */
390 int found_some_locale = 0;
391 /* This fails on Solaris. */
392 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
394 char input[] = "B\303\274\303\237er"; /* "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) != 1)
405 found_some_locale = 1;
407 /* This fails on HP-UX 11.11. */
408 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
410 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
414 memset (&state, '\0', sizeof (mbstate_t));
415 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
418 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
421 found_some_locale = 1;
423 /* This fails on native Windows. */
424 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
426 char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
430 memset (&state, '\0', sizeof (mbstate_t));
431 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
434 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
437 found_some_locale = 1;
439 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
441 char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
445 memset (&state, '\0', sizeof (mbstate_t));
446 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
449 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
452 found_some_locale = 1;
454 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
456 char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
460 memset (&state, '\0', sizeof (mbstate_t));
461 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
464 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
467 found_some_locale = 1;
469 return (found_some_locale ? result : 77);
471 [gl_cv_func_mbrtowc_retval=yes],
472 [if test $? != 77; then
473 gl_cv_func_mbrtowc_retval=no
481 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
482 dnl Result is gl_cv_func_mbrtowc_nul_retval.
484 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
486 AC_REQUIRE([AC_PROG_CC])
487 AC_REQUIRE([gt_LOCALE_ZH_CN])
488 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
489 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
490 [gl_cv_func_mbrtowc_nul_retval],
492 dnl Initial guess, used when cross-compiling or when no suitable locale
496 # Guess no on Solaris 8 and 9.
497 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
498 # Guess yes otherwise.
499 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
502 if test $LOCALE_ZH_CN != none; then
507 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
509 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
510 included before <wchar.h>. */
517 /* This fails on Solaris 8 and 9. */
518 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
523 memset (&state, '\0', sizeof (mbstate_t));
524 if (mbrtowc (&wc, "", 1, &state) != 0)
529 [gl_cv_func_mbrtowc_nul_retval=yes],
530 [gl_cv_func_mbrtowc_nul_retval=no],
536 # Prerequisites of lib/mbrtowc.c.
537 AC_DEFUN([gl_PREREQ_MBRTOWC], [
544 dnl This is an override of an autoconf macro.
546 AC_DEFUN([AC_FUNC_MBRTOWC],
548 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
549 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
553 [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
554 included before <wchar.h>.
555 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
556 must be included before <wchar.h>. */
560 #include <wchar.h>]],
565 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
566 gl_cv_func_mbrtowc=yes,
567 gl_cv_func_mbrtowc=no)])
568 if test $gl_cv_func_mbrtowc = yes; then
569 AC_DEFINE([HAVE_MBRTOWC], [1],
570 [Define to 1 if mbrtowc and mbstate_t are properly declared.])