rmux-pty 0.3.0

PTY allocation, resize, and child-process control for the RMUX terminal multiplexer.
Documentation
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
use std::io;

#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, OwnedFd, RawFd};
#[cfg(windows)]
use std::sync::Arc;

use crate::backend;
#[cfg(all(not(unix), not(windows)))]
use crate::unsupported_op;
#[cfg(all(not(unix), not(windows)))]
use crate::PtyError;
use crate::{Result, TerminalGeometry, TerminalSize};

#[cfg(unix)]
/// The slave endpoint of a Unix pseudoterminal pair.
#[derive(Debug)]
pub struct PtySlave {
    fd: OwnedFd,
}

#[cfg(unix)]
impl PtySlave {
    /// Duplicates the slave terminal endpoint.
    pub fn try_clone(&self) -> Result<Self> {
        Ok(Self {
            fd: self.fd.try_clone()?,
        })
    }

    /// Consumes the slave endpoint and returns the owned file descriptor.
    #[must_use]
    pub fn into_owned_fd(self) -> OwnedFd {
        self.fd
    }
}

#[cfg(unix)]
impl AsFd for PtySlave {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.fd.as_fd()
    }
}

#[cfg(unix)]
impl AsRawFd for PtySlave {
    fn as_raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }
}

/// The I/O endpoint for a pseudoterminal.
#[derive(Debug)]
pub struct PtyIo {
    #[cfg(unix)]
    fd: OwnedFd,
    #[cfg(windows)]
    pty: Arc<backend::WindowsPty>,
}

impl PtyIo {
    #[cfg(unix)]
    pub(crate) fn new(fd: OwnedFd) -> Self {
        Self { fd }
    }

    #[cfg(windows)]
    pub(crate) fn new(pty: Arc<backend::WindowsPty>) -> Self {
        Self { pty }
    }

    /// Queries the current terminal geometry for this PTY endpoint.
    pub fn size(&self) -> Result<TerminalSize> {
        #[cfg(unix)]
        {
            backend::query_size(self.fd.as_fd())
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                backend::query_size(&self.pty)
            }

            #[cfg(not(windows))]
            {
                Err(PtyError::Unsupported(unsupported_op::QUERY_PTY_SIZE))
            }
        }
    }

    /// Resizes this PTY endpoint.
    pub fn resize(&self, size: TerminalSize) -> Result<()> {
        #[cfg(unix)]
        {
            backend::apply_size(self.fd.as_fd(), size)
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                backend::apply_size(&self.pty, size)
            }

            #[cfg(not(windows))]
            {
                let _ = size;
                Err(PtyError::Unsupported(unsupported_op::RESIZE_PTY))
            }
        }
    }

    /// Resizes this PTY endpoint, preserving optional pixel geometry where supported.
    pub fn resize_geometry(&self, geometry: TerminalGeometry) -> Result<()> {
        #[cfg(unix)]
        {
            backend::apply_geometry(self.fd.as_fd(), geometry)
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                backend::apply_geometry(&self.pty, geometry)
            }

            #[cfg(not(windows))]
            {
                let _ = geometry;
                Err(PtyError::Unsupported(unsupported_op::RESIZE_PTY))
            }
        }
    }

    /// Duplicates this PTY I/O endpoint.
    pub fn try_clone(&self) -> Result<Self> {
        #[cfg(unix)]
        {
            Ok(Self {
                fd: self.fd.try_clone()?,
            })
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                Ok(Self {
                    pty: Arc::clone(&self.pty),
                })
            }

            #[cfg(not(windows))]
            {
                Err(PtyError::Unsupported(unsupported_op::CLONE_PTY_IO))
            }
        }
    }

    /// Reads bytes from this PTY endpoint.
    pub fn read(&self, buffer: &mut [u8]) -> io::Result<usize> {
        #[cfg(unix)]
        {
            backend::read(self.fd.as_fd(), buffer)
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                self.pty.read(buffer)
            }

            #[cfg(not(windows))]
            {
                let _ = buffer;
                Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "pty I/O is unsupported on this platform",
                ))
            }
        }
    }

    /// Writes all bytes to this PTY endpoint.
    pub fn write_all(&self, bytes: &[u8]) -> io::Result<()> {
        #[cfg(unix)]
        {
            backend::write_all(self.fd.as_fd(), bytes)
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                self.pty.write_all(bytes)
            }

            #[cfg(not(windows))]
            {
                let _ = bytes;
                Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "pty I/O is unsupported on this platform",
                ))
            }
        }
    }

    /// Makes the PTY endpoint nonblocking.
    pub fn set_nonblocking(&self) -> io::Result<()> {
        #[cfg(unix)]
        {
            backend::set_nonblocking(self.fd.as_fd())
        }

        #[cfg(not(unix))]
        {
            #[cfg(windows)]
            {
                Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "set_nonblocking is not applicable to ConPTY pipe handles; \
                     async readiness is provided by the Tokio named-pipe driver",
                ))
            }

            #[cfg(not(windows))]
            {
                Err(io::Error::new(
                    io::ErrorKind::Unsupported,
                    "pty I/O is unsupported on this platform",
                ))
            }
        }
    }

    /// Returns a borrowed Unix descriptor for integration points that still
    /// require `AsyncFd`.
    #[cfg(unix)]
    #[must_use]
    pub fn as_fd(&self) -> BorrowedFd<'_> {
        self.fd.as_fd()
    }

    #[cfg(unix)]
    pub(crate) fn raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }
}

#[cfg(unix)]
impl AsFd for PtyIo {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.fd.as_fd()
    }
}

#[cfg(unix)]
impl AsRawFd for PtyIo {
    fn as_raw_fd(&self) -> RawFd {
        self.fd.as_raw_fd()
    }
}

/// The master handle of a pseudoterminal.
#[derive(Debug)]
pub struct PtyMaster {
    io: PtyIo,
}

impl PtyMaster {
    #[cfg(unix)]
    pub(crate) fn new(fd: OwnedFd) -> Self {
        Self { io: PtyIo::new(fd) }
    }

    #[cfg(windows)]
    pub(crate) fn new(pty: backend::WindowsPty) -> Self {
        Self {
            io: PtyIo::new(Arc::new(pty)),
        }
    }

    /// Queries the current terminal geometry for this PTY.
    pub fn size(&self) -> Result<TerminalSize> {
        self.io.size()
    }

    /// Resizes this PTY.
    pub fn resize(&self, size: TerminalSize) -> Result<()> {
        self.io.resize(size)
    }

    /// Resizes this PTY, preserving optional pixel geometry where supported.
    pub fn resize_geometry(&self, geometry: TerminalGeometry) -> Result<()> {
        self.io.resize_geometry(geometry)
    }

    /// Duplicates the master handle.
    pub fn try_clone(&self) -> Result<Self> {
        Ok(Self {
            io: self.io.try_clone()?,
        })
    }

    /// Duplicates the master handle as an I/O endpoint.
    pub fn try_clone_io(&self) -> Result<PtyIo> {
        self.io.try_clone()
    }

    /// Consumes this master handle into its I/O endpoint.
    #[must_use]
    pub fn into_io(self) -> PtyIo {
        self.io
    }

    /// Consumes this Unix PTY master and returns the owned file descriptor.
    #[cfg(unix)]
    #[must_use]
    pub fn into_owned_fd(self) -> OwnedFd {
        self.io.fd
    }

    /// Returns the PTY I/O endpoint.
    #[must_use]
    pub fn io(&self) -> &PtyIo {
        &self.io
    }

    /// Writes all bytes to the PTY master.
    pub fn write_all(&self, bytes: &[u8]) -> io::Result<()> {
        self.io.write_all(bytes)
    }

    #[cfg(unix)]
    pub(crate) fn raw_fd(&self) -> RawFd {
        self.io.raw_fd()
    }

    #[cfg(windows)]
    pub(crate) fn windows_pty(&self) -> Arc<backend::WindowsPty> {
        Arc::clone(&self.io.pty)
    }
}

/// A freshly allocated PTY pair.
#[derive(Debug)]
pub struct PtyPair {
    master: PtyMaster,
    #[cfg(unix)]
    slave: PtySlave,
}

impl PtyPair {
    /// Allocates a PTY pair using the platform backend.
    pub fn open() -> Result<Self> {
        #[cfg(unix)]
        {
            let (master, slave) = backend::open_pty_pair()?;

            Ok(Self {
                master: PtyMaster::new(master),
                slave: PtySlave { fd: slave },
            })
        }

        #[cfg(windows)]
        {
            let master = backend::open_pty_pair(TerminalSize::new(80, 24))?;
            Ok(Self {
                master: PtyMaster::new(master),
            })
        }

        #[cfg(not(unix))]
        #[cfg(not(windows))]
        {
            Err(PtyError::Unsupported(unsupported_op::OPEN_PTY_PAIR))
        }
    }

    /// Allocates a PTY pair and applies an initial window size.
    pub fn open_with_size(size: TerminalSize) -> Result<Self> {
        let pair = Self::open()?;
        pair.master.resize(size)?;
        Ok(pair)
    }

    /// Returns the master endpoint.
    #[must_use]
    pub fn master(&self) -> &PtyMaster {
        &self.master
    }

    /// Returns the slave endpoint.
    #[cfg(unix)]
    #[must_use]
    pub fn slave(&self) -> &PtySlave {
        &self.slave
    }

    /// Consumes this Unix PTY pair into its master and slave endpoints.
    #[cfg(unix)]
    #[must_use]
    pub fn into_split(self) -> (PtyMaster, PtySlave) {
        (self.master, self.slave)
    }

    /// Consumes the pair and returns the master endpoint.
    #[must_use]
    pub fn into_master(self) -> PtyMaster {
        self.master
    }
}