iconv.m4 9.2 KB

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