mbsrtowcs.m4 4.66 KB
Newer Older
1
# mbsrtowcs.m4 serial 13
2
dnl Copyright (C) 2008-2018 Free Software Foundation, Inc.
Bruno Haible's avatar
Bruno Haible committed
3 4 5 6 7 8 9 10 11
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
dnl with or without modifications, as long as this notice is preserved.

AC_DEFUN([gl_FUNC_MBSRTOWCS],
[
  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])

  AC_REQUIRE([AC_TYPE_MBSTATE_T])
12
  gl_MBSTATE_T_BROKEN
13

Bruno Haible's avatar
Bruno Haible committed
14 15 16
  AC_CHECK_FUNCS_ONCE([mbsrtowcs])
  if test $ac_cv_func_mbsrtowcs = no; then
    HAVE_MBSRTOWCS=0
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
    AC_CHECK_DECLS([mbsrtowcs],,, [[
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
#include <wchar.h>
]])
    if test $ac_cv_have_decl_mbsrtowcs = yes; then
      dnl On Minix 3.1.8, the system's <wchar.h> declares mbsrtowcs() although
      dnl it does not have the function. Avoid a collision with gnulib's
      dnl replacement.
      REPLACE_MBSRTOWCS=1
    fi
33 34 35 36 37 38 39 40 41 42
  else
    if test $REPLACE_MBSTATE_T = 1; then
      REPLACE_MBSRTOWCS=1
    else
      gl_MBSRTOWCS_WORKS
      case "$gl_cv_func_mbsrtowcs_works" in
        *yes) ;;
        *) REPLACE_MBSRTOWCS=1 ;;
      esac
    fi
43
  fi
Bruno Haible's avatar
Bruno Haible committed
44 45
])

46 47 48 49 50 51
dnl Test whether mbsrtowcs works.
dnl Result is gl_cv_func_mbsrtowcs_works.

AC_DEFUN([gl_MBSRTOWCS_WORKS],
[
  AC_REQUIRE([AC_PROG_CC])
52
  AC_REQUIRE([gt_LOCALE_FR])
53 54 55 56 57 58 59 60 61 62 63
  AC_REQUIRE([gt_LOCALE_FR_UTF8])
  AC_REQUIRE([gt_LOCALE_JA])
  AC_REQUIRE([gt_LOCALE_ZH_CN])
  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  AC_CACHE_CHECK([whether mbsrtowcs works],
    [gl_cv_func_mbsrtowcs_works],
    [
      dnl Initial guess, used when cross-compiling or when no suitable locale
      dnl is present.
changequote(,)dnl
      case "$host_os" in
64 65 66 67
                                   # Guess no on HP-UX, Solaris, mingw.
        hpux* | solaris* | mingw*) gl_cv_func_mbsrtowcs_works="guessing no" ;;
                                   # Guess yes otherwise.
        *)                         gl_cv_func_mbsrtowcs_works="guessing yes" ;;
68 69
      esac
changequote([,])dnl
70
      if test $LOCALE_FR != none || test $LOCALE_FR_UTF8 != none || test $LOCALE_JA != none || test $LOCALE_ZH_CN != none; then
71 72
        AC_RUN_IFELSE(
          [AC_LANG_SOURCE([[
73 74
#include <locale.h>
#include <string.h>
75 76 77 78 79 80 81
/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
   <wchar.h>.
   BSD/OS 4.0.1 has a bug: <stddef.h>, <stdio.h> and <time.h> must be
   included before <wchar.h>.  */
#include <stddef.h>
#include <stdio.h>
#include <time.h>
82 83 84
#include <wchar.h>
int main ()
{
85
  int result = 0;
86 87 88 89 90 91 92 93 94 95 96 97 98
  /* Test whether the function supports a NULL destination argument.
     This fails on native Windows.  */
  if (setlocale (LC_ALL, "$LOCALE_FR") != NULL)
    {
      const char input[] = "\337er";
      const char *src = input;
      mbstate_t state;

      memset (&state, '\0', sizeof (mbstate_t));
      if (mbsrtowcs (NULL, &src, 1, &state) != 3
          || src != input)
        result |= 1;
    }
99 100 101 102 103 104 105 106 107 108 109 110 111
  /* Test whether the function works when started with a conversion state
     in non-initial state.  This fails on HP-UX 11.11 and Solaris 10.  */
  if (setlocale (LC_ALL, "$LOCALE_FR_UTF8") != NULL)
    {
      const char input[] = "B\303\274\303\237er";
      mbstate_t state;

      memset (&state, '\0', sizeof (mbstate_t));
      if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
        if (!mbsinit (&state))
          {
            const char *src = input + 2;
            if (mbsrtowcs (NULL, &src, 10, &state) != 4)
112
              result |= 2;
113 114 115 116 117 118 119 120 121 122 123 124 125
          }
    }
  if (setlocale (LC_ALL, "$LOCALE_JA") != NULL)
    {
      const char input[] = "<\306\374\313\334\270\354>";
      mbstate_t state;

      memset (&state, '\0', sizeof (mbstate_t));
      if (mbrtowc (NULL, input + 3, 1, &state) == (size_t)(-2))
        if (!mbsinit (&state))
          {
            const char *src = input + 4;
            if (mbsrtowcs (NULL, &src, 10, &state) != 3)
126
              result |= 4;
127 128 129 130 131 132 133 134 135 136 137 138 139
          }
    }
  if (setlocale (LC_ALL, "$LOCALE_ZH_CN") != NULL)
    {
      const char input[] = "B\250\271\201\060\211\070er";
      mbstate_t state;

      memset (&state, '\0', sizeof (mbstate_t));
      if (mbrtowc (NULL, input + 1, 1, &state) == (size_t)(-2))
        if (!mbsinit (&state))
          {
            const char *src = input + 2;
            if (mbsrtowcs (NULL, &src, 10, &state) != 4)
140
              result |= 8;
141 142
          }
    }
143
  return result;
144
}]])],
145 146
          [gl_cv_func_mbsrtowcs_works=yes],
          [gl_cv_func_mbsrtowcs_works=no],
147
          [:])
148 149 150 151
      fi
    ])
])

Bruno Haible's avatar
Bruno Haible committed
152 153 154 155
# Prerequisites of lib/mbsrtowcs.c.
AC_DEFUN([gl_PREREQ_MBSRTOWCS], [
  :
])