From 01b83996d1ecb4f531fa8a3560d6b99a076be5c1 Mon Sep 17 00:00:00 2001 From: jasmine Date: Sat, 7 Jun 2025 15:05:59 +0800 Subject: [PATCH] add some basic inbuilt commands --- src/inbuilt.rs | 15 +++++++++++++++ src/main.rs | 9 +++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 src/inbuilt.rs diff --git a/src/inbuilt.rs b/src/inbuilt.rs new file mode 100644 index 0000000..57d7b4a --- /dev/null +++ b/src/inbuilt.rs @@ -0,0 +1,15 @@ +use std::env; + +pub fn cd(args: Vec<&str>) { + let path = args[0].to_string(); + assert!(env::set_current_dir(&path).is_ok()); +} + +pub fn help() { + println!("This is an example help message") +} + +pub fn exit() { + std::process::exit(0x0100) +} + diff --git a/src/main.rs b/src/main.rs index 60a9dd9..95d0ad1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,12 @@ fn main() { let cmd = core::get_command(tokens.clone()); let args = core::get_args(tokens); - // Execute command and argument. - core::execute(cmd, args); + // Execute inbuilt commands if supplied, otherwise execute command and argument. + match cmd { + "cd" => inbuilt::cd(args), + "help" => inbuilt::help(), + "exit" => inbuilt::exit(), + _ => core::execute(cmd, args), + } } }