diff --git a/1 b/1 new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/manual/src/ch11-00-builtins.md b/manual/src/ch11-00-builtins.md index e9dd023a3dbe2341cfb159600b1e5910678fd76b..aa791398d93a40feb8689fc5f5a9751c6d924c20 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 0c12399d19e8a05897f81bf47537863c41ba5082..a01e1430c1c30a97c5790638b9b4777c9eca9710 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 {