iconv: Work around AIX 6.1..7.1 bug.
[gnulib.git] / m4 / iconv.m4
1 # iconv.m4 serial 11b
2 dnl Copyright (C) 2000-2002, 2007-2010 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.
6
7 dnl From Bruno Haible.
8
9 AC_DEFUN([AM_ICONV_LINKFLAGS_BODY],
10 [
11   dnl Prerequisites of AC_LIB_LINKFLAGS_BODY.
12   AC_REQUIRE([AC_LIB_PREPARE_PREFIX])
13   AC_REQUIRE([AC_LIB_RPATH])
14
15   dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
16   dnl accordingly.
17   AC_LIB_LINKFLAGS_BODY([iconv])
18 ])
19
20 AC_DEFUN([AM_ICONV_LINK],
21 [
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
25
26   dnl Search for libiconv and define LIBICONV, LTLIBICONV and INCICONV
27   dnl accordingly.
28   AC_REQUIRE([AM_ICONV_LINKFLAGS_BODY])
29
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_TRY_LINK will then fail, the second AC_TRY_LINK will succeed.
34   am_save_CPPFLAGS="$CPPFLAGS"
35   AC_LIB_APPENDTOVAR([CPPFLAGS], [$INCICONV])
36
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_TRY_LINK([#include <stdlib.h>
41 #include <iconv.h>],
42       [iconv_t cd = iconv_open("","");
43        iconv(cd,NULL,NULL,NULL,NULL);
44        iconv_close(cd);],
45       [am_cv_func_iconv=yes])
46     if test "$am_cv_func_iconv" != yes; then
47       am_save_LIBS="$LIBS"
48       LIBS="$LIBS $LIBICONV"
49       AC_TRY_LINK([#include <stdlib.h>
50 #include <iconv.h>],
51         [iconv_t cd = iconv_open("","");
52          iconv(cd,NULL,NULL,NULL,NULL);
53          iconv_close(cd);],
54         [am_cv_lib_iconv=yes]
55         [am_cv_func_iconv=yes])
56       LIBS="$am_save_LIBS"
57     fi
58   ])
59   if test "$am_cv_func_iconv" = yes; then
60     AC_CACHE_CHECK([for working iconv], [am_cv_func_iconv_works], [
61       dnl This tests against bugs in AIX 5.1, AIX 6.1..7.1, HP-UX 11.11,
62       dnl Solaris 10.
63       am_save_LIBS="$LIBS"
64       if test $am_cv_lib_iconv = yes; then
65         LIBS="$LIBS $LIBICONV"
66       fi
67       AC_TRY_RUN([
68 #include <iconv.h>
69 #include <string.h>
70 int main ()
71 {
72   /* Test against AIX 5.1 bug: Failures are not distinguishable from successful
73      returns.  */
74   {
75     iconv_t cd_utf8_to_88591 = iconv_open ("ISO8859-1", "UTF-8");
76     if (cd_utf8_to_88591 != (iconv_t)(-1))
77       {
78         static const char input[] = "\342\202\254"; /* EURO SIGN */
79         char buf[10];
80         const char *inptr = input;
81         size_t inbytesleft = strlen (input);
82         char *outptr = buf;
83         size_t outbytesleft = sizeof (buf);
84         size_t res = iconv (cd_utf8_to_88591,
85                             (char **) &inptr, &inbytesleft,
86                             &outptr, &outbytesleft);
87         if (res == 0)
88           return 1;
89       }
90   }
91   /* Test against Solaris 10 bug: Failures are not distinguishable from
92      successful returns.  */
93   {
94     iconv_t cd_ascii_to_88591 = iconv_open ("ISO8859-1", "646");
95     if (cd_ascii_to_88591 != (iconv_t)(-1))
96       {
97         static const char input[] = "\263";
98         char buf[10];
99         const char *inptr = input;
100         size_t inbytesleft = strlen (input);
101         char *outptr = buf;
102         size_t outbytesleft = sizeof (buf);
103         size_t res = iconv (cd_ascii_to_88591,
104                             (char **) &inptr, &inbytesleft,
105                             &outptr, &outbytesleft);
106         if (res == 0)
107           return 1;
108       }
109   }
110   /* Test against AIX 6.1..7.1 bug: Buffer overrun.  */
111   {
112     iconv_t cd_88591_to_utf8 = iconv_open ("UTF-8", "ISO-8859-1");
113     if (cd_88591_to_utf8 != (iconv_t)(-1))
114       {
115         static const char input[] = "\304";
116         static char buf[2] = { (char)0xDE, (char)0xAD };
117         const char *inptr = input;
118         size_t inbytesleft = 1;
119         char *outptr = buf;
120         size_t outbytesleft = 1;
121         size_t res = iconv (cd_88591_to_utf8,
122                             (char **) &inptr, &inbytesleft,
123                             &outptr, &outbytesleft);
124         if (res != (size_t)(-1) || outptr - buf > 1 || buf[1] != (char)0xAD)
125           return 1;
126       }
127   }
128 #if 0 /* This bug could be worked around by the caller.  */
129   /* Test against HP-UX 11.11 bug: Positive return value instead of 0.  */
130   {
131     iconv_t cd_88591_to_utf8 = iconv_open ("utf8", "iso88591");
132     if (cd_88591_to_utf8 != (iconv_t)(-1))
133       {
134         static const char input[] = "\304rger mit b\366sen B\374bchen ohne Augenma\337";
135         char buf[50];
136         const char *inptr = input;
137         size_t inbytesleft = strlen (input);
138         char *outptr = buf;
139         size_t outbytesleft = sizeof (buf);
140         size_t res = iconv (cd_88591_to_utf8,
141                             (char **) &inptr, &inbytesleft,
142                             &outptr, &outbytesleft);
143         if ((int)res > 0)
144           return 1;
145       }
146   }
147 #endif
148   /* Test against HP-UX 11.11 bug: No converter from EUC-JP to UTF-8 is
149      provided.  */
150   if (/* Try standardized names.  */
151       iconv_open ("UTF-8", "EUC-JP") == (iconv_t)(-1)
152       /* Try IRIX, OSF/1 names.  */
153       && iconv_open ("UTF-8", "eucJP") == (iconv_t)(-1)
154       /* Try AIX names.  */
155       && iconv_open ("UTF-8", "IBM-eucJP") == (iconv_t)(-1)
156       /* Try HP-UX names.  */
157       && iconv_open ("utf8", "eucJP") == (iconv_t)(-1))
158     return 1;
159   return 0;
160 }], [am_cv_func_iconv_works=yes], [am_cv_func_iconv_works=no],
161         [
162 changequote(,)dnl
163          case "$host_os" in
164            aix* | hpux*) am_cv_func_iconv_works="guessing no" ;;
165            *)            am_cv_func_iconv_works="guessing yes" ;;
166          esac
167 changequote([,])dnl
168         ])
169       LIBS="$am_save_LIBS"
170     ])
171     case "$am_cv_func_iconv_works" in
172       *no) am_func_iconv=no am_cv_lib_iconv=no ;;
173       *)   am_func_iconv=yes ;;
174     esac
175   else
176     am_func_iconv=no am_cv_lib_iconv=no
177   fi
178   if test "$am_func_iconv" = yes; then
179     AC_DEFINE([HAVE_ICONV], [1],
180       [Define if you have the iconv() function and it works.])
181   fi
182   if test "$am_cv_lib_iconv" = yes; then
183     AC_MSG_CHECKING([how to link with libiconv])
184     AC_MSG_RESULT([$LIBICONV])
185   else
186     dnl If $LIBICONV didn't lead to a usable library, we don't need $INCICONV
187     dnl either.
188     CPPFLAGS="$am_save_CPPFLAGS"
189     LIBICONV=
190     LTLIBICONV=
191   fi
192   AC_SUBST([LIBICONV])
193   AC_SUBST([LTLIBICONV])
194 ])
195
196 dnl Define AM_ICONV using AC_DEFUN_ONCE for Autoconf >= 2.64, in order to
197 dnl avoid warnings like
198 dnl "warning: AC_REQUIRE: `AM_ICONV' was expanded before it was required".
199 dnl This is tricky because of the way 'aclocal' is implemented:
200 dnl - It requires defining an auxiliary macro whose name ends in AC_DEFUN.
201 dnl   Otherwise aclocal's initial scan pass would miss the macro definition.
202 dnl - It requires a line break inside the AC_DEFUN_ONCE and AC_DEFUN expansions.
203 dnl   Otherwise aclocal would emit many "Use of uninitialized value $1"
204 dnl   warnings.
205 m4_define([gl_iconv_AC_DEFUN],
206   m4_version_prereq([2.64],
207     [[AC_DEFUN_ONCE(
208         [$1], [$2])]],
209     [[AC_DEFUN(
210         [$1], [$2])]]))
211 gl_iconv_AC_DEFUN([AM_ICONV],
212 [
213   AM_ICONV_LINK
214   if test "$am_cv_func_iconv" = yes; then
215     AC_MSG_CHECKING([for iconv declaration])
216     AC_CACHE_VAL([am_cv_proto_iconv], [
217       AC_TRY_COMPILE([
218 #include <stdlib.h>
219 #include <iconv.h>
220 extern
221 #ifdef __cplusplus
222 "C"
223 #endif
224 #if defined(__STDC__) || defined(__cplusplus)
225 size_t iconv (iconv_t cd, char * *inbuf, size_t *inbytesleft, char * *outbuf, size_t *outbytesleft);
226 #else
227 size_t iconv();
228 #endif
229 ], [], [am_cv_proto_iconv_arg1=""], [am_cv_proto_iconv_arg1="const"])
230       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);"])
231     am_cv_proto_iconv=`echo "[$]am_cv_proto_iconv" | tr -s ' ' | sed -e 's/( /(/'`
232     AC_MSG_RESULT([
233          $am_cv_proto_iconv])
234     AC_DEFINE_UNQUOTED([ICONV_CONST], [$am_cv_proto_iconv_arg1],
235       [Define as const if the declaration of iconv() needs const.])
236   fi
237 ])