2
0

iconv.m4 9.5 KB

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