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
//! The `Fd` (descriptor address) and `Stdio` (redirection target) model.
use std::fmt;
use std::fs::File;
use crate::error::Error;
/// A target descriptor. `i32` matches POSIX fd numbering; on Windows 0/1/2 are
/// the std handles and n>=3 is the MSVCRT fd-table slot. Use a bare
/// `i32` at call sites via `From`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Fd(i32);
impl Fd {
pub const STDIN: Fd = Fd(0);
pub const STDOUT: Fd = Fd(1);
pub const STDERR: Fd = Fd(2);
pub fn raw(self) -> i32 {
self.0
}
/// Rebuild an `Fd` from a raw descriptor number. Used by the Windows raw spawn backend to
/// walk a dense `0..=maxfd` fd-table; unused (hence `dead_code`-allowed) on Unix.
#[allow(
dead_code,
reason = "used by the Windows raw spawn backend to walk a dense fd-table; unused on Unix"
)]
pub(crate) fn from_raw(n: i32) -> Fd {
Fd(n)
}
}
/// Infallible: `Fd` itself carries no validity requirement (a negative value round-trips fine
/// through [`raw`](Fd::raw)). The one call site that must never hand a real spawn a negative
/// descriptor is [`Command::fd`](crate::Command::fd), which checks explicitly and returns
/// `Err` — not a `debug_assert!` here, which would panic in debug builds and do nothing at all
/// in release.
impl From<i32> for Fd {
fn from(n: i32) -> Fd {
Fd(n)
}
}
impl fmt::Display for Fd {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
0 => f.write_str("stdin"),
1 => f.write_str("stdout"),
2 => f.write_str("stderr"),
n => write!(f, "fd {n}"),
}
}
}
/// Which way a pipe flows relative to the child.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Direction {
/// Child reads (parent holds the write end).
In,
/// Child writes (parent holds the read end).
Out,
}
/// A redirection target for a descriptor. Opaque; built via the constructors.
#[derive(Debug)]
pub struct Stdio(StdioKind);
#[derive(Debug)]
enum StdioKind {
Inherit,
Null,
/// `None` = infer direction from the slot at resolve time; `Some` = explicit.
Pipe(Option<Direction>),
File(File),
Merge(Fd),
#[cfg(feature = "pty")]
Pty,
}
impl Stdio {
/// Pass the parent's matching descriptor through to the child.
pub fn inherit() -> Stdio {
Stdio(StdioKind::Inherit)
}
/// Connect to the null device (`/dev/null` / `NUL`).
pub fn null() -> Stdio {
Stdio(StdioKind::Null)
}
/// A pipe whose direction is inferred from the slot (stdin=child-reads,
/// stdout/stderr=child-writes). Use [`Stdio::pipe_in`]/[`Stdio::pipe_out`] for fds >= 3.
pub fn pipe() -> Stdio {
Stdio(StdioKind::Pipe(None))
}
/// A pipe the child reads (parent holds the write end).
pub fn pipe_in() -> Stdio {
Stdio(StdioKind::Pipe(Some(Direction::In)))
}
/// A pipe the child writes (parent holds the read end).
pub fn pipe_out() -> Stdio {
Stdio(StdioKind::Pipe(Some(Direction::Out)))
}
/// Redirect to/from an already-open file.
pub fn from_file(f: File) -> Stdio {
Stdio(StdioKind::File(f))
}
/// Duplicate another descriptor's target onto this one (e.g. `2>&1`).
pub fn merge(other: Fd) -> Stdio {
Stdio(StdioKind::Merge(other))
}
/// A pseudo-terminal. Not yet wired: resolves to `Unsupported`.
#[cfg(feature = "pty")]
pub fn pty() -> Stdio {
Stdio(StdioKind::Pty)
}
/// Resolve to a concrete, direction-unambiguous target for `slot`.
pub(crate) fn resolve(self, slot: Fd) -> Result<ResolvedStdio, Error> {
Ok(match self.0 {
StdioKind::Inherit => ResolvedStdio::Inherit,
StdioKind::Null => ResolvedStdio::Null,
StdioKind::File(f) => ResolvedStdio::File(f),
StdioKind::Merge(t) => ResolvedStdio::Merge(t),
StdioKind::Pipe(Some(dir)) => ResolvedStdio::Pipe(dir),
StdioKind::Pipe(None) => match slot {
Fd::STDIN => ResolvedStdio::Pipe(Direction::In),
Fd::STDOUT | Fd::STDERR => ResolvedStdio::Pipe(Direction::Out),
other => {
return Err(Error::Unsupported {
op: format!("ambiguous pipe direction on {other}"),
platform: std::env::consts::OS,
detail: "use Stdio::pipe_in()/pipe_out() for descriptors >= 3".into(),
})
}
},
#[cfg(feature = "pty")]
StdioKind::Pty => {
return Err(Error::Unsupported {
op: format!("pty on {slot}"),
platform: std::env::consts::OS,
detail: "PTY support is not wired yet".into(),
})
}
})
}
}
/// A fully direction-resolved redirection target (crate-internal).
#[derive(Debug)]
pub(crate) enum ResolvedStdio {
Inherit,
Null,
Pipe(Direction),
File(File),
Merge(Fd),
}
#[cfg(test)]
#[path = "stdio_tests.rs"]
mod stdio_tests;