Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Add set_nonblocking method on Stdin #73343

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/libstd/io/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ impl Read for StdinRaw {
Initializer::nop()
}
}
impl StdinRaw {
#[stable(since = "1.46.0", feature = "stdin_nonblocking")]
pub unsafe fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.0.set_nonblocking(nonblocking)
}
}
impl Write for StdoutRaw {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
Expand Down Expand Up @@ -365,6 +371,32 @@ impl Stdin {
pub fn read_line(&self, buf: &mut String) -> io::Result<usize> {
self.lock().read_line(buf)
}

/// Set the `stdin` in non-blocking mode.
///
/// It is useful in case you're playing with termcaps. However, please note that it is
/// unsafe because it'll modify the behaviour of all other stdin.
///
/// # Example
///
/// ```no_run
/// use std::io::{self, Read};
///
/// fn main() -> io::Result<()> {
/// let mut buffer = String::new();
/// let stdin = io::stdin();
/// let mut handle = stdin.lock();
///
/// handle.set_nonblocking(true)?;
/// handle.read_to_string(&mut buffer)?;
///
/// Ok(())
/// }
/// ```
#[stable(since = "1.46.0", feature = "stdin_nonblocking")]
pub unsafe fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
self.lock().set_nonblocking(nonblocking)
}
}

#[stable(feature = "std_debug", since = "1.16.0")]
Expand Down Expand Up @@ -439,6 +471,37 @@ impl fmt::Debug for StdinLock<'_> {
}
}

impl StdinLock<'_> {
/// Set the `stdin` in non-blocking mode.
///
/// It is useful in case you're playing with termcaps. However, please note that it is
/// unsafe because it'll modify the behaviour of all other stdin.
///
/// # Example
///
/// ```no_run
/// use std::io::{self, Read};
///
/// fn main() -> io::Result<()> {
/// let mut buffer = String::new();
/// let stdin = io::stdin();
/// let mut handle = stdin.lock();
///
/// handle.set_nonblocking(true)?;
/// handle.read_to_string(&mut buffer)?;
///
/// Ok(())
/// }
/// ```
#[stable(since = "1.46.0", feature = "stdin_nonblocking")]
pub unsafe fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
match self.inner.get_ref() {
Maybe::Real(ref stdin) => stdin.set_nonblocking(nonblocking),
Maybe::Fake => Ok(()),
}
}
}

/// A handle to the global standard output stream of the current process.
///
/// Each handle shares a global buffer of data to be written to the standard
Expand Down
11 changes: 11 additions & 0 deletions src/libstd/sys/unix/stdio.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::io::{self, IoSlice, IoSliceMut};
use crate::mem::ManuallyDrop;
use crate::sys::cvt;
use crate::sys::fd::FileDesc;

pub struct Stdin(());
Expand All @@ -10,6 +11,16 @@ impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin(()))
}
pub unsafe fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
let mut flags = libc::fcntl(libc::STDIN_FILENO, libc::F_GETFL);
if nonblocking {
flags |= libc::O_NONBLOCK;
} else {
flags &= !libc::O_NONBLOCK;
}
cvt(libc::fcntl(libc::STDIN_FILENO, libc::F_SETFL, flags))?;
Ok(())
}
}

impl io::Read for Stdin {
Expand Down
3 changes: 3 additions & 0 deletions src/libstd/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,7 @@ if #[cfg(not(target_vendor = "uwp"))] {
pub const HANDLE_FLAG_INHERIT: DWORD = 0x00000001;

pub const TOKEN_READ: DWORD = 0x20008;
pub const ENABLE_LINE_INPUT: DWORD = 0x0002;

extern "system" {
#[link_name = "SystemFunction036"]
Expand All @@ -688,6 +689,8 @@ if #[cfg(not(target_vendor = "uwp"))] {

pub fn GetConsoleMode(hConsoleHandle: HANDLE,
lpMode: LPDWORD) -> BOOL;
pub fn SetConsoleMode(hConsoleHandle: HANDLE,
lpMode: DWORD) -> BOOL;
// Allowed but unused by UWP
pub fn OpenProcessToken(ProcessHandle: HANDLE,
DesiredAccess: DWORD,
Expand Down
22 changes: 22 additions & 0 deletions src/libstd/sys/windows/stdio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,28 @@ impl Stdin {
pub fn new() -> io::Result<Stdin> {
Ok(Stdin { surrogate: 0 })
}
pub unsafe fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
let handle = get_handle(c::STD_INPUT_HANDLE)?;
let mut mode = 0;
if c::GetConsoleMode(handle, &mut mode) == 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Cannot get console mode for the given handle",
));
}
if nonblocking {
mode &= !c::ENABLE_LINE_INPUT;
} else {
mode |= c::ENABLE_LINE_INPUT;
}
if c::SetConsoleMode(handle, mode) == 0 {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"Cannot set console mode for the given handle",
));
}
Ok(())
}
}

impl io::Read for Stdin {
Expand Down