025198a91dc380e7d1a4cecbe126ec186cbb50f4
[gnulib.git] / m4 / mbsrtowcs.m4
1 # mbsrtowcs.m4 serial 12
2 dnl Copyright (C) 2008-2011 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
14   AC_CHECK_FUNCS_ONCE([mbsrtowcs])
15   if test $ac_cv_func_mbsrtowcs = no; then
16     HAVE_MBSRTOWCS=0
17   else
18     if test $REPLACE_MBSTATE_T = 1; then
19       REPLACE_MBSRTOWCS=1
20     else
21       gl_MBSRTOWCS_WORKS
22       case "$gl_cv_func_mbsrtowcs_works" in
23         *yes) ;;
24         *) REPLACE_MBSRTOWCS=1 ;;
25       esac
26     fi
27   fi
28 ])
29
30 dnl Test whether mbsrtowcs works.
31 dnl Result is gl_cv_func_mbsrtowcs_works.
32
33 AC_DEFUN([gl_MBSRTOWCS_WORKS],
34 [
35   AC_REQUIRE([AC_PROG_CC])
36   AC_REQUIRE([gt_LOCALE_FR])
37   AC_REQUIRE([gt_LOCALE_FR_UTF8])
38   AC_REQUIRE([gt_LOCALE_JA])
39   AC_REQUIRE([gt_LOCALE_ZH_CN])
40   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
41   AC_CACHE_CHECK([whether mbsrtowcs works],
42     [gl_cv_func_mbsrtowcs_works],
43     [
44       dnl Initial guess, used when cross-compiling or when no suitable locale
45       dnl is present.
46 changequote(,)dnl
47       case "$host_os" in
48                                    # Guess no on HP-UX, Solaris, mingw.
49         hpux* | solaris* | mingw*) gl_cv_func_mbsrtowcs_works="guessing no" ;;
50                                    # Guess yes otherwise.
51         *)                         gl_cv_func_mbsrtowcs_works="guessing yes" ;;
52       esac
53 changequote([,])dnl
54       if test $LOCALE_FR != none || test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none || test $LOCALE_ZH_CN != none; then
55         AC_RUN_IFELSE(
56           [AC_LANG_SOURCE([[
57 #include <locale.h>
58 #include <string.h>
59 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
60    <wchar.h>.
61    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
62    included before <wchar.h>.  */
63 #include <stddef.h>
64 #include <stdio.h>
65 #include <time.h>
66 #include <wchar.h>
67 int main ()
68 {
69   int result = 0;
70   /* Test whether the function supports a NULL destination argument.
71      This fails on native Windows.  */
72   if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
73     {
74       const char input[] = "\337er";
75       const char *src = input;
76       mbstate_t state;
77
78       memset (&state, '\0', sizeof (mbstate_t));
79       if (mbsrtowcs (NULL, &src, 1, &state) != 3
80           || src != input)
81         result |= 1;
82     }
83   /* Test whether the function works when started with a conversion state
84      in non-initial state.  This fails on HP-UX 11.11 and Solaris 10.  */
85   if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
86     {
87       const char input[] = "B\303\274\303\237er";
88       mbstate_t state;
89
90       memset (&state, '\0', sizeof (mbstate_t));
91       if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
92         if (!mbsinit (&state))
93           {
94             const char *src = input + 2;
95             if (mbsrtowcs (NULL, &src, 10, &state) != 4)
96               result |= 2;
97           }
98     }
99   if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
100     {
101       const char input[] = "<\306\374\313\334\270\354>";
102       mbstate_t state;
103
104       memset (&state, '\0', sizeof (mbstate_t));
105       if (mbrtowc (NULL, input + 3, 1, &state) == (size_t)(-2))
106         if (!mbsinit (&state))
107           {
108             const char *src = input + 4;
109             if (mbsrtowcs (NULL, &src, 10, &state) != 3)
110               result |= 4;
111           }
112     }
113   if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
114     {
115       const char input[] = "B\250\271\201\060\211\070er";
116       mbstate_t state;
117
118       memset (&state, '\0', sizeof (mbstate_t));
119       if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
120         if (!mbsinit (&state))
121           {
122             const char *src = input + 2;
123             if (mbsrtowcs (NULL, &src, 10, &state) != 4)
124               result |= 8;
125           }
126     }
127   return result;
128 }]])],
129           [gl_cv_func_mbsrtowcs_works=yes],
130           [gl_cv_func_mbsrtowcs_works=no],
131           [:])
132       fi
133     ])
134 ])
135
136 # Prerequisites of lib/mbsrtowcs.c.
137 AC_DEFUN([gl_PREREQ_MBSRTOWCS], [
138   :
139 ])