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