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