Debian Codegraph @

m4/mbrtowc.m4

raw
1 # mbrtowc.m4 serial 43 -*- coding: utf-8 -*-
2 dnl Copyright (C) 2001-2002, 2004-2005, 2008-2023 Free Software Foundation,
3 dnl 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.
7
8 AC_DEFUN([gl_FUNC_MBRTOWC],
9 [
10 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
11 AC_REQUIRE([gl_PTHREADLIB])
12 AC_CHECK_HEADERS_ONCE([threads.h])
13
14 AC_REQUIRE([AC_TYPE_MBSTATE_T])
15 gl_MBSTATE_T_BROKEN
16
17 AC_CHECK_FUNCS_ONCE([mbrtowc])
18 if test $ac_cv_func_mbrtowc = no; then
19 HAVE_MBRTOWC=0
20 AC_CHECK_DECLS([mbrtowc],,, [[
21 #include <wchar.h>
22 ]])
23 if test $ac_cv_have_decl_mbrtowc = yes; then
24 dnl On Minix 3.1.8, the system's <wchar.h> declares mbrtowc() although
25 dnl it does not have the function. Avoid a collision with gnulib's
26 dnl replacement.
27 REPLACE_MBRTOWC=1
28 fi
29 else
30 if test $REPLACE_MBSTATE_T = 1; then
31 REPLACE_MBRTOWC=1
32 else
33 gl_MBRTOWC_NULL_ARG1
34 gl_MBRTOWC_NULL_ARG2
35 gl_MBRTOWC_RETVAL
36 gl_MBRTOWC_NUL_RETVAL
37 gl_MBRTOWC_STORES_INCOMPLETE
38 gl_MBRTOWC_EMPTY_INPUT
39 gl_MBRTOWC_C_LOCALE
40 case "$gl_cv_func_mbrtowc_null_arg1" in
41 *yes) ;;
42 *) AC_DEFINE([MBRTOWC_NULL_ARG1_BUG], [1],
43 [Define if the mbrtowc function has the NULL pwc argument bug.])
44 REPLACE_MBRTOWC=1
45 ;;
46 esac
47 case "$gl_cv_func_mbrtowc_null_arg2" in
48 *yes) ;;
49 *) AC_DEFINE([MBRTOWC_NULL_ARG2_BUG], [1],
50 [Define if the mbrtowc function has the NULL string argument bug.])
51 REPLACE_MBRTOWC=1
52 ;;
53 esac
54 case "$gl_cv_func_mbrtowc_retval" in
55 *yes) ;;
56 *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
57 [Define if the mbrtowc function returns a wrong return value.])
58 REPLACE_MBRTOWC=1
59 ;;
60 esac
61 case "$gl_cv_func_mbrtowc_nul_retval" in
62 *yes) ;;
63 *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
64 [Define if the mbrtowc function does not return 0 for a NUL character.])
65 REPLACE_MBRTOWC=1
66 ;;
67 esac
68 case "$gl_cv_func_mbrtowc_stores_incomplete" in
69 *no) ;;
70 *) AC_DEFINE([MBRTOWC_STORES_INCOMPLETE_BUG], [1],
71 [Define if the mbrtowc function stores a wide character when reporting incomplete input.])
72 REPLACE_MBRTOWC=1
73 ;;
74 esac
75 case "$gl_cv_func_mbrtowc_empty_input" in
76 *yes) ;;
77 *) AC_DEFINE([MBRTOWC_EMPTY_INPUT_BUG], [1],
78 [Define if the mbrtowc function does not return (size_t) -2
79 for empty input.])
80 REPLACE_MBRTOWC=1
81 ;;
82 esac
83 case "$gl_cv_func_mbrtowc_C_locale_sans_EILSEQ" in
84 *yes) ;;
85 *) AC_DEFINE([MBRTOWC_IN_C_LOCALE_MAYBE_EILSEQ], [1],
86 [Define if the mbrtowc function may signal encoding errors in the C locale.])
87 REPLACE_MBRTOWC=1
88 ;;
89 esac
90 fi
91 fi
92 if test $REPLACE_MBSTATE_T = 1; then
93 case "$host_os" in
94 mingw*) MBRTOWC_LIB= ;;
95 *)
96 gl_WEAK_SYMBOLS
97 case "$gl_cv_have_weak" in
98 *yes) MBRTOWC_LIB= ;;
99 *) MBRTOWC_LIB="$LIBPTHREAD" ;;
100 esac
101 ;;
102 esac
103 else
104 MBRTOWC_LIB=
105 fi
106 dnl MBRTOWC_LIB is expected to be '-pthread' or '-lpthread' on AIX
107 dnl with gcc or xlc, and empty otherwise.
108 AC_SUBST([MBRTOWC_LIB])
109 dnl For backward compatibility.
110 LIB_MBRTOWC="$MBRTOWC_LIB"
111 AC_SUBST([LIB_MBRTOWC])
112 ])
113
114 dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
115 dnl redefines the semantics of the given mbstate_t type.
116 dnl Result is REPLACE_MBSTATE_T.
117 dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
118 dnl avoid inconsistencies.
119
120 AC_DEFUN_ONCE([gl_MBSTATE_T_BROKEN],
121 [
122 AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
123 AC_REQUIRE([AC_CANONICAL_HOST])
124
125 AC_REQUIRE([AC_TYPE_MBSTATE_T])
126 AC_CHECK_FUNCS_ONCE([mbsinit])
127 AC_CHECK_FUNCS_ONCE([mbrtowc])
128 dnl On native Windows, we know exactly how mbsinit() behaves and don't need
129 dnl to override it, even if - like on MSVC - mbsinit() is only defined as
130 dnl an inline function, not as a global function.
131 if case "$host_os" in
132 mingw*) true ;;
133 *) test $ac_cv_func_mbsinit = yes ;;
134 esac \
135 && test $ac_cv_func_mbrtowc = yes; then
136 gl_MBRTOWC_INCOMPLETE_STATE
137 gl_MBRTOWC_SANITYCHECK
138 REPLACE_MBSTATE_T=0
139 case "$gl_cv_func_mbrtowc_incomplete_state" in
140 *yes) ;;
141 *) REPLACE_MBSTATE_T=1 ;;
142 esac
143 case "$gl_cv_func_mbrtowc_sanitycheck" in
144 *yes) ;;
145 *) REPLACE_MBSTATE_T=1 ;;
146 esac
147 else
148 REPLACE_MBSTATE_T=1
149 fi
150 ])
151
152 dnl Test whether mbrtowc puts the state into non-initial state when parsing an
153 dnl incomplete multibyte character.
154 dnl Result is gl_cv_func_mbrtowc_incomplete_state.
155
156 AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
157 [
158 AC_REQUIRE([AC_PROG_CC])
159 AC_REQUIRE([gt_LOCALE_JA])
160 AC_REQUIRE([gt_LOCALE_FR_UTF8])
161 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
162 AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
163 [gl_cv_func_mbrtowc_incomplete_state],
164 [
165 dnl Initial guess, used when cross-compiling or when no suitable locale
166 dnl is present.
167 changequote(,)dnl
168 case "$host_os" in
169 # Guess no on AIX and OSF/1.
170 aix* | osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
171 # Guess yes otherwise.
172 *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
173 esac
174 changequote([,])dnl
175 if test $LOCALE_JA != none; then
176 AC_RUN_IFELSE(
177 [AC_LANG_SOURCE([[
178 #include <locale.h>
179 #include <string.h>
180 #include <wchar.h>
181 int main ()
182 {
183 if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
184 {
185 const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
186 mbstate_t state;
187 wchar_t wc;
188
189 memset (&state, '\0', sizeof (mbstate_t));
190 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
191 if (mbsinit (&state))
192 return 2;
193 }
194 return 0;
195 }]])],
196 [gl_cv_func_mbrtowc_incomplete_state=yes],
197 [gl_cv_func_mbrtowc_incomplete_state=no],
198 [:])
199 else
200 if test $LOCALE_FR_UTF8 != none; then
201 AC_RUN_IFELSE(
202 [AC_LANG_SOURCE([[
203 #include <locale.h>
204 #include <string.h>
205 #include <wchar.h>
206 int main ()
207 {
208 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
209 {
210 const char input[] = "B\303\274\303\237er"; /* "Büßer" */
211 mbstate_t state;
212 wchar_t wc;
213
214 memset (&state, '\0', sizeof (mbstate_t));
215 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
216 if (mbsinit (&state))
217 return 2;
218 }
219 return 0;
220 }]])],
221 [gl_cv_func_mbrtowc_incomplete_state=yes],
222 [gl_cv_func_mbrtowc_incomplete_state=no],
223 [:])
224 fi
225 fi
226 ])
227 ])
228
229 dnl Test whether mbrtowc works not worse than mbtowc.
230 dnl Result is gl_cv_func_mbrtowc_sanitycheck.
231
232 AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
233 [
234 AC_REQUIRE([AC_PROG_CC])
235 AC_REQUIRE([gt_LOCALE_ZH_CN])
236 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
237 AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
238 [gl_cv_func_mbrtowc_sanitycheck],
239 [
240 dnl Initial guess, used when cross-compiling or when no suitable locale
241 dnl is present.
242 changequote(,)dnl
243 case "$host_os" in
244 # Guess no on Solaris 8.
245 solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
246 # Guess yes otherwise.
247 *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
248 esac
249 changequote([,])dnl
250 if test $LOCALE_ZH_CN != none; then
251 AC_RUN_IFELSE(
252 [AC_LANG_SOURCE([[
253 #include <locale.h>
254 #include <stdlib.h>
255 #include <string.h>
256 #include <wchar.h>
257 int main ()
258 {
259 /* This fails on Solaris 8:
260 mbrtowc returns 2, and sets wc to 0x00F0.
261 mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
262 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
263 {
264 char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
265 mbstate_t state;
266 wchar_t wc;
267
268 memset (&state, '\0', sizeof (mbstate_t));
269 if (mbrtowc (&wc, input + 3, 6, &state) != 4
270 && mbtowc (&wc, input + 3, 6) == 4)
271 return 2;
272 }
273 return 0;
274 }]])],
275 [gl_cv_func_mbrtowc_sanitycheck=yes],
276 [gl_cv_func_mbrtowc_sanitycheck=no],
277 [:])
278 fi
279 ])
280 ])
281
282 dnl Test whether mbrtowc supports a NULL pwc argument correctly.
283 dnl Result is gl_cv_func_mbrtowc_null_arg1.
284
285 AC_DEFUN([gl_MBRTOWC_NULL_ARG1],
286 [
287 AC_REQUIRE([AC_PROG_CC])
288 AC_REQUIRE([gt_LOCALE_FR_UTF8])
289 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
290 AC_CACHE_CHECK([whether mbrtowc handles a NULL pwc argument],
291 [gl_cv_func_mbrtowc_null_arg1],
292 [
293 dnl Initial guess, used when cross-compiling or when no suitable locale
294 dnl is present.
295 changequote(,)dnl
296 case "$host_os" in
297 # Guess no on Solaris.
298 solaris*) gl_cv_func_mbrtowc_null_arg1="guessing no" ;;
299 # Guess yes otherwise.
300 *) gl_cv_func_mbrtowc_null_arg1="guessing yes" ;;
301 esac
302 changequote([,])dnl
303 if test $LOCALE_FR_UTF8 != none; then
304 AC_RUN_IFELSE(
305 [AC_LANG_SOURCE([[
306 #include <locale.h>
307 #include <stdlib.h>
308 #include <string.h>
309 #include <wchar.h>
310 int main ()
311 {
312 int result = 0;
313
314 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
315 {
316 char input[] = "\303\237er";
317 mbstate_t state;
318 wchar_t wc;
319 size_t ret;
320
321 memset (&state, '\0', sizeof (mbstate_t));
322 wc = (wchar_t) 0xBADFACE;
323 ret = mbrtowc (&wc, input, 5, &state);
324 if (ret != 2)
325 result |= 1;
326 if (!mbsinit (&state))
327 result |= 2;
328
329 memset (&state, '\0', sizeof (mbstate_t));
330 ret = mbrtowc (NULL, input, 5, &state);
331 if (ret != 2) /* Solaris 7 fails here: ret is -1. */
332 result |= 4;
333 if (!mbsinit (&state))
334 result |= 8;
335 }
336 return result;
337 }]])],
338 [gl_cv_func_mbrtowc_null_arg1=yes],
339 [gl_cv_func_mbrtowc_null_arg1=no],
340 [:])
341 fi
342 ])
343 ])
344
345 dnl Test whether mbrtowc supports a NULL string argument correctly.
346 dnl Result is gl_cv_func_mbrtowc_null_arg2.
347
348 AC_DEFUN([gl_MBRTOWC_NULL_ARG2],
349 [
350 AC_REQUIRE([AC_PROG_CC])
351 AC_REQUIRE([gt_LOCALE_FR_UTF8])
352 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
353 AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
354 [gl_cv_func_mbrtowc_null_arg2],
355 [
356 dnl Initial guess, used when cross-compiling or when no suitable locale
357 dnl is present.
358 changequote(,)dnl
359 case "$host_os" in
360 # Guess no on OSF/1.
361 osf*) gl_cv_func_mbrtowc_null_arg2="guessing no" ;;
362 # Guess yes otherwise.
363 *) gl_cv_func_mbrtowc_null_arg2="guessing yes" ;;
364 esac
365 changequote([,])dnl
366 if test $LOCALE_FR_UTF8 != none; then
367 AC_RUN_IFELSE(
368 [AC_LANG_SOURCE([[
369 #include <locale.h>
370 #include <string.h>
371 #include <wchar.h>
372 int main ()
373 {
374 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
375 {
376 mbstate_t state;
377 wchar_t wc;
378 int ret;
379
380 memset (&state, '\0', sizeof (mbstate_t));
381 wc = (wchar_t) 0xBADFACE;
382 mbrtowc (&wc, NULL, 5, &state);
383 /* Check that wc was not modified. */
384 if (wc != (wchar_t) 0xBADFACE)
385 return 2;
386 }
387 return 0;
388 }]])],
389 [gl_cv_func_mbrtowc_null_arg2=yes],
390 [gl_cv_func_mbrtowc_null_arg2=no],
391 [:])
392 fi
393 ])
394 ])
395
396 dnl Test whether mbrtowc, when parsing the end of a multibyte character,
397 dnl correctly returns the number of bytes that were needed to complete the
398 dnl character (not the total number of bytes of the multibyte character).
399 dnl Result is gl_cv_func_mbrtowc_retval.
400
401 AC_DEFUN([gl_MBRTOWC_RETVAL],
402 [
403 AC_REQUIRE([AC_PROG_CC])
404 AC_REQUIRE([gt_LOCALE_FR_UTF8])
405 AC_REQUIRE([gt_LOCALE_JA])
406 AC_REQUIRE([AC_CANONICAL_HOST])
407 AC_CACHE_CHECK([whether mbrtowc has a correct return value],
408 [gl_cv_func_mbrtowc_retval],
409 [
410 dnl Initial guess, used when cross-compiling or when no suitable locale
411 dnl is present.
412 changequote(,)dnl
413 case "$host_os" in
414 # Guess no on HP-UX, Solaris, native Windows.
415 hpux* | solaris* | mingw*) gl_cv_func_mbrtowc_retval="guessing no" ;;
416 # Guess yes otherwise.
417 *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
418 esac
419 changequote([,])dnl
420 if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none \
421 || { case "$host_os" in mingw*) true;; *) false;; esac; }; then
422 AC_RUN_IFELSE(
423 [AC_LANG_SOURCE([[
424 #include <locale.h>
425 #include <string.h>
426 #include <wchar.h>
427 int main ()
428 {
429 int result = 0;
430 int found_some_locale = 0;
431 /* This fails on Solaris. */
432 if (strcmp ("$LOCALE_FR_UTF8", "none") != 0
433 && setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
434 {
435 char input[] = "B\303\274\303\237er"; /* "Büßer" */
436 mbstate_t state;
437 wchar_t wc;
438
439 memset (&state, '\0', sizeof (mbstate_t));
440 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
441 {
442 input[1] = '\0';
443 if (mbrtowc (&wc, input + 2, 5, &state) != 1)
444 result |= 1;
445 }
446 found_some_locale = 1;
447 }
448 /* This fails on HP-UX 11.11. */
449 if (strcmp ("$LOCALE_JA", "none") != 0
450 && setlocale (LC_ALL, "$LOCALE_JA") != NULL)
451 {
452 char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
453 mbstate_t state;
454 wchar_t wc;
455
456 memset (&state, '\0', sizeof (mbstate_t));
457 if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
458 {
459 input[1] = '\0';
460 if (mbrtowc (&wc, input + 2, 5, &state) != 2)
461 result |= 2;
462 }
463 found_some_locale = 1;
464 }
465 /* This fails on native Windows. */
466 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
467 {
468 char input[] = "<\223\372\226\173\214\352>"; /* "<日本語>" */
469 mbstate_t state;
470 wchar_t wc;
471
472 memset (&state, '\0', sizeof (mbstate_t));
473 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
474 {
475 input[3] = '\0';
476 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
477 result |= 4;
478 }
479 found_some_locale = 1;
480 }
481 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
482 {
483 char input[] = "<\244\351\245\273\273\171>"; /* "<日本語>" */
484 mbstate_t state;
485 wchar_t wc;
486
487 memset (&state, '\0', sizeof (mbstate_t));
488 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
489 {
490 input[3] = '\0';
491 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
492 result |= 8;
493 }
494 found_some_locale = 1;
495 }
496 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
497 {
498 char input[] = "<\310\325\261\276\325\132>"; /* "<日本語>" */
499 mbstate_t state;
500 wchar_t wc;
501
502 memset (&state, '\0', sizeof (mbstate_t));
503 if (mbrtowc (&wc, input + 3, 1, &state) == (size_t)(-2))
504 {
505 input[3] = '\0';
506 if (mbrtowc (&wc, input + 4, 4, &state) != 1)
507 result |= 16;
508 }
509 found_some_locale = 1;
510 }
511 return (found_some_locale ? result : 77);
512 }]])],
513 [gl_cv_func_mbrtowc_retval=yes],
514 [if test $? != 77; then
515 gl_cv_func_mbrtowc_retval=no
516 fi
517 ],
518 [:])
519 fi
520 ])
521 ])
522
523 dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
524 dnl Result is gl_cv_func_mbrtowc_nul_retval.
525
526 AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
527 [
528 AC_REQUIRE([AC_PROG_CC])
529 AC_REQUIRE([gt_LOCALE_ZH_CN])
530 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
531 AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
532 [gl_cv_func_mbrtowc_nul_retval],
533 [
534 dnl Initial guess, used when cross-compiling or when no suitable locale
535 dnl is present.
536 changequote(,)dnl
537 case "$host_os" in
538 # Guess no on Solaris 8 and 9.
539 solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
540 # Guess yes otherwise.
541 *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
542 esac
543 changequote([,])dnl
544 if test $LOCALE_ZH_CN != none; then
545 AC_RUN_IFELSE(
546 [AC_LANG_SOURCE([[
547 #include <locale.h>
548 #include <string.h>
549 #include <wchar.h>
550 int main ()
551 {
552 /* This fails on Solaris 8 and 9. */
553 if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
554 {
555 mbstate_t state;
556 wchar_t wc;
557
558 memset (&state, '\0', sizeof (mbstate_t));
559 if (mbrtowc (&wc, "", 1, &state) != 0)
560 return 2;
561 }
562 return 0;
563 }]])],
564 [gl_cv_func_mbrtowc_nul_retval=yes],
565 [gl_cv_func_mbrtowc_nul_retval=no],
566 [:])
567 fi
568 ])
569 ])
570
571 dnl Test whether mbrtowc stores a wide character when reporting incomplete
572 dnl input.
573
574 AC_DEFUN([gl_MBRTOWC_STORES_INCOMPLETE],
575 [
576 AC_REQUIRE([AC_PROG_CC])
577 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
578 AC_CACHE_CHECK([whether mbrtowc stores incomplete characters],
579 [gl_cv_func_mbrtowc_stores_incomplete],
580 [
581 dnl Initial guess, used when cross-compiling or when no suitable locale
582 dnl is present.
583 changequote(,)dnl
584 case "$host_os" in
585 # Guess yes on native Windows.
586 mingw*) gl_cv_func_mbrtowc_stores_incomplete="guessing yes" ;;
587 *) gl_cv_func_mbrtowc_stores_incomplete="guessing no" ;;
588 esac
589 changequote([,])dnl
590 case "$host_os" in
591 mingw*)
592 AC_RUN_IFELSE(
593 [AC_LANG_SOURCE([[
594 #include <locale.h>
595 #include <string.h>
596 #include <wchar.h>
597 int main ()
598 {
599 int result = 0;
600 if (setlocale (LC_ALL, "French_France.65001") != NULL)
601 {
602 wchar_t wc = (wchar_t) 0xBADFACE;
603 mbstate_t state;
604
605 memset (&state, '\0', sizeof (mbstate_t));
606 if (mbrtowc (&wc, "\303", 1, &state) == (size_t)(-2)
607 && wc != (wchar_t) 0xBADFACE)
608 result |= 1;
609 }
610 if (setlocale (LC_ALL, "Japanese_Japan.932") != NULL)
611 {
612 wchar_t wc = (wchar_t) 0xBADFACE;
613 mbstate_t state;
614
615 memset (&state, '\0', sizeof (mbstate_t));
616 if (mbrtowc (&wc, "\226", 1, &state) == (size_t)(-2)
617 && wc != (wchar_t) 0xBADFACE)
618 result |= 2;
619 }
620 if (setlocale (LC_ALL, "Chinese_Taiwan.950") != NULL)
621 {
622 wchar_t wc = (wchar_t) 0xBADFACE;
623 mbstate_t state;
624
625 memset (&state, '\0', sizeof (mbstate_t));
626 if (mbrtowc (&wc, "\245", 1, &state) == (size_t)(-2)
627 && wc != (wchar_t) 0xBADFACE)
628 result |= 4;
629 }
630 if (setlocale (LC_ALL, "Chinese_China.936") != NULL)
631 {
632 wchar_t wc = (wchar_t) 0xBADFACE;
633 mbstate_t state;
634
635 memset (&state, '\0', sizeof (mbstate_t));
636 if (mbrtowc (&wc, "\261", 1, &state) == (size_t)(-2)
637 && wc != (wchar_t) 0xBADFACE)
638 result |= 8;
639 }
640 return result;
641 }]])],
642 [gl_cv_func_mbrtowc_stores_incomplete=no],
643 [gl_cv_func_mbrtowc_stores_incomplete=yes],
644 [:])
645 ;;
646 *)
647 AC_REQUIRE([gt_LOCALE_FR_UTF8])
648 if test $LOCALE_FR_UTF8 != none; then
649 AC_RUN_IFELSE(
650 [AC_LANG_SOURCE([[
651 #include <locale.h>
652 #include <string.h>
653 #include <wchar.h>
654 int main ()
655 {
656 if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
657 {
658 wchar_t wc = (wchar_t) 0xBADFACE;
659 mbstate_t state;
660
661 memset (&state, '\0', sizeof (mbstate_t));
662 if (mbrtowc (&wc, "\303", 1, &state) == (size_t)(-2)
663 && wc != (wchar_t) 0xBADFACE)
664 return 1;
665 }
666 return 0;
667 }]])],
668 [gl_cv_func_mbrtowc_stores_incomplete=no],
669 [gl_cv_func_mbrtowc_stores_incomplete=yes],
670 [:])
671 fi
672 ;;
673 esac
674 ])
675 ])
676
677 dnl Test whether mbrtowc returns the correct value on empty input.
678
679 AC_DEFUN([gl_MBRTOWC_EMPTY_INPUT],
680 [
681 AC_REQUIRE([AC_PROG_CC])
682 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
683 AC_CACHE_CHECK([whether mbrtowc works on empty input],
684 [gl_cv_func_mbrtowc_empty_input],
685 [AC_RUN_IFELSE(
686 [AC_LANG_SOURCE([[
687 #include <wchar.h>
688 static wchar_t wc;
689 static mbstate_t mbs;
690 int
691 main (void)
692 {
693 return mbrtowc (&wc, "", 0, &mbs) != (size_t) -2;
694 }]])],
695 [gl_cv_func_mbrtowc_empty_input=yes],
696 [gl_cv_func_mbrtowc_empty_input=no],
697 [case "$host_os" in
698 # Guess no on AIX and glibc systems.
699 aix* | *-gnu* | gnu*) gl_cv_func_mbrtowc_empty_input="guessing no" ;;
700 # Guess no on Android.
701 linux*-android*) gl_cv_func_mbrtowc_empty_input="guessing no" ;;
702 # Guess no on native Windows.
703 mingw*) gl_cv_func_mbrtowc_empty_input="guessing no" ;;
704 *) gl_cv_func_mbrtowc_empty_input="guessing yes" ;;
705 esac
706 ])
707 ])
708 ])
709
710 dnl Test whether mbrtowc reports encoding errors in the C locale.
711 dnl Although POSIX was never intended to allow this, the GNU C Library
712 dnl and other implementations do it. See:
713 dnl https://sourceware.org/bugzilla/show_bug.cgi?id=19932
714 dnl POSIX has now clarified it:
715 dnl <https://pubs.opengroup.org/onlinepubs/9699919799/functions/mbrtowc.html>
716 dnl says: "In the POSIX locale an [EILSEQ] error cannot occur since all byte
717 dnl values are valid characters."
718
719 AC_DEFUN([gl_MBRTOWC_C_LOCALE],
720 [
721 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
722 AC_CACHE_CHECK([whether the C locale is free of encoding errors],
723 [gl_cv_func_mbrtowc_C_locale_sans_EILSEQ],
724 [AC_RUN_IFELSE(
725 [AC_LANG_PROGRAM(
726 [[#include <limits.h>
727 #include <locale.h>
728 #include <wchar.h>
729 ]], [[
730 int i;
731 char *locale = setlocale (LC_ALL, "C");
732 if (! locale)
733 return 2;
734 for (i = CHAR_MIN; i <= CHAR_MAX; i++)
735 {
736 char c = i;
737 wchar_t wc;
738 mbstate_t mbs = { 0, };
739 size_t ss = mbrtowc (&wc, &c, 1, &mbs);
740 if (1 < ss)
741 return 3;
742 }
743 return 0;
744 ]])],
745 [gl_cv_func_mbrtowc_C_locale_sans_EILSEQ=yes],
746 [gl_cv_func_mbrtowc_C_locale_sans_EILSEQ=no],
747 [case "$host_os" in
748 # Guess yes on native Windows.
749 mingw*) gl_cv_func_mbrtowc_C_locale_sans_EILSEQ="guessing yes" ;;
750 *) gl_cv_func_mbrtowc_C_locale_sans_EILSEQ="$gl_cross_guess_normal" ;;
751 esac
752 ])
753 ])
754 ])
755
756 # Prerequisites of lib/mbrtowc.c and lib/lc-charset-dispatch.c.
757 AC_DEFUN([gl_PREREQ_MBRTOWC], [
758 AC_REQUIRE([AC_C_INLINE])
759 :
760 ])
761
762 # Prerequisites of lib/mbtowc-lock.c.
763 AC_DEFUN([gl_PREREQ_MBTOWC_LOCK],
764 [
765 gl_VISIBILITY
766 ])
767
768
769 dnl From Paul Eggert
770
771 dnl This is an override of an autoconf macro.
772
773 AC_DEFUN([AC_FUNC_MBRTOWC],
774 [
775 dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
776 AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
777 [gl_cv_func_mbrtowc],
778 [AC_LINK_IFELSE(
779 [AC_LANG_PROGRAM(
780 [[#include <wchar.h>]],
781 [[wchar_t wc;
782 char const s[] = "";
783 size_t n = 1;
784 mbstate_t state;
785 return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
786 [gl_cv_func_mbrtowc=yes],
787 [gl_cv_func_mbrtowc=no])])
788 if test $gl_cv_func_mbrtowc = yes; then
789 AC_DEFINE([HAVE_MBRTOWC], [1],
790 [Define to 1 if mbrtowc and mbstate_t are properly declared.])
791 fi
792 ])