Skip to content
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
20 changes: 15 additions & 5 deletions crates/bashkit/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3743,13 +3743,20 @@ impl Interpreter {
});
}
for redirect in redirects {
// Resolve fd from either explicit fd or {var} fd-variable syntax
let resolved_fd_var: Option<i32> = redirect.fd_var.as_ref().and_then(|var_name| {
self.variables
.get(var_name)
.and_then(|val| val.parse::<i32>().ok())
});
match redirect.kind {
RedirectKind::Input => {
let target_path = self.expand_word(&redirect.target).await?;
let path = self.resolve_path(&target_path);
let content = self.fs.read_file(&path).await?;
let text = bytes_to_latin1_string(&content);
if let Some(fd) = redirect.fd {
let fd = redirect.fd.or(resolved_fd_var);
if let Some(fd) = fd {
let lines: Vec<String> =
text.lines().rev().map(|l| l.to_string()).collect();
self.coproc_buffers.insert(fd, lines);
Expand All @@ -3760,14 +3767,15 @@ impl Interpreter {
}
RedirectKind::DupInput => {
let target = self.expand_word(&redirect.target).await?;
let fd = redirect.fd.or(resolved_fd_var);
if target == "-"
&& let Some(fd) = redirect.fd
&& let Some(fd) = fd
{
self.coproc_buffers.remove(&fd);
}
}
RedirectKind::Output | RedirectKind::Clobber => {
let fd = redirect.fd.unwrap_or(1);
let fd = redirect.fd.or(resolved_fd_var).unwrap_or(1);
let target_path = self.expand_word(&redirect.target).await?;
let path = self.resolve_path(&target_path);
if is_dev_null(&path) {
Expand All @@ -3780,7 +3788,7 @@ impl Interpreter {
}
}
RedirectKind::Append => {
let fd = redirect.fd.unwrap_or(1);
let fd = redirect.fd.or(resolved_fd_var).unwrap_or(1);
let target_path = self.expand_word(&redirect.target).await?;
let path = self.resolve_path(&target_path);
if is_dev_null(&path) {
Expand All @@ -3792,7 +3800,7 @@ impl Interpreter {
}
RedirectKind::DupOutput => {
let target = self.expand_word(&redirect.target).await?;
let fd = redirect.fd.unwrap_or(1);
let fd = redirect.fd.or(resolved_fd_var).unwrap_or(1);
if target == "-" {
// exec N>&- closes the fd
self.exec_fd_table.remove(&fd);
Expand Down Expand Up @@ -5508,6 +5516,7 @@ impl Interpreter {
let inner_redirects = if let Some(ref stdin_data) = command.stdin {
vec![Redirect {
fd: None,
fd_var: None,
kind: RedirectKind::HereString,
target: Word::literal(stdin_data.trim_end_matches('\n').to_string()),
}]
Expand Down Expand Up @@ -5551,6 +5560,7 @@ impl Interpreter {
let cmd_redirects = if let Some(ref stdin_data) = cmd.stdin {
vec![Redirect {
fd: None,
fd_var: None,
kind: RedirectKind::HereString,
target: Word::literal(stdin_data.trim_end_matches('\n').to_string()),
}]
Expand Down
4 changes: 4 additions & 0 deletions crates/bashkit/src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ pub enum ParameterOp {
pub struct Redirect {
/// File descriptor (default: 1 for output, 0 for input)
pub fd: Option<i32>,
/// Variable name for `{var}` fd-variable redirects (e.g. `exec {myfd}>&-`)
pub fd_var: Option<String>,
/// Type of redirection
pub kind: RedirectKind,
/// Target (file, fd, or heredoc content)
Expand Down Expand Up @@ -921,6 +923,7 @@ mod tests {
args: vec![Word::literal("hi")],
redirects: vec![Redirect {
fd: Some(1),
fd_var: None,
kind: RedirectKind::Output,
target: Word::literal("out.txt"),
}],
Expand Down Expand Up @@ -1049,6 +1052,7 @@ mod tests {
fn redirect_default_fd_none() {
let r = Redirect {
fd: None,
fd_var: None,
kind: RedirectKind::Input,
target: Word::literal("input.txt"),
};
Expand Down
Loading
Loading