From 353d759e94a606b4c0b46b918c76d20f3dd73abf Mon Sep 17 00:00:00 2001 From: Able Date: Tue, 28 Dec 2021 02:56:29 -0600 Subject: [PATCH] colorify integrated into print --- ableos/Cargo.lock | 2 +- ableos/src/arch/x86_64/drivers/vga.rs | 8 +++- ableos/src/arch/x86_64/init.rs | 3 +- ableos/src/arch/x86_64/interrupts.rs | 19 ++++++-- ableos/src/dirty_hacks/mod.rs | 4 -- ableos/src/experiments/absi.rs | 7 ++- ableos/src/experiments/clparse.rs | 69 +++++++++++++++++++++++++++ ableos/src/kmain.rs | 11 +++-- ableos/src/lib.rs | 4 +- ableos/src/log.rs | 49 ++++++++++++++----- ableos/src/print.rs | 6 +-- ableos/src/scheduler.rs | 2 +- ableos/src/wasm/mod.rs | 2 +- 13 files changed, 149 insertions(+), 37 deletions(-) delete mode 100644 ableos/src/dirty_hacks/mod.rs create mode 100644 ableos/src/experiments/clparse.rs diff --git a/ableos/Cargo.lock b/ableos/Cargo.lock index c312307..fc5b4e7 100644 --- a/ableos/Cargo.lock +++ b/ableos/Cargo.lock @@ -59,7 +59,7 @@ checksum = "9ea835d29036a4087793836fa931b08837ad5e957da9e23886b29586fb9b6650" [[package]] name = "externc-libm" version = "0.1.0" -source = "git+https://github.com/HaruxOS/externc-libm#597110801cbcbbfb5d4731746d13014494deb39f" +source = "git+https://github.com/HaruxOS/externc-libm#ad2865a706fd46d829550ed4cdfa4126f2c81e19" dependencies = [ "libm", ] diff --git a/ableos/src/arch/x86_64/drivers/vga.rs b/ableos/src/arch/x86_64/drivers/vga.rs index 91264c7..f288999 100644 --- a/ableos/src/arch/x86_64/drivers/vga.rs +++ b/ableos/src/arch/x86_64/drivers/vga.rs @@ -92,6 +92,10 @@ impl Writer { self.buffer.chars[row][col].write(blank); } } + + pub fn backspace(&mut self) { + self.column_position -= 1; + } } impl fmt::Write for Writer { fn write_str(&mut self, s: &str) -> fmt::Result { @@ -102,7 +106,7 @@ impl fmt::Write for Writer { lazy_static! { pub static ref WRITER: Mutex = Mutex::new(Writer { column_position: 0, - color_code: ColorCode::new(Color::Green, Color::Black), + color_code: ColorCode::new(Color::White, Color::Black), buffer: unsafe { &mut *(0xb8000 as *mut Buffer) }, }); } @@ -111,6 +115,8 @@ use core::fmt; use lazy_static::lazy_static; use spin::Mutex; use volatile::Volatile; + +use crate::print; #[macro_export] macro_rules! kprint { ($($arg:tt)*) => ($crate::arch::drivers::vga::_kprint(format_args!($($arg)*))); diff --git a/ableos/src/arch/x86_64/init.rs b/ableos/src/arch/x86_64/init.rs index a0319a9..42d4c4d 100644 --- a/ableos/src/arch/x86_64/init.rs +++ b/ableos/src/arch/x86_64/init.rs @@ -1,7 +1,7 @@ #![allow(clippy::print_literal)] use { super::{gdt, interrupts}, - crate::{info, println}, + crate::info, }; pub fn init() { @@ -9,6 +9,5 @@ pub fn init() { interrupts::init_idt(); unsafe { interrupts::PICS.lock().initialize() }; x86_64::instructions::interrupts::enable(); - println!("Initialized"); info!("Initialized"); } diff --git a/ableos/src/arch/x86_64/interrupts.rs b/ableos/src/arch/x86_64/interrupts.rs index 5179729..86cc387 100644 --- a/ableos/src/arch/x86_64/interrupts.rs +++ b/ableos/src/arch/x86_64/interrupts.rs @@ -1,4 +1,7 @@ -use crate::{arch::gdt, print, println}; +use crate::{ + arch::{drivers::vga::WRITER, gdt}, + print, println, +}; use lazy_static::lazy_static; use pic8259::ChainedPics; use spin; @@ -81,8 +84,18 @@ extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStac kind: DecodedKeyKind::Unicode, value: character, } => { - print!("{}", char::try_from(character).unwrap()); - // serial_print!("{}", character); + match character { + // Backspace + 8 => { + WRITER.lock().backspace(); + print!(" "); + WRITER.lock().backspace(); + } + _ => { + print!("{}", char::try_from(character).unwrap()); + } + } + crate::kmain::key_entropy(character.try_into().unwrap()); } DecodedKey { diff --git a/ableos/src/dirty_hacks/mod.rs b/ableos/src/dirty_hacks/mod.rs deleted file mode 100644 index 57db143..0000000 --- a/ableos/src/dirty_hacks/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -#[no_mangle] -extern "C" fn __truncdfsf2(_x: f64) -> f32 { - 0.0 -} diff --git a/ableos/src/experiments/absi.rs b/ableos/src/experiments/absi.rs index 7497240..4bbdd91 100644 --- a/ableos/src/experiments/absi.rs +++ b/ableos/src/experiments/absi.rs @@ -1,4 +1,7 @@ -use crate::arch::drivers::vga::{set_vga_color, Color}; +use crate::{ + arch::drivers::vga::{set_vga_color, Color}, + kprint, +}; pub fn colorify(eval: &str) { let y = eval.split("$"); @@ -56,7 +59,7 @@ pub fn colorify(eval: &str) { set_vga_color(Color::White, Color::Black); } elk => { - print!("{}", elk); + kprint!("{}", elk); } } } diff --git a/ableos/src/experiments/clparse.rs b/ableos/src/experiments/clparse.rs new file mode 100644 index 0000000..923b9f8 --- /dev/null +++ b/ableos/src/experiments/clparse.rs @@ -0,0 +1,69 @@ +/* +clparse +* A simple command line parser for ableOS +*/ + +use std::collections::HashMap; +const CURRENT_PATH: &str = "file://test/"; +#[derive(Debug)] +struct Command { + root_command: String, + arguments: HashMap, +} + +impl Command { + pub fn parse(command: String) -> Command { + let split_command = command.split("?"); + let mut root = "".to_string(); + let mut rootCount = 0; + let mut args = HashMap::::new(); + for subcommand in split_command { + match rootCount { + 0 => root = subcommand.to_string(), + 1 => { + for subarg in subcommand.split("&") { + let mut arg1 = ""; + let mut arg2 = ""; + let mut arg_count = 0; + for arg in subarg.split("=") { + if arg_count == 0 { + arg1 = arg; + } else { + arg2 = arg; + } + arg_count += 1; + } + args.insert(arg1.to_string(), arg2.to_string()); + } + } + _ => {} + } + rootCount += 1; + } + + Command { + root_command: root, + arguments: args, + } + } +} +fn main() { + let x = Command::parse("ls?path=".to_string()); + + let y = &x.arguments["path"]; + parsePath(y.to_string()); +} + +fn parsePath(path: String) { + if path.len() == 0 { + println!("EMPTY PATH"); + } else { + match &path[..1] { + "@" => println!("PARTIAL PATH: {}{}", CURRENT_PATH, path), + "/" => println!("FULL PATH: {}", path), + _ => { + println!("MALFORMED PATH: {}", path) + } + } + } +} diff --git a/ableos/src/kmain.rs b/ableos/src/kmain.rs index 9ad76b5..6e07e97 100644 --- a/ableos/src/kmain.rs +++ b/ableos/src/kmain.rs @@ -1,6 +1,7 @@ #![allow(clippy::empty_loop)] -use crate::experiments::absi::colorify; +use crate::log::LOG_STATE; + use { crate::{ arch::{drivers::graphics::GraphicsBuffer, init, sloop}, @@ -31,23 +32,23 @@ lazy_static! { #[no_mangle] pub fn kernel_main() -> ! { init::init(); + LOG_STATE.lock().log_to_screen = false; let mut a_thread = Thread::new(); a_thread.new_task(test_fn); a_thread.new_task(test_fn); - THREAD_LIST.lock().push(a_thread); GraphicsBuffer::draw(); GraphicsBuffer::hide_cursor(); - GraphicsBuffer::show_cursor(); crate::wasm::evaluate(); - println!("{} v{}", RELEASE_TYPE, KERNEL_VERSION); info!("{} v{}", RELEASE_TYPE, KERNEL_VERSION); - colorify("$PINK$Hi$RED$ from$GREEN$ able"); + println!("$PINK$Hi$RED$ from$GREEN$ able!$RESET$"); + + println!("$RED$hi$RESET$"); // stack_overflow(); // crate::arch::shutdown(); diff --git a/ableos/src/lib.rs b/ableos/src/lib.rs index d3e9b44..890c999 100644 --- a/ableos/src/lib.rs +++ b/ableos/src/lib.rs @@ -31,7 +31,6 @@ pub mod print; pub mod log; pub mod allocator; -pub mod dirty_hacks; pub mod driver_traits; pub mod experiments; pub mod keyboard; @@ -41,6 +40,5 @@ pub mod relib; pub mod scheduler; pub mod wasm; -extern crate alloc; - +pub extern crate alloc; pub extern crate externc_libm as libm; diff --git a/ableos/src/log.rs b/ableos/src/log.rs index 9933051..8280182 100644 --- a/ableos/src/log.rs +++ b/ableos/src/log.rs @@ -5,6 +5,9 @@ pub trait Log { fn trace(val: &str); } pub struct LogState { + pub log_to_serial: bool, + pub log_to_screen: bool, + pub debug: bool, pub error: bool, pub info: bool, @@ -12,36 +15,60 @@ pub struct LogState { } lazy_static::lazy_static! { - pub static ref LOG_STATE: LogState = LogState { + pub static ref LOG_STATE: Mutex = spin::Mutex::new(LogState { + log_to_screen: true, + log_to_serial: true, debug: true, error: true, info: true, trace: true, - }; + }); } -use crate::serial_print; +use crate::serial_println; use lliw::{Fg, Reset}; +use spin::Mutex; pub struct ANSISerialLogger; impl Log for ANSISerialLogger { fn debug(val: &str) { - serial_print!("[{}Debug{}] {}\n", Fg::Blue, Reset, val); + if LOG_STATE.lock().log_to_serial { + serial_println!("[{}Debug{}] {}", Fg::Blue, Reset, val); + } + if LOG_STATE.lock().log_to_screen { + println!("[$BLUE$Debug$RESET$] {}", val); + } } fn error(val: &str) { - serial_print!("[{}Error{}] {}\n", Fg::Red, Reset, val); + if LOG_STATE.lock().log_to_serial { + serial_println!("[{}Error{}] {}", Fg::Red, Reset, val); + } + + if LOG_STATE.lock().log_to_screen { + println!("[$RED$Error$RESET$] {}", val); + } } fn info(val: &str) { - serial_print!("[{}Info{}] {}\n", Fg::LightWhite, Reset, val); + if LOG_STATE.lock().log_to_serial { + serial_println!("[{}Info{}] {}", Fg::LightWhite, Reset, val); + } + if LOG_STATE.lock().log_to_screen { + println!("[$LIGHTGRAY$Info$RESET$] {}", val); + } } fn trace(val: &str) { - serial_print!("[{}Trace{}] {}\n", Fg::Yellow, Reset, val); + if LOG_STATE.lock().log_to_serial { + serial_println!("[{}Trace{}] {}", Fg::Yellow, Reset, val); + } + if LOG_STATE.lock().log_to_screen { + println!("[$YELLOW$Trace$RESET$] {}", val); + } } } #[macro_export] macro_rules! debug { ($($arg:tt)*) => ({ - if $crate::log::LOG_STATE.debug{ + if $crate::log::LOG_STATE.lock().debug{ use crate::log::Log; let _ = &log::ANSISerialLogger::debug(&alloc::format!($($arg)*)); @@ -52,7 +79,7 @@ macro_rules! debug { #[macro_export] macro_rules! error { ($($arg:tt)*) => ({ - if $crate::log::LOG_STATE.error{ + if $crate::log::LOG_STATE.lock().error{ use crate::log::Log; crate::log::ANSISerialLogger::error(&alloc::format!($($arg)*)); } @@ -62,7 +89,7 @@ macro_rules! error { #[macro_export] macro_rules! info { ($($arg:tt)*) => ({ - if $crate::log::LOG_STATE.info{ + if $crate::log::LOG_STATE.lock().info{ use crate::log::Log; crate::log::ANSISerialLogger::info(&alloc::format!($($arg)*)); } @@ -72,7 +99,7 @@ macro_rules! info { #[macro_export] macro_rules! trace { ($($arg:tt)*) => ({ - if $crate::log::LOG_STATE.trace{ + if $crate::log::LOG_STATE.lock().trace{ use crate::log::Log; crate::log::ANSISerialLogger::trace(&alloc::format!($($arg)*)); } diff --git a/ableos/src/print.rs b/ableos/src/print.rs index f1ee4fc..59ea1fb 100644 --- a/ableos/src/print.rs +++ b/ableos/src/print.rs @@ -16,9 +16,9 @@ impl core::fmt::Write for Stdout { } #[cfg(target_arch = "x86_64")] fn write_str(&mut self, s: &str) -> Result<(), Error> { - use crate::kprint; - // FIXME: causes issues - kprint!("{}", s); + use crate::experiments::absi::colorify; + colorify(s); + // kprint!("{}", s); Ok(()) } #[cfg(target_arch = "riscv64")] diff --git a/ableos/src/scheduler.rs b/ableos/src/scheduler.rs index 9800031..66e951c 100644 --- a/ableos/src/scheduler.rs +++ b/ableos/src/scheduler.rs @@ -60,7 +60,7 @@ impl Thread { Some(last_thread) => final_threadid = last_thread.id + 1, None => {} } - debug!("New thread with ThreadID: {}", final_threadid); + debug!("New thread with $BLUE$ThreadID$RESET$: {}", final_threadid); Self { id: final_threadid, tasks: vec![], diff --git a/ableos/src/wasm/mod.rs b/ableos/src/wasm/mod.rs index d2777dc..a411372 100644 --- a/ableos/src/wasm/mod.rs +++ b/ableos/src/wasm/mod.rs @@ -72,7 +72,7 @@ impl ModuleImportResolver for HostFunctions { } pub fn evaluate() { - let wasm_binary = include_bytes!("zig.wasm"); + let wasm_binary = include_bytes!("rust.wasm"); // Load wasm binary and prepare it for instantiation. let module = wasmi::Module::from_buffer(&wasm_binary).expect("failed to load wasm");