From b2be3149b474c01f52851b630cd58941060edd4d Mon Sep 17 00:00:00 2001
From: Christopher Faylor <me@cgf.cx>
Date: Tue, 17 Dec 2002 03:49:34 +0000
Subject: [PATCH] * fhandler_termios.cc (fhandler_termios::line_edit): Return
 line_edit_error and remove last char from readahead buffer if accept_input()
 fails. * fhandler_tty.cc (fhandler_pty_master::accept_input): Return 0 and
 restore readahead buffer when tty slave pipe is full.

---
 winsup/cygwin/ChangeLog           |  9 +++++
 winsup/cygwin/fhandler_termios.cc |  7 +++-
 winsup/cygwin/fhandler_tty.cc     | 57 +++++++++++++++----------------
 3 files changed, 42 insertions(+), 31 deletions(-)

diff --git a/winsup/cygwin/ChangeLog b/winsup/cygwin/ChangeLog
index 4a5bf424d..f0015a183 100644
--- a/winsup/cygwin/ChangeLog
+++ b/winsup/cygwin/ChangeLog
@@ -1,3 +1,12 @@
+2002-12-16  Steve Osborn  <bub@io.com>
+	    Christopher Faylor <cgf@redhat.com>
+
+	* fhandler_termios.cc (fhandler_termios::line_edit): Return
+	line_edit_error and remove last char from readahead buffer if
+	accept_input() fails.
+	* fhandler_tty.cc (fhandler_pty_master::accept_input): Return 0 and
+	restore readahead buffer when tty slave pipe is full.
+
 2002-12-16  Christopher Faylor  <cgf@redhat.com>
 
 	* pinfo.cc (_pinfo::cmdline): Allocate sufficient space for myself
diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_termios.cc
index 16a826bd9..e6a113241 100644
--- a/winsup/cygwin/fhandler_termios.cc
+++ b/winsup/cygwin/fhandler_termios.cc
@@ -326,7 +326,12 @@ fhandler_termios::line_edit (const char *rptr, int nread, int always_accept)
       put_readahead (c);
       if (!iscanon || always_accept || input_done)
 	{
-	  (void) accept_input();
+	  if (!accept_input ()) 
+	    {
+	      ret = line_edit_error;
+	      eat_readahead (1);
+	      break;
+	    }
 	  ret = line_edit_input_done;
 	  input_done = 0;
 	}
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 60918f6bd..f5f0c79e2 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -145,51 +145,48 @@ fhandler_pty_master::doecho (const void *str, DWORD len)
 int
 fhandler_pty_master::accept_input ()
 {
-  DWORD bytes_left, written;
-  DWORD n;
-  DWORD rc;
-  char* p;
+  DWORD bytes_left;
+  int ret = 1;
 
-  rc = WaitForSingleObject (input_mutex, INFINITE);
+  (void) WaitForSingleObject (input_mutex, INFINITE);
 
-  bytes_left = n = eat_readahead (-1);
-  p = rabuf;
+  bytes_left = eat_readahead (-1);
 
-  if (n != 0)
+  if (!bytes_left)
     {
-      while (bytes_left > 0)
-	{
-	  termios_printf ("about to write %d chars to slave", bytes_left);
-	  rc = WriteFile (get_output_handle (), p, bytes_left, &written, NULL);
-	  if (!rc)
-	    {
-	      debug_printf ("error writing to pipe %E");
-	      get_ttyp ()->read_retval = -1;
-	      break;
-	    }
-	  else
-	    get_ttyp ()->read_retval = 1;
+      termios_printf ("sending EOF to slave");
+      get_ttyp ()->read_retval = 0;
+    }
+  else
+    {
+      char *p = rabuf;
+      DWORD rc;
+      DWORD written = 0;
 
+      termios_printf ("about to write %d chars to slave", bytes_left);
+      rc = WriteFile (get_output_handle (), p, bytes_left, &written, NULL);
+      if (!rc)
+	{
+	  debug_printf ("error writing to pipe %E");
+	  get_ttyp ()->read_retval = -1;
+	}
+      else
+	{
+	  get_ttyp ()->read_retval = 1;
 	  p += written;
 	  bytes_left -= written;
 	  if (bytes_left > 0)
 	    {
 	      debug_printf ("to_slave pipe is full");
-	      SetEvent (input_available_event);
-	      ReleaseMutex (input_mutex);
-	      Sleep (10);
-	      rc = WaitForSingleObject (input_mutex, INFINITE);
+	      puts_readahead (p, bytes_left);
+	      ret = 0;
 	    }
 	}
     }
-  else
-    {
-      termios_printf ("sending EOF to slave");
-      get_ttyp ()->read_retval = 0;
-    }
+
   SetEvent (input_available_event);
   ReleaseMutex (input_mutex);
-  return 1;
+  return ret;
 }
 
 static DWORD WINAPI