nonblocking, good stuff w/ vte :)

This commit is contained in:
andromeda
2026-01-17 00:40:22 +01:00
parent eedc84d871
commit 2fee948de1
3 changed files with 40 additions and 18 deletions

View File

@@ -1,14 +1,16 @@
use nix::fcntl::{fcntl, F_GETFL, F_SETFL, OFlag};
use nix::pty::{ForkptyResult, forkpty, PtyMaster};
use nix::unistd::read;
use nix::unistd::{execv, read, write};
use std::vec::Vec;
use std::io::{self, Read, Write};
use std::process::Command;
use std::thread;
use std::time::Duration;
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) {
@@ -54,25 +56,35 @@ impl Perform for Performer {
}
fn main() {
// TODO get rid of hard coded `sh` path
let mut pty = spawn_pty("/home/andromeda/.nix-profile/bin/sh").unwrap();
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 input = io::stdin();
let mut handle = input.lock();
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!();
while true {
loop {
match read(&pty, &mut buf) {
Ok(0) => break,
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) => {
statemachine.advance(&mut performer, &buf[..n]);
let un = *n as usize;
buf[..un].copy_from_slice(&vec[..un]);
let _ = write(&pty, &buf[..un]);
},
Err(_e) => break,
Err(e) => println!("error: {:?}", e),
};
};
}
@@ -82,14 +94,13 @@ 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 {master, ..} => {
ForkptyResult::Parent {child:_, master} => {
// SAFETY `master` is a valid PtyMaster
return Some(unsafe{PtyMaster::from_owned_fd(master)});
}
ForkptyResult::Child =>{
Command::new(shell).spawn();
// TODO come up with an elegant solution
thread::sleep(Duration::MAX);
let _ = execv::<CString>(&CString::new(shell).unwrap(), &[]);
return None;
}
},