[PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

Takashi Yano takashi.yano@nifty.ne.jp
Wed Sep 4 01:46:00 GMT 2019


- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 shows garbage ^[[H^[[J in
  some of emacs screens. These screens do not handle ANSI escape
  sequences. Therefore, clear screen is disabled on these screens.
---
 winsup/cygwin/fhandler_tty.cc | 26 +++++++++++++++++++-------
 winsup/cygwin/tty.cc          |  1 +
 winsup/cygwin/tty.h           |  1 +
 3 files changed, 21 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 283558985..a74c3eecf 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -972,6 +972,19 @@ skip_console_setting:
 void
 fhandler_pty_slave::reset_switch_to_pcon (void)
 {
+  if (get_ttyp ()->need_clear_screen)
+    {
+      const char *term = getenv ("TERM");
+      if (term && strcmp (term, "dumb") && !strstr (term, "emacs"))
+	{
+	  /* FIXME: Clearing sequence may not be "^[[H^[[J"
+	     depending on the terminal type. */
+	  DWORD n;
+	  WriteFile (get_output_handle_cyg (), "\033[H\033[J", 6, &n, NULL);
+	}
+      get_ttyp ()->need_clear_screen = false;
+    }
+
   if (ALWAYS_USE_PCON)
     return;
   if (isHybrid)
@@ -2808,14 +2821,13 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
 	      /* Clear screen to synchronize pseudo console screen buffer
 		 with real terminal. This is necessary because pseudo
 		 console screen buffer is empty at start. */
-	      /* FIXME: Clearing sequence may not be "^[[H^[[J"
-		 depending on the terminal type. */
-	      DWORD n;
-	      if (get_ttyp ()->num_pcon_attached_slaves == 0
-		  && !ALWAYS_USE_PCON)
+	      const char *term = getenv ("TERM");
+	      if (get_ttyp ()->num_pcon_attached_slaves == 0 &&
+		  term && strcmp (term, "dumb") &&
+		  term && !strstr (term, "emacs") &&
+		  !ALWAYS_USE_PCON)
 		/* Assume this is the first process using this pty slave. */
-		WriteFile (get_output_handle_cyg (),
-			   "\033[H\033[J", 6, &n, NULL);
+		get_ttyp ()->need_clear_screen = true;
 
 	      get_ttyp ()->num_pcon_attached_slaves ++;
 	    }
diff --git a/winsup/cygwin/tty.cc b/winsup/cygwin/tty.cc
index 9244267c0..c94aee3ba 100644
--- a/winsup/cygwin/tty.cc
+++ b/winsup/cygwin/tty.cc
@@ -243,6 +243,7 @@ tty::init ()
   pcon_pid = 0;
   num_pcon_attached_slaves = 0;
   TermCodePage = 20127; /* ASCII */
+  need_clear_screen = false;
 }
 
 HANDLE
diff --git a/winsup/cygwin/tty.h b/winsup/cygwin/tty.h
index d59b2027d..c2b0490d0 100644
--- a/winsup/cygwin/tty.h
+++ b/winsup/cygwin/tty.h
@@ -104,6 +104,7 @@ private:
   pid_t pcon_pid;
   int num_pcon_attached_slaves;
   UINT TermCodePage;
+  bool need_clear_screen;
 
 public:
   HANDLE from_master () const { return _from_master; }
-- 
2.21.0



More information about the Cygwin-patches mailing list