Merge branch 'upstream' into stable
[gnulib.git] / m4 / mbrlen.m4
1 # mbrlen.m4 serial 6
2 dnl Copyright (C) 2008, 2010-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_MBRLEN],
8 [
9   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
10
11   AC_REQUIRE([AC_TYPE_MBSTATE_T])
12   AC_REQUIRE([gl_FUNC_MBRTOWC])
13   AC_CHECK_FUNCS_ONCE([mbrlen])
14   if test $ac_cv_func_mbrlen = no; then
15     HAVE_MBRLEN=0
16   else
17     dnl Most bugs affecting the system's mbrtowc function also affect the
18     dnl mbrlen function. So override mbrlen whenever mbrtowc is overridden.
19     dnl We could also run the individual tests below; the results would be
20     dnl the same.
21     if test $REPLACE_MBRTOWC = 1; then
22       REPLACE_MBRLEN=1
23     fi
24   fi
25   if test $HAVE_MBRLEN = 0 || test $REPLACE_MBRLEN = 1; then
26     AC_LIBOBJ([mbrlen])
27     gl_PREREQ_MBRLEN
28   fi
29 ])
30
31 dnl Test whether mbrlen puts the state into non-initial state when parsing an
32 dnl incomplete multibyte character.
33 dnl Result is gl_cv_func_mbrlen_incomplete_state.
34
35 AC_DEFUN([gl_MBRLEN_INCOMPLETE_STATE],
36 [
37   AC_REQUIRE([AC_PROG_CC])
38   AC_REQUIRE([gt_LOCALE_JA])
39   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
40   AC_CACHE_CHECK([whether mbrlen handles incomplete characters],
41     [gl_cv_func_mbrlen_incomplete_state],
42     [
43       dnl Initial guess, used when cross-compiling or when no suitable locale
44       dnl is present.
45 changequote(,)dnl
46       case "$host_os" in
47                      # Guess no on AIX and OSF/1.
48         aix* | osf*) gl_cv_func_mbrlen_incomplete_state="guessing no" ;;
49                      # Guess yes otherwise.
50         *)           gl_cv_func_mbrlen_incomplete_state="guessing yes" ;;
51       esac
52 changequote([,])dnl
53       if test $LOCALE_JA != none; then
54         AC_RUN_IFELSE(
55           [AC_LANG_SOURCE([[
56 #include <locale.h>
57 #include <string.h>
58 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
59    <wchar.h>.
60    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
61    included before <wchar.h>.  */
62 #include <stddef.h>
63 #include <stdio.h>
64 #include <time.h>
65 #include <wchar.h>
66 int main ()
67 {
68   if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
69     {
70       const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
71       mbstate_t state;
72
73       memset (&state, '\0', sizeof (mbstate_t));
74       if (mbrlen (input + 1, 1, &state) == (size_t)(-2))
75         if (mbsinit (&state))
76           return 1;
77     }
78   return 0;
79 }]])],
80           [gl_cv_func_mbrlen_incomplete_state=yes],
81           [gl_cv_func_mbrlen_incomplete_state=no],
82           [])
83       fi
84     ])
85 ])
86
87 dnl Test whether mbrlen, when parsing the end of a multibyte character,
88 dnl correctly returns the number of bytes that were needed to complete the
89 dnl character (not the total number of bytes of the multibyte character).
90 dnl Result is gl_cv_func_mbrlen_retval.
91
92 AC_DEFUN([gl_MBRLEN_RETVAL],
93 [
94   AC_REQUIRE([AC_PROG_CC])
95   AC_REQUIRE([gt_LOCALE_FR_UTF8])
96   AC_REQUIRE([gt_LOCALE_JA])
97   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
98   AC_CACHE_CHECK([whether mbrlen has a correct return value],
99     [gl_cv_func_mbrlen_retval],
100     [
101       dnl Initial guess, used when cross-compiling or when no suitable locale
102       dnl is present.
103 changequote(,)dnl
104       case "$host_os" in
105                           # Guess no on HP-UX and Solaris.
106         hpux* | solaris*) gl_cv_func_mbrlen_retval="guessing no" ;;
107                           # Guess yes otherwise.
108         *)                gl_cv_func_mbrlen_retval="guessing yes" ;;
109       esac
110 changequote([,])dnl
111       if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none; then
112         AC_RUN_IFELSE(
113           [AC_LANG_SOURCE([[
114 #include <locale.h>
115 #include <string.h>
116 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
117    <wchar.h>.
118    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
119    included before <wchar.h>.  */
120 #include <stddef.h>
121 #include <stdio.h>
122 #include <time.h>
123 #include <wchar.h>
124 int main ()
125 {
126   int result = 0;
127   /* This fails on Solaris.  */
128   if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
129     {
130       char input[] = "B\303\274\303\237er"; /* "Büßer" */
131       mbstate_t state;
132
133       memset (&state, '\0', sizeof (mbstate_t));
134       if (mbrlen (input + 1, 1, &state) == (size_t)(-2))
135         {
136           input[1] = '\0';
137           if (mbrlen (input + 2, 5, &state) != 1)
138             result |= 1;
139         }
140     }
141   /* This fails on HP-UX 11.11.  */
142   if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
143     {
144       char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
145       mbstate_t state;
146
147       memset (&state, '\0', sizeof (mbstate_t));
148       if (mbrlen (input + 1, 1, &state) == (size_t)(-2))
149         {
150           input[1] = '\0';
151           if (mbrlen (input + 2, 5, &state) != 2)
152             result |= 2;
153         }
154     }
155   return result;
156 }]])],
157           [gl_cv_func_mbrlen_retval=yes],
158           [gl_cv_func_mbrlen_retval=no],
159           [])
160       fi
161     ])
162 ])
163
164 dnl Test whether mbrlen, when parsing a NUL character, correctly returns 0.
165 dnl Result is gl_cv_func_mbrlen_nul_retval.
166
167 AC_DEFUN([gl_MBRLEN_NUL_RETVAL],
168 [
169   AC_REQUIRE([AC_PROG_CC])
170   AC_REQUIRE([gt_LOCALE_ZH_CN])
171   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
172   AC_CACHE_CHECK([whether mbrlen returns 0 when parsing a NUL character],
173     [gl_cv_func_mbrlen_nul_retval],
174     [
175       dnl Initial guess, used when cross-compiling or when no suitable locale
176       dnl is present.
177 changequote(,)dnl
178       case "$host_os" in
179                     # Guess no on Solaris 9.
180         solaris2.9) gl_cv_func_mbrlen_nul_retval="guessing no" ;;
181                     # Guess yes otherwise.
182         *)          gl_cv_func_mbrlen_nul_retval="guessing yes" ;;
183       esac
184 changequote([,])dnl
185       if test $LOCALE_ZH_CN != none; then
186         AC_RUN_IFELSE(
187           [AC_LANG_SOURCE([[
188 #include <locale.h>
189 #include <string.h>
190 /* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
191    <wchar.h>.
192    BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
193    included before <wchar.h>.  */
194 #include <stddef.h>
195 #include <stdio.h>
196 #include <time.h>
197 #include <wchar.h>
198 int main ()
199 {
200   /* This crashes on Solaris 9 inside __mbrtowc_dense_gb18030.  */
201   if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
202     {
203       mbstate_t state;
204
205       memset (&state, '\0', sizeof (mbstate_t));
206       if (mbrlen ("", 1, &state) != 0)
207         return 1;
208     }
209   return 0;
210 }]])],
211           [gl_cv_func_mbrlen_nul_retval=yes],
212           [gl_cv_func_mbrlen_nul_retval=no],
213           [])
214       fi
215     ])
216 ])
217
218 # Prerequisites of lib/mbrlen.c.
219 AC_DEFUN([gl_PREREQ_MBRLEN], [
220   :
221 ])