mbrtowc.m4 23 KB

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