Implemented the exec builtin
Fixed readline and terminal interactions using stdin instead of /dev/tty
This commit is contained in:
@@ -158,7 +158,7 @@ pub fn pushd(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> ShResult<
|
||||
let dirs = m.dirs_mut();
|
||||
dirs.push_front(cwd);
|
||||
match idx {
|
||||
StackIdx::FromTop(n) => dirs.rotate_left(n + 1),
|
||||
StackIdx::FromTop(n) => dirs.rotate_left(n),
|
||||
StackIdx::FromBottom(n) => dirs.rotate_right(n + 1),
|
||||
}
|
||||
dirs.pop_front()
|
||||
|
||||
50
src/builtin/exec.rs
Normal file
50
src/builtin/exec.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
use nix::{errno::Errno, unistd::execvpe};
|
||||
|
||||
use crate::{
|
||||
builtin::setup_builtin,
|
||||
jobs::JobBldr,
|
||||
libsh::error::{ShErr, ShErrKind, ShResult},
|
||||
parse::{NdRule, Node, execute::ExecArgs},
|
||||
procio::IoStack,
|
||||
state,
|
||||
};
|
||||
|
||||
pub fn exec_builtin(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> ShResult<()> {
|
||||
let NdRule::Command {
|
||||
assignments: _,
|
||||
argv,
|
||||
} = node.class
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
|
||||
let (expanded_argv, guard) = setup_builtin(argv, job, Some((io_stack, node.redirs)))?;
|
||||
if let Some(g) = guard {
|
||||
// Persist redirections so they affect the entire shell,
|
||||
// not just this command call
|
||||
g.persist()
|
||||
}
|
||||
|
||||
if expanded_argv.is_empty() {
|
||||
state::set_status(0);
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let args = ExecArgs::from_expanded(expanded_argv)?;
|
||||
|
||||
let cmd = &args.cmd.0;
|
||||
let span = args.cmd.1;
|
||||
|
||||
let Err(e) = execvpe(cmd, &args.argv, &args.envp);
|
||||
|
||||
// execvpe only returns on error
|
||||
let cmd_str = cmd.to_str().unwrap().to_string();
|
||||
match e {
|
||||
Errno::ENOENT => {
|
||||
Err(ShErr::full(ShErrKind::CmdNotFound(cmd_str), "", span))
|
||||
}
|
||||
_ => {
|
||||
Err(ShErr::full(ShErrKind::Errno(e), format!("{e}"), span))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -26,11 +26,12 @@ pub mod test; // [[ ]] thing
|
||||
pub mod trap;
|
||||
pub mod zoltraak;
|
||||
pub mod dirstack;
|
||||
pub mod exec;
|
||||
|
||||
pub const BUILTINS: [&str; 24] = [
|
||||
pub const BUILTINS: [&str; 25] = [
|
||||
"echo", "cd", "read", "export", "pwd", "source", "shift", "jobs", "fg", "bg", "alias", "unalias",
|
||||
"return", "break", "continue", "exit", "zoltraak", "shopt", "builtin", "command", "trap",
|
||||
"pushd", "popd", "dirs"
|
||||
"pushd", "popd", "dirs", "exec",
|
||||
];
|
||||
|
||||
/// Sets up a builtin command
|
||||
|
||||
Reference in New Issue
Block a user