New module 'mbsinit'.
authorBruno Haible <bruno@clisp.org>
Wed, 17 Dec 2008 12:03:10 +0000 (13:03 +0100)
committerBruno Haible <bruno@clisp.org>
Wed, 17 Dec 2008 12:05:50 +0000 (13:05 +0100)
ChangeLog
doc/posix-functions/mbsinit.texi
lib/mbsinit.c [new file with mode: 0644]
lib/wchar.in.h
m4/mbsinit.m4 [new file with mode: 0644]
m4/wchar.m4
modules/mbsinit [new file with mode: 0644]
modules/wchar

index 2a401a1..7b07b94 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,16 @@
+2008-12-17  Bruno Haible  <bruno@clisp.org>
+
+       New module 'mbsinit'.
+       * lib/wchar.in.h (mbsinit): New declaration.
+       * lib/mbsinit.c: New file.
+       * m4/mbsinit.m4: New file.
+       * modules/mbsinit: New file.
+       * m4/wchar.m4 (gl_WCHAR_H_DEFAULTS): Initialize GNULIB_MBSINIT and
+       HAVE_MBSINIT.
+       * modules/wchar (Makefile.am): Substitute GNULIB_MBSINIT and
+       HAVE_MBSINIT.
+       * doc/posix-functions/mbsinit.texi: Document the new module.
+
 2008-12-16  Bruno Haible  <bruno@clisp.org>
 
        * lib/unistd.in.h: Add comment.
index 5121415..71a1925 100644 (file)
@@ -4,15 +4,15 @@
 
 POSIX specification: @url{http://www.opengroup.org/onlinepubs/9699919799/functions/mbsinit.html}
 
-Gnulib module: ---
+Gnulib module: mbsinit
 
 Portability problems fixed by Gnulib:
 @itemize
+@item
+This function is missing on some platforms:
+HP-UX 11, IRIX 6.5, Solaris 2.6, Interix 3.5.
 @end itemize
 
 Portability problems not fixed by Gnulib:
 @itemize
-@item
-This function is missing on some platforms:
-HP-UX 11, IRIX 6.5, Solaris 2.6, Interix 3.5.
 @end itemize
diff --git a/lib/mbsinit.c b/lib/mbsinit.c
new file mode 100644 (file)
index 0000000..2753b41
--- /dev/null
@@ -0,0 +1,43 @@
+/* Test for initial conversion state.
+   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>
+
+/* Platforms that lack mbsinit() also lack mbrlen(), mbrtowc(), mbsrtowcs()
+   and wcrtomb(), wcsrtombs().
+   We assume that
+     - sizeof (mbstate_t) >= 4,
+     - only stateless encodings are supported (such as UTF-8 and EUC-JP, but
+       not ISO-2022 variants),
+     - for each encoding, the number of bytes for a wide character is <= 4.
+       (This maximum is attained for UTF-8, GB18030, EUC-TW.)
+   We define the meaning of mbstate_t as follows:
+     - In mb -> wc direction, mbstate_t's first byte contains the number of
+       buffered bytes (in the range 0..3), followed by up to 3 buffered bytes.
+     - In wc -> mb direction, mbstate_t contains no information. In other
+       words, it is always in the initial state.  */
+
+int
+mbsinit (const mbstate_t *ps)
+{
+  const char *pstate = (const char *)ps;
+
+  return pstate[0] == 0;
+}
index ceb6208..f1bf174 100644 (file)
@@ -71,6 +71,20 @@ extern "C" {
 #endif
 
 
+/* Test whether *PS is in the initial state.  */
+#if @GNULIB_MBSINIT@
+# if !@HAVE_MBSINIT@
+extern int mbsinit (const mbstate_t *ps);
+# endif
+#elif defined GNULIB_POSIXCHECK
+# undef mbsinit
+# define mbsinit(p) \
+    (GL_LINK_WARNING ("mbsinit is unportable - " \
+                      "use gnulib module mbsinit for portability"), \
+     mbsinit (p))
+#endif
+
+
 /* Return the number of screen columns needed for WC.  */
 #if @GNULIB_WCWIDTH@
 # if @REPLACE_WCWIDTH@
diff --git a/m4/mbsinit.m4 b/m4/mbsinit.m4
new file mode 100644 (file)
index 0000000..f19ec0c
--- /dev/null
@@ -0,0 +1,23 @@
+# mbsinit.m4 serial 14
+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_MBSINIT],
+[
+  AC_REQUIRE([gl_WCHAR_H_DEFAULTS])
+
+  AC_REQUIRE([AC_TYPE_MBSTATE_T])
+  AC_CHECK_FUNCS_ONCE([mbsinit])
+  if test $ac_cv_func_mbsinit = no; then
+    HAVE_MBSINIT=0
+    AC_LIBOBJ([mbsinit])
+    gl_PREREQ_MBSINIT
+  fi
+])
+
+# Prerequisites of lib/mbsinit.c.
+AC_DEFUN([gl_PREREQ_MBSINIT], [
+  :
+])
index 5c923fa..809402e 100644 (file)
@@ -7,7 +7,7 @@ dnl with or without modifications, as long as this notice is preserved.
 
 dnl Written by Eric Blake.
 
-# wchar.m4 serial 6
+# wchar.m4 serial 7
 
 AC_DEFUN([gl_WCHAR_H],
 [
@@ -61,8 +61,10 @@ AC_DEFUN([gl_WCHAR_MODULE_INDICATOR],
 
 AC_DEFUN([gl_WCHAR_H_DEFAULTS],
 [
+  GNULIB_MBSINIT=0; AC_SUBST([GNULIB_MBSINIT])
   GNULIB_WCWIDTH=0; AC_SUBST([GNULIB_WCWIDTH])
   dnl Assume proper GNU behavior unless another module says otherwise.
+  HAVE_MBSINIT=1;      AC_SUBST([HAVE_MBSINIT])
   HAVE_DECL_WCWIDTH=1; AC_SUBST([HAVE_DECL_WCWIDTH])
   REPLACE_WCWIDTH=0;   AC_SUBST([REPLACE_WCWIDTH])
   WCHAR_H='';          AC_SUBST([WCHAR_H])
diff --git a/modules/mbsinit b/modules/mbsinit
new file mode 100644 (file)
index 0000000..e8a3ef9
--- /dev/null
@@ -0,0 +1,26 @@
+Description:
+mbsinit() function: test for initial conversion state.
+
+Files:
+lib/mbsinit.c
+m4/mbsinit.m4
+m4/mbstate_t.m4
+
+Depends-on:
+wchar
+
+configure.ac:
+gl_FUNC_MBSINIT
+gl_WCHAR_MODULE_INDICATOR([mbsinit])
+
+Makefile.am:
+
+Include:
+<wchar.h>
+
+License:
+LGPL
+
+Maintainer:
+Bruno Haible
+
index 381f475..7b22002 100644 (file)
@@ -25,8 +25,10 @@ wchar.h: wchar.in.h
              -e 's|@''PRAGMA_SYSTEM_HEADER''@|@PRAGMA_SYSTEM_HEADER@|g' \
              -e 's|@''NEXT_WCHAR_H''@|$(NEXT_WCHAR_H)|g' \
              -e 's/@''HAVE_WCHAR_H''@/$(HAVE_WCHAR_H)/g' \
+             -e 's|@''GNULIB_MBSINIT''@|$(GNULIB_MBSINIT)|g' \
              -e 's|@''GNULIB_WCWIDTH''@|$(GNULIB_WCWIDTH)|g' \
              -e 's/@''HAVE_WINT_T''@/$(HAVE_WINT_T)/g' \
+             -e 's|@''HAVE_MBSINIT''@|$(HAVE_MBSINIT)|g' \
              -e 's|@''HAVE_DECL_WCWIDTH''@|$(HAVE_DECL_WCWIDTH)|g' \
              -e 's|@''REPLACE_WCWIDTH''@|$(REPLACE_WCWIDTH)|g' \
              -e '/definition of GL_LINK_WARNING/r $(LINK_WARNING_H)' \