mbrtowc.m4 23 KB

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