]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/windows/stdio.rs
Rollup merge of #65144 - clarfon:moo, r=sfackler
[rust.git] / src / libstd / sys / windows / stdio.rs
1 #![unstable(issue = "0", 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(io::ErrorKind::InvalidData,
72                 "Windows stdio in console mode does not support writing non-UTF-8 byte sequences"))
73         },
74         Err(e) => str::from_utf8(&data[..e.valid_up_to()]).unwrap(),
75     };
76     let mut utf16 = [0u16; MAX_BUFFER_SIZE / 2];
77     let mut len_utf16 = 0;
78     for (chr, dest) in utf8.encode_utf16().zip(utf16.iter_mut()) {
79         *dest = chr;
80         len_utf16 += 1;
81     }
82     let utf16 = &utf16[..len_utf16];
83
84     let mut written = write_u16s(handle, &utf16)?;
85
86     // Figure out how many bytes of as UTF-8 were written away as UTF-16.
87     if written == utf16.len() {
88         Ok(utf8.len())
89     } else {
90         // Make sure we didn't end up writing only half of a surrogate pair (even though the chance
91         // is tiny). Because it is not possible for user code to re-slice `data` in such a way that
92         // a missing surrogate can be produced (and also because of the UTF-8 validation above),
93         // write the missing surrogate out now.
94         // Buffering it would mean we have to lie about the number of bytes written.
95         let first_char_remaining = utf16[written];
96         if first_char_remaining >= 0xDCEE && first_char_remaining <= 0xDFFF { // low surrogate
97             // We just hope this works, and give up otherwise
98             let _ = write_u16s(handle, &utf16[written..written+1]);
99             written += 1;
100         }
101         // Calculate the number of bytes of `utf8` that were actually written.
102         let mut count = 0;
103         for ch in utf16[..written].iter() {
104             count += match ch {
105                 0x0000 ..= 0x007F => 1,
106                 0x0080 ..= 0x07FF => 2,
107                 0xDCEE ..= 0xDFFF => 1, // Low surrogate. We already counted 3 bytes for the other.
108                 _ => 3,
109             };
110         }
111         debug_assert!(String::from_utf16(&utf16[..written]).unwrap() == utf8[..count]);
112         Ok(count)
113     }
114 }
115
116 fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
117     let mut written = 0;
118     cvt(unsafe {
119         c::WriteConsoleW(handle,
120                          data.as_ptr() as c::LPCVOID,
121                          data.len() as u32,
122                          &mut written,
123                          ptr::null_mut())
124     })?;
125     Ok(written as usize)
126 }
127
128 impl Stdin {
129     pub fn new() -> io::Result<Stdin> {
130         Ok(Stdin { surrogate: 0 })
131     }
132 }
133
134 impl io::Read for Stdin {
135     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
136         let handle = get_handle(c::STD_INPUT_HANDLE)?;
137         if !is_console(handle) {
138             let handle = Handle::new(handle);
139             let ret = handle.read(buf);
140             handle.into_raw(); // Don't close the handle
141             return ret;
142         }
143
144         if buf.len() == 0 {
145             return Ok(0);
146         } else if buf.len() < 4 {
147             return Err(io::Error::new(io::ErrorKind::InvalidInput,
148                         "Windows stdin in console mode does not support a buffer too small to \
149                         guarantee holding one arbitrary UTF-8 character (4 bytes)"))
150         }
151
152         let mut utf16_buf = [0u16; MAX_BUFFER_SIZE / 2];
153         // In the worst case, an UTF-8 string can take 3 bytes for every `u16` of an UTF-16. So
154         // we can read at most a third of `buf.len()` chars and uphold the guarantee no data gets
155         // lost.
156         let amount = cmp::min(buf.len() / 3, utf16_buf.len());
157         let read = read_u16s_fixup_surrogates(handle, &mut utf16_buf, amount, &mut self.surrogate)?;
158
159         utf16_to_utf8(&utf16_buf[..read], buf)
160     }
161 }
162
163
164 // We assume that if the last `u16` is an unpaired surrogate they got sliced apart by our
165 // buffer size, and keep it around for the next read hoping to put them together.
166 // This is a best effort, and may not work if we are not the only reader on Stdin.
167 fn read_u16s_fixup_surrogates(handle: c::HANDLE,
168                               buf: &mut [u16],
169                               mut amount: usize,
170                               surrogate: &mut u16) -> io::Result<usize>
171 {
172     // Insert possibly remaining unpaired surrogate from last read.
173     let mut start = 0;
174     if *surrogate != 0 {
175         buf[0] = *surrogate;
176         *surrogate = 0;
177         start = 1;
178         if amount == 1 {
179             // Special case: `Stdin::read` guarantees we can always read at least one new `u16`
180             // and combine it with an unpaired surrogate, because the UTF-8 buffer is at least
181             // 4 bytes.
182             amount = 2;
183         }
184     }
185     let mut amount = read_u16s(handle, &mut buf[start..amount])? + start;
186
187     if amount > 0 {
188         let last_char = buf[amount - 1];
189         if last_char >= 0xD800 && last_char <= 0xDBFF { // high surrogate
190             *surrogate = last_char;
191             amount -= 1;
192         }
193     }
194     Ok(amount)
195 }
196
197 fn read_u16s(handle: c::HANDLE, buf: &mut [u16]) -> io::Result<usize> {
198     // Configure the `pInputControl` parameter to not only return on `\r\n` but also Ctrl-Z, the
199     // traditional DOS method to indicate end of character stream / user input (SUB).
200     // See #38274 and https://stackoverflow.com/questions/43836040/win-api-readconsole.
201     const CTRL_Z: u16 = 0x1A;
202     const CTRL_Z_MASK: c::ULONG = 1 << CTRL_Z;
203     let mut input_control = c::CONSOLE_READCONSOLE_CONTROL {
204         nLength: crate::mem::size_of::<c::CONSOLE_READCONSOLE_CONTROL>() as c::ULONG,
205         nInitialChars: 0,
206         dwCtrlWakeupMask: CTRL_Z_MASK,
207         dwControlKeyState: 0,
208     };
209
210     let mut amount = 0;
211     cvt(unsafe {
212         c::ReadConsoleW(handle,
213                         buf.as_mut_ptr() as c::LPVOID,
214                         buf.len() as u32,
215                         &mut amount,
216                         &mut input_control as c::PCONSOLE_READCONSOLE_CONTROL)
217     })?;
218
219     if amount > 0 && buf[amount as usize - 1] == CTRL_Z {
220         amount -= 1;
221     }
222     Ok(amount as usize)
223 }
224
225 #[allow(unused)]
226 fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
227     let mut written = 0;
228     for chr in decode_utf16(utf16.iter().cloned()) {
229         match chr {
230             Ok(chr) => {
231                 chr.encode_utf8(&mut utf8[written..]);
232                 written += chr.len_utf8();
233             }
234             Err(_) => {
235                 // We can't really do any better than forget all data and return an error.
236                 return Err(io::Error::new(io::ErrorKind::InvalidData,
237                     "Windows stdin in console mode does not support non-UTF-16 input; \
238                     encountered unpaired surrogate"))
239             }
240         }
241     }
242     Ok(written)
243 }
244
245 impl Stdout {
246     pub fn new() -> io::Result<Stdout> {
247         Ok(Stdout)
248     }
249 }
250
251 impl io::Write for Stdout {
252     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
253         write(c::STD_OUTPUT_HANDLE, buf)
254     }
255
256     fn flush(&mut self) -> io::Result<()> {
257         Ok(())
258     }
259 }
260
261 impl Stderr {
262     pub fn new() -> io::Result<Stderr> {
263         Ok(Stderr)
264     }
265 }
266
267 impl io::Write for Stderr {
268     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
269         write(c::STD_ERROR_HANDLE, buf)
270     }
271
272     fn flush(&mut self) -> io::Result<()> {
273         Ok(())
274     }
275 }
276
277 pub fn is_ebadf(err: &io::Error) -> bool {
278     err.raw_os_error() == Some(c::ERROR_INVALID_HANDLE as i32)
279 }
280
281 pub fn panic_output() -> Option<impl io::Write> {
282     Stderr::new().ok()
283 }