Skip to content

Commit

Permalink
commands integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
adamperkowski committed Jul 19, 2024
1 parent 3165508 commit 4edaa57
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 27 deletions.
51 changes: 28 additions & 23 deletions shell/src/cmd/commands.rs → shell/src/cmd/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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]",
Expand Down
66 changes: 62 additions & 4 deletions shell/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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);
Expand Down

0 comments on commit 4edaa57

Please sign in to comment.