1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
use std::io::{Read as _, Write as _};
use std::os::unix::io::AsRawFd as _;
use pty_process::Command as _;
fn main() {
let mut child = std::process::Command::new("perl")
.args(&[
"-MTerm::ReadKey",
"-E",
"my @size = GetTerminalSize; say for @size",
])
.spawn_pty(Some(&pty_process::Size::new(24, 80)))
.unwrap();
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];
print!(
"got {} bytes: '{}'",
bytes,
std::str::from_utf8(buf).unwrap()
);
std::io::stdout().flush().unwrap();
}
Err(e) => {
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;
}
}
}
}
}
Err(e) => println!("select failed: {:?}", e),
}
}
child.wait().unwrap();
}
|