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

feat: implement read -t #227

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
49 changes: 47 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion brush-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ thiserror = "1.0.64"
tracing = "0.1.40"

[target.'cfg(target_family = "wasm")'.dependencies]
tokio = { version = "1.40.0", features = ["io-util", "macros", "rt"] }
tokio = { version = "1.40.0", features = ["io-util", "macros", "rt", "time"] }

[target.'cfg(any(windows, unix))'.dependencies]
hostname = "0.4.0"
Expand All @@ -45,11 +45,24 @@ tokio = { version = "1.40.0", features = [
"rt",
"rt-multi-thread",
"signal",
"time"
] }

[target.'cfg(windows)'.dependencies]
homedir = "0.3.3"
whoami = "1.5.2"
windows = { version = "0.56", features = [
"Win32_Storage",
"Win32_Storage_FileSystem",
"Win32_Foundation",
"Win32_Networking_WinHttp",
"Win32_Security",
"Win32_System_Threading",
"Win32_System_IO",
"Win32_Networking_HttpServer",
"Win32_Networking_WinSock",
"Win32_System_Console",
] }

[target.'cfg(unix)'.dependencies]
command-fds = "0.3.0"
Expand All @@ -59,6 +72,7 @@ nix = { version = "0.29.0", features = [
"signal",
"term",
"user",
"poll",
] }
uzers = "0.12.1"

Expand Down
93 changes: 74 additions & 19 deletions brush-core/src/builtins/read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use clap::Parser;
use itertools::Itertools;
use std::collections::VecDeque;
use std::io::{Read, Write};
use std::io::Write;
use std::time::Duration;

use crate::{builtins, commands, env, error, openfiles, sys, variables};

Expand Down Expand Up @@ -48,8 +49,8 @@ pub(crate) struct ReadCommand {

/// Specify timeout in seconds; fail if the timeout elapses before
/// input is completed.
#[clap(short = 't')]
timeout_in_seconds: Option<usize>,
#[clap(short = 't', value_parser = parse_duration)]
timeout_in_seconds: Option<Duration>,

/// File descriptor to read from instead of stdin.
#[clap(short = 'u', name = "FD")]
Expand All @@ -59,6 +60,11 @@ pub(crate) struct ReadCommand {
variable_names: Vec<String>,
}

fn parse_duration(arg: &str) -> Result<std::time::Duration, std::num::ParseIntError> {
let seconds = arg.parse()?;
Ok(std::time::Duration::from_secs(seconds))
}

impl builtins::Command for ReadCommand {
async fn execute(
&self,
Expand All @@ -73,13 +79,10 @@ impl builtins::Command for ReadCommand {
if self.raw_mode {
tracing::debug!("read -r is not implemented");
}
if self.timeout_in_seconds.is_some() {
return error::unimp("read -t");
}

// Find the input stream to use.
#[allow(clippy::cast_lossless)]
let input_stream = if let Some(fd_num) = self.fd_num_to_read {
let mut input_stream = if let Some(fd_num) = self.fd_num_to_read {
let fd_num = fd_num as u32;
context
.fd(fd_num)
Expand All @@ -88,7 +91,22 @@ impl builtins::Command for ReadCommand {
context.stdin()
};

let input_line = self.read_line(input_stream, context.stdout())?;
let orig_term_attr = self.setup_terminal_settings(&input_stream)?;
let input_line = if let Some(timeout) = self.timeout_in_seconds {
if !matches!(input_stream, openfiles::OpenFile::Stdin) {
return error::unimp("reat -t with a regular file (e.g. from input redirection)");
}
let mut input_stream = TimeoutStdinReader::new(timeout);
self.read_line(&mut input_stream, context.stdout()).await
} else {
self.read_line(&mut input_stream, context.stdout()).await
};

if let Some(orig_term_attr) = &orig_term_attr {
input_stream.set_term_attr(orig_term_attr)?;
}

let input_line = input_line?;

if let Some(input_line) = input_line {
let mut fields: VecDeque<_> = split_line_by_ifs(&context, input_line.as_str());
Expand Down Expand Up @@ -164,16 +182,15 @@ enum ReadTermination {
EndOfInput,
CtrlC,
Limit,
Timeout,
}

impl ReadCommand {
fn read_line(
async fn read_line(
&self,
mut input_file: openfiles::OpenFile,
input_file: &mut impl AsyncRead,
mut output_file: openfiles::OpenFile,
) -> Result<Option<String>, error::Error> {
let orig_term_attr = self.setup_terminal_settings(&input_file)?;

let delimiter = if self.return_after_n_chars_no_delimiter.is_some() {
None
} else if let Some(delimiter_str) = &self.delimiter {
Expand All @@ -195,13 +212,24 @@ impl ReadCommand {
let mut buffer = [0; 1]; // 1-byte buffer

let reason = loop {
let n = input_file.read(&mut buffer)?;
if n == 0 {
let n = input_file.read(&mut buffer).await;
if let Err(err) = &n {
if matches!(err.kind(), std::io::ErrorKind::TimedOut) {
break ReadTermination::Timeout;
}
}
if n? == 0 {
break ReadTermination::EndOfInput; // EOF reached.
}

let ch = buffer[0] as char;

// TODO: fix this (in io.rs)
#[cfg(windows)]
if ch == '\r' {
break ReadTermination::EndOfInput;
}

// Check for Ctrl+C.
if ch == '\x03' {
break ReadTermination::CtrlC;
Expand All @@ -224,6 +252,10 @@ impl ReadCommand {

line.push(ch);

// TODO: fix this disabled ECHO_INPUT (in io.rs)
#[cfg(windows)]
eprint!("{ch}");

// Check to see if we've hit a character limit.
if let Some(char_limit) = char_limit {
if line.len() >= char_limit {
Expand All @@ -232,10 +264,6 @@ impl ReadCommand {
}
};

if let Some(orig_term_attr) = &orig_term_attr {
input_file.set_term_attr(orig_term_attr)?;
}

match reason {
ReadTermination::EndOfInput => {
if line.is_empty() {
Expand All @@ -244,7 +272,7 @@ impl ReadCommand {
Ok(Some(line))
}
}
ReadTermination::CtrlC => {
ReadTermination::CtrlC | ReadTermination::Timeout => {
// Discard the input and return.
Ok(None)
}
Expand Down Expand Up @@ -281,3 +309,30 @@ fn split_line_by_ifs(context: &commands::ExecutionContext<'_>, line: &str) -> Ve
.map(|field| field.to_owned())
.collect()
}

trait AsyncRead {
async fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize>;
}

pub struct TimeoutStdinReader {
timeout: Duration,
}

impl TimeoutStdinReader {
pub fn new(timeout: Duration) -> TimeoutStdinReader {
TimeoutStdinReader { timeout }
}
}

impl AsyncRead for TimeoutStdinReader {
async fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
crate::sys::io::read_stdin_timeout(buf, self.timeout).await
}
}

// TODO: without timeout, blocking read for now
impl AsyncRead for openfiles::OpenFile {
async fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
(self as &mut dyn std::io::Read).read(buf)
}
}
1 change: 1 addition & 0 deletions brush-core/src/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub(crate) mod os_pipe;
pub(crate) mod tokio_process;

pub(crate) mod fs;
pub(crate) mod io;

pub(crate) use platform::network;
pub(crate) use platform::pipes;
Expand Down
Loading
Loading