Skip to content

Commit 87d5722

Browse files
authored
Merge pull request #65 from dscho/backport-console-fixes
Backport console fixes
2 parents 5c6ec74 + c292b3d commit 87d5722

File tree

3 files changed

+86
-10
lines changed

3 files changed

+86
-10
lines changed

include/ansidecl.h

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,49 @@ So instead we use the macro below and test it against specific values. */
283283
# endif /* GNUC >= 4.9 */
284284
#endif /* ATTRIBUTE_NO_SANITIZE_UNDEFINED */
285285

286+
/* Attribute 'nonstring' was valid as of gcc 8. */
287+
#ifndef ATTRIBUTE_NONSTRING
288+
# if GCC_VERSION >= 8000
289+
# define ATTRIBUTE_NONSTRING __attribute__ ((__nonstring__))
290+
# else
291+
# define ATTRIBUTE_NONSTRING
292+
# endif
293+
#endif
294+
295+
/* Attribute `alloc_size' was valid as of gcc 4.3. */
296+
#ifndef ATTRIBUTE_RESULT_SIZE_1
297+
# if (GCC_VERSION >= 4003)
298+
# define ATTRIBUTE_RESULT_SIZE_1 __attribute__ ((alloc_size (1)))
299+
# else
300+
# define ATTRIBUTE_RESULT_SIZE_1
301+
#endif
302+
#endif
303+
304+
#ifndef ATTRIBUTE_RESULT_SIZE_2
305+
# if (GCC_VERSION >= 4003)
306+
# define ATTRIBUTE_RESULT_SIZE_2 __attribute__ ((alloc_size (2)))
307+
# else
308+
# define ATTRIBUTE_RESULT_SIZE_2
309+
#endif
310+
#endif
311+
312+
#ifndef ATTRIBUTE_RESULT_SIZE_1_2
313+
# if (GCC_VERSION >= 4003)
314+
# define ATTRIBUTE_RESULT_SIZE_1_2 __attribute__ ((alloc_size (1, 2)))
315+
# else
316+
# define ATTRIBUTE_RESULT_SIZE_1_2
317+
#endif
318+
#endif
319+
320+
/* Attribute `warn_unused_result' was valid as of gcc 3.3. */
321+
#ifndef ATTRIBUTE_WARN_UNUSED_RESULT
322+
# if GCC_VERSION >= 3003
323+
# define ATTRIBUTE_WARN_UNUSED_RESULT __attribute__ ((__warn_unused_result__))
324+
# else
325+
# define ATTRIBUTE_WARN_UNUSED_RESULT
326+
# endif
327+
#endif
328+
286329
/* We use __extension__ in some places to suppress -pedantic warnings
287330
about GCC extensions. This feature didn't work properly before
288331
gcc 2.8. */

winsup/cygwin/fhandler/console.cc

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,12 @@ fhandler_console::cons_master_thread (handle_set_t *p, tty *ttyp)
361361
}
362362

363363
WaitForSingleObject (p->input_mutex, mutex_timeout);
364+
/* Ensure accessing input recored is not disabled. */
365+
if (con.disable_master_thread)
366+
{
367+
ReleaseMutex (p->input_mutex);
368+
continue;
369+
}
364370
total_read = 0;
365371
switch (cygwait (p->input_handle, (DWORD) 0))
366372
{
@@ -1005,10 +1011,14 @@ fhandler_console::read (void *pv, size_t& buflen)
10051011

10061012
push_process_state process_state (PID_TTYIN);
10071013

1008-
int copied_chars = 0;
1014+
size_t copied_chars = 0;
10091015

1010-
DWORD timeout = is_nonblocking () ? 0 : INFINITE;
1016+
DWORD timeout = is_nonblocking () ? 0 :
1017+
(get_ttyp ()->ti.c_lflag & ICANON ? INFINITE :
1018+
(get_ttyp ()->ti.c_cc[VMIN] == 0 ? 0 :
1019+
(get_ttyp ()->ti.c_cc[VTIME]*100 ? : INFINITE)));
10111020

1021+
read_more:
10121022
while (!input_ready && !get_cons_readahead_valid ())
10131023
{
10141024
int bgres;
@@ -1031,6 +1041,11 @@ fhandler_console::read (void *pv, size_t& buflen)
10311041
pthread::static_cancel_self ();
10321042
/*NOTREACHED*/
10331043
case WAIT_TIMEOUT:
1044+
if (copied_chars)
1045+
{
1046+
buflen = copied_chars;
1047+
return;
1048+
}
10341049
set_sig_errno (EAGAIN);
10351050
buflen = (size_t) -1;
10361051
return;
@@ -1076,19 +1091,20 @@ fhandler_console::read (void *pv, size_t& buflen)
10761091
}
10771092

10781093
/* Check console read-ahead buffer filled from terminal requests */
1079-
while (con.cons_rapoi && *con.cons_rapoi && buflen)
1080-
{
1081-
buf[copied_chars++] = *con.cons_rapoi++;
1082-
buflen --;
1083-
}
1094+
while (con.cons_rapoi && *con.cons_rapoi && buflen > copied_chars)
1095+
buf[copied_chars++] = *con.cons_rapoi++;
10841096

10851097
copied_chars +=
1086-
get_readahead_into_buffer (buf + copied_chars, buflen);
1098+
get_readahead_into_buffer (buf + copied_chars, buflen - copied_chars);
10871099

10881100
if (!con_ra.ralen)
10891101
input_ready = false;
10901102
release_input_mutex ();
10911103

1104+
if (buflen > copied_chars && !(get_ttyp ()->ti.c_lflag & ICANON)
1105+
&& copied_chars < get_ttyp ()->ti.c_cc[VMIN])
1106+
goto read_more;
1107+
10921108
#undef buf
10931109

10941110
buflen = copied_chars;

winsup/cygwin/fhandler/pty.cc

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -940,6 +940,8 @@ fhandler_pty_slave::open (int flags, mode_t)
940940
errmsg = "can't call master, %E";
941941
goto err;
942942
}
943+
CloseHandle (repl.to_slave_nat); /* not used. */
944+
CloseHandle (repl.to_slave); /* not used. */
943945
from_master_nat_local = repl.from_master_nat;
944946
from_master_local = repl.from_master;
945947
to_master_nat_local = repl.to_master_nat;
@@ -1218,6 +1220,10 @@ fhandler_pty_slave::reset_switch_to_nat_pipe (void)
12181220
if (!CallNamedPipe (pipe, &req, sizeof req,
12191221
&repl, sizeof repl, &len, 500))
12201222
return; /* What can we do? */
1223+
CloseHandle (repl.from_master); /* not used. */
1224+
CloseHandle (repl.to_master); /* not used. */
1225+
CloseHandle (repl.to_slave_nat); /* not used. */
1226+
CloseHandle (repl.to_slave); /* not used. */
12211227
CloseHandle (get_handle_nat ());
12221228
set_handle_nat (repl.from_master_nat);
12231229
CloseHandle (get_output_handle_nat ());
@@ -3932,10 +3938,20 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
39323938
if (!CallNamedPipe (pipe, &req, sizeof req,
39333939
&repl, sizeof repl, &len, 500))
39343940
return; /* What can we do? */
3941+
CloseHandle (repl.from_master_nat); /* not used. */
3942+
CloseHandle (repl.from_master); /* not used. */
3943+
CloseHandle (repl.to_master_nat); /* not used. */
3944+
CloseHandle (repl.to_master); /* not used. */
39353945
if (dir == tty::to_nat)
3936-
to = repl.to_slave_nat;
3946+
{
3947+
CloseHandle (repl.to_slave); /* not used. */
3948+
to = repl.to_slave_nat;
3949+
}
39373950
else
3938-
to = repl.to_slave;
3951+
{
3952+
CloseHandle (repl.to_slave_nat); /* not used. */
3953+
to = repl.to_slave;
3954+
}
39393955
}
39403956

39413957
UINT cp_from = 0, cp_to = 0;
@@ -4066,6 +4082,7 @@ fhandler_pty_slave::transfer_input (tty::xfer_dir dir, HANDLE from, tty *ttyp,
40664082
transfered = true;;
40674083
}
40684084
}
4085+
CloseHandle (to);
40694086

40704087
/* Fix input_available_event which indicates availability in cyg pipe. */
40714088
if (dir == tty::to_nat) /* all data is transfered to nat pipe,

0 commit comments

Comments
 (0)