On 2025-12-16 05:13, Corinna Vinschen wrote:
On Dec 10 10:52, Takashi Yano wrote:
This patch fixes the bug in ESC sequence parser used when pseudo
console is enabled in pty_master_fwd_thread(). Previously, if
multiple ESC sequences exist in a fowarding chunk, the later one
might not be processed appropriately. In addition, the termination
ST was not supported, that is, only BEL was supported.

What's ST?  I only know STX, 0x02 in the C0 codeblock.  There's an ST in
the C1 codeblock, 0x9c, "String Terminator", but I don't see this in the
code, nor are the other C1 controls even recognized here.
Some symbolic character function names would help a lot!

ST \E\\ is an alternative to \a <BEL> another ANSI X3.64/FIPS-86/ECMA-48/DEC STD 070/ISO 6429 control sequence with 7/8 bit alternatives like APC, DCS, CSI, OSC, PM:

https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands

"Operating System Commands

OSC Ps ; Pt BEL
OSC Ps ; Pt ST"

https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Control-Bytes_-Characters_-and-Sequences

"o Some controls (such as OSC) introduce a string mode, which is ended on a ST (string terminator)."

https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-C1-lparen-8-Bit-rparen-Control-Characters
"...
        ESC P   Device Control String           (DCS  is 0x90).
...
        ESC [   Control Sequence Introducer     (CSI  is 0x9b).
        ESC \   String Terminator               (ST   is 0x9c).
        ESC ]   Operating System Command        (OSC  is 0x9d).
        ESC ^   Privacy Message                 (PM   is 0x9e).
        ESC _   Application Program Command     (APC  is 0x9f).
..."

ANSI X3.64-1979 FIPS-86 Additional Controls for Use with ANS Code for Information Interchange 1979-07-18

        https://nvlpubs.nist.gov/nistpubs/Legacy/FIPS/fipspub86.pdf

ECMA-48 Control Functions for Coded Character Sets 5th.ed 1991-06

https://ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf

EL-SM070-00 DEC STD 070 Video Systems Reference Manual 1991-12-03

http://www.bitsavers.org/pdf/dec/standards/EL-SM070-00_DEC_STD_070_Video_Systems_Reference_Manual_Dec91.pdf

ISO/IEC 6429:1992 Control functions for coded character sets Ed.3 1992-12

        https://www.iso.org/standard/12782.html

Fixes: 10d083c745dd ("Cygwin: pty: Inherit typeahead data between two input 
pipes.")
Reviewed-by:
Signed-off-by: Takashi Yano <[email protected]>
---
  winsup/cygwin/fhandler/pty.cc | 13 +++++++++----
  1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/winsup/cygwin/fhandler/pty.cc b/winsup/cygwin/fhandler/pty.cc
index 679068ea2..3b0b4f073 100644
--- a/winsup/cygwin/fhandler/pty.cc
+++ b/winsup/cygwin/fhandler/pty.cc
@@ -2680,7 +2680,7 @@ fhandler_pty_master::pty_master_fwd_thread (const 
master_fwd_thread_param_t *p)
          int state = 0;
          int start_at = 0;
          for (DWORD i=0; i<rlen; i++)
-           if (outbuf[i] == '\033')
+           if (state == 0 && outbuf[i] == '\033')
                                        ^ ESC   '\x1b'
As a long former RAD50 and 08 fan can we please move on to hex \x1b now? ;^>
              {
                start_at = i;
                state = 1;
@@ -2688,12 +2688,14 @@ fhandler_pty_master::pty_master_fwd_thread (const 
master_fwd_thread_param_t *p)
              }
            else if ((state == 1 && outbuf[i] == ']') ||
                                                ^ OSC
                     (state == 2 && outbuf[i] == '0') ||
                                                ^ OSC_ICN_NM_WIN_TTL
-                    (state == 3 && outbuf[i] == ';'))
+                    (state == 3 && outbuf[i] == ';') ||
                                                ^ PARAM_SEP
+                    (state == 4 && outbuf[i] == '\033'))
                                                ^ ESC   '\x1b'
              {
                state ++;
                continue;
              }
-           else if (state == 4 && outbuf[i] == '\a')
+           else if ((state == 4 && outbuf[i] == '\a')
                                                ^ BEL
+                    || (state == 5 && outbuf[i] == '\\'))
                                                ^ ST
              {
                const char *helper_str = "\\bin\\cygwin-console-helper.exe";
                if (memmem (&outbuf[start_at], i + 1 - start_at,
@@ -2701,11 +2703,14 @@ fhandler_pty_master::pty_master_fwd_thread (const 
master_fwd_thread_param_t *p)
                  {
                    memmove (&outbuf[start_at], &outbuf[i+1], rlen-i-1);
                    rlen = wlen = start_at + rlen - i - 1;
+                   i = start_at - 1;
                  }
                state = 0;
                continue;
              }
-           else if (outbuf[i] == '\a')
+           else if (state == 4)
+             continue;
+           else
              {
                state = 0;
                continue;
--
2.51.0

[IMHO literals should never appear in code - except *maybe sometimes* 0!
Define all your constants with meaningful names in context before all code,
or in a meaningfully named header with those definitions, if you can't steal them from somewhere with a history, say mintty or xterm sources, or ancient assembler macros, convert them from a document table, say man pages `man -m linux console_codes`, Wikipedia, or xterm (above), or from references therein.]

--
Take care. Thanks, Brian Inglis              Calgary, Alberta, Canada

La perfection est atteinte                   Perfection is achieved
non pas lorsqu'il n'y a plus rien à ajouter  not when there is no more to add
mais lorsqu'il n'y a plus rien à retrancher  but when there is no more to cut
                                -- Antoine de Saint-Exupéry

Reply via email to