aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2020-07-17 02:32:27 -0400
committerJesse Luehrs <doy@tozt.net>2020-07-17 02:32:27 -0400
commit259a8abe9dd9b11c20445cdfda7eb9f481882b93 (patch)
treecd91f3dbc568fcdb92aeafc245a3f6507950d9d4
parentce4135854ca7209b99347b24c107d49990283eda (diff)
downloadpty-process-259a8abe9dd9b11c20445cdfda7eb9f481882b93.tar.gz
pty-process-259a8abe9dd9b11c20445cdfda7eb9f481882b93.zip
refactor example
-rw-r--r--examples/basic.rs35
1 files changed, 28 insertions, 7 deletions
diff --git a/examples/basic.rs b/examples/basic.rs
index f63b767..c0c1236 100644
--- a/examples/basic.rs
+++ b/examples/basic.rs
@@ -1,5 +1,6 @@
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 _;
@@ -34,14 +35,12 @@ impl Drop for RawGuard {
}
}
-fn main() {
- let mut child = std::process::Command::new("sh")
- .spawn_pty(Some(&pty_process::Size::new(24, 80)))
- .unwrap();
+fn run(child: &pty_process::Child) {
let _raw = 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);
@@ -60,7 +59,11 @@ fn main() {
stdout.flush().unwrap();
}
Err(e) => {
- eprintln!("pty read failed: {:?}", 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;
}
};
@@ -79,8 +82,26 @@ fn main() {
}
}
}
- Err(e) => println!("select failed: {:?}", e),
+ Err(e) => {
+ println!("select failed: {:?}", e);
+ break;
+ }
}
}
- child.wait().unwrap();
+}
+
+fn main() {
+ let mut child = std::process::Command::new("sleep")
+ .args(&["500"])
+ .spawn_pty(Some(&pty_process::Size::new(24, 80)))
+ .unwrap();
+
+ run(&child);
+
+ let status = child.wait().unwrap();
+ std::process::exit(
+ status
+ .code()
+ .unwrap_or_else(|| status.signal().unwrap_or(0) + 128),
+ );
}