Spawning a shell into portable_pty #2392
-
|
Hello! I was interested in created an embedded terminal for my own app, and came across Here is the code: use portable_pty::{CommandBuilder, PtySize, PtySystem, NativePtySystem};
use anyhow::Error;
fn main() -> Result<(), Error> {
// Use the native pty implementation for the system
let pty_system = NativePtySystem::default();
// Create a new pty
let mut pair = pty_system.openpty(PtySize {
rows: 24,
cols: 80,
pixel_width: 0,
pixel_height: 0,
})?;
// Spawn a shell into the pty
let command = CommandBuilder::new("bash");
let mut child = pair.slave.spawn_command(command)?;
drop(pair.slave);
// Read and parse output from the pty with reader
let mut reader = pair.master.try_clone_reader()?;
// Send data to the pty by writing to the master
writeln!(pair.master, "ls -l\r\n")?;
drop(pair.master);
let mut out = String::new();
reader.read_to_string(&mut out)?;
println!("{}", out);
println!("{}", child.wait()?);
Ok(())
}System: macOS 12.4 What I want for it to do is for it to enter a bash environment and run Thank you so much! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
|
The issue is that the kernel hasn't flagged the stdin stream as having reached EOF so bash is waiting for the next line of input, so the read_to_string call is blocked waiting for bash to output more or signal EOF. You can force the kernel to recognize EOF by sending EOT ( writeln!(pair.master, "ls -l\r\n\x04")?; |
Beta Was this translation helpful? Give feedback.
-
|
Hello @wez |
Beta Was this translation helpful? Give feedback.
The issue is that the kernel hasn't flagged the stdin stream as having reached EOF so bash is waiting for the next line of input, so the read_to_string call is blocked waiting for bash to output more or signal EOF.
You can force the kernel to recognize EOF by sending EOT (
\x04) at the end of your input: