diff --git a/Cargo.lock b/Cargo.lock index d5edb92..d8e92a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,15 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "aho-corasick" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc936419f96fa211c1b9166887b38e5e40b19958e5b895be7c1f93adec7071ac" +dependencies = [ + "memchr", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -243,6 +252,7 @@ dependencies = [ "dirs", "gethostname", "once_cell", + "regex", "rustyline", "shlex", ] @@ -295,6 +305,23 @@ dependencies = [ "thiserror", ] +[[package]] +name = "regex" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.28" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848" + [[package]] name = "rustix" version = "0.36.6" diff --git a/Cargo.toml b/Cargo.toml index 14fb220..00d7429 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,6 @@ ctrlc = "3.2" dirs = "4.0" gethostname = "0.4" once_cell = "1.17" +regex = "1.7" rustyline = "10.1.0" shlex = "1.1" diff --git a/src/execute.rs b/src/execute.rs index fd3de28..a8b637a 100644 --- a/src/execute.rs +++ b/src/execute.rs @@ -6,12 +6,15 @@ use std::{io, thread}; use crate::builtins::{execute_builtin, is_builtin, BuiltinConfig}; use crate::error::ShellError; use crate::parse::parse_line; +use crate::preprocess::preprocess; pub fn interpret( - line: String, + mut line: String, config: &mut BuiltinConfig, ctrlc_recv: Receiver<()>, ) -> Result<(), ShellError> { + line = preprocess(line)?; + if line.is_empty() { return Err(ShellError::EmptyLine); } diff --git a/src/main.rs b/src/main.rs index 17cc86a..8a76ee8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ mod builtins; mod error; mod execute; mod parse; +mod preprocess; mod prompt; use crate::builtins::BuiltinConfig; diff --git a/src/preprocess.rs b/src/preprocess.rs new file mode 100644 index 0000000..6541726 --- /dev/null +++ b/src/preprocess.rs @@ -0,0 +1,21 @@ +use crate::error::ShellError; +use once_cell::unsync::Lazy; +use regex::Regex; +use std::env; + +const ENV_SET: Lazy = Lazy::new(|| Regex::new(r#"(?P\w+)=(?P\w*)"#).unwrap()); + +pub fn preprocess(line: String) -> Result { + if let Some(capture) = ENV_SET.captures(&line) { + let Some(key) = capture.name("key") else { + return Err(ShellError::MalformedArgs("cannot find key to set env variable".to_string())) + }; + let value = capture + .name("value") + .map_or("".to_string(), |v| v.as_str().to_string()); + + env::set_var(key.as_str(), value) + } + + Ok(line) +}