110 lines
3.2 KiB
Rust
110 lines
3.2 KiB
Rust
use nix::fcntl::{fcntl, F_GETFL, F_SETFL, OFlag};
|
|
use nix::pty::{ForkptyResult, forkpty, PtyMaster};
|
|
use nix::unistd::{execv, read, write};
|
|
|
|
use nonblock::NonBlockingReader;
|
|
|
|
use std::vec;
|
|
use std::ffi::CString;
|
|
use std::io;
|
|
|
|
use vte::{Params, Parser, Perform};
|
|
|
|
// yoinked form vte docs
|
|
struct Performer;
|
|
impl Perform for Performer {
|
|
fn print(&mut self, c: char) {
|
|
println!("[print] {:?}", c);
|
|
}
|
|
|
|
fn execute(&mut self, byte: u8) {
|
|
println!("[execute] {:02x}", byte);
|
|
}
|
|
|
|
fn hook(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char) {
|
|
println!(
|
|
"[hook] params={:?}, intermediates={:?}, ignore={:?}, char={:?}",
|
|
params, intermediates, ignore, c
|
|
);
|
|
}
|
|
|
|
fn put(&mut self, byte: u8) {
|
|
println!("[put] {:02x}", byte);
|
|
}
|
|
|
|
fn unhook(&mut self) {
|
|
println!("[unhook]");
|
|
}
|
|
|
|
fn osc_dispatch(&mut self, params: &[&[u8]], bell_terminated: bool) {
|
|
println!("[osc_dispatch] params={:?} bell_terminated={}", params, bell_terminated);
|
|
}
|
|
|
|
fn csi_dispatch(&mut self, params: &Params, intermediates: &[u8], ignore: bool, c: char) {
|
|
println!(
|
|
"[csi_dispatch] params={:#?}, intermediates={:?}, ignore={:?}, char={:?}",
|
|
params, intermediates, ignore, c
|
|
);
|
|
}
|
|
|
|
fn esc_dispatch(&mut self, intermediates: &[u8], ignore: bool, byte: u8) {
|
|
println!(
|
|
"[esc_dispatch] intermediates={:?}, ignore={:?}, byte={:02x}",
|
|
intermediates, ignore, byte
|
|
);
|
|
}
|
|
}
|
|
|
|
fn main() {
|
|
// TODO get rid of hard coded `sh` path
|
|
let pty = spawn_pty("/home/andromeda/.nix-profile/bin/sh").unwrap();
|
|
println!("{:?}", &pty);
|
|
let pty_flags = fcntl(&pty, F_GETFL).unwrap();
|
|
fcntl(&pty, F_SETFL(OFlag::from_bits_truncate(pty_flags) | OFlag::O_NONBLOCK)).unwrap();
|
|
|
|
let stdin = io::stdin();
|
|
let mut nb_stdin = NonBlockingReader::from_fd(stdin).unwrap();
|
|
|
|
let mut statemachine = Parser::new();
|
|
let mut performer = Performer;
|
|
let mut buf = [0u8; 2048];
|
|
let mut vec: Vec<u8> = vec!();
|
|
|
|
loop {
|
|
match read(&pty, &mut buf) {
|
|
Ok(0) => (),
|
|
Ok(n) => statemachine.advance(&mut performer, &buf[..n]),
|
|
Err(_e) => (),
|
|
};
|
|
vec.clear();
|
|
match &nb_stdin.read_available(&mut vec) {
|
|
Ok(0) => (),
|
|
Ok(n) => {
|
|
let un = *n as usize;
|
|
buf[..un].copy_from_slice(&vec[..un]);
|
|
let _ = write(&pty, &buf[..un]);
|
|
},
|
|
Err(e) => println!("error: {:?}", e),
|
|
};
|
|
};
|
|
}
|
|
|
|
// forks a new pty and returns file descriptor of the master
|
|
fn spawn_pty(shell: &str) -> Option<PtyMaster> {
|
|
// SAFETY safe unless os out of PTYs; incredibly unlikely
|
|
match unsafe {forkpty(None, None)} {
|
|
Ok(fork_pty_res) => match fork_pty_res {
|
|
ForkptyResult::Parent {child:_, master} => {
|
|
|
|
// SAFETY `master` is a valid PtyMaster
|
|
return Some(unsafe{PtyMaster::from_owned_fd(master)});
|
|
}
|
|
ForkptyResult::Child =>{
|
|
let _ = execv::<CString>(&CString::new(shell).unwrap(), &[]);
|
|
return None;
|
|
}
|
|
},
|
|
Err(_e) => return None,
|
|
};
|
|
}
|