From 678b5f7932c6bcbf0e9fd378feea3892829f8138 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Mon, 5 Feb 2024 18:21:00 -0600 Subject: [PATCH] more builtin refactoring --- src/builtin.rs | 5 ++++- src/builtin/which.rs | 26 ++++++++++++++++++++++++++ src/ext.rs | 2 -- src/ext/which.rs | 34 ---------------------------------- src/run.rs | 1 + 5 files changed, 31 insertions(+), 37 deletions(-) create mode 100644 src/builtin/which.rs delete mode 100644 src/ext/which.rs diff --git a/src/builtin.rs b/src/builtin.rs index b1d8ef8..65451fa 100644 --- a/src/builtin.rs +++ b/src/builtin.rs @@ -1,5 +1,6 @@ mod echo; mod printenv; +mod which; use crate::{ error::ExecError, @@ -11,6 +12,7 @@ use std::collections::HashMap; pub enum Builtin { Echo, Printenv, + Which, } impl Builtin { @@ -19,6 +21,7 @@ impl Builtin { match self { Echo => &echo::Echo, Printenv => &printenv::Printenv, + Which => &which::Which, } } } @@ -31,5 +34,5 @@ impl Call for Builtin { pub fn all() -> HashMap<&'static str, Builtin> { use Builtin::*; - HashMap::from([("echo", Echo), ("printenv", Printenv)]) + HashMap::from([("echo", Echo), ("printenv", Printenv), ("which", Which)]) } diff --git a/src/builtin/which.rs b/src/builtin/which.rs new file mode 100644 index 0000000..d3659d9 --- /dev/null +++ b/src/builtin/which.rs @@ -0,0 +1,26 @@ +use crate::{ + error::ExecError, + run::{Call, Context, Value}, +}; +use std::{env, io::Write}; + +pub struct Which; + +impl Call for Which { + fn call(&self, ctx: &mut Context, args: &[Value]) -> Result { + let path = env::var("path").unwrap_or_default(); + let dirs: Vec<&str> = path.split(";").collect(); + + for arg in args { + let name = arg.try_as_str()?; + for d in dirs.iter() { + let dir = std::path::Path::new(d); + let fname = dir.join(name).with_extension("exe"); + if fname.exists() && fname.is_file() { + _ = writeln!(ctx.stdout, "{}", fname.to_str().unwrap()); + } + } + } + Ok(Value::None) + } +} diff --git a/src/ext.rs b/src/ext.rs index e6ffbdf..5fde9ac 100644 --- a/src/ext.rs +++ b/src/ext.rs @@ -1,10 +1,8 @@ mod command; mod tail; -mod which; pub use command::Command; pub use tail::Tail; -pub use which::Which; /* Posix Shell builtins: diff --git a/src/ext/which.rs b/src/ext/which.rs deleted file mode 100644 index 9d9cb35..0000000 --- a/src/ext/which.rs +++ /dev/null @@ -1,34 +0,0 @@ -use crate::ext::Command; -use anyhow::Result; - -pub struct Which {} - -impl Command for Which { - fn name() -> String { - String::from("which") - } - - fn create() -> Self { - Self {} - } - - fn exec(&mut self, args: Vec<&str>) -> Result { - if args.len() > 0 { - let path = std::env::var("path").unwrap(); - let dirs: Vec<&str> = path.split(";").collect(); - for d in dirs { - let dir = std::path::Path::new(d); - let fname = dir.join(args[0]).with_extension("exe"); - if fname.exists() && fname.is_file() { - println!("{}", fname.to_str().unwrap()); - return Ok(true); - } - } - println!("not found: {}", args[0]); - Ok(false) - } else { - println!("what do you want to look for?"); - Ok(false) - } - } -} diff --git a/src/run.rs b/src/run.rs index 8cd2039..37e49cd 100644 --- a/src/run.rs +++ b/src/run.rs @@ -50,6 +50,7 @@ impl Eval for Value { pub struct State { #[allow(unused)] variables: HashMap<&'static str, Value>, + pub(crate) builtins: HashMap<&'static str, builtin::Builtin>, }