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