Blame SOURCES/coreutils-i18n-expand-unexpand.patch

04161d
From e87ab5b991b08092a7e07af82b3ec822a8604151 Mon Sep 17 00:00:00 2001
04161d
From: Ondrej Oprala <ooprala@redhat.com>
04161d
Date: Wed, 5 Aug 2015 09:15:09 +0200
04161d
Subject: [PATCH] expand,unexpand: add multibyte support
04161d
MIME-Version: 1.0
04161d
Content-Type: text/plain; charset=UTF-8
04161d
Content-Transfer-Encoding: 8bit
04161d
04161d
* NEWS: Mention the changes.
04161d
* bootstrap.conf: Add mbfile to the list of modules.
04161d
* configure.ac: Properly initialize mbfile.
04161d
* src/expand.c (expand): Iterate over multibyte characters properly.
04161d
* src/unexpand.c (unexpand): Iterate over multibyte characters
04161d
properly.
04161d
* tests/local.mk: Add new tests.
04161d
* tests/{expand,unexpand}/mb.sh: New tests.
04161d
04161d
Co-authored-by: Pádraig Brady <pbrady@redhat.com>
04161d
---
04161d
 bootstrap.conf       |   1 +
04161d
 configure.ac         |   2 +
04161d
 lib/mbfile.c         |   3 +
04161d
 lib/mbfile.h         | 255 +++++++++++++++++++++++++++++++++++++++++++++++++++
04161d
 m4/mbfile.m4         |  14 +++
04161d
 src/expand.c         |  43 +++++----
04161d
 src/local.mk         |   4 +-
04161d
 src/unexpand.c       |  54 +++++++----
04161d
 tests/expand/mb.sh   |  98 ++++++++++++++++++++
04161d
 tests/local.mk       |   2 +
04161d
 tests/unexpand/mb.sh |  97 ++++++++++++++++++++
04161d
 10 files changed, 535 insertions(+), 34 deletions(-)
04161d
 create mode 100644 lib/mbfile.c
04161d
 create mode 100644 lib/mbfile.h
04161d
 create mode 100644 m4/mbfile.m4
04161d
 create mode 100755 tests/expand/mb.sh
04161d
 create mode 100755 tests/unexpand/mb.sh
04161d
04161d
diff --git a/bootstrap.conf b/bootstrap.conf
04161d
index 8a0ff31..a1c78b2 100644
04161d
--- a/bootstrap.conf
04161d
+++ b/bootstrap.conf
04161d
@@ -152,6 +152,7 @@ gnulib_modules="
04161d
   maintainer-makefile
04161d
   malloc-gnu
04161d
   manywarnings
04161d
+  mbfile
04161d
   mbrlen
04161d
   mbrtowc
04161d
   mbsalign
04161d
diff --git a/configure.ac b/configure.ac
04161d
index 1e74b36..24c9725 100644
04161d
--- a/configure.ac
04161d
+++ b/configure.ac
04161d
@@ -427,6 +427,8 @@ fi
04161d
 # I'm leaving it here for now.  This whole thing needs to be modernized...
04161d
 gl_WINSIZE_IN_PTEM
04161d
 
04161d
+gl_MBFILE
04161d
+
04161d
 gl_HEADER_TIOCGWINSZ_IN_TERMIOS_H
04161d
 
04161d
 if test $gl_cv_sys_tiocgwinsz_needs_termios_h = no && \
04161d
diff --git a/lib/mbfile.c b/lib/mbfile.c
04161d
new file mode 100644
04161d
index 0000000..b0a468e
04161d
--- /dev/null
04161d
+++ b/lib/mbfile.c
04161d
@@ -0,0 +1,3 @@
04161d
+#include <config.h>
04161d
+#define MBFILE_INLINE _GL_EXTERN_INLINE
04161d
+#include "mbfile.h"
04161d
diff --git a/lib/mbfile.h b/lib/mbfile.h
04161d
new file mode 100644
04161d
index 0000000..11f1b12
04161d
--- /dev/null
04161d
+++ b/lib/mbfile.h
04161d
@@ -0,0 +1,255 @@
04161d
+/* Multibyte character I/O: macros for multi-byte encodings.
04161d
+   Copyright (C) 2001, 2005, 2009-2015 Free Software Foundation, Inc.
04161d
+
04161d
+   This program is free software: you can redistribute it and/or modify
04161d
+   it under the terms of the GNU General Public License as published by
04161d
+   the Free Software Foundation; either version 3 of the License, or
04161d
+   (at your option) any later version.
04161d
+
04161d
+   This program is distributed in the hope that it will be useful,
04161d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
04161d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
04161d
+   GNU General Public License for more details.
04161d
+
04161d
+   You should have received a copy of the GNU General Public License
04161d
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
04161d
+
04161d
+/* Written by Mitsuru Chinen <mchinen@yamato.ibm.com>
04161d
+   and Bruno Haible <bruno@clisp.org>.  */
04161d
+
04161d
+/* The macros in this file implement multi-byte character input from a
04161d
+   stream.
04161d
+
04161d
+   mb_file_t
04161d
+     is the type for multibyte character input stream, usable for variable
04161d
+     declarations.
04161d
+
04161d
+   mbf_char_t
04161d
+     is the type for multibyte character or EOF, usable for variable
04161d
+     declarations.
04161d
+
04161d
+   mbf_init (mbf, stream)
04161d
+     initializes the MB_FILE for reading from stream.
04161d
+
04161d
+   mbf_getc (mbc, mbf)
04161d
+     reads the next multibyte character from mbf and stores it in mbc.
04161d
+
04161d
+   mb_iseof (mbc)
04161d
+     returns true if mbc represents the EOF value.
04161d
+
04161d
+   Here are the function prototypes of the macros.
04161d
+
04161d
+   extern void          mbf_init (mb_file_t mbf, FILE *stream);
04161d
+   extern void          mbf_getc (mbf_char_t mbc, mb_file_t mbf);
04161d
+   extern bool          mb_iseof (const mbf_char_t mbc);
04161d
+ */
04161d
+
04161d
+#ifndef _MBFILE_H
04161d
+#define _MBFILE_H 1
04161d
+
04161d
+#include <assert.h>
04161d
+#include <stdbool.h>
04161d
+#include <stdio.h>
04161d
+#include <string.h>
04161d
+
04161d
+/* Tru64 with Desktop Toolkit C has a bug: <stdio.h> must be included before
04161d
+   <wchar.h>.
04161d
+   BSD/OS 4.1 has a bug: <stdio.h> and <time.h> must be included before
04161d
+   <wchar.h>.  */
04161d
+#include <stdio.h>
04161d
+#include <time.h>
04161d
+#include <wchar.h>
04161d
+
04161d
+#include "mbchar.h"
04161d
+
04161d
+#ifndef _GL_INLINE_HEADER_BEGIN
04161d
+ #error "Please include config.h first."
04161d
+#endif
04161d
+_GL_INLINE_HEADER_BEGIN
04161d
+#ifndef MBFILE_INLINE
04161d
+# define MBFILE_INLINE _GL_INLINE
04161d
+#endif
04161d
+
04161d
+struct mbfile_multi {
04161d
+  FILE *fp;
04161d
+  bool eof_seen;
04161d
+  bool have_pushback;
04161d
+  mbstate_t state;
04161d
+  unsigned int bufcount;
04161d
+  char buf[MBCHAR_BUF_SIZE];
04161d
+  struct mbchar pushback;
04161d
+};
04161d
+
04161d
+MBFILE_INLINE void
04161d
+mbfile_multi_getc (struct mbchar *mbc, struct mbfile_multi *mbf)
04161d
+{
04161d
+  size_t bytes;
04161d
+
04161d
+  /* If EOF has already been seen, don't use getc.  This matters if
04161d
+     mbf->fp is connected to an interactive tty.  */
04161d
+  if (mbf->eof_seen)
04161d
+    goto eof;
04161d
+
04161d
+  /* Return character pushed back, if there is one.  */
04161d
+  if (mbf->have_pushback)
04161d
+    {
04161d
+      mb_copy (mbc, &mbf->pushback);
04161d
+      mbf->have_pushback = false;
04161d
+      return;
04161d
+    }
04161d
+
04161d
+  /* Before using mbrtowc, we need at least one byte.  */
04161d
+  if (mbf->bufcount == 0)
04161d
+    {
04161d
+      int c = getc (mbf->fp);
04161d
+      if (c == EOF)
04161d
+        {
04161d
+          mbf->eof_seen = true;
04161d
+          goto eof;
04161d
+        }
04161d
+      mbf->buf[0] = (unsigned char) c;
04161d
+      mbf->bufcount++;
04161d
+    }
04161d
+
04161d
+  /* Handle most ASCII characters quickly, without calling mbrtowc().  */
04161d
+  if (mbf->bufcount == 1 && mbsinit (&mbf->state) && is_basic (mbf->buf[0]))
04161d
+    {
04161d
+      /* These characters are part of the basic character set.  ISO C 99
04161d
+         guarantees that their wide character code is identical to their
04161d
+         char code.  */
04161d
+      mbc->wc = mbc->buf[0] = mbf->buf[0];
04161d
+      mbc->wc_valid = true;
04161d
+      mbc->ptr = &mbc->buf[0];
04161d
+      mbc->bytes = 1;
04161d
+      mbf->bufcount = 0;
04161d
+      return;
04161d
+    }
04161d
+
04161d
+  /* Use mbrtowc on an increasing number of bytes.  Read only as many bytes
04161d
+     from mbf->fp as needed.  This is needed to give reasonable interactive
04161d
+     behaviour when mbf->fp is connected to an interactive tty.  */
04161d
+  for (;;)
04161d
+    {
04161d
+      /* We don't know whether the 'mbrtowc' function updates the state when
04161d
+         it returns -2, - this is the ISO C 99 and glibc-2.2 behaviour - or
04161d
+         not - amended ANSI C, glibc-2.1 and Solaris 2.7 behaviour.  We
04161d
+         don't have an autoconf test for this, yet.
04161d
+         The new behaviour would allow us to feed the bytes one by one into
04161d
+         mbrtowc.  But the old behaviour forces us to feed all bytes since
04161d
+         the end of the last character into mbrtowc.  Since we want to retry
04161d
+         with more bytes when mbrtowc returns -2, we must backup the state
04161d
+         before calling mbrtowc, because implementations with the new
04161d
+         behaviour will clobber it.  */
04161d
+      mbstate_t backup_state = mbf->state;
04161d
+
04161d
+      bytes = mbrtowc (&mbc->wc, &mbf->buf[0], mbf->bufcount, &mbf->state);
04161d
+
04161d
+      if (bytes == (size_t) -1)
04161d
+        {
04161d
+          /* An invalid multibyte sequence was encountered.  */
04161d
+          /* Return a single byte.  */
04161d
+          bytes = 1;
04161d
+          mbc->wc_valid = false;
04161d
+          break;
04161d
+        }
04161d
+      else if (bytes == (size_t) -2)
04161d
+        {
04161d
+          /* An incomplete multibyte character.  */
04161d
+          mbf->state = backup_state;
04161d
+          if (mbf->bufcount == MBCHAR_BUF_SIZE)
04161d
+            {
04161d
+              /* An overlong incomplete multibyte sequence was encountered.  */
04161d
+              /* Return a single byte.  */
04161d
+              bytes = 1;
04161d
+              mbc->wc_valid = false;
04161d
+              break;
04161d
+            }
04161d
+          else
04161d
+            {
04161d
+              /* Read one more byte and retry mbrtowc.  */
04161d
+              int c = getc (mbf->fp);
04161d
+              if (c == EOF)
04161d
+                {
04161d
+                  /* An incomplete multibyte character at the end.  */
04161d
+                  mbf->eof_seen = true;
04161d
+                  bytes = mbf->bufcount;
04161d
+                  mbc->wc_valid = false;
04161d
+                  break;
04161d
+                }
04161d
+              mbf->buf[mbf->bufcount] = (unsigned char) c;
04161d
+              mbf->bufcount++;
04161d
+            }
04161d
+        }
04161d
+      else
04161d
+        {
04161d
+          if (bytes == 0)
04161d
+            {
04161d
+              /* A null wide character was encountered.  */
04161d
+              bytes = 1;
04161d
+              assert (mbf->buf[0] == '\0');
04161d
+              assert (mbc->wc == 0);
04161d
+            }
04161d
+          mbc->wc_valid = true;
04161d
+          break;
04161d
+        }
04161d
+    }
04161d
+
04161d
+  /* Return the multibyte sequence mbf->buf[0..bytes-1].  */
04161d
+  mbc->ptr = &mbc->buf[0];
04161d
+  memcpy (&mbc->buf[0], &mbf->buf[0], bytes);
04161d
+  mbc->bytes = bytes;
04161d
+
04161d
+  mbf->bufcount -= bytes;
04161d
+  if (mbf->bufcount > 0)
04161d
+    {
04161d
+      /* It's not worth calling memmove() for so few bytes.  */
04161d
+      unsigned int count = mbf->bufcount;
04161d
+      char *p = &mbf->buf[0];
04161d
+
04161d
+      do
04161d
+        {
04161d
+          *p = *(p + bytes);
04161d
+          p++;
04161d
+        }
04161d
+      while (--count > 0);
04161d
+    }
04161d
+  return;
04161d
+
04161d
+eof:
04161d
+  /* An mbchar_t with bytes == 0 is used to indicate EOF.  */
04161d
+  mbc->ptr = NULL;
04161d
+  mbc->bytes = 0;
04161d
+  mbc->wc_valid = false;
04161d
+  return;
04161d
+}
04161d
+
04161d
+MBFILE_INLINE void
04161d
+mbfile_multi_ungetc (const struct mbchar *mbc, struct mbfile_multi *mbf)
04161d
+{
04161d
+  mb_copy (&mbf->pushback, mbc);
04161d
+  mbf->have_pushback = true;
04161d
+}
04161d
+
04161d
+typedef struct mbfile_multi mb_file_t;
04161d
+
04161d
+typedef mbchar_t mbf_char_t;
04161d
+
04161d
+#define mbf_init(mbf, stream)                                           \
04161d
+  ((mbf).fp = (stream),                                                 \
04161d
+   (mbf).eof_seen = false,                                              \
04161d
+   (mbf).have_pushback = false,                                         \
04161d
+   memset (&(mbf).state, '\0', sizeof (mbstate_t)),                     \
04161d
+   (mbf).bufcount = 0)
04161d
+
04161d
+#define mbf_getc(mbc, mbf) mbfile_multi_getc (&(mbc), &(mbf))
04161d
+
04161d
+#define mbf_ungetc(mbc, mbf) mbfile_multi_ungetc (&(mbc), &(mbf))
04161d
+
04161d
+#define mb_iseof(mbc) ((mbc).bytes == 0)
04161d
+
04161d
+#ifndef _GL_INLINE_HEADER_BEGIN
04161d
+ #error "Please include config.h first."
04161d
+#endif
04161d
+_GL_INLINE_HEADER_BEGIN
04161d
+
04161d
+#endif /* _MBFILE_H */
04161d
diff --git a/m4/mbfile.m4 b/m4/mbfile.m4
04161d
new file mode 100644
04161d
index 0000000..8589902
04161d
--- /dev/null
04161d
+++ b/m4/mbfile.m4
04161d
@@ -0,0 +1,14 @@
04161d
+# mbfile.m4 serial 7
04161d
+dnl Copyright (C) 2005, 2008-2015 Free Software Foundation, Inc.
04161d
+dnl This file is free software; the Free Software Foundation
04161d
+dnl gives unlimited permission to copy and/or distribute it,
04161d
+dnl with or without modifications, as long as this notice is preserved.
04161d
+
04161d
+dnl autoconf tests required for use of mbfile.h
04161d
+dnl From Bruno Haible.
04161d
+
04161d
+AC_DEFUN([gl_MBFILE],
04161d
+[
04161d
+  AC_REQUIRE([AC_TYPE_MBSTATE_T])
04161d
+  :
04161d
+])
04161d
diff --git a/src/expand.c b/src/expand.c
04161d
index 9fa2e10..380e020 100644
04161d
--- a/src/expand.c
04161d
+++ b/src/expand.c
04161d
@@ -37,6 +37,9 @@
04161d
 #include <stdio.h>
04161d
 #include <getopt.h>
04161d
 #include <sys/types.h>
04161d
+
04161d
+#include <mbfile.h>
04161d
+
04161d
 #include "system.h"
04161d
 #include "die.h"
04161d
 #include "xstrndup.h"
04161d
@@ -100,19 +103,19 @@ expand (void)
04161d
 {
04161d
   /* Input stream.  */
04161d
   FILE *fp = next_file (NULL);
04161d
+  mb_file_t mbf;
04161d
+  mbf_char_t c;
04161d
 
04161d
   if (!fp)
04161d
     return;
04161d
 
04161d
+  mbf_init (mbf, fp);
04161d
+
04161d
   while (true)
04161d
     {
04161d
-      /* Input character, or EOF.  */
04161d
-      int c;
04161d
-
04161d
       /* If true, perform translations.  */
04161d
       bool convert = true;
04161d
 
04161d
-
04161d
       /* The following variables have valid values only when CONVERT
04161d
          is true:  */
04161d
 
04161d
@@ -122,17 +125,23 @@ expand (void)
04161d
       /* Index in TAB_LIST of next tab stop to examine.  */
04161d
       size_t tab_index = 0;
04161d
 
04161d
-
04161d
       /* Convert a line of text.  */
04161d
 
04161d
       do
04161d
         {
04161d
-          while ((c = getc (fp)) < 0 && (fp = next_file (fp)))
04161d
-            continue;
04161d
+          do {
04161d
+            mbf_getc (c, mbf);
04161d
+            if (mb_iseof (c))
04161d
+              {
04161d
+                mbf_init (mbf, fp = next_file (fp));
04161d
+                continue;
04161d
+              }
04161d
+            }
04161d
+          while (false);
04161d
 
04161d
           if (convert)
04161d
             {
04161d
-              if (c == '\t')
04161d
+              if (mb_iseq (c, '\t'))
04161d
                 {
04161d
                   /* Column the next input tab stop is on.  */
04161d
                   uintmax_t next_tab_column;
04161d
@@ -151,32 +160,34 @@ expand (void)
04161d
                     if (putchar (' ') < 0)
04161d
                       die (EXIT_FAILURE, errno, _("write error"));
04161d
 
04161d
-                  c = ' ';
04161d
+                  mb_setascii (&c, ' ');
04161d
                 }
04161d
-              else if (c == '\b')
04161d
+              else if (mb_iseq (c, '\b'))
04161d
                 {
04161d
                   /* Go back one column, and force recalculation of the
04161d
                      next tab stop.  */
04161d
                   column -= !!column;
04161d
                   tab_index -= !!tab_index;
04161d
                 }
04161d
-              else
04161d
+              /* A leading control character could make us trip over.  */
04161d
+              else if (!mb_iscntrl (c))
04161d
                 {
04161d
-                  column++;
04161d
+                  column += mb_width (c);
04161d
                   if (!column)
04161d
                     die (EXIT_FAILURE, 0, _("input line is too long"));
04161d
                 }
04161d
 
04161d
-              convert &= convert_entire_line || !! isblank (c);
04161d
+              convert &= convert_entire_line || mb_isblank (c);
04161d
             }
04161d
 
04161d
-          if (c < 0)
04161d
+          if (mb_iseof (c))
04161d
             return;
04161d
 
04161d
-          if (putchar (c) < 0)
04161d
+          mb_putc (c, stdout);
04161d
+          if (ferror (stdout))
04161d
             die (EXIT_FAILURE, errno, _("write error"));
04161d
         }
04161d
-      while (c != '\n');
04161d
+      while (!mb_iseq (c, '\n'));
04161d
     }
04161d
 }
04161d
 
04161d
diff --git a/src/local.mk b/src/local.mk
04161d
index 72db9c704..ef3bfa469 100644
04161d
--- a/src/local.mk
04161d
+++ b/src/local.mk
04161d
@@ -415,8 +415,8 @@ src_basenc_CPPFLAGS = -DBASE_TYPE=42 $(AM_CPPFLAGS)
04161d
 
04161d
 src_ginstall_CPPFLAGS = -DENABLE_MATCHPATHCON=1 $(AM_CPPFLAGS)
04161d
 
04161d
-src_expand_SOURCES = src/expand.c src/expand-common.c
04161d
-src_unexpand_SOURCES = src/unexpand.c src/expand-common.c
04161d
+src_expand_SOURCES = src/expand.c src/expand-common.c lib/mbfile.c
04161d
+src_unexpand_SOURCES = src/unexpand.c src/expand-common.c lib/mbfile.c
04161d
 
04161d
 # Ensure we don't link against libcoreutils.a as that lib is
04161d
 # not compiled with -fPIC which causes issues on 64 bit at least
04161d
diff --git a/src/unexpand.c b/src/unexpand.c
04161d
index 7801274..569a7ee 100644
04161d
--- a/src/unexpand.c
04161d
+++ b/src/unexpand.c
04161d
@@ -38,6 +38,9 @@
04161d
 #include <stdio.h>
04161d
 #include <getopt.h>
04161d
 #include <sys/types.h>
04161d
+
04161d
+#include <mbfile.h>
04161d
+
04161d
 #include "system.h"
04161d
 #include "die.h"
04161d
 #include "xstrndup.h"
04161d
@@ -107,11 +110,12 @@ unexpand (void)
04161d
 {
04161d
   /* Input stream.  */
04161d
   FILE *fp = next_file (NULL);
04161d
+  mb_file_t mbf;
04161d
 
04161d
   /* The array of pending blanks.  In non-POSIX locales, blanks can
04161d
      include characters other than spaces, so the blanks must be
04161d
      stored, not merely counted.  */
04161d
-  char *pending_blank;
04161d
+  mbf_char_t *pending_blank;
04161d
 
04161d
   if (!fp)
04161d
     return;
04161d
@@ -119,12 +123,14 @@ unexpand (void)
04161d
   /* The worst case is a non-blank character, then one blank, then a
04161d
      tab stop, then MAX_COLUMN_WIDTH - 1 blanks, then a non-blank; so
04161d
      allocate MAX_COLUMN_WIDTH bytes to store the blanks.  */
04161d
-  pending_blank = xmalloc (max_column_width);
04161d
+  pending_blank = xmalloc (max_column_width * sizeof (mbf_char_t));
04161d
+
04161d
+  mbf_init (mbf, fp);
04161d
 
04161d
   while (true)
04161d
     {
04161d
       /* Input character, or EOF.  */
04161d
-      int c;
04161d
+      mbf_char_t c;
04161d
 
04161d
       /* If true, perform translations.  */
04161d
       bool convert = true;
04161d
@@ -158,12 +164,19 @@ unexpand (void)
04161d
 
04161d
       do
04161d
         {
04161d
-          while ((c = getc (fp)) < 0 && (fp = next_file (fp)))
04161d
-            continue;
04161d
+          do {
04161d
+            mbf_getc (c, mbf);
04161d
+            if (mb_iseof (c))
04161d
+              {
04161d
+                mbf_init (mbf, fp = next_file (fp));
04161d
+                continue;
04161d
+              }
04161d
+            }
04161d
+          while (false);
04161d
 
04161d
           if (convert)
04161d
             {
04161d
-              bool blank = !! isblank (c);
04161d
+              bool blank = mb_isblank (c);
04161d
 
04161d
               if (blank)
04161d
                 {
04161d
@@ -180,16 +193,16 @@ unexpand (void)
04161d
                       if (next_tab_column < column)
04161d
                         die (EXIT_FAILURE, 0, _("input line is too long"));
04161d
 
04161d
-                      if (c == '\t')
04161d
+                      if (mb_iseq (c, '\t'))
04161d
                         {
04161d
                           column = next_tab_column;
04161d
 
04161d
                           if (pending)
04161d
-                            pending_blank[0] = '\t';
04161d
+                            mb_setascii (&pending_blank[0], '\t');
04161d
                         }
04161d
                       else
04161d
                         {
04161d
-                          column++;
04161d
+                          column += mb_width (c);
04161d
 
04161d
                           if (! (prev_blank && column == next_tab_column))
04161d
                             {
04161d
@@ -197,13 +210,14 @@ unexpand (void)
04161d
                                  will be replaced by tabs.  */
04161d
                               if (column == next_tab_column)
04161d
                                 one_blank_before_tab_stop = true;
04161d
-                              pending_blank[pending++] = c;
04161d
+                              mb_copy (&pending_blank[pending++], &c);
04161d
                               prev_blank = true;
04161d
                               continue;
04161d
                             }
04161d
 
04161d
                           /* Replace the pending blanks by a tab or two.  */
04161d
-                          pending_blank[0] = c = '\t';
04161d
+                          mb_setascii (&c, '\t');
04161d
+                          mb_setascii (&pending_blank[0], '\t');
04161d
                         }
04161d
 
04161d
                       /* Discard pending blanks, unless it was a single
04161d
@@ -211,7 +225,7 @@ unexpand (void)
04161d
                       pending = one_blank_before_tab_stop;
04161d
                     }
04161d
                 }
04161d
-              else if (c == '\b')
04161d
+              else if (mb_iseq (c, '\b'))
04161d
                 {
04161d
                   /* Go back one column, and force recalculation of the
04161d
                      next tab stop.  */
38ed2f
@@ -221,16 +235,20 @@ unexpand (void)
04161d
                 }
04161d
               else
04161d
                 {
04161d
-                  column++;
38ed2f
-                  if (!column)
38ed2f
+                  const uintmax_t orig_column = column;
04161d
+                  column += mb_width (c);
38ed2f
+                  if (column < orig_column)
04161d
                     die (EXIT_FAILURE, 0, _("input line is too long"));
04161d
                 }
38ed2f
 
04161d
               if (pending)
04161d
                 {
04161d
                   if (pending > 1 && one_blank_before_tab_stop)
04161d
-                    pending_blank[0] = '\t';
04161d
-                  if (fwrite (pending_blank, 1, pending, stdout) != pending)
04161d
+                    mb_setascii (&pending_blank[0], '\t');
04161d
+
04161d
+                  for (int n = 0; n < pending; ++n)
04161d
+                    mb_putc (pending_blank[n], stdout);
04161d
+                  if (ferror (stdout))
04161d
                     die (EXIT_FAILURE, errno, _("write error"));
04161d
                   pending = 0;
04161d
                   one_blank_before_tab_stop = false;
38ed2f
@@ -240,16 +258,17 @@ unexpand (void)
04161d
               convert &= convert_entire_line || blank;
04161d
             }
04161d
 
04161d
-          if (c < 0)
04161d
+          if (mb_iseof (c))
04161d
             {
04161d
               free (pending_blank);
04161d
               return;
04161d
             }
04161d
 
04161d
-          if (putchar (c) < 0)
04161d
+          mb_putc (c, stdout);
04161d
+          if (ferror (stdout))
04161d
             die (EXIT_FAILURE, errno, _("write error"));
04161d
         }
04161d
-      while (c != '\n');
04161d
+      while (!mb_iseq (c, '\n'));
04161d
     }
04161d
 }
04161d
 
04161d
diff --git a/tests/expand/mb.sh b/tests/expand/mb.sh
04161d
new file mode 100755
04161d
index 0000000..7971e18
04161d
--- /dev/null
04161d
+++ b/tests/expand/mb.sh
04161d
@@ -0,0 +1,98 @@
04161d
+#!/bin/sh
04161d
+
04161d
+# Copyright (C) 2012-2015 Free Software Foundation, Inc.
04161d
+
04161d
+# This program is free software: you can redistribute it and/or modify
04161d
+# it under the terms of the GNU General Public License as published by
04161d
+# the Free Software Foundation, either version 3 of the License, or
04161d
+# (at your option) any later version.
04161d
+
04161d
+# This program is distributed in the hope that it will be useful,
04161d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
04161d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
04161d
+# GNU General Public License for more details.
04161d
+
04161d
+# You should have received a copy of the GNU General Public License
04161d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
04161d
+
04161d
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
04161d
+print_ver_ expand
04161d
+
04161d
+export LC_ALL=en_US.UTF-8
04161d
+
04161d
+#input containing multibyte characters
04161d
+cat <<\EOF > in || framework_failure_
04161d
+1234567812345678123456781
04161d
+.       .       .       .
04161d
+a	b	c	d
04161d
+.       .       .       .
04161d
+ä	ö	ü	ß
04161d
+.       .       .       .
04161d
+EOF
04161d
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in || framework_failure_
04161d
+
04161d
+cat <<\EOF > exp || framework_failure_
04161d
+1234567812345678123456781
04161d
+.       .       .       .
04161d
+a       b       c       d
04161d
+.       .       .       .
04161d
+ä       ö       ü       ß
04161d
+.       .       .       .
04161d
+   äöü  .    öüä.       ä xx
04161d
+EOF
04161d
+
04161d
+expand < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+#test characters with display widths != 1
04161d
+env printf '12345678
04161d
+e\t|ascii(1)
04161d
+\u00E9\t|composed(1)
04161d
+e\u0301\t|decomposed(1)
04161d
+\u3000\t|ideo-space(2)
04161d
+\uFF0D\t|full-hypen(2)
04161d
+' > in || framework_failure_
04161d
+
04161d
+env printf '12345678
04161d
+e       |ascii(1)
04161d
+\u00E9       |composed(1)
04161d
+e\u0301       |decomposed(1)
04161d
+\u3000      |ideo-space(2)
04161d
+\uFF0D      |full-hypen(2)
04161d
+' > exp || framework_failure_
04161d
+
04161d
+expand < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+#shouldn't fail with "input line too long"
04161d
+#when a line starts with a control character
04161d
+env printf '\n' > in || framework_failure_
04161d
+
04161d
+expand < in > out || fail=1
04161d
+compare in out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+#non-Unicode characters interspersed between Unicode ones
04161d
+env printf '12345678
04161d
+\t\xFF|
04161d
+\xFF\t|
04161d
+\t\xFFä|
04161d
+ä\xFF\t|
04161d
+\tä\xFF|
04161d
+\xFF\tä|
04161d
+äbcdef\xFF\t|
04161d
+' > in || framework_failure_
04161d
+
04161d
+env printf '12345678
04161d
+        \xFF|
04161d
+\xFF       |
04161d
+        \xFFä|
04161d
+ä\xFF      |
04161d
+        ä\xFF|
04161d
+\xFF       ä|
04161d
+äbcdef\xFF |
04161d
+' > exp || framework_failure_
04161d
+
04161d
+expand < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+exit $fail
04161d
diff --git a/tests/local.mk b/tests/local.mk
04161d
index 192f776..8053397 100644
04161d
--- a/tests/local.mk
04161d
+++ b/tests/local.mk
04161d
@@ -544,6 +544,7 @@ all_tests =					\
04161d
   tests/du/threshold.sh				\
04161d
   tests/du/trailing-slash.sh			\
04161d
   tests/du/two-args.sh				\
04161d
+  tests/expand/mb.sh				\
04161d
   tests/id/gnu-zero-uids.sh			\
04161d
   tests/id/no-context.sh			\
04161d
   tests/id/context.sh				\
04161d
@@ -684,6 +685,7 @@ all_tests =					\
04161d
   tests/touch/read-only.sh			\
04161d
   tests/touch/relative.sh			\
04161d
   tests/touch/trailing-slash.sh			\
04161d
+  tests/unexpand/mb.sh				\
04161d
   $(all_root_tests)
04161d
 
04161d
 # See tests/factor/create-test.sh.
04161d
diff --git a/tests/unexpand/mb.sh b/tests/unexpand/mb.sh
04161d
new file mode 100755
04161d
index 0000000..60d4c1a
04161d
--- /dev/null
04161d
+++ b/tests/unexpand/mb.sh
04161d
@@ -0,0 +1,97 @@
04161d
+#!/bin/sh
04161d
+
04161d
+# Copyright (C) 2012-2015 Free Software Foundation, Inc.
04161d
+
04161d
+# This program is free software: you can redistribute it and/or modify
04161d
+# it under the terms of the GNU General Public License as published by
04161d
+# the Free Software Foundation, either version 3 of the License, or
04161d
+# (at your option) any later version.
04161d
+
04161d
+# This program is distributed in the hope that it will be useful,
04161d
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
04161d
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
04161d
+# GNU General Public License for more details.
04161d
+
04161d
+# You should have received a copy of the GNU General Public License
04161d
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
04161d
+
04161d
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
04161d
+print_ver_ unexpand
04161d
+
04161d
+export LC_ALL=en_US.UTF-8
04161d
+
04161d
+#input containing multibyte characters
04161d
+cat > in <<\EOF
04161d
+1234567812345678123456781
04161d
+.       .       .       .
04161d
+a       b       c       d
04161d
+.       .       .       .
04161d
+ä       ö       ü       ß
04161d
+.       .       .       .
04161d
+   äöü  .    öüä.       ä xx
04161d
+EOF
04161d
+
04161d
+cat > exp <<\EOF
04161d
+1234567812345678123456781
04161d
+.	.	.	.
04161d
+a	b	c	d
04161d
+.	.	.	.
04161d
+ä	ö	ü	ß
04161d
+.	.	.	.
04161d
+   äöü	.    öüä.	ä xx
04161d
+EOF
04161d
+
04161d
+unexpand -a < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+#test characters with a display width larger than 1
04161d
+
04161d
+env printf '12345678
04161d
+e       |ascii(1)
04161d
+\u00E9       |composed(1)
04161d
+e\u0301       |decomposed(1)
04161d
+\u3000      |ideo-space(2)
04161d
+\uFF0D      |full-hypen(2)
04161d
+' > in || framework_failure_
04161d
+
04161d
+env printf '12345678
04161d
+e\t|ascii(1)
04161d
+\u00E9\t|composed(1)
04161d
+e\u0301\t|decomposed(1)
04161d
+\u3000\t|ideo-space(2)
04161d
+\uFF0D\t|full-hypen(2)
04161d
+' > exp || framework_failure_
04161d
+
04161d
+unexpand -a < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+#test input where a blank of width > 1 is not being substituted
04161d
+in="$(LC_ALL=en_US.UTF-8 printf ' \u3000  ö       ü       ß')"
04161d
+exp='    ö	     ü	     ß'
04161d
+
04161d
+unexpand -a < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
+
04161d
+#non-Unicode characters interspersed between Unicode ones
04161d
+env printf '12345678
04161d
+        \xFF|
04161d
+\xFF       |
04161d
+        \xFFä|
04161d
+ä\xFF      |
04161d
+        ä\xFF|
04161d
+\xFF       ä|
04161d
+äbcdef\xFF |
04161d
+' > in || framework_failure_
04161d
+
04161d
+env printf '12345678
04161d
+\t\xFF|
04161d
+\xFF\t|
04161d
+\t\xFFä|
04161d
+ä\xFF\t|
04161d
+\tä\xFF|
04161d
+\xFF\tä|
04161d
+äbcdef\xFF\t|
04161d
+' > exp || framework_failure_
04161d
+
04161d
+unexpand -a < in > out || fail=1
04161d
+compare exp out > /dev/null 2>&1 || fail=1
04161d
-- 
04161d
2.7.4
04161d