Skip to content

Commit

Permalink
Add print! and println! macro & add timestamp to kprintln!
Browse files Browse the repository at this point in the history
+ print! and println! macro print on vga_buffer without timestamp
+ kprint! macro print on vga_buffer with timestamp (mimic dmesg)
  • Loading branch information
0x050f committed May 28, 2024
1 parent e34e7f3 commit d3928a2
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 100 deletions.
28 changes: 14 additions & 14 deletions srcs/cli/commands/debugfs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ pub static CURRENTDIR_INODE: Mutex<usize> = Mutex::new(ROOT_INODE);
pub static PWD: Mutex<Option<Path>> = Mutex::new(None);
pub static DISKNO: Mutex<Option<ext2::Ext2>> = Mutex::new(None);

use crate::println;

fn help() {
crate::kprintln!(
"Command available: ls,stat,cat,imap,cd,touch,mkdir,rm,pwd,test"
);
println!("Command available: ls,stat,cat,imap,cd,touch,mkdir,rm,pwd,test");
}

pub fn debugfs(mut command: Vec<String>) {
Expand All @@ -31,7 +31,7 @@ pub fn debugfs(mut command: Vec<String>) {
"pwd" => pwd(),
"test" => test(),
_ => {
crate::kprintln!("Unknown command: {}", command[0]);
println!("Unknown command: {}", command[0]);
help();
}
}
Expand All @@ -41,17 +41,17 @@ pub fn debugfs(mut command: Vec<String>) {
}

fn pwd() {
crate::kprintln!(
println!(
"[pwd] INODE: {:>6} PATH: {}",
*CURRENTDIR_INODE.lock(),
PWD.lock().as_ref().unwrap_or(&Path::new("/"))
);
crate::kprintln!("[root] INODE: {:>6} PATH: /", ROOT_INODE);
println!("[root] INODE: {:>6} PATH: /", ROOT_INODE);
}

fn rm(command: Vec<String>) {
if command.len() < 2 {
crate::kprintln!("usage: debugfs rm FILE");
println!("usage: debugfs rm FILE");
return;
}
ext2::remove_file(
Expand All @@ -63,7 +63,7 @@ fn rm(command: Vec<String>) {

fn stat(command: Vec<String>) {
if command.len() < 2 {
crate::kprintln!("usage: debugfs stat FILE");
println!("usage: debugfs stat FILE");
return;
}
ext2::show_inode_info(
Expand All @@ -75,7 +75,7 @@ fn stat(command: Vec<String>) {

fn mkdir(command: Vec<String>) {
if command.len() < 2 {
crate::kprintln!("usage: debugfs mkdir DIR");
println!("usage: debugfs mkdir DIR");
return;
}
ext2::create_dir(
Expand All @@ -87,7 +87,7 @@ fn mkdir(command: Vec<String>) {

fn touch(command: Vec<String>) {
if command.len() < 2 {
crate::kprintln!("usage: debugfs touch FILE");
println!("usage: debugfs touch FILE");
return;
}
ext2::create_file(
Expand All @@ -99,7 +99,7 @@ fn touch(command: Vec<String>) {

fn cat(command: Vec<String>) {
if command.len() < 2 {
crate::kprintln!("usage: debugfs cat FILE");
println!("usage: debugfs cat FILE");
return;
}
let file_content = ext2::get_file_content(
Expand Down Expand Up @@ -137,7 +137,7 @@ fn ls(command: Vec<String>) {
for i in dentries {
crate::kprint!("{} ", i.name);
}
crate::kprintln!("");
println!("");
}

fn cd(command: Vec<String>) {
Expand All @@ -150,7 +150,7 @@ fn cd(command: Vec<String>) {
let ext2 = binding.as_ref().unwrap();
let lookup = ext2.recurs_find(path.as_str(), *CURRENTDIR_INODE.lock());
match lookup {
None => crate::kprintln!("Dir not found"),
None => println!("Dir not found"),
Some((inodeno, inode)) => {
if inode.is_dir() {
*CURRENTDIR_INODE.lock() = inodeno;
Expand All @@ -166,7 +166,7 @@ fn cd(command: Vec<String>) {
pwd.cleanup();
*PWD.lock() = Some(pwd);
} else {
crate::kprintln!("Error: {} is not a directory", path.as_str());
println!("Error: {} is not a directory", path.as_str());
}
},
};
Expand Down
14 changes: 7 additions & 7 deletions srcs/cli/commands/hexdump.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::alloc::string::String;
use crate::alloc::vec::Vec;
use crate::kprintln;
use crate::println;
use crate::vga_buffer::hexdump;

pub fn hextou(string: &str) -> Option<usize> {
Expand Down Expand Up @@ -31,24 +31,24 @@ pub fn hexdump_parser(command: Vec<String>) {
let mut args: [usize; 2] = [0, 0];

if command.len() != 3 {
kprintln!("Invalid number of arguments.");
kprintln!("Usage: hexdump [addr] [size]");
println!("Invalid number of arguments.");
println!("Usage: hexdump [addr] [size]");
return;
}

if let Some(res) = atou(command[1].as_str()) {
args[0] = res;
} else {
kprintln!("Invalid number of arguments.");
kprintln!("Usage: hexdump [addr] [size]");
println!("Invalid number of arguments.");
println!("Usage: hexdump [addr] [size]");
return;
}

if let Some(res) = atou(command[2].as_str()) {
args[1] = res;
} else {
kprintln!("Invalid number of arguments.");
kprintln!("Usage: hexdump [addr] [size]");
println!("Invalid number of arguments.");
println!("Usage: hexdump [addr] [size]");
return;
}

Expand Down
42 changes: 21 additions & 21 deletions srcs/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::syscalls::signal::sys_kill;
use crate::syscalls::timer::sys_getppid;
use crate::vec::Vec;
use crate::vga_buffer::screenclear;
use crate::{io, kprint, kprintln};
use crate::{io, kprint, print, println};

// Commands modules
pub mod debugfs;
Expand Down Expand Up @@ -61,16 +61,16 @@ fn play(command: Vec<String>) {
if command.len() == 2 {
sound = command[1].as_str();
}
crate::kprintln!("sound: {}", sound);
println!("sound: {}", sound);
crate::sound::play(sound);
}
fn clear(_: Vec<String>) {
screenclear!();
}
fn help(_: Vec<String>) {
kprintln!("Available commands:");
println!("Available commands:");
for i in KNOWN_CMD {
kprintln!(" {}", i);
println!(" {}", i);
}
}

Expand All @@ -91,8 +91,8 @@ use crate::keyboard::{KEYMAP, KEYMAP_FR, KEYMAP_US};

fn keymap(command: Vec<String>) {
if command.len() != 2 {
kprintln!("Invalid number of arguments.");
kprintln!("Usage: keymap {{us, fr}}");
println!("Invalid number of arguments.");
println!("Usage: keymap {{us, fr}}");
return;
}

Expand All @@ -101,8 +101,8 @@ fn keymap(command: Vec<String>) {
} else if command[1] == "fr" {
unsafe { KEYMAP = &KEYMAP_FR };
} else {
kprintln!("Invalid argument.");
kprintln!("Usage: keymap {{us, fr}}");
println!("Invalid argument.");
println!("Usage: keymap {{us, fr}}");
}
}

Expand All @@ -112,22 +112,22 @@ fn interrupt(command: Vec<String>) {
let arg: usize;

if command.len() != 2 {
kprintln!("Invalid number of arguments.");
kprintln!("Usage: int [nb]");
println!("Invalid number of arguments.");
println!("Usage: int [nb]");
return;
}

if let Some(res) = hexdump::atou(command[1].as_str()) {
arg = res;
} else {
kprintln!("Invalid number of arguments.");
kprintln!("Usage: hexdump [addr] [size]");
println!("Invalid number of arguments.");
println!("Usage: hexdump [addr] [size]");
return;
}

if arg > 255 {
kprintln!("Invalid argument.");
kprintln!("Usage: int [nb]");
println!("Invalid argument.");
println!("Usage: int [nb]");
return;
}
unsafe { int(arg as u8) };
Expand Down Expand Up @@ -191,7 +191,7 @@ impl Command {
let tmp: &str =
&self.command[self.index - 1..self.command.len()];
self.index -= 1;
crate::kprint!(
print!(
"{delbyte}{string} {delbyte}",
string = tmp,
delbyte = '\x08'
Expand All @@ -202,17 +202,17 @@ impl Command {
}
} else if charcode >= ' ' && charcode <= '~' {
if self.insert(charcode).is_err() {
kprintln!("Can't handle longer command, clearing buffer");
kprint!("$> ");
println!("Can't handle longer command, clearing buffer");
print!("$> ");
self.clear();
}
let tmp: &str = &self.command[self.index - 1..self.command.len()];
crate::kprint!("{}", tmp);
print!("{}", tmp);
crate::vga_buffer::WRITER
.lock()
.move_cursor(-(tmp.len() as i32) + 1);
} else if charcode == '\n' {
crate::kprint!("{}", charcode);
print!("{}", charcode);
match self.is_known() {
Some(x) => {
unsafe { LOCK_CMD = true };
Expand Down Expand Up @@ -250,12 +250,12 @@ impl Command {
},
_ => {
if self.command.len() != 0 {
kprintln!("Unknown command. Type `help` to list available commands");
println!("Unknown command. Type `help` to list available commands");
}
},
}
self.clear();
kprint!("$> ");
print!("$> ");
}
}
}
32 changes: 16 additions & 16 deletions srcs/cli/commands/process.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::alloc::string::String;
use crate::alloc::vec::Vec;
use crate::kprintln;
use crate::println;

use crate::cli::commands::hexdump::atou;
use crate::proc::process::{Pid, Process};
Expand All @@ -10,15 +10,15 @@ pub fn pmap(command: Vec<String>) {
let pid: Pid;

if command.len() != 2 {
kprintln!("Invalid argument.");
kprintln!("Usage: pmap [pid]");
println!("Invalid argument.");
println!("Usage: pmap [pid]");
return;
}
if let Some(res) = atou(command[1].as_str()) {
pid = res as Pid;
} else {
kprintln!("Invalid argument.");
kprintln!("Usage: pmap [pid]");
println!("Invalid argument.");
println!("Usage: pmap [pid]");
return;
}

Expand All @@ -29,42 +29,42 @@ pub fn pmap(command: Vec<String>) {
};
let process = binding.lock();
let mut used_size: usize = 0;
crate::kprintln!("{}:", pid);
crate::kprintln!("{}", process.heap);
println!("{}:", pid);
println!("{}", process.heap);
used_size += process.heap.size;
crate::kprintln!("{}", process.stack);
println!("{}", process.stack);
used_size += process.stack.size;
crate::kprintln!("{}", process.kernel_stack);
println!("{}", process.kernel_stack);
used_size += process.kernel_stack.size;
for i in &process.mem_map {
let guard = i.lock();
crate::kprintln!("{}", *guard);
println!("{}", *guard);
used_size += guard.size;
}
crate::kprintln!(" total: {:#x}", used_size);
println!(" total: {:#x}", used_size);
}

pub fn kill(command: Vec<String>) {
let pid: Pid;

if command.len() != 2 {
kprintln!("Invalid argument.");
kprintln!("Usage: kill [pid]");
println!("Invalid argument.");
println!("Usage: kill [pid]");
return;
}

if let Some(res) = atou(command[1].as_str()) {
pid = res as Pid;
} else {
kprintln!("Invalid argument.");
kprintln!("Usage: kill [pid]");
println!("Invalid argument.");
println!("Usage: kill [pid]");
return;
}

let res: i32 = sys_kill(pid, 9); // SIGKILL

if res != 0 {
kprintln!("[Error]: {}", res);
println!("[Error]: {}", res);
return;
}
}
Expand Down
11 changes: 6 additions & 5 deletions srcs/cli/commands/time.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use crate::alloc::string::String;
use crate::alloc::vec::Vec;

use crate::println;

pub fn jiffies(_: Vec<String>) {
crate::kprintln!("Jiffies: {}", crate::time::jiffies());
println!("Jiffies: {}", crate::time::jiffies());
}

pub fn uptime(_: Vec<String>) {
let time = crate::time::get_timestamp();
crate::kprintln!(
println!(
"Time elapsed since boot: {}s {}ms",
time.second,
time.millisecond
time.second, time.millisecond
);
}

pub fn date(_: Vec<String>) {
crate::kprintln!("{}", crate::cmos::get_time());
println!("{}", crate::cmos::get_time());
}
Loading

0 comments on commit d3928a2

Please sign in to comment.