summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2008-12-21 14:45:44 +0100
committerBruno Haible <bruno@clisp.org>2008-12-21 14:45:44 +0100
commit6906a95bfc7fc83e4ffa7ef2f68e377157d926f7 (patch)
tree72a48d1d9d0fdd2d02278ae8e6a013cca6a48255
parentda3fe7009a18cbf8d7f1f43c02cedaf8a23281e4 (diff)
downloadgnulib-6906a95bfc7fc83e4ffa7ef2f68e377157d926f7.tar.gz
New module 'mbsnrtowcs'.
-rw-r--r--ChangeLog20
-rw-r--r--doc/posix-functions/mbsnrtowcs.texi12
-rw-r--r--lib/mbsnrtowcs.c139
-rw-r--r--lib/mbsrtowcs-state.c23
-rw-r--r--lib/mbsrtowcs.c4
-rw-r--r--lib/wchar.in.h18
-rw-r--r--m4/mbsnrtowcs.m434
-rw-r--r--m4/mbsrtowcs.m43
-rw-r--r--m4/wchar.m45
-rw-r--r--modules/mbsnrtowcs31
-rw-r--r--modules/mbsrtowcs1
-rw-r--r--modules/wchar3
12 files changed, 286 insertions, 7 deletions
diff --git a/ChangeLog b/ChangeLog
index 70f5580cca..0fc5e4d264 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,25 @@
2008-12-21 Bruno Haible <bruno@clisp.org>
+ New module 'mbsnrtowcs'.
+ * lib/wchar.in.h (mbsnrtowcs): New declaration.
+ * lib/mbsnrtowcs.c: New file.
+ * lib/mbsrtowcs-state.c: New file.
+ * lib/mbsrtowcs.c: Refer to _gl_mbsrtowcs_state.
+ (internal_state): Remove variable.
+ * m4/mbsnrtowcs.m4: New file.
+ * m4/mbsrtowcs.m4 (gl_FUNC_MBSRTOWCS): Add mbsrtowcs-state.c to the
+ compilation units.
+ * m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_MBSNRTOWCS,
+ HAVE_MBSNRTOWCS, REPLACE_MBSNRTOWCS.
+ * modules/wchar (Makefile.am): Substitute GNULIB_MBSNRTOWCS,
+ HAVE_MBSNRTOWCS, REPLACE_MBSNRTOWCS.
+ * modules/mbsnrtowcs: New file.
+ * modules/mbsrtowcs (Files): Add lib/mbsrtowcs-state.c.
+ * doc/posix-functions/mbsnrtowcs.texi: Mention the new module and a
+ portability problem.
+
+2008-12-21 Bruno Haible <bruno@clisp.org>
+
Work around mbsrtowcs bug.
* m4/mbsrtowcs.m4 (gl_MBSRTOWCS_WORKS): New macro.
(gl_FUNC_MBSRTOWCS): Invoke it.
diff --git a/doc/posix-functions/mbsnrtowcs.texi b/doc/posix-functions/mbsnrtowcs.texi
index b85aca3b02..916f5ecdb5 100644
--- a/doc/posix-functions/mbsnrtowcs.texi
+++ b/doc/posix-functions/mbsnrtowcs.texi
@@ -4,15 +4,21 @@
POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/mbsnrtowcs.html}
-Gnulib module: ---
+Gnulib module: mbsnrtowcs
Portability problems fixed by Gnulib:
@itemize
+@item
+This function is missing on some platforms:
+MacOS X 10.3, FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS.
@end itemize
Portability problems not fixed by Gnulib:
@itemize
@item
-This function is missing on some platforms:
-MacOS X 10.3, FreeBSD 5.2.1, NetBSD 3.0, OpenBSD 3.8, AIX 4.3.2, HP-UX 11, IRIX 6.5, OSF/1 5.1, Solaris 10, Cygwin, mingw, Interix 3.5, BeOS.
+The specification is not clear about whether this function should update the
+conversion state when the first argument (the destination pointer) is NULL.
+The glibc, MacOS X, FreeBSD implementations do update the state in this case.
+For portability, when passing a NULL destination argument, it is best to pass
+a pointer to a temporary copy of the conversion state.
@end itemize
diff --git a/lib/mbsnrtowcs.c b/lib/mbsnrtowcs.c
new file mode 100644
index 0000000000..57db98d29d
--- /dev/null
+++ b/lib/mbsnrtowcs.c
@@ -0,0 +1,139 @@
+/* Convert string to wide string.
+ Copyright (C) 2008 Free Software Foundation, Inc.
+ Written by Bruno Haible <bruno@clisp.org>, 2008.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+/* Specification. */
+#include <wchar.h>
+
+#include <errno.h>
+#include <limits.h>
+#include <stdlib.h>
+
+#include "minmax.h"
+#include "strnlen1.h"
+
+
+extern mbstate_t _gl_mbsrtowcs_state;
+
+size_t
+mbsnrtowcs (wchar_t *dest, const char **srcp, size_t srclen, size_t len, mbstate_t *ps)
+{
+ if (ps == NULL)
+ ps = &_gl_mbsrtowcs_state;
+ {
+ const char *src = *srcp;
+
+ if (dest != NULL)
+ {
+ wchar_t *destptr = dest;
+
+ for (; srclen > 0 && len > 0; destptr++, len--)
+ {
+ size_t src_avail;
+ size_t ret;
+
+ /* An optimized variant of
+ src_avail = strnlen1 (src, MIN (srclen, MB_LEN_MAX)); */
+ if (srclen == 1 || src[0] == '\0')
+ src_avail = 1;
+ else if (srclen == 2 || src[1] == '\0')
+ src_avail = 2;
+ else if (srclen == 3 || src[2] == '\0')
+ src_avail = 3;
+ else if (MB_LEN_MAX <= 4 || srclen == 4 || src[3] == '\0')
+ src_avail = 4;
+ else
+ src_avail = 4 + strnlen1 (src + 4, MIN (srclen, MB_LEN_MAX) - 4);
+
+ /* Parse the next multibyte character. */
+ ret = mbrtowc (destptr, src, src_avail, ps);
+
+ if (ret == (size_t)(-2))
+ /* Encountered a multibyte character that extends past a '\0' byte
+ or that is longer than MB_LEN_MAX bytes. Cannot happen. */
+ abort ();
+
+ if (ret == (size_t)(-1))
+ goto bad_input;
+ if (ret == 0)
+ {
+ src = NULL;
+ /* Here mbsinit (ps). */
+ break;
+ }
+ src += ret;
+ srclen -= ret;
+ }
+
+ *srcp = src;
+ return destptr - dest;
+ }
+ else
+ {
+ /* Ignore dest and len, don't store *srcp at the end, and
+ don't clobber *ps. */
+ mbstate_t state = *ps;
+ size_t totalcount = 0;
+
+ for (; srclen > 0; totalcount++)
+ {
+ size_t src_avail;
+ size_t ret;
+
+ /* An optimized variant of
+ src_avail = strnlen1 (src, MIN (srclen, MB_LEN_MAX)); */
+ if (srclen == 1 || src[0] == '\0')
+ src_avail = 1;
+ else if (srclen == 2 || src[1] == '\0')
+ src_avail = 2;
+ else if (srclen == 3 || src[2] == '\0')
+ src_avail = 3;
+ else if (MB_LEN_MAX <= 4 || srclen == 4 || src[3] == '\0')
+ src_avail = 4;
+ else
+ src_avail = 4 + strnlen1 (src + 4, MIN (srclen, MB_LEN_MAX) - 4);
+
+ /* Parse the next multibyte character. */
+ ret = mbrtowc (NULL, src, src_avail, &state);
+
+ if (ret == (size_t)(-2))
+ /* Encountered a multibyte character that extends past a '\0' byte
+ or that is longer than MB_LEN_MAX bytes. Cannot happen. */
+ abort ();
+
+ if (ret == (size_t)(-1))
+ goto bad_input2;
+ if (ret == 0)
+ {
+ /* Here mbsinit (&state). */
+ break;
+ }
+ src += ret;
+ srclen -= ret;
+ }
+
+ return totalcount;
+ }
+
+ bad_input:
+ *srcp = src;
+ bad_input2:
+ errno = EILSEQ;
+ return (size_t)(-1);
+ }
+}
diff --git a/lib/mbsrtowcs-state.c b/lib/mbsrtowcs-state.c
new file mode 100644
index 0000000000..8216c790d7
--- /dev/null
+++ b/lib/mbsrtowcs-state.c
@@ -0,0 +1,23 @@
+/* Convert string to wide string.
+ Copyright (C) 2008 Free Software Foundation, Inc.
+ Written by Bruno Haible <bruno@clisp.org>, 2008.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <wchar.h>
+
+/* Internal state used by the functions mbsrtowcs() and mbsnrtowcs(). */
+mbstate_t _gl_mbsrtowcs_state;
diff --git a/lib/mbsrtowcs.c b/lib/mbsrtowcs.c
index 6a9a9e9ae1..ea6a018721 100644
--- a/lib/mbsrtowcs.c
+++ b/lib/mbsrtowcs.c
@@ -27,13 +27,13 @@
#include "strnlen1.h"
-static mbstate_t internal_state;
+extern mbstate_t _gl_mbsrtowcs_state;
size_t
mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t *ps)
{
if (ps == NULL)
- ps = &internal_state;
+ ps = &_gl_mbsrtowcs_state;
{
const char *src = *srcp;
diff --git a/lib/wchar.in.h b/lib/wchar.in.h
index c4269664cd..bf90415006 100644
--- a/lib/wchar.in.h
+++ b/lib/wchar.in.h
@@ -183,6 +183,24 @@ extern size_t mbsrtowcs (wchar_t *dest, const char **srcp, size_t len, mbstate_t
#endif
+/* Convert a string to a wide string. */
+#if @GNULIB_MBSNRTOWCS@
+# if @REPLACE_MBSNRTOWCS@
+# undef mbsnrtowcs
+# define mbsnrtowcs rpl_mbsnrtowcs
+# endif
+# if !@HAVE_MBSNRTOWCS@ || @REPLACE_MBSNRTOWCS@
+extern size_t mbsnrtowcs (wchar_t *dest, const char **srcp, size_t srclen, size_t len, mbstate_t *ps);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef mbsnrtowcs
+# define mbsnrtowcs(d,s,n,l,p) \
+ (GL_LINK_WARNING ("mbsnrtowcs is unportable - " \
+ "use gnulib module mbsnrtowcs for portability"), \
+ mbsnrtowcs (d, s, n, l, p))
+#endif
+
+
/* Return the number of screen columns needed for WC. */
#if @GNULIB_WCWIDTH@
# if @REPLACE_WCWIDTH@
diff --git a/m4/mbsnrtowcs.m4 b/m4/mbsnrtowcs.m4
new file mode 100644
index 0000000000..b61da902b2
--- /dev/null
+++ b/m4/mbsnrtowcs.m4
@@ -0,0 +1,34 @@
+# mbsnrtowcs.m4 serial 1
+dnl Copyright (C) 2008 Free Software Foundation, Inc.
+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_MBSNRTOWCS],
+[
+ AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
+
+ dnl Persuade glibc <wchar.h> to declare mbsnrtowcs().
+ AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
+
+ AC_REQUIRE([AC_TYPE_MBSTATE_T])
+ gl_MBSTATE_T_BROKEN
+ if test $REPLACE_MBSTATE_T = 1; then
+ REPLACE_MBSNRTOWCS=1
+ fi
+ AC_CHECK_FUNCS_ONCE([mbsnrtowcs])
+ if test $ac_cv_func_mbsnrtowcs = no; then
+ HAVE_MBSNRTOWCS=0
+ fi
+ if test $HAVE_MBSNRTOWCS = 0 || test $REPLACE_MBSNRTOWCS = 1; then
+ gl_REPLACE_WCHAR_H
+ AC_LIBOBJ([mbsnrtowcs])
+ AC_LIBOBJ([mbsrtowcs-state])
+ gl_PREREQ_MBSNRTOWCS
+ fi
+])
+
+# Prerequisites of lib/mbsnrtowcs.c.
+AC_DEFUN([gl_PREREQ_MBSNRTOWCS], [
+ :
+])
diff --git a/m4/mbsrtowcs.m4 b/m4/mbsrtowcs.m4
index d18ef13ee5..70e4a27951 100644
--- a/m4/mbsrtowcs.m4
+++ b/m4/mbsrtowcs.m4
@@ -1,4 +1,4 @@
-# mbsrtowcs.m4 serial 3
+# mbsrtowcs.m4 serial 4
dnl Copyright (C) 2008 Free Software Foundation, Inc.
dnl This file is free software; the Free Software Foundation
dnl gives unlimited permission to copy and/or distribute it,
@@ -27,6 +27,7 @@ AC_DEFUN([gl_FUNC_MBSRTOWCS],
if test $HAVE_MBSRTOWCS = 0 || test $REPLACE_MBSRTOWCS = 1; then
gl_REPLACE_WCHAR_H
AC_LIBOBJ([mbsrtowcs])
+ AC_LIBOBJ([mbsrtowcs-state])
gl_PREREQ_MBSRTOWCS
fi
])
diff --git a/m4/wchar.m4 b/m4/wchar.m4
index 6e0fe01886..b907939d12 100644
--- a/m4/wchar.m4
+++ b/m4/wchar.m4
@@ -7,7 +7,7 @@ dnl with or without modifications, as long as this notice is preserved.
dnl Written by Eric Blake.
-# wchar.m4 serial 15
+# wchar.m4 serial 16
AC_DEFUN([gl_WCHAR_H],
[
@@ -67,6 +67,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
GNULIB_MBRTOWC=0; AC_SUBST([GNULIB_MBRTOWC])
GNULIB_MBRLEN=0; AC_SUBST([GNULIB_MBRLEN])
GNULIB_MBSRTOWCS=0; AC_SUBST([GNULIB_MBSRTOWCS])
+ GNULIB_MBSNRTOWCS=0; AC_SUBST([GNULIB_MBSNRTOWCS])
GNULIB_WCWIDTH=0; AC_SUBST([GNULIB_WCWIDTH])
dnl Assume proper GNU behavior unless another module says otherwise.
HAVE_BTOWC=1; AC_SUBST([HAVE_BTOWC])
@@ -74,6 +75,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
HAVE_MBRTOWC=1; AC_SUBST([HAVE_MBRTOWC])
HAVE_MBRLEN=1; AC_SUBST([HAVE_MBRLEN])
HAVE_MBSRTOWCS=1; AC_SUBST([HAVE_MBSRTOWCS])
+ HAVE_MBSNRTOWCS=1; AC_SUBST([HAVE_MBSNRTOWCS])
HAVE_DECL_WCTOB=1; AC_SUBST([HAVE_DECL_WCTOB])
HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH])
REPLACE_MBSTATE_T=0; AC_SUBST([REPLACE_MBSTATE_T])
@@ -81,6 +83,7 @@ AC_DEFUN([gl_WCHAR_H_DEFAULTS],
REPLACE_MBSINIT=0; AC_SUBST([REPLACE_MBSINIT])
REPLACE_MBRTOWC=0; AC_SUBST([REPLACE_MBRTOWC])
REPLACE_MBSRTOWCS=0; AC_SUBST([REPLACE_MBSRTOWCS])
+ REPLACE_MBSNRTOWCS=0;AC_SUBST([REPLACE_MBSNRTOWCS])
REPLACE_WCWIDTH=0; AC_SUBST([REPLACE_WCWIDTH])
WCHAR_H=''; AC_SUBST([WCHAR_H])
])
diff --git a/modules/mbsnrtowcs b/modules/mbsnrtowcs
new file mode 100644
index 0000000000..30ffcadb29
--- /dev/null
+++ b/modules/mbsnrtowcs
@@ -0,0 +1,31 @@
+Description:
+mbsnrtowcs() function: convert string to wide string.
+
+Files:
+lib/mbsnrtowcs.c
+lib/mbsrtowcs-state.c
+m4/mbsnrtowcs.m4
+m4/mbstate_t.m4
+
+Depends-on:
+extensions
+wchar
+mbrtowc
+minmax
+strnlen1
+
+configure.ac:
+gl_FUNC_MBSNRTOWCS
+gl_WCHAR_MODULE_INDICATOR([mbsnrtowcs])
+
+Makefile.am:
+
+Include:
+<wchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
diff --git a/modules/mbsrtowcs b/modules/mbsrtowcs
index 0a5450a5e4..9882bbddc2 100644
--- a/modules/mbsrtowcs
+++ b/modules/mbsrtowcs
@@ -3,6 +3,7 @@ mbsrtowcs() function: convert string to wide string.
Files:
lib/mbsrtowcs.c
+lib/mbsrtowcs-state.c
m4/mbsrtowcs.m4
m4/mbstate_t.m4
m4/locale-fr.m4
diff --git a/modules/wchar b/modules/wchar
index fcef30e69a..22724c1fe9 100644
--- a/modules/wchar
+++ b/modules/wchar
@@ -31,6 +31,7 @@ wchar.h: wchar.in.h
-e 's|@''GNULIB_MBRTOWC''@|$(GNULIB_MBRTOWC)|g' \
-e 's|@''GNULIB_MBRLEN''@|$(GNULIB_MBRLEN)|g' \
-e 's|@''GNULIB_MBSRTOWCS''@|$(GNULIB_MBSRTOWCS)|g' \
+ -e 's|@''GNULIB_MBSNRTOWCS''@|$(GNULIB_MBSNRTOWCS)|g' \
-e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \
-e 's|@''HAVE_WINT_T''@|$(HAVE_WINT_T)|g' \
-e 's|@''HAVE_BTOWC''@|$(HAVE_BTOWC)|g' \
@@ -38,6 +39,7 @@ wchar.h: wchar.in.h
-e 's|@''HAVE_MBRTOWC''@|$(HAVE_MBRTOWC)|g' \
-e 's|@''HAVE_MBRLEN''@|$(HAVE_MBRLEN)|g' \
-e 's|@''HAVE_MBSRTOWCS''@|$(HAVE_MBSRTOWCS)|g' \
+ -e 's|@''HAVE_MBSNRTOWCS''@|$(HAVE_MBSNRTOWCS)|g' \
-e 's|@''HAVE_DECL_WCTOB''@|$(HAVE_DECL_WCTOB)|g' \
-e 's|@''HAVE_DECL_WCWIDTH''@|$(HAVE_DECL_WCWIDTH)|g' \
-e 's|@''REPLACE_MBSTATE_T''@|$(REPLACE_MBSTATE_T)|g' \
@@ -45,6 +47,7 @@ wchar.h: wchar.in.h
-e 's|@''REPLACE_MBSINIT''@|$(REPLACE_MBSINIT)|g' \
-e 's|@''REPLACE_MBRTOWC''@|$(REPLACE_MBRTOWC)|g' \
-e 's|@''REPLACE_MBSRTOWCS''@|$(REPLACE_MBSRTOWCS)|g' \
+ -e 's|@''REPLACE_MBSNRTOWCS''@|$(REPLACE_MBSNRTOWCS)|g' \
-e 's|@''REPLACE_WCWIDTH''@|$(REPLACE_WCWIDTH)|g' \
-e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \
< $(srcdir)/wchar.in.h; \