mbrtowc.m4 9.3 KB

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