Re: TIOCPKT mode of PTY is broken if ONLCR bit is cleared.

2015-03-25 Thread Takashi Yano
Dear cygwin developers,

Regarding this (http://cygwin.com/ml/cygwin/2015-02/msg00929.html)
problem, I made a patch attached.

I have posted the patch to this ML with Corinna's directions.

On Mon, 23 Mar 2015 11:08:23 +0100
Corinna Vinschen corinna-cyg...@cygwin.com wrote:

 However, due to the changes in fhandler_tty.cc, this patch doesn't
 apply cleanly anymore.  Any chance you could resend it (ideally to the
 cygwin-patches ML) diff'ed against git HEAD?

Please find a revised patch attached to this mail.

ChangeLog is as follows:

2015-03-25  Takashi Yano takashi.y...@nifty.ne.jp

* tty.h (class tty_min): Remove variable write_error to which any
errors are not currently set at anywhere.
(class tty): Add variable column for handling ONOCR.
* tty.cc (tty::init): Add initialization code for variable column.
* fhandler.h (class fhandler_pty_master): Remove variable need_nl
which is not necessary any more. need_nl was needed by OPOST process
in fhandler_pty_master::process_slave_output().
(class fhandler_pty_common): Add function process_opost_output() for
handling post processing for OPOST in write process.
* fhandler_tty.cc (fhandler_pty_master::process_slave_output): Count
TIOCPKT control byte into length to be read in TIOCPKT mode. Move
post processing for OPOST to write process. Remove code related to
variable write_error. Return with EIO error if slave is already
closed.
(fhandler_pty_master::fhandler_pty_master): Remove initialization
code for variable need_nl.
(fhandler_pty_common::process_opost_output): Add this function for
handling of OPOST in write process. Add code to avoid blocking in
non-blocking mode when output is suspended by ^S.
(fhandler_pty_slave::write): Call fhandler_pty_common::
process_opost_output() instead of WriteFile(). Remove code related to
variable write_error. 
(fhandler_pty_master::doecho): Call fhandler_pty_common::
 process_opost_output() instead of WriteFile().
* select.cc (peek_pipe): Remove code related to variable need_nl.

Best regards,
Takashi Yano

-- 
Takashi Yano takashi.y...@nifty.ne.jp


cygwin.patch.20150325
Description: Binary data


Re: Cygwin hangs up if several keys are typed during outputting a lot of texts.

2015-04-22 Thread Takashi Yano
Hi Corinna,

On Mon, 20 Apr 2015 17:12:30 +0200
Corinna Vinschen corinna-cyg...@cygwin.com wrote:

 Ok.  Let's go with that.  Can you please rename handle2/master2 to
 handle_cyg/master_cyg and resend the patch to the cygwin-patches
 mailing list?

Please find a patch attached. ChangeLog is as follows.

2015-04-22  Takashi Yano  takashi.y...@nifty.ne.jp

* fhandler.h (class fhandler_base): Add virtual function
get_io_handle_cyg() to get handle from which OPOST-processed output is
read on PTY master.
(class fhandler_pty_slave): Add variable output_handle_cyg to store a
handle to which OPOST-processed output is written. Add two functions,
i.e., set_output_handle_cyg() and get_output_handle_cyg(), regarding
variable output_handle_cyg. Now, output_handle is used only by native
windows program. The data before OPOST-processing is written to
output_handle and OPOST-processing is applied in the master-side. For a
cygwin process, OPOST-processing is applied in the slave-side, and the
data after OPOST-processing is written to output_handle_cyg.
(class fhandler_pty_master): Add two variables, i.e., io_handle_cyg and
to_master_cyg, to store handles of a pipe through which OPOST-processed
output passes. Add pty_master_fwd_thread and function
pty_master_fwd_thread() for a thread which applies OPOST-processing
and forwards data from io_handle to to_master_cyg. Add function
get_io_handle_cyg() regarding variable io_handle_cyg. Now, the pipe
between io_handle and to_master are used only by native windows program
for applying OPOST-processing in the master-side. For a cygwin process,
the pipe between io_handle_cyg and to_master_cyg is used for passing
through the data which is applied OPOST-processing in the slave-side.
* fhandler_tty.cc (struct pipe_reply): Add member to_master_cyg.
(fhandler_pty_master::process_slave_output): Read slave output from
io_handle_cyg rather than io_handle.
(fhandler_pty_slave::fhandler_pty_salve): Initialize output_handle_cyg.
(fhandler_pty_slave::open): Set output_handle_cyg by duplicating handle
to_master_cyg on PTY master.
(fhandler_pty_slave::close): Close handle output_handle_cyg.
(fhandler_pty_slave::write): Write data to output_handle_cyg rather
than output_handle.
(fhandler_pty_slave::fch_close_handles): Close handle output_handle_cyg.
(fhandler_pty_master::fhandler_pty_master): Initialize io_handle_cyg,
to_master_cyg and master_fwd_thread.
(fhandler_pty_master::cleanup): Clean up to_master_cyg as well.
(fhandler_pty_master::close): Print to_master_cyg as well in debug
message. Terminate master forwarding thread. Close handles
to_master_cyg and io_handle_cyg.
(fhandler_pty_master::ioctl): Use io_handle_cyg rather than to_master.
(fhandler_pty_master::pty_master_thread): Add code for duplicating
handle to_master_cyg.
(fhandler_pty_master::pty_master_fwd_thread): New function for a thread
to forward OPOST-processed data from io_handle to to_master_cyg.  This
thread applies OPOST-processing to the output of native windows program.
(::pty_master_fwd_thread): Ditto.
(fhandler_pty_master::setup): Create a new pipe to pass thruegh OPOST-
processed output. Create new thread to forward data from io_handle to
to_master_cyg. Set handle to_master_cyg to tty. Print io_handle_cyg as
well in debug message. Close handles io_handle_cyg and to_master_cyg in
case of error.
(fhandler_pty_master::fixup_after_fork): Set handle to_master_cyg to
tty. Copy handle to_master_cyg from arch-to_master_cyg.
(fhandler_pty_master::fixup_after_exec): Clean up to_master_cyg.
* select.cc: Check handle returned by get_io_handle_cyg() rather than
get_handle().
* tty.h (class tty): Add variable _to_master_cyg to store a handle to
which OPOST-processed data is written. Add two functions,
to_master_cyg() and set_to_master_cyg(), regarding _to_master_cyg.


-- 
Takashi Yano takashi.y...@nifty.ne.jp


cygwin.patch.20150422-2
Description: Binary data


[PATCH cygwin] Re: Date field of ls -l command is garbled in latest cygwin1.dll snapshot.

2018-06-23 Thread Takashi Yano
On Sat, 23 Jun 2018 08:06:59 +0900
Takashi Yano wrote:
> Therefore, the script mkcategories which generates categories.t
> should be fixed.

I was thinking to wait for Thomas's action, but I tried to fix it
myself since it seemed easy.

I attach a patch to solve this problem.
I hope this would help you.

-- 
Takashi Yano 


0001-Fix-unicode-table.patch
Description: Binary data


Re: perror() changes the orientation of stderr to byte-oriented mode if stderr is not oriented yet.

2018-07-03 Thread Takashi Yano
Hi Corinna,

On Mon, 2 Jul 2018 12:28:38 +0200
Corinna Vinschen wrote:
> > By the way, I have noticed that psignal() and psiginfo() also have the
> > same problem. psignal() belongs to newlib, so the same strategy can
> > be applied. However, what can we do for psiginfo()? Only the FreeBSD
> > route may be the answer...
> 
> I guess the simplest solution is to use the FreeBSD/OpenBSD method
> all the time.

This is for fixing psiginfo().

-- 
Takashi Yano 


0001-Fix-a-bug-of-psiginfo-that-changes-the-orientation-o.patch
Description: Binary data


Re: [PATCH cygwin] Re: Problems of AF_INET domain socket regarding out-of-band data.

2018-06-21 Thread Takashi Yano
Sorry, again. Fix a typo in commit message.

-- 
Takashi Yano 


0001-Fix-the-handling-of-out-of-band-OOB-data-in-a-socket.patch
Description: Binary data


[PATCH cygwin] Re: Problems of AF_INET domain socket regarding out-of-band data.

2018-06-21 Thread Takashi Yano
Hi Corinna,

On Thu, 21 Jun 2018 09:19:54 +0200
Corinna Vinschen wrote:
> - The minor point: There's a typo "oub-of-band" in the comment
>   handling the SO_OOBINLINE ioctl.

Fixed. Thanks.

> - The major point: Now that INET and LOCAL sockets are handled
>   separately, we may want to follow Linux' lead:
> 
>   "UNIX domain sockets do not support the transmission of out-of-band data
>(the MSG_OOB flag for send(2) and recv(2))" (quote from `man 7 unix')
> 
>   If I ever get around to finish the new AF_UNIX implemantation it won't
>   support OOB anyway.  For the time being, we may want to disable OOB in
>   fhandler_socket_local already, so, rather than duplicating the changes
>   to AF_INET, we could simply disable OOB handling in
>   fhandler_socket_local entirely.  Care to do that?

I removed the OOB data handling from AF_LOCAL socket.

Please find a new patch attached.

-- 
Takashi Yano 


0001-Fix-the-handling-of-out-band-data-OOB-in-a-socket.patch
Description: Binary data


Re: [PATCH cygwin] Re: Problems of AF_INET domain socket regarding out-of-band data.

2018-06-21 Thread Takashi Yano
On Fri, 22 Jun 2018 00:05:18 +0900
Takashi Yano wrote:
> Sorry, again. Fix a typo in commit message.

Fix a typo in commit message again

-- 
Takashi Yano 


0001-Fix-the-handling-of-out-of-band-OOB-data-in-a-socket.patch
Description: Binary data


Re: Why /dev/kmsg was deleted from cygwin1.dll in git?

2018-07-05 Thread Takashi Yano
Hi Corinna,

On Wed, 4 Jul 2018 16:52:47 +0200
Corinna Vinschen wrote:
> Hang on.  /dev/kmsg was implemented using a mailslot and it was never
> accessible via the syslog(3) interface.  The code you removed has
> nothing to do with /dev/kmsg.

First of all, /dev/kmsg was not guilty. The real culprit is the code
I had removed by the previous patch.

However, the patch I posted was based on mis-understanding regarding
AF_UNIX implementation. I had checked fhandler_socket_unix.cc and
thought cygwin AF_UNIX socket is implemented not using AF_INET socket.
On the other hand, the code, I removed, checks existence of UDP socket
to determine whether syslogd is activated. So I thought this is no
longer correct and should be removed.

As a matter of fact, cygwin AF_UNIX socket usually use fhandler_socket_
local.cc, in which AF_UNIX socket is implemented using AF_INET socket.
That is, the obove understanding was incorrect.

> What the code does is to check if we have a listener on the /dev/msg UDP
> socket, otherwise log data may get lost or, IIRC, the syslog call may
> even hang.  So removing this code sounds like a bad idea.

In the case of syslogd is not activated, /dev/log does not exist.
So connect() results in an error. Therefore log data is directed to 
windows event logging mechanism even without the removed code. In
usual case, no problem happens. However if syslogd is killed by signal
9 or died accidently, /dev/log remains without listener. In this case,
the problem you mentioned may happen.

> Can you please explain *why* removing this code helps and what happens
> if syslogd is not running after removing the code?

OK. First, connect_syslogd() tries to connect to syslogd via /dev/log
which is created by syslogd. However, the code which I removed can not
perform checking existence of syslogd as expected.
Previously, get_inet_addr() is used to get name information of the socket
opened by syslogd. This was working correctly at that time. Currently,
getsockname() is used instead. This does not return name infomation of
the socket on syslogd side but returns that of client side. Since no
listener exists for this socket, it is not listed in the table returned
by GetUdpTable(). Therefore this check results in false.

As a result, current connect_syslogd() code gives up to connect to syslogd.

To fix this, I made a new patch attached. In this patch, get_inet_addr_local()
is used instead of getsockname() as in the past.

I will appreciate any comments.

-- 
Takashi Yano 


0001-Fix-a-problem-that-connection-to-syslogd-fails.patch
Description: Binary data


Re: Why /dev/kmsg was deleted from cygwin1.dll in git?

2018-07-04 Thread Takashi Yano
Hi Corinna,

On Wed, 4 Jul 2018 12:54:20 +0200
Corinna Vinschen wrote:
> On Jul  4 04:44, Takashi Yano wrote:
> > Why was /dev/kmsg deleted from cygwin1.dll in git?
> > Due to this change, syslogd in inetutils package no longer works.
> 
> /dev/kmsg doesn't really give any useful information.  It was never used
> for more than some exception information, but it required a complete
> fhandler class on its own.  I wanted to get rid of the useless code.

I looked into this problem and I realized that the real cause is not the 
absence of /dev/kmsg but old codes in the connect_syslogd() function in
syslog.cc.

I removed these codes and confirmed that syslogd works again.

I make a patch attached. Could you please have a look?

-- 
Takashi Yano 


0001-Fix-a-problem-that-connection-to-syslogd-fails.patch
Description: Binary data


Re: Why /dev/kmsg was deleted from cygwin1.dll in git?

2018-07-05 Thread Takashi Yano
Hi Brian,

On Thu, 5 Jul 2018 12:38:08 -0600
Brian Inglis wrote:
> Isn't this moot as the supported package is syslog-ng, which seems to work 
> okay?

Not only inetutils syslogd but also syslog-ng does not work in cygwin
git HEAD. I confirmed syslog-ng gets back working with the patch I posted.

Moreover, syslog-ng does not remove /dev/log even after exiting normally.
This means my first patch removing the code may cause the problem below.

On Fri, 6 Jul 2018 00:29:24 +0900
Takashi Yano wrote:
> On Wed, 4 Jul 2018 16:52:47 +0200
> Corinna Vinschen wrote:
> > What the code does is to check if we have a listener on the /dev/msg UDP
> > socket, otherwise log data may get lost or, IIRC, the syslog call may
> > even hang.  So removing this code sounds like a bad idea.
...
> usual case, no problem happens. However if syslogd is killed by signal
> 9 or died accidently, /dev/log remains without listener. In this case,
> the problem you mentioned may happen.

-- 
Takashi Yano 


[PATCH 1/1] Cygwin: console: fix key input for native console application

2019-04-03 Thread Takashi Yano
- After 24 bit color support patch, arrow keys and function keys
  do not work properly in native console applications if they
  are started in cygwin console. This patch fixes this issue.
---
 winsup/cygwin/fhandler_console.cc | 17 +
 1 file changed, 17 insertions(+)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index d2e3184a6..335467b0b 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -455,6 +455,15 @@ sig_exit:
 fhandler_console::input_states
 fhandler_console::process_input_message (void)
 {
+  if (wincap.has_con_24bit_colors ())
+{
+  DWORD dwMode;
+  /* Enable xterm compatible mode in input */
+  GetConsoleMode (get_handle (), );
+  dwMode |= ENABLE_VIRTUAL_TERMINAL_INPUT;
+  SetConsoleMode (get_handle (), dwMode);
+}
+
   char tmp[60];
 
   if (!shared_console_info)
@@ -2894,6 +2903,14 @@ fhandler_console::fixup_after_fork_exec (bool execing)
 {
   set_unit ();
   setup_io_mutex ();
+  if (wincap.has_con_24bit_colors ())
+{
+  DWORD dwMode;
+  /* Disable xterm compatible mode in input */
+  GetConsoleMode (get_handle (), );
+  dwMode &= ~ENABLE_VIRTUAL_TERMINAL_INPUT;
+  SetConsoleMode (get_handle (), dwMode);
+}
 }
 
 // #define WINSTA_ACCESS (WINSTA_READATTRIBUTES | STANDARD_RIGHTS_READ | 
STANDARD_RIGHTS_WRITE | WINSTA_CREATEDESKTOP | WINSTA_EXITWINDOWS)
-- 
2.17.0



[PATCH 0/1] Fix console again (Re: Pseudo console support in PTY)

2019-04-03 Thread Takashi Yano
Hi Corinna,

I have made a patch for the issue reported in cygwin-developers
mailing list as follows.

Could you please check?

On Wed, 3 Apr 2019 09:27:58 +0200 Corinna Vinschen wrote:
> On Apr  3 09:18, Thomas Wolff wrote:
> > > - cmd history works
> > In the cygwin console, it does not, echos the cursor-up escape sequence
> 
> Oh, right, ...
> 
> > instead. See my previous comment that I think ConPTY should only be applied
> > if running at a pty. (And for other reasons only when starting a non-cygwin
> > app.)
> 
> but you got that wrong.  The conpty stuff *is* only applied for ptys.
> 
> The above appears to be a fallout of the console changes to support
> the Windows console changes to emulate an xterm-256color,
> https://sourceware.org/git/?p=newlib-cygwin.git;a=commitdiff;h=bd627864ab41
> 
> Takashi, can you take a look?

Takashi Yano (1):
  Cygwin: console: fix key input for native console application

 winsup/cygwin/fhandler_console.cc | 17 +
 1 file changed, 17 insertions(+)

-- 
2.17.0



Re: [PATCH fifo 2/2] Cygwin: FIFO: add support for the duplex case

2019-03-30 Thread Takashi Yano
482,8 +526,8 @@ fhandler_fifo::open (int flags, mode_t)
> res = error_errno_set;
> goto out;
>   }
> -  /* Wait for the listen_client thread to create the pipe and
> -  signal read_ready.  This should be quick.  */
> +  /* Wait for the listen_client thread to signal read_ready.  This
> +  should be quick.  */
>HANDLE w[2] = { listen_client_thr, read_ready };
>switch (WaitForMultipleObjects (2, w, FALSE, INFINITE))
>   {
> @@ -703,12 +747,25 @@ fhandler_fifo::raw_read (void *in_ptr, size_t& len)
>   fifo_client_unlock ();
>   return;
> }
> - else if (nread < 0 && GetLastError () != ERROR_NO_DATA)
> -   {
> - fifo_client_unlock ();
> - goto errout;
> -   }
> - else if (nread == 0) /* Client has disconnected. */
> + /* In the duplex case with no data, we seem to get nread
> +== -1 with ERROR_PIPE_LISTENING on the first attempt to
> +read from the duplex pipe (client[0]), and nread == 0
> +on subsequent attempts. */
> + else if (nread < 0)
> +   switch (GetLastError ())
> + {
> + case ERROR_NO_DATA:
> +   break;
> + case ERROR_PIPE_LISTENING:
> +   if (_duplexer && i == 0)
> + break;
> +   /* Fall through. */
> + default:
> +   fifo_client_unlock ();
> +   goto errout;
> + }
> + else if (nread == 0 && (!_duplexer || i > 0))
> +   /* Client has disconnected. */
> {
>   client[i].state = fc_invalid;
>   nconnected--;
> -- 
> 2.17.0
> 


-- 
Takashi Yano 


[PATCH] Reworks for console code

2019-03-31 Thread Takashi Yano
Hi,

I would like to propose 3 patches attached to improve console code.

Patch 0001:
This revises console code for better color handling. This provides
24 bit color support for Windows 10 build 14931 or later. For legacy
console, fake 24 bit color support is implemented, which use the
nearest color from 16 system colors.

To check the color handling, use:
https://gist.github.com/hSATAC/1095100

Results are at:
http://tyan0.dip.jp/cgi-bin/FSWiki/wiki.cgi?page=24+bit+color+support+for+cygwin+console

Patch 0002:
Previously, select() would return by typing only one key even in
canonical mode. With this patch, it returns after one line is
completed.

To check this behaviour, use attached STC (confirm-select.c) and
type something and [Enter]. Type ^D to exit from the STC.

Expected result:
This is one line.
select() returns 1
read() returns 18
54, 68, 69, 73, 20, 69, 73, 20, 6f, 6e, 65, 20, 6c, 69, 6e, 65, 2e, 0a,
select() returns 1
read() returns 0

Cygwin current version:
select() returns 1
This is one line.
read() returns 18
54, 68, 69, 73, 20, 69, 73, 20, 6f, 6e, 65, 20, 6c, 69, 6e, 65, 2e, 0a,
select() returns 1
read() returns 0

Patch 0003:
POSIX states I/O functions shall be thread-safe, however, cygwin
console I/O functions were not. This patch makes console I/O
functions thread-safe.

To check this, use attached STC (confirm-thread-safe.c).
Expected resut is red 'A's at the first 12 lines and green 'B's
at the second 12 lines. However, the result is completely broken
in current cygwin.

-- 
Takashi Yano 


0001-Cygwin-console-support-24-bit-color.patch
Description: Binary data


0002-Cygwin-console-fix-select-behaviour.patch
Description: Binary data


0003-Cygwin-console-Make-I-O-functions-thread-safe.patch
Description: Binary data
#include 
#include 
#include 
#include 
#include 

int main()
{
	fd_set fds;
	int ret;

	while (1) {
		FD_ZERO();
		FD_SET(0, );
		errno = 0;
		ret = select(1, , NULL, NULL, NULL);
		printf("select() returns %d\n", ret);
		if (FD_ISSET(0, )) {
			char buf[1024];
			size_t n = read(0, buf, sizeof(buf));
			printf("read() returns %d\n", n);
			if (n <= 0) break;
			size_t i;
			for (i=0; i#include 
#include 
#include 
#include 

void *write_thread(void *arg)
{
	int n = (int)arg;
	const int rows = 12;
	const int cols = 79;
	char buf[64];
	for (int i=0; i<1; i++) {
		for (int r=1; r<=rows; r++) {
			for (int c=1; c<=cols; c++) {
sprintf(buf, "\033[%d;%dH\033[%dm%c", r + n*rows, c, 31+n, 'A'+n);
write(1, buf, strlen(buf));
			}
		}
	}
}

int main()
{
	pthread_t th1, th2;

	printf("\033[H\033[J");
	fflush(stdout);
	if (pthread_create(, NULL, write_thread, (void*)0)) {
		perror ("1: pthred_create");
		return -1;
	}
	if (pthread_create(, NULL, write_thread, (void*)1)) {
		perror ("2: pthred_create");
		return -1;
	}
	pthread_join(th1, NULL);
	pthread_join(th2, NULL);
	usleep(300);
	printf("\033[H\033[J\033[m");
	fflush(stdout);
	return 0;
}


[PATCH v3 2/3] Cygwin: console: fix select() behaviour

2019-03-31 Thread Takashi Yano
- Previously, select() would return when only one key is typed even
  in canonical mode. With this patch, it returns after one line is
  completed.
---
 winsup/cygwin/fhandler.h  |  12 +-
 winsup/cygwin/fhandler_console.cc | 794 --
 winsup/cygwin/select.cc   |  81 +--
 3 files changed, 462 insertions(+), 425 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 66e724bcb..e4a6de610 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1884,6 +1884,15 @@ public:
 tty_min tty_min_state;
 dev_console con;
   };
+  bool input_ready;
+  enum input_states
+  {
+input_error = -1,
+input_processing = 0,
+input_ok = 1,
+input_signalled = 2,
+input_winch = 3
+  };
 private:
   static const unsigned MAX_WRITE_CHARS;
   static console_state *shared_console_info;
@@ -1969,7 +1978,7 @@ private:
   void fixup_after_fork (HANDLE) {fixup_after_fork_exec (false);}
   void set_close_on_exec (bool val);
   void set_input_state ();
-  void send_winch_maybe ();
+  bool send_winch_maybe ();
   void setup ();
   bool set_unit ();
   static bool need_invisible ();
@@ -1992,6 +2001,7 @@ private:
 copyto (fh);
 return fh;
   }
+  input_states process_input_message ();
   friend tty_min * tty_list::get_cttyp ();
 };
 
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 6b14d4a25..160ae284a 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -53,7 +53,9 @@ details. */
 
 #define con (shared_console_info->con)
 #define srTop (con.b.srWindow.Top + con.scroll_region.Top)
-#define srBottom ((con.scroll_region.Bottom < 0) ? con.b.srWindow.Bottom : 
con.b.srWindow.Top + con.scroll_region.Bottom)
+#define srBottom ((con.scroll_region.Bottom < 0) ? \
+ con.b.srWindow.Bottom : \
+ con.b.srWindow.Top + con.scroll_region.Bottom)
 
 const unsigned fhandler_console::MAX_WRITE_CHARS = 16384;
 
@@ -149,16 +151,19 @@ fhandler_console::set_unit ()
shared_unit : FH_ERROR;
   created = false;
 }
-  else if ((!generic_console && (myself->ctty != -1 && !iscons_dev 
(myself->ctty)))
+  else if ((!generic_console &&
+   (myself->ctty != -1 && !iscons_dev (myself->ctty)))
   || !(me = GetConsoleWindow ()))
 devset = FH_ERROR;
   else
 {
   created = true;
-  shared_console_info = open_shared_console (me, cygheap->console_h, 
created);
+  shared_console_info =
+   open_shared_console (me, cygheap->console_h, created);
   ProtectHandleINH (cygheap->console_h);
   if (created)
-   shared_console_info->tty_min_state.setntty (DEV_CONS_MAJOR, 
console_unit (me));
+   shared_console_info->
+ tty_min_state.setntty (DEV_CONS_MAJOR, console_unit (me));
   devset = (fh_devices) shared_console_info->tty_min_state.getntty ();
   if (created)
con.owner = getpid ();
@@ -251,7 +256,8 @@ fhandler_console::set_raw_win32_keyboard_mode (bool 
new_mode)
 {
   bool old_mode = con.raw_win32_keyboard_mode;
   con.raw_win32_keyboard_mode = new_mode;
-  syscall_printf ("raw keyboard mode %sabled", con.raw_win32_keyboard_mode ? 
"en" : "dis");
+  syscall_printf ("raw keyboard mode %sabled",
+ con.raw_win32_keyboard_mode ? "en" : "dis");
   return old_mode;
 };
 
@@ -267,7 +273,7 @@ fhandler_console::set_cursor_maybe ()
 }
 }
 
-void
+bool
 fhandler_console::send_winch_maybe ()
 {
   SHORT y = con.dwWinSize.Y;
@@ -279,7 +285,9 @@ fhandler_console::send_winch_maybe ()
   con.scroll_region.Top = 0;
   con.scroll_region.Bottom = -1;
   get_ttyp ()->kill_pgrp (SIGWINCH);
+  return true;
 }
+  return false;
 }
 
 /* Check whether a mouse event is to be reported as an escape sequence */
@@ -299,7 +307,8 @@ fhandler_console::mouse_aware (MOUSE_EVENT_RECORD& 
mouse_event)
   con.dwMousePosition.X = mouse_event.dwMousePosition.X - now.srWindow.Left;
   con.dwMousePosition.Y = mouse_event.dwMousePosition.Y - now.srWindow.Top;
 
-  return ((mouse_event.dwEventFlags == 0 || mouse_event.dwEventFlags == 
DOUBLE_CLICK)
+  return ((mouse_event.dwEventFlags == 0
+  || mouse_event.dwEventFlags == DOUBLE_CLICK)
  && mouse_event.dwButtonState != con.dwLastButtonState)
 || mouse_event.dwEventFlags == MOUSE_WHEELED
 || (mouse_event.dwEventFlags == MOUSE_MOVED
@@ -312,36 +321,17 @@ fhandler_console::mouse_aware (MOUSE_EVENT_RECORD& 
mouse_event)
 void __reg3
 fhandler_console::read (void *pv, size_t& buflen)
 {
+  termios_printf ("read(%p,%d)", pv, buflen);
+
   push_process_state process_state (PID_TTYIN);
 
-  HANDLE h = get_handle ();
+  int copied_chars = 0;
 
-#define buf ((char *) pv)
+  DWORD timeout = is_nonblocking () ? 0 : INFINITE;
 
-  int ch;
   set_input_state ();
 
-  /* Check console read-ahead buffer filled from terminal requests */
-  if (con.cons_rapoi && *con.cons_rapoi)
-{
-   

[PATCH v3 0/3] Reworks for console code

2019-03-31 Thread Takashi Yano
Hi Corinna,

On Sun, 31 Mar 2019 16:36:51 +0200 Corinna Vinschen rote:
> This hunk is ok, but I wonder if the time hasn't come to simplify the
> original code.  The `static char NO_COPY' only makes marginal sense
> since it's strdup'ed anyway.
> 
> What if we just define two const char's like this
> 
>   const char cygterm[] = "TERM=cygwin";
>   const char xterm[] = "TERM=xterm-256color";
> 
> and then just strdup them conditionally:
> 
>   if (!sawTERM)
> envp[i++] = strdup (wincap.has_con_24bit_colors () ? xterm :
> cygterm);
> 
> What do you think?

> Sorry, didn't notice this before:  Please prepend this block with
> a comment along the lines of "/* Not yet defined in Mingw-w64 */"

Adopted.

> Doesn't this belong into the select patch?

Actually, no. This makes select() recognize Ctrl-space, but
is just tentative. Patch 0002 overwrites this fix.

This is corresponding to:
> @@ -435,7 +451,8 @@ fhandler_console::read (void *pv, size_t& buflen)
> toadd = tmp;
>   }
> /* Allow Ctrl-Space to emit ^@ */
> -   else if (input_rec.Event.KeyEvent.wVirtualKeyCode == VK_SPACE
> +   else if (input_rec.Event.KeyEvent.wVirtualKeyCode
> +== (wincap.has_con_24bit_colors () ? '2' : VK_SPACE)
>  && (ctrl_key_state & CTRL_PRESSED)
>  && !(ctrl_key_state & ALT_PRESSED))
>   toadd = "";

Takashi Yano (3):
  Cygwin: console: support 24 bit color
  Cygwin: console: fix select() behaviour
  Cygwin: console: Make I/O functions thread-safe

 winsup/cygwin/environ.cc  |7 +-
 winsup/cygwin/fhandler.h  |   34 +-
 winsup/cygwin/fhandler_console.cc | 1154 +++--
 winsup/cygwin/select.cc   |   90 +--
 winsup/cygwin/wincap.cc   |   10 +
 winsup/cygwin/wincap.h|2 +
 6 files changed, 840 insertions(+), 457 deletions(-)

-- 
2.17.0



[PATCH v3 3/3] Cygwin: console: Make I/O functions thread-safe

2019-03-31 Thread Takashi Yano
- POSIX states I/O functions shall be thread-safe, however, cygwin
  console I/O functions were not. This patch makes console I/O
  functions thread-safe.
---
 winsup/cygwin/fhandler.h  |  18 +++-
 winsup/cygwin/fhandler_console.cc | 136 +-
 winsup/cygwin/select.cc   |  23 +++--
 3 files changed, 165 insertions(+), 12 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index e4a6de610..bc66377cd 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1698,6 +1698,12 @@ class fhandler_serial: public fhandler_base
   }
 };
 
+#define acquire_input_mutex(ms) \
+  __acquire_input_mutex (__PRETTY_FUNCTION__, __LINE__, ms)
+
+#define release_input_mutex() \
+  __release_input_mutex (__PRETTY_FUNCTION__, __LINE__)
+
 #define acquire_output_mutex(ms) \
   __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms)
 
@@ -1897,6 +1903,7 @@ private:
   static const unsigned MAX_WRITE_CHARS;
   static console_state *shared_console_info;
   static bool invisible_console;
+  HANDLE input_mutex, output_mutex;
 
   /* Used when we encounter a truncated multi-byte sequence.  The
  lead bytes are stored here and revisited in the next write call. */
@@ -1966,8 +1973,11 @@ private:
   bool focus_aware () {return shared_console_info->con.use_focus;}
   bool get_cons_readahead_valid ()
   {
-return shared_console_info->con.cons_rapoi != NULL &&
+acquire_input_mutex (INFINITE);
+bool ret = shared_console_info->con.cons_rapoi != NULL &&
   *shared_console_info->con.cons_rapoi;
+release_input_mutex ();
+return ret;
   }
 
   select_record *select_read (select_stuff *);
@@ -2002,6 +2012,12 @@ private:
 return fh;
   }
   input_states process_input_message ();
+  void setup_io_mutex (void);
+  DWORD __acquire_input_mutex (const char *fn, int ln, DWORD ms);
+  void __release_input_mutex (const char *fn, int ln);
+  DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms);
+  void __release_output_mutex (const char *fn, int ln);
+
   friend tty_min * tty_list::get_cttyp ();
 };
 
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 160ae284a..6d42ed888 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -232,6 +232,45 @@ tty_list::get_cttyp ()
 return NULL;
 }
 
+void
+fhandler_console::setup_io_mutex (void)
+{
+  char buf[MAX_PATH];
+  DWORD res;
+
+  res = WAIT_FAILED;
+  if (!input_mutex || WAIT_FAILED == (res = acquire_input_mutex (0)))
+{
+  shared_name (buf, "cygcons.input.mutex", get_minor ());
+  input_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
+  if (!input_mutex)
+   input_mutex = CreateMutex (_none, FALSE, buf);
+  if (!input_mutex)
+   {
+ __seterrno ();
+ return;
+   }
+}
+  if (res == WAIT_OBJECT_0)
+release_input_mutex ();
+
+  res = WAIT_FAILED;
+  if (!output_mutex || WAIT_FAILED == (res = acquire_output_mutex (0)))
+{
+  shared_name (buf, "cygcons.output.mutex", get_minor ());
+  output_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
+  if (!output_mutex)
+   output_mutex = CreateMutex (_none, FALSE, buf);
+  if (!output_mutex)
+   {
+ __seterrno ();
+ return;
+   }
+}
+  if (res == WAIT_OBJECT_0)
+release_output_mutex ();
+}
+
 inline DWORD
 dev_console::con_to_str (char *d, int dlen, WCHAR w)
 {
@@ -362,7 +401,9 @@ fhandler_console::read (void *pv, size_t& buflen)
 #define buf ((char *) pv)
 
   int ret;
+  acquire_input_mutex (INFINITE);
   ret = process_input_message ();
+  release_input_mutex ();
   switch (ret)
{
case input_error:
@@ -382,6 +423,7 @@ fhandler_console::read (void *pv, size_t& buflen)
 }
 
   /* Check console read-ahead buffer filled from terminal requests */
+  acquire_input_mutex (INFINITE);
   while (con.cons_rapoi && *con.cons_rapoi && buflen)
 {
   buf[copied_chars++] = *con.cons_rapoi++;
@@ -393,6 +435,7 @@ fhandler_console::read (void *pv, size_t& buflen)
 
   if (!ralen)
 input_ready = false;
+  release_input_mutex ();
 
 #undef buf
 
@@ -903,6 +946,8 @@ fhandler_console::open (int flags, mode_t)
 }
   set_output_handle (h);
 
+  setup_io_mutex ();
+
   if (con.fillin (get_output_handle ()))
 {
   con.current_win32_attr = con.b.wAttributes;
@@ -953,6 +998,11 @@ fhandler_console::close ()
 {
   debug_printf ("closing: %p, %p", get_handle (), get_output_handle ());
 
+  CloseHandle (input_mutex);
+  input_mutex = NULL;
+  CloseHandle (output_mutex);
+  output_mutex = NULL;
+
   if (shared_console_info && getpid () == con.owner &&
   wincap.has_con_24bit_colors ())
 {
@@ -980,6 +1030,7 @@ fhandler_console::ioctl (unsigned int cmd, void *arg)
   int res = fhandler_termios::ioctl (cmd, arg);
   if (res <= 0)
 return res;
+  acquire_output_mutex (INFINITE);
   switch (cmd)
 {
   case TIOCGWINSZ:

[PATCH v3 1/3] Cygwin: console: support 24 bit color

2019-03-31 Thread Takashi Yano
- Add 24 bit color support using xterm compatibility mode in
  Windows 10 1703 or later.
- Add fake 24 bit color support for legacy console, which uses
  the nearest color from 16 system colors.
---
 winsup/cygwin/environ.cc  |   7 +-
 winsup/cygwin/fhandler.h  |   4 +
 winsup/cygwin/fhandler_console.cc | 230 +-
 winsup/cygwin/select.cc   |   8 ++
 winsup/cygwin/wincap.cc   |  10 ++
 winsup/cygwin/wincap.h|   2 +
 6 files changed, 227 insertions(+), 34 deletions(-)

diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index 21f13734c..a47ed72e7 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -869,7 +869,8 @@ win32env_to_cygenv (PWCHAR rawenv, bool posify)
   char *newp;
   int i;
   int sawTERM = 0;
-  static char NO_COPY cygterm[] = "TERM=cygwin";
+  const char cygterm[] = "TERM=cygwin";
+  const char xterm[] = "TERM=xterm-256color";
   char *tmpbuf = tp.t_get ();
   PWCHAR w;
 
@@ -899,8 +900,10 @@ win32env_to_cygenv (PWCHAR rawenv, bool posify)
   debug_printf ("%p: %s", envp[i], envp[i]);
 }
 
+  /* If console has 24 bit color capability, TERM=xterm-256color,
+ otherwise, TERM=cygwin */
   if (!sawTERM)
-envp[i++] = strdup (cygterm);
+envp[i++] = strdup (wincap.has_con_24bit_colors () ? xterm : cygterm);
 
   envp[i] = NULL;
   return envp;
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index b336eb63a..66e724bcb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1778,6 +1778,8 @@ enum ansi_intensity
 #define eattitle 7
 #define gotparen 8
 #define gotrparen 9
+#define eatpalette 10
+#define endpalette 11
 #define MAXARGS 10
 
 enum cltype
@@ -1791,6 +1793,8 @@ enum cltype
 
 class dev_console
 {
+  pid_t owner;
+
   WORD default_color, underline_color, dim_color;
 
   /* Used to determine if an input keystroke should be modified with META. */
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 281c2005c..6b14d4a25 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -15,6 +15,7 @@ details. */
 #include 
 #include 
 #include 
+#include 
 #include "cygerrno.h"
 #include "security.h"
 #include "path.h"
@@ -32,6 +33,17 @@ details. */
 #include "child_info.h"
 #include "cygwait.h"
 
+/* Not yet defined in Mingw-w64 */
+#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
+#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
+#endif /* ENABLE_VIRTUAL_TERMINAL_PROCESSING */
+#ifndef DISABLE_NEWLINE_AUTO_RETURN
+#define DISABLE_NEWLINE_AUTO_RETURN 0x0008
+#endif /* DISABLE_NEWLINE_AUTO_RETURN */
+#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
+#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
+#endif /* ENABLE_VIRTUAL_TERMINAL_INPUT */
+
 /* Don't make this bigger than NT_MAX_PATH as long as the temporary buffer
is allocated using tmp_pathbuf!!! */
 #define CONVERT_LIMIT NT_MAX_PATH
@@ -148,7 +160,11 @@ fhandler_console::set_unit ()
   if (created)
shared_console_info->tty_min_state.setntty (DEV_CONS_MAJOR, 
console_unit (me));
   devset = (fh_devices) shared_console_info->tty_min_state.getntty ();
+  if (created)
+   con.owner = getpid ();
 }
+  if (!created && shared_console_info && kill (con.owner, 0) == -1)
+con.owner = getpid ();
 
   dev ().parse (devset);
   if (devset != FH_ERROR)
@@ -167,33 +183,33 @@ void
 fhandler_console::setup ()
 {
   if (set_unit ())
-  {
-   con.scroll_region.Bottom = -1;
-   con.dwLastCursorPosition.X = -1;
-   con.dwLastCursorPosition.Y = -1;
-   con.dwLastMousePosition.X = -1;
-   con.dwLastMousePosition.Y = -1;
-   con.dwLastButtonState = 0;  /* none pressed */
-   con.last_button_code = 3;   /* released */
-   con.underline_color = FOREGROUND_GREEN | FOREGROUND_BLUE;
-   con.dim_color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
-   con.meta_mask = LEFT_ALT_PRESSED;
-   /* Set the mask that determines if an input keystroke is modified by
-  META.  We set this based on the keyboard layout language loaded
-  for the current thread.  The left  key always generates
-  META, but the right  key only generates META if we are using
-  an English keyboard because many "international" keyboards
-  replace common shell symbols ('[', '{', etc.) with accented
-  language-specific characters (umlaut, accent grave, etc.).  On
-  these keyboards right  (called AltGr) is used to produce the
-  shell symbols and should not be interpreted as META. */
-   if (PRIMARYLANGID (LOWORD (GetKeyboardLayout (0))) == LANG_ENGLISH)
- con.meta_mask |= RIGHT_ALT_PRESSED;
-   con.set_default_attr ();
-   con.backspace_keycode = CERASE;
-   con.cons_rapoi = NULL;
-   shared_console_info->tty_min_state.is_console = true;
-  }
+{
+  con.scroll_region.Bottom = -1;
+  

[PATCH v2 3/3] Cygwin: console: Make I/O functions thread-safe

2019-03-31 Thread Takashi Yano
- POSIX states I/O functions shall be thread-safe, however, cygwin
  console I/O functions were not. This patch makes console I/O
  functions thread-safe.
---
 winsup/cygwin/fhandler.h  |  18 +++-
 winsup/cygwin/fhandler_console.cc | 136 +-
 winsup/cygwin/select.cc   |  23 +++--
 3 files changed, 165 insertions(+), 12 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index e4a6de610..bc66377cd 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1698,6 +1698,12 @@ class fhandler_serial: public fhandler_base
   }
 };
 
+#define acquire_input_mutex(ms) \
+  __acquire_input_mutex (__PRETTY_FUNCTION__, __LINE__, ms)
+
+#define release_input_mutex() \
+  __release_input_mutex (__PRETTY_FUNCTION__, __LINE__)
+
 #define acquire_output_mutex(ms) \
   __acquire_output_mutex (__PRETTY_FUNCTION__, __LINE__, ms)
 
@@ -1897,6 +1903,7 @@ private:
   static const unsigned MAX_WRITE_CHARS;
   static console_state *shared_console_info;
   static bool invisible_console;
+  HANDLE input_mutex, output_mutex;
 
   /* Used when we encounter a truncated multi-byte sequence.  The
  lead bytes are stored here and revisited in the next write call. */
@@ -1966,8 +1973,11 @@ private:
   bool focus_aware () {return shared_console_info->con.use_focus;}
   bool get_cons_readahead_valid ()
   {
-return shared_console_info->con.cons_rapoi != NULL &&
+acquire_input_mutex (INFINITE);
+bool ret = shared_console_info->con.cons_rapoi != NULL &&
   *shared_console_info->con.cons_rapoi;
+release_input_mutex ();
+return ret;
   }
 
   select_record *select_read (select_stuff *);
@@ -2002,6 +2012,12 @@ private:
 return fh;
   }
   input_states process_input_message ();
+  void setup_io_mutex (void);
+  DWORD __acquire_input_mutex (const char *fn, int ln, DWORD ms);
+  void __release_input_mutex (const char *fn, int ln);
+  DWORD __acquire_output_mutex (const char *fn, int ln, DWORD ms);
+  void __release_output_mutex (const char *fn, int ln);
+
   friend tty_min * tty_list::get_cttyp ();
 };
 
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 788bc5cf4..04e59a474 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -231,6 +231,45 @@ tty_list::get_cttyp ()
 return NULL;
 }
 
+void
+fhandler_console::setup_io_mutex (void)
+{
+  char buf[MAX_PATH];
+  DWORD res;
+
+  res = WAIT_FAILED;
+  if (!input_mutex || WAIT_FAILED == (res = acquire_input_mutex (0)))
+{
+  shared_name (buf, "cygcons.input.mutex", get_minor ());
+  input_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
+  if (!input_mutex)
+   input_mutex = CreateMutex (_none, FALSE, buf);
+  if (!input_mutex)
+   {
+ __seterrno ();
+ return;
+   }
+}
+  if (res == WAIT_OBJECT_0)
+release_input_mutex ();
+
+  res = WAIT_FAILED;
+  if (!output_mutex || WAIT_FAILED == (res = acquire_output_mutex (0)))
+{
+  shared_name (buf, "cygcons.output.mutex", get_minor ());
+  output_mutex = OpenMutex (MAXIMUM_ALLOWED, TRUE, buf);
+  if (!output_mutex)
+   output_mutex = CreateMutex (_none, FALSE, buf);
+  if (!output_mutex)
+   {
+ __seterrno ();
+ return;
+   }
+}
+  if (res == WAIT_OBJECT_0)
+release_output_mutex ();
+}
+
 inline DWORD
 dev_console::con_to_str (char *d, int dlen, WCHAR w)
 {
@@ -361,7 +400,9 @@ fhandler_console::read (void *pv, size_t& buflen)
 #define buf ((char *) pv)
 
   int ret;
+  acquire_input_mutex (INFINITE);
   ret = process_input_message ();
+  release_input_mutex ();
   switch (ret)
{
case input_error:
@@ -381,6 +422,7 @@ fhandler_console::read (void *pv, size_t& buflen)
 }
 
   /* Check console read-ahead buffer filled from terminal requests */
+  acquire_input_mutex (INFINITE);
   while (con.cons_rapoi && *con.cons_rapoi && buflen)
 {
   buf[copied_chars++] = *con.cons_rapoi++;
@@ -392,6 +434,7 @@ fhandler_console::read (void *pv, size_t& buflen)
 
   if (!ralen)
 input_ready = false;
+  release_input_mutex ();
 
 #undef buf
 
@@ -902,6 +945,8 @@ fhandler_console::open (int flags, mode_t)
 }
   set_output_handle (h);
 
+  setup_io_mutex ();
+
   if (con.fillin (get_output_handle ()))
 {
   con.current_win32_attr = con.b.wAttributes;
@@ -952,6 +997,11 @@ fhandler_console::close ()
 {
   debug_printf ("closing: %p, %p", get_handle (), get_output_handle ());
 
+  CloseHandle (input_mutex);
+  input_mutex = NULL;
+  CloseHandle (output_mutex);
+  output_mutex = NULL;
+
   if (shared_console_info && getpid () == con.owner &&
   wincap.has_con_24bit_colors ())
 {
@@ -979,6 +1029,7 @@ fhandler_console::ioctl (unsigned int cmd, void *arg)
   int res = fhandler_termios::ioctl (cmd, arg);
   if (res <= 0)
 return res;
+  acquire_output_mutex (INFINITE);
   switch (cmd)
 {
   case TIOCGWINSZ:

[PATCH v2 0/3] Reworks for console code

2019-03-31 Thread Takashi Yano
Hi Corinna,

I have revised the patches according to your advice.
Could you please have a look?

Takashi Yano (3):
  Cygwin: console: support 24 bit color
  Cygwin: console: fix select() behaviour
  Cygwin: console: Make I/O functions thread-safe

 winsup/cygwin/environ.cc  |7 +-
 winsup/cygwin/fhandler.h  |   34 +-
 winsup/cygwin/fhandler_console.cc | 1153 +++--
 winsup/cygwin/select.cc   |   90 +--
 winsup/cygwin/wincap.cc   |   10 +
 winsup/cygwin/wincap.h|2 +
 6 files changed, 840 insertions(+), 456 deletions(-)

-- 
2.17.0



[PATCH v2 2/3] Cygwin: console: fix select() behaviour

2019-03-31 Thread Takashi Yano
- Previously, select() would return when only one key is typed even
  in canonical mode. With this patch, it returns after one line is
  completed.
---
 winsup/cygwin/fhandler.h  |  12 +-
 winsup/cygwin/fhandler_console.cc | 794 --
 winsup/cygwin/select.cc   |  81 +--
 3 files changed, 462 insertions(+), 425 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 66e724bcb..e4a6de610 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1884,6 +1884,15 @@ public:
 tty_min tty_min_state;
 dev_console con;
   };
+  bool input_ready;
+  enum input_states
+  {
+input_error = -1,
+input_processing = 0,
+input_ok = 1,
+input_signalled = 2,
+input_winch = 3
+  };
 private:
   static const unsigned MAX_WRITE_CHARS;
   static console_state *shared_console_info;
@@ -1969,7 +1978,7 @@ private:
   void fixup_after_fork (HANDLE) {fixup_after_fork_exec (false);}
   void set_close_on_exec (bool val);
   void set_input_state ();
-  void send_winch_maybe ();
+  bool send_winch_maybe ();
   void setup ();
   bool set_unit ();
   static bool need_invisible ();
@@ -1992,6 +2001,7 @@ private:
 copyto (fh);
 return fh;
   }
+  input_states process_input_message ();
   friend tty_min * tty_list::get_cttyp ();
 };
 
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 7fed912c7..788bc5cf4 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -52,7 +52,9 @@ details. */
 
 #define con (shared_console_info->con)
 #define srTop (con.b.srWindow.Top + con.scroll_region.Top)
-#define srBottom ((con.scroll_region.Bottom < 0) ? con.b.srWindow.Bottom : 
con.b.srWindow.Top + con.scroll_region.Bottom)
+#define srBottom ((con.scroll_region.Bottom < 0) ? \
+ con.b.srWindow.Bottom : \
+ con.b.srWindow.Top + con.scroll_region.Bottom)
 
 const unsigned fhandler_console::MAX_WRITE_CHARS = 16384;
 
@@ -148,16 +150,19 @@ fhandler_console::set_unit ()
shared_unit : FH_ERROR;
   created = false;
 }
-  else if ((!generic_console && (myself->ctty != -1 && !iscons_dev 
(myself->ctty)))
+  else if ((!generic_console &&
+   (myself->ctty != -1 && !iscons_dev (myself->ctty)))
   || !(me = GetConsoleWindow ()))
 devset = FH_ERROR;
   else
 {
   created = true;
-  shared_console_info = open_shared_console (me, cygheap->console_h, 
created);
+  shared_console_info =
+   open_shared_console (me, cygheap->console_h, created);
   ProtectHandleINH (cygheap->console_h);
   if (created)
-   shared_console_info->tty_min_state.setntty (DEV_CONS_MAJOR, 
console_unit (me));
+   shared_console_info->
+ tty_min_state.setntty (DEV_CONS_MAJOR, console_unit (me));
   devset = (fh_devices) shared_console_info->tty_min_state.getntty ();
   if (created)
con.owner = getpid ();
@@ -250,7 +255,8 @@ fhandler_console::set_raw_win32_keyboard_mode (bool 
new_mode)
 {
   bool old_mode = con.raw_win32_keyboard_mode;
   con.raw_win32_keyboard_mode = new_mode;
-  syscall_printf ("raw keyboard mode %sabled", con.raw_win32_keyboard_mode ? 
"en" : "dis");
+  syscall_printf ("raw keyboard mode %sabled",
+ con.raw_win32_keyboard_mode ? "en" : "dis");
   return old_mode;
 };
 
@@ -266,7 +272,7 @@ fhandler_console::set_cursor_maybe ()
 }
 }
 
-void
+bool
 fhandler_console::send_winch_maybe ()
 {
   SHORT y = con.dwWinSize.Y;
@@ -278,7 +284,9 @@ fhandler_console::send_winch_maybe ()
   con.scroll_region.Top = 0;
   con.scroll_region.Bottom = -1;
   get_ttyp ()->kill_pgrp (SIGWINCH);
+  return true;
 }
+  return false;
 }
 
 /* Check whether a mouse event is to be reported as an escape sequence */
@@ -298,7 +306,8 @@ fhandler_console::mouse_aware (MOUSE_EVENT_RECORD& 
mouse_event)
   con.dwMousePosition.X = mouse_event.dwMousePosition.X - now.srWindow.Left;
   con.dwMousePosition.Y = mouse_event.dwMousePosition.Y - now.srWindow.Top;
 
-  return ((mouse_event.dwEventFlags == 0 || mouse_event.dwEventFlags == 
DOUBLE_CLICK)
+  return ((mouse_event.dwEventFlags == 0
+  || mouse_event.dwEventFlags == DOUBLE_CLICK)
  && mouse_event.dwButtonState != con.dwLastButtonState)
 || mouse_event.dwEventFlags == MOUSE_WHEELED
 || (mouse_event.dwEventFlags == MOUSE_MOVED
@@ -311,36 +320,17 @@ fhandler_console::mouse_aware (MOUSE_EVENT_RECORD& 
mouse_event)
 void __reg3
 fhandler_console::read (void *pv, size_t& buflen)
 {
+  termios_printf ("read(%p,%d)", pv, buflen);
+
   push_process_state process_state (PID_TTYIN);
 
-  HANDLE h = get_handle ();
+  int copied_chars = 0;
 
-#define buf ((char *) pv)
+  DWORD timeout = is_nonblocking () ? 0 : INFINITE;
 
-  int ch;
   set_input_state ();
 
-  /* Check console read-ahead buffer filled from terminal requests */
-  if (con.cons_rapoi && *con.cons_rapoi)
-{
-   

[PATCH v2 1/3] Cygwin: console: support 24 bit color

2019-03-31 Thread Takashi Yano
- Add 24 bit color support using xterm compatibility mode in
  Windows 10 1703 or later.
- Add fake 24 bit color support for legacy console, which uses
  the nearest color from 16 system colors.
---
 winsup/cygwin/environ.cc  |   7 +-
 winsup/cygwin/fhandler.h  |   4 +
 winsup/cygwin/fhandler_console.cc | 229 +-
 winsup/cygwin/select.cc   |   8 ++
 winsup/cygwin/wincap.cc   |  10 ++
 winsup/cygwin/wincap.h|   2 +
 6 files changed, 227 insertions(+), 33 deletions(-)

diff --git a/winsup/cygwin/environ.cc b/winsup/cygwin/environ.cc
index 21f13734c..c27d10fe1 100644
--- a/winsup/cygwin/environ.cc
+++ b/winsup/cygwin/environ.cc
@@ -869,10 +869,15 @@ win32env_to_cygenv (PWCHAR rawenv, bool posify)
   char *newp;
   int i;
   int sawTERM = 0;
-  static char NO_COPY cygterm[] = "TERM=cygwin";
+  static char NO_COPY cygterm[] = "TERM=xterm-256color";
   char *tmpbuf = tp.t_get ();
   PWCHAR w;
 
+  /* If console has 24 bit color capability, TERM=xterm-256color,
+ otherwise, TERM=cygwin */
+  if (!wincap.has_con_24bit_colors ())
+strncpy (cygterm, "TERM=cygwin", sizeof (cygterm));
+
   /* Allocate space for environment + trailing NULL + CYGWIN env. */
   envp = (char **) malloc ((4 + (envc = 100)) * sizeof (char *));
 
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index b336eb63a..66e724bcb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -1778,6 +1778,8 @@ enum ansi_intensity
 #define eattitle 7
 #define gotparen 8
 #define gotrparen 9
+#define eatpalette 10
+#define endpalette 11
 #define MAXARGS 10
 
 enum cltype
@@ -1791,6 +1793,8 @@ enum cltype
 
 class dev_console
 {
+  pid_t owner;
+
   WORD default_color, underline_color, dim_color;
 
   /* Used to determine if an input keystroke should be modified with META. */
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 281c2005c..7fed912c7 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -15,6 +15,7 @@ details. */
 #include 
 #include 
 #include 
+#include 
 #include "cygerrno.h"
 #include "security.h"
 #include "path.h"
@@ -32,6 +33,16 @@ details. */
 #include "child_info.h"
 #include "cygwait.h"
 
+#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
+#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
+#endif /* ENABLE_VIRTUAL_TERMINAL_PROCESSING */
+#ifndef DISABLE_NEWLINE_AUTO_RETURN
+#define DISABLE_NEWLINE_AUTO_RETURN 0x0008
+#endif /* DISABLE_NEWLINE_AUTO_RETURN */
+#ifndef ENABLE_VIRTUAL_TERMINAL_INPUT
+#define ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
+#endif /* ENABLE_VIRTUAL_TERMINAL_INPUT */
+
 /* Don't make this bigger than NT_MAX_PATH as long as the temporary buffer
is allocated using tmp_pathbuf!!! */
 #define CONVERT_LIMIT NT_MAX_PATH
@@ -148,7 +159,11 @@ fhandler_console::set_unit ()
   if (created)
shared_console_info->tty_min_state.setntty (DEV_CONS_MAJOR, 
console_unit (me));
   devset = (fh_devices) shared_console_info->tty_min_state.getntty ();
+  if (created)
+   con.owner = getpid ();
 }
+  if (!created && shared_console_info && kill (con.owner, 0) == -1)
+con.owner = getpid ();
 
   dev ().parse (devset);
   if (devset != FH_ERROR)
@@ -167,33 +182,33 @@ void
 fhandler_console::setup ()
 {
   if (set_unit ())
-  {
-   con.scroll_region.Bottom = -1;
-   con.dwLastCursorPosition.X = -1;
-   con.dwLastCursorPosition.Y = -1;
-   con.dwLastMousePosition.X = -1;
-   con.dwLastMousePosition.Y = -1;
-   con.dwLastButtonState = 0;  /* none pressed */
-   con.last_button_code = 3;   /* released */
-   con.underline_color = FOREGROUND_GREEN | FOREGROUND_BLUE;
-   con.dim_color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
-   con.meta_mask = LEFT_ALT_PRESSED;
-   /* Set the mask that determines if an input keystroke is modified by
-  META.  We set this based on the keyboard layout language loaded
-  for the current thread.  The left  key always generates
-  META, but the right  key only generates META if we are using
-  an English keyboard because many "international" keyboards
-  replace common shell symbols ('[', '{', etc.) with accented
-  language-specific characters (umlaut, accent grave, etc.).  On
-  these keyboards right  (called AltGr) is used to produce the
-  shell symbols and should not be interpreted as META. */
-   if (PRIMARYLANGID (LOWORD (GetKeyboardLayout (0))) == LANG_ENGLISH)
- con.meta_mask |= RIGHT_ALT_PRESSED;
-   con.set_default_attr ();
-   con.backspace_keycode = CERASE;
-   con.cons_rapoi = NULL;
-   shared_console_info->tty_min_state.is_console = true;
-  }
+{
+  con.scroll_region.Bottom = -1;
+  con.dwLastCursorPosition.X = -1;
+  con.dwLastCursorPosition.Y = -1;
+  con.dwLastMousePosition.X = -1;
+  

[PATCH 1/1] Cygwin: console: Fix cursor position restore after screen alternation.

2019-08-12 Thread Takashi Yano
- If screen is alternated on console, cursor position is not restored
  correctly in the case of xterm compatible mode is enabled. For example,
  the shell prompt is shown at incorrect position after using vim.
  This patch fixes this problem.
---
 winsup/cygwin/fhandler_console.cc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index e3656a33a..075593523 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -304,6 +304,9 @@ void
 fhandler_console::set_cursor_maybe ()
 {
   con.fillin (get_output_handle ());
+  /* Nothing to do for xterm compatible mode. */
+  if (wincap.has_con_24bit_colors ())
+return;
   if (con.dwLastCursorPosition.X != con.b.dwCursorPosition.X ||
   con.dwLastCursorPosition.Y != con.b.dwCursorPosition.Y)
 {
-- 
2.21.0



[PATCH 0/1] Fix cursor position restoration on console

2019-08-12 Thread Takashi Yano
In cygwin test release 3.1.0-0.1, the cursor position is not restored
correctly after screen alternation in the case of xterm compatible mode
is enabled. For example, the shell prompt is shown at incorrect position
after using vim. This patch fixes this problem.

Takashi Yano (1):
  Cygwin: console: Fix cursor position restore after screen alternation.

 winsup/cygwin/fhandler_console.cc | 3 +++
 1 file changed, 3 insertions(+)

-- 
2.21.0



[PATCH 0/1] Workaround for bug of hrizontal tab on console.

2019-08-12 Thread Takashi Yano
In cygwin test release 3.1.0-0.1, the horizontal tab setting is broken
after resizing console window. This seems to be a bug of xterm
compatible mode of windows console. This patch fixes this problem.

Takashi Yano (1):
  Cygwin: console: Add workaround for windows xterm compatible mode bug.

 winsup/cygwin/fhandler_console.cc | 19 +++
 1 file changed, 19 insertions(+)

-- 
2.21.0



[PATCH 1/1] Cygwin: console: Add workaround for windows xterm compatible mode bug.

2019-08-12 Thread Takashi Yano
- The horizontal tab positions are broken after resizing console window.
  This seems to be a bug of xterm compatible mode of windows console.
  This workaround fixes this problem.
---
 winsup/cygwin/fhandler_console.cc | 19 +++
 1 file changed, 19 insertions(+)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 075593523..3d26a0b90 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -326,6 +326,25 @@ fhandler_console::send_winch_maybe ()
 {
   con.scroll_region.Top = 0;
   con.scroll_region.Bottom = -1;
+  if (wincap.has_con_24bit_colors ())
+   {
+ /* Workaround for a bug of windows xterm compatible mode. */
+ /* The horizontal tab positions are broken after resize. */
+ DWORD dwLen;
+ CONSOLE_SCREEN_BUFFER_INFO sbi;
+ GetConsoleScreenBufferInfo (get_output_handle (), );
+ /* Clear all horizontal tabs */
+ WriteConsole (get_output_handle (), "\033[3g", 4, , 0);
+ /* Set horizontal tabs */
+ for (int col=8; colkill_pgrp (SIGWINCH);
   return true;
 }
-- 
2.21.0



[PATCH 0/1] Fix deadlock at calling fork() in console

2019-08-12 Thread Takashi Yano
In cygwin test release 3.1.0-0.1, calling fork on console ocasionally
falls into deadlock. The reason is not clear, however, this patch fixes
this problem anyway.

Takashi Yano (1):
  Cygwin: console: Fix deadlock at calling fork().

 winsup/cygwin/fhandler_console.cc | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

-- 
2.21.0



[PATCH 1/1] Cygwin: console: Fix deadlock at calling fork().

2019-08-12 Thread Takashi Yano
- Calling fork() on console occasionally falls into deadlock. The reason
  is not clear, however, this patch fixes this problem anyway.
---
 winsup/cygwin/fhandler_console.cc | 8 ++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 3d26a0b90..4afb7efb7 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -168,8 +168,12 @@ fhandler_console::set_unit ()
   if (created)
con.owner = getpid ();
 }
-  if (!created && shared_console_info && kill (con.owner, 0) == -1)
-con.owner = getpid ();
+  if (!created && shared_console_info)
+{
+  pinfo p (con.owner);
+  if (!p)
+   con.owner = getpid ();
+}
 
   dev ().parse (devset);
   if (devset != FH_ERROR)
-- 
2.21.0



[PATCH 0/1] Fix select() regarding SIGWINCH in console

2019-08-14 Thread Takashi Yano
In cygwin test release 3.1.0-0.1, script command (from util-linux)
is terminated abnormaly if the window size is changed in the case
that cygwin is running in command prompt. This patch fixes this
issue.  This problem occurs when select() or poll() returns with
EINTR by SIGWINCH even though SIGWINCH is ignored. This patch adds
code so that select() is not interrupted by SIGWINCH when it is
ignored (SIG_IGN or SIG_DFL).

Takashi Yano (1):
  Cygwin: console: Fix the condition to interrupt select() by SIGWINCH

 winsup/cygwin/select.cc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

-- 
2.21.0



[PATCH 1/1] Cygwin: console: Fix workaround for horizontal tab position

2019-08-14 Thread Takashi Yano
- The workaround commit 33a21904a702191cebf0e81b4deba2dfa10a406c
  does not work as expected if window size is changed while screen
  is alternated. Fixed.
---
 winsup/cygwin/fhandler_console.cc | 47 +++
 1 file changed, 29 insertions(+), 18 deletions(-)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 4afb7efb7..67638055e 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -319,6 +319,25 @@ fhandler_console::set_cursor_maybe ()
 }
 }
 
+/* Workaround for a bug of windows xterm compatible mode. */
+/* The horizontal tab positions are broken after resize. */
+static void
+fix_tab_position (HANDLE h, SHORT width)
+{
+  char buf[2048] = {0,};
+  /* Save cursor position */
+  __small_sprintf (buf+strlen (buf), "\0337");
+  /* Clear all horizontal tabs */
+  __small_sprintf (buf+strlen (buf), "\033[3g");
+  /* Set horizontal tabs */
+  for (int col=8; colkill_pgrp (SIGWINCH);
   return true;
 }
@@ -1615,6 +1617,12 @@ static const wchar_t __vt100_conv[31] = {
 inline
 bool fhandler_console::write_console (PWCHAR buf, DWORD len, DWORD& done)
 {
+  bool need_fix_tab_position = false;
+  /* Check if screen will be alternated. */
+  if (wincap.has_con_24bit_colors ()
+  && memmem (buf, len*sizeof (WCHAR), L"\033[?1049", 7*sizeof (WCHAR)))
+need_fix_tab_position = true;
+
   if (con.iso_2022_G1
? con.vt100_graphics_mode_G1
: con.vt100_graphics_mode_G0)
@@ -1633,6 +1641,9 @@ bool fhandler_console::write_console (PWCHAR buf, DWORD 
len, DWORD& done)
   len -= done;
   buf += done;
 }
+  /* Call fix_tab_position() if screen has been alternated. */
+  if (need_fix_tab_position)
+fix_tab_position (get_output_handle (), con.dwWinSize.X);
   return true;
 }
 
-- 
2.21.0



[PATCH 1/1] Cygwin: console: Fix the condition to interrupt select() by SIGWINCH

2019-08-14 Thread Takashi Yano
- Add code so that select() is not interrupted by SIGWINCH if it is
  ignored (SIG_IGN or SIG_DFL).
---
 winsup/cygwin/select.cc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 9cf892801..4e9256b9f 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1045,7 +1045,9 @@ peek_console (select_record *me, bool)
   else if (!PeekConsoleInputW (h, , 1, _read) || !events_read)
break;
   fh->acquire_input_mutex (INFINITE);
-  if (fhandler_console::input_winch == fh->process_input_message ())
+  if (fhandler_console::input_winch == fh->process_input_message ()
+ && global_sigs[SIGWINCH].sa_handler != SIG_IGN
+ && global_sigs[SIGWINCH].sa_handler != SIG_DFL)
{
  set_sig_errno (EINTR);
  fh->release_input_mutex ();
-- 
2.21.0



[PATCH 0/1] Workaround for horizontal tab on console (again)

2019-08-14 Thread Takashi Yano
The workaround commit 33a21904a702191cebf0e81b4deba2dfa10a406c does not
work as expected if the window size is changed while screen is alternated.
This patch fixes the issue.

Takashi Yano (1):
  Cygwin: console: Fix workaround for horizontal tab position

 winsup/cygwin/fhandler_console.cc | 47 +++
 1 file changed, 29 insertions(+), 18 deletions(-)

-- 
2.21.0



[PATCH 0/1] Fix PTY state management in pseudo console support.

2019-08-31 Thread Takashi Yano
Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
has some bugs which cause mismatch between state variables and
real pseudo console state regarding console attaching and r/w
pipe switching. This patch fixes this issue by redesigning the
state management.

Takashi Yano (1):
  Cygwin: pty: Fix state management for pseudo console support.

 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 401 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 ++---
 6 files changed, 280 insertions(+), 237 deletions(-)

-- 
2.21.0



[PATCH 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-08-31 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.
---
 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 401 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 ++---
 6 files changed, 280 insertions(+), 237 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index ba5d16206..6266f1bc2 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -150,8 +150,11 @@ dtable::stdio_init ()
   bool need_fixup_handle = false;
   fhandler_pty_slave *ptys = NULL;
   bool is_pty[3] = {false, false, false};
-  for (int fd = 0; fd < 3; fd ++)
+  bool already_attached = false;
+  int chk_order[] = {1, 0, 2};
+  for (int i = 0; i < 3; i ++)
 {
+  int fd = chk_order[i];
   fhandler_base *fh = cygheap->fdtab[fd];
   if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
@@ -161,14 +164,18 @@ dtable::stdio_init ()
  is_pty[fd] = true;
  bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
- if (!attached)
+ already_attached = already_attached || attached;
+ if (!already_attached)
{
  /* Not attached to pseudo console in fork() or spawn()
 by some reason. This happens if the executable is
 a windows GUI binary, such as mintty. */
  FreeConsole ();
- AttachConsole (ptys->getHelperProcessId ());
- need_fixup_handle = true;
+ if (AttachConsole (ptys->getHelperProcessId ()))
+   {
+ need_fixup_handle = true;
+ already_attached = true;
+   }
}
  ptys->reset_switch_to_pcon ();
}
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c75e40c0a..af2b948cb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2106,19 +2106,22 @@ class fhandler_pty_common: public fhandler_termios
  protected:
   BOOL process_opost_output (HANDLE h,
 const void *ptr, ssize_t& len, bool is_echo);
-  bool check_switch_to_pcon (void);
 };
 
 class fhandler_pty_slave: public fhandler_pty_common
 {
   HANDLE inuse;// used to indicate that a tty is in use
   HANDLE output_handle_cyg, io_handle_cyg;
+  DWORD pidRestore;
 
   /* Helper functions for fchmod and fchown. */
   bool fch_open_handles (bool chown);
   int fch_set_sd (security_descriptor , bool chown);
   void fch_close_handles ();
 
+  bool try_reattach_pcon ();
+  void restore_reattach_pcon ();
+
  public:
   /* Constructor */
   fhandler_pty_slave (int);
@@ -2172,7 +2175,6 @@ class fhandler_pty_slave: public fhandler_pty_common
   void set_switch_to_pcon (void);
   void reset_switch_to_pcon (void);
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
-  bool has_master_opened (void);
   void mask_switch_to_pcon (bool mask)
   {
 get_ttyp ()->mask_switch_to_pcon = mask;
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 997c50d23..ae7f66b80 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -3138,14 +3138,14 @@ fhandler_console::get_console_process_id (DWORD pid, 
bool match)
   DWORD tmp;
   int num = GetConsoleProcessList (, 1);
   DWORD *list = (DWORD *)
- HeapAlloc (GetProcessHeap (), 0, num * sizeof (DWORD));
-  num = GetConsoleProcessList (list, num);
+ HeapAlloc (GetProcessHeap (), 0, (num + 16) * sizeof (DWORD));
+  num = GetConsoleProcessList (list, num + 16);
   tmp = 0;
   for (int i=0; iset_switch_to_pcon ();
-   return;
   }
 }
 
@@ -381,25 +380,6 @@ fhandler_pty_common::__release_output_mutex (const char 
*fn, int ln)
 #endif
 }
 
-static bool switch_to_pcon_prev;
-
-bool
-fhandler_pty_common::check_switch_to_pcon (void)
-{
-  bool switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (!isHybrid && !switch_to_pcon_prev && switch_to_pcon_now)
-{
-  Sleep (40);
-  /* Check again */
-  switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (switch_to_pcon_now)
-   switch_to_pcon_prev = true;
-}
-  else
-switch_to_pcon_prev = switch_to_pcon_now;
-  return switch_to_pcon_prev;
-}
-
 /* Process pty input. */
 
 void
@@ -595,7 +575,7 @@ out:
 
 fhandler_pty_slave::fhandler_pty_slave (int unit)
   : fhandler_pty_common (), inuse (NULL), output_handle_cyg (NULL),
-  io_handle_cyg (NULL)
+  io_handle_cyg (NULL), 

Re: [PATCH v4 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-09-02 Thread Takashi Yano
started, file is typescript
script: failed to execute /bin/tcsh: Bad address
Script done, file is typescript

I could not find out the reason. Some kind of initialization which
is needed by __loadlocale() may not be done yet. So I copied
only necessary part from __loadlocale() and nl_langinfo().

Simply,
 path_conv a ("/usr/share/locale/locale.alias");
also causes errors on starting mintty.

Ideally, the cause of the error should be found out, I suppose.


-- 
Takashi Yano 


[PATCH v5 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-09-03 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.
---
 winsup/cygwin/dtable.cc   |  38 +--
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |  25 +-
 winsup/cygwin/fhandler_tty.cc | 385 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 ++---
 6 files changed, 289 insertions(+), 254 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index ba5d16206..4e9b6ed56 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -147,18 +147,16 @@ dtable::get_debugger_info ()
 void
 dtable::stdio_init ()
 {
-  bool need_fixup_handle = false;
-  fhandler_pty_slave *ptys = NULL;
-  bool is_pty[3] = {false, false, false};
-  for (int fd = 0; fd < 3; fd ++)
+  int chk_order[] = {1, 0, 2};
+  for (int i = 0; i < 3; i ++)
 {
+  int fd = chk_order[i];
   fhandler_base *fh = cygheap->fdtab[fd];
   if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
- ptys = (fhandler_pty_slave *) fh;
+ fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
  if (ptys->getPseudoConsole ())
{
- is_pty[fd] = true;
  bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
  if (!attached)
@@ -167,15 +165,12 @@ dtable::stdio_init ()
 by some reason. This happens if the executable is
 a windows GUI binary, such as mintty. */
  FreeConsole ();
- AttachConsole (ptys->getHelperProcessId ());
- need_fixup_handle = true;
+ if (AttachConsole (ptys->getHelperProcessId ()))
+   break;
}
- ptys->reset_switch_to_pcon ();
}
}
 }
-  if (need_fixup_handle)
-goto fixup_handle;
 
   if (myself->cygstarted || ISSTATE (myself, PID_CYGPARENT))
 {
@@ -185,27 +180,6 @@ dtable::stdio_init ()
   return;
 }
 
-fixup_handle:
-  if (need_fixup_handle)
-{
-  HANDLE h;
-  h = CreateFile ("CONIN$", GENERIC_READ, FILE_SHARE_READ,
-  NULL, OPEN_EXISTING, 0, 0);
-  if (is_pty[0])
-   {
- SetStdHandle (STD_INPUT_HANDLE, h);
- ptys->set_handle (h);
-   }
-  h = CreateFile ("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE,
-  NULL, OPEN_EXISTING, 0, 0);
-  if (is_pty[1])
-   SetStdHandle (STD_OUTPUT_HANDLE, h);
-  if (is_pty[2])
-   SetStdHandle (STD_ERROR_HANDLE, h);
-  if (is_pty[1] || is_pty[2])
-   ptys->set_output_handle (h);
-}
-
   HANDLE in = GetStdHandle (STD_INPUT_HANDLE);
   HANDLE out = GetStdHandle (STD_OUTPUT_HANDLE);
   HANDLE err = GetStdHandle (STD_ERROR_HANDLE);
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c75e40c0a..e8c165100 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2106,19 +2106,22 @@ class fhandler_pty_common: public fhandler_termios
  protected:
   BOOL process_opost_output (HANDLE h,
 const void *ptr, ssize_t& len, bool is_echo);
-  bool check_switch_to_pcon (void);
 };
 
 class fhandler_pty_slave: public fhandler_pty_common
 {
   HANDLE inuse;// used to indicate that a tty is in use
   HANDLE output_handle_cyg, io_handle_cyg;
+  DWORD pid_restore;
 
   /* Helper functions for fchmod and fchown. */
   bool fch_open_handles (bool chown);
   int fch_set_sd (security_descriptor , bool chown);
   void fch_close_handles ();
 
+  bool try_reattach_pcon ();
+  void restore_reattach_pcon ();
+
  public:
   /* Constructor */
   fhandler_pty_slave (int);
@@ -2172,7 +2175,6 @@ class fhandler_pty_slave: public fhandler_pty_common
   void set_switch_to_pcon (void);
   void reset_switch_to_pcon (void);
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
-  bool has_master_opened (void);
   void mask_switch_to_pcon (bool mask)
   {
 get_ttyp ()->mask_switch_to_pcon = mask;
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 997c50d23..1b034f4b9 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -3136,16 +3136,29 @@ DWORD
 fhandler_console::get_console_process_id (DWORD pid, bool match)
 {
   DWORD tmp;
-  int num = GetConsoleProcessList (, 1);
-  DWORD *list = (DWORD *)
- HeapAlloc (GetProcessHeap (), 0, num * sizeof (DWORD));
-  num = GetConsoleProcessList (list, num);
+  DWORD num, num_req;
+  num = 1;
+  num_req = GetConsoleProcessList (, num);
+  DWORD *list;
+  while (true)
+{
+  list = (DWORD *)
+   HeapAlloc 

[PATCH 0/2] Some fixes for PTY with pseudo console support (2)

2019-09-03 Thread Takashi Yano
[PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.

[PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.
- 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.

Takashi Yano (2):
  Cygwin: pty: Add a workaround for ^C handling.
  Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

 winsup/cygwin/fhandler_tty.cc | 31 ---
 winsup/cygwin/spawn.cc|  6 ++
 winsup/cygwin/tty.cc  |  1 +
 winsup/cygwin/tty.h   |  1 +
 4 files changed, 32 insertions(+), 7 deletions(-)

-- 
2.21.0



[PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.

2019-09-03 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.
---
 winsup/cygwin/fhandler_tty.cc | 5 +
 winsup/cygwin/spawn.cc| 6 ++
 2 files changed, 11 insertions(+)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 240ee017c..283558985 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -2846,6 +2846,9 @@ fhandler_pty_slave::fixup_after_fork (HANDLE parent)
   // fork_fixup (parent, inuse, "inuse");
   // fhandler_pty_common::fixup_after_fork (parent);
   report_tty_counts (this, "inherited", "");
+  if (getPseudoConsole ())
+myself->ctty = 0; /* Avoid setting init_console_handler() in fork.cc.
+This is a workaround for ^C handling problem. */
 }
 
 void
@@ -2882,6 +2885,8 @@ fhandler_pty_slave::fixup_after_exec ()
  FreeConsole ();
}
 }
+  if (getPseudoConsole () && myself->ctty == 0)
+myself->ctty = get_ttyp ()->ntty; /* Restore ctty */
 
 #if USE_API_HOOK
   /* Hook Console API */
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 4bb28c47b..22dafbce3 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -634,6 +634,12 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
}
   if (ptys)
ptys->fixup_after_attach (!iscygwin ());
+  if (attach_to_pcon && !iscygwin ())
+   {
+ myself->ctty = 0; /* Random freezes caused by ^C can be avoided
+  with this. */
+ init_console_handler (true);
+   }
 
 loop:
   /* When ruid != euid we create the new process under the current original
-- 
2.21.0



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

2019-09-03 Thread Takashi Yano
- 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, , 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, , 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



[PATCH v5 0/1] Fix PTY state management in pseudo console support.

2019-09-03 Thread Takashi Yano
Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
has some bugs which cause mismatch between state variables and
real pseudo console state regarding console attaching and r/w
pipe switching. This patch fixes this issue by redesigning the
state management.

v5:
Revise based on
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00111.html

v4:
Small bug fix again.

v3:
Fix the first issue (Bad file descriptor) reported in
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html

v2:
Small bug fixed from v1.

Takashi Yano (1):
  Cygwin: pty: Fix state management for pseudo console support.

 winsup/cygwin/dtable.cc   |  38 +--
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |  25 +-
 winsup/cygwin/fhandler_tty.cc | 385 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 ++---
 6 files changed, 289 insertions(+), 254 deletions(-)

-- 
2.21.0



[PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-03 Thread Takashi Yano
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.
---
 winsup/cygwin/fhandler_tty.cc | 60 ---
 winsup/cygwin/smallprint.cc   |  5 +++
 winsup/cygwin/strace.cc   | 29 +++--
 3 files changed, 51 insertions(+), 43 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index f76f7b262..3a23083de 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -89,6 +89,18 @@ set_switch_to_pcon (void)
   }
 }
 
+void set_ishybrid_and_switch_to_pcon (HANDLE h)
+{
+  DWORD dummy;
+  if (!isHybrid
+  && GetFileType (h) == FILE_TYPE_CHAR
+  && GetConsoleMode (h, ))
+{
+  isHybrid = true;
+  set_switch_to_pcon ();
+}
+}
+
 #define DEF_HOOK(name) static __typeof__ (name) *name##_Orig
 DEF_HOOK (WriteFile);
 DEF_HOOK (WriteConsoleA);
@@ -101,6 +113,7 @@ DEF_HOOK (WriteConsoleOutputW);
 DEF_HOOK (WriteConsoleOutputCharacterA);
 DEF_HOOK (WriteConsoleOutputCharacterW);
 DEF_HOOK (WriteConsoleOutputAttribute);
+DEF_HOOK (SetConsoleTextAttribute);
 DEF_HOOK (WriteConsoleInputA);
 DEF_HOOK (WriteConsoleInputW);
 DEF_HOOK (ReadConsoleInputA);
@@ -197,6 +210,13 @@ WriteConsoleOutputAttribute_Hooked
   return WriteConsoleOutputAttribute_Orig (h, a, l, c, n);
 }
 static BOOL WINAPI
+SetConsoleTextAttribute_Hooked
+ (HANDLE h, WORD a)
+{
+  CHK_CONSOLE_ACCESS (h);
+  return SetConsoleTextAttribute_Orig (h, a);
+}
+static BOOL WINAPI
 WriteConsoleInputA_Hooked
  (HANDLE h, CONST INPUT_RECORD *r, DWORD l, LPDWORD n)
 {
@@ -242,6 +262,7 @@ PeekConsoleInputW_Hooked
 #define WriteFile_Orig 0
 #define ReadFile_Orig 0
 #define PeekConsoleInputA_Orig 0
+void set_ishybrid_and_switch_to_pcon (void) {}
 #endif /* USE_API_HOOK */
 
 bool
@@ -2855,25 +2876,26 @@ fhandler_pty_slave::fixup_after_exec ()
{ \
  void *api = hook_api (module, #name, (void *) name##_Hooked); \
  name##_Orig = (__typeof__ (name) *) api; \
- if (!api) system_printf("Hooking " #name " failed."); \
-   }
-  DO_HOOK ("kernel32.dll", WriteFile);
-  DO_HOOK ("kernel32.dll", WriteConsoleA);
-  DO_HOOK ("kernel32.dll", WriteConsoleW);
-  DO_HOOK ("kernel32.dll", ReadFile);
-  DO_HOOK ("kernel32.dll", ReadConsoleA);
-  DO_HOOK ("kernel32.dll", ReadConsoleW);
-  DO_HOOK ("kernel32.dll", WriteConsoleOutputA);
-  DO_HOOK ("kernel32.dll", WriteConsoleOutputW);
-  DO_HOOK ("kernel32.dll", WriteConsoleOutputCharacterA);
-  DO_HOOK ("kernel32.dll", WriteConsoleOutputCharacterW);
-  DO_HOOK ("kernel32.dll", WriteConsoleOutputAttribute);
-  DO_HOOK ("kernel32.dll", WriteConsoleInputA);
-  DO_HOOK ("kernel32.dll", WriteConsoleInputW);
-  DO_HOOK ("kernel32.dll", ReadConsoleInputA);
-  DO_HOOK ("kernel32.dll", ReadConsoleInputW);
-  DO_HOOK ("kernel32.dll", PeekConsoleInputA);
-  DO_HOOK ("kernel32.dll", PeekConsoleInputW);
+ /*if (api) system_printf(#name " hooked.");*/ \
+   }
+  DO_HOOK (NULL, WriteFile);
+  DO_HOOK (NULL, WriteConsoleA);
+  DO_HOOK (NULL, WriteConsoleW);
+  DO_HOOK (NULL, ReadFile);
+  DO_HOOK (NULL, ReadConsoleA);
+  DO_HOOK (NULL, ReadConsoleW);
+  DO_HOOK (NULL, WriteConsoleOutputA);
+  DO_HOOK (NULL, WriteConsoleOutputW);
+  DO_HOOK (NULL, WriteConsoleOutputCharacterA);
+  DO_HOOK (NULL, WriteConsoleOutputCharacterW);
+  DO_HOOK (NULL, WriteConsoleOutputAttribute);
+  DO_HOOK (NULL, SetConsoleTextAttribute);
+  DO_HOOK (NULL, WriteConsoleInputA);
+  DO_HOOK (NULL, WriteConsoleInputW);
+  DO_HOOK (NULL, ReadConsoleInputA);
+  DO_HOOK (NULL, ReadConsoleInputW);
+  DO_HOOK (NULL, PeekConsoleInputA);
+  DO_HOOK (NULL, PeekConsoleInputW);
 }
 #endif /* USE_API_HOOK */
 }
diff --git a/winsup/cygwin/smallprint.cc b/winsup/cygwin/smallprint.cc
index a7a19132b..bd19c1f5f 100644
--- a/winsup/cygwin/smallprint.cc
+++ b/winsup/cygwin/smallprint.cc
@@ -384,6 +384,9 @@ __small_sprintf (char *dst, const char *fmt, ...)
   return r;
 }
 
+/* Defined in fhandler_tty.cc */
+extern void set_ishybrid_and_switch_to_pcon (HANDLE);
+
 void
 small_printf (const char *fmt, ...)
 {
@@ -405,6 +408,7 @@ small_printf (const char *fmt, ...)
   count = __small_vsprintf (buf, fmt, ap);
   va_end (ap);
 
+  set_ishybrid_and_switch_to_pcon (GetStdHandle (STD_ERROR_HANDLE));
   WriteFile (GetStdHandle (STD_ERROR_HANDLE), buf, count, , NULL);
   FlushFileBuffers (GetStdHandle (STD_ERROR_HANDLE));
 }
@@ -431,6 +435,7 @@ console_printf (const char 

[PATCH 3/4] Cygwin: pty: Move function hook_api() into hookapi.cc.

2019-09-03 Thread Takashi Yano
- PTY uses Win32 API hook for pseudo console suppot. The function
  hook_api() is used for this purpose and defined in fhandler_tty.cc
  previously. This patch moves it into hookapi.cc.
---
 winsup/cygwin/fhandler_tty.cc | 44 ---
 winsup/cygwin/hookapi.cc  | 34 +++
 winsup/cygwin/winsup.h|  1 +
 3 files changed, 35 insertions(+), 44 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 94ef2f8d4..f76f7b262 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -75,50 +75,6 @@ static bool pcon_attached[NTTYS];
 static bool isHybrid;
 
 #if USE_API_HOOK
-/* Hook WIN32 API */
-static
-void *hook_api (const char *mname, const char *name, const void *fn)
-{
-  HMODULE hm = GetModuleHandle (mname);
-  PIMAGE_NT_HEADERS pExeNTHdr = PIMAGE_NT_HEADERS (PBYTE (hm)
-  + PIMAGE_DOS_HEADER (hm)->e_lfanew);
-  DWORD importRVA = pExeNTHdr->OptionalHeader.DataDirectory
-[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
-  PIMAGE_IMPORT_DESCRIPTOR pdfirst =
-(PIMAGE_IMPORT_DESCRIPTOR) ((char *) hm + importRVA);
-  for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
-{
-  if (pd->OriginalFirstThunk == 0)
-   continue;
-  PIMAGE_THUNK_DATA pt =
-   (PIMAGE_THUNK_DATA) ((char *) hm + pd->FirstThunk);
-  PIMAGE_THUNK_DATA pn =
-   (PIMAGE_THUNK_DATA) ((char *) hm + pd->OriginalFirstThunk);
-  for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
-   {
- if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal))
-   continue;
- PIMAGE_IMPORT_BY_NAME pimp =
-   (PIMAGE_IMPORT_BY_NAME) ((char *) hm + pn->u1.AddressOfData);
- if (strcmp (name, (char *) pimp->Name) != 0)
-   continue;
-#ifdef __x86_64__
-#define THUNK_FUNC_TYPE ULONGLONG
-#else
-#define THUNK_FUNC_TYPE DWORD
-#endif
- DWORD ofl = PAGE_READWRITE;
- if (!VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, ))
-   return NULL;
- void *origfn = (void *) pi->u1.Function;
- pi->u1.Function = (THUNK_FUNC_TYPE) fn;
- VirtualProtect (pi, sizeof (THUNK_FUNC_TYPE), ofl, );
- return origfn;
-   }
-}
-  return NULL;
-}
-
 static void
 set_switch_to_pcon (void)
 {
diff --git a/winsup/cygwin/hookapi.cc b/winsup/cygwin/hookapi.cc
index 4078e65bd..dcd9b1df8 100644
--- a/winsup/cygwin/hookapi.cc
+++ b/winsup/cygwin/hookapi.cc
@@ -428,6 +428,40 @@ hook_or_detect_cygwin (const char *name, const void *fn, 
WORD& subsys, HANDLE h)
   return fh.origfn;
 }
 
+/* Hook a function in any DLL such as kernel32.dll */
+/* The DLL must be loaded in advance. */
+/* Used in fhandler_tty.cc */
+void *hook_api (const char *mname, const char *name, const void *fn)
+{
+  HMODULE hm = GetModuleHandle (mname);
+  PIMAGE_NT_HEADERS pExeNTHdr =
+rva (PIMAGE_NT_HEADERS, hm, PIMAGE_DOS_HEADER (hm)->e_lfanew);
+  DWORD importRVA = pExeNTHdr->OptionalHeader.DataDirectory
+[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
+  PIMAGE_IMPORT_DESCRIPTOR pdfirst =
+rva (PIMAGE_IMPORT_DESCRIPTOR, hm, importRVA);
+  for (PIMAGE_IMPORT_DESCRIPTOR pd = pdfirst; pd->FirstThunk; pd++)
+{
+  if (pd->OriginalFirstThunk == 0)
+   continue;
+  PIMAGE_THUNK_DATA pt = rva (PIMAGE_THUNK_DATA, hm, pd->FirstThunk);
+  PIMAGE_THUNK_DATA pn =
+   rva (PIMAGE_THUNK_DATA, hm, pd->OriginalFirstThunk);
+  for (PIMAGE_THUNK_DATA pi = pt; pn->u1.Ordinal; pi++, pn++)
+   {
+ if (IMAGE_SNAP_BY_ORDINAL (pn->u1.Ordinal))
+   continue;
+ PIMAGE_IMPORT_BY_NAME pimp =
+   rva (PIMAGE_IMPORT_BY_NAME, hm, pn->u1.AddressOfData);
+ if (strcmp (name, (char *) pimp->Name) != 0)
+   continue;
+ void *origfn = putmem (pi, fn);
+ return origfn;
+   }
+}
+  return NULL;
+}
+
 void
 ld_preload ()
 {
diff --git a/winsup/cygwin/winsup.h b/winsup/cygwin/winsup.h
index 95ab41e6b..ab7b3bbdc 100644
--- a/winsup/cygwin/winsup.h
+++ b/winsup/cygwin/winsup.h
@@ -199,6 +199,7 @@ ino_t __reg2 hash_path_name (ino_t hash, const char *name);
 void __reg2 nofinalslash (const char *src, char *dst);
 
 void __reg3 *hook_or_detect_cygwin (const char *, const void *, WORD&, HANDLE 
h = NULL);
+void __reg3 *hook_api (const char *mname, const char *name, const void *fn);
 
 /* Time related */
 void __stdcall totimeval (struct timeval *, PLARGE_INTEGER, int, int);
-- 
2.21.0



[PATCH 1/4] Cygwin: pty: Code cleanup

2019-09-03 Thread Takashi Yano
- Cleanup the code which is commented out by #if 0 regarding pseudo
  console.
- Remove #if 1 for experimental code which seems to be stable.
---
 winsup/cygwin/fhandler_tty.cc | 28 
 1 file changed, 28 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index dd5ab528a..4dbe96b4a 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -855,26 +855,6 @@ fhandler_pty_slave::cleanup ()
 int
 fhandler_pty_slave::close ()
 {
-#if 0
-  if (getPseudoConsole ())
-{
-  INPUT_RECORD inp[128];
-  DWORD n;
-  PeekFunc =
-   PeekConsoleInputA_Orig ? PeekConsoleInputA_Orig : PeekConsoleInput;
-  PeekFunc (get_handle (), inp, 128, );
-  bool pipe_empty = true;
-  while (n-- > 0)
-   if (inp[n].EventType == KEY_EVENT && inp[n].Event.KeyEvent.bKeyDown)
- pipe_empty = false;
-  if (pipe_empty)
-   {
- /* Flush input buffer */
- size_t len = UINT_MAX;
- read (NULL, len);
-   }
-}
-#endif
   termios_printf ("closing last open %s handle", ttyname ());
   if (inuse && !CloseHandle (inuse))
 termios_printf ("CloseHandle (inuse), %E");
@@ -1524,7 +1504,6 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
 out:
   termios_printf ("%d = read(%p, %lu)", totalread, ptr, len);
   len = (size_t) totalread;
-#if 1 /* Experimenta code */
   /* Push slave read as echo to pseudo console screen buffer. */
   if (getPseudoConsole () && ptr0 && (get_ttyp ()->ti.c_lflag & ECHO))
 {
@@ -1532,7 +1511,6 @@ out:
   push_to_pcon_screenbuffer (ptr0, len);
   release_output_mutex ();
 }
-#endif
   mask_switch_to_pcon (false);
 }
 
@@ -2748,10 +2726,6 @@ restart:
   if (p)
 *p = L'-';
   LCID lcid = LocaleNameToLCID (lc, 0);
-#if 0
-  if (lcid == (LCID) -1)
-return lcid;
-#endif
   if (!lcid && !strcmp (charset, "ASCII"))
 return 0;
 
@@ -2842,7 +2816,6 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
}
}
 
-#if 1 /* Experimental code */
  /* Clear screen to synchronize pseudo console screen buffer
 with real terminal. This is necessary because pseudo
 console screen buffer is empty at start. */
@@ -2854,7 +2827,6 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
/* Assume this is the first process using this pty slave. */
WriteFile (get_output_handle_cyg (),
   "\033[H\033[J", 6, , NULL);
-#endif
 
  pcon_attached[get_minor ()] = true;
  get_ttyp ()->num_pcon_attached_slaves ++;
-- 
2.21.0



[PATCH 2/4] Cygwin: pty: Speed up a little hooked Win32 API for pseudo console.

2019-09-03 Thread Takashi Yano
- Some Win32 APIs are hooked in pty code for pseudo console support.
  This causes slow down. This patch improves speed a little.
---
 winsup/cygwin/fhandler_tty.cc | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 4dbe96b4a..94ef2f8d4 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -155,7 +155,9 @@ DEF_HOOK (PeekConsoleInputW);
 #define CHK_CONSOLE_ACCESS(h) \
 { \
   DWORD dummy; \
-  if (!isHybrid && GetConsoleMode (h, )) \
+  if (!isHybrid \
+  && GetFileType (h) == FILE_TYPE_CHAR \
+  && GetConsoleMode (h, )) \
 { \
   isHybrid = true; \
   set_switch_to_pcon (); \
-- 
2.21.0



[PATCH 0/4] Some fixes for PTY with pseudo console support (1)

2019-09-03 Thread Takashi Yano
[PATCH 1/4] Cygwin: pty: Code cleanup
- Cleanup the code which is commented out by #if 0 regarding pseudo
  console.
- Remove #if 1 for experimental code which seems to be stable.

[PATCH 2/4] Cygwin: pty: Speed up a little hooked Win32 API for pseudo console.
- Some Win32 APIs are hooked in pty code for pseudo console support.
  This causes slow down. This patch improves speed a little.

[PATCH 3/4] Cygwin: pty: Move function hook_api() into hookapi.cc.
- PTY uses Win32 API hook for pseudo console suppot. The function
  hook_api() is used for this purpose and defined in fhandler_tty.cc
  previously. This patch moves it into hookapi.cc.

[PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.

Takashi Yano (4):
  Cygwin: pty: Code cleanup
  Cygwin: pty: Speed up a little hooked Win32 API for pseudo console.
  Cygwin: pty: Move function hook_api() into hookapi.cc.
  Cygwin: pty: Limit API hook to the program linked with the APIs.

 winsup/cygwin/fhandler_tty.cc | 136 +++---
 winsup/cygwin/hookapi.cc  |  34 +
 winsup/cygwin/smallprint.cc   |   5 ++
 winsup/cygwin/strace.cc   |  29 ++--
 winsup/cygwin/winsup.h|   1 +
 5 files changed, 89 insertions(+), 116 deletions(-)

-- 
2.21.0



Re: [PATCH v4 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-09-03 Thread Takashi Yano
Hi Corinna,

I have posted several patches for PTY with pseudo console support.
Please apply them in the following order.

[PATCH 1/4] Cygwin: pty: Code cleanup
- Cleanup the code which is commented out by #if 0 regarding pseudo
  console.
- Remove #if 1 for experimental code which seems to be stable.

[PATCH 2/4] Cygwin: pty: Speed up a little hooked Win32 API for pseudo console.
- Some Win32 APIs are hooked in pty code for pseudo console support.
  This causes slow down. This patch improves speed a little.

[PATCH 3/4] Cygwin: pty: Move function hook_api() into hookapi.cc.
- PTY uses Win32 API hook for pseudo console suppot. The function
  hook_api() is used for this purpose and defined in fhandler_tty.cc
  previously. This patch moves it into hookapi.cc.

[PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.

[PATCH v5 1/1] Cygwin: pty: Fix state management for pseudo console support.
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.

This hopefully fixes the problem 3 in
https://cygwin.com/ml/cygwin/2019-08/msg00401.html
This also fixes the first problem regarding "Bad file descriptor" error
reported in
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html

[PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.

[PATCH 2/2] Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.
- 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.

This fixes the second problem on emacs reported in
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html

On Tue, 3 Sep 2019 11:16:38 +0200
Corinna Vinschen wrote:
> This is a slowdown of about 15%.  That's quite a lot.  Can't you just
> check the incoming handle against the interesting handles somehow?
> If there's no other way around it, we should at least make sure (in a
> separate patch) that Cygwin calls NtReadFile/NtWriteFile throughout,
> except in tty and console code.

I came up with an idea, and implemented it. As described obove,
Win32 APIs are not hooked any more in normal cygwin process.
Hook is done only if the program is directly linked with corresponding
APIs. However, this strategy does not have the effect for console
access by cygwin1.dll itself. So, to switch r/w pipe to pseudo console
side, I added the code in strace.cc and smallprint.cc.

Could you please have a look?

-- 
Takashi Yano 


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

2019-09-03 Thread Takashi Yano
Hi Brian,

On Tue, 3 Sep 2019 20:47:14 -0600
Brian Inglis wrote:
> On 2019-09-03 19:46, Takashi Yano wrote:
> > - 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.
> 
> Dealing with escape sequences is way out of the scope of any pty driver.
> It is up to the terminal emulator or applications running in the terminal to
> handle terminal characteristics appropriately.
> 
> The pty driver should not touch *ANY* escape sequences coming from the system,
> nor should it generate any to it, as TERM may not be set at the time, or
> appropriately or usefully in some shells e.g. cmd or powershell.
> 
> Most folks probably use mintty or cmd as their Cygwin terminal, but some use
> other terminals, like various flavours of xterm and rxvt, with ssh sessions in
> and out, so they could be on Linux consoles or proprietary AIX/HP-UX/Sun
> terminals, and operate properly as long as they have a good terminfo 
> definition.
> 
> I see this issue as similar to the Windows text file handling changes required
> when coreutils/textutils went POSIX and removed '\r\n' crlf handling to give 
> the
> same results as on Unix systems.
> To handle terminal characteristics properly would require terminfo support in
> the pty driver: I doubt anyone wants that, so the best approach is to do
> nothing, and let the terminal or application handle it: they are more likely 
> to
> have the configuration options or hooks to do so easily.

You are definitely right. However, the essence of the problem is that
the pseudo console itself outputs a lot of ANSI escape sequences
even if client program output only ASCII characters.

Attached is the raw output from pseudo console when the screen shows
the simple text below.

 from here 
[yano@Express5800-S70 ~]$ cmd
Microsoft Windows [Version 10.0.18362.329]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\cygwin\home\yano>exit
[yano@Express5800-S70 ~]$ exit
exit
 to here 

You will noticed that the screen is cleared if you execute
cat pcon-output.log
in a terminal which support ANSI escape sequences.

So clearing the screen when creating pseudo console is the result of
compromise to synchronize real screen and screen buffer of pseudo
console.

-- 
Takashi Yano 


pcon-output.log
Description: Binary data


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

2019-09-03 Thread Takashi Yano
On Wed, 4 Sep 2019 12:34:31 +0900
Takashi Yano wrote:
> Attached is the raw output from pseudo console when the screen shows
> the simple text below.
> 
>  from here 
> [yano@Express5800-S70 ~]$ cmd
> Microsoft Windows [Version 10.0.18362.329]
> (c) 2019 Microsoft Corporation. All rights reserved.
> 
> C:\cygwin\home\yano>exit
> [yano@Express5800-S70 ~]$ exit
> exit
>  to here 
> 
> You will noticed that the screen is cleared if you execute
> cat pcon-output.log
> in a terminal which support ANSI escape sequences.

This pcon-output.log was recorded in screen of 80x24 size.
Please try above in 80x24 terminal.

-- 
Takashi Yano 


[PATCH v3 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-09-01 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.
---
 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 413 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 +++--
 6 files changed, 288 insertions(+), 241 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index ba5d16206..6266f1bc2 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -150,8 +150,11 @@ dtable::stdio_init ()
   bool need_fixup_handle = false;
   fhandler_pty_slave *ptys = NULL;
   bool is_pty[3] = {false, false, false};
-  for (int fd = 0; fd < 3; fd ++)
+  bool already_attached = false;
+  int chk_order[] = {1, 0, 2};
+  for (int i = 0; i < 3; i ++)
 {
+  int fd = chk_order[i];
   fhandler_base *fh = cygheap->fdtab[fd];
   if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
@@ -161,14 +164,18 @@ dtable::stdio_init ()
  is_pty[fd] = true;
  bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
- if (!attached)
+ already_attached = already_attached || attached;
+ if (!already_attached)
{
  /* Not attached to pseudo console in fork() or spawn()
 by some reason. This happens if the executable is
 a windows GUI binary, such as mintty. */
  FreeConsole ();
- AttachConsole (ptys->getHelperProcessId ());
- need_fixup_handle = true;
+ if (AttachConsole (ptys->getHelperProcessId ()))
+   {
+ need_fixup_handle = true;
+ already_attached = true;
+   }
}
  ptys->reset_switch_to_pcon ();
}
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c75e40c0a..af2b948cb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2106,19 +2106,22 @@ class fhandler_pty_common: public fhandler_termios
  protected:
   BOOL process_opost_output (HANDLE h,
 const void *ptr, ssize_t& len, bool is_echo);
-  bool check_switch_to_pcon (void);
 };
 
 class fhandler_pty_slave: public fhandler_pty_common
 {
   HANDLE inuse;// used to indicate that a tty is in use
   HANDLE output_handle_cyg, io_handle_cyg;
+  DWORD pidRestore;
 
   /* Helper functions for fchmod and fchown. */
   bool fch_open_handles (bool chown);
   int fch_set_sd (security_descriptor , bool chown);
   void fch_close_handles ();
 
+  bool try_reattach_pcon ();
+  void restore_reattach_pcon ();
+
  public:
   /* Constructor */
   fhandler_pty_slave (int);
@@ -2172,7 +2175,6 @@ class fhandler_pty_slave: public fhandler_pty_common
   void set_switch_to_pcon (void);
   void reset_switch_to_pcon (void);
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
-  bool has_master_opened (void);
   void mask_switch_to_pcon (bool mask)
   {
 get_ttyp ()->mask_switch_to_pcon = mask;
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 997c50d23..ae7f66b80 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -3138,14 +3138,14 @@ fhandler_console::get_console_process_id (DWORD pid, 
bool match)
   DWORD tmp;
   int num = GetConsoleProcessList (, 1);
   DWORD *list = (DWORD *)
- HeapAlloc (GetProcessHeap (), 0, num * sizeof (DWORD));
-  num = GetConsoleProcessList (list, num);
+ HeapAlloc (GetProcessHeap (), 0, (num + 16) * sizeof (DWORD));
+  num = GetConsoleProcessList (list, num + 16);
   tmp = 0;
   for (int i=0; iset_switch_to_pcon ();
-   return;
   }
 }
 
@@ -381,25 +380,6 @@ fhandler_pty_common::__release_output_mutex (const char 
*fn, int ln)
 #endif
 }
 
-static bool switch_to_pcon_prev;
-
-bool
-fhandler_pty_common::check_switch_to_pcon (void)
-{
-  bool switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (!isHybrid && !switch_to_pcon_prev && switch_to_pcon_now)
-{
-  Sleep (40);
-  /* Check again */
-  switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (switch_to_pcon_now)
-   switch_to_pcon_prev = true;
-}
-  else
-switch_to_pcon_prev = switch_to_pcon_now;
-  return switch_to_pcon_prev;
-}
-
 /* Process pty input. */
 
 void
@@ -595,7 +575,7 @@ out:
 
 fhandler_pty_slave::fhandler_pty_slave (int unit)
   : fhandler_pty_common (), inuse (NULL), output_handle_cyg (NULL),
-  io_handle_cyg (NULL)
+  io_handle_cyg (NULL), 

[PATCH v3 0/1] Fix PTY state management in pseudo console support.

2019-09-01 Thread Takashi Yano
Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
has some bugs which cause mismatch between state variables and
real pseudo console state regarding console attaching and r/w
pipe switching. This patch fixes this issue by redesigning the
state management.

v3:
Fix the first issue (Bad file descriptor) reported in
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html

v2:
Small bug fixed from v1.

Takashi Yano (1):
  Cygwin: pty: Fix state management for pseudo console support.

 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 413 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 +++--
 6 files changed, 288 insertions(+), 241 deletions(-)

-- 
2.21.0



[PATCH v4 0/1] Fix PTY state management in pseudo console support.

2019-09-01 Thread Takashi Yano
Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
has some bugs which cause mismatch between state variables and
real pseudo console state regarding console attaching and r/w
pipe switching. This patch fixes this issue by redesigning the
state management.

v4:
Small bug fix again.

v3:
Fix the first issue (Bad file descriptor) reported in
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html

v2:
Small bug fixed from v1.


Takashi Yano (1):
  Cygwin: pty: Fix state management for pseudo console support.

 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 415 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 +++--
 6 files changed, 289 insertions(+), 242 deletions(-)

-- 
2.21.0



[PATCH v4 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-09-01 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.
---
 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 415 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 +++--
 6 files changed, 289 insertions(+), 242 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index ba5d16206..6266f1bc2 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -150,8 +150,11 @@ dtable::stdio_init ()
   bool need_fixup_handle = false;
   fhandler_pty_slave *ptys = NULL;
   bool is_pty[3] = {false, false, false};
-  for (int fd = 0; fd < 3; fd ++)
+  bool already_attached = false;
+  int chk_order[] = {1, 0, 2};
+  for (int i = 0; i < 3; i ++)
 {
+  int fd = chk_order[i];
   fhandler_base *fh = cygheap->fdtab[fd];
   if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
@@ -161,14 +164,18 @@ dtable::stdio_init ()
  is_pty[fd] = true;
  bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
- if (!attached)
+ already_attached = already_attached || attached;
+ if (!already_attached)
{
  /* Not attached to pseudo console in fork() or spawn()
 by some reason. This happens if the executable is
 a windows GUI binary, such as mintty. */
  FreeConsole ();
- AttachConsole (ptys->getHelperProcessId ());
- need_fixup_handle = true;
+ if (AttachConsole (ptys->getHelperProcessId ()))
+   {
+ need_fixup_handle = true;
+ already_attached = true;
+   }
}
  ptys->reset_switch_to_pcon ();
}
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c75e40c0a..af2b948cb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2106,19 +2106,22 @@ class fhandler_pty_common: public fhandler_termios
  protected:
   BOOL process_opost_output (HANDLE h,
 const void *ptr, ssize_t& len, bool is_echo);
-  bool check_switch_to_pcon (void);
 };
 
 class fhandler_pty_slave: public fhandler_pty_common
 {
   HANDLE inuse;// used to indicate that a tty is in use
   HANDLE output_handle_cyg, io_handle_cyg;
+  DWORD pidRestore;
 
   /* Helper functions for fchmod and fchown. */
   bool fch_open_handles (bool chown);
   int fch_set_sd (security_descriptor , bool chown);
   void fch_close_handles ();
 
+  bool try_reattach_pcon ();
+  void restore_reattach_pcon ();
+
  public:
   /* Constructor */
   fhandler_pty_slave (int);
@@ -2172,7 +2175,6 @@ class fhandler_pty_slave: public fhandler_pty_common
   void set_switch_to_pcon (void);
   void reset_switch_to_pcon (void);
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
-  bool has_master_opened (void);
   void mask_switch_to_pcon (bool mask)
   {
 get_ttyp ()->mask_switch_to_pcon = mask;
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 997c50d23..ae7f66b80 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -3138,14 +3138,14 @@ fhandler_console::get_console_process_id (DWORD pid, 
bool match)
   DWORD tmp;
   int num = GetConsoleProcessList (, 1);
   DWORD *list = (DWORD *)
- HeapAlloc (GetProcessHeap (), 0, num * sizeof (DWORD));
-  num = GetConsoleProcessList (list, num);
+ HeapAlloc (GetProcessHeap (), 0, (num + 16) * sizeof (DWORD));
+  num = GetConsoleProcessList (list, num + 16);
   tmp = 0;
   for (int i=0; iset_switch_to_pcon ();
-   return;
   }
 }
 
@@ -381,25 +380,6 @@ fhandler_pty_common::__release_output_mutex (const char 
*fn, int ln)
 #endif
 }
 
-static bool switch_to_pcon_prev;
-
-bool
-fhandler_pty_common::check_switch_to_pcon (void)
-{
-  bool switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (!isHybrid && !switch_to_pcon_prev && switch_to_pcon_now)
-{
-  Sleep (40);
-  /* Check again */
-  switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (switch_to_pcon_now)
-   switch_to_pcon_prev = true;
-}
-  else
-switch_to_pcon_prev = switch_to_pcon_now;
-  return switch_to_pcon_prev;
-}
-
 /* Process pty input. */
 
 void
@@ -595,7 +575,7 @@ out:
 
 fhandler_pty_slave::fhandler_pty_slave (int unit)
   : fhandler_pty_common (), inuse (NULL), output_handle_cyg (NULL),
-  io_handle_cyg (NULL)
+  io_handle_cyg (NULL), 

Re: [PATCH v2 0/1] Fix PTY state management in pseudo console support.

2019-09-01 Thread Takashi Yano
Hi Ken,

Thank you for testing.

On Sun, 1 Sep 2019 15:13:47 +
Ken Brown wrote:
> On 8/31/2019 6:54 PM, Takashi Yano wrote:
> > Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
> > introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
> > has some bugs which cause mismatch between state variables and
> > real pseudo console state regarding console attaching and r/w
> > pipe switching. This patch fixes this issue by redesigning the
> > state management.
> 
> After applying this patch, I get the following in mintty:
> 
> $ cygcheck -cd | grep bash
> grep: write error: Bad file descriptor
> 
> Further commands after that lead to the cursor jumping around.

I have fixed this problem. I will post it as v3 patch soon.

> Here's a second glitch I've noticed (starting with commit 
> 169d65a5774acc76ce3f3feeedcbae7405aa9b57): In emacs, if I run a command that 
> uses compilation mode, the output displayed in the compilation buffer starts 
> with ^[[H^[[J.  Here ^[ is the escape character, so this is apparently the 
> two 
> ANSI escape sequences ESC[H and ESC[J.
> 
> Sample commands that use compilation mode are 'M-x compile', 'M-x rgrep', and 
> 'M-x find-name-directory'.  I can provide more detailed reproduction 
> instructions if you're not an emacs user.

Hmmm, it seems that ANSI escape sequences are not recognized in emacs.

> 'M-x find-name-directory'
Do you mean find-name-dired ?

-- 
Takashi Yano 


[PATCH v2 1/1] Cygwin: pty: Fix state management for pseudo console support.

2019-08-31 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 has some bugs which
  cause mismatch between state variables and real pseudo console
  state regarding console attaching and r/w pipe switching. This
  patch fixes this issue by redesigning the state management.
---
 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 404 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 ++---
 6 files changed, 283 insertions(+), 237 deletions(-)

diff --git a/winsup/cygwin/dtable.cc b/winsup/cygwin/dtable.cc
index ba5d16206..6266f1bc2 100644
--- a/winsup/cygwin/dtable.cc
+++ b/winsup/cygwin/dtable.cc
@@ -150,8 +150,11 @@ dtable::stdio_init ()
   bool need_fixup_handle = false;
   fhandler_pty_slave *ptys = NULL;
   bool is_pty[3] = {false, false, false};
-  for (int fd = 0; fd < 3; fd ++)
+  bool already_attached = false;
+  int chk_order[] = {1, 0, 2};
+  for (int i = 0; i < 3; i ++)
 {
+  int fd = chk_order[i];
   fhandler_base *fh = cygheap->fdtab[fd];
   if (fh && fh->get_major () == DEV_PTYS_MAJOR)
{
@@ -161,14 +164,18 @@ dtable::stdio_init ()
  is_pty[fd] = true;
  bool attached = !!fhandler_console::get_console_process_id
(ptys->getHelperProcessId (), true);
- if (!attached)
+ already_attached = already_attached || attached;
+ if (!already_attached)
{
  /* Not attached to pseudo console in fork() or spawn()
 by some reason. This happens if the executable is
 a windows GUI binary, such as mintty. */
  FreeConsole ();
- AttachConsole (ptys->getHelperProcessId ());
- need_fixup_handle = true;
+ if (AttachConsole (ptys->getHelperProcessId ()))
+   {
+ need_fixup_handle = true;
+ already_attached = true;
+   }
}
  ptys->reset_switch_to_pcon ();
}
diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index c75e40c0a..af2b948cb 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2106,19 +2106,22 @@ class fhandler_pty_common: public fhandler_termios
  protected:
   BOOL process_opost_output (HANDLE h,
 const void *ptr, ssize_t& len, bool is_echo);
-  bool check_switch_to_pcon (void);
 };
 
 class fhandler_pty_slave: public fhandler_pty_common
 {
   HANDLE inuse;// used to indicate that a tty is in use
   HANDLE output_handle_cyg, io_handle_cyg;
+  DWORD pidRestore;
 
   /* Helper functions for fchmod and fchown. */
   bool fch_open_handles (bool chown);
   int fch_set_sd (security_descriptor , bool chown);
   void fch_close_handles ();
 
+  bool try_reattach_pcon ();
+  void restore_reattach_pcon ();
+
  public:
   /* Constructor */
   fhandler_pty_slave (int);
@@ -2172,7 +2175,6 @@ class fhandler_pty_slave: public fhandler_pty_common
   void set_switch_to_pcon (void);
   void reset_switch_to_pcon (void);
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
-  bool has_master_opened (void);
   void mask_switch_to_pcon (bool mask)
   {
 get_ttyp ()->mask_switch_to_pcon = mask;
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 997c50d23..ae7f66b80 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -3138,14 +3138,14 @@ fhandler_console::get_console_process_id (DWORD pid, 
bool match)
   DWORD tmp;
   int num = GetConsoleProcessList (, 1);
   DWORD *list = (DWORD *)
- HeapAlloc (GetProcessHeap (), 0, num * sizeof (DWORD));
-  num = GetConsoleProcessList (list, num);
+ HeapAlloc (GetProcessHeap (), 0, (num + 16) * sizeof (DWORD));
+  num = GetConsoleProcessList (list, num + 16);
   tmp = 0;
   for (int i=0; iset_switch_to_pcon ();
-   return;
   }
 }
 
@@ -381,25 +380,6 @@ fhandler_pty_common::__release_output_mutex (const char 
*fn, int ln)
 #endif
 }
 
-static bool switch_to_pcon_prev;
-
-bool
-fhandler_pty_common::check_switch_to_pcon (void)
-{
-  bool switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (!isHybrid && !switch_to_pcon_prev && switch_to_pcon_now)
-{
-  Sleep (40);
-  /* Check again */
-  switch_to_pcon_now = get_ttyp ()->switch_to_pcon;
-  if (switch_to_pcon_now)
-   switch_to_pcon_prev = true;
-}
-  else
-switch_to_pcon_prev = switch_to_pcon_now;
-  return switch_to_pcon_prev;
-}
-
 /* Process pty input. */
 
 void
@@ -595,7 +575,7 @@ out:
 
 fhandler_pty_slave::fhandler_pty_slave (int unit)
   : fhandler_pty_common (), inuse (NULL), output_handle_cyg (NULL),
-  io_handle_cyg (NULL)
+  io_handle_cyg (NULL), 

[PATCH v2 0/1] Fix PTY state management in pseudo console support.

2019-08-31 Thread Takashi Yano
Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
has some bugs which cause mismatch between state variables and
real pseudo console state regarding console attaching and r/w
pipe switching. This patch fixes this issue by redesigning the
state management.

v2:
Small bug fixed from v1.

Takashi Yano (1):
  Cygwin: pty: Fix state management for pseudo console support.

 winsup/cygwin/dtable.cc   |  15 +-
 winsup/cygwin/fhandler.h  |   6 +-
 winsup/cygwin/fhandler_console.cc |   6 +-
 winsup/cygwin/fhandler_tty.cc | 404 --
 winsup/cygwin/fork.cc |  24 +-
 winsup/cygwin/spawn.cc|  65 ++---
 6 files changed, 283 insertions(+), 237 deletions(-)

-- 
2.21.0



Re: [PATCH 0/1] Fix PTY state management in pseudo console support.

2019-08-31 Thread Takashi Yano
This patch has a small bug.
Please use v2 instead.

On Sat, 31 Aug 2019 23:53:17 +0900
Takashi Yano  wrote:

> Pseudo console support in test release TEST: Cygwin 3.1.0-0.3,
> introduced by commit 169d65a5774acc76ce3f3feeedcbae7405aa9b57,
> has some bugs which cause mismatch between state variables and
> real pseudo console state regarding console attaching and r/w
> pipe switching. This patch fixes this issue by redesigning the
> state management.
> 
> Takashi Yano (1):
>   Cygwin: pty: Fix state management for pseudo console support.
> 
>  winsup/cygwin/dtable.cc   |  15 +-
>  winsup/cygwin/fhandler.h  |   6 +-
>  winsup/cygwin/fhandler_console.cc |   6 +-
>  winsup/cygwin/fhandler_tty.cc | 401 --
>  winsup/cygwin/fork.cc |  24 +-
>  winsup/cygwin/spawn.cc|  65 ++---
>  6 files changed, 280 insertions(+), 237 deletions(-)
> 
> -- 
> 2.21.0
> 


-- 
Takashi Yano 


[PATCH 0/1] Cygwin: pty: Make it sure to show up system error messages.

2019-09-05 Thread Takashi Yano
- Forcibly attach to pseudo console in advance so that the error
  messages by system_printf() is displayed to screen reliably.
  This is needed when stdout is redirected to another pty. In this
  case, process has two ptys opened. However, process can attach
  to only one console. So it is necessary to change console attached.

Takashi Yano (1):
  Cygwin: pty: Make it sure to show up system error messages.

 winsup/cygwin/fhandler_tty.cc | 55 +++
 1 file changed, 49 insertions(+), 6 deletions(-)

-- 
2.21.0



[PATCH 1/1] Cygwin: pty: Make it sure to show up system error messages.

2019-09-05 Thread Takashi Yano
- Forcibly attach to pseudo console in advance so that the error
  messages by system_printf() is displayed to screen reliably.
  This is needed when stdout is redirected to another pty. In this
  case, process has two ptys opened. However, process can attach
  to only one console. So it is necessary to change console attached.
---
 winsup/cygwin/fhandler_tty.cc | 55 +++
 1 file changed, 49 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 78c9c9128..1a1ae1a5c 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -88,16 +88,59 @@ set_switch_to_pcon (void)
   }
 }
 
+static void
+force_attach_to_pcon (HANDLE h)
+{
+  bool attach_done = false;
+  for (int n = 0; n < 2; n ++)
+{
+  /* First time, attach to the pty whoes handle value is match.
+Second time, try to attach to any pty. */
+  cygheap_fdenum cfd (false);
+  while (cfd.next () >= 0)
+   if (cfd->get_major () == DEV_PTYS_MAJOR)
+ {
+   fhandler_base *fh = cfd;
+   fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
+   if (n != 0
+   || h == ptys->get_handle ()
+   || h == ptys->get_output_handle ())
+ {
+   if (fhandler_console::get_console_process_id
+ (ptys->getHelperProcessId (), true))
+ attach_done = true;
+   else
+ {
+   FreeConsole ();
+   if (AttachConsole (ptys->getHelperProcessId ()))
+ {
+   pcon_attached_to = ptys->get_minor ();
+   attach_done = true;
+ }
+   else
+ pcon_attached_to = -1;
+ }
+   break;
+ }
+ }
+  if (attach_done)
+   break;
+}
+}
+
 void
 set_ishybrid_and_switch_to_pcon (HANDLE h)
 {
-  DWORD dummy;
-  if (!isHybrid
-  && GetFileType (h) == FILE_TYPE_CHAR
-  && GetConsoleMode (h, ))
+  if (GetFileType (h) == FILE_TYPE_CHAR)
 {
-  isHybrid = true;
-  set_switch_to_pcon ();
+  force_attach_to_pcon (h);
+  DWORD dummy;
+  if (!isHybrid && (GetConsoleMode (h, )
+   || GetLastError () != ERROR_INVALID_HANDLE))
+   {
+ isHybrid = true;
+ set_switch_to_pcon ();
+   }
 }
 }
 
-- 
2.21.0



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

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 15:55:03 +0200
Corinna Vinschen wrote:
> The code in fixup_after_attach() is the only code snippet setting
> need_clear_screen = true.  And that code also requires term != "dump" &&
> term == "*emacs*" to set need_clear_screen.

term != "*emacs*"

> The code in reset_switch_to_pcon() requires that the need_clear_screen
> flag is true regardless of checking TERM.  So this code depends on the
> successful TERM check from fixup_after_attach anyway.
> 
> What am I missing?

Two checking results may not be the same. Indeed, emacs changes
TERM between two checks.

fixup_after_attach() is called from fixup_after_exec(),
which is called before executing the program code.
reset_switch_to_pcon () is mainly called from PTY slave I/O functions.
This is usually from the program code.

The behaviour of the patch is as follows.

First check : True  True  False False
Second check: True  False True  False
Clear screen: Yes   NoNo   No

# True: neither dumb nor emacs*
#  False: either dumb or emacs*


> + if (get_ttyp ()->num_pcon_attached_slaves == 0 &&
> + term && strcmp (term, "dumb") &&
> +   term && !strstr (term, "emacs") &&
> + !ALWAYS_USE_PCON)
> 
> You're checking term for != NULL twice.

Oh my!

-- 
Takashi Yano 


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

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 17:19:05 +0200
Corinna Vinschen wrote:
> > But the first check (the one in fixup_after_attach()) could be dropped, 
> > couldn't it?
> 
> IIUC the second test first checks for need_clear_screen but then the
> TERM might have changed in the meantime which in turn requires to change
> the behaviour again.  But yeah, this sound like the first patch is not
> actually required at all.

I was convinced. I will revise the patch.

-- 
Takashi Yano 


Re: [PATCH 4/4] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Takashi Yano
Hi Corinna,

On Wed, 4 Sep 2019 12:03:51 +0200
Corinna Vinschen wrote:
> I'll push the other 3 patches from this series.  For this patch,
> I wonder why you create set_ishybrid_and_switch_to_pcon while
> at the same time define a macro CHK_CONSOLE_ACCESS with identical
> functionality.

Yah, indeed!

> Suggestion: Only define set_ishybrid_and_switch_to_pcon() as
> inline function (probably in winsup.h) and use only this througout.

This function uses static variable isHybrid (sorry camelback again)
and static function set_switch_to_pcon() defined in fhandler_tty.cc.

To make it inline, a lot of changes will be necessary. How about
non-inline function?

-- 
Takashi Yano 


Re: [PATCH 1/2] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 12:42:22 +0200
Corinna Vinschen wrote:
> If this workaround works, what about making it the standard behaviour,
> rather than pseudo-console only?  Would there be a downside?

I am not sure why, but console does not have this issue.
However, I do not notice any downside.

If making it standard, the patch will be very simple as follows.


diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index a3a7e7505..0a929dffd 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -213,7 +213,6 @@ frok::child (volatile char * volatile here)
  - terminate the current fork call even if the child is initialized. */
   sync_with_parent ("performed fork fixups and dynamic dll loading", true);

-  init_console_handler (myself->ctty > 0);
   ForceCloseHandle1 (fork_info->forker_finished, forker_finished);

   pthread::atforkchild ();
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 4bb28c47b..15cba3610 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -635,6 +635,12 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   if (ptys)
ptys->fixup_after_attach (!iscygwin ());

+  if (!iscygwin ())
+   {
+ init_console_handler (myself->ctty > 0);
+ myself->ctty = 0;
+   }
+
 loop:
   /* When ruid != euid we create the new process under the current original
 account and impersonate in child, this way maintaining the different

-- 
Takashi Yano 


[PATCH v2 1/1] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.
---
 winsup/cygwin/fork.cc  | 1 -
 winsup/cygwin/spawn.cc | 6 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fork.cc b/winsup/cygwin/fork.cc
index a3a7e7505..0a929dffd 100644
--- a/winsup/cygwin/fork.cc
+++ b/winsup/cygwin/fork.cc
@@ -213,7 +213,6 @@ frok::child (volatile char * volatile here)
  - terminate the current fork call even if the child is initialized. */
   sync_with_parent ("performed fork fixups and dynamic dll loading", true);
 
-  init_console_handler (myself->ctty > 0);
   ForceCloseHandle1 (fork_info->forker_finished, forker_finished);
 
   pthread::atforkchild ();
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 4bb28c47b..15cba3610 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -635,6 +635,12 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   if (ptys)
ptys->fixup_after_attach (!iscygwin ());
 
+  if (!iscygwin ())
+   {
+ init_console_handler (myself->ctty > 0);
+ myself->ctty = 0;
+   }
+
 loop:
   /* When ruid != euid we create the new process under the current original
 account and impersonate in child, this way maintaining the different
-- 
2.21.0



[PATCH v2 1/1] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Takashi Yano
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.
---
 winsup/cygwin/fhandler_tty.cc | 106 +++---
 winsup/cygwin/smallprint.cc   |   2 +
 winsup/cygwin/strace.cc   |  26 +
 winsup/cygwin/winsup.h|   3 +
 4 files changed, 66 insertions(+), 71 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 262c41bfe..fadff59a3 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -88,6 +88,19 @@ set_switch_to_pcon (void)
   }
 }
 
+void
+set_ishybrid_and_switch_to_pcon (HANDLE h)
+{
+  DWORD dummy;
+  if (!isHybrid
+  && GetFileType (h) == FILE_TYPE_CHAR
+  && GetConsoleMode (h, ))
+{
+  isHybrid = true;
+  set_switch_to_pcon ();
+}
+}
+
 #define DEF_HOOK(name) static __typeof__ (name) *name##_Orig
 DEF_HOOK (WriteFile);
 DEF_HOOK (WriteConsoleA);
@@ -100,6 +113,7 @@ DEF_HOOK (WriteConsoleOutputW);
 DEF_HOOK (WriteConsoleOutputCharacterA);
 DEF_HOOK (WriteConsoleOutputCharacterW);
 DEF_HOOK (WriteConsoleOutputAttribute);
+DEF_HOOK (SetConsoleTextAttribute);
 DEF_HOOK (WriteConsoleInputA);
 DEF_HOOK (WriteConsoleInputW);
 DEF_HOOK (ReadConsoleInputA);
@@ -107,140 +121,137 @@ DEF_HOOK (ReadConsoleInputW);
 DEF_HOOK (PeekConsoleInputA);
 DEF_HOOK (PeekConsoleInputW);
 
-#define CHK_CONSOLE_ACCESS(h) \
-{ \
-  DWORD dummy; \
-  if (!isHybrid \
-  && GetFileType (h) == FILE_TYPE_CHAR \
-  && GetConsoleMode (h, )) \
-{ \
-  isHybrid = true; \
-  set_switch_to_pcon (); \
-} \
-}
 static BOOL WINAPI
 WriteFile_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteFile_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleA_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleA_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleW_Hooked
  (HANDLE h, LPCVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleW_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadFile_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPOVERLAPPED o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadFile_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadConsoleA_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadConsoleA_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 ReadConsoleW_Hooked
  (HANDLE h, LPVOID p, DWORD l, LPDWORD n, LPVOID o)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return ReadConsoleW_Orig (h, p, l, n, o);
 }
 static BOOL WINAPI
 WriteConsoleOutputA_Hooked
  (HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputA_Orig (h, p, s, c, r);
 }
 static BOOL WINAPI
 WriteConsoleOutputW_Hooked
  (HANDLE h, CONST CHAR_INFO *p, COORD s, COORD c, PSMALL_RECT r)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputW_Orig (h, p, s, c, r);
 }
 static BOOL WINAPI
 WriteConsoleOutputCharacterA_Hooked
  (HANDLE h, LPCSTR p, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputCharacterA_Orig (h, p, l, c, n);
 }
 static BOOL WINAPI
 WriteConsoleOutputCharacterW_Hooked
  (HANDLE h, LPCWSTR p, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputCharacterW_Orig (h, p, l, c, n);
 }
 static BOOL WINAPI
 WriteConsoleOutputAttribute_Hooked
  (HANDLE h, CONST WORD *a, DWORD l, COORD c, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleOutputAttribute_Orig (h, a, l, c, n);
 }
 static BOOL WINAPI
+SetConsoleTextAttribute_Hooked
+ (HANDLE h, WORD a)
+{
+  set_ishybrid_and_switch_to_pcon (h);
+  return SetConsoleTextAttribute_Orig (h, a);
+}
+static BOOL WINAPI
 WriteConsoleInputA_Hooked
  (HANDLE h, CONST INPUT_RECORD *r, DWORD l, LPDWORD n)
 {
-  CHK_CONSOLE_ACCESS (h);
+  set_ishybrid_and_switch_to_pcon (h);
   return WriteConsoleInputA_Orig (h, r, l, n);
 }
 static BOOL WINAPI
 WriteConsoleInputW_Hooked
  (HANDLE h, CONST 

[PATCH v2 0/1] Cygwin: pty: Limit API hook to the program linked with the APIs.

2019-09-04 Thread Takashi Yano
- API hook used for pseudo console support causes slow down.
  This patch limits API hook to only program which is linked
  with the corresponding APIs. Normal cygwin program is not
  linked with such APIs (such as WriteFile, etc...) directly,
  therefore, no slow down occurs. However, console access by
  cygwin.dll itself cannot switch the r/w pipe to pseudo console
  side. Therefore, the code to switch it forcely to pseudo
  console side is added to smallprint.cc and strace.cc.

v2:
Unify set_ishybrid_and_switch_to_pcon() and CHK_CONSOLE_ACCESS()
because they have exactly the same functionality.

Takashi Yano (1):
  Cygwin: pty: Limit API hook to the program linked with the APIs.

 winsup/cygwin/fhandler_tty.cc | 106 +++---
 winsup/cygwin/smallprint.cc   |   2 +
 winsup/cygwin/strace.cc   |  26 +
 winsup/cygwin/winsup.h|   3 +
 4 files changed, 66 insertions(+), 71 deletions(-)

-- 
2.21.0



[PATCH v2 0/1] Cygwin: pty: Add a workaround for ^C handling.

2019-09-04 Thread Takashi Yano
- Pseudo console support introduced by commit
  169d65a5774acc76ce3f3feeedcbae7405aa9b57 sometimes cause random
  crash or freeze by pressing ^C while cygwin and non-cygwin
  processes are executed simultaneously in the same pty. This
  patch is a workaround for this issue.

v2:
Make the behaviour of pty and console identical.

Takashi Yano (1):
  Cygwin: pty: Add a workaround for ^C handling.

 winsup/cygwin/fork.cc  | 1 -
 winsup/cygwin/spawn.cc | 6 ++
 2 files changed, 6 insertions(+), 1 deletion(-)

-- 
2.21.0



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

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 12:47:38 +0200
Corinna Vinschen wrote:
> Why do you check the TERMs again here?  After all, need_clear_screen
> is only true if one of these terms are used.

Because, emacs seems to set environment TERM after fixup_after_exec()
is called. At the first check, TERM has the value of the terminal
in which emacs is executed. The first check is just in case.

-- 
Takashi Yano 


[PATCH 0/1] Cygwin: pty: Fix select() with pseudo console support.

2019-09-04 Thread Takashi Yano
- select() did not work correctly when both read and except are
  polled simultaneously for the same fd and the r/w pipe is switched
  to pseudo console side. This patch fixes this isseu.

Takashi Yano (1):
  Cygwin: pty: Fix select() with pseudo console support.

 winsup/cygwin/fhandler.h  |  15 +++
 winsup/cygwin/fhandler_tty.cc |  13 ++-
 winsup/cygwin/select.cc   | 192 --
 winsup/cygwin/select.h|   2 +
 4 files changed, 207 insertions(+), 15 deletions(-)

-- 
2.21.0



[PATCH 1/1] Cygwin: pty: Fix select() with pseudo console support.

2019-09-04 Thread Takashi Yano
- select() did not work correctly when both read and except are
  polled simultaneously for the same fd and the r/w pipe is switched
  to pseudo console side. This patch fixes this isseu.
---
 winsup/cygwin/fhandler.h  |  15 +++
 winsup/cygwin/fhandler_tty.cc |  13 ++-
 winsup/cygwin/select.cc   | 192 --
 winsup/cygwin/select.h|   2 +
 4 files changed, 207 insertions(+), 15 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index e8c165100..e72e11f7a 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2102,6 +2102,7 @@ class fhandler_pty_common: public fhandler_termios
   {
 return get_ttyp ()->hPseudoConsole;
   }
+  bool to_be_read_from_pcon (void);
 
  protected:
   BOOL process_opost_output (HANDLE h,
@@ -2150,6 +2151,8 @@ class fhandler_pty_slave: public fhandler_pty_common
   void fixup_after_exec ();
 
   select_record *select_read (select_stuff *);
+  select_record *select_write (select_stuff *);
+  select_record *select_except (select_stuff *);
   virtual char const *ttyname () { return pc.dev.name (); }
   int __reg2 fstat (struct stat *buf);
   int __reg3 facl (int, int, struct acl *);
@@ -2177,9 +2180,21 @@ class fhandler_pty_slave: public fhandler_pty_common
   void push_to_pcon_screenbuffer (const char *ptr, size_t len);
   void mask_switch_to_pcon (bool mask)
   {
+if (!mask && get_ttyp ()->pcon_pid &&
+   get_ttyp ()->pcon_pid != myself->pid &&
+   kill (get_ttyp ()->pcon_pid, 0) == 0)
+  return;
 get_ttyp ()->mask_switch_to_pcon = mask;
   }
   void fixup_after_attach (bool native_maybe);
+  pid_t get_pcon_pid (void)
+  {
+return get_ttyp ()->pcon_pid;
+  }
+  bool is_line_input (void)
+  {
+return get_ttyp ()->ti.c_lflag & ICANON;
+  }
 };
 
 #define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit))
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index a6844832b..78c9c9128 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1223,6 +1223,13 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
   return towrite;
 }
 
+bool
+fhandler_pty_common::to_be_read_from_pcon (void)
+{
+  return get_ttyp ()->switch_to_pcon &&
+(!get_ttyp ()->mask_switch_to_pcon || ALWAYS_USE_PCON);
+}
+
 void __reg3
 fhandler_pty_slave::read (void *ptr, size_t& len)
 {
@@ -1351,8 +1358,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
}
  goto out;
}
-  if (get_ttyp ()->switch_to_pcon &&
- (!get_ttyp ()->mask_switch_to_pcon || ALWAYS_USE_PCON))
+  if (to_be_read_from_pcon ())
{
  if (!try_reattach_pcon ())
{
@@ -2129,8 +2135,7 @@ fhandler_pty_master::write (const void *ptr, size_t len)
 
   /* Write terminal input to to_slave pipe instead of output_handle
  if current application is native console application. */
-  if (get_ttyp ()->switch_to_pcon &&
-  (!get_ttyp ()->mask_switch_to_pcon || ALWAYS_USE_PCON))
+  if (to_be_read_from_pcon ())
 {
   char *buf;
   size_t nlen;
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index d29f3d2f4..4efc302df 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -667,9 +667,6 @@ peek_pipe (select_record *s, bool from_select)
fhm->flush_to_slave ();
  }
  break;
-   case DEV_PTYS_MAJOR:
- ((fhandler_pty_slave *) fh)->reset_switch_to_pcon ();
- break;
default:
  if (fh->get_readahead_valid ())
{
@@ -713,6 +710,7 @@ peek_pipe (select_record *s, bool from_select)
 }
 
 out:
+  h = fh->get_output_handle_cyg ();
   if (s->write_selected && dev != FH_PIPER)
 {
   gotone += s->write_ready =  pipe_data_available (s->fd, fh, h, true);
@@ -1176,33 +1174,173 @@ static int
 verify_tty_slave (select_record *me, fd_set *readfds, fd_set *writefds,
   fd_set *exceptfds)
 {
-  if (IsEventSignalled (me->h))
+  fhandler_pty_slave *ptys = (fhandler_pty_slave *) me->fh;
+  if (me->read_selected && !ptys->to_be_read_from_pcon () &&
+  IsEventSignalled (ptys->input_available_event))
 me->read_ready = true;
   return set_bits (me, readfds, writefds, exceptfds);
 }
 
 static int
-pty_slave_startup (select_record *s, select_stuff *)
+peek_pty_slave (select_record *s, bool from_select)
 {
+  int gotone = 0;
   fhandler_base *fh = (fhandler_base *) s->fh;
-  ((fhandler_pty_slave *) fh)->mask_switch_to_pcon (true);
+  fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
+
+  ptys->reset_switch_to_pcon ();
+
+  if (s->read_selected)
+{
+  if (s->read_ready)
+   {
+ select_printf ("%s, already ready for read", fh->get_name ());
+ gotone = 1;
+ goto out;
+   }
+
+  if (fh->bg_check (SIGTTIN, true) <= bg_eof)
+   {
+ gotone = s->read_ready = true;
+ goto out;
+   }
+
+  if (ptys->to_be_read_from_pcon ())

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

2019-09-04 Thread Takashi Yano
- 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 | 19 ++-
 winsup/cygwin/tty.cc  |  1 +
 winsup/cygwin/tty.h   |  1 +
 3 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index fadff59a3..a6844832b 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -962,6 +962,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, , NULL);
+   }
+  get_ttyp ()->need_clear_screen = false;
+}
+
   if (ALWAYS_USE_PCON)
 return;
   if (isHybrid)
@@ -2798,14 +2811,10 @@ 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)
/* Assume this is the first process using this pty slave. */
-   WriteFile (get_output_handle_cyg (),
-  "\033[H\033[J", 6, , 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



[PATCH v2 0/1] Disable clear screen on new pty if TERM=dumb or emacs*.

2019-09-04 Thread Takashi Yano
- 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.

v2:
Remove check for TERM in fixup_after_attach().

Takashi Yano (1):
  Cygwin: pty: Disable clear screen on new pty if TERM=dumb or emacs*.

 winsup/cygwin/fhandler_tty.cc | 19 ++-
 winsup/cygwin/tty.cc  |  1 +
 winsup/cygwin/tty.h   |  1 +
 3 files changed, 16 insertions(+), 5 deletions(-)

-- 
2.21.0



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

2019-09-04 Thread Takashi Yano
On Wed, 4 Sep 2019 11:22:42 -0600
Brian Inglis wrote:
> That output seems to be generated by a shell or program running in the pty.
> If the recipient can not handle escape sequences, then the shell or program in
> the pty should be configured by setting e.g. TERM=dumb when launching the
> terminal, or in the shell environment.

No. Output is almost same even if setting TERM=dumb.
See attached log.

Consider what should be the output of pseudo console if console
application like below is executed.

#include 

int main()
{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD p = {20, 10};
DWORD n;
SetConsoleCursorPosition(h, p);
SetConsoleTextAttribute(h, FOREGROUND_RED);
WriteConsole(h, "R", 1, , 0);
SetConsoleTextAttribute(h, FOREGROUND_GREEN);
WriteConsole(h, "G", 1, , 0);
SetConsoleTextAttribute(h, FOREGROUND_BLUE);
WriteConsole(h, "B", 1, , 0);
SetConsoleTextAttribute(h, FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE);
WriteConsole(h, "W", 1, , 0);
WriteConsole(h, "\r\n", 2, , 0);
return 0;
}

Setting cursor posision and text color cannot be realized
without ANSI escape sequences.

Indeed, the output of the pseudo console by the program above is:
^[[?25l^[[11;21H^[[?25h^[[?25l^[[31mR^[[32mG^[[34mB^[[mW^M
^[[?25h

-- 
Takashi Yano 


pcon-output-dumb.log
Description: Binary data


[PATCH 1/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-06 Thread Takashi Yano
- When the I/O pipe is switched to the pseudo console side, the
  behaviour of Ctrl-C is unstable. This rarely happens, however,
  for example, shell sometimes crashes by Ctrl-C in that situation.
  This patch fixes that issue.
---
 winsup/cygwin/fhandler.h  |   4 +-
 winsup/cygwin/fhandler_tty.cc |  32 +
 winsup/cygwin/select.cc   |   2 +-
 winsup/cygwin/spawn.cc| 128 +-
 4 files changed, 88 insertions(+), 78 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index e72e11f7a..1e3cada08 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2187,9 +2187,9 @@ class fhandler_pty_slave: public fhandler_pty_common
 get_ttyp ()->mask_switch_to_pcon = mask;
   }
   void fixup_after_attach (bool native_maybe);
-  pid_t get_pcon_pid (void)
+  pid_t getpgid (void)
   {
-return get_ttyp ()->pcon_pid;
+return get_ttyp ()->getpgid ();
   }
   bool is_line_input (void)
   {
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 2533e5618..150530c5e 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1018,20 +1018,25 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
   get_ttyp ()->need_clear_screen = false;
 }
 
-  if (ALWAYS_USE_PCON)
-return;
   if (isHybrid)
-{
-  this->set_switch_to_pcon ();
-  return;
-}
+this->set_switch_to_pcon ();
   if (get_ttyp ()->pcon_pid &&
   get_ttyp ()->pcon_pid != myself->pid &&
   kill (get_ttyp ()->pcon_pid, 0) == 0)
 /* There is a process which is grabbing pseudo console. */
 return;
-  if (get_ttyp ()->switch_to_pcon &&
-  get_ttyp ()->pcon_pid != myself->pid)
+  if (isHybrid)
+{
+  DWORD mode;
+  GetConsoleMode (get_handle (), );
+  if (ALWAYS_USE_PCON)
+   mode &= ~ENABLE_PROCESSED_INPUT;
+  SetConsoleMode (get_handle (), mode & ~ENABLE_ECHO_INPUT);
+  get_ttyp ()->pcon_pid = 0;
+  init_console_handler (true);
+  return;
+}
+  if (get_ttyp ()->switch_to_pcon)
 {
   DWORD mode;
   GetConsoleMode (get_handle (), );
@@ -1040,6 +1045,7 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
 }
   get_ttyp ()->pcon_pid = 0;
   get_ttyp ()->switch_to_pcon = false;
+  init_console_handler (true);
 }
 
 void
@@ -1299,8 +1305,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
   if (ptr) /* Indicating not tcflush(). */
 {
   reset_switch_to_pcon ();
-  if (get_ttyp ()->pcon_pid != myself->pid)
-   mask_switch_to_pcon (true);
+  mask_switch_to_pcon (true);
 }
 
   if (is_nonblocking () || !ptr) /* Indicating tcflush(). */
@@ -1420,7 +1425,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
flags &= ~ENABLE_ECHO_INPUT;
  if ((get_ttyp ()->ti.c_lflag & ISIG) &&
  !(get_ttyp ()->ti.c_iflag & IGNBRK))
-   flags |= ENABLE_PROCESSED_INPUT;
+   flags |= ALWAYS_USE_PCON ? 0 : ENABLE_PROCESSED_INPUT;
  if (dwMode != flags)
SetConsoleMode (get_handle (), flags);
  /* Read get_handle() instad of get_handle_cyg() */
@@ -2867,8 +2872,10 @@ fhandler_pty_slave::fixup_after_attach (bool 
native_maybe)
  get_ttyp ()->num_pcon_attached_slaves ++;
}
}
+  if (ALWAYS_USE_PCON && pcon_attached_to == get_minor ())
+   set_ishybrid_and_switch_to_pcon (get_output_handle ());
 }
-  if (pcon_attached_to == get_minor () && (native_maybe || ALWAYS_USE_PCON))
+  if (pcon_attached_to == get_minor () && native_maybe)
 {
   FlushConsoleInputBuffer (get_handle ());
   DWORD mode;
@@ -2883,6 +2890,7 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
  kill (get_ttyp ()->pcon_pid, 0) != 0)
get_ttyp ()->pcon_pid = myself->pid;
   get_ttyp ()->switch_to_pcon = true;
+  init_console_handler(false);
 }
 }
 
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 4efc302df..3589ccabf 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1294,7 +1294,7 @@ pty_slave_startup (select_record *me, select_stuff *stuff)
 {
   fhandler_base *fh = (fhandler_base *) me->fh;
   fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
-  if (me->read_selected && ptys->get_pcon_pid () != myself->pid)
+  if (me->read_selected)
 ptys->mask_switch_to_pcon (true);
 
   select_pipe_info *pi = stuff->device_specific_ptys;
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 15cba3610..2cd43f681 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -262,6 +262,67 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   DWORD pidRestore = 0;
   bool attach_to_pcon = false;
 
+  /* Attach to pseudo console if pty salve is used */
+  pidRestore = fhandler_console::get_console_process_id
+(GetCurrentProcessId (), false);
+  fhandler_pty_slave *ptys = NULL;
+  int chk_order[] = {1, 0, 2};
+  for (int i 

[PATCH 0/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-06 Thread Takashi Yano
- When the I/O pipe is switched to the pseudo console side, the
  behaviour of Ctrl-C is unstable. This rarely happens, however,
  for example, shell sometimes crashes by Ctrl-C in that situation.
  This patch fixes that issue.

Takashi Yano (1):
  Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

 winsup/cygwin/fhandler.h  |   4 +-
 winsup/cygwin/fhandler_tty.cc |  32 +
 winsup/cygwin/select.cc   |   2 +-
 winsup/cygwin/spawn.cc| 128 +-
 4 files changed, 88 insertions(+), 78 deletions(-)

-- 
2.21.0



[PATCH 1/1] Cygwin: pty: Make SetConsoleCursorPosition() to be hooked.

2019-09-06 Thread Takashi Yano
- Win32 API SetConsoleCursorPosition() injects ANSI escape sequence
  to pseudo console. Therefore, it should be added to the API list
  to be hooked.
---
 winsup/cygwin/fhandler_tty.cc | 9 +
 1 file changed, 9 insertions(+)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 150530c5e..de1c1ae6f 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -156,6 +156,7 @@ DEF_HOOK (WriteConsoleOutputW);
 DEF_HOOK (WriteConsoleOutputCharacterA);
 DEF_HOOK (WriteConsoleOutputCharacterW);
 DEF_HOOK (WriteConsoleOutputAttribute);
+DEF_HOOK (SetConsoleCursorPosition);
 DEF_HOOK (SetConsoleTextAttribute);
 DEF_HOOK (WriteConsoleInputA);
 DEF_HOOK (WriteConsoleInputW);
@@ -242,6 +243,13 @@ WriteConsoleOutputAttribute_Hooked
   return WriteConsoleOutputAttribute_Orig (h, a, l, c, n);
 }
 static BOOL WINAPI
+SetConsoleCursorPosition_Hooked
+ (HANDLE h, COORD c)
+{
+  set_ishybrid_and_switch_to_pcon (h);
+  return SetConsoleCursorPosition_Orig (h, c);
+}
+static BOOL WINAPI
 SetConsoleTextAttribute_Hooked
  (HANDLE h, WORD a)
 {
@@ -2960,6 +2968,7 @@ fhandler_pty_slave::fixup_after_exec ()
   DO_HOOK (NULL, WriteConsoleOutputCharacterA);
   DO_HOOK (NULL, WriteConsoleOutputCharacterW);
   DO_HOOK (NULL, WriteConsoleOutputAttribute);
+  DO_HOOK (NULL, SetConsoleCursorPosition);
   DO_HOOK (NULL, SetConsoleTextAttribute);
   DO_HOOK (NULL, WriteConsoleInputA);
   DO_HOOK (NULL, WriteConsoleInputW);
-- 
2.21.0



[PATCH 0/1] Cygwin: pty: Make SetConsoleCursorPosition() to be hooked.

2019-09-06 Thread Takashi Yano
- Win32 API SetConsoleCursorPosition() injects ANSI escape sequence
  to pseudo console. Therefore, it should be added to the API list
  to be hooked.

Takashi Yano (1):
  Cygwin: pty: Make SetConsoleCursorPosition() to be hooked.

 winsup/cygwin/fhandler_tty.cc | 9 +
 1 file changed, 9 insertions(+)

-- 
2.21.0



[PATCH 0/1] Cygwin: pty: Prevent the helper process from exiting by Ctrl-C.

2019-09-09 Thread Takashi Yano
The helper process cygwin-console-helper.exe exsited by Ctrl-C
in some situation. This patch fixes this issue.

Takashi Yano (1):
  Cygwin: pty: Prevent the helper process from exiting by Ctrl-C.

 winsup/utils/cygwin-console-helper.cc | 1 +
 1 file changed, 1 insertion(+)

-- 
2.21.0



[PATCH 1/1] Cygwin: pty: Prevent the helper process from exiting by Ctrl-C.

2019-09-09 Thread Takashi Yano
---
 winsup/utils/cygwin-console-helper.cc | 1 +
 1 file changed, 1 insertion(+)

diff --git a/winsup/utils/cygwin-console-helper.cc 
b/winsup/utils/cygwin-console-helper.cc
index ad451ecf5..66004bd15 100644
--- a/winsup/utils/cygwin-console-helper.cc
+++ b/winsup/utils/cygwin-console-helper.cc
@@ -10,6 +10,7 @@ main (int argc, char **argv)
   SetEvent (h);
   if (argc == 4) /* Pseudo console helper mode for PTY */
 {
+  SetConsoleCtrlHandler (NULL, TRUE);
   HANDLE hPipe = (HANDLE) strtoull (argv[3], , 0);
   char buf[64];
   sprintf (buf, "StdHandles=%p,%p\n",
-- 
2.21.0



[PATCH 0/3] Some fixes for PTY with pseudo console support (3)

2019-09-14 Thread Takashi Yano
[PATCH 1/3] Fix bad file descriptor error in some environment.
The bad file descriptor problem reported in:
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html
was recurring. Fixed again.

[PATCH 2/3] Use system NLS function instead of PTY's own one.
Since calling system __loadlocale() caused execution error,
PTY used its own NLS function. The cause of the error has been
found, the corresponding code has been rewritten using system
function.

[PATCH 3/3] Change the timing of clearing screen.
The code which clears screen is moved from reset_switch_to_pcon()
to fixup_after_exec() because it seems not too early even at this
timing.

Takashi Yano (3):
  Cygwin: pty: Fix bad file descriptor error in some environment.
  Cygwin: pty: Use system NLS function instead of PTY's own one.
  Cygwin: pty: Change the timing of clearing screen.

 winsup/cygwin/fhandler.h  |   1 +
 winsup/cygwin/fhandler_tty.cc | 527 --
 winsup/cygwin/tty.cc  |   2 +-
 winsup/cygwin/tty.h   |   2 +-
 4 files changed, 120 insertions(+), 412 deletions(-)

-- 
2.21.0



[PATCH 2/3] Cygwin: pty: Use system NLS function instead of PTY's own one.

2019-09-14 Thread Takashi Yano
- Since calling system __loadlocale() caused execution error,
  PTY used its own NLS function. The cause of the error has been
  found, the corresponding code has been rewritten using system
  function.
---
 winsup/cygwin/fhandler.h  |   1 +
 winsup/cygwin/fhandler_tty.cc | 499 +++---
 winsup/cygwin/tty.cc  |   2 +-
 winsup/cygwin/tty.h   |   2 +-
 4 files changed, 107 insertions(+), 397 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 1bf5dfb09..4efb6a4f2 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2192,6 +2192,7 @@ class fhandler_pty_slave: public fhandler_pty_common
   {
 return get_ttyp ()->ti.c_lflag & ICANON;
   }
+  void setup_locale (void);
 };
 
 #define __ptsname(buf, unit) __small_sprintf ((buf), "/dev/pty%d", (unit))
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 1b1d54447..3bf8d0b75 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -29,11 +29,6 @@ details. */
 
 #define ALWAYS_USE_PCON false
 #define USE_API_HOOK true
-#define USE_OWN_NLS_FUNC true
-
-#if !USE_OWN_NLS_FUNC
-#include "langinfo.h"
-#endif
 
 /* Not yet defined in Mingw-w64 */
 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
@@ -1129,7 +1124,7 @@ fhandler_pty_slave::push_to_pcon_screenbuffer (const char 
*ptr, size_t len)
   size_t nlen;
   DWORD origCP;
   origCP = GetConsoleOutputCP ();
-  SetConsoleOutputCP (get_ttyp ()->TermCodePage);
+  SetConsoleOutputCP (get_ttyp ()->term_code_page);
   /* Just copy */
   buf = (char *) HeapAlloc (GetProcessHeap (), 0, len);
   memcpy (buf, (char *)ptr, len);
@@ -1246,16 +1241,16 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
   char *buf;
   ssize_t nlen;
   UINT targetCodePage = get_ttyp ()->switch_to_pcon_out ?
-GetConsoleOutputCP () : get_ttyp ()->TermCodePage;
-  if (targetCodePage != get_ttyp ()->TermCodePage)
+GetConsoleOutputCP () : get_ttyp ()->term_code_page;
+  if (targetCodePage != get_ttyp ()->term_code_page)
 {
   size_t wlen =
-   MultiByteToWideChar (get_ttyp ()->TermCodePage, 0,
+   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
 (char *)ptr, len, NULL, 0);
   wchar_t *wbuf = (wchar_t *)
HeapAlloc (GetProcessHeap (), 0, wlen * sizeof (wchar_t));
   wlen =
-   MultiByteToWideChar (get_ttyp ()->TermCodePage, 0,
+   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
 (char *)ptr, len, wbuf, wlen);
   nlen = WideCharToMultiByte (targetCodePage, 0,
  wbuf, wlen, NULL, 0, NULL, NULL);
@@ -2242,15 +2237,15 @@ fhandler_pty_master::write (const void *ptr, size_t len)
   char *buf;
   size_t nlen;
 
-  if (get_ttyp ()->TermCodePage != CP_UTF8)
+  if (get_ttyp ()->term_code_page != CP_UTF8)
{
  size_t wlen =
-   MultiByteToWideChar (get_ttyp ()->TermCodePage, 0,
+   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
 (char *)ptr, len, NULL, 0);
  wchar_t *wbuf = (wchar_t *)
HeapAlloc (GetProcessHeap (), 0, wlen * sizeof (wchar_t));
  wlen =
-   MultiByteToWideChar (get_ttyp ()->TermCodePage, 0,
+   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
 (char *)ptr, len, wbuf, wlen);
  nlen = WideCharToMultiByte (CP_UTF8, 0,
  wbuf, wlen, NULL, 0, NULL, NULL);
@@ -2502,7 +2497,6 @@ get_locale_from_env (char *locale)
   strcpy (locale, env);
 }
 
-#if USE_OWN_NLS_FUNC
 static LCID
 get_langinfo (char *locale_out, char *charset_out)
 {
@@ -2510,318 +2504,52 @@ get_langinfo (char *locale_out, char *charset_out)
   char new_locale[ENCODING_LEN + 1];
   get_locale_from_env (new_locale);
 
-  /* The following code is borrowed from __loadlocale() in
- newlib/libc/locale/locale.c */
-
-  /* At this point a full-featured system would just load the locale
- specific data from the locale files.
- What we do here for now is to check the incoming string for correctness.
- The string must be in one of the allowed locale strings, either
- one in POSIX-style, or one in the old newlib style to maintain
- backward compatibility.  If the local string is correct, the charset
- is extracted and stored in ctype_codeset or message_charset
- dependent on the cateogry. */
-  char *locale = NULL;
-  char charset[ENCODING_LEN + 1];
-  long val = 0;
-  char *end, *c = NULL;
-
-  /* This additional code handles the case that the incoming locale string
- is not valid.  If so, it calls the function 
__set_locale_from_locale_alias,
- which is only available on Cygwin right now.  The function reads the
- file /usr/share/locale/locale.alias.  The file contains locale aliases
- and their replacement locale.  For instance, the alias "french" 

[PATCH 1/3] Cygwin: pty: Fix bad file descriptor error in some environment.

2019-09-14 Thread Takashi Yano
- The bad file descriptor problem reported in:
  https://cygwin.com/ml/cygwin-patches/2019-q3/msg00104.html
  was recurring. Fixed again.
---
 winsup/cygwin/fhandler_tty.cc | 2 --
 1 file changed, 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 9aa832641..1b1d54447 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -857,8 +857,6 @@ fhandler_pty_slave::open (int flags, mode_t)
   pcon_attached_to = get_minor ();
   init_console_handler (true);
 }
-  else if (pcon_attached_to < 0)
-fhandler_console::need_invisible ();
 
   set_open_status ();
   return 1;
-- 
2.21.0



[PATCH 3/3] Cygwin: pty: Change the timing of clearing screen.

2019-09-14 Thread Takashi Yano
- The code which clears screen is moved from reset_switch_to_pcon()
  to fixup_after_exec() because it seems not too early even at this
  timing.
---
 winsup/cygwin/fhandler_tty.cc | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 3bf8d0b75..5c27510be 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1041,19 +1041,6 @@ 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, , NULL);
-   }
-  get_ttyp ()->need_clear_screen = false;
-}
-
   if (isHybrid)
 this->set_switch_to_pcon (fd);
   if (get_ttyp ()->pcon_pid &&
@@ -2742,6 +2729,19 @@ fhandler_pty_slave::fixup_after_exec ()
}
 }
 
+  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, , NULL);
+   }
+  get_ttyp ()->need_clear_screen = false;
+}
+
   /* Set locale */
   setup_locale ();
 
-- 
2.21.0



Re: [PATCH v3 0/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-14 Thread Takashi Yano
On Sat, 14 Sep 2019 15:29:50 +
Ken Brown wrote:
> All pushed.  Thanks.

Thanks.

> Do you think there have been enough changes that I should issue another test 
> release, or do you have more patches in the works?

I have submitted three more patches just now. Hopefully, the functional
fixes have been settled with these patches, so I think it is ready to
issue a new test release.

-- 
Takashi Yano 


[PATCH] Cygwin: pty: Correct typos that do not fit the coding style.

2019-09-14 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 5c27510be..5072c6243 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -45,7 +45,7 @@ details. */
 #endif /* ENABLE_VIRTUAL_TERMINAL_INPUT */
 
 extern "C" int sscanf (const char *, const char *, ...);
-extern "C" int ttyname_r(int, char*, size_t);
+extern "C" int ttyname_r (int, char*, size_t);
 
 #define close_maybe(h) \
   do { \
@@ -2147,7 +2147,7 @@ fhandler_pty_master::close ()
   else if (obi.HandleCount == (getPseudoConsole () ? 2 : 1))
  /* Helper process has inherited one. */
 {
-  termios_printf("Closing last master of pty%d", get_minor ());
+  termios_printf ("Closing last master of pty%d", get_minor ());
   /* Close Pseudo Console */
   if (getPseudoConsole ())
{
@@ -2446,9 +2446,9 @@ get_locale_from_env (char *locale)
   char lang[ENCODING_LEN + 1] = {0, }, country[ENCODING_LEN + 1] = {0, };
   env = getenv ("LC_ALL");
   if (env == NULL || !*env)
-env = getenv("LC_CTYPE");
+env = getenv ("LC_CTYPE");
   if (env == NULL || !*env)
-env = getenv("LANG");
+env = getenv ("LANG");
   if (env == NULL || !*env)
 {
   if (GetLocaleInfo (LOCALE_CUSTOM_UI_DEFAULT,
@@ -2476,7 +2476,7 @@ get_locale_from_env (char *locale)
 LOCALE_SISO3166CTRYNAME,
 country, sizeof (country));
   if (strlen (lang) && strlen (country))
-   __small_sprintf (lang + strlen(lang), "_%s.UTF-8", country);
+   __small_sprintf (lang + strlen (lang), "_%s.UTF-8", country);
   else
strcpy (lang , "C.UTF-8");
   env = lang;
@@ -2492,7 +2492,7 @@ get_langinfo (char *locale_out, char *charset_out)
   get_locale_from_env (new_locale);
 
   __locale_t loc;
-  memset(, 0, sizeof (loc));
+  memset (, 0, sizeof (loc));
   const char *locale = __loadlocale (, LC_CTYPE, new_locale);
   if (!locale)
 locale = "C";
@@ -2565,8 +2565,8 @@ get_langinfo (char *locale_out, char *charset_out)
 return 0;
 
   /* Set results */
-  strcpy(locale_out, new_locale);
-  strcpy(charset_out, charset);
+  strcpy (locale_out, new_locale);
+  strcpy (charset_out, charset);
   return lcid;
 }
 
@@ -2670,7 +2670,7 @@ fhandler_pty_slave::fixup_after_attach (bool 
native_maybe, int fd_set)
get_ttyp ()->pcon_pid = myself->pid;
  get_ttyp ()->switch_to_pcon_out = true;
}
- init_console_handler(false);
+ init_console_handler (false);
}
   else if (fd == 0 && native_maybe)
/* Read from unattached pseudo console cause freeze,
@@ -2754,7 +2754,7 @@ fhandler_pty_slave::fixup_after_exec ()
{ \
  void *api = hook_api (module, #name, (void *) name##_Hooked); \
  name##_Orig = (__typeof__ (name) *) api; \
- /*if (api) system_printf(#name " hooked.");*/ \
+ /*if (api) system_printf (#name " hooked.");*/ \
}
   DO_HOOK (NULL, WriteFile);
   DO_HOOK (NULL, WriteConsoleA);
@@ -3118,7 +3118,7 @@ fhandler_pty_master::setup_pseudoconsole ()
   if (res != S_OK)
 {
   system_printf ("CreatePseudoConsole() failed. %08x\n",
-GetLastError());
+GetLastError ());
   CloseHandle (from_master);
   CloseHandle (to_slave);
   from_master = from_master_cyg;
@@ -3230,7 +3230,7 @@ fhandler_pty_master::setup ()
 termios_printf ("can't set output_handle(%p) to non-blocking mode",
get_output_handle ());
 
-  char pipename[sizeof("pty-to-master-cyg")];
+  char pipename[sizeof ("pty-to-master-cyg")];
   __small_sprintf (pipename, "pty%d-to-master", unit);
   res = fhandler_pipe::create (_none, _slave, _master,
   fhandler_pty_common::pipesize, pipename, 0);
@@ -3406,7 +3406,7 @@ fhandler_pty_common::process_opost_output (HANDLE h, 
const void *ptr, ssize_t& l
break;
  else
{
- set_errno(EAGAIN);
+ set_errno (EAGAIN);
  len = -1;
  return TRUE;
}
-- 
2.21.0



[PATCH] Cygwin: pty: Use autoload feature for pseudo console system calls.

2019-09-15 Thread Takashi Yano
- The autoload feature is used rather than GetModuleHandle(),
  GetProcAddress() for CreatePseudoConsole(), ResizePseudoConsole()
  and ClosePseudoConsole().
---
 winsup/cygwin/autoload.cc |  3 +++
 winsup/cygwin/fhandler_tty.cc | 36 +--
 2 files changed, 16 insertions(+), 23 deletions(-)

diff --git a/winsup/cygwin/autoload.cc b/winsup/cygwin/autoload.cc
index c4d91611e..1851ab3b6 100644
--- a/winsup/cygwin/autoload.cc
+++ b/winsup/cygwin/autoload.cc
@@ -759,4 +759,7 @@ LoadDLLfunc (PdhAddEnglishCounterW, 16, pdh)
 LoadDLLfunc (PdhCollectQueryData, 4, pdh)
 LoadDLLfunc (PdhGetFormattedCounterValue, 16, pdh)
 LoadDLLfunc (PdhOpenQueryW, 12, pdh)
+LoadDLLfuncEx (CreatePseudoConsole, 20, kernel32, 1)
+LoadDLLfuncEx (ResizePseudoConsole, 8, kernel32, 1)
+LoadDLLfuncEx (ClosePseudoConsole, 4, kernel32, 1)
 }
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 5072c6243..659e7b595 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -47,6 +47,12 @@ details. */
 extern "C" int sscanf (const char *, const char *, ...);
 extern "C" int ttyname_r (int, char*, size_t);
 
+extern "C" {
+  HRESULT WINAPI CreatePseudoConsole (COORD, HANDLE, HANDLE, DWORD, HPCON *);
+  HRESULT WINAPI ResizePseudoConsole (HPCON, COORD);
+  VOID WINAPI ClosePseudoConsole (HPCON);
+}
+
 #define close_maybe(h) \
   do { \
 if (h && h != INVALID_HANDLE_VALUE) \
@@ -2157,14 +2163,8 @@ fhandler_pty_master::close ()
  /* FIXME: Pseudo console can be accessed via its handle
 only in the process which created it. What else can we do? */
  if (master_pid_tmp == myself->pid)
-   {
- /* Release pseudo console */
- HMODULE hModule = GetModuleHandle ("kernel32.dll");
- FARPROC func = GetProcAddress (hModule, "ClosePseudoConsole");
- VOID (WINAPI *ClosePseudoConsole) (HPCON) = NULL;
- ClosePseudoConsole = (VOID (WINAPI *) (HPCON)) func;
- ClosePseudoConsole (getPseudoConsole ());
-   }
+   /* Release pseudo console */
+   ClosePseudoConsole (getPseudoConsole ());
  get_ttyp ()->switch_to_pcon_in = false;
  get_ttyp ()->switch_to_pcon_out = false;
}
@@ -2348,10 +2348,6 @@ fhandler_pty_master::ioctl (unsigned int cmd, void *arg)
 only in the process which created it. What else can we do? */
   if (getPseudoConsole () && get_ttyp ()->master_pid == myself->pid)
{
- HMODULE hModule = GetModuleHandle ("kernel32.dll");
- FARPROC func = GetProcAddress (hModule, "ResizePseudoConsole");
- HRESULT (WINAPI *ResizePseudoConsole) (HPCON, COORD) = NULL;
- ResizePseudoConsole = (HRESULT (WINAPI *) (HPCON, COORD)) func;
  COORD size;
  size.X = ((struct winsize *) arg)->ws_col;
  size.Y = ((struct winsize *) arg)->ws_row;
@@ -3103,22 +3099,16 @@ fhandler_pty_master::setup_pseudoconsole ()
  process in a pseudo console and get them from the helper.
  Slave process will attach to the pseudo console in the
  helper process using AttachConsole(). */
-  HMODULE hModule = GetModuleHandle ("kernel32.dll");
-  FARPROC func = GetProcAddress (hModule, "CreatePseudoConsole");
-  HRESULT (WINAPI *CreatePseudoConsole)
-(COORD, HANDLE, HANDLE, DWORD, HPCON *) = NULL;
-  if (!func)
-return false;
-  CreatePseudoConsole =
-(HRESULT (WINAPI *) (COORD, HANDLE, HANDLE, DWORD, HPCON *)) func;
   COORD size = {80, 25};
   CreatePipe (_master, _slave, _none, 0);
+  SetLastError (ERROR_SUCCESS);
   HRESULT res = CreatePseudoConsole (size, from_master, to_master,
 0, _ttyp ()->hPseudoConsole);
-  if (res != S_OK)
+  if (res != S_OK || GetLastError () == ERROR_PROC_NOT_FOUND)
 {
-  system_printf ("CreatePseudoConsole() failed. %08x\n",
-GetLastError ());
+  if (res != S_OK)
+   system_printf ("CreatePseudoConsole() failed. %08x\n",
+  GetLastError ());
   CloseHandle (from_master);
   CloseHandle (to_slave);
   from_master = from_master_cyg;
-- 
2.21.0



Re: [PATCH v3 0/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-06 Thread Takashi Yano
Hi Ken,

I appreciate checking the patch.

On Fri, 6 Sep 2019 17:59:02 +
Ken Brown wrote:
> I had several problems after applying this patch.
> 
> 1. I noticed some display glitches when building cygwin (with -j13 if that's 
> relevant).  For example, there were some unexpected blank lines and indented 
> lines.
> 
> 2. At one point the build wouldn't complete at all.  It hung and had to be 
> killed with Ctrl-C.

I could not reproduce these with 32-bit cygwin (which I usually use),
however, I noticed that they happen with 64-bit cygwin. I apologize
for the lack of enough test.

I will post fixed version as v4 patch.

> 3. I used ssh from my normal account to log into an administrator account.  I 
> ran a script that produced a lot of output and piped it to less.  I pressed 
> 'q' 
> after the first screen was displayed, and the displayed text didn't get 
> cleared.

Are you using non-cygwin program in the script? If so, this may happen
in test release 3.1.0-0.4 as well.

> P.S. I'm leaving tomorrow for a short vacation, so I might not have time to 
> review any more patches until I return in about a week.

I see. So I have at least a week for consideration. :)

-- 
Takashi Yano 


[PATCH v4 0/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-06 Thread Takashi Yano
- When the I/O pipe is switched to the pseudo console side, the
  behaviour of Ctrl-C is unstable. This rarely happens, however,
  for example, shell sometimes crashes by Ctrl-C in that situation.
  This patch fixes that issue.

v4:
Fix the problem 1 and 2 reported in
https://cygwin.com/ml/cygwin-patches/2019-q3/msg00175.html

v3:
Fix mistake in v2.

v2:
Remove the code which accidentally clears ENABLE_ECHO_INPUT flag.

Takashi Yano (1):
  Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

 winsup/cygwin/fhandler.h  |  4 
 winsup/cygwin/fhandler_tty.cc | 33 +--
 winsup/cygwin/select.cc   |  2 +-
 winsup/cygwin/spawn.cc| 42 ++-
 4 files changed, 39 insertions(+), 42 deletions(-)

-- 
2.21.0



[PATCH v4 1/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-06 Thread Takashi Yano
- When the I/O pipe is switched to the pseudo console side, the
  behaviour of Ctrl-C is unstable. This rarely happens, however,
  for example, shell sometimes crashes by Ctrl-C in that situation.
  This patch fixes that issue.
---
 winsup/cygwin/fhandler.h  |  4 
 winsup/cygwin/fhandler_tty.cc | 33 +--
 winsup/cygwin/select.cc   |  2 +-
 winsup/cygwin/spawn.cc| 42 ++-
 4 files changed, 39 insertions(+), 42 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index e72e11f7a..e0c56cd34 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -2187,10 +2187,6 @@ class fhandler_pty_slave: public fhandler_pty_common
 get_ttyp ()->mask_switch_to_pcon = mask;
   }
   void fixup_after_attach (bool native_maybe);
-  pid_t get_pcon_pid (void)
-  {
-return get_ttyp ()->pcon_pid;
-  }
   bool is_line_input (void)
   {
 return get_ttyp ()->ti.c_lflag & ICANON;
diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 3ffd64e21..e7b018b4c 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -1026,20 +1026,26 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
   get_ttyp ()->need_clear_screen = false;
 }
 
-  if (ALWAYS_USE_PCON)
-return;
   if (isHybrid)
-{
-  this->set_switch_to_pcon ();
-  return;
-}
+this->set_switch_to_pcon ();
   if (get_ttyp ()->pcon_pid &&
   get_ttyp ()->pcon_pid != myself->pid &&
   kill (get_ttyp ()->pcon_pid, 0) == 0)
 /* There is a process which is grabbing pseudo console. */
 return;
-  if (get_ttyp ()->switch_to_pcon &&
-  get_ttyp ()->pcon_pid != myself->pid)
+  if (isHybrid)
+{
+  if (ALWAYS_USE_PCON)
+   {
+ DWORD mode;
+ GetConsoleMode (get_handle (), );
+ SetConsoleMode (get_handle (), mode & ~ENABLE_PROCESSED_INPUT);
+   }
+  get_ttyp ()->pcon_pid = 0;
+  init_console_handler (true);
+  return;
+}
+  if (get_ttyp ()->switch_to_pcon)
 {
   DWORD mode;
   GetConsoleMode (get_handle (), );
@@ -1048,6 +1054,7 @@ fhandler_pty_slave::reset_switch_to_pcon (void)
 }
   get_ttyp ()->pcon_pid = 0;
   get_ttyp ()->switch_to_pcon = false;
+  init_console_handler (true);
 }
 
 void
@@ -1307,8 +1314,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
   if (ptr) /* Indicating not tcflush(). */
 {
   reset_switch_to_pcon ();
-  if (get_ttyp ()->pcon_pid != myself->pid)
-   mask_switch_to_pcon (true);
+  mask_switch_to_pcon (true);
 }
 
   if (is_nonblocking () || !ptr) /* Indicating tcflush(). */
@@ -1428,7 +1434,7 @@ fhandler_pty_slave::read (void *ptr, size_t& len)
flags &= ~ENABLE_ECHO_INPUT;
  if ((get_ttyp ()->ti.c_lflag & ISIG) &&
  !(get_ttyp ()->ti.c_iflag & IGNBRK))
-   flags |= ENABLE_PROCESSED_INPUT;
+   flags |= ALWAYS_USE_PCON ? 0 : ENABLE_PROCESSED_INPUT;
  if (dwMode != flags)
SetConsoleMode (get_handle (), flags);
  /* Read get_handle() instad of get_handle_cyg() */
@@ -2875,8 +2881,10 @@ fhandler_pty_slave::fixup_after_attach (bool 
native_maybe)
  get_ttyp ()->num_pcon_attached_slaves ++;
}
}
+  if (ALWAYS_USE_PCON && pcon_attached_to == get_minor ())
+   set_ishybrid_and_switch_to_pcon (get_output_handle ());
 }
-  if (pcon_attached_to == get_minor () && (native_maybe || ALWAYS_USE_PCON))
+  if (pcon_attached_to == get_minor () && native_maybe)
 {
   FlushConsoleInputBuffer (get_handle ());
   DWORD mode;
@@ -2891,6 +2899,7 @@ fhandler_pty_slave::fixup_after_attach (bool native_maybe)
  kill (get_ttyp ()->pcon_pid, 0) != 0)
get_ttyp ()->pcon_pid = myself->pid;
   get_ttyp ()->switch_to_pcon = true;
+  init_console_handler(false);
 }
 }
 
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index 4efc302df..3589ccabf 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1294,7 +1294,7 @@ pty_slave_startup (select_record *me, select_stuff *stuff)
 {
   fhandler_base *fh = (fhandler_base *) me->fh;
   fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
-  if (me->read_selected && ptys->get_pcon_pid () != myself->pid)
+  if (me->read_selected)
 ptys->mask_switch_to_pcon (true);
 
   select_pipe_info *pi = stuff->device_specific_ptys;
diff --git a/winsup/cygwin/spawn.cc b/winsup/cygwin/spawn.cc
index 15cba3610..7c9e67303 100644
--- a/winsup/cygwin/spawn.cc
+++ b/winsup/cygwin/spawn.cc
@@ -261,6 +261,21 @@ child_info_spawn::worker (const char *prog_arg, const char 
*const *argv,
   int res = -1;
   DWORD pidRestore = 0;
   bool attach_to_pcon = false;
+  pid_t ctty_pgid = 0;
+
+  /* Search for CTTY and retrieve its PGID */
+  cygheap_fdenum cfd (false);
+  while (cfd.next () >= 0)
+if (cfd->get_major () == DEV_PTYS_MAJOR ||
+   cfd->get_major () == 

Re: [PATCH v3 0/1] Cygwin: pty: Fix the behaviour of Ctrl-C in the pseudo console mode.

2019-09-07 Thread Takashi Yano
On Sat, 7 Sep 2019 12:20:15 +0900
Takashi Yano wrote:
> On Fri, 6 Sep 2019 17:59:02 +
> Ken Brown wrote:
> > 3. I used ssh from my normal account to log into an administrator account.  
> > I 
> > ran a script that produced a lot of output and piped it to less.  I pressed 
> > 'q' 
> > after the first screen was displayed, and the displayed text didn't get 
> > cleared.
> 
> Are you using non-cygwin program in the script? If so, this may happen
> in test release 3.1.0-0.4 as well.

3.1.0-0.3 as well.

-- 
Takashi Yano 


[PATCH 5/5] Cygwin: pty: Add missing guard when PTY is in the legacy mode.

2019-09-18 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 5 -
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 2a92e44cf..1095c82eb 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -87,7 +87,8 @@ set_switch_to_pcon (void)
   {
fhandler_base *fh = cfd;
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
-   ptys->set_switch_to_pcon (fd);
+   if (ptys->getPseudoConsole ())
+ ptys->set_switch_to_pcon (fd);
   }
 }
 
@@ -105,6 +106,8 @@ force_attach_to_pcon (HANDLE h)
  {
fhandler_base *fh = cfd;
fhandler_pty_slave *ptys = (fhandler_pty_slave *) fh;
+   if (!ptys->getPseudoConsole ())
+ continue;
if (n != 0
|| h == ptys->get_handle ()
|| h == ptys->get_output_handle ())
-- 
2.21.0



[PATCH 2/5] Cygwin: pty: Make GDB work again on pty.

2019-09-18 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 35 +++
 1 file changed, 35 insertions(+)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 2a1c34f7d..843807aab 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -197,6 +197,9 @@ DEF_HOOK (ReadConsoleInputA);
 DEF_HOOK (ReadConsoleInputW);
 DEF_HOOK (PeekConsoleInputA);
 DEF_HOOK (PeekConsoleInputW);
+/* CreateProcess() is hooked for GDB etc. */
+DEF_HOOK (CreateProcessA);
+DEF_HOOK (CreateProcessW);
 
 static BOOL WINAPI
 WriteFile_Hooked
@@ -331,6 +334,35 @@ PeekConsoleInputW_Hooked
   set_ishybrid_and_switch_to_pcon (h);
   return PeekConsoleInputW_Orig (h, r, l, n);
 }
+/* CreateProcess() is hooked for GDB etc. */
+static BOOL WINAPI
+CreateProcessA_Hooked
+ (LPCSTR n, LPSTR c, LPSECURITY_ATTRIBUTES pa, LPSECURITY_ATTRIBUTES ta,
+  BOOL inh, DWORD f, LPVOID e, LPCSTR d,
+  LPSTARTUPINFOA si, LPPROCESS_INFORMATION pi)
+{
+  HANDLE h;
+  if (si->dwFlags & STARTF_USESTDHANDLES)
+h = si->hStdOutput;
+  else
+h = GetStdHandle (STD_OUTPUT_HANDLE);
+  set_ishybrid_and_switch_to_pcon (h);
+  return CreateProcessA_Orig (n, c, pa, ta, inh, f, e, d, si, pi);
+}
+static BOOL WINAPI
+CreateProcessW_Hooked
+ (LPCWSTR n, LPWSTR c, LPSECURITY_ATTRIBUTES pa, LPSECURITY_ATTRIBUTES ta,
+  BOOL inh, DWORD f, LPVOID e, LPCWSTR d,
+  LPSTARTUPINFOW si, LPPROCESS_INFORMATION pi)
+{
+  HANDLE h;
+  if (si->dwFlags & STARTF_USESTDHANDLES)
+h = si->hStdOutput;
+  else
+h = GetStdHandle (STD_OUTPUT_HANDLE);
+  set_ishybrid_and_switch_to_pcon (h);
+  return CreateProcessW_Orig (n, c, pa, ta, inh, f, e, d, si, pi);
+}
 #else /* USE_API_HOOK */
 #define WriteFile_Orig 0
 #define ReadFile_Orig 0
@@ -2778,6 +2810,9 @@ fhandler_pty_slave::fixup_after_exec ()
   DO_HOOK (NULL, ReadConsoleInputW);
   DO_HOOK (NULL, PeekConsoleInputA);
   DO_HOOK (NULL, PeekConsoleInputW);
+  /* CreateProcess() is hooked for GDB etc. */
+  DO_HOOK (NULL, CreateProcessA);
+  DO_HOOK (NULL, CreateProcessW);
 }
 #endif /* USE_API_HOOK */
 }
-- 
2.21.0



[PATCH] Cygwin: console: Make console input work in GDB and strace.

2019-09-18 Thread Takashi Yano
- After commit 2232498c712acc97a38fdc297cbe53ba74d0ec2c, console
  input cause error in GDB or strace. This patch fixes this issue.
---
 winsup/cygwin/fhandler_termios.cc | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/winsup/cygwin/fhandler_termios.cc 
b/winsup/cygwin/fhandler_termios.cc
index 5b0ba5603..282f0fbf4 100644
--- a/winsup/cygwin/fhandler_termios.cc
+++ b/winsup/cygwin/fhandler_termios.cc
@@ -202,7 +202,8 @@ fhandler_termios::bg_check (int sig, bool dontsignal)
   */
   if (!myself->pgid || !tc () || tc ()->getpgid () == myself->pgid ||
myself->ctty != tc ()->ntty ||
-   ((sig == SIGTTOU) && !(tc ()->ti.c_lflag & TOSTOP)))
+   ((sig == SIGTTOU) && !(tc ()->ti.c_lflag & TOSTOP)) ||
+   being_debugged ())
 return bg_ok;
 
   /* sig -SIGTTOU is used to indicate a change to terminal settings, where
-- 
2.21.0



[PATCH 1/5] Cygwin: pty: Avoid potential segfault in PTY code when ppid = 1.

2019-09-18 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 11 +--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 659e7b595..2a1c34f7d 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -137,9 +137,16 @@ force_attach_to_pcon (HANDLE h)
/* If the process is running on a console,
   the parent process should be attached
   to the same console. */
-   pinfo p (myself->ppid);
+   DWORD attach_wpid;
+   if (myself->ppid == 1)
+ attach_wpid = ATTACH_PARENT_PROCESS;
+   else
+ {
+   pinfo p (myself->ppid);
+   attach_wpid = p->dwProcessId;
+ }
FreeConsole ();
-   if (AttachConsole (p->dwProcessId))
+   if (AttachConsole (attach_wpid))
  {
pcon_attached_to = -1;
attach_done = true;
-- 
2.21.0



[PATCH 0/5] Some fixes for PTY with pseudo console support (4)

2019-09-18 Thread Takashi Yano
Takashi Yano (5):
  Cygwin: pty: Avoid potential segfault in PTY code when ppid = 1.
  Cygwin: pty: Make GDB work again on pty.
  Cygwin: pty: Unify the charset conversion codes into a function.
  Cygwin: pty: Add charset conversion for console apps in legacy PTY.
  Cygwin: pty: Add missing guard when PTY is in the legacy mode.

 winsup/cygwin/fhandler_tty.cc | 188 +++---
 1 file changed, 104 insertions(+), 84 deletions(-)

-- 
2.21.0



[PATCH] Cygwin: console: Revive Win7 compatibility.

2019-09-18 Thread Takashi Yano
- The commit fca4cda7a420d7b15ac217d008527e029d05758e broke Win7
  compatibility. This patch fixes the issue.
---
 winsup/cygwin/fhandler_console.cc | 10 +-
 winsup/cygwin/select.cc   |  2 +-
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 709b8255d..75143f27a 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -499,8 +499,11 @@ fhandler_console::process_input_message (void)
 
   termios *ti = &(get_ttyp ()->ti);
 
- /* Per MSDN, max size of buffer required is below 64K. */
-#define  INREC_SIZE(65536 / sizeof (INPUT_RECORD))
+  /* Per MSDN, max size of buffer required is below 64K. */
+  /* (65536 / sizeof (INPUT_RECORD)) is 3276, however,
+ ERROR_NOT_ENOUGH_MEMORY occurs in win7 if this value
+ is used. */
+#define INREC_SIZE 2048
 
   fhandler_console::input_states stat = input_processing;
   DWORD total_read, i;
@@ -1165,9 +1168,6 @@ fhandler_console::ioctl (unsigned int cmd, void *arg)
return -1;
   case FIONREAD:
{
- /* Per MSDN, max size of buffer required is below 64K. */
-#define  INREC_SIZE(65536 / sizeof (INPUT_RECORD))
-
  DWORD n;
  int ret = 0;
  INPUT_RECORD inp[INREC_SIZE];
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index ed8c98d1c..8fdce06a4 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1209,7 +1209,7 @@ peek_pty_slave (select_record *s, bool from_select)
{
  if (ptys->is_line_input ())
{
-#define INREC_SIZE (65536 / sizeof (INPUT_RECORD))
+#define INREC_SIZE 2048
  INPUT_RECORD inp[INREC_SIZE];
  DWORD n;
  PeekConsoleInput (ptys->get_handle (), inp, INREC_SIZE, );
-- 
2.21.0



[PATCH 4/5] Cygwin: pty: Add charset conversion for console apps in legacy PTY.

2019-09-18 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 7 +++
 1 file changed, 7 insertions(+)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index f723ec7cf..2a92e44cf 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -3054,6 +3054,12 @@ fhandler_pty_master::pty_master_fwd_thread ()
  mb_str_free (buf);
  continue;
}
+  size_t nlen;
+  char *buf = convert_mb_str
+   (get_ttyp ()->term_code_page, , GetConsoleOutputCP (), ptr, wlen);
+
+  ptr = buf;
+  wlen = rlen = nlen;
   acquire_output_mutex (INFINITE);
   while (rlen>0)
{
@@ -3066,6 +3072,7 @@ fhandler_pty_master::pty_master_fwd_thread ()
  wlen = (rlen -= wlen);
}
   release_output_mutex ();
+  mb_str_free (buf);
 }
   return 0;
 }
-- 
2.21.0



[PATCH 3/5] Cygwin: pty: Unify the charset conversion codes into a function.

2019-09-18 Thread Takashi Yano
---
 winsup/cygwin/fhandler_tty.cc | 130 +-
 1 file changed, 49 insertions(+), 81 deletions(-)

diff --git a/winsup/cygwin/fhandler_tty.cc b/winsup/cygwin/fhandler_tty.cc
index 843807aab..f723ec7cf 100644
--- a/winsup/cygwin/fhandler_tty.cc
+++ b/winsup/cygwin/fhandler_tty.cc
@@ -370,7 +370,43 @@ CreateProcessW_Hooked
 void set_ishybrid_and_switch_to_pcon (HANDLE) {}
 #endif /* USE_API_HOOK */
 
-bool
+static char *
+convert_mb_str (UINT cp_to, size_t *len_to,
+   UINT cp_from, const char *ptr_from, size_t len_from)
+{
+  char *buf;
+  size_t nlen;
+  if (cp_to != cp_from)
+{
+  size_t wlen =
+   MultiByteToWideChar (cp_from, 0, ptr_from, len_from, NULL, 0);
+  wchar_t *wbuf = (wchar_t *)
+   HeapAlloc (GetProcessHeap (), 0, wlen * sizeof (wchar_t));
+  wlen =
+   MultiByteToWideChar (cp_from, 0, ptr_from, len_from, wbuf, wlen);
+  nlen = WideCharToMultiByte (cp_to, 0, wbuf, wlen, NULL, 0, NULL, NULL);
+  buf = (char *) HeapAlloc (GetProcessHeap (), 0, nlen);
+  nlen = WideCharToMultiByte (cp_to, 0, wbuf, wlen, buf, nlen, NULL, NULL);
+  HeapFree (GetProcessHeap (), 0, wbuf);
+}
+  else
+{
+  /* Just copy */
+  buf = (char *) HeapAlloc (GetProcessHeap (), 0, len_from);
+  memcpy (buf, ptr_from, len_from);
+  nlen = len_from;
+}
+  *len_to = nlen;
+  return buf;
+}
+
+static void
+mb_str_free (char *ptr)
+{
+  HeapFree (GetProcessHeap (), 0, ptr);
+}
+
+static bool
 bytes_available (DWORD& n, HANDLE h)
 {
   DWORD navail, nleft;
@@ -1270,34 +1306,11 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
 
   reset_switch_to_pcon ();
 
-  char *buf;
-  ssize_t nlen;
-  UINT targetCodePage = get_ttyp ()->switch_to_pcon_out ?
+  UINT target_code_page = get_ttyp ()->switch_to_pcon_out ?
 GetConsoleOutputCP () : get_ttyp ()->term_code_page;
-  if (targetCodePage != get_ttyp ()->term_code_page)
-{
-  size_t wlen =
-   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
-(char *)ptr, len, NULL, 0);
-  wchar_t *wbuf = (wchar_t *)
-   HeapAlloc (GetProcessHeap (), 0, wlen * sizeof (wchar_t));
-  wlen =
-   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
-(char *)ptr, len, wbuf, wlen);
-  nlen = WideCharToMultiByte (targetCodePage, 0,
- wbuf, wlen, NULL, 0, NULL, NULL);
-  buf = (char *) HeapAlloc (GetProcessHeap (), 0, nlen);
-  nlen = WideCharToMultiByte (targetCodePage, 0,
- wbuf, wlen, buf, nlen, NULL, NULL);
-  HeapFree (GetProcessHeap (), 0, wbuf);
-}
-  else
-{
-  /* Just copy */
-  buf = (char *) HeapAlloc (GetProcessHeap (), 0, len);
-  memcpy (buf, (char *)ptr, len);
-  nlen = len;
-}
+  ssize_t nlen;
+  char *buf = convert_mb_str (target_code_page, (size_t *) ,
+get_ttyp ()->term_code_page, (const char *) ptr, len);
 
   /* If not attached to this pseudo console, try to attach temporarily. */
   pid_restore = 0;
@@ -1334,7 +1347,7 @@ fhandler_pty_slave::write (const void *ptr, size_t len)
   towrite = -1;
 }
   release_output_mutex ();
-  HeapFree (GetProcessHeap (), 0, buf);
+  mb_str_free (buf);
   flags = ENABLE_VIRTUAL_TERMINAL_PROCESSING;
   if (get_ttyp ()->switch_to_pcon_out && !fallback)
 SetConsoleMode (get_output_handle (), dwMode | flags);
@@ -2260,33 +2273,10 @@ fhandler_pty_master::write (const void *ptr, size_t len)
  if current application is native console application. */
   if (to_be_read_from_pcon ())
 {
-  char *buf;
   size_t nlen;
+  char *buf = convert_mb_str
+   (CP_UTF8, , get_ttyp ()->term_code_page, (const char *) ptr, len);
 
-  if (get_ttyp ()->term_code_page != CP_UTF8)
-   {
- size_t wlen =
-   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
-(char *)ptr, len, NULL, 0);
- wchar_t *wbuf = (wchar_t *)
-   HeapAlloc (GetProcessHeap (), 0, wlen * sizeof (wchar_t));
- wlen =
-   MultiByteToWideChar (get_ttyp ()->term_code_page, 0,
-(char *)ptr, len, wbuf, wlen);
- nlen = WideCharToMultiByte (CP_UTF8, 0,
- wbuf, wlen, NULL, 0, NULL, NULL);
- buf = (char *) HeapAlloc (GetProcessHeap (), 0, nlen);
- nlen = WideCharToMultiByte (CP_UTF8, 0,
- wbuf, wlen, buf, nlen, NULL, NULL);
- HeapFree (GetProcessHeap (), 0, wbuf);
-   }
-  else
-   {
- /* Just copy */
- buf = (char *) HeapAlloc (GetProcessHeap (), 0, len);
- memcpy (buf, (char *)ptr, len);
- nlen = len;
-   }
   DWORD wLen;
   WriteFile (to_slave, buf, nlen, , NULL);
 
@@ -2302,7 +2292,7 @@ fhandler_pty_master::write (const void *ptr, size_t len)
   else
SetEvent 

[PATCH v2 1/1] Cygwin: console: Revive Win7 compatibility.

2019-09-18 Thread Takashi Yano
- The commit fca4cda7a420d7b15ac217d008527e029d05758e broke Win7
  compatibility. This patch fixes the issue.
---
 winsup/cygwin/fhandler.h  | 6 ++
 winsup/cygwin/fhandler_console.cc | 6 --
 winsup/cygwin/select.cc   | 1 -
 3 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h
index 4efb6a4f2..94b0e520b 100644
--- a/winsup/cygwin/fhandler.h
+++ b/winsup/cygwin/fhandler.h
@@ -43,6 +43,12 @@ details. */
 
 #define O_TMPFILE_FILE_ATTRS (FILE_ATTRIBUTE_TEMPORARY | FILE_ATTRIBUTE_HIDDEN)
 
+/* Buffer size for ReadConsoleInput() and PeakConsoleInput(). */
+/* Per MSDN, max size of buffer required is below 64K. */
+/* (65536 / sizeof (INPUT_RECORD)) is 3276, however,
+   ERROR_NOT_ENOUGH_MEMORY occurs in win7 if this value is used. */
+#define INREC_SIZE 2048
+
 extern const char *windows_device_names[];
 extern struct __cygwin_perfile *perfile_table;
 #define __fmode (*(user_data->fmode_ptr))
diff --git a/winsup/cygwin/fhandler_console.cc 
b/winsup/cygwin/fhandler_console.cc
index 709b8255d..86c39db25 100644
--- a/winsup/cygwin/fhandler_console.cc
+++ b/winsup/cygwin/fhandler_console.cc
@@ -499,9 +499,6 @@ fhandler_console::process_input_message (void)
 
   termios *ti = &(get_ttyp ()->ti);
 
- /* Per MSDN, max size of buffer required is below 64K. */
-#define  INREC_SIZE(65536 / sizeof (INPUT_RECORD))
-
   fhandler_console::input_states stat = input_processing;
   DWORD total_read, i;
   INPUT_RECORD input_rec[INREC_SIZE];
@@ -1165,9 +1162,6 @@ fhandler_console::ioctl (unsigned int cmd, void *arg)
return -1;
   case FIONREAD:
{
- /* Per MSDN, max size of buffer required is below 64K. */
-#define  INREC_SIZE(65536 / sizeof (INPUT_RECORD))
-
  DWORD n;
  int ret = 0;
  INPUT_RECORD inp[INREC_SIZE];
diff --git a/winsup/cygwin/select.cc b/winsup/cygwin/select.cc
index ed8c98d1c..e7014422b 100644
--- a/winsup/cygwin/select.cc
+++ b/winsup/cygwin/select.cc
@@ -1209,7 +1209,6 @@ peek_pty_slave (select_record *s, bool from_select)
{
  if (ptys->is_line_input ())
{
-#define INREC_SIZE (65536 / sizeof (INPUT_RECORD))
  INPUT_RECORD inp[INREC_SIZE];
  DWORD n;
  PeekConsoleInput (ptys->get_handle (), inp, INREC_SIZE, );
-- 
2.21.0



Re: [PATCH] Cygwin: console: Revive Win7 compatibility.

2019-09-18 Thread Takashi Yano
On Wed, 18 Sep 2019 18:21:49 +0200
Achim Gratz wrote:
> Takashi Yano writes:
> > - The commit fca4cda7a420d7b15ac217d008527e029d05758e broke Win7
> >   compatibility. This patch fixes the issue.
> > ---
> >  winsup/cygwin/fhandler_console.cc | 10 +-
> >  winsup/cygwin/select.cc   |  2 +-
> >  2 files changed, 6 insertions(+), 6 deletions(-)
> 
> It seems like an attractor for future bugs to define the same constant
> in two different places.  Would there be a header that could provide the
> definition instead?

I agree with you. I will post revised one as v2.

-- 
Takashi Yano 


[PATCH v2 0/1] Cygwin: console: Revive Win7 compatibility.

2019-09-18 Thread Takashi Yano
- The commit fca4cda7a420d7b15ac217d008527e029d05758e broke Win7
  compatibility. This patch fixes the issue.

v2:
Move definition of INREC_SIZE into fhandler.h from fhandler_console.cc
and select.cc.

Takashi Yano (1):
  Cygwin: console: Revive Win7 compatibility.

 winsup/cygwin/fhandler.h  | 6 ++
 winsup/cygwin/fhandler_console.cc | 6 --
 winsup/cygwin/select.cc   | 1 -
 3 files changed, 6 insertions(+), 7 deletions(-)

-- 
2.21.0



  1   2   3   4   5   6   7   8   9   >