From d19fa2c722943fe09c2001d4e9bc6c27acfdc9d3 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Thu, 5 Mar 2026 13:14:46 +0100 Subject: split into builtins --- src/run/mod.rs | 51 +++++++++++---------------------------------------- 1 file changed, 11 insertions(+), 40 deletions(-) (limited to 'src/run/mod.rs') diff --git a/src/run/mod.rs b/src/run/mod.rs index 82409e9..0a51da2 100644 --- a/src/run/mod.rs +++ b/src/run/mod.rs @@ -6,15 +6,17 @@ use std::path::PathBuf; use crate::parse::Ast; use crate::*; +mod builtin; + pub fn run(se: &Session, cmd: Vec) { run_command(&se.raw, cmd); } -pub trait Builtin : Send + Sync { - fn name(&self) -> & str; +pub trait Builtin: Send + Sync { + fn name(&self) -> &str; /// quick synchronous call, `cd` for example - fn mod_session(&self, session: &mut Session) {} + fn mod_session(&self, session: &mut Session, args: &[BString]) {} /// potentially long, pipelineable thread, builtin `cat` for example fn io( @@ -27,7 +29,12 @@ pub trait Builtin : Send + Sync { } } -const BUILTINS: &[&'static dyn Builtin] = &[]; +const BUILTINS: &[&'static dyn Builtin] = &[ + &builtin::Cd, + &builtin::Clear, + #[cfg(debug_assertions)] + &builtin::Re, +]; pub struct CommandDispatch { map: HashMap, @@ -110,42 +117,6 @@ fn run_command(raw: &ScopedRawMode, line: Vec) { todo!("can only handle pipes"); }; - // simple command that can probably be builtin - // TODO: handle builtins uniformly instead of big if case - if pipes.cmds.len() == 1 { - let c = &pipes.cmds[0]; - match &c.cmd[..] { - b"cd" => { - let target: &Path = match c.args.get(0).map(|v| &v[..]) { - Some(b"-") => todo!("prev"), - Some(path) => OsStr::from_bytes(path).as_ref(), - None => todo!("homedir"), - }; - - if let Err(_) = std::env::set_current_dir(target) { - print!("ERR {PROMPT}"); - } else { - print!("{PROMPT}"); - } - - return; - } - b"clear" => clear_screen(), - #[cfg(debug_assertions)] - b"re" => { - // restart shell - raw.disable(); - let _ = Command::new("cargo").arg("run").status(); - raw.disable(); - std::process::exit(0); - } - b"from" => todo!("read from file"), - b"to" | b"into" => todo!("write into file"), - b"append" => todo!("append to file"), - _ => (), - } - } - let mut children: Vec = Vec::new(); let mut prev_stdout = None; let mut spawn_error = false; -- cgit v1.2.3