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