diff --git a/shell/src/cmd/commands.rs b/shell/src/cmd/mod.rs similarity index 78% rename from shell/src/cmd/commands.rs rename to shell/src/cmd/mod.rs index a4cc8a8..e05bd73 100644 --- a/shell/src/cmd/commands.rs +++ b/shell/src/cmd/mod.rs @@ -1,8 +1,13 @@ // Copyleft 🄯 2024 Adam Perkowski -use std::io; -use std::io::Write; -use std::process; +// use std::io; +// use std::io::Write; +// use std::process; + +extern crate alloc; +use alloc::vec::Vec; + +use hlshell::{print, println}; pub struct Command { pub name: &'static str, @@ -56,23 +61,23 @@ fn cc(_args: Vec<&str>) -> i32 { 0 } -fn exit_hls(_args: Vec<&str>) -> i32 { - print!("are you sure you want to exit? [ y/N ] < "); +// fn exit_hls(_args: Vec<&str>) -> i32 { +// print!("are you sure you want to exit? [ y/N ] < "); - let mut inpt = String::new(); +// let mut inpt = String::new(); - io::stdout().flush().unwrap(); - io::stdin().read_line(&mut inpt).unwrap(); +// io::stdout().flush().unwrap(); +// io::stdin().read_line(&mut inpt).unwrap(); - if inpt.to_lowercase() == "y\n" { - println!(); - process::exit(0); - } - // return 0 - else { - 3 - } -} +// if inpt.to_lowercase() == "y\n" { +// println!(); +// process::exit(0); +// } +// // return 0 +// else { +// 3 +// } +// } fn document(_args: Vec<&str>) -> i32 { if !_args.is_empty() { @@ -114,12 +119,12 @@ pub const COMMAND_LIST: &[Command] = &[ doc: "display copyright info", fun: cc, }, - Command { - name: "exit", - args: "", - doc: "exit the shell :((", - fun: exit_hls, - }, + // Command { + // name: "exit", + // args: "", + // doc: "exit the shell :((", + // fun: exit_hls, + // }, Command { name: "getdoc", args: "[cmd]", diff --git a/shell/src/main.rs b/shell/src/main.rs index dc7c0c8..7aa824e 100644 --- a/shell/src/main.rs +++ b/shell/src/main.rs @@ -4,13 +4,24 @@ #![warn(clippy::new_without_default)] #![warn(clippy::missing_safety_doc)] +extern crate alloc; +use alloc::{string::String, vec}; + use bootloader::{entry_point, BootInfo}; use core::panic::PanicInfo; use hlshell::{print, println}; +mod cmd; +use cmd::COMMAND_LIST; + entry_point!(kernel_main); +struct RtrType { + code: &'static i32, + info: &'static str, +} + fn kernel_main(boot_info: &'static BootInfo) -> ! { use hlshell::allocator; use hlshell::mem::{self, BootInfoFrameAlloc}; @@ -27,14 +38,61 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { allocator::init_heap(&mut mapper, &mut frame_allocator).expect("Heap initialization failed"); - print!( - "\nHighlightOS Shell v{}\n\nhls < ", - env!("CARGO_PKG_VERSION") - ); + // let args: vec::Vec<&str> = vec![""]; + // (COMMAND_LIST[1].fun)(args); + + print!("\nHighlightOS Shell v{}\n\n", env!("CARGO_PKG_VERSION")); + + loop { + let mut input = String::new(); + print!("hls < "); + + // io::stdout().flush().unwrap(); + // io::stdin().read_line(&mut inpt).unwrap(); + + // input.pop(); + + let mut args: vec::Vec<&str> = input.split(' ').collect(); + + if let Some(command) = COMMAND_LIST.iter().find(|&com| com.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) { + println!("\n > {}\n{}\n", input, return_code.info); + } else { + println!("\n > {}\nreturned : {}\n", input, rtr); + } + } + } else { + println!("\n > {}\ncommand not found\n", input); + } + } hlshell::hlt_loop(); } +const RTR_LIST: &[RtrType] = &[ + RtrType { + code: &0, + info: "executed successfully", + }, + RtrType { + code: &2, + info: "returned general error", + }, + RtrType { + code: &3, + info: "returned critical error", + }, + RtrType { + code: &4, + info: "returned user error", + }, +]; + #[panic_handler] fn panic(info: &PanicInfo) -> ! { println!("{}", info);