Debian Codegraph @

m4/mbrtowc.m4

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