2 dnl Copyright (C) 2001-2002, 2004-2005, 2008-2014 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 gl_MBRTOWC_EMPTY_INPUT
43 case "$gl_cv_func_mbrtowc_null_arg1" in
45 *) AC_DEFINE([MBRTOWC_NULL_ARG1_BUG], [1],
46 [Define if the mbrtowc function has the NULL pwc argument bug.])
50 case "$gl_cv_func_mbrtowc_null_arg2" in
52 *) AC_DEFINE([MBRTOWC_NULL_ARG2_BUG], [1],
53 [Define if the mbrtowc function has the NULL string argument bug.])
57 case "$gl_cv_func_mbrtowc_retval" in
59 *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
60 [Define if the mbrtowc function returns a wrong return value.])
64 case "$gl_cv_func_mbrtowc_nul_retval" in
66 *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
67 [Define if the mbrtowc function does not return 0 for a NUL character.])
71 case "$gl_cv_func_mbrtowc_empty_input" in
73 *) AC_DEFINE([MBRTOWC_EMPTY_INPUT_BUG], [1],
74 [Define if the mbrtowc function does not return (size_t) -2
83 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
84 dnl redefines the semantics of the given mbstate_t type.
85 dnl Result is REPLACE_MBSTATE_T.
86 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
87 dnl avoid inconsistencies.
89 AC_DEFUN([gl_MBSTATE_T_BROKEN],
91 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
93 AC_REQUIRE([AC_TYPE_MBSTATE_T])
94 AC_CHECK_FUNCS_ONCE([mbsinit])
95 AC_CHECK_FUNCS_ONCE([mbrtowc])
96 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
97 gl_MBRTOWC_INCOMPLETE_STATE
98 gl_MBRTOWC_SANITYCHECK
100 case "$gl_cv_func_mbrtowc_incomplete_state" in
102 *) REPLACE_MBSTATE_T=1 ;;
104 case "$gl_cv_func_mbrtowc_sanitycheck" in
106 *) REPLACE_MBSTATE_T=1 ;;
113 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
114 dnl incomplete multibyte character.
115 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
117 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
119 AC_REQUIRE([AC_PROG_CC])
120 AC_REQUIRE([gt_LOCALE_JA])
121 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
122 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
123 [gl_cv_func_mbrtowc_incomplete_state],
125 dnl Initial guess, used when cross-compiling or when no suitable locale
129 # Guess no on AIX and OSF/1.
130 aix* | osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
131 # Guess yes otherwise.
132 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
135 if test $LOCALE_JA != none; then
140 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
142 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
143 included before <wchar.h>. */
150 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
152 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
156 memset (&state, '\0', sizeof (mbstate_t));
157 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
158 if (mbsinit (&state))
163 [gl_cv_func_mbrtowc_incomplete_state=yes],
164 [gl_cv_func_mbrtowc_incomplete_state=no],
170 dnl Test whether mbrtowc works not worse than mbtowc.
171 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
173 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
175 AC_REQUIRE([AC_PROG_CC])
176 AC_REQUIRE([gt_LOCALE_ZH_CN])
177 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
178 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
179 [gl_cv_func_mbrtowc_sanitycheck],
181 dnl Initial guess, used when cross-compiling or when no suitable locale
185 # Guess no on Solaris 8.
186 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
187 # Guess yes otherwise.
188 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
191 if test $LOCALE_ZH_CN != none; then
197 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
199 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
200 included before <wchar.h>. */
207 /* This fails on Solaris 8:
208 mbrtowc returns 2, and sets wc to 0x00F0.
209 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
210 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
212 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
216 memset (&state, '\0', sizeof (mbstate_t));
217 if (mbrtowc (&wc, input + 3, 6, &state) != 4
218 && mbtowc (&wc, input + 3, 6) == 4)
223 [gl_cv_func_mbrtowc_sanitycheck=yes],
224 [gl_cv_func_mbrtowc_sanitycheck=no],
230 dnl Test whether mbrtowc supports a NULL pwc argument correctly.
231 dnl Result is gl_cv_func_mbrtowc_null_arg1.
233 AC_DEFUN([gl_MBRTOWC_NULL_ARG1],
235 AC_REQUIRE([AC_PROG_CC])
236 AC_REQUIRE([gt_LOCALE_FR_UTF8])
237 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
238 AC_CACHE_CHECK([whether mbrtowc handles a NULL pwc argument],
239 [gl_cv_func_mbrtowc_null_arg1],
241 dnl Initial guess, used when cross-compiling or when no suitable locale
245 # Guess no on Solaris.
246 solaris*) gl_cv_func_mbrtowc_null_arg1="guessing no" ;;
247 # Guess yes otherwise.
248 *) gl_cv_func_mbrtowc_null_arg1="guessing yes" ;;
251 if test $LOCALE_FR_UTF8 != none; then
257 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
259 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
260 included before <wchar.h>. */
269 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
271 char input[] = "\303\237er";
276 memset (&state, '\0', sizeof (mbstate_t));
277 wc = (wchar_t) 0xBADFACE;
278 ret = mbrtowc (&wc, input, 5, &state);
281 if (!mbsinit (&state))
284 memset (&state, '\0', sizeof (mbstate_t));
285 ret = mbrtowc (NULL, input, 5, &state);
286 if (ret != 2) /* Solaris 7 fails here: ret is -1. */
288 if (!mbsinit (&state))
293 [gl_cv_func_mbrtowc_null_arg1=yes],
294 [gl_cv_func_mbrtowc_null_arg1=no],
300 dnl Test whether mbrtowc supports a NULL string argument correctly.
301 dnl Result is gl_cv_func_mbrtowc_null_arg2.
303 AC_DEFUN([gl_MBRTOWC_NULL_ARG2],
305 AC_REQUIRE([AC_PROG_CC])
306 AC_REQUIRE([gt_LOCALE_FR_UTF8])
307 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
308 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
309 [gl_cv_func_mbrtowc_null_arg2],
311 dnl Initial guess, used when cross-compiling or when no suitable locale
316 osf*) gl_cv_func_mbrtowc_null_arg2="guessing no" ;;
317 # Guess yes otherwise.
318 *) gl_cv_func_mbrtowc_null_arg2="guessing yes" ;;
321 if test $LOCALE_FR_UTF8 != none; then
326 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
328 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
329 included before <wchar.h>. */
336 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
342 memset (&state, '\0', sizeof (mbstate_t));
343 wc = (wchar_t) 0xBADFACE;
344 mbrtowc (&wc, NULL, 5, &state);
345 /* Check that wc was not modified. */
346 if (wc != (wchar_t) 0xBADFACE)
351 [gl_cv_func_mbrtowc_null_arg2=yes],
352 [gl_cv_func_mbrtowc_null_arg2=no],
358 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
359 dnl correctly returns the number of bytes that were needed to complete the
360 dnl character (not the total number of bytes of the multibyte character).
361 dnl Result is gl_cv_func_mbrtowc_retval.
363 AC_DEFUN([gl_MBRTOWC_RETVAL],
365 AC_REQUIRE([AC_PROG_CC])
366 AC_REQUIRE([gt_LOCALE_FR_UTF8])
367 AC_REQUIRE([gt_LOCALE_JA])
368 AC_REQUIRE([AC_CANONICAL_HOST])
369 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
370 [gl_cv_func_mbrtowc_retval],
372 dnl Initial guess, used when cross-compiling or when no suitable locale
376 # Guess no on HP-UX, Solaris, native Windows.
377 hpux* | solaris* | mingw*) gl_cv_func_mbrtowc_retval="guessing no" ;;
378 # Guess yes otherwise.
379 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
382 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none \
383 || { case "$host_os" in mingw*) true;; *) false;; esac; }; then
388 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
390 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
391 included before <wchar.h>. */
399 int found_some_locale = 0;
400 /* This fails on Solaris. */
401 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
403 char input[] = "B\303\274\303\237er"; /* "Büßer" */
407 memset (&state, '\0', sizeof (mbstate_t));
408 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
411 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
414 found_some_locale = 1;
416 /* This fails on HP-UX 11.11. */
417 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
419 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
423 memset (&state, '\0', sizeof (mbstate_t));
424 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
427 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
430 found_some_locale = 1;
432 /* This fails on native Windows. */
433 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
435 char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
439 memset (&state, '\0', sizeof (mbstate_t));
440 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
443 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
446 found_some_locale = 1;
448 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
450 char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
454 memset (&state, '\0', sizeof (mbstate_t));
455 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
458 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
461 found_some_locale = 1;
463 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
465 char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
469 memset (&state, '\0', sizeof (mbstate_t));
470 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
473 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
476 found_some_locale = 1;
478 return (found_some_locale ? result : 77);
480 [gl_cv_func_mbrtowc_retval=yes],
481 [if test $? != 77; then
482 gl_cv_func_mbrtowc_retval=no
490 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
491 dnl Result is gl_cv_func_mbrtowc_nul_retval.
493 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
495 AC_REQUIRE([AC_PROG_CC])
496 AC_REQUIRE([gt_LOCALE_ZH_CN])
497 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
498 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
499 [gl_cv_func_mbrtowc_nul_retval],
501 dnl Initial guess, used when cross-compiling or when no suitable locale
505 # Guess no on Solaris 8 and 9.
506 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
507 # Guess yes otherwise.
508 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
511 if test $LOCALE_ZH_CN != none; then
516 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
518 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
519 included before <wchar.h>. */
526 /* This fails on Solaris 8 and 9. */
527 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
532 memset (&state, '\0', sizeof (mbstate_t));
533 if (mbrtowc (&wc, "", 1, &state) != 0)
538 [gl_cv_func_mbrtowc_nul_retval=yes],
539 [gl_cv_func_mbrtowc_nul_retval=no],
545 dnl Test whether mbrtowc returns the correct value on empty input.
547 AC_DEFUN([gl_MBRTOWC_EMPTY_INPUT],
549 AC_REQUIRE([AC_PROG_CC])
550 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
551 AC_CACHE_CHECK([whether mbrtowc works on empty input],
552 [gl_cv_func_mbrtowc_empty_input],
554 dnl Initial guess, used when cross-compiling or when no suitable locale
558 # Guess no on AIX and glibc systems.
560 gl_cv_func_mbrtowc_empty_input="guessing no" ;;
561 *) gl_cv_func_mbrtowc_empty_input="guessing yes" ;;
568 static mbstate_t mbs;
572 return mbrtowc (&wc, "", 0, &mbs) == (size_t) -2;
574 [gl_cv_func_mbrtowc_empty_input=no],
575 [gl_cv_func_mbrtowc_empty_input=yes],
580 # Prerequisites of lib/mbrtowc.c.
581 AC_DEFUN([gl_PREREQ_MBRTOWC], [
588 dnl This is an override of an autoconf macro.
590 AC_DEFUN([AC_FUNC_MBRTOWC],
592 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
593 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
597 [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
598 included before <wchar.h>.
599 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
600 must be included before <wchar.h>. */
604 #include <wchar.h>]],
609 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
610 gl_cv_func_mbrtowc=yes,
611 gl_cv_func_mbrtowc=no)])
612 if test $gl_cv_func_mbrtowc = yes; then
613 AC_DEFINE([HAVE_MBRTOWC], [1],
614 [Define to 1 if mbrtowc and mbstate_t are properly declared.])