8d0e4d
From 7a7c776a4e228d180e74614fd8c8afcad5d4bdf7 Mon Sep 17 00:00:00 2001
8d0e4d
From: Jakub Martisko <jamartis@redhat.com>
8d0e4d
Date: Thu, 7 Jul 2016 12:53:26 +0200
8d0e4d
Subject: [PATCH] coreutils-i18n-un-expand-BOM.patch
8d0e4d
8d0e4d
---
8d0e4d
 src/expand-common.c  | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++
8d0e4d
 src/expand-common.h  |  12 ++++++
8d0e4d
 src/expand.c         |  45 +++++++++++++++++++-
8d0e4d
 src/unexpand.c       |  43 ++++++++++++++++++-
8d0e4d
 tests/expand/mb.sh   |  71 ++++++++++++++++++++++++++++++++
8d0e4d
 tests/unexpand/mb.sh |  59 ++++++++++++++++++++++++++
8d0e4d
 6 files changed, 342 insertions(+), 2 deletions(-)
8d0e4d
8d0e4d
diff --git a/src/expand-common.c b/src/expand-common.c
8d0e4d
index 4657e46..97cbb09 100644
8d0e4d
--- a/src/expand-common.c
8d0e4d
+++ b/src/expand-common.c
8d0e4d
@@ -19,6 +19,7 @@
8d0e4d
 #include <assert.h>
8d0e4d
 #include <stdio.h>
8d0e4d
 #include <sys/types.h>
8d0e4d
+#include <mbfile.h>
8d0e4d
 #include "system.h"
8d0e4d
 #include "die.h"
8d0e4d
 #include "error.h"
8d0e4d
@@ -126,6 +127,119 @@ set_increment_size (uintmax_t tabval)
8d0e4d
   return ok;
8d0e4d
 }
8d0e4d
 
8d0e4d
+extern int
8d0e4d
+set_utf_locale (void)
8d0e4d
+{
8d0e4d
+      /*try using some predefined locale */
8d0e4d
+      const char* predef_locales[] = {"C.UTF8","en_US.UTF8","en_GB.UTF8"};
8d0e4d
+
8d0e4d
+      const int predef_locales_count=3;
8d0e4d
+      for (int i=0;i
8d0e4d
+        {
8d0e4d
+          if (setlocale(LC_ALL,predef_locales[i])!=NULL)
8d0e4d
+          {
8d0e4d
+            break;
8d0e4d
+          }
8d0e4d
+          else if (i==predef_locales_count-1)
8d0e4d
+          {
8d0e4d
+            return 1;
8d0e4d
+            error (EXIT_FAILURE, errno, _("cannot set UTF-8 locale"));
8d0e4d
+          }
8d0e4d
+        }
8d0e4d
+        return 0;
8d0e4d
+}
8d0e4d
+
8d0e4d
+extern bool
8d0e4d
+check_utf_locale(void)
8d0e4d
+{
8d0e4d
+  char* locale = setlocale (LC_CTYPE , NULL);
8d0e4d
+  if (locale == NULL)
8d0e4d
+  {
8d0e4d
+    return false;
8d0e4d
+  }
8d0e4d
+  else if (strcasestr(locale, "utf8") == NULL && strcasestr(locale, "utf-8") == NULL)
8d0e4d
+  {
8d0e4d
+    return false;
8d0e4d
+  }
8d0e4d
+  return true;
8d0e4d
+}
8d0e4d
+
8d0e4d
+extern bool
8d0e4d
+check_bom(FILE* fp, mb_file_t *mbf)
8d0e4d
+{
8d0e4d
+  int c;
8d0e4d
+
8d0e4d
+
8d0e4d
+  c=fgetc(fp);
8d0e4d
+
8d0e4d
+  /*test BOM header of the first file */
8d0e4d
+  mbf->bufcount=0;
8d0e4d
+  if (c == 0xEF)
8d0e4d
+  {
8d0e4d
+    c=fgetc(fp);
8d0e4d
+  }
8d0e4d
+  else
8d0e4d
+  {
8d0e4d
+    if (c != EOF)
8d0e4d
+    {
8d0e4d
+      ungetc(c,fp);
8d0e4d
+    }
8d0e4d
+    return false;
8d0e4d
+  }
8d0e4d
+
8d0e4d
+  if (c == 0xBB)
8d0e4d
+  {
8d0e4d
+    c=fgetc(fp);
8d0e4d
+  }
8d0e4d
+  else
8d0e4d
+  {
8d0e4d
+    if ( c!= EOF )
8d0e4d
+    {
8d0e4d
+      mbf->buf[0]=(unsigned char) 0xEF;
8d0e4d
+      mbf->bufcount=1;
8d0e4d
+      ungetc(c,fp);
8d0e4d
+      return false;
8d0e4d
+    }
8d0e4d
+    else
8d0e4d
+    {
8d0e4d
+      ungetc(0xEF,fp);
8d0e4d
+      return false;
8d0e4d
+    }
8d0e4d
+  }
8d0e4d
+  if (c == 0xBF)
8d0e4d
+  {
8d0e4d
+    mbf->bufcount=0;
8d0e4d
+    return true;
8d0e4d
+  }
8d0e4d
+  else
8d0e4d
+  {
8d0e4d
+    if (c != EOF)
8d0e4d
+    {
8d0e4d
+      mbf->buf[0]=(unsigned char) 0xEF;
8d0e4d
+      mbf->buf[1]=(unsigned char) 0xBB;
8d0e4d
+      mbf->bufcount=2;
8d0e4d
+      ungetc(c,fp);
8d0e4d
+      return false;
8d0e4d
+    }
8d0e4d
+    else
8d0e4d
+    {
8d0e4d
+      mbf->buf[0]=(unsigned char) 0xEF;
8d0e4d
+      mbf->bufcount=1;
8d0e4d
+      ungetc(0xBB,fp);
8d0e4d
+      return false;
8d0e4d
+    }
8d0e4d
+  }
8d0e4d
+  return false;
8d0e4d
+}
8d0e4d
+
8d0e4d
+extern void
8d0e4d
+print_bom(void)
8d0e4d
+{
8d0e4d
+  putc (0xEF, stdout);
8d0e4d
+  putc (0xBB, stdout);
8d0e4d
+  putc (0xBF, stdout);
8d0e4d
+}
8d0e4d
+
8d0e4d
 /* Add the comma or blank separated list of tab stops STOPS
8d0e4d
    to the list of tab stops.  */
8d0e4d
 extern void
8d0e4d
diff --git a/src/expand-common.h b/src/expand-common.h
8d0e4d
index 8cb2079..763bfda 100644
8d0e4d
--- a/src/expand-common.h
8d0e4d
+++ b/src/expand-common.h
8d0e4d
@@ -34,6 +34,18 @@ extern size_t max_column_width;
8d0e4d
 /* The desired exit status.  */
8d0e4d
 extern int exit_status;
8d0e4d
 
8d0e4d
+extern int
8d0e4d
+set_utf_locale (void);
8d0e4d
+
8d0e4d
+extern bool
8d0e4d
+check_utf_locale(void);
8d0e4d
+
8d0e4d
+extern bool
8d0e4d
+check_bom(FILE* fp, mb_file_t *mbf);
8d0e4d
+
8d0e4d
+extern void
8d0e4d
+print_bom(void);
8d0e4d
+
8d0e4d
 /* Add tab stop TABVAL to the end of 'tab_list'.  */
8d0e4d
 extern void
8d0e4d
 add_tab_stop (uintmax_t tabval);
8d0e4d
diff --git a/src/expand.c b/src/expand.c
8d0e4d
index 310b349..4136824 100644
8d0e4d
--- a/src/expand.c
8d0e4d
+++ b/src/expand.c
8d0e4d
@@ -103,11 +103,33 @@ expand (void)
8d0e4d
   FILE *fp = next_file (NULL);
8d0e4d
   mb_file_t mbf;
8d0e4d
   mbf_char_t c;
8d0e4d
+  /* True if the starting locale is utf8.  */
8d0e4d
+  bool using_utf_locale;
8d0e4d
+
8d0e4d
+  /* True if the first file contains BOM header.  */
8d0e4d
+  bool found_bom;
8d0e4d
+  using_utf_locale=check_utf_locale();
8d0e4d
 
8d0e4d
   if (!fp)
8d0e4d
     return;
8d0e4d
-
8d0e4d
   mbf_init (mbf, fp);
8d0e4d
+  found_bom=check_bom(fp,&mbf);
8d0e4d
+
8d0e4d
+  if (using_utf_locale == false && found_bom == true)
8d0e4d
+  {
8d0e4d
+    /*try using some predefined locale */
8d0e4d
+
8d0e4d
+    if (set_utf_locale () != 0)
8d0e4d
+    {
8d0e4d
+      error (EXIT_FAILURE, errno, _("cannot set UTF-8 locale"));
8d0e4d
+    }
8d0e4d
+  }
8d0e4d
+
8d0e4d
+
8d0e4d
+  if (found_bom == true)
8d0e4d
+  {
8d0e4d
+    print_bom();
8d0e4d
+  }
8d0e4d
 
8d0e4d
   while (true)
8d0e4d
     {
8d0e4d
@@ -132,6 +154,27 @@ expand (void)
8d0e4d
             if ((mb_iseof (c)) && (fp = next_file (fp)))
8d0e4d
               {
8d0e4d
                 mbf_init (mbf, fp);
8d0e4d
+                if (fp!=NULL)
8d0e4d
+                {
8d0e4d
+                  if (check_bom(fp,&mbf)==true)
8d0e4d
+                  {
8d0e4d
+                    /*Not the first file - check BOM header*/
8d0e4d
+                    if (using_utf_locale==false && found_bom==false)
8d0e4d
+                    {
8d0e4d
+                      /*BOM header in subsequent file but not in the first one. */
8d0e4d
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
8d0e4d
+                    }
8d0e4d
+                  }
8d0e4d
+                  else
8d0e4d
+                  {
8d0e4d
+                    if(using_utf_locale==false && found_bom==true)
8d0e4d
+                    {
8d0e4d
+                      /*First file conatined BOM header - locale was switched to UTF
8d0e4d
+                       *all subsequent files should contain BOM. */
8d0e4d
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
8d0e4d
+                    }
8d0e4d
+                  }
8d0e4d
+                }
8d0e4d
                 continue;
8d0e4d
               }
8d0e4d
             else
8d0e4d
diff --git a/src/unexpand.c b/src/unexpand.c
8d0e4d
index 863a90a..5681b58 100644
8d0e4d
--- a/src/unexpand.c
8d0e4d
+++ b/src/unexpand.c
8d0e4d
@@ -116,16 +116,36 @@ unexpand (void)
8d0e4d
      include characters other than spaces, so the blanks must be
8d0e4d
      stored, not merely counted.  */
8d0e4d
   mbf_char_t *pending_blank;
8d0e4d
+  /* True if the starting locale is utf8.  */
8d0e4d
+  bool using_utf_locale;
8d0e4d
+
8d0e4d
+  /* True if the first file contains BOM header.  */
8d0e4d
+  bool found_bom;
8d0e4d
+  using_utf_locale=check_utf_locale();
8d0e4d
 
8d0e4d
   if (!fp)
8d0e4d
     return;
8d0e4d
+  mbf_init (mbf, fp);
8d0e4d
+  found_bom=check_bom(fp,&mbf);
8d0e4d
+
8d0e4d
+  if (using_utf_locale == false && found_bom == true)
8d0e4d
+  {
8d0e4d
+    /*try using some predefined locale */
8d0e4d
 
8d0e4d
+    if (set_utf_locale () != 0)
8d0e4d
+    {
8d0e4d
+      error (EXIT_FAILURE, errno, _("cannot set UTF-8 locale"));
8d0e4d
+    }
8d0e4d
+  }
8d0e4d
   /* The worst case is a non-blank character, then one blank, then a
8d0e4d
      tab stop, then MAX_COLUMN_WIDTH - 1 blanks, then a non-blank; so
8d0e4d
      allocate MAX_COLUMN_WIDTH bytes to store the blanks.  */
8d0e4d
   pending_blank = xmalloc (max_column_width * sizeof (mbf_char_t));
8d0e4d
 
8d0e4d
-  mbf_init (mbf, fp);
8d0e4d
+  if (found_bom == true)
8d0e4d
+  {
8d0e4d
+    print_bom();
8d0e4d
+  }
8d0e4d
 
8d0e4d
   while (true)
8d0e4d
     {
8d0e4d
@@ -169,6 +189,27 @@ unexpand (void)
8d0e4d
             if ((mb_iseof (c)) && (fp = next_file (fp)))
8d0e4d
               {
8d0e4d
                 mbf_init (mbf, fp);
8d0e4d
+                if (fp!=NULL)
8d0e4d
+                {
8d0e4d
+                  if (check_bom(fp,&mbf)==true)
8d0e4d
+                  {
8d0e4d
+                    /*Not the first file - check BOM header*/
8d0e4d
+                    if (using_utf_locale==false && found_bom==false)
8d0e4d
+                    {
8d0e4d
+                      /*BOM header in subsequent file but not in the first one. */
8d0e4d
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
8d0e4d
+                    }
8d0e4d
+                  }
8d0e4d
+                  else
8d0e4d
+                  {
8d0e4d
+                    if(using_utf_locale==false && found_bom==true)
8d0e4d
+                    {
8d0e4d
+                      /*First file conatined BOM header - locale was switched to UTF
8d0e4d
+                       *all subsequent files should contain BOM. */
8d0e4d
+                      error (EXIT_FAILURE, errno, _("combination of files with and without BOM header"));
8d0e4d
+                    }
8d0e4d
+                  }
8d0e4d
+                }
8d0e4d
                 continue;
8d0e4d
               }
8d0e4d
             else
8d0e4d
diff --git a/tests/expand/mb.sh b/tests/expand/mb.sh
8d0e4d
index 031be7a..1621c84 100755
8d0e4d
--- a/tests/expand/mb.sh
8d0e4d
+++ b/tests/expand/mb.sh
8d0e4d
@@ -109,4 +109,75 @@ env printf '12345678
8d0e4d
 expand < in > out || fail=1
8d0e4d
 compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
 
8d0e4d
+
8d0e4d
+
8d0e4d
+#BOM header test 1
8d0e4d
+printf "\xEF\xBB\xBF" > in; cat <<\EOF >> in || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.       .       .       .
8d0e4d
+a	b	c	d
8d0e4d
+.       .       .       .
8d0e4d
+ä	ö	ü	ß
8d0e4d
+.       .       .       .
8d0e4d
+EOF
8d0e4d
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in || framework_failure_
8d0e4d
+
8d0e4d
+printf "\xEF\xBB\xBF" > exp; cat <<\EOF >> exp || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.       .       .       .
8d0e4d
+a       b       c       d
8d0e4d
+.       .       .       .
8d0e4d
+ä       ö       ü       ß
8d0e4d
+.       .       .       .
8d0e4d
+   äöü  .    öüä.       ä xx
8d0e4d
+EOF
8d0e4d
+
8d0e4d
+
8d0e4d
+expand < in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LANG=C expand < in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LC_ALL=C expand < in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+
8d0e4d
+printf '\xEF\xBB\xBF' > in1; cat <<\EOF >> in1 || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.       .       .       .
8d0e4d
+a	b	c	d
8d0e4d
+.       .       .       .
8d0e4d
+ä	ö	ü	ß
8d0e4d
+.       .       .       .
8d0e4d
+EOF
8d0e4d
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in1 || framework_failure_
8d0e4d
+
8d0e4d
+
8d0e4d
+printf '\xEF\xBB\xBF' > exp; cat <<\EOF >> exp || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.       .       .       .
8d0e4d
+a       b       c       d
8d0e4d
+.       .       .       .
8d0e4d
+ä       ö       ü       ß
8d0e4d
+.       .       .       .
8d0e4d
+   äöü  .    öüä.       ä xx
8d0e4d
+1234567812345678123456781
8d0e4d
+.       .       .       .
8d0e4d
+a       b       c       d
8d0e4d
+.       .       .       .
8d0e4d
+ä       ö       ü       ß
8d0e4d
+.       .       .       .
8d0e4d
+   äöü  .    öüä.       ä xx
8d0e4d
+EOF
8d0e4d
+
8d0e4d
+expand in1 in1 > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LANG=C expand in1 in1  > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LC_ALL=C expand in1 in1 > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
 exit $fail
8d0e4d
diff --git a/tests/unexpand/mb.sh b/tests/unexpand/mb.sh
8d0e4d
index 8d75652..9d4ee3e 100755
8d0e4d
--- a/tests/unexpand/mb.sh
8d0e4d
+++ b/tests/unexpand/mb.sh
8d0e4d
@@ -111,3 +111,62 @@ env printf '12345678
8d0e4d
 
8d0e4d
 unexpand -a < in > out || fail=1
8d0e4d
 compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+#BOM header test 1
8d0e4d
+printf "\xEF\xBB\xBF" > in; cat <<\EOF >> in || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.       .       .       .
8d0e4d
+a       b       c       d
8d0e4d
+.       .       .       .
8d0e4d
+ä       ö       ü       ß
8d0e4d
+.       .       .       .
8d0e4d
+   äöü  .    öüä.       ä xx
8d0e4d
+EOF
8d0e4d
+env printf '   äöü\t.    öüä.   \tä xx\n' >> in || framework_failure_
8d0e4d
+
8d0e4d
+printf "\xEF\xBB\xBF" > exp; cat <<\EOF >> exp || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.	.	.	.
8d0e4d
+a	b	c	d
8d0e4d
+.	.	.	.
8d0e4d
+ä	ö	ü	ß
8d0e4d
+.	.	.	.
8d0e4d
+   äöü	.    öüä.	ä xx
8d0e4d
+EOF
8d0e4d
+
8d0e4d
+unexpand < in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LANG=C unexpand < in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LC_ALL=C unexpand < in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+
8d0e4d
+printf "\xEF\xBB\xBF" > exp; cat <<\EOF >> exp || framework_failure_
8d0e4d
+1234567812345678123456781
8d0e4d
+.	.	.	.
8d0e4d
+a	b	c	d
8d0e4d
+.	.	.	.
8d0e4d
+ä	ö	ü	ß
8d0e4d
+.	.	.	.
8d0e4d
+   äöü	.    öüä.	ä xx
8d0e4d
+1234567812345678123456781
8d0e4d
+.	.	.	.
8d0e4d
+a	b	c	d
8d0e4d
+.	.	.	.
8d0e4d
+ä	ö	ü	ß
8d0e4d
+.	.	.	.
8d0e4d
+   äöü	.    öüä.	ä xx
8d0e4d
+EOF
8d0e4d
+
8d0e4d
+
8d0e4d
+unexpand in in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LANG=C unexpand in in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
+
8d0e4d
+LC_ALL=C unexpand in in > out || fail=1
8d0e4d
+compare exp out > /dev/null 2>&1 || fail=1
8d0e4d
-- 
8d0e4d
2.9.3
8d0e4d