Work around mbsrtowcs bug on various platforms.
[gnulib.git] / m4 / mbsrtowcs.m4
1 # mbsrtowcs.m4 serial 3
2 dnl Copyright (C) 2008 Free Software Foundation, Inc.
3 dnl This file is free software; the Free Software Foundation
4 dnl gives unlimited permission to copy and/or distribute it,
5 dnl with or without modifications, as long as this notice is preserved.
6
7 AC_DEFUN([gl_FUNC_MBSRTOWCS],
8 [
9   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
10
11   AC_REQUIRE([AC_TYPE_MBSTATE_T])
12   gl_MBSTATE_T_BROKEN
13   if test $REPLACE_MBSTATE_T = 1; then
14     REPLACE_MBSRTOWCS=1
15   fi
16   AC_CHECK_FUNCS_ONCE([mbsrtowcs])
17   if test $ac_cv_func_mbsrtowcs = no; then
18     HAVE_MBSRTOWCS=0
19   fi
20   if test $HAVE_MBSRTOWCS != 0 && test $REPLACE_MBSRTOWCS != 1; then
21     gl_MBSRTOWCS_WORKS
22     case "$gl_cv_func_mbsrtowcs_works" in
23       *yes) ;;
24       *) REPLACE_MBSRTOWCS=1 ;;
25     esac
26   fi
27   if test $HAVE_MBSRTOWCS = 0 || test $REPLACE_MBSRTOWCS = 1; then
28     gl_REPLACE_WCHAR_H
29     AC_LIBOBJ([mbsrtowcs])
30     gl_PREREQ_MBSRTOWCS
31   fi
32 ])
33
34 dnl Test whether mbsrtowcs works.
35 dnl Result is gl_cv_func_mbsrtowcs_works.
36
37 AC_DEFUN([gl_MBSRTOWCS_WORKS],
38 [
39   AC_REQUIRE([AC_PROG_CC])
40   AC_REQUIRE([gt_LOCALE_FR_UTF8])
41   AC_REQUIRE([gt_LOCALE_JA])
42   AC_REQUIRE([gt_LOCALE_ZH_CN])
43   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
44   AC_CACHE_CHECK([whether mbsrtowcs works],
45     [gl_cv_func_mbsrtowcs_works],
46     [
47       dnl Initial guess, used when cross-compiling or when no suitable locale
48       dnl is present.
49 changequote(,)dnl
50       case "$host_os" in
51                           # Guess no on HP-UX and Solaris.
52         hpux* | solaris*) gl_cv_func_mbsrtowcs_works="guessing no" ;;
53                           # Guess yes otherwise.
54         *)                gl_cv_func_mbsrtowcs_works="guessing yes" ;;
55       esac
56 changequote([,])dnl
57       if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none || test $LOCALE_ZH_CN != none; then
58         AC_TRY_RUN([
59 #include <locale.h>
60 #include <string.h>
61 #include <wchar.h>
62 int main ()
63 {
64   /* Test whether the function works when started with a conversion state
65      in non-initial state.  This fails on HP-UX 11.11 and Solaris 10.  */
66   if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
67     {
68       const char input[] = "B\303\274\303\237er";
69       mbstate_t state;
70
71       memset (&state, '\0', sizeof (mbstate_t));
72       if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
73         if (!mbsinit (&state))
74           {
75             const char *src = input + 2;
76             if (mbsrtowcs (NULL, &src, 10, &state) != 4)
77               return 1;
78           }
79     }
80   if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
81     {
82       const char input[] = "<\306\374\313\334\270\354>";
83       mbstate_t state;
84
85       memset (&state, '\0', sizeof (mbstate_t));
86       if (mbrtowc (NULL, input + 3, 1, &state) == (size_t)(-2))
87         if (!mbsinit (&state))
88           {
89             const char *src = input + 4;
90             if (mbsrtowcs (NULL, &src, 10, &state) != 3)
91               return 1;
92           }
93     }
94   if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
95     {
96       const char input[] = "B\250\271\201\060\211\070er";
97       mbstate_t state;
98
99       memset (&state, '\0', sizeof (mbstate_t));
100       if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
101         if (!mbsinit (&state))
102           {
103             const char *src = input + 2;
104             if (mbsrtowcs (NULL, &src, 10, &state) != 4)
105               return 1;
106           }
107     }
108   return 0;
109 }],
110           [gl_cv_func_mbsrtowcs_works=yes],
111           [gl_cv_func_mbsrtowcs_works=no],
112           [])
113       fi
114     ])
115 ])
116
117 # Prerequisites of lib/mbsrtowcs.c.
118 AC_DEFUN([gl_PREREQ_MBSRTOWCS], [
119   :
120 ])