public inbox for cygwin-patches@cygwin.com
 help / color / mirror / Atom feed
* [PATCH] Cygwin: pty: Treat both CR and NL as line feed in transfer_inpup().
@ 2022-03-04 11:05 Takashi Yano
  2022-03-04 11:05 ` [PATCH] Cygwin: pty: Rearrange reset_switch_to_nat_pipe() calls Takashi Yano
  2022-03-04 11:05 ` [PATCH] Cygwin: pty: Omit transfer_input() call where it is no longer needed Takashi Yano
  0 siblings, 2 replies; 3+ messages in thread
From: Takashi Yano @ 2022-03-04 11:05 UTC (permalink / raw)
  To: cygwin-patches

- To make read() work properly in canonical mode, writing to the pty
  pipe should be done line by line. However, only CR was treated as
  line separator previously in transfer_input(). This patch fixes
  the issue.
---
 winsup/cygwin/fhandler_tty.cc | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 2f13e9990..76c5e2413 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -3938,9 +3938,11 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
 	    }
 	  /* Call WriteFile() line by line */
 	  char *p0 = ptr;
-	  char *p1 = ptr;
-	  while ((p1 = (char *) memchr (p0, '\r', len - (p0 - ptr))))
+	  char *p_cr, *p_nl;
+	  while ((p_cr = (char *) memchr (p0, '\r', len - (p0 - ptr)))
+		 || (p_nl = (char *) memchr (p0, '\n', len - (p0 - ptr))))
 	    {
+	      char *p1 = p_cr ? (p_nl ? min (p_cr, p_nl) : p_cr) : p_nl;
 	      *p1 = '\n';
 	      n = p1 - p0 + 1;
 	      if (n && WriteFile (to, p0, n, &n, NULL) && n)
-- 
2.35.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] Cygwin: pty: Rearrange reset_switch_to_nat_pipe() calls.
  2022-03-04 11:05 [PATCH] Cygwin: pty: Treat both CR and NL as line feed in transfer_inpup() Takashi Yano
@ 2022-03-04 11:05 ` Takashi Yano
  2022-03-04 11:05 ` [PATCH] Cygwin: pty: Omit transfer_input() call where it is no longer needed Takashi Yano
  1 sibling, 0 replies; 3+ messages in thread
From: Takashi Yano @ 2022-03-04 11:05 UTC (permalink / raw)
  To: cygwin-patches

- Previously, reset_switch_to_nat_pipe() is called from many places
  in pty code. This patch reorganizes that. With this patch, it is
  called only from bg_check() and setpgid_aux(). The calls which
  does not have enough reason have been omitted.
---
 winsup/cygwin/fhandler.h      |  1 +
 winsup/cygwin/fhandler_tty.cc | 22 ++++++++++------------
 winsup/cygwin/select.cc       |  2 --
 3 files changed, 11 insertions(+), 14 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c32dc7b57..e7cf17df0 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2378,6 +2378,7 @@ class fhandler_pty_slave: public fhandler_pty_common
   select_record *select_read (select_stuff *);
   select_record *select_write (select_stuff *);
   select_record *select_except (select_stuff *);
+  bg_check_types bg_check (int sig, bool dontsignal = false);
   virtual char const *ttyname () { return pc.dev.name (); }
   int __reg2 fstat (struct stat *buf);
   int __reg3 facl (int, int, struct acl *);
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 3332fefd6..2f13e9990 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1287,8 +1287,6 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
 
   push_process_state process_state (PID_TTYOU);
 
-  reset_switch_to_nat_pipe ();
-
   acquire_output_mutex (mutex_timeout);
   if (!process_opost_output (get_output_handle (), ptr, towrite, false,
 			     get_ttyp (), is_nonblocking ()))
@@ -1409,10 +1407,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
   push_process_state process_state (PID_TTYIN);
 
   if (ptr) /* Indicating not tcflush(). */
-    {
-      mask_switch_to_nat_pipe (true, true);
-      reset_switch_to_nat_pipe ();
-    }
+    mask_switch_to_nat_pipe (true, true);
 
   if (is_nonblocking () || !ptr) /* Indicating tcflush(). */
     time_to_wait = 0;
@@ -1669,7 +1664,6 @@ fhandler_pty_master::dup (fhandler_base *child, int)
 int
 fhandler_pty_slave::tcgetattr (struct termios *t)
 {
-  reset_switch_to_nat_pipe ();
   *t = get_ttyp ()->ti;
 
   /* Workaround for rlwrap */
@@ -1690,7 +1684,6 @@ fhandler_pty_slave::tcgetattr (struct termios *t)
 int
 fhandler_pty_slave::tcsetattr (int, const struct termios *t)
 {
-  reset_switch_to_nat_pipe ();
   acquire_output_mutex (mutex_timeout);
   get_ttyp ()->ti = *t;
   release_output_mutex ();
@@ -1704,8 +1697,6 @@ fhandler_pty_slave::tcflush (int queue)
 
   termios_printf ("tcflush(%d) handle %p", queue, get_handle ());
 
-  reset_switch_to_nat_pipe ();
-
   if (queue == TCIFLUSH || queue == TCIOFLUSH)
     {
       size_t len = UINT_MAX;
@@ -1725,7 +1716,6 @@ int
 fhandler_pty_slave::ioctl (unsigned int cmd, void *arg)
 {
   termios_printf ("ioctl (%x)", cmd);
-  reset_switch_to_nat_pipe ();
   int res = fhandler_termios::ioctl (cmd, arg);
   if (res <= 0)
     return res;
@@ -2489,6 +2479,13 @@ fhandler_pty_slave::setup_locale (void)
     }
 }
 
+bg_check_types
+fhandler_pty_slave::bg_check (int sig, bool dontsignal)
+{
+  reset_switch_to_nat_pipe ();
+  return fhandler_termios::bg_check (sig, dontsignal);
+}
+
 void
 fhandler_pty_slave::fixup_after_fork (HANDLE parent)
 {
@@ -2500,7 +2497,6 @@ fhandler_pty_slave::fixup_after_fork (HANDLE parent)
 void
 fhandler_pty_slave::fixup_after_exec ()
 {
-  reset_switch_to_nat_pipe ();
   create_invisible_console ();
 
   if (!close_on_exec ())
@@ -4104,6 +4100,8 @@ fhandler_pty_slave::cleanup_for_non_cygwin_app (handle_set_t *p, tty *ttyp,
 void
 fhandler_pty_slave::setpgid_aux (pid_t pid)
 {
+  reset_switch_to_nat_pipe ();
+
   WaitForSingleObject (pipe_sw_mutex, INFINITE);
   bool was_nat_fg = get_ttyp ()->nat_fg (tc ()->pgid);
   bool nat_fg = get_ttyp ()->nat_fg (pid);
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 64e35cf12..4f23dfdef 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1353,8 +1353,6 @@ peek_pty_slave (select_record *s, bool from_select)
   fhandler_base *fh = (fhandler_base *) s->fh;
   fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
 
-  ptys->reset_switch_to_nat_pipe ();
-
   if (s->read_selected)
     {
       if (s->read_ready)
-- 
2.35.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] Cygwin: pty: Omit transfer_input() call where it is no longer needed.
  2022-03-04 11:05 [PATCH] Cygwin: pty: Treat both CR and NL as line feed in transfer_inpup() Takashi Yano
  2022-03-04 11:05 ` [PATCH] Cygwin: pty: Rearrange reset_switch_to_nat_pipe() calls Takashi Yano
@ 2022-03-04 11:05 ` Takashi Yano
  1 sibling, 0 replies; 3+ messages in thread
From: Takashi Yano @ 2022-03-04 11:05 UTC (permalink / raw)
  To: cygwin-patches

- This patch removes the old code which calls transfer_input() but
  is no longer needed. These code was necessary indeed in the past,
  however, as a result of recent frequent code changes, it is no
  longer needed.
---
 winsup/cygwin/fhandler_tty.cc | 47 -----------------------------------
 1 file changed, 47 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 76c5e2413..a0a5a70ba 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1216,56 +1216,9 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void)
       && process_alive (get_ttyp ()->nat_pipe_owner_pid))
     {
       /* There is a process which owns nat pipe. */
-      if (!to_be_read_from_nat_pipe ()
-	  && get_ttyp ()->pty_input_state_eq (tty::to_nat))
-	{
-	  if (get_ttyp ()->pcon_activated)
-	    {
-	      HANDLE pcon_owner = OpenProcess (PROCESS_DUP_HANDLE, FALSE,
-					   get_ttyp ()->nat_pipe_owner_pid);
-	      if (pcon_owner)
-		{
-		  HANDLE h_pcon_in;
-		  DuplicateHandle (pcon_owner, get_ttyp ()->h_pcon_in,
-				   GetCurrentProcess (), &h_pcon_in,
-				   0, TRUE, DUPLICATE_SAME_ACCESS);
-		  DWORD target_pid = get_ttyp ()->nat_pipe_owner_pid;
-		  DWORD resume_pid = attach_console_temporarily (target_pid);
-		  WaitForSingleObject (input_mutex, mutex_timeout);
-		  transfer_input (tty::to_cyg, h_pcon_in, get_ttyp (),
-				  input_available_event);
-		  ReleaseMutex (input_mutex);
-		  resume_from_temporarily_attach (resume_pid);
-		  CloseHandle (h_pcon_in);
-		  CloseHandle (pcon_owner);
-		}
-	    }
-	  else if (!get_ttyp ()->nat_fg (get_ttyp ()->getpgid ())
-		   && get_ttyp ()->switch_to_nat_pipe)
-	    {
-	      WaitForSingleObject (input_mutex, mutex_timeout);
-	      acquire_attach_mutex (mutex_timeout);
-	      transfer_input (tty::to_cyg, get_handle_nat (), get_ttyp (),
-			      input_available_event);
-	      release_attach_mutex ();
-	      ReleaseMutex (input_mutex);
-	    }
-	}
       ReleaseMutex (pipe_sw_mutex);
       return;
     }
-  /* This input transfer is needed if non-cygwin app is terminated
-     by Ctrl-C or killed. */
-  WaitForSingleObject (input_mutex, mutex_timeout);
-  if (get_ttyp ()->switch_to_nat_pipe && !get_ttyp ()->pcon_activated
-      && get_ttyp ()->pty_input_state_eq (tty::to_nat))
-    {
-      acquire_attach_mutex (mutex_timeout);
-      transfer_input (tty::to_cyg, get_handle_nat (), get_ttyp (),
-		      input_available_event);
-      release_attach_mutex ();
-    }
-  ReleaseMutex (input_mutex);
   /* Clean up nat pipe state */
   get_ttyp ()->pty_input_state = tty::to_cyg;
   get_ttyp ()->nat_pipe_owner_pid = 0;
-- 
2.35.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-03-04 11:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-04 11:05 [PATCH] Cygwin: pty: Treat both CR and NL as line feed in transfer_inpup() Takashi Yano
2022-03-04 11:05 ` [PATCH] Cygwin: pty: Rearrange reset_switch_to_nat_pipe() calls Takashi Yano
2022-03-04 11:05 ` [PATCH] Cygwin: pty: Omit transfer_input() call where it is no longer needed Takashi Yano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).