1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-26 09:53:28 +04:00
rCore/kernel/src/shell.rs

66 lines
1.7 KiB
Rust
Raw Normal View History

//! Kernel shell
use alloc::string::String;
2018-11-19 13:21:06 +04:00
use crate::fs::{ROOT_INODE, INodeExt};
use crate::process::*;
2019-01-24 15:03:45 +04:00
use crate::thread;
pub fn run_user_shell() {
2018-11-28 21:22:44 +04:00
if let Ok(inode) = ROOT_INODE.lookup("sh") {
println!("Going to user mode shell.");
println!("Use 'ls' to list available programs.");
2018-11-28 21:22:44 +04:00
let data = inode.read_as_vec().unwrap();
2019-01-25 18:58:49 +04:00
processor().manager().add(Thread::new_user(data.as_slice(), "sh".split(' ')), 0);
2018-11-28 21:22:44 +04:00
} else {
2019-01-25 18:58:49 +04:00
processor().manager().add(Thread::new_kernel(shell, 0), 0);
2018-11-28 21:22:44 +04:00
}
}
2018-11-28 21:22:44 +04:00
pub extern fn shell(_arg: usize) -> ! {
let files = ROOT_INODE.list().unwrap();
println!("Available programs: {:?}", files);
loop {
print!(">> ");
let cmd = get_line();
if cmd == "" {
continue;
}
let name = cmd.split(' ').next().unwrap();
if let Ok(file) = ROOT_INODE.lookup(name) {
let data = file.read_as_vec().unwrap();
2019-01-25 18:58:49 +04:00
let pid = processor().manager().add(Thread::new_user(data.as_slice(), cmd.split(' ')), thread::current().id());
unsafe { thread::JoinHandle::<()>::_of(pid) }.join().unwrap();
} else {
println!("Program not exist");
}
}
}
fn get_line() -> String {
let mut s = String::new();
loop {
let c = get_char();
match c {
2018-12-26 19:53:14 +04:00
'\u{8}' | '\u{7f}' /* '\b' */ => {
if s.pop().is_some() {
print!("\u{7f}");
}
}
' '...'\u{7e}' => {
s.push(c);
print!("{}", c);
}
'\n' | '\r' => {
print!("\n");
return s;
}
_ => {}
}
}
}
fn get_char() -> char {
2018-11-19 13:21:06 +04:00
crate::fs::STDIN.pop()
}