Skip to content

Commit

Permalink
fix(redirection): allow continuing past redir errors (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
reubeno authored Feb 3, 2025
1 parent 3e423c3 commit 43c18ea
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 19 deletions.
1 change: 1 addition & 0 deletions brush-core/src/builtins/mapfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl builtins::Command for MapFileCommand {
}

let input_file = context
.params
.fd(self.fd)
.ok_or_else(|| error::Error::BadFileDescriptor(self.fd))?;

Expand Down
1 change: 1 addition & 0 deletions brush-core/src/builtins/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ impl builtins::Command for ReadCommand {
let input_stream = if let Some(fd_num) = self.fd_num_to_read {
let fd_num = fd_num as u32;
context
.params
.fd(fd_num)
.ok_or_else(|| error::Error::BadFileDescriptor(fd_num))?
} else {
Expand Down
16 changes: 3 additions & 13 deletions brush-core/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,27 +115,17 @@ pub struct ExecutionContext<'a> {
impl ExecutionContext<'_> {
/// Returns the standard input file; usable with `write!` et al.
pub fn stdin(&self) -> openfiles::OpenFile {
self.fd(0).unwrap()
self.params.stdin()
}

/// Returns the standard output file; usable with `write!` et al.
pub fn stdout(&self) -> openfiles::OpenFile {
self.fd(1).unwrap()
self.params.stdout()
}

/// Returns the standard error file; usable with `write!` et al.
pub fn stderr(&self) -> openfiles::OpenFile {
self.fd(2).unwrap()
}

/// Returns the file descriptor with the given number.
#[allow(clippy::unwrap_in_result)]
pub fn fd(&self, fd: u32) -> Option<openfiles::OpenFile> {
self.params
.open_files
.files
.get(&fd)
.map(|f| f.try_dup().unwrap())
self.params.stderr()
}

pub(crate) fn should_cmd_lead_own_process_group(&self) -> bool {
Expand Down
39 changes: 33 additions & 6 deletions brush-core/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,29 @@ pub struct ExecutionParameters {
pub process_group_policy: ProcessGroupPolicy,
}

impl ExecutionParameters {
/// Returns the standard input file; usable with `write!` et al.
pub fn stdin(&self) -> openfiles::OpenFile {
self.fd(0).unwrap()
}

/// Returns the standard output file; usable with `write!` et al.
pub fn stdout(&self) -> openfiles::OpenFile {
self.fd(1).unwrap()
}

/// Returns the standard error file; usable with `write!` et al.
pub fn stderr(&self) -> openfiles::OpenFile {
self.fd(2).unwrap()
}

/// Returns the file descriptor with the given number.
#[allow(clippy::unwrap_in_result)]
pub fn fd(&self, fd: u32) -> Option<openfiles::OpenFile> {
self.open_files.files.get(&fd).map(|f| f.try_dup().unwrap())
}
}

#[derive(Clone, Debug, Default)]
/// Policy for how to manage spawned external processes.
pub enum ProcessGroupPolicy {
Expand Down Expand Up @@ -910,12 +933,16 @@ impl ExecuteInPipeline for ast::SimpleCommand {
{
match item {
CommandPrefixOrSuffixItem::IoRedirect(redirect) => {
if setup_redirect(context.shell, &mut params, redirect)
.await?
.is_none()
{
// Something went wrong.
return Ok(CommandSpawnResult::ImmediateExit(1));
match setup_redirect(context.shell, &mut params, redirect).await {
Err(e) => {
writeln!(params.stderr(), "error: {e}")?;
return Ok(CommandSpawnResult::ImmediateExit(1));
}
Ok(None) => {
// Something went wrong.
return Ok(CommandSpawnResult::ImmediateExit(1));
}
Ok(_) => (),
}
}
CommandPrefixOrSuffixItem::ProcessSubstitution(kind, subshell_command) => {
Expand Down
5 changes: 5 additions & 0 deletions brush-shell/tests/cases/redirection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,8 @@ cases:
ignore_stderr: true
stdin: |
( : $(echo hi >&3); ) 3>output.txt
- name: "Redirection failure"
ignore_stderr: true
stdin: |
echo hi > /non-existent-dir/file.txt; echo following-command

0 comments on commit 43c18ea

Please sign in to comment.