Early work on scripting features

This commit is contained in:
2025-03-16 02:56:29 -04:00
parent c0fb3f5914
commit 92d7766765
14 changed files with 376 additions and 132 deletions

View File

@@ -1,4 +1,4 @@
use crate::{jobs::{ChildProc, JobBldr}, libsh::error::ShResult, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}};
use crate::{builtin::setup_builtin, jobs::{ChildProc, JobBldr}, libsh::error::ShResult, parse::{execute::prepare_argv, NdRule, Node}, prelude::*, procio::{borrow_fd, IoStack}, state};
pub fn echo(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> ShResult<()> {
let NdRule::Command { assignments: _, argv } = node.class else {
@@ -6,33 +6,22 @@ pub fn echo(node: Node, io_stack: &mut IoStack, job: &mut JobBldr) -> ShResult<(
};
assert!(!argv.is_empty());
let child_pgid = if let Some(pgid) = job.pgid() {
pgid
} else {
job.set_pgid(Pid::this());
Pid::this()
};
let child = ChildProc::new(Pid::this(), Some("echo"), Some(child_pgid))?;
job.push_child(child);
io_stack.append_to_frame(node.redirs);
let mut io_frame = io_stack.pop_frame();
io_frame.redirect()?;
let (argv,io_frame) = setup_builtin(argv, job, Some((io_stack,node.redirs)))?;
let stdout = borrow_fd(STDOUT_FILENO);
flog!(DEBUG, argv);
let mut echo_output = prepare_argv(argv)
.into_iter()
let mut echo_output = argv.into_iter()
.map(|a| a.0) // Extract the String from the tuple of (String,Span)
.skip(1) // Skip 'echo'
.collect::<Vec<_>>()
.join(" ");
echo_output.push('\n');
flog!(DEBUG, echo_output);
write(stdout, echo_output.as_bytes())?;
io_frame.restore()?;
io_frame.unwrap().restore()?;
state::set_status(0);
Ok(())
}