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