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