From d8ab85e3ecc5253e44dccbaf79db0e70c929c23d Mon Sep 17 00:00:00 2001 From: Michael Aaron Murphy <mmstickman@gmail.com> Date: Sat, 29 Jul 2017 18:51:45 -0400 Subject: [PATCH] Implement contains, starts_with, and ends_with builtins --- src/builtins/conditionals.rs | 26 ++++++++++++++++++++++++++ src/builtins/mod.rs | 17 +++++++++++++++++ src/shell/pipe_exec/job_control.rs | 2 +- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/builtins/conditionals.rs diff --git a/src/builtins/conditionals.rs b/src/builtins/conditionals.rs new file mode 100644 index 00000000..5e2aa643 --- /dev/null +++ b/src/builtins/conditionals.rs @@ -0,0 +1,26 @@ +use shell::Shell; +use shell::status::*; + +macro_rules! string_function { + ($method:tt) => ( + pub fn $method(args: &[&str], _: &mut Shell) -> i32 { + match args.len() { + 0...2 => { + eprintln!("ion: $method: two arguments must be supplied"); + return BAD_ARG + } + 3 => if args[1].$method(&args[2]) { SUCCESS } else { FAILURE } + _ => { + for arg in args[2..].iter() { + if args[1].$method(arg) { return SUCCESS } + } + FAILURE + } + } + } + ) +} + +string_function!(starts_with); +string_function!(ends_with); +string_function!(contains); \ No newline at end of file diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs index ffc0d4fa..28d66f0f 100644 --- a/src/builtins/mod.rs +++ b/src/builtins/mod.rs @@ -3,12 +3,14 @@ pub mod variables; pub mod functions; pub mod calc; +mod conditionals; mod job_control; mod test; mod time; mod echo; mod set; +use self::conditionals::{starts_with, ends_with, contains}; use self::variables::{alias, drop_alias, drop_variable, drop_array}; use self::functions::fn_; use self::source::source; @@ -172,6 +174,21 @@ impl Builtin { builtin_or, "Execute the command if the shell's previous status is failure" ); + insert_builtin!( + "starts_with", + starts_with, + "Evaluates if the supplied argument starts with a given string" + ); + insert_builtin!( + "ends_with", + ends_with, + "Evaluates if the supplied argument ends with a given string" + ); + insert_builtin!( + "contains", + contains, + "Evaluates if the supplied argument contains a given string" + ); commands } diff --git a/src/shell/pipe_exec/job_control.rs b/src/shell/pipe_exec/job_control.rs index c1b4ba3d..e9b815ce 100644 --- a/src/shell/pipe_exec/job_control.rs +++ b/src/shell/pipe_exec/job_control.rs @@ -125,7 +125,7 @@ impl<'a> JobControl for Shell<'a> { /// Waits until all running background tasks have completed, and listens for signals in the /// event that a signal is sent to kill the running tasks. fn wait_for_background(&mut self) { - let mut sigcode = 0; + let sigcode; 'event: loop { for process in self.background.lock().unwrap().iter() { if let ProcessState::Running = process.state { -- GitLab