Skip to content

Commit

Permalink
fix(term): ansi: allow ESC in DCS string state
Browse files Browse the repository at this point in the history
Allow ESC character during DCS string collection state. We use ECMA-48
string parameters format to build DCS sequences. However, some
applications and multiplexers like screen and tmux, don't implement this
format and only pass data through DCS sequences. This data can include
ESC characters so we need to make a special case to accept that as part
of the data string.
  • Loading branch information
aymanbagabas committed Apr 8, 2024
1 parent ad80716 commit cff6f7e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
15 changes: 14 additions & 1 deletion exp/term/ansi/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func NewParser(paramsSize, dataSize int) *Parser {

// Reset resets the parser to its initial state.
func (p *Parser) Reset() {
p.clear()
p.State = parser.GroundState
}

// clear clears the parser parameters and command.
func (p *Parser) clear() {
if len(p.Params) > 0 {
p.Params[0] = parser.MissingParam
}
Expand Down Expand Up @@ -147,6 +153,13 @@ func (p *Parser) advance(d ParserDispatcher, b byte, more bool) parser.Action {
case parser.EscapeState:
p.performAction(d, parser.ClearAction, b)
}
if action == parser.PutAction &&
p.State == parser.DcsEntryState && state == parser.DcsStringState {
// XXX: This is a special case where we need to start collecting
// non-string parameterized data i.e. doesn't follow the ECMA-48 §
// 5.4.1 string parameters format.
p.performAction(d, parser.StartAction, 0)
}
}

// Handle special cases
Expand Down Expand Up @@ -194,7 +207,7 @@ func (p *Parser) performAction(dispatcher ParserDispatcher, action parser.Action
break

case parser.ClearAction:
p.Reset()
p.clear()

case parser.PrintAction:
if utf8ByteLen(b) > 1 {
Expand Down
7 changes: 6 additions & 1 deletion exp/term/ansi/parser/transition_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ func GenerateTransitionTable() TransitionTable {
}

// Dcs_entry
table.AddRange(0x00, 0x17, DcsEntryState, IgnoreAction, DcsEntryState)
table.AddRange(0x00, 0x07, DcsEntryState, IgnoreAction, DcsEntryState)
table.AddRange(0x0E, 0x17, DcsEntryState, IgnoreAction, DcsEntryState)
table.AddOne(0x19, DcsEntryState, IgnoreAction, DcsEntryState)
table.AddRange(0x1C, 0x1F, DcsEntryState, IgnoreAction, DcsEntryState)
table.AddOne(0x7F, DcsEntryState, IgnoreAction, DcsEntryState)
Expand All @@ -175,6 +176,10 @@ func GenerateTransitionTable() TransitionTable {
table.AddRange(0x30, 0x3B, DcsEntryState, ParamAction, DcsParamState)
table.AddRange(0x3C, 0x3F, DcsEntryState, MarkerAction, DcsParamState)
// Dcs_entry -> Dcs_passthrough
table.AddRange(0x08, 0x0D, DcsEntryState, PutAction, DcsStringState) // Follows ECMA-48 § 8.3.27
// XXX: allows passing ESC (not a ECMA-48 standard) this to allow for
// passthrough of ANSI sequences like in Screen or Tmux passthrough mode.
table.AddOne(0x1B, DcsEntryState, PutAction, DcsStringState)
table.AddRange(0x40, 0x7E, DcsEntryState, StartAction, DcsStringState)

// Dcs_intermediate
Expand Down

0 comments on commit cff6f7e

Please sign in to comment.