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