From 07f8c1715a4e900272de901303ef6a4e28ce4dfa Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Wed, 20 Nov 2019 03:31:16 -0500 Subject: add a binary to help read vt100 streams --- Cargo.lock | 13 ++++++ Cargo.toml | 2 + src/bin/explode.rs | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 src/bin/explode.rs diff --git a/Cargo.lock b/Cargo.lock index a7b7562..30c71d3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -855,6 +855,16 @@ dependencies = [ "tokio-reactor 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ttyrec" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", + "snafu 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-normalization" version = "0.1.9" @@ -906,7 +916,9 @@ dependencies = [ "mio 0.6.19 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-pty-process-stream 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ttyrec 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "vt100 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)", + "vte 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -1068,6 +1080,7 @@ dependencies = [ "checksum tokio-timer 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "f2106812d500ed25a4f38235b9cae8f78a09edf43203e16e59c3b769a342a60e" "checksum tokio-udp 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f02298505547f73e60f568359ef0d016d5acd6e830ab9bc7c4a5b3403440121b" "checksum tokio-uds 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "037ffc3ba0e12a0ab4aca92e5234e0dedeb48fddf6ccd260f1f150a36a9f2445" +"checksum ttyrec 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "24e28759b4efb0e2b5238e1faa979633f80b534659b8559ad8e2e43b8f982c05" "checksum unicode-normalization 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "09c8070a9942f5e7cfccd93f490fdebd230ee3c3c9f107cb25bad5351ef671cf" "checksum unicode-width 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7007dbd421b92cc6e28410fe7362e2e0a2503394908f417b68ec8d1c364c4e20" "checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" diff --git a/Cargo.toml b/Cargo.toml index 01f7194..f8fcb3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,4 +11,6 @@ futures = "0.1.29" mio = "0.6" tokio = "0.1.22" tokio-pty-process-stream = "0.2" +ttyrec = "0.2" vt100 = "0.6" +vte = "0.3" diff --git a/src/bin/explode.rs b/src/bin/explode.rs new file mode 100644 index 0000000..37ed07c --- /dev/null +++ b/src/bin/explode.rs @@ -0,0 +1,134 @@ +use std::io::Read as _; + +#[derive(Default)] +struct Printer { + chars: String, +} + +impl Printer { + fn append(&mut self, c: char) { + self.chars.push(c); + } + + fn flush(&mut self) { + if !self.chars.is_empty() { + println!("TEXT \"{}\"", self.chars); + self.chars.clear(); + } + } +} + +impl vte::Perform for Printer { + fn print(&mut self, c: char) { + self.append(c); + } + + fn execute(&mut self, b: u8) { + self.flush(); + println!("CTRL {}", (b + b'@') as char); + } + + fn esc_dispatch( + &mut self, + _params: &[i64], + intermediates: &[u8], + _ignore: bool, + b: u8, + ) { + self.flush(); + match intermediates.get(0) { + None => { + println!("ESC {}", b as char); + } + Some(i) => { + println!("ESC {} {}", *i as char, b as char); + } + } + } + + fn csi_dispatch( + &mut self, + params: &[i64], + intermediates: &[u8], + _ignore: bool, + c: char, + ) { + self.flush(); + match intermediates.get(0) { + None => { + println!("CSI {} {}", param_str(params), c); + } + Some(i) => { + println!("CSI {} {} {}", *i as char, param_str(params), c); + } + } + } + + fn osc_dispatch(&mut self, params: &[&[u8]]) { + self.flush(); + println!("OSC {}", osc_param_str(params)); + } + + fn hook(&mut self, params: &[i64], intermediates: &[u8], _ignore: bool) { + self.flush(); + match intermediates.get(0) { + None => { + println!("DCS {}", param_str(params)); + } + Some(i) => { + println!("DCS {} {}", *i as char, param_str(params)); + } + } + } + fn put(&mut self, _: u8) {} + fn unhook(&mut self) {} +} + +fn param_str(params: &[i64]) -> String { + let strs: Vec<_> = params + .iter() + .map(std::string::ToString::to_string) + .collect(); + strs.join(" ; ") +} + +fn osc_param_str(params: &[&[u8]]) -> String { + let strs: Vec<_> = params + .iter() + .map(|b| format!("\"{}\"", std::string::String::from_utf8_lossy(*b))) + .collect(); + strs.join(" ; ") +} + +fn main() { + env_logger::from_env( + env_logger::Env::default().default_filter_or("error"), + ) + .init(); + + let mut ttyrec = ttyrec::Parser::new(); + let mut vte = vte::Parser::new(); + let mut printer = Printer::default(); + let mut file = + std::fs::File::open(std::env::args().nth(1).unwrap()).unwrap(); + let mut frame_idx = 1; + + let mut buf = [0; 4096]; + loop { + let n = file.read(&mut buf).unwrap(); + if n == 0 { + break; + } + ttyrec.add_bytes(&buf[..n]); + while let Some(frame) = ttyrec.next_frame() { + if frame_idx > 1 { + println!(); + } + println!("FRAME {}", frame_idx); + frame_idx += 1; + for b in frame.data { + vte.advance(&mut printer, b); + } + } + } +} -- cgit v1.2.3-54-g00ecf