mbrtowc.m4 11 KB

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