Repository navigation
Parse VT100 ECMA-48 Control Strings: DCS, APC, PM, SOS - #644
Conversation
|
@basil, could you please review this? |
basil
left a comment
There was a problem hiding this comment.
A promising start. This part of the codebase needs a lot more work to support C1 codepoints and CAN/SUB abort, and it would probably benefit from a rewrite to use an explicit parser state machine like modern Konsole, but none of these changes are in scope for this PR.
The current narrowly scoped PR correctly extends the OSC string-consumption path to DCS/APC/PM/SOS, modulo a few issues which should be addressed before merge.
| #define esp( ) (p == 4 && s[3] == ' ') | ||
| #define Xpe (tokenBufferPos >= 2 && tokenBuffer[1] == ']') | ||
| #define Xpe (tokenBufferPos >= 2 && (tokenBuffer[1] == ']' || tokenBuffer[1] == 'P' || tokenBuffer[1] == '_' || tokenBuffer[1] == '^' || tokenBuffer[1] == 'X')) | ||
| #define Xte (Xpe && (cc == 7 || (prevCC == 27 && cc == 92) )) // 27, 92 => "\e\\" (ST, String Terminator) |
There was a problem hiding this comment.
Xte accepts BEL (0x07) as a terminator for every string type. BEL as a string terminator is an xterm convention valid only for OSC. DCS, APC, PM, and SOS terminate on ST only.
Because this PR routes all five string types through Xte, a DCS/APC/PM/SOS string whose payload contains a 0x07 byte is truncated at the BEL. The post-BEL bytes are printed to the screen and the trailing ESC becomes a stray ST, which is the behavior this PR is trying to eliminate. Some examples would be SOS payloads (ECMA-48 character string permits arbitrary bytes) and DCS passthrough (e.g. tmux ESC P tmux).
The fix is to gate BEL to OSC only. ST would still terminate all five types. BEL inside a non-OSC string would then fall through to the existing if (Xpe) { prevCC = cc; return; } path and be consumed as payload. The tokenBuffer[1] == ']' guard already present in the Xte branch would keep processWindowAttributeChange() OSC-only.
| #define egt( ) (p >= 3 && s[2] == '>') | ||
| #define esp( ) (p == 4 && s[3] == ' ') | ||
| #define Xpe (tokenBufferPos >= 2 && tokenBuffer[1] == ']') | ||
| #define Xpe (tokenBufferPos >= 2 && (tokenBuffer[1] == ']' || tokenBuffer[1] == 'P' || tokenBuffer[1] == '_' || tokenBuffer[1] == '^' || tokenBuffer[1] == 'X')) |
There was a problem hiding this comment.
This macro name Xpe still describes OSC exclusively, even though Xpe now matches five string initiators.
There was a problem hiding this comment.
This comment was accurate before this PR, when Xpe was defined solely as tokenBuffer[1] == ']'. After this PR, Xpe also matches P (DCS), _ (APC), ^ (PM), and X (SOS). So both the "(aka OSC)" parenthetical and the "ESC]" example now describe only one of the five string types the branch handles.
|
@basil |
Problem ------- QTerminal echoes raw bytes of unrecognized but valid ECMA-48 Control escape sequences (DCS, APC, PM, SOS) to the screen instead of silently consuming them. For example, XTGETTCAP queries are visible https://codeberg.org/dnkl/foot#xtgettcap which may be used by TUI/CLI apps to determine 24-bit color support, OSC 52 clipboard support, colored underline, etc. These are meant for the terminal emulator, not for display output. Solution -------- - Add `P` (DCS), `_` (APC), `^` (PM), `X` (SOS) to the `GRP` character class so they enter string mode - Extend the `Xpe` macro to recognize all five string initiators. - Guard `processWindowAttributeChange()` (OSC title/attribute handler) so it only fires for OSC strings; non-OSC strings are silently discarded. Testing ------- Here is a demonstration script, and its before and after output:: #!/bin/bash # ECMA-48 string tests ESC=$'\x1b' ST="${ESC}\\" BEL=$'\x07' send_seq() { # $1 = display label, $2 = initiator char (] P _ ^ X), $3 = payload local label="$1" init="$2" data="$3" term printf '%-3s: ' "$label" for term in ST BEL; do local term_str case $term in ST) term_str="$ST";; BEL) term_str="$BEL";; esac printf '%s%s%s%s' "$ESC" "$init" "$data" "$term_str" done echo } echo "Each sequence is sent twice using both kinds of terminators" echo "ST (ESC \\) and BEL (\\x07). If the fix works **nothing** is displayed after ':'" echo "" send_seq "OSC" ']' '0;TEST_OSC' send_seq "DCS" 'P' '+qTEST_DCS' send_seq "APC" '_' 'TEST_APC' send_seq "PM " '^' 'TEST_PM' send_seq "SOS" 'X' 'TEST_SOS' Details ------- From https://ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf > 5.6 Control strings > A control string is a string of bit combinations which may occur in the data stream as a logical entity for > control purposes. A control string consists of an opening delimiter, a command string or a character string, > and a terminating delimiter, the STRING TERMINATOR (ST). > > [..] > > a) APPLICATION PROGRAM COMMAND (APC) > b) DEVICE CONTROL STRING (DCS) > c) OPERATING SYSTEM COMMAND (OSC) > d) PRIVACY MESSAGE (PM) > e) START OF STRING (SOS) (OSC already implemented) These are all described by XTerm as VT100 Mode Controls beginning with ESC, APC - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Application-Program-Command-functions DCS - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Device-Control-functions PM - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Privacy-Message and SOS as a C1 (8-Bit) Control character, SOS - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-C1-lparen-8-Bit-rparen-Control-Characters
93a3d63 to
6445b19
Compare
6445b19 to
6a2da8f
Compare
|
I've addressed feedback @basil, I've also done further research on terminating sequences, with testing of the following script of missing ending terminators until reset. On HEAD, this script appears to "lock up" the terminal until/unless a BEL is emitted, fixed by 6a2da8f. This matches Konsole's behavior to reset/clear on ESC in intermediary bytes Screenshots: |
|
The |
can't seem fix the parser from crashing without a major refactor
|
Also, I'm not sure if you're suggesting |
|
The ST regression I described is fixed in this code. The remaining issue I still see is only the stale
That name made sense while To keep the local three-letter macro style, I'd rename:
That keeps the existing compact style while making the broadened behavior explicit. |
|
Renamed as requested 310c311 |
|
Thanks! LGTM |
|
@basil And welcome to LXQt as a member! |
basil
left a comment
There was a problem hiding this comment.
I tested this locally and can confirm that this PR correctly makes DCS, APC, PM, and SOS behave like control strings for well-formed input.
qtermwidget's existing (bad) behavior of hanging on unterminated OSC now also applies to these newly-recognized string types. While this is a real behavioral change, it is not a new parser bug. Rather, this PR exposes an existing limitation in qtermwidget's tokenizer. Once the tokenizer enters an unterminated control string, reset/abort handling is not robust.
The proper long-term fix is an explicit parser state machine with well-defined handling for ST, CAN, SUB, C1 controls, and reset behavior, but that is outside the scope of this PR. More work remains, but this PR is a net improvement over the status quo. 👍 Thanks!
qtermwidget only treated OSC as an ECMA-48 control string. Other valid string types (DCS, APC, PM, and SOS) were parsed as ordinary escape sequences, causing their payload bytes to leak onto the terminal display. This is visible with sequences such as XTGETTCAP, which terminal applications may use to query capabilities like true color, OSC 52 clipboard support, or underline styling. Extend the existing control-string handling path to recognize DCS, APC, PM, and SOS in addition to OSC. ST terminates all five string types, while BEL remains an OSC-only terminator. The OSC title/window-attribute handler is still guarded so it only runs for OSC; other control strings are consumed silently. This keeps qtermwidget's existing tokenizer model. Unterminated control strings can still leave the parser stuck until a valid terminator is seen; that limitation already existed for OSC and needs a separate parser-state refactor to fix properly. (cherry picked from commit adf8cf2)




Problem
QTerminal echoes raw bytes of unrecognized but valid ECMA-48 Control escape sequences (DCS, APC, PM, SOS) to the screen instead of silently consuming them.
For example, XTGETTCAP queries are visible, which may be used by TUI/CLI apps to determine colors/24-bit color support, OSC 52 clipboard, underline, keyboard sequences, etc.
These are meant for the terminal emulator, not for display output.
Solution
'P'(DCS),'_'(APC),'^'(PM), and'X'(SOS) to theGRPcharacter class so they enter string modeXpemacro to recognize all five string initiators.processWindowAttributeChange()(OSC title/attribute handler) so it only fires for OSC strings; non-OSC strings are silently discarded.Testing
Here is a demonstration script, and its before and after output::
Details
From https://ecma-international.org/wp-content/uploads/ECMA-48_5th_edition_june_1991.pdf
(OSC already implemented)
These are all described by XTerm as VT100 Mode Controls beginning with ESC,
APC - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Application-Program-Command-functions
DCS - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Device-Control-functions
PM - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Privacy-Message
and SOS as a C1 (8-Bit) Control character,
SOS - https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-C1-lparen-8-Bit-rparen-Control-Characters