From 360886fac94a4d834483459703ed68729b6991ac Mon Sep 17 00:00:00 2001 From: RaindropsSys Date: Sat, 25 Nov 2023 23:11:32 +0100 Subject: Updated 7 files and added src/error.rs (automated) --- .DS_Store | Bin 10244 -> 10244 bytes esp/.DS_Store | Bin 6148 -> 10244 bytes esp/efi/.DS_Store | Bin 6148 -> 8196 bytes esp/efi/boot/bootx64.efi | Bin 155648 -> 172032 bytes src/core.rs | 37 +++++++++++++++++++---- src/error.rs | 33 +++++++++++++++++++++ src/main.rs | 1 + src/shell.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++-- 8 files changed, 138 insertions(+), 8 deletions(-) create mode 100644 src/error.rs diff --git a/.DS_Store b/.DS_Store index 9ca7ba7..d2d2c28 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/esp/.DS_Store b/esp/.DS_Store index 8975e2a..c289e6e 100644 Binary files a/esp/.DS_Store and b/esp/.DS_Store differ diff --git a/esp/efi/.DS_Store b/esp/efi/.DS_Store index 68f40be..eeb3129 100644 Binary files a/esp/efi/.DS_Store and b/esp/efi/.DS_Store differ diff --git a/esp/efi/boot/bootx64.efi b/esp/efi/boot/bootx64.efi index 934f24c..7baf314 100755 Binary files a/esp/efi/boot/bootx64.efi and b/esp/efi/boot/bootx64.efi differ diff --git a/src/core.rs b/src/core.rs index 9f0d376..75b0ded 100644 --- a/src/core.rs +++ b/src/core.rs @@ -1,5 +1,6 @@ use alloc::string::{String, ToString}; -use alloc::vec; +use alloc::{format, vec}; +use alloc::fmt::format; use alloc::vec::Vec; use uefi::{Char16, CStr16}; use uefi::prelude::*; @@ -44,6 +45,7 @@ impl Toolkit { impl Toolkit { pub fn prompt(&mut self) -> String { let mut out: String = String::from(""); + let mut chars: u32 = 0; loop { let mut events = [self.system_table.stdin().wait_for_key_event().unwrap()]; @@ -52,13 +54,29 @@ impl Toolkit { .discard_errdata().expect("Failed to discard errors"); let ret = Char16::try_from('\r').unwrap(); + let bks = Char16::try_from('\x08').unwrap(); + let ctc = Char16::try_from('\u{3}').unwrap(); match self.system_table.stdin().read_key().expect("Failed to read key") { Some(Key::Printable(key)) if key == ret => { self.print("\r\n"); return out; } + Some(Key::Printable(key)) if key == bks => { + if chars > 0 { + chars -= 1; + out = String::from(&out[..out.len() - 1]); + self.print("\x08"); + } + } + + Some(Key::Printable(key)) if key == ctc => { + self.print("\r\n"); + return String::from(""); + } + Some(Key::Printable(key)) => { + chars += 1; out += &key.to_string(); self.print(&key.to_string()); } @@ -86,12 +104,15 @@ impl Toolkit { } pub fn chdir(&mut self, dir: &str) { + if dir == "." { + return; + } + if !self.file_exists(dir) { panic!("Attempted to change the working directory to a non-existent directory"); } - self.current_directory.name += "/"; - self.current_directory.name += dir; + self.current_directory.name = format!("/{}", dir).replace("\\", "/"); } pub fn uefi_version(&mut self) -> String { @@ -122,11 +143,17 @@ impl Toolkit { } pub fn file_exists(&self, path: &str) -> bool { + if path == "" { + return true; + } let handle = self.system_table.boot_services().image_handle(); let fs = self.system_table.boot_services().get_image_file_system(handle).expect("Failed to start up filesystem"); let mut filesystem = FileSystem::new(fs); let mut buf: Vec = vec![0; path.len() + 1]; - return filesystem.try_exists(&Path::new(&CStr16::from_str_with_buf("CutieOS", &mut buf).unwrap())).unwrap(); + return match filesystem.try_exists(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())) { + Ok(b) => b, + Err(_) => false + }; } pub fn mkdir(&self, path: &str) { @@ -134,7 +161,7 @@ impl Toolkit { let fs = self.system_table.boot_services().get_image_file_system(handle).expect("Failed to start up filesystem"); let mut filesystem = FileSystem::new(fs); let mut buf: Vec = vec![0; path.len() + 1]; - filesystem.create_dir(&Path::new(&CStr16::from_str_with_buf("CutieOS", &mut buf).unwrap())).unwrap(); + filesystem.create_dir(&Path::new(&CStr16::from_str_with_buf(path, &mut buf).unwrap())).unwrap(); } pub fn print(&mut self, str: &str) { diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..af96ee9 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,33 @@ +use alloc::format; +use crate::core::Toolkit; + +#[derive(Copy, Clone)] +pub enum Error { + //E01, + E02, + E03, + E04, + E05 +} + +impl Error { + fn description(&self) -> &str { + match self { + //Error::E01 => "An internal undocumented system error has occurred.", + Error::E02 => "The requested command could not be found.", + Error::E03 => "A required paramater for this command was not provided.", + Error::E04 => "The requested file or directory could not be found.", + Error::E05 => "Unable to go up one level from the root directory.", + //_ => "Unknown error." + } + } + + fn code (&self) -> u32 { + (*self as u32) + 1 + } +} + +pub fn display_error(tk: &mut Toolkit, error: Error) { + tk.println(&format!("Error {}: {}", error.code(), error.description())); + tk.println(""); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index cf11852..625aca2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod core; mod panic; mod shell; mod boot; +mod error; extern crate alloc; diff --git a/src/shell.rs b/src/shell.rs index a646c76..b72dcec 100644 --- a/src/shell.rs +++ b/src/shell.rs @@ -1,12 +1,81 @@ -use alloc::format; +use alloc::{format, vec}; +use alloc::borrow::ToOwned; +use alloc::string::ToString; +use uefi::CStr16; +use uefi::fs::Path; use crate::core::Toolkit; +use crate::error::*; pub fn run_shell(mut tk: Toolkit) { tk.println(""); loop { tk.print(&format!("{}> ", tk.get_cwd())); - let text = &tk.prompt(); - tk.println(&format!("You said: {}", text)); + let mut text: &str = &tk.prompt(); + text = text.trim(); + + if text == "" { + continue; + } + + if text.starts_with("cd") { + if text.starts_with("cd ") && text.len() > 3 { + let path = &text[3..].replace("/", "\\"); + let mut buf = vec![0; path.len() + 1]; + let cstr = CStr16::from_str_with_buf(path, &mut buf).unwrap(); + let path = Path::new(cstr); + let mut first = true; + + for i in path.components() { + if text[3..].starts_with("/") && first { + first = false; + + if tk.file_exists(&i.to_string()) { + tk.chdir(&i.to_string()); + continue; + } else { + display_error(&mut tk, Error::E04); + } + } else if tk.get_cwd() == "/" && i.to_string() == ".." { + display_error(&mut tk, Error::E05); + continue; + } else if i.to_string() == "" || tk.get_cwd() == "/" { + if tk.file_exists(&i.to_string()) { + tk.chdir(&i.to_string()); + continue; + } else { + display_error(&mut tk, Error::E04); + } + } else if i.to_string() == ".." { + let cwd = &tk.get_cwd().replace("/", "\\"); + let mut buf = vec![0; cwd.len() + 1]; + let cstr = CStr16::from_str_with_buf(cwd, &mut buf).unwrap(); + let path = Path::new(cstr); + tk.chdir(&path.parent().unwrap().to_cstr16().to_string()); + continue; + } else { + let cwd = (&tk.get_cwd()[1..]).to_owned(); + + if tk.file_exists(&format!("{}\\{}", cwd, i)) { + tk.chdir(&format!("{}\\{}", cwd, i)); + continue; + } else { + display_error(&mut tk, Error::E04); + } + } + } + + continue; + } else { + display_error(&mut tk, Error::E03); + continue; + } + } else if text.starts_with("pwd ") || text == "pwd" { + let cwd = tk.get_cwd().to_string(); + tk.println(&cwd); + continue; + } + + display_error(&mut tk, Error::E02); } } \ No newline at end of file -- cgit