Debian Codegraph @

m4/mbrtowc.m4

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