From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7868) id 862DD3858292; Tue, 5 Jul 2022 03:11:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 862DD3858292 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Takashi Yano To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: console: Fix issue of pasting very long text input again. X-Act-Checkin: newlib-cygwin X-Git-Author: Takashi Yano X-Git-Refname: refs/heads/master X-Git-Oldrev: 72f855f32b627e50fc232bbd798f6b99991ac68d X-Git-Newrev: b3e25f0bc1bf69bde17b0a8004d9ed4755ebd18a Message-Id: <20220705031152.862DD3858292@sourceware.org> Date: Tue, 5 Jul 2022 03:11:52 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 05 Jul 2022 03:11:52 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Db3e25f0bc1b= f69bde17b0a8004d9ed4755ebd18a commit b3e25f0bc1bf69bde17b0a8004d9ed4755ebd18a Author: Takashi Yano Date: Tue Jul 5 11:36:33 2022 +0900 Cygwin: console: Fix issue of pasting very long text input again. =20 - The recent commit "Cygwin: console: Allow pasting very long text input." did not fix the issue enough. This patch adds fixes for that. Diff: --- winsup/cygwin/fhandler_console.cc | 87 +++++++++++++++++++++++++++++------= ---- 1 file changed, 65 insertions(+), 22 deletions(-) diff --git a/winsup/cygwin/fhandler_console.cc b/winsup/cygwin/fhandler_con= sole.cc index 52031fe31..47d30bc88 100644 --- a/winsup/cygwin/fhandler_console.cc +++ b/winsup/cygwin/fhandler_console.cc @@ -342,9 +342,9 @@ fhandler_console::cons_master_thread (handle_set_t *p, = tty *ttyp) { DWORD new_inrec_size =3D total_read + additional_space; INPUT_RECORD *new_input_rec =3D (INPUT_RECORD *) - realloc (input_rec, new_inrec_size * sizeof (INPUT_RECORD)); + realloc (input_rec, m::bytes (new_inrec_size)); INPUT_RECORD *new_input_tmp =3D (INPUT_RECORD *) - realloc (input_tmp, new_inrec_size * sizeof (INPUT_RECORD)); + realloc (input_tmp, m::bytes (new_inrec_size)); if (new_input_rec && new_input_tmp) { inrec_size =3D new_inrec_size; @@ -360,6 +360,7 @@ fhandler_console::cons_master_thread (handle_set_t *p, = tty *ttyp) switch (cygwait (p->input_handle, (DWORD) 0)) { case WAIT_OBJECT_0: + acquire_attach_mutex (mutex_timeout); total_read =3D 0; while (cygwait (p->input_handle, (DWORD) 0) =3D=3D WAIT_OBJECT_0 && total_read < inrec_size) @@ -467,7 +468,30 @@ remove_record: min (total_read - n, inrec_size1), &len); n +=3D len; } + release_attach_mutex (); + + acquire_attach_mutex (mutex_timeout); + GetNumberOfConsoleInputEvents (p->input_handle, &n); + release_attach_mutex (); + if (n + additional_space > inrec_size) + { + DWORD new_inrec_size =3D n + additional_space; + INPUT_RECORD *new_input_rec =3D (INPUT_RECORD *) + realloc (input_rec, m::bytes (new_inrec_size)); + INPUT_RECORD *new_input_tmp =3D (INPUT_RECORD *) + realloc (input_tmp, m::bytes (new_inrec_size)); + if (new_input_rec && new_input_tmp) + { + inrec_size =3D new_inrec_size; + input_rec =3D new_input_rec; + input_tmp =3D new_input_tmp; + if (!wincap.cons_need_small_input_record_buf ()) + inrec_size1 =3D inrec_size; + } + } + /* Check if writeback was successfull. */ + acquire_attach_mutex (mutex_timeout); PeekConsoleInputW (p->input_handle, input_tmp, inrec_size1, &n); release_attach_mutex (); if (n < min (total_read, inrec_size1)) @@ -488,28 +512,47 @@ remove_record: n +=3D len; } release_attach_mutex (); - for (DWORD i =3D 0, j =3D 0; j < n; j++) - if (i =3D=3D total_read - || !inrec_eq (input_rec + i, input_tmp + j, 1)) - { - if (total_read + j - i >=3D n) - { /* Something is wrong. Giving up. */ - acquire_attach_mutex (mutex_timeout); - DWORD l =3D 0; - while (l < n) - { - DWORD len; - WriteConsoleInputW (p->input_handle, input_tmp + l, - min (n - l, inrec_size1), &len); - l +=3D len; + bool fixed =3D false; + for (DWORD ofs =3D n - total_read; ofs > 0; ofs--) + { + if (inrec_eq (input_rec, input_tmp + ofs, total_read)) + { + memcpy (input_rec + total_read, input_tmp, + m::bytes (ofs)); + memcpy (input_rec + total_read + ofs, + input_tmp + total_read + ofs, + m::bytes (n - ofs - total_read)); + fixed =3D true; + break; + } + } + if (!fixed) + { + for (DWORD i =3D 0, j =3D 0; j < n; j++) + if (i =3D=3D total_read + || !inrec_eq (input_rec + i, input_tmp + j, 1)) + { + if (total_read + j - i >=3D n) + { /* Something is wrong. Giving up. */ + acquire_attach_mutex (mutex_timeout); + DWORD l =3D 0; + while (l < n) + { + DWORD len; + WriteConsoleInputW (p->input_handle, + input_tmp + l, + min (n - l, inrec_size1), + &len); + l +=3D len; + } + release_attach_mutex (); + goto skip_writeback; } - release_attach_mutex (); - goto skip_writeback; + input_rec[total_read + j - i] =3D input_tmp[j]; } - input_rec[total_read + j - i] =3D input_tmp[j]; - } - else - i++; + else + i++; + } total_read =3D n; } while (true);