Connecting custom tty to a Command

So i need to create a custom tty and link it to the stdio & stdin of a spawned command. But i’m not sure how to create a Stdout object using the custom tty object. I am using pty-rs (unmaintained i know) but not sure what the next step should be.

tty:

use pty::fork::Fork;
use failure::Error;
use std::io::{Read, Write};
use std::process::Command;

pub struct Pty {
    fork: Fork
}

impl Pty {
    pub fn new() -> Pty {
        Pty {
            fork: Fork::from_ptmx().expect("cannot connect to fork of pty")
        }

    }

    pub fn stdout(&self) -> Result<String, Error> {
        if let Some(mut master) =  self.fork.is_parent().ok() {

            let mut output = String::new();
            match master.read_to_string(&mut output) {
                Ok(_) => Ok(output),
                Err(e) => panic!(e)
            }
        } else {
            panic!("cannot find parent")
        }


    }
}
Command::new(command)
            .args(args)
            .stdin(Stdio::inherit())
            .stdout(pty.stdout().expect("cannot attach to stdout")) // <-- what goes here?
            .spawn()
            .expect("failed to execute")

You can probably use FromRawFd which is implemented for Stdio.

1 Like

Thanks your suggestion worked. Though it panics with “Bad file descriptor”. Any clues will be helpful.