1 #![unstable(issue = "none", feature = "windows_stdio")]
3 use crate::char::decode_utf16;
10 use crate::sys::handle::Handle;
12 // Don't cache handles but get them fresh for every read/write. This allows us to track changes to
13 // the value over time (such as if a process calls `SetStdHandle` while it's running). See #40490.
20 // Apparently Windows doesn't handle large reads on stdin or writes to stdout/stderr well (see
21 // #13304 for details).
23 // From MSDN (2011): "The storage for this buffer is allocated from a shared heap for the
24 // process that is 64 KB in size. The maximum size of the buffer will depend on heap usage."
26 // We choose the cap at 8 KiB because libuv does the same, and it seems to be acceptable so far.
27 const MAX_BUFFER_SIZE: usize = 8192;
29 // The standard buffer size of BufReader for Stdin should be able to hold 3x more bytes than there
30 // are `u16`'s in MAX_BUFFER_SIZE. This ensures the read data can always be completely decoded from
32 pub const STDIN_BUF_SIZE: usize = MAX_BUFFER_SIZE / 2 * 3;
34 pub fn get_handle(handle_id: c::DWORD) -> io::Result<c::HANDLE> {
35 let handle = unsafe { c::GetStdHandle(handle_id) };
36 if handle == c::INVALID_HANDLE_VALUE {
37 Err(io::Error::last_os_error())
38 } else if handle.is_null() {
39 Err(io::Error::from_raw_os_error(c::ERROR_INVALID_HANDLE as i32))
45 fn is_console(handle: c::HANDLE) -> bool {
46 // `GetConsoleMode` will return false (0) if this is a pipe (we don't care about the reported
47 // mode). This will only detect Windows Console, not other terminals connected to a pipe like
48 // MSYS. Which is exactly what we need, as only Windows Console needs a conversion to UTF-16.
50 unsafe { c::GetConsoleMode(handle, &mut mode) != 0 }
53 fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result<usize> {
54 let handle = get_handle(handle_id)?;
55 if !is_console(handle) {
56 let handle = Handle::new(handle);
57 let ret = handle.write(data);
58 handle.into_raw(); // Don't close the handle
62 // As the console is meant for presenting text, we assume bytes of `data` come from a string
63 // and are encoded as UTF-8, which needs to be encoded as UTF-16.
65 // If the data is not valid UTF-8 we write out as many bytes as are valid.
66 // Only when there are no valid bytes (which will happen on the next call), return an error.
67 let len = cmp::min(data.len(), MAX_BUFFER_SIZE / 2);
68 let utf8 = match str::from_utf8(&data[..len]) {
70 Err(ref e) if e.valid_up_to() == 0 => {
71 return Err(io::Error::new_const(
72 io::ErrorKind::InvalidData,
73 &"Windows stdio in console mode does not support writing non-UTF-8 byte sequences",
76 Err(e) => str::from_utf8(&data[..e.valid_up_to()]).unwrap(),
78 let mut utf16 = [0u16; MAX_BUFFER_SIZE / 2];
79 let mut len_utf16 = 0;
80 for (chr, dest) in utf8.encode_utf16().zip(utf16.iter_mut()) {
84 let utf16 = &utf16[..len_utf16];
86 let mut written = write_u16s(handle, &utf16)?;
88 // Figure out how many bytes of as UTF-8 were written away as UTF-16.
89 if written == utf16.len() {
92 // Make sure we didn't end up writing only half of a surrogate pair (even though the chance
93 // is tiny). Because it is not possible for user code to re-slice `data` in such a way that
94 // a missing surrogate can be produced (and also because of the UTF-8 validation above),
95 // write the missing surrogate out now.
96 // Buffering it would mean we have to lie about the number of bytes written.
97 let first_char_remaining = utf16[written];
98 if first_char_remaining >= 0xDCEE && first_char_remaining <= 0xDFFF {
100 // We just hope this works, and give up otherwise
101 let _ = write_u16s(handle, &utf16[written..written + 1]);
104 // Calculate the number of bytes of `utf8` that were actually written.
106 for ch in utf16[..written].iter() {
108 0x0000..=0x007F => 1,
109 0x0080..=0x07FF => 2,
110 0xDCEE..=0xDFFF => 1, // Low surrogate. We already counted 3 bytes for the other.
114 debug_assert!(String::from_utf16(&utf16[..written]).unwrap() == utf8[..count]);
119 fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
124 data.as_ptr() as c::LPCVOID,
134 pub const fn new() -> Stdin {
135 Stdin { surrogate: 0 }
139 impl io::Read for Stdin {
140 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
141 let handle = get_handle(c::STD_INPUT_HANDLE)?;
142 if !is_console(handle) {
143 let handle = Handle::new(handle);
144 let ret = handle.read(buf);
145 handle.into_raw(); // Don't close the handle
151 } else if buf.len() < 4 {
152 return Err(io::Error::new_const(
153 io::ErrorKind::InvalidInput,
154 &"Windows stdin in console mode does not support a buffer too small to \
155 guarantee holding one arbitrary UTF-8 character (4 bytes)",
159 let mut utf16_buf = [0u16; MAX_BUFFER_SIZE / 2];
160 // In the worst case, an UTF-8 string can take 3 bytes for every `u16` of an UTF-16. So
161 // we can read at most a third of `buf.len()` chars and uphold the guarantee no data gets
163 let amount = cmp::min(buf.len() / 3, utf16_buf.len());
164 let read = read_u16s_fixup_surrogates(handle, &mut utf16_buf, amount, &mut self.surrogate)?;
166 utf16_to_utf8(&utf16_buf[..read], buf)
170 // We assume that if the last `u16` is an unpaired surrogate they got sliced apart by our
171 // buffer size, and keep it around for the next read hoping to put them together.
172 // This is a best effort, and may not work if we are not the only reader on Stdin.
173 fn read_u16s_fixup_surrogates(
178 ) -> io::Result<usize> {
179 // Insert possibly remaining unpaired surrogate from last read.
186 // Special case: `Stdin::read` guarantees we can always read at least one new `u16`
187 // and combine it with an unpaired surrogate, because the UTF-8 buffer is at least
192 let mut amount = read_u16s(handle, &mut buf[start..amount])? + start;
195 let last_char = buf[amount - 1];
196 if last_char >= 0xD800 && last_char <= 0xDBFF {
198 *surrogate = last_char;
205 fn read_u16s(handle: c::HANDLE, buf: &mut [u16]) -> io::Result<usize> {
206 // Configure the `pInputControl` parameter to not only return on `\r\n` but also Ctrl-Z, the
207 // traditional DOS method to indicate end of character stream / user input (SUB).
208 // See #38274 and https://stackoverflow.com/questions/43836040/win-api-readconsole.
209 const CTRL_Z: u16 = 0x1A;
210 const CTRL_Z_MASK: c::ULONG = 1 << CTRL_Z;
211 let mut input_control = c::CONSOLE_READCONSOLE_CONTROL {
212 nLength: crate::mem::size_of::<c::CONSOLE_READCONSOLE_CONTROL>() as c::ULONG,
214 dwCtrlWakeupMask: CTRL_Z_MASK,
215 dwControlKeyState: 0,
222 buf.as_mut_ptr() as c::LPVOID,
225 &mut input_control as c::PCONSOLE_READCONSOLE_CONTROL,
229 if amount > 0 && buf[amount as usize - 1] == CTRL_Z {
236 fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
238 for chr in decode_utf16(utf16.iter().cloned()) {
241 chr.encode_utf8(&mut utf8[written..]);
242 written += chr.len_utf8();
245 // We can't really do any better than forget all data and return an error.
246 return Err(io::Error::new_const(
247 io::ErrorKind::InvalidData,
248 &"Windows stdin in console mode does not support non-UTF-16 input; \
249 encountered unpaired surrogate",
258 pub const fn new() -> Stdout {
263 impl io::Write for Stdout {
264 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
265 write(c::STD_OUTPUT_HANDLE, buf)
268 fn flush(&mut self) -> io::Result<()> {
274 pub const fn new() -> Stderr {
279 impl io::Write for Stderr {
280 fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
281 write(c::STD_ERROR_HANDLE, buf)
284 fn flush(&mut self) -> io::Result<()> {
289 pub fn is_ebadf(err: &io::Error) -> bool {
290 err.raw_os_error() == Some(c::ERROR_INVALID_HANDLE as i32)
293 pub fn panic_output() -> Option<impl io::Write> {