diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-28 03:33:52 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-28 05:28:28 -0500 |
commit | f8780ca1e76286688b74d8a6c64d5fadf3cfd2a1 (patch) | |
tree | b1e0fe6a378f3a8810e0332ca572a86185fd556c /examples | |
parent | b181b63a69d5db78769c1c3723a9940f66491466 (diff) | |
download | pty-process-f8780ca1e76286688b74d8a6c64d5fadf3cfd2a1.tar.gz pty-process-f8780ca1e76286688b74d8a6c64d5fadf3cfd2a1.zip |
wip
Diffstat (limited to 'examples')
-rw-r--r-- | examples/async-std.rs | 26 | ||||
-rw-r--r-- | examples/basic.rs | 28 | ||||
-rw-r--r-- | examples/interhack.rs | 18 | ||||
-rw-r--r-- | examples/smol.rs | 27 | ||||
-rw-r--r-- | examples/tokio.rs | 19 |
5 files changed, 64 insertions, 54 deletions
diff --git a/examples/async-std.rs b/examples/async-std.rs index 6e1b941..880c91b 100644 --- a/examples/async-std.rs +++ b/examples/async-std.rs @@ -7,7 +7,7 @@ mod main { use async_std::prelude::FutureExt as _; pub async fn run( - child: &pty_process::async_std::Child, + child: &mut pty_process::async_std::Child, ) -> std::result::Result<(), Box<dyn std::error::Error + '_>> { let _raw = super::raw_guard::RawGuard::new(); @@ -38,18 +38,18 @@ mod main { stdout.flush().await.unwrap(); } Err(e) => { - // EIO means that the process closed the other - // end of the pty - if e.raw_os_error() != Some(libc::EIO) { - eprintln!("pty read failed: {:?}", e); - } + eprintln!("pty read failed: {:?}", e); break; } } } }); - ex.run(input.race(output)).await; + let wait = async { + child.status_no_drop().await.unwrap(); + }; + + ex.run(input.race(output).race(wait)).await; Ok(()) } @@ -57,15 +57,15 @@ mod main { #[cfg(feature = "backend-async-std")] fn main() { - use pty_process::Command as _; use std::os::unix::process::ExitStatusExt as _; let status = async_std::task::block_on(async { - let mut child = async_std::process::Command::new("sleep") - .args(&["500"]) - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); - main::run(&child).await.unwrap(); + let pty = pty_process::async_std::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd = pty_process::async_std::Command::new("tac"); + // cmd.args(&["500"]); + let mut child = cmd.spawn(pty).unwrap(); + main::run(&mut child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( diff --git a/examples/basic.rs b/examples/basic.rs index bee6758..1e5913b 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -5,7 +5,7 @@ mod main { use std::io::{Read as _, Write as _}; use std::os::unix::io::AsRawFd as _; - pub fn run(child: &pty_process::std::Child) { + pub fn run(child: &mut pty_process::std::Child) { let _raw = super::raw_guard::RawGuard::new(); let mut buf = [0_u8; 4096]; let pty = child.pty().as_raw_fd(); @@ -34,11 +34,7 @@ mod main { stdout.flush().unwrap(); } Err(e) => { - // EIO means that the process closed the other - // end of the pty - if e.raw_os_error() != Some(libc::EIO) { - eprintln!("pty read failed: {:?}", e); - } + eprintln!("pty read failed: {:?}", e); break; } }; @@ -62,21 +58,29 @@ mod main { break; } } + match child.try_wait() { + Ok(Some(_)) => break, + Ok(None) => {} + Err(e) => { + println!("wait failed: {:?}", e); + break; + } + } } } } #[cfg(feature = "backend-std")] fn main() { - use pty_process::Command as _; use std::os::unix::process::ExitStatusExt as _; - let mut child = std::process::Command::new("sleep") - .args(&["500"]) - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); + let pty = pty_process::std::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd = pty_process::std::Command::new("tac"); + // cmd.args(&["500"]); + let mut child = cmd.spawn(pty).unwrap(); - main::run(&child); + main::run(&mut child); let status = child.wait().unwrap(); std::process::exit( diff --git a/examples/interhack.rs b/examples/interhack.rs index 8caebb4..e636282 100644 --- a/examples/interhack.rs +++ b/examples/interhack.rs @@ -5,7 +5,7 @@ mod main { use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; pub async fn run( - child: &pty_process::smol::Child, + child: &mut pty_process::smol::Child, ) -> std::result::Result<(), Box<dyn std::error::Error>> { let _raw = super::raw_guard::RawGuard::new(); @@ -82,7 +82,12 @@ mod main { } }); - ex.run(smol::future::or(input, output)).await; + let wait = async { + child.status_no_drop().await.unwrap(); + }; + + ex.run(smol::future::or(smol::future::or(input, output), wait)) + .await; Ok(()) } @@ -90,7 +95,6 @@ mod main { #[cfg(feature = "backend-smol")] fn main() { - use pty_process::Command as _; use std::os::unix::process::ExitStatusExt as _; let (w, h) = if let Some((w, h)) = term_size::dimensions() { @@ -99,10 +103,12 @@ fn main() { (80, 24) }; let status = smol::block_on(async { - let mut child = smol::process::Command::new("nethack") - .spawn_pty(Some(&pty_process::Size::new(h, w))) + let pty = pty_process::smol::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(h, w)).unwrap(); + let mut child = pty_process::smol::Command::new("nethack") + .spawn(pty) .unwrap(); - main::run(&child).await.unwrap(); + main::run(&mut child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( diff --git a/examples/smol.rs b/examples/smol.rs index d519fbe..b0b90e4 100644 --- a/examples/smol.rs +++ b/examples/smol.rs @@ -5,7 +5,7 @@ mod main { use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; pub async fn run( - child: &pty_process::smol::Child, + child: &mut pty_process::smol::Child, ) -> std::result::Result<(), Box<dyn std::error::Error>> { let _raw = super::raw_guard::RawGuard::new(); @@ -36,18 +36,19 @@ mod main { stdout.flush().await.unwrap(); } Err(e) => { - // EIO means that the process closed the other - // end of the pty - if e.raw_os_error() != Some(libc::EIO) { - eprintln!("pty read failed: {:?}", e); - } + eprintln!("pty read failed: {:?}", e); break; } } } }); - ex.run(smol::future::or(input, output)).await; + let wait = async { + child.status_no_drop().await.unwrap(); + }; + + ex.run(smol::future::or(smol::future::or(input, output), wait)) + .await; Ok(()) } @@ -55,15 +56,15 @@ mod main { #[cfg(feature = "backend-smol")] fn main() { - use pty_process::Command as _; use std::os::unix::process::ExitStatusExt as _; let status = smol::block_on(async { - let mut child = smol::process::Command::new("sleep") - .args(&["500"]) - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); - main::run(&child).await.unwrap(); + let pty = pty_process::smol::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd = pty_process::smol::Command::new("tac"); + // cmd.args(&["500"]); + let mut child = cmd.spawn(pty).unwrap(); + main::run(&mut child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( diff --git a/examples/tokio.rs b/examples/tokio.rs index 0dc007f..bc62592 100644 --- a/examples/tokio.rs +++ b/examples/tokio.rs @@ -32,14 +32,13 @@ mod main { stdout.flush().await.unwrap(); } Err(e) => { - // EIO means that the process closed the other - // end of the pty - if e.raw_os_error() != Some(libc::EIO) { - eprintln!("pty read failed: {:?}", e); - } + eprintln!("pty read failed: {:?}", e); break; } }, + // _ = child.status_no_drop() => { + // break; + // } } } @@ -50,13 +49,13 @@ mod main { #[cfg(feature = "backend-tokio")] #[tokio::main] async fn main() { - use pty_process::Command as _; use std::os::unix::process::ExitStatusExt as _; - let mut child = tokio::process::Command::new("sleep") - .args(&["500"]) - .spawn_pty(Some(&pty_process::Size::new(24, 80))) - .unwrap(); + let pty = pty_process::tokio::Pty::new().unwrap(); + pty.resize(pty_process::Size::new(24, 80)).unwrap(); + let mut cmd = pty_process::tokio::Command::new("tac"); + // cmd.args(&["500"]); + let mut child = cmd.spawn(pty).unwrap(); main::run(&mut child).await.unwrap(); let status = child.wait().await.unwrap(); std::process::exit( |