aboutsummaryrefslogtreecommitdiffstats
path: root/src/pty.rs
blob: 1d7f113b075c01ffca0907e6702a31b3e7c696ef (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
/// An allocated pty
pub struct Pty {
    pt: async_io::Async<std::fs::File>,
    pts: std::fs::File,
}

impl Pty {
    /// Allocate and return a new pty.
    ///
    /// # Errors
    /// Returns an error if the pty failed to be allocated, or if we were
    /// unable to put it into non-blocking mode.
    pub fn new() -> crate::Result<Self> {
        let (pt, ptsname) = crate::sys::create_pt()?;
        let pt = async_io::Async::new(pt)?;
        let pts = std::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .open(&ptsname)?;
        Ok(Self { pt, pts })
    }

    /// Change the terminal size associated with the pty.
    ///
    /// # Errors
    /// Returns an error if we were unable to set the terminal size.
    pub fn resize(&self, size: crate::Size) -> crate::Result<()> {
        Ok(crate::sys::set_term_size(self, size)?)
    }

    pub(crate) fn pts(&self) -> &std::fs::File {
        &self.pts
    }
}

impl std::ops::Deref for Pty {
    type Target = async_io::Async<std::fs::File>;

    fn deref(&self) -> &Self::Target {
        &self.pt
    }
}

impl std::ops::DerefMut for Pty {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.pt
    }
}

impl std::os::unix::io::AsRawFd for Pty {
    fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
        self.pt.as_raw_fd()
    }
}

// there is an AsyncRead impl for &Async<std::fs::File>, but without this
// explicit impl, rust finds the AsyncRead impl for Async<std::fs::File>
// first, and then complains that it requires &mut self, because method
// resolution/autoderef doesn't take mutability into account
impl futures_io::AsyncRead for &Pty {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut [u8],
    ) -> std::task::Poll<Result<usize, std::io::Error>> {
        std::pin::Pin::new(&mut &self.pt).poll_read(cx, buf)
    }
}

// same as above
impl futures_io::AsyncWrite for &Pty {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> std::task::Poll<Result<usize, std::io::Error>> {
        std::pin::Pin::new(&mut &self.pt).poll_write(cx, buf)
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), std::io::Error>> {
        std::pin::Pin::new(&mut &self.pt).poll_flush(cx)
    }

    fn poll_close(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), std::io::Error>> {
        std::pin::Pin::new(&mut &self.pt).poll_close(cx)
    }
}