1# iconv.m4 serial 23
2dnl Copyright (C) 2000-2002, 2007-2014, 2016-2020 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 bug: Failures are not distinguishable from successful
96     returns.  */
97  {
98    iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
99    if (cd_utf8_to_88591 != (iconv_t)(-1))
100      {
101        static ICONV_CONST char input[] = "\342\202\254"; /* EURO SIGN */
102        char buf[10];
103        ICONV_CONST char *inptr = input;
104        size_t inbytesleft = strlen (input);
105        char *outptr = buf;
106        size_t outbytesleft = sizeof (buf);
107        size_t res = iconv (cd_utf8_to_88591,
108                            &inptr, &inbytesleft,
109                            &outptr, &outbytesleft);
110        if (res == 0)
111          result |= 1;
112        iconv_close (cd_utf8_to_88591);
113      }
114  }
115  /* Test against Solaris 10 bug: Failures are not distinguishable from
116     successful returns.  */
117  {
118    iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
119    if (cd_ascii_to_88591 != (iconv_t)(-1))
120      {
121        static ICONV_CONST char input[] = "\263";
122        char buf[10];
123        ICONV_CONST char *inptr = input;
124        size_t inbytesleft = strlen (input);
125        char *outptr = buf;
126        size_t outbytesleft = sizeof (buf);
127        size_t res = iconv (cd_ascii_to_88591,
128                            &inptr, &inbytesleft,
129                            &outptr, &outbytesleft);
130        if (res == 0)
131          result |= 2;
132        iconv_close (cd_ascii_to_88591);
133      }
134  }
135  /* Test against AIX 6.1..7.1 bug: Buffer overrun.  */
136  {
137    iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
138    if (cd_88591_to_utf8 != (iconv_t)(-1))
139      {
140        static ICONV_CONST char input[] = "\304";
141        static char buf[2] = { (char)0xDE, (char)0xAD };
142        ICONV_CONST char *inptr = input;
143        size_t inbytesleft = 1;
144        char *outptr = buf;
145        size_t outbytesleft = 1;
146        size_t res = iconv (cd_88591_to_utf8,
147                            &inptr, &inbytesleft,
148                            &outptr, &outbytesleft);
149        if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
150          result |= 4;
151        iconv_close (cd_88591_to_utf8);
152      }
153  }
154#if 0 /* This bug could be worked around by the caller.  */
155  /* Test against HP-UX 11.11 bug: Positive return value instead of 0.  */
156  {
157    iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
158    if (cd_88591_to_utf8 != (iconv_t)(-1))
159      {
160        static ICONV_CONST char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
161        char buf[50];
162        ICONV_CONST char *inptr = input;
163        size_t inbytesleft = strlen (input);
164        char *outptr = buf;
165        size_t outbytesleft = sizeof (buf);
166        size_t res = iconv (cd_88591_to_utf8,
167                            &inptr, &inbytesleft,
168                            &outptr, &outbytesleft);
169        if ((int)res > 0)
170          result |= 8;
171        iconv_close (cd_88591_to_utf8);
172      }
173  }
174#endif
175  /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
176     provided.  */
177  {
178    /* Try standardized names.  */
179    iconv_t cd1 = iconv_open ("UTF-8", "EUC-JP");
180    /* Try IRIX, OSF/1 names.  */
181    iconv_t cd2 = iconv_open ("UTF-8", "eucJP");
182    /* Try AIX names.  */
183    iconv_t cd3 = iconv_open ("UTF-8", "IBM-eucJP");
184    /* Try HP-UX names.  */
185    iconv_t cd4 = iconv_open ("utf8", "eucJP");
186    if (cd1 == (iconv_t)(-1) && cd2 == (iconv_t)(-1)
187        && cd3 == (iconv_t)(-1) && cd4 == (iconv_t)(-1))
188      result |= 16;
189    if (cd1 != (iconv_t)(-1))
190      iconv_close (cd1);
191    if (cd2 != (iconv_t)(-1))
192      iconv_close (cd2);
193    if (cd3 != (iconv_t)(-1))
194      iconv_close (cd3);
195    if (cd4 != (iconv_t)(-1))
196      iconv_close (cd4);
197  }
198  return result;
199]])],
200          [am_cv_func_iconv_works=yes], ,
201          [case "$host_os" in
202             aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
203             *)            am_cv_func_iconv_works="guessing yes" ;;
204           esac])
205        test "$am_cv_func_iconv_works" = no || break
206      done
207      LIBS="$am_save_LIBS"
208    ])
209    case "$am_cv_func_iconv_works" in
210      *no) am_func_iconv=no am_cv_lib_iconv=no ;;
211      *)   am_func_iconv=yes ;;
212    esac
213  else
214    am_func_iconv=no am_cv_lib_iconv=no
215  fi
216  if test "$am_func_iconv" = yes; then
217    AC_DEFINE([HAVE_ICONV], [1],
218      [Define if you have the iconv() function and it works.])
219  fi
220  if test "$am_cv_lib_iconv" = yes; then
221    AC_MSG_CHECKING([how to link with libiconv])
222    AC_MSG_RESULT([$LIBICONV])
223  else
224    dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
225    dnl either.
226    CPPFLAGS="$am_save_CPPFLAGS"
227    LIBICONV=
228    LTLIBICONV=
229  fi
230  AC_SUBST([LIBICONV])
231  AC_SUBST([LTLIBICONV])
232])
233
234dnl Define AM_ICONV using AC_DEFUN_ONCE, in order to avoid warnings like
235dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
236dnl This is tricky because of the way 'aclocal' is implemented:
237dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
238dnl   Otherwise aclocal's initial scan pass would miss the macro definition.
239dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
240dnl   Otherwise aclocal would emit many "Use of uninitialized value $1"
241dnl   warnings.
242AC_DEFUN_ONCE([AM_ICONV],
243[
244  AM_ICONV_LINK
245  if test "$am_cv_func_iconv" = yes; then
246    AC_CACHE_CHECK([whether iconv is compatible with its POSIX signature],
247      [gl_cv_iconv_nonconst],
248      [AC_COMPILE_IFELSE(
249         [AC_LANG_PROGRAM(
250            [[
251#include <stdlib.h>
252#include <iconv.h>
253extern
254#ifdef __cplusplus
255"C"
256#endif
257size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
258            ]],
259            [[]])],
260         [gl_cv_iconv_nonconst=yes],
261         [gl_cv_iconv_nonconst=no])
262      ])
263  else
264    dnl When compiling GNU libiconv on a system that does not have iconv yet,
265    dnl pick the POSIX compliant declaration without 'const'.
266    gl_cv_iconv_nonconst=yes
267  fi
268  if test $gl_cv_iconv_nonconst = yes; then
269    iconv_arg1=""
270  else
271    iconv_arg1="const"
272  fi
273  AC_DEFINE_UNQUOTED([ICONV_CONST], [$iconv_arg1],
274    [Define as const if the declaration of iconv() needs const.])
275  dnl Also substitute ICONV_CONST in the gnulib generated <iconv.h>.
276  m4_ifdef([gl_ICONV_H_DEFAULTS],
277    [AC_REQUIRE([gl_ICONV_H_DEFAULTS])
278     if test $gl_cv_iconv_nonconst != yes; then
279       ICONV_CONST="const"
280     fi
281    ])
282])
283