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

64 lines
1.6 KiB
Rust
Raw Normal View History

//! Kernel shell
use alloc::string::String;
use alloc::vec::Vec;
2018-11-19 13:21:06 +04:00
use crate::fs::{ROOT_INODE, INodeExt};
use crate::process::*;
pub fn run_user_shell() {
2018-11-28 21:22:44 +04:00
if let Ok(inode) = ROOT_INODE.lookup("sh") {
let data = inode.read_as_vec().unwrap();
2018-12-13 22:37:51 +04:00
processor().manager().add(Process::new_user(data.as_slice(), "sh".split(' ')), 0);
2018-11-28 21:22:44 +04:00
} else {
2018-12-13 22:37:51 +04:00
processor().manager().add(Process::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();
2018-12-13 22:37:51 +04:00
let pid = processor().manager().add(Process::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 {
'\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()
}