mbrtowc.m4 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. # mbrtowc.m4 serial 17
  2. dnl Copyright (C) 2001-2002, 2004-2005, 2008-2010 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([AC_TYPE_MBSTATE_T])
  11. gl_MBSTATE_T_BROKEN
  12. AC_CHECK_FUNCS_ONCE([mbrtowc])
  13. if test $ac_cv_func_mbrtowc = no; then
  14. HAVE_MBRTOWC=0
  15. else
  16. if test $REPLACE_MBSTATE_T = 1; then
  17. REPLACE_MBRTOWC=1
  18. else
  19. gl_MBRTOWC_NULL_ARG
  20. gl_MBRTOWC_RETVAL
  21. gl_MBRTOWC_NUL_RETVAL
  22. case "$gl_cv_func_mbrtowc_null_arg" in
  23. *yes) ;;
  24. *) AC_DEFINE([MBRTOWC_NULL_ARG_BUG], [1],
  25. [Define if the mbrtowc function has the NULL string argument bug.])
  26. REPLACE_MBRTOWC=1
  27. ;;
  28. esac
  29. case "$gl_cv_func_mbrtowc_retval" in
  30. *yes) ;;
  31. *) AC_DEFINE([MBRTOWC_RETVAL_BUG], [1],
  32. [Define if the mbrtowc function returns a wrong return value.])
  33. REPLACE_MBRTOWC=1
  34. ;;
  35. esac
  36. case "$gl_cv_func_mbrtowc_nul_retval" in
  37. *yes) ;;
  38. *) AC_DEFINE([MBRTOWC_NUL_RETVAL_BUG], [1],
  39. [Define if the mbrtowc function does not return 0 for a NUL character.])
  40. REPLACE_MBRTOWC=1
  41. ;;
  42. esac
  43. fi
  44. fi
  45. if test $HAVE_MBRTOWC = 0 || test $REPLACE_MBRTOWC = 1; then
  46. gl_REPLACE_WCHAR_H
  47. AC_LIBOBJ([mbrtowc])
  48. gl_PREREQ_MBRTOWC
  49. fi
  50. ])
  51. dnl Test whether mbsinit() and mbrtowc() need to be overridden in a way that
  52. dnl redefines the semantics of the given mbstate_t type.
  53. dnl Result is REPLACE_MBSTATE_T.
  54. dnl When this is set to 1, we replace both mbsinit() and mbrtowc(), in order to
  55. dnl avoid inconsistencies.
  56. AC_DEFUN([gl_MBSTATE_T_BROKEN],
  57. [
  58. AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
  59. AC_REQUIRE([AC_TYPE_MBSTATE_T])
  60. AC_CHECK_FUNCS_ONCE([mbsinit])
  61. AC_CHECK_FUNCS_ONCE([mbrtowc])
  62. if test $ac_cv_func_mbsinit = yes && test $ac_cv_func_mbrtowc = yes; then
  63. gl_MBRTOWC_INCOMPLETE_STATE
  64. gl_MBRTOWC_SANITYCHECK
  65. REPLACE_MBSTATE_T=0
  66. case "$gl_cv_func_mbrtowc_incomplete_state" in
  67. *yes) ;;
  68. *) REPLACE_MBSTATE_T=1 ;;
  69. esac
  70. case "$gl_cv_func_mbrtowc_sanitycheck" in
  71. *yes) ;;
  72. *) REPLACE_MBSTATE_T=1 ;;
  73. esac
  74. else
  75. REPLACE_MBSTATE_T=1
  76. fi
  77. if test $REPLACE_MBSTATE_T = 1; then
  78. gl_REPLACE_WCHAR_H
  79. fi
  80. ])
  81. dnl Test whether mbrtowc puts the state into non-initial state when parsing an
  82. dnl incomplete multibyte character.
  83. dnl Result is gl_cv_func_mbrtowc_incomplete_state.
  84. AC_DEFUN([gl_MBRTOWC_INCOMPLETE_STATE],
  85. [
  86. AC_REQUIRE([AC_PROG_CC])
  87. AC_REQUIRE([gt_LOCALE_JA])
  88. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  89. AC_CACHE_CHECK([whether mbrtowc handles incomplete characters],
  90. [gl_cv_func_mbrtowc_incomplete_state],
  91. [
  92. dnl Initial guess, used when cross-compiling or when no suitable locale
  93. dnl is present.
  94. changequote(,)dnl
  95. case "$host_os" in
  96. # Guess no on AIX and OSF/1.
  97. osf*) gl_cv_func_mbrtowc_incomplete_state="guessing no" ;;
  98. # Guess yes otherwise.
  99. *) gl_cv_func_mbrtowc_incomplete_state="guessing yes" ;;
  100. esac
  101. changequote([,])dnl
  102. if test $LOCALE_JA != none; then
  103. AC_TRY_RUN([
  104. #include <locale.h>
  105. #include <string.h>
  106. #include <wchar.h>
  107. int main ()
  108. {
  109. if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
  110. {
  111. const char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
  112. mbstate_t state;
  113. wchar_t wc;
  114. memset (&state, '\0', sizeof (mbstate_t));
  115. if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
  116. if (mbsinit (&state))
  117. return 1;
  118. }
  119. return 0;
  120. }],
  121. [gl_cv_func_mbrtowc_incomplete_state=yes],
  122. [gl_cv_func_mbrtowc_incomplete_state=no],
  123. [:])
  124. fi
  125. ])
  126. ])
  127. dnl Test whether mbrtowc works not worse than mbtowc.
  128. dnl Result is gl_cv_func_mbrtowc_sanitycheck.
  129. AC_DEFUN([gl_MBRTOWC_SANITYCHECK],
  130. [
  131. AC_REQUIRE([AC_PROG_CC])
  132. AC_REQUIRE([gt_LOCALE_ZH_CN])
  133. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  134. AC_CACHE_CHECK([whether mbrtowc works as well as mbtowc],
  135. [gl_cv_func_mbrtowc_sanitycheck],
  136. [
  137. dnl Initial guess, used when cross-compiling or when no suitable locale
  138. dnl is present.
  139. changequote(,)dnl
  140. case "$host_os" in
  141. # Guess no on Solaris 8.
  142. solaris2.8) gl_cv_func_mbrtowc_sanitycheck="guessing no" ;;
  143. # Guess yes otherwise.
  144. *) gl_cv_func_mbrtowc_sanitycheck="guessing yes" ;;
  145. esac
  146. changequote([,])dnl
  147. if test $LOCALE_ZH_CN != none; then
  148. AC_TRY_RUN([
  149. #include <locale.h>
  150. #include <stdlib.h>
  151. #include <string.h>
  152. #include <wchar.h>
  153. int main ()
  154. {
  155. /* This fails on Solaris 8:
  156. mbrtowc returns 2, and sets wc to 0x00F0.
  157. mbtowc returns 4 (correct) and sets wc to 0x5EDC. */
  158. if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
  159. {
  160. char input[] = "B\250\271\201\060\211\070er"; /* "Büßer" */
  161. mbstate_t state;
  162. wchar_t wc;
  163. memset (&state, '\0', sizeof (mbstate_t));
  164. if (mbrtowc (&wc, input + 3, 6, &state) != 4
  165. && mbtowc (&wc, input + 3, 6) == 4)
  166. return 1;
  167. }
  168. return 0;
  169. }],
  170. [gl_cv_func_mbrtowc_sanitycheck=yes],
  171. [gl_cv_func_mbrtowc_sanitycheck=no],
  172. [:])
  173. fi
  174. ])
  175. ])
  176. dnl Test whether mbrtowc supports a NULL string argument correctly.
  177. dnl Result is gl_cv_func_mbrtowc_null_arg.
  178. AC_DEFUN([gl_MBRTOWC_NULL_ARG],
  179. [
  180. AC_REQUIRE([AC_PROG_CC])
  181. AC_REQUIRE([gt_LOCALE_FR_UTF8])
  182. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  183. AC_CACHE_CHECK([whether mbrtowc handles a NULL string argument],
  184. [gl_cv_func_mbrtowc_null_arg],
  185. [
  186. dnl Initial guess, used when cross-compiling or when no suitable locale
  187. dnl is present.
  188. changequote(,)dnl
  189. case "$host_os" in
  190. # Guess no on OSF/1.
  191. osf*) gl_cv_func_mbrtowc_null_arg="guessing no" ;;
  192. # Guess yes otherwise.
  193. *) gl_cv_func_mbrtowc_null_arg="guessing yes" ;;
  194. esac
  195. changequote([,])dnl
  196. if test $LOCALE_FR_UTF8 != none; then
  197. AC_TRY_RUN([
  198. #include <locale.h>
  199. #include <string.h>
  200. #include <wchar.h>
  201. int main ()
  202. {
  203. if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
  204. {
  205. mbstate_t state;
  206. wchar_t wc;
  207. int ret;
  208. memset (&state, '\0', sizeof (mbstate_t));
  209. wc = (wchar_t) 0xBADFACE;
  210. mbrtowc (&wc, NULL, 5, &state);
  211. /* Check that wc was not modified. */
  212. if (wc != (wchar_t) 0xBADFACE)
  213. return 1;
  214. }
  215. return 0;
  216. }], [gl_cv_func_mbrtowc_null_arg=yes], [gl_cv_func_mbrtowc_null_arg=no], [:])
  217. fi
  218. ])
  219. ])
  220. dnl Test whether mbrtowc, when parsing the end of a multibyte character,
  221. dnl correctly returns the number of bytes that were needed to complete the
  222. dnl character (not the total number of bytes of the multibyte character).
  223. dnl Result is gl_cv_func_mbrtowc_retval.
  224. AC_DEFUN([gl_MBRTOWC_RETVAL],
  225. [
  226. AC_REQUIRE([AC_PROG_CC])
  227. AC_REQUIRE([gt_LOCALE_FR_UTF8])
  228. AC_REQUIRE([gt_LOCALE_JA])
  229. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  230. AC_CACHE_CHECK([whether mbrtowc has a correct return value],
  231. [gl_cv_func_mbrtowc_retval],
  232. [
  233. dnl Initial guess, used when cross-compiling or when no suitable locale
  234. dnl is present.
  235. changequote(,)dnl
  236. case "$host_os" in
  237. # Guess no on HP-UX and Solaris.
  238. hpux* | solaris*) gl_cv_func_mbrtowc_retval="guessing no" ;;
  239. # Guess yes otherwise.
  240. *) gl_cv_func_mbrtowc_retval="guessing yes" ;;
  241. esac
  242. changequote([,])dnl
  243. if test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none; then
  244. AC_TRY_RUN([
  245. #include <locale.h>
  246. #include <string.h>
  247. #include <wchar.h>
  248. int main ()
  249. {
  250. /* This fails on Solaris. */
  251. if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
  252. {
  253. char input[] = "B\303\274\303\237er"; /* "Büßer" */
  254. mbstate_t state;
  255. wchar_t wc;
  256. memset (&state, '\0', sizeof (mbstate_t));
  257. if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
  258. {
  259. input[1] = '\0';
  260. if (mbrtowc (&wc, input + 2, 5, &state) != 1)
  261. return 1;
  262. }
  263. }
  264. /* This fails on HP-UX 11.11. */
  265. if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
  266. {
  267. char input[] = "B\217\253\344\217\251\316er"; /* "Büßer" */
  268. mbstate_t state;
  269. wchar_t wc;
  270. memset (&state, '\0', sizeof (mbstate_t));
  271. if (mbrtowc (&wc, input + 1, 1, &state) == (size_t)(-2))
  272. {
  273. input[1] = '\0';
  274. if (mbrtowc (&wc, input + 2, 5, &state) != 2)
  275. return 1;
  276. }
  277. }
  278. return 0;
  279. }],
  280. [gl_cv_func_mbrtowc_retval=yes],
  281. [gl_cv_func_mbrtowc_retval=no],
  282. [:])
  283. fi
  284. ])
  285. ])
  286. dnl Test whether mbrtowc, when parsing a NUL character, correctly returns 0.
  287. dnl Result is gl_cv_func_mbrtowc_nul_retval.
  288. AC_DEFUN([gl_MBRTOWC_NUL_RETVAL],
  289. [
  290. AC_REQUIRE([AC_PROG_CC])
  291. AC_REQUIRE([gt_LOCALE_ZH_CN])
  292. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  293. AC_CACHE_CHECK([whether mbrtowc returns 0 when parsing a NUL character],
  294. [gl_cv_func_mbrtowc_nul_retval],
  295. [
  296. dnl Initial guess, used when cross-compiling or when no suitable locale
  297. dnl is present.
  298. changequote(,)dnl
  299. case "$host_os" in
  300. # Guess no on Solaris 8 and 9.
  301. solaris2.[89]) gl_cv_func_mbrtowc_nul_retval="guessing no" ;;
  302. # Guess yes otherwise.
  303. *) gl_cv_func_mbrtowc_nul_retval="guessing yes" ;;
  304. esac
  305. changequote([,])dnl
  306. if test $LOCALE_ZH_CN != none; then
  307. AC_TRY_RUN([
  308. #include <locale.h>
  309. #include <string.h>
  310. #include <wchar.h>
  311. int main ()
  312. {
  313. /* This fails on Solaris 8 and 9. */
  314. if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
  315. {
  316. mbstate_t state;
  317. wchar_t wc;
  318. memset (&state, '\0', sizeof (mbstate_t));
  319. if (mbrtowc (&wc, "", 1, &state) != 0)
  320. return 1;
  321. }
  322. return 0;
  323. }],
  324. [gl_cv_func_mbrtowc_nul_retval=yes],
  325. [gl_cv_func_mbrtowc_nul_retval=no],
  326. [:])
  327. fi
  328. ])
  329. ])
  330. # Prerequisites of lib/mbrtowc.c.
  331. AC_DEFUN([gl_PREREQ_MBRTOWC], [
  332. :
  333. ])
  334. dnl From Paul Eggert
  335. dnl This override of an autoconf macro can be removed when autoconf 2.60 or
  336. dnl newer can be assumed everywhere.
  337. m4_if(m4_version_compare(m4_defn([m4_PACKAGE_VERSION]),[2.60]),[-1],[
  338. AC_DEFUN([AC_FUNC_MBRTOWC],
  339. [
  340. dnl Same as AC_FUNC_MBRTOWC in autoconf-2.60.
  341. AC_CACHE_CHECK([whether mbrtowc and mbstate_t are properly declared],
  342. gl_cv_func_mbrtowc,
  343. [AC_LINK_IFELSE(
  344. [AC_LANG_PROGRAM(
  345. [[#include <wchar.h>]],
  346. [[wchar_t wc;
  347. char const s[] = "";
  348. size_t n = 1;
  349. mbstate_t state;
  350. return ! (sizeof state && (mbrtowc) (&wc, s, n, &state));]])],
  351. gl_cv_func_mbrtowc=yes,
  352. gl_cv_func_mbrtowc=no)])
  353. if test $gl_cv_func_mbrtowc = yes; then
  354. AC_DEFINE([HAVE_MBRTOWC], [1],
  355. [Define to 1 if mbrtowc and mbstate_t are properly declared.])
  356. fi
  357. ])
  358. ])