From e9a976fba8da44c298756e189180d7fe061fe7ba Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 6 Mar 2021 01:13:42 -0500 Subject: make plain `cargo test` work --- examples/async-std.rs | 98 ++++++++++++++++-------------- examples/basic.rs | 116 +++++++++++++++++++---------------- examples/interhack.rs | 152 +++++++++++++++++++++++++--------------------- examples/raw_guard/mod.rs | 1 + examples/smol.rs | 94 +++++++++++++++------------- examples/tokio.rs | 94 +++++++++++++++------------- 6 files changed, 307 insertions(+), 248 deletions(-) diff --git a/examples/async-std.rs b/examples/async-std.rs index b521549..6e1b941 100644 --- a/examples/async-std.rs +++ b/examples/async-std.rs @@ -1,66 +1,71 @@ -use async_std::io::prelude::WriteExt as _; -use async_std::io::ReadExt as _; -use async_std::prelude::FutureExt as _; -use pty_process::Command as _; -use std::os::unix::process::ExitStatusExt as _; - mod raw_guard; -async fn run( - child: &pty_process::async_std::Child, -) -> std::result::Result<(), Box> { - let _raw = raw_guard::RawGuard::new(); +#[cfg(feature = "backend-async-std")] +mod main { + use async_std::io::prelude::WriteExt as _; + use async_std::io::ReadExt as _; + use async_std::prelude::FutureExt as _; - let ex = async_executor::Executor::new(); + pub async fn run( + child: &pty_process::async_std::Child, + ) -> std::result::Result<(), Box> { + let _raw = super::raw_guard::RawGuard::new(); - let input = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdin = async_std::io::stdin(); - loop { - match stdin.read(&mut buf).await { - Ok(bytes) => { - child.pty().write_all(&buf[..bytes]).await.unwrap(); - } - Err(e) => { - eprintln!("stdin read failed: {:?}", e); - break; + let ex = async_executor::Executor::new(); + + let input = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdin = async_std::io::stdin(); + loop { + match stdin.read(&mut buf).await { + Ok(bytes) => { + child.pty().write_all(&buf[..bytes]).await.unwrap(); + } + Err(e) => { + eprintln!("stdin read failed: {:?}", e); + break; + } } } - } - }); - let output = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdout = async_std::io::stdout(); - loop { - match child.pty().read(&mut buf).await { - Ok(bytes) => { - stdout.write_all(&buf[..bytes]).await.unwrap(); - 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); + }); + let output = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdout = async_std::io::stdout(); + loop { + match child.pty().read(&mut buf).await { + Ok(bytes) => { + stdout.write_all(&buf[..bytes]).await.unwrap(); + 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); + } + break; } - break; } } - } - }); + }); - ex.run(input.race(output)).await; + ex.run(input.race(output)).await; - Ok(()) + Ok(()) + } } +#[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(); - run(&child).await.unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -69,3 +74,8 @@ fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } + +#[cfg(not(feature = "backend-async-std"))] +fn main() { + unimplemented!() +} diff --git a/examples/basic.rs b/examples/basic.rs index 97bdd46..bee6758 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -1,73 +1,82 @@ -use std::io::{Read as _, Write as _}; -use std::os::unix::io::AsRawFd as _; -use std::os::unix::process::ExitStatusExt as _; - -use pty_process::Command as _; - mod raw_guard; -fn run(child: &pty_process::std::Child) { - let _raw = raw_guard::RawGuard::new(); - let mut buf = [0_u8; 4096]; - let pty = child.pty().as_raw_fd(); - let stdin = std::io::stdin().as_raw_fd(); +#[cfg(feature = "backend-std")] +mod main { + use std::io::{Read as _, Write as _}; + use std::os::unix::io::AsRawFd as _; - loop { - let mut set = nix::sys::select::FdSet::new(); - set.insert(pty); - set.insert(stdin); - match nix::sys::select::select(None, Some(&mut set), None, None, None) - { - Ok(n) => { - if n > 0 { - if set.contains(pty) { - match child.pty().read(&mut buf) { - Ok(bytes) => { - let buf = &buf[..bytes]; - let stdout = std::io::stdout(); - let mut stdout = stdout.lock(); - stdout.write_all(buf).unwrap(); - 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); + pub fn run(child: &pty_process::std::Child) { + let _raw = super::raw_guard::RawGuard::new(); + let mut buf = [0_u8; 4096]; + let pty = child.pty().as_raw_fd(); + let stdin = std::io::stdin().as_raw_fd(); + + loop { + let mut set = nix::sys::select::FdSet::new(); + set.insert(pty); + set.insert(stdin); + match nix::sys::select::select( + None, + Some(&mut set), + None, + None, + None, + ) { + Ok(n) => { + if n > 0 { + if set.contains(pty) { + match child.pty().read(&mut buf) { + Ok(bytes) => { + let buf = &buf[..bytes]; + let stdout = std::io::stdout(); + let mut stdout = stdout.lock(); + stdout.write_all(buf).unwrap(); + 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); + } + break; + } + }; + } + if set.contains(stdin) { + match std::io::stdin().read(&mut buf) { + Ok(bytes) => { + let buf = &buf[..bytes]; + child.pty().write_all(buf).unwrap(); + } + Err(e) => { + eprintln!("stdin read failed: {:?}", e); + break; } - break; - } - }; - } - if set.contains(stdin) { - match std::io::stdin().read(&mut buf) { - Ok(bytes) => { - let buf = &buf[..bytes]; - child.pty().write_all(buf).unwrap(); - } - Err(e) => { - eprintln!("stdin read failed: {:?}", e); - break; } } } } - } - Err(e) => { - println!("select failed: {:?}", e); - break; + Err(e) => { + println!("select 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(); - run(&child); + main::run(&child); let status = child.wait().unwrap(); std::process::exit( @@ -76,3 +85,8 @@ fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } + +#[cfg(not(feature = "backend-std"))] +fn main() { + unimplemented!() +} diff --git a/examples/interhack.rs b/examples/interhack.rs index 747c0c9..8caebb4 100644 --- a/examples/interhack.rs +++ b/examples/interhack.rs @@ -1,89 +1,98 @@ -use pty_process::Command as _; -use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; -use std::os::unix::process::ExitStatusExt as _; - mod raw_guard; -async fn run( - child: &pty_process::smol::Child, -) -> std::result::Result<(), Box> { - let _raw = raw_guard::RawGuard::new(); +#[cfg(feature = "backend-smol")] +mod main { + use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; + + pub async fn run( + child: &pty_process::smol::Child, + ) -> std::result::Result<(), Box> { + let _raw = super::raw_guard::RawGuard::new(); - let ex = smol::Executor::new(); + let ex = smol::Executor::new(); - let input = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdin = smol::Unblock::new(std::io::stdin()); - loop { - match stdin.read(&mut buf).await { - Ok(bytes) => { - // engrave Elbereth with ^E - if buf[..bytes].contains(&5u8) { - for byte in buf[..bytes].iter() { - match byte { - 5u8 => child - .pty() - .write_all(b"E- Elbereth\n") - .await - .unwrap(), - _ => child - .pty() - .write_all(&[*byte]) - .await - .unwrap(), + let input = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdin = smol::Unblock::new(std::io::stdin()); + loop { + match stdin.read(&mut buf).await { + Ok(bytes) => { + // engrave Elbereth with ^E + if buf[..bytes].contains(&5u8) { + for byte in buf[..bytes].iter() { + match byte { + 5u8 => child + .pty() + .write_all(b"E- Elbereth\n") + .await + .unwrap(), + _ => child + .pty() + .write_all(&[*byte]) + .await + .unwrap(), + } } + } else { + child + .pty() + .write_all(&buf[..bytes]) + .await + .unwrap(); } - } else { - child.pty().write_all(&buf[..bytes]).await.unwrap(); } - } - Err(e) => { - eprintln!("stdin read failed: {:?}", e); - break; + Err(e) => { + eprintln!("stdin read failed: {:?}", e); + break; + } } } - } - }); - let output = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdout = smol::Unblock::new(std::io::stdout()); - #[allow(clippy::trivial_regex)] - let re = regex::bytes::Regex::new("Elbereth").unwrap(); - loop { - match child.pty().read(&mut buf).await { - Ok(bytes) => { - // highlight successful Elbereths - if re.is_match(&buf[..bytes]) { - stdout - .write_all(&re.replace_all( - &buf[..bytes], - &b"\x1b[35m$0\x1b[m"[..], - )) - .await - .unwrap(); - } else { - stdout.write_all(&buf[..bytes]).await.unwrap(); + }); + let output = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdout = smol::Unblock::new(std::io::stdout()); + #[allow(clippy::trivial_regex)] + let re = regex::bytes::Regex::new("Elbereth").unwrap(); + loop { + match child.pty().read(&mut buf).await { + Ok(bytes) => { + // highlight successful Elbereths + if re.is_match(&buf[..bytes]) { + stdout + .write_all(&re.replace_all( + &buf[..bytes], + &b"\x1b[35m$0\x1b[m"[..], + )) + .await + .unwrap(); + } else { + stdout.write_all(&buf[..bytes]).await.unwrap(); + } + stdout.flush().await.unwrap(); } - 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); + 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); + } + break; } - break; } } - } - }); + }); - ex.run(smol::future::or(input, output)).await; + ex.run(smol::future::or(input, output)).await; - Ok(()) + Ok(()) + } } +#[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() { (w as u16, h as u16) } else { @@ -93,7 +102,7 @@ fn main() { let mut child = smol::process::Command::new("nethack") .spawn_pty(Some(&pty_process::Size::new(h, w))) .unwrap(); - run(&child).await.unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -102,3 +111,8 @@ fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } + +#[cfg(not(feature = "backend-smol"))] +fn main() { + unimplemented!() +} diff --git a/examples/raw_guard/mod.rs b/examples/raw_guard/mod.rs index 2930a08..cb6a7d6 100644 --- a/examples/raw_guard/mod.rs +++ b/examples/raw_guard/mod.rs @@ -5,6 +5,7 @@ pub struct RawGuard { } impl RawGuard { + #[allow(dead_code)] pub fn new() -> Self { let stdin = std::io::stdin().as_raw_fd(); let termios = nix::sys::termios::tcgetattr(stdin).unwrap(); diff --git a/examples/smol.rs b/examples/smol.rs index d7b816f..d519fbe 100644 --- a/examples/smol.rs +++ b/examples/smol.rs @@ -1,64 +1,69 @@ -use pty_process::Command as _; -use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; -use std::os::unix::process::ExitStatusExt as _; - mod raw_guard; -async fn run( - child: &pty_process::smol::Child, -) -> std::result::Result<(), Box> { - let _raw = raw_guard::RawGuard::new(); +#[cfg(feature = "backend-smol")] +mod main { + use smol::io::{AsyncReadExt as _, AsyncWriteExt as _}; - let ex = smol::Executor::new(); + pub async fn run( + child: &pty_process::smol::Child, + ) -> std::result::Result<(), Box> { + let _raw = super::raw_guard::RawGuard::new(); - let input = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdin = smol::Unblock::new(std::io::stdin()); - loop { - match stdin.read(&mut buf).await { - Ok(bytes) => { - child.pty().write_all(&buf[..bytes]).await.unwrap(); - } - Err(e) => { - eprintln!("stdin read failed: {:?}", e); - break; + let ex = smol::Executor::new(); + + let input = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdin = smol::Unblock::new(std::io::stdin()); + loop { + match stdin.read(&mut buf).await { + Ok(bytes) => { + child.pty().write_all(&buf[..bytes]).await.unwrap(); + } + Err(e) => { + eprintln!("stdin read failed: {:?}", e); + break; + } } } - } - }); - let output = ex.spawn(async { - let mut buf = [0_u8; 4096]; - let mut stdout = smol::Unblock::new(std::io::stdout()); - loop { - match child.pty().read(&mut buf).await { - Ok(bytes) => { - stdout.write_all(&buf[..bytes]).await.unwrap(); - 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); + }); + let output = ex.spawn(async { + let mut buf = [0_u8; 4096]; + let mut stdout = smol::Unblock::new(std::io::stdout()); + loop { + match child.pty().read(&mut buf).await { + Ok(bytes) => { + stdout.write_all(&buf[..bytes]).await.unwrap(); + 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); + } + break; } - break; } } - } - }); + }); - ex.run(smol::future::or(input, output)).await; + ex.run(smol::future::or(input, output)).await; - Ok(()) + Ok(()) + } } +#[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(); - run(&child).await.unwrap(); + main::run(&child).await.unwrap(); child.status().await.unwrap() }); std::process::exit( @@ -67,3 +72,8 @@ fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } + +#[cfg(not(feature = "backend-smol"))] +fn main() { + unimplemented!() +} diff --git a/examples/tokio.rs b/examples/tokio.rs index 2441c61..0dc007f 100644 --- a/examples/tokio.rs +++ b/examples/tokio.rs @@ -1,58 +1,63 @@ -use pty_process::Command as _; -use std::os::unix::process::ExitStatusExt as _; -use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; - mod raw_guard; -async fn run( - child: &mut pty_process::tokio::Child, -) -> std::result::Result<(), Box> { - let _raw = raw_guard::RawGuard::new(); - - let mut in_buf = [0_u8; 4096]; - let mut out_buf = [0_u8; 4096]; - - let mut stdin = tokio::io::stdin(); - let mut stdout = tokio::io::stdout(); - - loop { - tokio::select! { - bytes = stdin.read(&mut in_buf) => match bytes { - Ok(bytes) => { - child.pty_mut().write_all(&in_buf[..bytes]).await.unwrap(); - } - Err(e) => { - eprintln!("stdin read failed: {:?}", e); - break; - } - }, - bytes = child.pty_mut().read(&mut out_buf) => match bytes { - Ok(bytes) => { - stdout.write_all(&out_buf[..bytes]).await.unwrap(); - 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); +#[cfg(feature = "backend-tokio")] +mod main { + use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; + + pub async fn run( + child: &mut pty_process::tokio::Child, + ) -> std::result::Result<(), Box> { + let _raw = super::raw_guard::RawGuard::new(); + + let mut in_buf = [0_u8; 4096]; + let mut out_buf = [0_u8; 4096]; + + let mut stdin = tokio::io::stdin(); + let mut stdout = tokio::io::stdout(); + + loop { + tokio::select! { + bytes = stdin.read(&mut in_buf) => match bytes { + Ok(bytes) => { + child.pty_mut().write_all(&in_buf[..bytes]).await.unwrap(); + } + Err(e) => { + eprintln!("stdin read failed: {:?}", e); + break; + } + }, + bytes = child.pty_mut().read(&mut out_buf) => match bytes { + Ok(bytes) => { + stdout.write_all(&out_buf[..bytes]).await.unwrap(); + stdout.flush().await.unwrap(); } - break; - } - }, + 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); + } + break; + } + }, + } } - } - Ok(()) + Ok(()) + } } +#[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(); - run(&mut child).await.unwrap(); + main::run(&mut child).await.unwrap(); let status = child.wait().await.unwrap(); std::process::exit( status @@ -60,3 +65,8 @@ async fn main() { .unwrap_or_else(|| status.signal().unwrap_or(0) + 128), ); } + +#[cfg(not(feature = "backend-tokio"))] +fn main() { + unimplemented!() +} -- cgit v1.2.3-54-g00ecf