iconv.m4 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. # iconv.m4 serial 18 (gettext-0.18.2)
  2. dnl Copyright (C) 2000-2002, 2007-2013 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. dnl From Bruno Haible.
  7. AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
  8. [
  9. dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
  10. dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
  11. dnl accordingly.
  12. AC_LIB_LINKFLAGS_BODY([iconv])
  13. ])
  14. AC_DEFUN([AM_ICONV_LINK],
  15. [
  16. dnl Some systems have iconv in libc, some have it in libiconv (OSF/1 and
  17. dnl those with the standalone portable GNU libiconv installed).
  18. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  19. dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
  20. dnl accordingly.
  21. AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
  22. dnl Add $INCICONV to CPPFLAGS before performing the following checks,
  23. dnl because if the user has installed libiconv and not disabled its use
  24. dnl via --without-libiconv-prefix, he wants to use it. The first
  25. dnl AC_LINK_IFELSE will then fail, the second AC_LINK_IFELSE will succeed.
  26. am_save_CPPFLAGS="$CPPFLAGS"
  27. AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
  28. AC_CACHE_CHECK([for iconv], [am_cv_func_iconv], [
  29. am_cv_func_iconv="no, consider installing GNU libiconv"
  30. am_cv_lib_iconv=no
  31. AC_LINK_IFELSE(
  32. [AC_LANG_PROGRAM(
  33. [[
  34. #include <stdlib.h>
  35. #include <iconv.h>
  36. ]],
  37. [[iconv_t cd = iconv_open("","");
  38. iconv(cd,NULL,NULL,NULL,NULL);
  39. iconv_close(cd);]])],
  40. [am_cv_func_iconv=yes])
  41. if test "$am_cv_func_iconv" != yes; then
  42. am_save_LIBS="$LIBS"
  43. LIBS="$LIBS $LIBICONV"
  44. AC_LINK_IFELSE(
  45. [AC_LANG_PROGRAM(
  46. [[
  47. #include <stdlib.h>
  48. #include <iconv.h>
  49. ]],
  50. [[iconv_t cd = iconv_open("","");
  51. iconv(cd,NULL,NULL,NULL,NULL);
  52. iconv_close(cd);]])],
  53. [am_cv_lib_iconv=yes]
  54. [am_cv_func_iconv=yes])
  55. LIBS="$am_save_LIBS"
  56. fi
  57. ])
  58. if test "$am_cv_func_iconv" = yes; then
  59. AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
  60. dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
  61. dnl Solaris 10.
  62. am_save_LIBS="$LIBS"
  63. if test $am_cv_lib_iconv = yes; then
  64. LIBS="$LIBS $LIBICONV"
  65. fi
  66. AC_RUN_IFELSE(
  67. [AC_LANG_SOURCE([[
  68. #include <iconv.h>
  69. #include <string.h>
  70. int main ()
  71. {
  72. int result = 0;
  73. /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
  74. returns. */
  75. {
  76. iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
  77. if (cd_utf8_to_88591 != (iconv_t)(-1))
  78. {
  79. static const char input[] = "\342\202\254"; /* EURO SIGN */
  80. char buf[10];
  81. const char *inptr = input;
  82. size_t inbytesleft = strlen (input);
  83. char *outptr = buf;
  84. size_t outbytesleft = sizeof (buf);
  85. size_t res = iconv (cd_utf8_to_88591,
  86. (char **) &inptr, &inbytesleft,
  87. &outptr, &outbytesleft);
  88. if (res == 0)
  89. result |= 1;
  90. iconv_close (cd_utf8_to_88591);
  91. }
  92. }
  93. /* Test against Solaris 10 bug: Failures are not distinguishable from
  94. successful returns. */
  95. {
  96. iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
  97. if (cd_ascii_to_88591 != (iconv_t)(-1))
  98. {
  99. static const char input[] = "\263";
  100. char buf[10];
  101. const char *inptr = input;
  102. size_t inbytesleft = strlen (input);
  103. char *outptr = buf;
  104. size_t outbytesleft = sizeof (buf);
  105. size_t res = iconv (cd_ascii_to_88591,
  106. (char **) &inptr, &inbytesleft,
  107. &outptr, &outbytesleft);
  108. if (res == 0)
  109. result |= 2;
  110. iconv_close (cd_ascii_to_88591);
  111. }
  112. }
  113. /* Test against AIX 6.1..7.1 bug: Buffer overrun. */
  114. {
  115. iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
  116. if (cd_88591_to_utf8 != (iconv_t)(-1))
  117. {
  118. static const char input[] = "\304";
  119. static char buf[2] = { (char)0xDE, (char)0xAD };
  120. const char *inptr = input;
  121. size_t inbytesleft = 1;
  122. char *outptr = buf;
  123. size_t outbytesleft = 1;
  124. size_t res = iconv (cd_88591_to_utf8,
  125. (char **) &inptr, &inbytesleft,
  126. &outptr, &outbytesleft);
  127. if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
  128. result |= 4;
  129. iconv_close (cd_88591_to_utf8);
  130. }
  131. }
  132. #if 0 /* This bug could be worked around by the caller. */
  133. /* Test against HP-UX 11.11 bug: Positive return value instead of 0. */
  134. {
  135. iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
  136. if (cd_88591_to_utf8 != (iconv_t)(-1))
  137. {
  138. static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
  139. char buf[50];
  140. const char *inptr = input;
  141. size_t inbytesleft = strlen (input);
  142. char *outptr = buf;
  143. size_t outbytesleft = sizeof (buf);
  144. size_t res = iconv (cd_88591_to_utf8,
  145. (char **) &inptr, &inbytesleft,
  146. &outptr, &outbytesleft);
  147. if ((int)res > 0)
  148. result |= 8;
  149. iconv_close (cd_88591_to_utf8);
  150. }
  151. }
  152. #endif
  153. /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
  154. provided. */
  155. if (/* Try standardized names. */
  156. iconv_open ("UTF-8", "EUC-JP") == (iconv_t)(-1)
  157. /* Try IRIX, OSF/1 names. */
  158. && iconv_open ("UTF-8", "eucJP") == (iconv_t)(-1)
  159. /* Try AIX names. */
  160. && iconv_open ("UTF-8", "IBM-eucJP") == (iconv_t)(-1)
  161. /* Try HP-UX names. */
  162. && iconv_open ("utf8", "eucJP") == (iconv_t)(-1))
  163. result |= 16;
  164. return result;
  165. }]])],
  166. [am_cv_func_iconv_works=yes],
  167. [am_cv_func_iconv_works=no],
  168. [
  169. changequote(,)dnl
  170. case "$host_os" in
  171. aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
  172. *) am_cv_func_iconv_works="guessing yes" ;;
  173. esac
  174. changequote([,])dnl
  175. ])
  176. LIBS="$am_save_LIBS"
  177. ])
  178. case "$am_cv_func_iconv_works" in
  179. *no) am_func_iconv=no am_cv_lib_iconv=no ;;
  180. *) am_func_iconv=yes ;;
  181. esac
  182. else
  183. am_func_iconv=no am_cv_lib_iconv=no
  184. fi
  185. if test "$am_func_iconv" = yes; then
  186. AC_DEFINE([HAVE_ICONV], [1],
  187. [Define if you have the iconv() function and it works.])
  188. fi
  189. if test "$am_cv_lib_iconv" = yes; then
  190. AC_MSG_CHECKING([how to link with libiconv])
  191. AC_MSG_RESULT([$LIBICONV])
  192. else
  193. dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
  194. dnl either.
  195. CPPFLAGS="$am_save_CPPFLAGS"
  196. LIBICONV=
  197. LTLIBICONV=
  198. fi
  199. AC_SUBST([LIBICONV])
  200. AC_SUBST([LTLIBICONV])
  201. ])
  202. dnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
  203. dnl avoid warnings like
  204. dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
  205. dnl This is tricky because of the way 'aclocal' is implemented:
  206. dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
  207. dnl Otherwise aclocal's initial scan pass would miss the macro definition.
  208. dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
  209. dnl Otherwise aclocal would emit many "Use of uninitialized value $1"
  210. dnl warnings.
  211. m4_define([gl_iconv_AC_DEFUN],
  212. m4_version_prereq([2.64],
  213. [[AC_DEFUN_ONCE(
  214. [$1], [$2])]],
  215. [m4_ifdef([gl_00GNULIB],
  216. [[AC_DEFUN_ONCE(
  217. [$1], [$2])]],
  218. [[AC_DEFUN(
  219. [$1], [$2])]])]))
  220. gl_iconv_AC_DEFUN([AM_ICONV],
  221. [
  222. AM_ICONV_LINK
  223. if test "$am_cv_func_iconv" = yes; then
  224. AC_MSG_CHECKING([for iconv declaration])
  225. AC_CACHE_VAL([am_cv_proto_iconv], [
  226. AC_COMPILE_IFELSE(
  227. [AC_LANG_PROGRAM(
  228. [[
  229. #include <stdlib.h>
  230. #include <iconv.h>
  231. extern
  232. #ifdef __cplusplus
  233. "C"
  234. #endif
  235. #if defined(__STDC__) || defined(_MSC_VER) || defined(__cplusplus)
  236. size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
  237. #else
  238. size_t iconv();
  239. #endif
  240. ]],
  241. [[]])],
  242. [am_cv_proto_iconv_arg1=""],
  243. [am_cv_proto_iconv_arg1="const"])
  244. am_cv_proto_iconv="extern size_t iconv (iconv_t cd, $am_cv_proto_iconv_arg1 char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);"])
  245. am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
  246. AC_MSG_RESULT([
  247. $am_cv_proto_iconv])
  248. AC_DEFINE_UNQUOTED([ICONV_CONST], [$am_cv_proto_iconv_arg1],
  249. [Define as const if the declaration of iconv() needs const.])
  250. dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
  251. m4_ifdef([gl_ICONV_H_DEFAULTS],
  252. [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
  253. if test -n "$am_cv_proto_iconv_arg1"; then
  254. ICONV_CONST="const"
  255. fi
  256. ])
  257. fi
  258. ])