]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/stdio.rs
Rollup merge of #75837 - GuillaumeGomez:fix-font-color-help-button, r=Cldfire
[rust.git] / library / std / src / sys / windows / stdio.rs
1 #![unstable(issue = "none", feature = "windows_stdio")]
2
3 use crate::char::decode_utf16;
4 use crate::cmp;
5 use crate::io;
6 use crate::ptr;
7 use crate::str;
8 use crate::sys::c;
9 use crate::sys::cvt;
10 use crate::sys::handle::Handle;
11
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.
14 pub struct Stdin {
15     surrogate: u16,
16 }
17 pub struct Stdout;
18 pub struct Stderr;
19
20 // Apparently Windows doesn't handle large reads on stdin or writes to stdout/stderr well (see
21 // #13304 for details).
22 //
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."
25 //
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;
28
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
31 // UTF-16 to UTF-8.
32 pub const STDIN_BUF_SIZE: usize = MAX_BUFFER_SIZE / 2 * 3;
33
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))
40     } else {
41         Ok(handle)
42     }
43 }
44
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.
49     let mut mode = 0;
50     unsafe { c::GetConsoleMode(handle, &mut mode) != 0 }
51 }
52
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
59         return ret;
60     }
61
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.
64     //
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]) {
69         Ok(s) => s,
70         Err(ref e) if e.valid_up_to() == 0 => {
71             return Err(io::Error::new(
72                 io::ErrorKind::InvalidData,
73                 "Windows stdio in console mode does not support writing non-UTF-8 byte sequences",
74             ));
75         }
76         Err(e) => str::from_utf8(&data[..e.valid_up_to()]).unwrap(),
77     };
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()) {
81         *dest = chr;
82         len_utf16 += 1;
83     }
84     let utf16 = &utf16[..len_utf16];
85
86     let mut written = write_u16s(handle, &utf16)?;
87
88     // Figure out how many bytes of as UTF-8 were written away as UTF-16.
89     if written == utf16.len() {
90         Ok(utf8.len())
91     } else {
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 {
99             // low surrogate
100             // We just hope this works, and give up otherwise
101             let _ = write_u16s(handle, &utf16[written..written + 1]);
102             written += 1;
103         }
104         // Calculate the number of bytes of `utf8` that were actually written.
105         let mut count = 0;
106         for ch in utf16[..written].iter() {
107             count += match ch {
108                 0x0000..=0x007F => 1,
109                 0x0080..=0x07FF => 2,
110                 0xDCEE..=0xDFFF => 1, // Low surrogate. We already counted 3 bytes for the other.
111                 _ => 3,
112             };
113         }
114         debug_assert!(String::from_utf16(&utf16[..written]).unwrap() == utf8[..count]);
115         Ok(count)
116     }
117 }
118
119 fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
120     let mut written = 0;
121     cvt(unsafe {
122         c::WriteConsoleW(
123             handle,
124             data.as_ptr() as c::LPCVOID,
125             data.len() as u32,
126             &mut written,
127             ptr::null_mut(),
128         )
129     })?;
130     Ok(written as usize)
131 }
132
133 impl Stdin {
134     pub const fn new() -> Stdin {
135         Stdin { surrogate: 0 }
136     }
137 }
138
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
146             return ret;
147         }
148
149         if buf.len() == 0 {
150             return Ok(0);
151         } else if buf.len() < 4 {
152             return Err(io::Error::new(
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)",
156             ));
157         }
158
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
162         // lost.
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)?;
165
166         utf16_to_utf8(&utf16_buf[..read], buf)
167     }
168 }
169
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(
174     handle: c::HANDLE,
175     buf: &mut [u16],
176     mut amount: usize,
177     surrogate: &mut u16,
178 ) -> io::Result<usize> {
179     // Insert possibly remaining unpaired surrogate from last read.
180     let mut start = 0;
181     if *surrogate != 0 {
182         buf[0] = *surrogate;
183         *surrogate = 0;
184         start = 1;
185         if amount == 1 {
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
188             // 4 bytes.
189             amount = 2;
190         }
191     }
192     let mut amount = read_u16s(handle, &mut buf[start..amount])? + start;
193
194     if amount > 0 {
195         let last_char = buf[amount - 1];
196         if last_char >= 0xD800 && last_char <= 0xDBFF {
197             // high surrogate
198             *surrogate = last_char;
199             amount -= 1;
200         }
201     }
202     Ok(amount)
203 }
204
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,
213         nInitialChars: 0,
214         dwCtrlWakeupMask: CTRL_Z_MASK,
215         dwControlKeyState: 0,
216     };
217
218     let mut amount = 0;
219     cvt(unsafe {
220         c::ReadConsoleW(
221             handle,
222             buf.as_mut_ptr() as c::LPVOID,
223             buf.len() as u32,
224             &mut amount,
225             &mut input_control as c::PCONSOLE_READCONSOLE_CONTROL,
226         )
227     })?;
228
229     if amount > 0 && buf[amount as usize - 1] == CTRL_Z {
230         amount -= 1;
231     }
232     Ok(amount as usize)
233 }
234
235 #[allow(unused)]
236 fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
237     let mut written = 0;
238     for chr in decode_utf16(utf16.iter().cloned()) {
239         match chr {
240             Ok(chr) => {
241                 chr.encode_utf8(&mut utf8[written..]);
242                 written += chr.len_utf8();
243             }
244             Err(_) => {
245                 // We can't really do any better than forget all data and return an error.
246                 return Err(io::Error::new(
247                     io::ErrorKind::InvalidData,
248                     "Windows stdin in console mode does not support non-UTF-16 input; \
249                      encountered unpaired surrogate",
250                 ));
251             }
252         }
253     }
254     Ok(written)
255 }
256
257 impl Stdout {
258     pub const fn new() -> Stdout {
259         Stdout
260     }
261 }
262
263 impl io::Write for Stdout {
264     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
265         write(c::STD_OUTPUT_HANDLE, buf)
266     }
267
268     fn flush(&mut self) -> io::Result<()> {
269         Ok(())
270     }
271 }
272
273 impl Stderr {
274     pub const fn new() -> Stderr {
275         Stderr
276     }
277 }
278
279 impl io::Write for Stderr {
280     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
281         write(c::STD_ERROR_HANDLE, buf)
282     }
283
284     fn flush(&mut self) -> io::Result<()> {
285         Ok(())
286     }
287 }
288
289 pub fn is_ebadf(err: &io::Error) -> bool {
290     err.raw_os_error() == Some(c::ERROR_INVALID_HANDLE as i32)
291 }
292
293 pub fn panic_output() -> Option<impl io::Write> {
294     Some(Stderr::new())
295 }