1 use crate::marker::PhantomData;
2 use crate::mem::size_of;
3 use crate::os::windows::io::{AsHandle, AsRawHandle, BorrowedHandle};
5 use crate::sys::{c, Align8};
11 pub struct IoSlice<'a> {
13 _p: PhantomData<&'a [u8]>,
16 impl<'a> IoSlice<'a> {
18 pub fn new(buf: &'a [u8]) -> IoSlice<'a> {
19 assert!(buf.len() <= c::ULONG::MAX as usize);
22 len: buf.len() as c::ULONG,
23 buf: buf.as_ptr() as *mut u8 as *mut c::CHAR,
30 pub fn advance(&mut self, n: usize) {
31 if (self.vec.len as usize) < n {
32 panic!("advancing IoSlice beyond its length");
36 self.vec.len -= n as c::ULONG;
37 self.vec.buf = self.vec.buf.add(n);
42 pub fn as_slice(&self) -> &[u8] {
43 unsafe { slice::from_raw_parts(self.vec.buf as *mut u8, self.vec.len as usize) }
48 pub struct IoSliceMut<'a> {
50 _p: PhantomData<&'a mut [u8]>,
53 impl<'a> IoSliceMut<'a> {
55 pub fn new(buf: &'a mut [u8]) -> IoSliceMut<'a> {
56 assert!(buf.len() <= c::ULONG::MAX as usize);
58 vec: c::WSABUF { len: buf.len() as c::ULONG, buf: buf.as_mut_ptr() as *mut c::CHAR },
64 pub fn advance(&mut self, n: usize) {
65 if (self.vec.len as usize) < n {
66 panic!("advancing IoSliceMut beyond its length");
70 self.vec.len -= n as c::ULONG;
71 self.vec.buf = self.vec.buf.add(n);
76 pub fn as_slice(&self) -> &[u8] {
77 unsafe { slice::from_raw_parts(self.vec.buf as *mut u8, self.vec.len as usize) }
81 pub fn as_mut_slice(&mut self) -> &mut [u8] {
82 unsafe { slice::from_raw_parts_mut(self.vec.buf as *mut u8, self.vec.len as usize) }
86 pub fn is_terminal(h: &impl AsHandle) -> bool {
87 unsafe { handle_is_console(h.as_handle()) }
90 unsafe fn handle_is_console(handle: BorrowedHandle<'_>) -> bool {
91 let handle = handle.as_raw_handle();
93 // A null handle means the process has no console.
99 if c::GetConsoleMode(handle, &mut out) != 0 {
100 // False positives aren't possible. If we got a console then we definitely have a console.
104 // At this point, we *could* have a false negative. We can determine that this is a true
105 // negative if we can detect the presence of a console on any of the standard I/O streams. If
106 // another stream has a console, then we know we're in a Windows console and can therefore
107 // trust the negative.
108 for std_handle in [c::STD_INPUT_HANDLE, c::STD_OUTPUT_HANDLE, c::STD_ERROR_HANDLE] {
109 let std_handle = c::GetStdHandle(std_handle);
110 if !std_handle.is_null()
111 && std_handle != handle
112 && c::GetConsoleMode(std_handle, &mut out) != 0
118 // Otherwise, we fall back to an msys hack to see if we can detect the presence of a pty.
122 unsafe fn msys_tty_on(handle: c::HANDLE) -> bool {
123 // Early return if the handle is not a pipe.
124 if c::GetFileType(handle) != c::FILE_TYPE_PIPE {
128 const SIZE: usize = size_of::<c::FILE_NAME_INFO>() + c::MAX_PATH * size_of::<c::WCHAR>();
129 let mut name_info_bytes = Align8([0u8; SIZE]);
130 let res = c::GetFileInformationByHandleEx(
133 name_info_bytes.0.as_mut_ptr() as *mut libc::c_void,
139 let name_info: &c::FILE_NAME_INFO = &*(name_info_bytes.0.as_ptr() as *const c::FILE_NAME_INFO);
140 let name_len = name_info.FileNameLength as usize / 2;
141 // Offset to get the `FileName` field.
142 let name_ptr = name_info_bytes.0.as_ptr().offset(size_of::<c::DWORD>() as isize).cast::<u16>();
143 let s = core::slice::from_raw_parts(name_ptr, name_len);
144 let name = String::from_utf16_lossy(s);
145 // Get the file name only.
146 let name = name.rsplit('\\').next().unwrap_or(&name);
147 // This checks whether 'pty' exists in the file name, which indicates that
148 // a pseudo-terminal is attached. To mitigate against false positives
149 // (e.g., an actual file name that contains 'pty'), we also require that
150 // the file name begins with either the strings 'msys-' or 'cygwin-'.)
151 let is_msys = name.starts_with("msys-") || name.starts_with("cygwin-");
152 let is_pty = name.contains("-pty");