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
63 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
64 dnl redefines the semantics of the given mbstate_t type.
65 dnl Result is REPLACE_MBSTATE_T.
66 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
67 dnl avoid inconsistencies.
69 AC_DEFUN([gl_MBSTATE_T_BROKEN],
71 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
73 AC_REQUIRE([AC_TYPE_MBSTATE_T])
74 AC_CHECK_FUNCS_ONCE([mbsinit])
75 AC_CHECK_FUNCS_ONCE([mbrtowc])
76 if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
77 gl_MBRTOWC_INCOMPLETE_STATE
78 gl_MBRTOWC_SANITYCHECK
80 case "$gl_cv_func_mbrtowc_incomplete_state" in
82 *) REPLACE_MBSTATE_T=1 ;;
84 case "$gl_cv_func_mbrtowc_sanitycheck" in
86 *) REPLACE_MBSTATE_T=1 ;;
91 if test $REPLACE_MBSTATE_T = 1; then
96 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
97 dnl incomplete multibyte character.
98 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
100 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
102 AC_REQUIRE([AC_PROG_CC])
103 AC_REQUIRE([gt_LOCALE_JA])
104 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
105 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
106 [gl_cv_func_mbrtowc_incomplete_state],
108 dnl Initial guess, used when cross-compiling or when no suitable locale
112 # Guess no on AIX and OSF/1.
113 aix* | osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
114 # Guess yes otherwise.
115 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
118 if test $LOCALE_JA != none; then
123 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
125 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
126 included before <wchar.h>. */
133 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
135 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
139 memset (&state, '\0', sizeof (mbstate_t));
140 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
141 if (mbsinit (&state))
146 [gl_cv_func_mbrtowc_incomplete_state=yes],
147 [gl_cv_func_mbrtowc_incomplete_state=no],
153 dnl Test whether mbrtowc works not worse than mbtowc.
154 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
156 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
158 AC_REQUIRE([AC_PROG_CC])
159 AC_REQUIRE([gt_LOCALE_ZH_CN])
160 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
161 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
162 [gl_cv_func_mbrtowc_sanitycheck],
164 dnl Initial guess, used when cross-compiling or when no suitable locale
168 # Guess no on Solaris 8.
169 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
170 # Guess yes otherwise.
171 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
174 if test $LOCALE_ZH_CN != none; then
180 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
182 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
183 included before <wchar.h>. */
190 /* This fails on Solaris 8:
191 mbrtowc returns 2, and sets wc to 0x00F0.
192 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
193 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
195 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
199 memset (&state, '\0', sizeof (mbstate_t));
200 if (mbrtowc (&wc, input + 3, 6, &state) != 4
201 && mbtowc (&wc, input + 3, 6) == 4)
206 [gl_cv_func_mbrtowc_sanitycheck=yes],
207 [gl_cv_func_mbrtowc_sanitycheck=no],
213 dnl Test whether mbrtowc supports a NULL pwc argument correctly.
214 dnl Result is gl_cv_func_mbrtowc_null_arg1.
216 AC_DEFUN([gl_MBRTOWC_NULL_ARG1],
218 AC_REQUIRE([AC_PROG_CC])
219 AC_REQUIRE([gt_LOCALE_FR_UTF8])
220 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
221 AC_CACHE_CHECK([whether mbrtowc handles a NULL pwc argument],
222 [gl_cv_func_mbrtowc_null_arg1],
224 dnl Initial guess, used when cross-compiling or when no suitable locale
228 # Guess no on Solaris.
229 solaris*) gl_cv_func_mbrtowc_null_arg1="guessing no" ;;
230 # Guess yes otherwise.
231 *) gl_cv_func_mbrtowc_null_arg1="guessing yes" ;;
234 if test $LOCALE_FR_UTF8 != none; then
240 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
242 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
243 included before <wchar.h>. */
252 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
254 char input[] = "\303\237er";
259 memset (&state, '\0', sizeof (mbstate_t));
260 wc = (wchar_t) 0xBADFACE;
261 ret = mbrtowc (&wc, input, 5, &state);
264 if (!mbsinit (&state))
267 memset (&state, '\0', sizeof (mbstate_t));
268 ret = mbrtowc (NULL, input, 5, &state);
269 if (ret != 2) /* Solaris 7 fails here: ret is -1. */
271 if (!mbsinit (&state))
276 [gl_cv_func_mbrtowc_null_arg1=yes],
277 [gl_cv_func_mbrtowc_null_arg1=no],
283 dnl Test whether mbrtowc supports a NULL string argument correctly.
284 dnl Result is gl_cv_func_mbrtowc_null_arg2.
286 AC_DEFUN([gl_MBRTOWC_NULL_ARG2],
288 AC_REQUIRE([AC_PROG_CC])
289 AC_REQUIRE([gt_LOCALE_FR_UTF8])
290 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
291 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
292 [gl_cv_func_mbrtowc_null_arg2],
294 dnl Initial guess, used when cross-compiling or when no suitable locale
299 osf*) gl_cv_func_mbrtowc_null_arg2="guessing no" ;;
300 # Guess yes otherwise.
301 *) gl_cv_func_mbrtowc_null_arg2="guessing yes" ;;
304 if test $LOCALE_FR_UTF8 != none; then
309 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
311 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
312 included before <wchar.h>. */
319 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
325 memset (&state, '\0', sizeof (mbstate_t));
326 wc = (wchar_t) 0xBADFACE;
327 mbrtowc (&wc, NULL, 5, &state);
328 /* Check that wc was not modified. */
329 if (wc != (wchar_t) 0xBADFACE)
334 [gl_cv_func_mbrtowc_null_arg2=yes],
335 [gl_cv_func_mbrtowc_null_arg2=no],
341 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
342 dnl correctly returns the number of bytes that were needed to complete the
343 dnl character (not the total number of bytes of the multibyte character).
344 dnl Result is gl_cv_func_mbrtowc_retval.
346 AC_DEFUN([gl_MBRTOWC_RETVAL],
348 AC_REQUIRE([AC_PROG_CC])
349 AC_REQUIRE([gt_LOCALE_FR_UTF8])
350 AC_REQUIRE([gt_LOCALE_JA])
351 AC_REQUIRE([AC_CANONICAL_HOST])
352 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
353 [gl_cv_func_mbrtowc_retval],
355 dnl Initial guess, used when cross-compiling or when no suitable locale
359 # Guess no on HP-UX, Solaris, native Windows.
360 hpux* | solaris* | mingw*) gl_cv_func_mbrtowc_retval="guessing no" ;;
361 # Guess yes otherwise.
362 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
365 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none \
366 || { case "$host_os" in mingw*) true;; *) false;; esac; }; then
371 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
373 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
374 included before <wchar.h>. */
382 int found_some_locale = 0;
383 /* This fails on Solaris. */
384 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
386 char input[] = "B\303\274\303\237er"; /* "Büßer" */
390 memset (&state, '\0', sizeof (mbstate_t));
391 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
394 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
397 found_some_locale = 1;
399 /* This fails on HP-UX 11.11. */
400 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
402 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
406 memset (&state, '\0', sizeof (mbstate_t));
407 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
410 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
413 found_some_locale = 1;
415 /* This fails on native Windows. */
416 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
418 char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
422 memset (&state, '\0', sizeof (mbstate_t));
423 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
426 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
429 found_some_locale = 1;
431 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
433 char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
437 memset (&state, '\0', sizeof (mbstate_t));
438 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
441 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
444 found_some_locale = 1;
446 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
448 char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
452 memset (&state, '\0', sizeof (mbstate_t));
453 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
456 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
459 found_some_locale = 1;
461 return (found_some_locale ? result : 77);
463 [gl_cv_func_mbrtowc_retval=yes],
464 [if test $? != 77; then
465 gl_cv_func_mbrtowc_retval=no
473 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
474 dnl Result is gl_cv_func_mbrtowc_nul_retval.
476 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
478 AC_REQUIRE([AC_PROG_CC])
479 AC_REQUIRE([gt_LOCALE_ZH_CN])
480 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
481 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
482 [gl_cv_func_mbrtowc_nul_retval],
484 dnl Initial guess, used when cross-compiling or when no suitable locale
488 # Guess no on Solaris 8 and 9.
489 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
490 # Guess yes otherwise.
491 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
494 if test $LOCALE_ZH_CN != none; then
499 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
501 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
502 included before <wchar.h>. */
509 /* This fails on Solaris 8 and 9. */
510 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
515 memset (&state, '\0', sizeof (mbstate_t));
516 if (mbrtowc (&wc, "", 1, &state) != 0)
521 [gl_cv_func_mbrtowc_nul_retval=yes],
522 [gl_cv_func_mbrtowc_nul_retval=no],
528 # Prerequisites of lib/mbrtowc.c.
529 AC_DEFUN([gl_PREREQ_MBRTOWC], [
536 dnl This is an override of an autoconf macro.
538 AC_DEFUN([AC_FUNC_MBRTOWC],
540 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
541 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
545 [[/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be
546 included before <wchar.h>.
547 BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h>
548 must be included before <wchar.h>. */
552 #include <wchar.h>]],
557 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
558 gl_cv_func_mbrtowc=yes,
559 gl_cv_func_mbrtowc=no)])
560 if test $gl_cv_func_mbrtowc = yes; then
561 AC_DEFINE([HAVE_MBRTOWC], [1],
562 [Define to 1 if mbrtowc and mbstate_t are properly declared.])