Cygwin: console: Fix issue of pasting very long text input again.

- The recent commit "Cygwin: console: Allow pasting very long text
  input." did not fix the issue enough. This patch adds fixes for
  that.
This commit is contained in:
Takashi Yano 2022-07-05 11:36:33 +09:00
parent 72f855f32b
commit b3e25f0bc1
1 changed files with 65 additions and 22 deletions

View File

@ -342,9 +342,9 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp)
{ {
DWORD new_inrec_size = total_read + additional_space; DWORD new_inrec_size = total_read + additional_space;
INPUT_RECORD *new_input_rec = (INPUT_RECORD *) INPUT_RECORD *new_input_rec = (INPUT_RECORD *)
realloc (input_rec, new_inrec_size * sizeof (INPUT_RECORD)); realloc (input_rec, m::bytes (new_inrec_size));
INPUT_RECORD *new_input_tmp = (INPUT_RECORD *) INPUT_RECORD *new_input_tmp = (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) if (new_input_rec && new_input_tmp)
{ {
inrec_size = new_inrec_size; inrec_size = 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)) switch (cygwait (p->input_handle, (DWORD) 0))
{ {
case WAIT_OBJECT_0: case WAIT_OBJECT_0:
acquire_attach_mutex (mutex_timeout);
total_read = 0; total_read = 0;
while (cygwait (p->input_handle, (DWORD) 0) == WAIT_OBJECT_0 while (cygwait (p->input_handle, (DWORD) 0) == WAIT_OBJECT_0
&& total_read < inrec_size) && total_read < inrec_size)
@ -467,7 +468,30 @@ remove_record:
min (total_read - n, inrec_size1), &len); min (total_read - n, inrec_size1), &len);
n += len; n += 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 = n + additional_space;
INPUT_RECORD *new_input_rec = (INPUT_RECORD *)
realloc (input_rec, m::bytes (new_inrec_size));
INPUT_RECORD *new_input_tmp = (INPUT_RECORD *)
realloc (input_tmp, m::bytes (new_inrec_size));
if (new_input_rec && new_input_tmp)
{
inrec_size = new_inrec_size;
input_rec = new_input_rec;
input_tmp = new_input_tmp;
if (!wincap.cons_need_small_input_record_buf ())
inrec_size1 = inrec_size;
}
}
/* Check if writeback was successfull. */ /* Check if writeback was successfull. */
acquire_attach_mutex (mutex_timeout);
PeekConsoleInputW (p->input_handle, input_tmp, inrec_size1, &n); PeekConsoleInputW (p->input_handle, input_tmp, inrec_size1, &n);
release_attach_mutex (); release_attach_mutex ();
if (n < min (total_read, inrec_size1)) if (n < min (total_read, inrec_size1))
@ -488,28 +512,47 @@ remove_record:
n += len; n += len;
} }
release_attach_mutex (); release_attach_mutex ();
for (DWORD i = 0, j = 0; j < n; j++) bool fixed = false;
if (i == total_read for (DWORD ofs = n - total_read; ofs > 0; ofs--)
|| !inrec_eq (input_rec + i, input_tmp + j, 1)) {
{ if (inrec_eq (input_rec, input_tmp + ofs, total_read))
if (total_read + j - i >= n) {
{ /* Something is wrong. Giving up. */ memcpy (input_rec + total_read, input_tmp,
acquire_attach_mutex (mutex_timeout); m::bytes (ofs));
DWORD l = 0; memcpy (input_rec + total_read + ofs,
while (l < n) input_tmp + total_read + ofs,
{ m::bytes (n - ofs - total_read));
DWORD len; fixed = true;
WriteConsoleInputW (p->input_handle, input_tmp + l, break;
min (n - l, inrec_size1), &len); }
l += len; }
if (!fixed)
{
for (DWORD i = 0, j = 0; j < n; j++)
if (i == total_read
|| !inrec_eq (input_rec + i, input_tmp + j, 1))
{
if (total_read + j - i >= n)
{ /* Something is wrong. Giving up. */
acquire_attach_mutex (mutex_timeout);
DWORD l = 0;
while (l < n)
{
DWORD len;
WriteConsoleInputW (p->input_handle,
input_tmp + l,
min (n - l, inrec_size1),
&len);
l += len;
}
release_attach_mutex ();
goto skip_writeback;
} }
release_attach_mutex (); input_rec[total_read + j - i] = input_tmp[j];
goto skip_writeback;
} }
input_rec[total_read + j - i] = input_tmp[j]; else
} i++;
else }
i++;
total_read = n; total_read = n;
} }
while (true); while (true);