Merge pull request #3274 from no382001/issue3262

fix read/1 on non-TTY stdin blocking until newline
This commit is contained in:
Mark Thom
2026-04-18 18:16:53 -06:00
committed by GitHub
3 changed files with 23 additions and 1 deletions

View File

@@ -23,7 +23,7 @@ use std::fmt::Debug;
use std::fs::{File, OpenOptions};
use std::hash::Hash;
use std::io;
use std::io::{Cursor, ErrorKind, Read, Seek, SeekFrom, Write};
use std::io::{Cursor, ErrorKind, IsTerminal, Read, Seek, SeekFrom, Write};
use std::mem::ManuallyDrop;
use std::net::{Shutdown, TcpStream};
use std::ops::{Deref, DerefMut};
@@ -657,6 +657,16 @@ impl Stream {
#[inline]
pub fn stdin(arena: &mut Arena, add_history: bool) -> Stream {
#[cfg(unix)]
if !std::io::stdin().is_terminal() {
use std::os::unix::io::{FromRawFd, RawFd};
// dup fd 0 so the File can be owned (and closed later) without
// closing the real stdin.
let fd = unsafe { libc::dup(0 as RawFd) };
let file = unsafe { File::from_raw_fd(fd) };
return Stream::from_file_as_input(atom!("user_input"), file, arena);
}
Stream::Readline(arena_alloc!(
StreamLayout::new(ReadlineStream::new("", add_history)),
arena