git-merge-changelog: Simplify installation instructions.
[gnulib.git] / m4 / mbrlen.m4
blobfd37fe98fb354c624ca0bd43172404b64d4f18d6
1 # mbrlen.m4
2 # serial 12  -*- coding: utf-8 -*-
3 dnl Copyright (C) 2008, 2010-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 AC_DEFUN([gl_FUNC_MBRLEN],
10   AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
12   AC_REQUIRE([AC_TYPE_MBSTATE_T])
13   AC_REQUIRE([gl_FUNC_MBRTOWC])
14   AC_CHECK_FUNCS_ONCE([mbrlen])
15   if test $ac_cv_func_mbrlen = no; then
16     HAVE_MBRLEN=0
17     AC_CHECK_DECLS([mbrlen],,, [[
18       #include <wchar.h>
19     ]])
20     if test $ac_cv_have_decl_mbrlen = yes; then
21       dnl On Minix 3.1.8, the system's <wchar.h> declares mbrlen() although
22       dnl it does not have the function. Avoid a collision with gnulib's
23       dnl replacement.
24       REPLACE_MBRLEN=1
25     fi
26   else
27     dnl Most bugs affecting the system's mbrtowc function also affect the
28     dnl mbrlen function. So override mbrlen whenever mbrtowc is overridden.
29     dnl We could also run the individual tests below; the results would be
30     dnl the same.
31     if test $REPLACE_MBRTOWC = 1; then
32       REPLACE_MBRLEN=1
33     fi
34   fi
37 dnl Test whether mbrlen puts the state into non-initial state when parsing an
38 dnl incomplete multibyte character.
39 dnl Result is gl_cv_func_mbrlen_incomplete_state.
41 AC_DEFUN([gl_MBRLEN_INCOMPLETE_STATE],
43   AC_REQUIRE([AC_PROG_CC])
44   AC_REQUIRE([gt_LOCALE_JA])
45   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
46   AC_CACHE_CHECK([whether mbrlen handles incomplete characters],
47     [gl_cv_func_mbrlen_incomplete_state],
48     [
49       dnl Initial guess, used when cross-compiling or when no suitable locale
50       dnl is present.
51 changequote(,)dnl
52       case "$host_os" in
53                      # Guess no on AIX and OSF/1.
54         aix* | osf*) gl_cv_func_mbrlen_incomplete_state="guessing no" ;;
55                      # Guess yes otherwise.
56         *)           gl_cv_func_mbrlen_incomplete_state="guessing yes" ;;
57       esac
58 changequote([,])dnl
59       if test $LOCALE_JA != none; then
60         AC_RUN_IFELSE(
61           [AC_LANG_SOURCE([[
62 #include <locale.h>
63 #include <string.h>
64 #include <wchar.h>
65 int main ()
67   if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
68     {
69       const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
70       mbstate_t state;
72       memset (&state, '\0', sizeof (mbstate_t));
73       if (mbrlen (input + 1, 1, &state) == (size_t)(-2))
74         if (mbsinit (&state))
75           return 1;
76     }
77   return 0;
78 }]])],
79           [gl_cv_func_mbrlen_incomplete_state=yes],
80           [gl_cv_func_mbrlen_incomplete_state=no],
81           [])
82       fi
83     ])
86 dnl Test whether mbrlen, when parsing the end of a multibyte character,
87 dnl correctly returns the number of bytes that were needed to complete the
88 dnl character (not the total number of bytes of the multibyte character).
89 dnl Result is gl_cv_func_mbrlen_retval.
91 AC_DEFUN([gl_MBRLEN_RETVAL],
93   AC_REQUIRE([AC_PROG_CC])
94   AC_REQUIRE([gt_LOCALE_FR_UTF8])
95   AC_REQUIRE([gt_LOCALE_JA])
96   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
97   AC_CACHE_CHECK([whether mbrlen has a correct return value],
98     [gl_cv_func_mbrlen_retval],
99     [
100       dnl Initial guess, used when cross-compiling or when no suitable locale
101       dnl is present.
102 changequote(,)dnl
103       case "$host_os" in
104                           # Guess no on HP-UX and Solaris.
105         hpux* | solaris*) gl_cv_func_mbrlen_retval="guessing no" ;;
106                           # Guess yes otherwise.
107         *)                gl_cv_func_mbrlen_retval="guessing yes" ;;
108       esac
109 changequote([,])dnl
110       if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none; then
111         AC_RUN_IFELSE(
112           [AC_LANG_SOURCE([[
113 #include <locale.h>
114 #include <string.h>
115 #include <wchar.h>
116 int main ()
118   int result = 0;
119   /* This fails on Solaris.  */
120   if (strcmp ("$LOCALE_FR_UTF8", "none") != 0
121       && setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
122     {
123       char input[] = "B\303\274\303\237er"; /* "Büßer" */
124       mbstate_t state;
126       memset (&state, '\0', sizeof (mbstate_t));
127       if (mbrlen (input + 1, 1, &state) == (size_t)(-2))
128         {
129           input[1] = '\0';
130           if (mbrlen (input + 2, 5, &state) != 1)
131             result |= 1;
132         }
133     }
134   /* This fails on HP-UX 11.11.  */
135   if (strcmp ("$LOCALE_JA", "none") != 0
136       && setlocale (LC_ALL, "$LOCALE_JA") != NULL)
137     {
138       char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
139       mbstate_t state;
141       memset (&state, '\0', sizeof (mbstate_t));
142       if (mbrlen (input + 1, 1, &state) == (size_t)(-2))
143         {
144           input[1] = '\0';
145           if (mbrlen (input + 2, 5, &state) != 2)
146             result |= 2;
147         }
148     }
149   return result;
150 }]])],
151           [gl_cv_func_mbrlen_retval=yes],
152           [gl_cv_func_mbrlen_retval=no],
153           [])
154       fi
155     ])
158 dnl Test whether mbrlen, when parsing a NUL character, correctly returns 0.
159 dnl Result is gl_cv_func_mbrlen_nul_retval.
161 AC_DEFUN([gl_MBRLEN_NUL_RETVAL],
163   AC_REQUIRE([AC_PROG_CC])
164   AC_REQUIRE([gt_LOCALE_ZH_CN])
165   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
166   AC_CACHE_CHECK([whether mbrlen returns 0 when parsing a NUL character],
167     [gl_cv_func_mbrlen_nul_retval],
168     [
169       dnl Initial guess, used when cross-compiling or when no suitable locale
170       dnl is present.
171 changequote(,)dnl
172       case "$host_os" in
173                     # Guess no on Solaris 9.
174         solaris2.9) gl_cv_func_mbrlen_nul_retval="guessing no" ;;
175                     # Guess yes otherwise.
176         *)          gl_cv_func_mbrlen_nul_retval="guessing yes" ;;
177       esac
178 changequote([,])dnl
179       if test $LOCALE_ZH_CN != none; then
180         AC_RUN_IFELSE(
181           [AC_LANG_SOURCE([[
182 #include <locale.h>
183 #include <string.h>
184 #include <wchar.h>
185 int main ()
187   /* This crashes on Solaris 9 inside __mbrtowc_dense_gb18030.  */
188   if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
189     {
190       mbstate_t state;
192       memset (&state, '\0', sizeof (mbstate_t));
193       if (mbrlen ("", 1, &state) != 0)
194         return 1;
195     }
196   return 0;
197 }]])],
198           [gl_cv_func_mbrlen_nul_retval=yes],
199           [gl_cv_func_mbrlen_nul_retval=no],
200           [])
201       fi
202     ])
205 dnl Test whether mbrlen returns the correct value on empty input.
207 AC_DEFUN([gl_MBRLEN_EMPTY_INPUT],
209   AC_REQUIRE([AC_PROG_CC])
210   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
211   AC_CACHE_CHECK([whether mbrlen works on empty input],
212     [gl_cv_func_mbrlen_empty_input],
213     [
214       dnl Initial guess, used when cross-compiling or when no suitable locale
215       dnl is present.
216 changequote(,)dnl
217       case "$host_os" in
218                               # Guess no on AIX and glibc systems.
219         aix* | *-gnu* | gnu*) gl_cv_func_mbrlen_empty_input="guessing no" ;;
220         *)                    gl_cv_func_mbrlen_empty_input="guessing yes" ;;
221       esac
222 changequote([,])dnl
223       AC_RUN_IFELSE(
224         [AC_LANG_SOURCE([[
225            #include <wchar.h>
226            static mbstate_t mbs;
227            int
228            main (void)
229            {
230              return mbrlen ("", 0, &mbs) != (size_t) -2;
231            }]])],
232         [gl_cv_func_mbrlen_empty_input=yes],
233         [gl_cv_func_mbrlen_empty_input=no],
234         [:])
235     ])
238 # Prerequisites of lib/mbrlen.c.
239 AC_DEFUN([gl_PREREQ_MBRLEN], [
240   :