From 65f94bd57bb6983925599969edaed730d210b693 Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Tue, 9 Jun 2020 09:03:08 -0700 Subject: [PATCH] vtparse: fix DCS parsing This corrects an issue where the mode byte of the DCS sequence was discarded from the DcsHook, making it impossible to know what sequence is being activated. So far this hasn't come up as these sequences are relatively rare, but in looking at sixel parsing I noticed the error. --- Cargo.lock | 4 +- tabout/Cargo.toml | 2 +- termwiz/Cargo.toml | 4 +- termwiz/src/escape/mod.rs | 2 + termwiz/src/escape/parser/mod.rs | 41 ++++++++++++++ vtparse/Cargo.toml | 2 +- vtparse/src/lib.rs | 95 +++++++++++++++++++++++++++++++- 7 files changed, 143 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 474009ec7..c8485952c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3258,7 +3258,7 @@ dependencies = [ [[package]] name = "termwiz" -version = "0.9.0" +version = "0.10.0" dependencies = [ "anyhow", "base64 0.10.1", @@ -3564,7 +3564,7 @@ dependencies = [ [[package]] name = "vtparse" -version = "0.2.2" +version = "0.3.0" dependencies = [ "pretty_assertions", "utf8parse", diff --git a/tabout/Cargo.toml b/tabout/Cargo.toml index 0fc93b9d0..7fa820906 100644 --- a/tabout/Cargo.toml +++ b/tabout/Cargo.toml @@ -9,4 +9,4 @@ license = "MIT" documentation = "https://docs.rs/tabout" [dependencies] -termwiz = { path = "../termwiz", version="0.9"} +termwiz = { path = "../termwiz", version="0.10"} diff --git a/termwiz/Cargo.toml b/termwiz/Cargo.toml index c1d876a5d..c701f6c35 100644 --- a/termwiz/Cargo.toml +++ b/termwiz/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Wez Furlong"] name = "termwiz" -version = "0.9.0" +version = "0.10.0" edition = "2018" repository = "https://github.com/wez/wezterm" description = "Terminal Wizardry for Unix and Windows" @@ -32,7 +32,7 @@ terminfo = "0.7" unicode-segmentation = "1.5" unicode-width = "0.1" xi-unicode = "0.2" -vtparse = { version="0.2", path="../vtparse" } +vtparse = { version="0.3", path="../vtparse" } [features] widgets = ["cassowary", "fnv"] diff --git a/termwiz/src/escape/mod.rs b/termwiz/src/escape/mod.rs index b30b21f2a..8775a180e 100644 --- a/termwiz/src/escape/mod.rs +++ b/termwiz/src/escape/mod.rs @@ -57,6 +57,8 @@ pub enum DeviceControlMode { /// active, data is made available to the device mode via /// the `Data` variant. Enter { + /// The final byte in the DCS mode + byte: u8, params: Vec, // TODO: can we just make intermediates a single u8? intermediates: Vec, diff --git a/termwiz/src/escape/parser/mod.rs b/termwiz/src/escape/parser/mod.rs index 6167df6f1..90f6f8f4c 100644 --- a/termwiz/src/escape/parser/mod.rs +++ b/termwiz/src/escape/parser/mod.rs @@ -118,11 +118,13 @@ impl<'a, F: FnMut(Action)> VTActor for Performer<'a, F> { fn dcs_hook( &mut self, + byte: u8, params: &[i64], intermediates: &[u8], ignored_extra_intermediates: bool, ) { (self.callback)(Action::DeviceControl(Box::new(DeviceControlMode::Enter { + byte, params: params.to_vec(), intermediates: intermediates.to_vec(), ignored_extra_intermediates, @@ -301,4 +303,43 @@ mod test { ); assert_eq!(encode(&actions), "\x1b%H"); } + + #[test] + fn sixel() { + let mut p = Parser::new(); + let actions = p.parse_as_vec(b"\x1bP1;2;3;qwat\x1b\\"); + assert_eq!( + vec![ + Action::DeviceControl(Box::new(DeviceControlMode::Enter { + byte: b'q', + params: vec![1, 2, 3], + intermediates: vec![], + ignored_extra_intermediates: false, + })), + Action::DeviceControl(Box::new(DeviceControlMode::Data(b'w'))), + Action::DeviceControl(Box::new(DeviceControlMode::Data(b'a'))), + Action::DeviceControl(Box::new(DeviceControlMode::Data(b't'))), + Action::DeviceControl(Box::new(DeviceControlMode::Exit)), + Action::Esc(Esc::Code(EscCode::StringTerminator)), + ], + actions + ); + + /* + // This is the "HI" example from wikipedia + let mut p = Parser::new(); + let actions = p.parse_as_vec( + b"\x1bPq\ + #0;2;0;0;0#1;2;100;100;0#2;2;0;100;0\ + #1~~@@vv@@~~@@~~$\ + #2??}}GG}}??}}??-\ + #1!14@\ + \x1b\\", + ); + assert_eq!( + vec![Action::Esc(Esc::Code(EscCode::HorizontalTabSet))], + actions + ); + */ + } } diff --git a/vtparse/Cargo.toml b/vtparse/Cargo.toml index ae42a4e34..7dfcc25c0 100644 --- a/vtparse/Cargo.toml +++ b/vtparse/Cargo.toml @@ -1,7 +1,7 @@ [package] authors = ["Wez Furlong "] name = "vtparse" -version = "0.2.2" +version = "0.3.0" edition = "2018" repository = "https://github.com/wez/wezterm" description = "Low level escape sequence parser" diff --git a/vtparse/src/lib.rs b/vtparse/src/lib.rs index 302bf7647..14b8e8a57 100644 --- a/vtparse/src/lib.rs +++ b/vtparse/src/lib.rs @@ -108,6 +108,7 @@ pub trait VTActor { /// for more information on device control strings. fn dcs_hook( &mut self, + mode: u8, params: &[i64], intermediates: &[u8], ignored_excess_intermediates: bool, @@ -175,6 +176,7 @@ pub enum VTAction { params: Vec, intermediates: Vec, ignored_excess_intermediates: bool, + byte: u8, }, DcsPut(u8), DcsUnhook, @@ -228,11 +230,13 @@ impl VTActor for CollectingVTActor { fn dcs_hook( &mut self, + byte: u8, params: &[i64], intermediates: &[u8], ignored_excess_intermediates: bool, ) { self.actions.push(VTAction::DcsHook { + byte, params: params.to_vec(), intermediates: intermediates.to_vec(), ignored_excess_intermediates, @@ -423,6 +427,7 @@ impl VTParser { Action::Hook => { self.finish_param(); actor.dcs_hook( + param, &self.params[0..self.num_params], &self.intermediates[0..self.num_intermediates], self.ignored_excess_intermediates, @@ -555,7 +560,7 @@ impl VTParser { self.action(lookup_exit(self.state), 0, actor); } self.action(action, byte, actor); - self.action(lookup_entry(state), 0, actor); + self.action(lookup_entry(state), byte, actor); self.utf8_return_state = self.state; self.state = state; } else { @@ -787,4 +792,92 @@ mod test { vec![VTAction::Print('\u{af}')] ); } + + #[test] + fn tmux_control() { + assert_eq!( + parse_as_vec("\x1bP1000phello\x1b\\".as_bytes()), + vec![ + VTAction::DcsHook { + byte: b'p', + params: vec![1000], + intermediates: vec![], + ignored_excess_intermediates: false, + }, + VTAction::DcsPut(b'h'), + VTAction::DcsPut(b'e'), + VTAction::DcsPut(b'l'), + VTAction::DcsPut(b'l'), + VTAction::DcsPut(b'o'), + VTAction::DcsUnhook, + VTAction::EscDispatch { + params: vec![], + intermediates: vec![], + ignored_excess_intermediates: false, + byte: b'\\', + } + ] + ); + } + + #[test] + fn tmux_passthru() { + // I'm not convinced that we *should* represent this tmux sequence + // in this way, but it is how it currently maps. + // It's worth noting that we see this as final byte `t` here, which + // collides with decVT105G in https://vt100.net/emu/dcsseq_dec.html + assert_eq!( + parse_as_vec("\x1bPtmux;data\x1b\\".as_bytes()), + vec![ + VTAction::DcsHook { + byte: b't', + params: vec![], + intermediates: vec![], + ignored_excess_intermediates: false, + }, + VTAction::DcsPut(b'm'), + VTAction::DcsPut(b'u'), + VTAction::DcsPut(b'x'), + VTAction::DcsPut(b';'), + VTAction::DcsPut(b'd'), + VTAction::DcsPut(b'a'), + VTAction::DcsPut(b't'), + VTAction::DcsPut(b'a'), + VTAction::DcsUnhook, + VTAction::EscDispatch { + params: vec![], + intermediates: vec![], + ignored_excess_intermediates: false, + byte: b'\\', + } + ] + ); + } + + #[test] + fn sixel() { + assert_eq!( + parse_as_vec("\x1bPqhello\x1b\\".as_bytes()), + vec![ + VTAction::DcsHook { + byte: b'q', + params: vec![], + intermediates: vec![], + ignored_excess_intermediates: false, + }, + VTAction::DcsPut(b'h'), + VTAction::DcsPut(b'e'), + VTAction::DcsPut(b'l'), + VTAction::DcsPut(b'l'), + VTAction::DcsPut(b'o'), + VTAction::DcsUnhook, + VTAction::EscDispatch { + params: vec![], + intermediates: vec![], + ignored_excess_intermediates: false, + byte: b'\\', + } + ] + ); + } }