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

fix: add &>> implementation #103

Merged
merged 1 commit into from
Jun 29, 2024
Merged
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
30 changes: 18 additions & 12 deletions brush-core/src/interp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ impl Execute for ast::Pipeline {
let mut spawn_results = VecDeque::new();

for (current_pipeline_index, command) in self.seq.iter().enumerate() {
// If there's only one command in the pipeline, then we run directly in the current shell.
// Otherwise, we spawn a separate subshell for each command in the pipeline.
// If there's only one command in the pipeline, then we run directly in the current
// shell. Otherwise, we spawn a separate subshell for each command in the
// pipeline.
let spawn_result = if pipeline_len > 1 {
let mut subshell = shell.clone();
let mut pipeline_context = PipelineExecutionContext {
Expand Down Expand Up @@ -354,8 +355,9 @@ impl Execute for ast::Pipeline {

let mut child_future = Box::pin(child.wait_with_output());

// Wait for the process to exit or for a relevant signal, whichever happens first.
// TODO: Figure out how to detect a SIGSTOP'd process.
// Wait for the process to exit or for a relevant signal, whichever happens
// first. TODO: Figure out how to detect a SIGSTOP'd
// process.
let wait_result = if stopped.is_empty() {
loop {
tokio::select! {
Expand Down Expand Up @@ -879,8 +881,9 @@ impl ExecuteInPipeline for ast::SimpleCommand {
basic_expand_assignment(context.shell, assignment).await?;
args.push(CommandArg::Assignment(expanded));
} else {
// This *looks* like an assignment, but it's really a string we should fully
// treat as a regular looking argument.
// This *looks* like an assignment, but it's really a string we should
// fully treat as a regular looking
// argument.
let mut next_args =
expansion::full_expand_and_split_word(context.shell, word)
.await?
Expand Down Expand Up @@ -914,8 +917,9 @@ impl ExecuteInPipeline for ast::SimpleCommand {
next_args = alias_pieces;
}

// Check if we're going to be invoking a special declaration builtin. That will
// change how we parse and process args.
// Check if we're going to be invoking a special declaration builtin.
// That will change how we parse and process
// args.
if context
.shell
.builtins
Expand Down Expand Up @@ -1002,7 +1006,7 @@ impl ExecuteInPipeline for ast::SimpleCommand {

// Execute.
let execution_result =
commands::execute(cmd_context, args, true /*use functions?*/).await;
commands::execute(cmd_context, args, true /* use functions? */).await;

// Pop off that ephemeral environment scope.
context.shell.env.pop_scope(EnvironmentScope::Command)?;
Expand Down Expand Up @@ -1103,7 +1107,8 @@ async fn apply_assignment(
required_scope: Option<EnvironmentScope>,
creation_scope: EnvironmentScope,
) -> Result<(), error::Error> {
// Figure out if we are trying to assign to a variable or assign to an element of an existing array.
// Figure out if we are trying to assign to a variable or assign to an element of an existing
// array.
let mut array_index;
let variable_name = match &assignment.name {
ast::AssignmentName::VariableName(name) => {
Expand Down Expand Up @@ -1263,7 +1268,7 @@ pub(crate) async fn setup_redirect<'a>(
redirect: &ast::IoRedirect,
) -> Result<Option<u32>, error::Error> {
match redirect {
ast::IoRedirect::OutputAndError(f) => {
ast::IoRedirect::OutputAndError(f, append) => {
let mut expanded_file_path = expansion::full_expand_and_split_word(shell, f).await?;
if expanded_file_path.len() != 1 {
return Err(error::Error::InvalidRedirection);
Expand All @@ -1274,7 +1279,8 @@ pub(crate) async fn setup_redirect<'a>(
let opened_file = std::fs::File::options()
.create(true)
.write(true)
.truncate(true)
.truncate(!*append)
.append(*append)
.open(expanded_file_path.as_str())
.map_err(|err| error::Error::RedirectionFailure(expanded_file_path, err))?;

Expand Down
21 changes: 14 additions & 7 deletions brush-parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,8 +703,8 @@ pub enum IoRedirect {
HereDocument(Option<u32>, IoHereDocument),
/// Redirection from a here-string.
HereString(Option<u32>, Word),
/// Redirection of both standard output and standard error.
OutputAndError(Word),
/// Redirection of both standard output and standard error (with optional append).
OutputAndError(Word, bool),
}

impl Display for IoRedirect {
Expand All @@ -717,8 +717,12 @@ impl Display for IoRedirect {

write!(f, "{} {}", kind, target)?;
}
IoRedirect::OutputAndError(target) => {
write!(f, "&> {}", target)?;
IoRedirect::OutputAndError(target, append) => {
write!(f, "&>")?;
if *append {
write!(f, ">")?;
}
write!(f, " {}", target)?;
}
IoRedirect::HereDocument(
fd_num,
Expand Down Expand Up @@ -934,11 +938,14 @@ pub enum UnaryPredicate {
FileExistsAndIsWritable,
/// Computes if the operand is a path to an existing file that is executable.
FileExistsAndIsExecutable,
/// Computes if the operand is a path to an existing file owned by the current context's effective group ID.
/// Computes if the operand is a path to an existing file owned by the current context's
/// effective group ID.
FileExistsAndOwnedByEffectiveGroupId,
/// Computes if the operand is a path to an existing file that has been modified since last being read.
/// Computes if the operand is a path to an existing file that has been modified since last
/// being read.
FileExistsAndModifiedSinceLastRead,
/// Computes if the operand is a path to an existing file owned by the current context's effective user ID.
/// Computes if the operand is a path to an existing file owned by the current context's
/// effective user ID.
FileExistsAndOwnedByEffectiveUserId,
/// Computes if the operand is a path to an existing socket file.
FileExistsAndIsSocket,
Expand Down
3 changes: 2 additions & 1 deletion brush-parser/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ peg::parser! {
let (kind, target) = f;
ast::IoRedirect::File(n, kind, target)
} /
non_posix_extensions_enabled() specific_operator("&>") target:filename() { ast::IoRedirect::OutputAndError(ast::Word::from(target)) } /
non_posix_extensions_enabled() specific_operator("&>>") target:filename() { ast::IoRedirect::OutputAndError(ast::Word::from(target), true) } /
non_posix_extensions_enabled() specific_operator("&>") target:filename() { ast::IoRedirect::OutputAndError(ast::Word::from(target), false) } /
non_posix_extensions_enabled() n:io_number()? specific_operator("<<<") w:word() { ast::IoRedirect::HereString(n, ast::Word::from(w)) } /
n:io_number()? h:io_here() { ast::IoRedirect::HereDocument(n, h) } /
expected!("I/O redirect")
Expand Down
2 changes: 1 addition & 1 deletion brush-parser/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ impl<'a, R: ?Sized + std::io::BufRead> Tokenizer<'a, R> {

fn is_operator(&self, s: &str) -> bool {
// Handle non-POSIX operators.
if !self.options.posix_mode && matches!(s, "<<<" | "&>") {
if !self.options.posix_mode && matches!(s, "<<<" | "&>" | "&>>") {
return true;
}

Expand Down
1 change: 1 addition & 0 deletions brush-shell/tests/cases/redirection.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ cases:
- name: "Redirect stdout and stderr"
stdin: |
ls -d . non-existent-dir &>/dev/null
ls -d . non-existent-dir &>>/dev/null

- name: "Process substitution: input + output"
known_failure: true
Expand Down
Loading