From 4bf90e02b319405b807755d6d16e218e6295d207 Mon Sep 17 00:00:00 2001
From: Orgad Shaneh <orgads@gmail.com>
Date: Mon, 26 Feb 2018 08:10:42 +0200
Subject: [PATCH] Revert default mode on Cygwin from binary back to text.

1. The gawk info page states that:

> Under MS-Windows, 'gawk' (and many other text programs) silently
> translates end-of-line '\r\n' to '\n' on input and '\n' to '\r\n' on
> output.

This breaks compatibility between different gawk versions.

2. Git and other tools automatically convert text files to CRLF on Windows.
This means that any awk script that runs on both platforms must use RS =
"\r?\n". One example that was broken by this behavior change is gerrit's
commit-msg hook[1], which scans for empty lines by /^$/ regexp.

[1] https://gerrit.googlesource.com/gerrit/+/376a7bbb64f1b3f13c261f4efa0af0e8538cfe9b/resources/com/google/gerrit/server/tools/root/hooks/commit-msg#101

This reverts commits 5db38f775d9ba239e125d81dff2010a2ddacb48e
and partially 4197063335d758d0eacb8b4cb74b232ad596c784.
---
 doc/ChangeLog    |  4 ++++
 doc/gawk.info    |  8 --------
 doc/gawk.texi    |  5 -----
 doc/gawktexi.in  |  5 -----
 posix/ChangeLog  |  5 +++++
 posix/gawkmisc.c | 19 +++++++++++++++++++
 6 files changed, 28 insertions(+), 18 deletions(-)

diff --git a/doc/ChangeLog b/doc/ChangeLog
index fe48e088..f9bf5873 100644
--- a/doc/ChangeLog
+++ b/doc/ChangeLog
@@ -1,3 +1,7 @@
+2018-02-27         Orgad Shaneh     <orgads@gmail.com>
+
+	* Revert default mode from binary back to text on Cygwin.
+
 2018-02-17         Arnold D. Robbins     <arnold@skeeve.com>
 
 	* gawktexi.in: Further fix to NONFATAL stuff. Thanks to
diff --git a/doc/gawk.info b/doc/gawk.info
index 6ad34ae9..a9968401 100644
--- a/doc/gawk.info
+++ b/doc/gawk.info
@@ -29238,14 +29238,6 @@ Cygwin is the same as for a Unix system:
 on Cygwin takes considerably longer.  However, it does finish, and then
 the 'make' proceeds as usual.
 
-   Recent versions of Cygwin open all files in binary mode.  This means
-that you should use 'RS = "\r?\n"' in order to be able to handle
-standard MS-Windows text files with carriage-return plus line-feed line
-endings.
-
-   The Cygwin environment supports both the '|&' operator and TCP/IP
-networking (*note TCP/IP Networking::).
-
 
 File: gawk.info,  Node: MSYS,  Prev: Cygwin,  Up: PC Installation
 
diff --git a/doc/gawk.texi b/doc/gawk.texi
index 5c812c7d..367df446 100644
--- a/doc/gawk.texi
+++ b/doc/gawk.texi
@@ -39873,11 +39873,6 @@ When compared to GNU/Linux on the same system, the @samp{configure}
 step on Cygwin takes considerably longer.  However, it does finish,
 and then the @samp{make} proceeds as usual.
 
-Recent versions of Cygwin open all files in binary mode. This means
-that you should use @samp{RS = "\r?\n"} in order to be able to
-handle standard MS-Windows text files with carriage-return plus
-line-feed line endings.
-
 The Cygwin environment supports
 both the @samp{|&} operator and TCP/IP networking
 (@pxref{TCP/IP Networking}).
diff --git a/doc/gawktexi.in b/doc/gawktexi.in
index 0cd3bd47..db55608e 100644
--- a/doc/gawktexi.in
+++ b/doc/gawktexi.in
@@ -38847,11 +38847,6 @@ When compared to GNU/Linux on the same system, the @samp{configure}
 step on Cygwin takes considerably longer.  However, it does finish,
 and then the @samp{make} proceeds as usual.
 
-Recent versions of Cygwin open all files in binary mode. This means
-that you should use @samp{RS = "\r?\n"} in order to be able to
-handle standard MS-Windows text files with carriage-return plus
-line-feed line endings.
-
 The Cygwin environment supports
 both the @samp{|&} operator and TCP/IP networking
 (@pxref{TCP/IP Networking}).
diff --git a/posix/ChangeLog b/posix/ChangeLog
index 1b74804b..af1d51a2 100644
--- a/posix/ChangeLog
+++ b/posix/ChangeLog
@@ -1,3 +1,8 @@
+2018-02-26         Orgad Shaneh      <orgads@gmail.com>
+
+	* gawkmisc.c (cygwin_premain0, cygwin_premain2): Recover.
+	Broke stripping of \r on Windows.
+
 2017-10-19         Arnold D. Robbins     <arnold@skeeve.com>
 
 	* 4.2.0: Release tar ball made.
diff --git a/posix/gawkmisc.c b/posix/gawkmisc.c
index ecf5aff9..40950826 100644
--- a/posix/gawkmisc.c
+++ b/posix/gawkmisc.c
@@ -289,3 +289,22 @@ void
 init_sockets(void)
 {
 }
+
+#ifdef __CYGWIN__
+void
+cygwin_premain0(int argc, char **argv, struct per_process *myself)
+{
+	static struct __cygwin_perfile pf[] = {
+		{ "", O_RDONLY | O_TEXT },
+		/*{ "", O_WRONLY | O_BINARY },*/
+		{ NULL, 0 }
+	};
+	cygwin_internal(CW_PERFILE, pf);
+}
+
+void
+cygwin_premain2(int argc, char **argv, struct per_process *myself)
+{
+	setmode(fileno (stdin), O_TEXT);
+}
+#endif
-- 
2.16.2

