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
27 changes: 27 additions & 0 deletions src/builtins/builtin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use std::future::Future;
use std::pin::Pin;

use crate::commands::CommandResult;
use crate::os::{Kernel, Process};

/// Run the named builtin, bypassing any shell function of the same name.
pub fn builtin_builtin<'a>(
os: &'a dyn Kernel,
proc: &'a mut Process,
args: &'a [String],
) -> Pin<Box<dyn Future<Output = CommandResult> + 'a>> {
Box::pin(async move {
let Some(name) = args.first() else {
return Ok(0);
};
match super::lookup(name) {
Some(f) => f(os, proc, &args[1..]).await,
None => {
proc.err_msg(&format!(
"strands-shell: builtin: {name}: not a shell builtin"
));
Ok(1)
}
}
})
}
2 changes: 2 additions & 0 deletions src/builtins/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod alias;
mod builtin;
mod cd;
mod colon;
mod echo;
Expand Down Expand Up @@ -37,6 +38,7 @@ pub fn lookup(name: &str) -> Option<BuiltinFn> {
match name {
":" | "true" => Some(colon::builtin_colon),
"alias" => Some(alias::builtin_alias),
"builtin" => Some(builtin::builtin_builtin),
"cd" => Some(cd::builtin_cd),
"echo" => Some(echo::builtin_echo),
"export" => Some(export::builtin_export),
Expand Down
28 changes: 28 additions & 0 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2224,6 +2224,34 @@ async fn execute_pipeline_checked(
if pipeline.len() == 1 && !pipeline[0].args.is_empty() {
let name_word = &pipeline[0].args[0];
if let Some(name) = parser::word_to_str(name_word) {
// `builtin NAME` for a builtin handled below drops the prefix and runs NAME.
if name == "builtin"
&& pipeline[0]
.args
.get(1)
.and_then(parser::word_to_str)
.is_some_and(|inner| {
matches!(
inner.as_str(),
"break"
| "continue"
| "exit"
| "return"
| "eval"
| "."
| "source"
| "exec"
| "command"
)
})
{
let unprefixed = parser::Command {
env: pipeline[0].env.clone(),
args: pipeline[0].args[1..].to_vec(),
redirects: pipeline[0].redirects.clone(),
};
return Box::pin(execute_pipeline_checked(os, proc, &[unprefixed])).await;
}
match name.as_str() {
"break" => {
let n = get_numeric_arg(os.clone(), proc, pipeline).await;
Expand Down
47 changes: 47 additions & 0 deletions tests/shell_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,6 +1512,53 @@ expect!(
"1"
);

// ── builtin builtin ─────────────────────────────────────────────────

expect!(builtin_runs_echo, "builtin echo hello", "hello");
expect!(builtin_passes_args, "builtin printf '%s-%s\\n' a b", "a-b");
expect!(builtin_keeps_status, "builtin false; echo $?", "1");
expect!(builtin_no_args, "builtin", "");
expect!(
builtin_bypasses_function,
"echo() { printf 'shadowed\\n'; }; builtin echo real",
"real"
);
expect!(
builtin_in_wrapper_function,
"go() { builtin cd \"$@\"; }; go /tmp; pwd",
"/tmp"
);
expect!(
builtin_special_return,
"f() { builtin return 4; echo unreachable; }; f; echo $?",
"4"
);
expect_status!(builtin_special_exit, "builtin exit 3; echo unreachable", 3);

shell_test!(
builtin_rejects_command,
"builtin cat /dev/null",
|_shell: &mut Shell, out: strands_shell::Output| {
assert_eq!(out.status, 1);
assert_eq!(
out.stderr.trim(),
"strands-shell: builtin: cat: not a shell builtin"
);
}
);

shell_test!(
builtin_rejects_unknown,
"builtin nosuch_xyz",
|_shell: &mut Shell, out: strands_shell::Output| {
assert_eq!(out.status, 1);
assert_eq!(
out.stderr.trim(),
"strands-shell: builtin: nosuch_xyz: not a shell builtin"
);
}
);

// ── Redirections: read-write, dup, close ────────────────────────────

expect!(
Expand Down
Loading