Commit Graph

417 Commits

Author SHA1 Message Date
Takashi Yano via Cygwin-patches f186f61d60 Cygwin: pty: Make slave read() thread-safe.
- Currently slave read() is somehow not thread-safe. This patch
  fixes the issue.
2021-02-01 10:54:04 +01:00
Takashi Yano via Cygwin-patches 253352e796 Cygwin: pty: Allow multiple apps to enable pseudo console simultaneously.
- After commit bb428520, there has been the disadvantage:
  7) Pseudo console cannot be activated if it is already activated for
     another process on same pty.
  This patch clears this disadvantage.
2021-01-28 11:21:12 +01:00
Takashi Yano via Cygwin-patches 8aeb3f3e50 Cygwin: pty: Make apps using console APIs be able to debug with gdb.
- After commit bb428520, there has been the disadvantage:
  2) The apps which use console API cannot be debugged with gdb. This
     is because pseudo console is not activated since gdb uses
     CreateProcess() rather than exec(). Even with this limitation,
     attaching gdb to native app, in which pseudo console is already
     activated, works.
  This patch clears this disadvantage.
2021-01-28 11:21:12 +01:00
Takashi Yano via Cygwin-patches 442093214d Cygwin: pty: Keep code page between non-cygwin apps.
- After commit bb428520, there has been the disadvantage:
  4) Code page cannot be changed by chcp.com. Acctually, chcp works
     itself and changes code page of its own pseudo console.  However,
     since pseudo console is recreated for another process, it cannot
     inherit the code page.
  This patch clears this disadvantage.
2021-01-28 11:21:12 +01:00
Takashi Yano via Cygwin-patches 10d083c745 Cygwin: pty: Inherit typeahead data between two input pipes.
- PTY has a problem that the key input, which is typed during windows
  native app is running, disappears when it returns to shell. This is
  beacuse pty has two input pipes, one is for cygwin apps and the other
  one is for native windows apps. The key input during windows native
  program is running is sent to the second input pipe while cygwin
  shell reads input from the first input pipe. This issue had been
  fixed once by commit 29431fcb, however, the new implementation of
  pseudo console support by commit bb428520 could not inherit this
  feature. This patch realize transfering input data between these
  two pipes bidirectionally by utilizing cygwin-console-helper process.
  The helper process is launched prior to starting the non-cygwin app,
  however, exits immediately unlike previous implementation.
2021-01-28 11:21:12 +01:00
Takashi Yano via Cygwin-patches 5755870f7c Cygwin: pty: Reduce buffer size in get_console_process_id().
- The buffer used in get_console_process_id(), introduced by commit
  72770148, is too large and ERROR_NOT_ENOUGH_MEMORY occurs in Win7.
  Therefore, the buffer size has been reduced.
2021-01-20 10:19:39 +01:00
Takashi Yano via Cygwin-patches 62e739b51b Cygwin: pty: Lessen the side effect of workaround for rlwarp.
- This patch lessens the side effect of the workaround for rlwrap
  introduced by commit 4e16b033.
2021-01-19 10:56:43 +01:00
Takashi Yano via Cygwin-patches a776a0ce26 Cygwin: pty: Set input_available_event only for cygwin pipe.
- cat exits immediately in the following senario.
    1) Execute env CYGWIN=disable_pcon script
    2) Execute cmd.exe
    3) Execute cat in cmd.exe.
  This is caused by setting input_available_event for the pipe for
  non-cygwin app. This patch fixes the issue.
2021-01-18 16:26:28 +01:00
Takashi Yano via Cygwin-patches 25ce7a6245 Cygwin: pty: Make master thread functions be static.
- The functions pty_master_thread() and pty_master_fwd_thread()
  should be static (i.e. should not access class member) because
  the instance is deleted if the master is dup()'ed and the first
  master is closed. In this case, because the dup()'ed instance
  still exists, these master threads are also still alive even
  though the instance has been deleted. As a result, accesing
  class members in these functions causes accessi violation.

  Addresses:
  https://cygwin.com/pipermail/cygwin-developers/2021-January/012030.html
2021-01-18 14:18:43 +01:00
Takashi Yano via Cygwin-patches 72770148ad Cygwin: pty: Prevent pty from changing code page of parent console.
- After commit 232fde0e, pty changes console code page when the first
  non-cygwin app is executed. If pty is started in real console device,
  pty changes the code page of root console. This causes very annoying
  result because changing code page changes the font of command prompt
  if console is in legacy mode. This patch avoids this by creating a
  new invisible console for the first pty started in console device.
2021-01-18 14:18:43 +01:00
Takashi Yano via Cygwin-patches 85ea2614f5 Cygwin: pty: Make close_pseudoconsole() be a static member function.
- The function close_pseudoconsole() should be static so that it
  can be safely called in spawn.cc even after the fhandler_pty_slave
  instance has been deleted. That is, there is a problem with the
  current code. This patch fixes the issue.
2021-01-18 11:02:46 +01:00
Takashi Yano via Cygwin-patches fa22eea29d Cygwin: pty: Add workaround for rlwrap 0.40 or later.
- The workaround for rlwrap introduced by commit 8199b0cc does not
  take effect for rlwrap 0.40 or later. This patch add a workaround
  for rlwrap 0.40 or later as well.
2021-01-18 11:02:46 +01:00
Takashi Yano via Cygwin-patches 4e16b0330a Cygwin: pty: Revise the workaround for rlwrap.
- Previous workaround has a problem that screen is distorted if up
  arrow key is pressed at the first line after running "rlwrap cmd".
  This patch fixes the issue.
2020-12-16 10:31:09 +01:00
Takashi Yano via Cygwin-patches da8cebcded Cygwin: pty: Check response for CSI6n more strictly.
- Previous code to read response for CSI6n allows invalid response
  such as "CSI Pl; Pc H" other than correct response "CSI Pl; Pc R".
  With this patch, the response is checked more strictly.
2020-12-16 10:31:09 +01:00
Takashi Yano via Cygwin-patches 8199b0cc38 Cygwin: pty: Add a workaround for rlwrap.
- If rlwrap is used with non-cygwin apps, it fails to setup pseudo
  console. This patch adds a workaround for this issue.
2020-12-14 11:02:23 +01:00
Takashi Yano via Cygwin-patches 55eff668b8 Cygwin: pty: Revise the code for timeout in term_has_pcon_cap().
- Sometimes timeout period in term_has_pcon_cap() may not be enough
  when the machine slows down for some reason. This patch eases the
  issue. In the new code, effective timeout period is expected to be
  extended as a result due to slowing-down the wait loop as well when
  the machine gets into busy.
2020-12-14 11:02:23 +01:00
Takashi Yano via Cygwin-patches c2c33e4d67 Cygwin: pty: Skip term_has_pcon_cap() if pseudo console is disabled.
- This patch skips unnecessary term_has_pcon_cap() call if pseudo
  console is disabled.
2020-12-14 11:02:23 +01:00
Takashi Yano via Cygwin-patches 796044ddcf Cygwin: pty: Fix minor style issue. 2020-11-23 16:23:27 +01:00
Takashi Yano via Cygwin-patches 2eee095928 Cygwin: pty: Discard "OSC Ps; ? BEL/ST" in pseudo console output.
- If vim is executed in WSL in mintty, some garbage string caused
  by "OSC Ps;? BEL/ST" will be shown in some situations. This patch
  fixes the issue by removing "OSC Ps;? BEL/ST" from pseudo console
  output.
2020-11-23 16:23:27 +01:00
Takashi Yano via Cygwin-patches 65ee05d326 Cygwin: pty: Fix a bug in the code removing "CSI > Pm m".
- The code added by 8121b606e8 has a
  bug which fails to remove multiple "CSI > Pm m" sequences. This
  patch fixes the bug.
2020-11-23 16:23:27 +01:00
Takashi Yano via Cygwin-patches 490d4862df Cygwin: pty: Disable ResizePseudoConsole() if stdout is redirected.
- Calling ResizePseudoConsole() generates some escape sequences.
  Due to this behaviour, if the output of non-cygwin app is piped
  to less, screen is sometimes distorted when the screen is resized.
  With this patch, ResizePseudoConsole() is not called if stdout is
  redirected.
2020-10-27 10:03:42 +01:00
Takashi Yano via Cygwin-patches 7b996f807e Cygwin: pty: Fix race condition in initialization of pseudo console.
- If output of non-cygwin process is piped to cygwin process, such
  as less, the non-cygwin process sometimes fails to start and hangs.
  This patch fixes the issue.
2020-10-26 10:04:12 +01:00
Takashi Yano 2ed80d04f4 Cygwin: pty: Drop handling for UTF-7 in convert_mb_str().
- Charset conversion for UTF-7, ISO-2022 and ISCII, which are not
  supported in cygwin, does not work properly as a result. At the
  expense of the above, the code has been simplified a bit.
2020-09-11 20:14:33 +02:00
Takashi Yano via Cygwin-patches 232fde0e76 Cygwin: pty: Prevent garbled output for existing non-cygwin apps.
- If pseudo console is disabled, non-cygwin apps do not detect
  console device. In this case, some apps output UTF-8 regardless
  of the locale setting. At least git-for-windows, rust-based apps
  and node.js do that. This patch provides backward compatibility
  as default behaviour by setting console codepage to the charset of
  the locale. Even in the cases above, garbled output is prevented
  with this patch in most cases because mintty uses UTF-8 by default.

  I beleave this is not really a problem in cygwin side but that in
  app side, however, some users complain about garbled output with
  existing apps in MSYS2 (which is based on cygwin) in which pseudo
  console is disabled by default.
2020-09-11 14:09:10 +02:00
Corinna Vinschen 09738c3062 Cygwin: pty: setup new pty on opening the master, not in constructor
Setting up the pty in the master constructor ends up creating a new pty
on every stat(2) call on /dev/ptmx.  Only do this when actually opening
the device, not when using the device class in another, non-opening
context.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-09-09 22:57:09 +02:00
Takashi Yano via Cygwin-patches fd3ad92f9e Cygwin: pty: Fix input charset for non-cygwin apps with disable_pcon.
- If the non-cygwin apps is executed under pseudo console disabled,
  multibyte input for the apps are garbled. This patch fixes the
  issue.
2020-09-09 21:35:46 +02:00
Takashi Yano via Cygwin-patches 91908fe000 Cygwin: pty: Revise convert_mb_str() function.
- Use tmp_pathbuf instead of HeapAlloc()/HeapFree().
- Remove mb_str_free() function.
- Consider the case where the multibyte string stops in the middle
  of a multibyte char.
2020-09-09 21:35:46 +02:00
Corinna Vinschen eaed594d73 Cygwin: pty: move codepage evaluation to nlsfuncs.cc
The new function __eval_codepage_from_internal_charset
is a simplified version of the former code in
fhandler_tty.cc.  It probably needs some extension,
but the gist is to use knowledge of internals to
be as quick as possible.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2020-09-08 10:36:04 +02:00
Takashi Yano via Cygwin-patches 6871c8418d Cygwin: pty: Fix a bug in the code removing set window title sequence.
- Commit 4e08fe42c9 has a bug which
  may cause infinite loop in pty_master_fwd_thread(). This patch
  fixes the issue.
2020-08-31 16:25:11 +02:00
Takashi Yano via Cygwin-patches 4e08fe42c9 Cygwin: pty: Disable pseudo console if TERM does not have CSI6n.
- Pseudo console internally sends escape sequence CSI6n (query cursor
  position) on startup of non-cygwin apps. If the terminal does not
  support CSI6n, CreateProcess() hangs waiting for response. To prevent
  hang, this patch disables pseudo console if the terminal does not
  have CSI6n. This is checked on the first execution of non-cygwin
  app using the following steps.
    1) Check if the terminal support ANSI escape sequences by looking
       into terminfo database. If terminfo has cursor_home (ESC [H),
       the terminal is supposed to support ANSI escape sequences.
    2) If the terminal supports ANSI escape sequneces, send CSI6n for
       a test and wait for a responce for 40ms.
    3) If there is a responce within 40ms, CSI6n is supposed to be
       supported.
  Also set-title capability is checked, and removes escape sequence
  for setting window title if the terminal does not have the set-
  title capability.
2020-08-31 12:07:09 +02:00
Takashi Yano bb42852062 Cygwin: pty: Implement new pseudo console support.
- In this implementation, pseudo console is created for each native
  console app. Advantages and disadvantages of this implementation
  over the previous implementation are as follows.

  Advantages:
  1) No performance degradation in pty output for cygwin process.
      https://cygwin.com/pipermail/cygwin/2020-February/243858.html
  2) Free from the problem caused by difference of behaviour of control
     sequences between real terminal and pseudo console.
      https://cygwin.com/pipermail/cygwin/2019-December/243281.html
      https://cygwin.com/pipermail/cygwin/2020-February/243855.html
  3) Free from the problem in cgdb and emacs gud.
      https://cygwin.com/pipermail/cygwin/2020-January/243601.html
      https://cygwin.com/pipermail/cygwin/2020-March/244146.html
  4) Redrawing screen on executing native console apps is not necessary.
  5) cygwin-console-helper is not necessary for the pseudo console
     support.
  6) The codes for pseudo console support are much simpler than that
     of the previous one.

  Disadvantages:
  1) The cygwin program which calls console API directly does not work.
  2) The apps which use console API cannot be debugged with gdb. This
     is because pseudo console is not activated since gdb uses
     CreateProcess() rather than exec(). Even with this limitation,
     attaching gdb to native apps, in which pseudo console is already
     activated, works.
  3) Typeahead key inputs are discarded while native console app is
     executed. Simirally, typeahead key inputs while cygwin app is
     executed are not inherited to native console app.
  4) Code page cannot be changed by chcp.com. Acctually, chcp works
     itself and changes code page of its own pseudo console.  However,
     since pseudo console is recreated for another process, it cannot
     inherit the code page.
  5) system_printf() does not work after stderr is closed. (Same with
     cygwin 3.0.7)
  6) Startup time of native console apps is about 3 times slower than
     previous implemenation.
  7) Pseudo console cannot be activated if it is already activated for
     another process on same pty.
2020-08-22 13:43:49 +02:00
Takashi Yano via Cygwin-patches 70d02aaca6 Cygwin: pty: Change the timing of set_locale() call again.
- After commit 095972ce5b, charset
  conversion in mintty is broken if charset is set to other than
  UTF-8. This seems to be caused because mintty does not set locale
  yet at fork() call. This patch changes the timing of set_locale()
  call again to avoid this issue.
2020-08-17 11:12:59 +02:00
Takashi Yano via Cygwin-patches 095972ce5b Cygwin: pty: Change the timing of setup_locale() call.
- If native app is exec()'ed in a new pty, setup_locale() loses the
  chance to be called. For example, with "mintty -e cmd", charset
  conversion does not work as expected. This patch fixes the issue.
2020-08-13 10:20:53 +02:00
Corinna Vinschen 50ad198085 Cygwin: Add 'fallthrough' pseudo keyword for switch/case use
This patch has been inspired by the Linux kernel patch

  294f69e662d1 compiler_attributes.h: Add 'fallthrough' pseudo keyword for switch/case use

written by Joe Perches <joe AT perches DOT com> based on an idea from
Dan Carpenter <dan DOT carpenter AT oracle DOT com>.  The following text
is from the original log message:

Reserve the pseudo keyword 'fallthrough' for the ability to convert the
various case block /* fallthrough */ style comments to appear to be an
actual reserved word with the same gcc case block missing fallthrough
warning capability.

All switch/case blocks now should end in one of:

	break;
	fallthrough;
	goto <label>;
	return [expression];
	continue;

In C mode, GCC supports the __fallthrough__ attribute since 7.1,
the same time the warning and the comment parsing were introduced.

Cygwin-only: add an explicit -Wimplicit-fallthrough=5 to the build
flags.
2020-08-05 21:58:22 +02:00
Takashi Yano via Cygwin-patches e0a53d6625 Cygwin: pty: Fix a bug on redirecting something to /dev/pty*.
- After commit 0365031ce1, key input
  becomes not working by following steps.
   1) Start cmd.exe in mintty.
   2) Open another mintty.
   3) Execute "echo AAA > /dev/pty*" (pty* is the pty opened in 1.)
  This patch fixes the issue.
2020-07-20 10:06:37 +02:00
Takashi Yano via Cygwin-patches c11b0343c0 Cygwin: pty, termios: Unify thoughts of read ahead beffer handling.
- Return value of eat_readahead() is redefined. The return values
  of fhandler_termios::eat_readahead() and fhandler_pty_slave::
  eat_readahead() were little bit different. This patch unifies
  them to number of bytes eaten by eat_readahead().
- Considerration for raixget() is added to fhandler_pty_master::
  accept_input() code.
- Transfering contents of read ahead buffer in
  fhandler_pty_master::transfer_input_to_pcon() is removed since
  it is not necessary.
- fhandler_pty_slave::eat_readahead() ckecks EOL only when ICANON
  is set.
- Guard for _POSIX_VDISABLE is added in checking EOL.
2020-07-01 09:25:54 +02:00
Takashi Yano via Cygwin-patches 8121b606e8 Cygwin: pty: Discard CSI > Pm m sequence from native windows apps.
- If vim is started from WSL (Ubuntu) which is executed in pseudo
  console in mintty, shift key and ctrl key do not work. Though
  this issue is similar to the issue resolved by commit
  4527541ec6, that commit is not
  effective for this issue. This patch fixes the issue by discarding
  "CSI > Pm m" in fhandler_pty_master::pty_master_fwd_thread().
2020-07-01 09:25:54 +02:00
Takashi Yano via Cygwin-patches 8014dc7099 Cygwin: pty: Fix screen distortion after less for native apps again.
- Commit c4b060e3fe seems to be not
  enough. Moreover, it does not work as expected at all in Win10
  1809. This patch essentially reverts that commit and add another
  fix. After all, the cause of the problem was a race issue in
  switch_to_pcon_out flag. That is, this flag is set when native
  app starts, however, it is delayed by wait_pcon_fwd(). Since the
  flag is not set yet when less starts, the data which should go
  into the output_handle accidentally goes into output_handle_cyg.
  This patch fixes the problem more essentially for the cause of
  the problem than previous one.
2020-06-05 21:34:52 -04:00
Takashi Yano via Cygwin-patches c4b060e3fe Cygwin: pty: Fix screen distortion after using less for native apps.
- If the output of non-cygwin apps is browsed using less, screen is
  ocasionally distorted after less exits. This frequently happens
  if cmd.exe is executed after less. This patch fixes the issue.
2020-06-01 10:13:57 +02:00
Takashi Yano via Cygwin-patches d212bdc400 Cygwin: pty: Revise the code which prevents undesired window title.
- In current pty, the window title can not be set from non-cygwin
  program due to the code which prevents overwriting the window
  title to "cygwin-console-helper.exe" in fhandler_pty_master::pty_
  master_fwd_thread(). This patch fixes the issue.
2020-05-31 10:33:55 +02:00
Takashi Yano via Cygwin-patches ac1f63ef28 Cygwin: pty: Clean up fhandler_pty_master::pty_master_fwd_thread().
- Remove the code which is not necessary anymore.
2020-05-31 10:33:55 +02:00
Takashi Yano via Cygwin-patches 0f7193f4fb Cygwin: pty: Prevent garbage remained in read ahead buffer.
- After commit 29431fcb5b, the issue
  reported in https://cygwin.com/pipermail/cygwin/2020-May/245057.html
  occurs. This is caused by the following mechanism. Cygwin less
  called from non-cygwin git is executed under /dev/cons* rather
  than /dev/pty* because parent git process only inherits pseudo
  console handle. Therefore, less sets ICANON flag for /dev/cons*
  rather than original /dev/pty*. When pty is switched to non-cygwin
  git process, line_edit() is used in fhandler_pty_master::write()
  only to set input_available_event and read ahead buffer is supposed
  to be flushed in accept_input(). However, ICANON flag is not set
  for /dev/pty*, so accept_input() is not called unless newline
  is entered. As a result, the input data remains in the read ahead
  buffer. This patch fixes the issue.
2020-05-31 10:33:55 +02:00
Takashi Yano via Cygwin-patches b5089f339a Cygwin: pty: Prevent meaningless ResizePseudoConsole() calls.
- This patch prevents to call ResizePseudoConsole() unless the pty
  is resized.
2020-05-28 16:44:36 +02:00
Takashi Yano via Cygwin-patches 25987b2c2a Cygwin: pty: Fix a bug in free_attached_console().
- After commit 7659ff0f5a, nohup does
  not work as expected. This patch fixes the issue.

  Addresses:
  https://cygwin.com/pipermail/cygwin-developers/2020-May/011885.html
2020-05-28 09:25:49 +02:00
Takashi Yano via Cygwin-patches 8d7a5b39d2 Cygwin: pty: Stop counting number of slaves attached to pseudo console.
- The number of slaves attached to pseudo console is used only for
  triggering redraw screen. Counting was not only needless, but also
  did not work as expected. This patch removes the code for counting.
2020-05-25 11:01:28 +02:00
Takashi Yano via Cygwin-patches c26e08095d Cygwin: pty: Revise code to make system_printf() work after close.
- After commit 0365031ce1, the issue
  https://cygwin.com/pipermail/cygwin-patches/2020q2/010259.html
  occurs. This patch fixes the issue.
2020-05-25 10:51:54 +02:00
Takashi Yano via Cygwin-patches 0365031ce1 Cygwin: pty: Make system_printf() work after closing pty slave.
- Current pty cannot show system_printf() output after closing pty
  slave. This patch fixes the issue.
2020-05-19 15:28:21 +02:00
Takashi Yano via Cygwin-patches 7659ff0f5a Cygwin: pty: Call FreeConsole() only if attached to current pty.
- After commit 071b8e0cbd, the problem
  reported in https://cygwin.com/pipermail/cygwin/2020-May/244873.html
  occurs. This is due to freeing console device accidentally rather
  than pseudo console. This patch makes sure to call FreeConsole()
  only if the process is attached to the pseudo console of the current
  pty.
2020-05-19 15:27:31 +02:00
Johannes Schindelin 717db9fd1c setup_pseudoconsole(): handle missing/incorrect helper gracefully
When `cygwin-console-helper.exe` is either missing, or corresponds to a
different Cygwin runtime, we currently wait forever while setting up
access to the pseudo console, even long after the process is gone that
was supposed to signal that it set up access to the pseudo console.

Let's handle that more gracefully: if the process exited without
signaling, we cannot use the pseudo console. In that case, let's just
fall back to not using it.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
2020-05-04 11:19:19 +02:00
Takashi Yano via Cygwin-patches 071b8e0cbd Cygwin: pty: Add FreeConsole to destructor of pty slave.
- When pseudo console is closed, all the processes attched to the
  pseudo console are terminated. This causes the problem reported
  in https://sourceware.org/pipermail/cygwin/2020-March/244046.html.
  This patch fixes the issue.
2020-03-13 10:55:41 +01:00