From ddcdab1f51c3f809ffb30d5efc1a439cbf2b6348 Mon Sep 17 00:00:00 2001 From: Adam Perkowski Date: Sun, 14 Jul 2024 20:59:41 +0200 Subject: [PATCH] Command arguments work --- shell/src/commands.rs | 41 ++++++++++++++++++++++++++++++++++------- shell/src/main.rs | 18 +++++++++++++++--- 2 files changed, 49 insertions(+), 10 deletions(-) diff --git a/shell/src/commands.rs b/shell/src/commands.rs index 60013ee..17e3b9e 100644 --- a/shell/src/commands.rs +++ b/shell/src/commands.rs @@ -6,16 +6,17 @@ use std::process; pub struct Command { pub name: &'static str, + pub args: &'static str, pub doc: &'static str, - pub fun: fn() -> i32, + pub fun: fn(Vec<&str>) -> i32, } -fn clrs() -> i32 { +fn clrs(_args: Vec<&str>) -> i32 { print!("\x1B[2J\x1B[1;1H"); return 1; } -fn help() -> i32 { +fn help(_args: Vec<&str>) -> i32 { println!( "HighlightOS Shell @@ -23,18 +24,18 @@ fn help() -> i32 { ); for cmd in COMMAND_LIST { - println!(". {} >> {}", cmd.name, cmd.doc); + println!(". {} {} >> {}", cmd.name, cmd.args, cmd.doc); } return 0; } -fn test() -> i32 { +fn test(_args: Vec<&str>) -> i32 { println!("hello. this is a test command. it's life goal is to always return 2."); return 2; } -fn cc() -> i32 { +fn cc(_args: Vec<&str>) -> i32 { println!( "Copyright (C) 2024 Adam Perkowski @@ -55,7 +56,7 @@ fn cc() -> i32 { return 0; } -fn exit_hls() -> i32 { +fn exit_hls(_args: Vec<&str>) -> i32 { print!("are you sure you want to exit? [ y/N ] < "); let mut inpt = String::new(); @@ -73,30 +74,56 @@ fn exit_hls() -> i32 { } } +fn document(_args: Vec<&str>) -> i32 { + if _args.len() != 0 { + if let Some(command) = COMMAND_LIST.iter().find(|&cmd| cmd.name == _args[0]) { + println!("{} >> {}", command.name, command.doc); + return 0; + } else { + println!("Command not found."); + return 3; + } + } else { + println!("No command specified."); + return 4; + } +} + pub const COMMAND_LIST: &[Command] = &[ Command { name: "clrs", + args: "", doc: "clear screen", fun: clrs, }, Command { name: "help", + args: "", doc: "show list of commands", fun: help, }, Command { name: "test", + args: "", doc: "test :)", fun: test, }, Command { name: "cc", + args: "", doc: "display copyright info", fun: cc, }, Command { name: "exit", + args: "", doc: "exit the shell :((", fun: exit_hls, }, + Command { + name: "getdoc", + args: "[cmd]", + doc: "display doc of selected command", + fun: document, + }, ]; diff --git a/shell/src/main.rs b/shell/src/main.rs index 349d239..029c3f1 100644 --- a/shell/src/main.rs +++ b/shell/src/main.rs @@ -13,7 +13,7 @@ struct RtrType { } fn main() { - (COMMAND_LIST[0].fun)(); + // (COMMAND_LIST[0].fun)(); println!("HighlightOS Shell\nversion {}\n", env!("CARGO_PKG_VERSION")); @@ -27,8 +27,12 @@ fn main() { inpt.pop(); - if let Some(command) = COMMAND_LIST.iter().find(|&cmd| cmd.name == inpt) { - let rtr = (command.fun)(); + let mut args: Vec<&str> = inpt.split(" ").collect(); + + if let Some(command) = COMMAND_LIST.iter().find(|&cmd| cmd.name == args[0]) { + args.remove(0); + + let rtr = (command.fun)(args); if rtr != 1 { if let Some(return_code) = RTR_LIST.iter().find(|&rtr_t| rtr_t.code == &rtr) { @@ -52,4 +56,12 @@ const RTR_LIST: &[RtrType] = &[ code: &2, info: "returned general error", }, + RtrType { + code: &3, + info: "returned critical error", + }, + RtrType { + code: &4, + info: "returned user error", + }, ];