diff options
Diffstat (limited to 'src/command.rs')
-rw-r--r-- | src/command.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/command.rs b/src/command.rs new file mode 100644 index 0000000..b2d8ef7 --- /dev/null +++ b/src/command.rs @@ -0,0 +1,41 @@ +use crate::error::*; + +use std::os::unix::io::{AsRawFd as _, FromRawFd as _}; + +pub struct Command { + pty: crate::pty::Pty, + command: std::process::Command, +} + +impl Command { + pub fn new<S: std::convert::AsRef<std::ffi::OsStr>>( + program: S, + ) -> Result<Self> { + let pty = crate::pty::Pty::new()?; + let fd = pty.master().as_raw_fd(); + let stdin = unsafe { std::process::Stdio::from_raw_fd(fd) }; + let stdout = unsafe { std::process::Stdio::from_raw_fd(fd) }; + let stderr = unsafe { std::process::Stdio::from_raw_fd(fd) }; + let mut command = std::process::Command::new(program); + command.stdin(stdin).stdout(stdout).stderr(stderr); + Ok(Self { pty, command }) + } + + pub fn pty(&self) -> &std::fs::File { + self.pty.slave() + } +} + +impl std::ops::Deref for Command { + type Target = std::process::Command; + + fn deref(&self) -> &Self::Target { + &self.command + } +} + +impl std::ops::DerefMut for Command { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.command + } +} |