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