From bfe7dffe507bb2565cf9feb92b46da6059b7757b Mon Sep 17 00:00:00 2001
From: Sag0Sag0 <Sag0Sag0@users.noreply.github.com>
Date: Mon, 27 Nov 2017 13:59:13 +1100
Subject: [PATCH] Add builtin bool https://github.com/redox-os/ion/issues/409
 (#606)

* Add builtin bool

* Add variable support for builtin bool
---
 1                              |  0
 manual/src/ch11-00-builtins.md | 10 +++++++++-
 src/builtins/mod.rs            | 31 +++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 1

diff --git a/1 b/1
new file mode 100644
index 00000000..e69de29b
diff --git a/manual/src/ch11-00-builtins.md b/manual/src/ch11-00-builtins.md
index e9dd023a..aa791398 100644
--- a/manual/src/ch11-00-builtins.md
+++ b/manual/src/ch11-00-builtins.md
@@ -403,4 +403,12 @@ Evaluates the current runtime status
 
 - **-l**: returns true if shell is a login shell
 - **-i**: returns true if shell is interactive
-- **-f**: prints the filename of the currently running script or stdio
\ No newline at end of file
+- **-f**: prints the filename of the currently running script or stdio
+
+## bool
+
+```
+bool VALUE
+```
+
+If the value is '1' or 'true', return 0 exit status
\ No newline at end of file
diff --git a/src/builtins/mod.rs b/src/builtins/mod.rs
index 0c12399d..a01e1430 100644
--- a/src/builtins/mod.rs
+++ b/src/builtins/mod.rs
@@ -59,6 +59,7 @@ pub const BUILTINS: &'static BuiltinMap = &map!(
     "alias" => builtin_alias : "View, set or unset aliases",
     "and" => builtin_and : "Execute the command if the shell's previous status is success",
     "bg" => builtin_bg : "Resumes a stopped background process",
+    "bool" => builtin_bool : "If the value is '1' or 'true', return 0 exit status",
     "calc" => builtin_calc : "Calculate a mathematical expression",
     "cd" => builtin_cd : "Change the current directory\n    cd <path>",
     "contains" => contains : "Evaluates if the supplied argument contains a given string",
@@ -152,6 +153,36 @@ pub fn builtin_cd(args: &[&str], shell: &mut Shell) -> i32 {
     }
 }
 
+fn builtin_bool(args: &[&str], shell: &mut Shell) -> i32 {
+    if args.len() != 2 {
+        let stderr = io::stderr();
+        let mut stderr = stderr.lock();
+        let _ = stderr.write_all(b"bool requires one argument\n");
+        return FAILURE
+    }
+
+    let opt = shell.variables.get_var(args[1]);
+    let sh_var: &str = match opt.as_ref() {
+        Some(s) => s,
+        None => "",
+    };
+
+    let help_msg = "DESCRIPTION: If the value is '1' or 'true', bool returns the 0 exit status\nusage: bool <value>";
+    match sh_var {
+        "1" => (),
+        "true" => (),
+        _ => match args[1] {
+            "1" => (),
+            "true" => (),
+            "--help" => println!("{}", help_msg),
+            "-h" => println!("{}", help_msg),
+            _ => return FAILURE
+        }
+    }
+
+    SUCCESS
+}
+
 fn builtin_dirs(args: &[&str], shell: &mut Shell) -> i32 { shell.directory_stack.dirs(args) }
 
 fn builtin_pushd(args: &[&str], shell: &mut Shell) -> i32 {
-- 
GitLab