aboutsummaryrefslogtreecommitdiffstats
path: root/examples/basic.rs
blob: 4f996b972c4f7e70b63ff43c80b1db14b96d2f9c (plain) (blame)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
mod raw_guard;

mod main {
    use std::io::{Read as _, Write as _};
    use std::os::unix::io::AsRawFd as _;

    pub fn run(
        child: &mut std::process::Child,
        mut pty: &pty_process::blocking::Pty,
    ) {
        let _raw = super::raw_guard::RawGuard::new();
        let mut buf = [0_u8; 4096];
        let pty_fd = pty.as_raw_fd();
        let stdin_fd = std::io::stdin().as_raw_fd();

        loop {
            let mut set = nix::sys::select::FdSet::new();
            set.insert(pty_fd);
            set.insert(stdin_fd);
            match nix::sys::select::select(
                None,
                Some(&mut set),
                None,
                None,
                None,
            ) {
                Ok(n) => {
                    if n > 0 {
                        if set.contains(pty_fd) {
                            match 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) => {
                                    eprintln!("pty read failed: {:?}", e);
                                    break;
                                }
                            };
                        }
                        if set.contains(stdin_fd) {
                            match std::io::stdin().read(&mut buf) {
                                Ok(bytes) => {
                                    let buf = &buf[..bytes];
                                    pty.write_all(buf).unwrap();
                                }
                                Err(e) => {
                                    eprintln!("stdin read failed: {:?}", e);
                                    break;
                                }
                            }
                        }
                    }
                }
                Err(e) => {
                    println!("select failed: {:?}", e);
                    break;
                }
            }
            match child.try_wait() {
                Ok(Some(_)) => break,
                Ok(None) => {}
                Err(e) => {
                    println!("wait failed: {:?}", e);
                    break;
                }
            }
        }
    }
}

fn main() {
    use std::os::unix::process::ExitStatusExt as _;

    let pty = pty_process::blocking::Pty::new().unwrap();
    pty.resize(pty_process::Size::new(24, 80)).unwrap();
    let mut child = pty_process::blocking::Command::new("tac")
        // .args(&["500"])
        .spawn(&pty)
        .unwrap();

    main::run(&mut child, &pty);

    let status = child.wait().unwrap();
    std::process::exit(
        status
            .code()
            .unwrap_or_else(|| status.signal().unwrap_or(0) + 128),
    );
}