From c11b0343c069c6d69b3bdf329bcaeb550a18d9e9 Mon Sep 17 00:00:00 2001 From: Takashi Yano via Cygwin-patches Date: Tue, 30 Jun 2020 20:12:50 +0900 Subject: [PATCH] Cygwin: pty, termios: Unify thoughts of read ahead beffer handling. - Return value of eat_readahead() is redefined. The return values of fhandler_termios::eat_readahead() and fhandler_pty_slave:: eat_readahead() were little bit different. This patch unifies them to number of bytes eaten by eat_readahead(). - Considerration for raixget() is added to fhandler_pty_master:: accept_input() code. - Transfering contents of read ahead buffer in fhandler_pty_master::transfer_input_to_pcon() is removed since it is not necessary. - fhandler_pty_slave::eat_readahead() ckecks EOL only when ICANON is set. - Guard for _POSIX_VDISABLE is added in checking EOL. --- winsup/cygwin/fhandler_termios.cc | 20 ++++++++--------- winsup/cygwin/fhandler_tty.cc | 37 +++++++++++++------------------ 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/winsup/cygwin/fhandler_termios.cc b/winsup/cygwin/fhandler_termios.cc index b03478b87..ac1d5bd5c 100644 --- a/winsup/cygwin/fhandler_termios.cc +++ b/winsup/cygwin/fhandler_termios.cc @@ -268,25 +268,25 @@ fhandler_termios::eat_readahead (int n) { int oralen = ralen (); if (n < 0) - n = ralen (); - if (n > 0 && ralen () > 0) + n = ralen () - raixget (); + if (n > 0 && ralen () > raixget ()) { - if ((int) (ralen () -= n) < 0) - ralen () = 0; + if ((int) (ralen () -= n) < (int) raixget ()) + ralen () = raixget (); /* If IUTF8 is set, the terminal is in UTF-8 mode. If so, we erase a complete UTF-8 multibyte sequence on VERASE/VWERASE. Otherwise, if we only erase a single byte, invalid unicode chars are left in the input. */ if (tc ()->ti.c_iflag & IUTF8) - while (ralen () > 0 && + while (ralen () > raixget () && ((unsigned char) rabuf ()[ralen ()] & 0xc0) == 0x80) --ralen (); - - if (raixget () >= ralen ()) - raixget () = raixput () = ralen () = 0; - else if (raixput () > ralen ()) - raixput () = ralen (); } + oralen = oralen - ralen (); + if (raixget () >= ralen ()) + raixget () = raixput () = ralen () = 0; + else if (raixput () > ralen ()) + raixput () = ralen (); return oralen; } diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc index 0f95cec2e..a61167116 100644 --- a/winsup/cygwin/fhandler_tty.cc +++ b/winsup/cygwin/fhandler_tty.cc @@ -548,6 +548,7 @@ fhandler_pty_master::accept_input () WaitForSingleObject (input_mutex, INFINITE); + char *p = rabuf () + raixget (); bytes_left = eat_readahead (-1); if (to_be_read_from_pcon ()) @@ -559,7 +560,6 @@ fhandler_pty_master::accept_input () } else { - char *p = rabuf (); DWORD rc; DWORD written = 0; @@ -1171,10 +1171,10 @@ fhandler_pty_slave::update_pcon_input_state (bool need_lock) '\n', '\r' }; - if (is_line_input () && memchr (eols, c, sizeof (eols))) + if (is_line_input () && c && memchr (eols, c, sizeof (eols))) saw_accept = true; - if ((get_ttyp ()->ti.c_lflag & ISIG) && - memchr (sigs, c, sizeof (sigs))) + if ((get_ttyp ()->ti.c_lflag & ISIG) + && c && memchr (sigs, c, sizeof (sigs))) saw_accept = true; } get_ttyp ()->pcon_in_empty = pipe_empty && !(ralen () > raixget ()); @@ -1189,7 +1189,7 @@ fhandler_pty_slave::update_pcon_input_state (bool need_lock) int fhandler_pty_slave::eat_readahead (int n) { - int oralen = ralen () - raixget (); + int oralen = ralen (); if (n < 0) n = ralen () - raixget (); if (n > 0 && ralen () > raixget ()) @@ -1202,7 +1202,8 @@ fhandler_pty_slave::eat_readahead (int n) }; while (n > 0 && ralen () > raixget ()) { - if (memchr (eols, rabuf ()[ralen ()-1], sizeof (eols))) + if (is_line_input () && rabuf ()[ralen ()-1] + && memchr (eols, rabuf ()[ralen ()-1], sizeof (eols))) break; -- n; -- ralen (); @@ -1213,15 +1214,15 @@ fhandler_pty_slave::eat_readahead (int n) if we only erase a single byte, invalid unicode chars are left in the input. */ if (get_ttyp ()->ti.c_iflag & IUTF8) - while (ralen () > 0 && + while (ralen () > raixget () && ((unsigned char) rabuf ()[ralen ()] & 0xc0) == 0x80) --ralen (); - - if (raixget () >= ralen ()) - raixget () = raixput () = ralen () = 0; - else if (raixput () > ralen ()) - raixput () = ralen (); } + oralen = oralen - ralen (); + if (raixget () >= ralen ()) + raixget () = raixput () = ralen () = 0; + else if (raixput () > ralen ()) + raixput () = ralen (); return oralen; } @@ -1245,7 +1246,7 @@ fhandler_pty_slave::get_readahead_into_buffer (char *buf, size_t buflen) }; buf[copied_chars++] = (unsigned char)(ch & 0xff); buflen--; - if (is_line_input () && memchr (eols, ch & 0xff, sizeof (eols))) + if (is_line_input () && ch && memchr (eols, ch & 0xff, sizeof (eols))) break; } @@ -1264,7 +1265,7 @@ fhandler_pty_slave::get_readahead_valid (void) '\n' }; for (size_t i=raixget (); iti.c_lflag & ISIG) for (size_t i=0; ipcon_in_empty = false; ReleaseMutex (input_mutex);