separate kernel shell to a mod, remove console mod

master
WangRunji 6 years ago
parent b3e5d1987e
commit 21b0bdcbca

@ -27,7 +27,7 @@ pub extern fn rust_main(hartid: usize, dtb: usize, hart_mask: usize) -> ! {
timer::init();
::process::init();
::thread::spawn(::fs::shell);
::thread::spawn(::shell::shell);
unsafe { cpu::start_others(hart_mask); }
::kmain();

@ -48,7 +48,7 @@ pub extern "C" fn _start(boot_info: &'static BootInfo) -> ! {
driver::init();
::process::init();
::thread::spawn(::fs::shell);
::thread::spawn(::shell::shell);
AP_CAN_INIT.store(true, Ordering::Relaxed);

@ -1,30 +0,0 @@
use core::ops::Deref;
use alloc::string::String;
use alloc::collections::VecDeque;
pub 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;
}
_ => {}
}
}
}
pub fn get_char() -> char {
::fs::STDIN.pop()
}

@ -5,6 +5,7 @@ use arch::driver::ide;
use sync::Condvar;
use sync::SpinNoIrqLock as Mutex;
use core::any::Any;
use core::slice;
// Hard link user program
#[cfg(target_arch = "riscv32")]
@ -36,33 +37,6 @@ lazy_static! {
};
}
pub fn shell() {
let files = ROOT_INODE.list().unwrap();
println!("Available programs: {:?}", files);
const BUF_SIZE: usize = 0x40000;
let mut buf = Vec::with_capacity(BUF_SIZE);
unsafe { buf.set_len(BUF_SIZE); }
loop {
print!(">> ");
use console::get_line;
let cmd = get_line();
if cmd == "" {
continue;
}
let name = cmd.split(' ').next().unwrap();
if let Ok(file) = ROOT_INODE.lookup(name) {
use process::*;
let len = file.read_at(0, &mut *buf).unwrap();
let pid = processor().manager().add(ContextImpl::new_user(&buf[..len], cmd.split(' ')));
processor().manager().wait(thread::current().id(), pid);
processor().yield_now();
} else {
println!("Program not exist");
}
}
}
struct MemBuf(&'static [u8]);
impl MemBuf {
@ -84,9 +58,6 @@ impl Device for MemBuf {
}
}
use core::slice;
use alloc::vec::Vec;
#[cfg(target_arch = "x86_64")]
impl BlockedDevice for ide::IDE {
const BLOCK_SIZE_LOG2: u8 = 9;

@ -51,7 +51,7 @@ mod syscall;
mod fs;
mod sync;
mod trap;
mod console;
mod shell;
#[allow(dead_code)]
#[cfg(target_arch = "x86_64")]

@ -0,0 +1,59 @@
//! Kernel shell
use alloc::string::String;
use alloc::vec::Vec;
use fs::ROOT_INODE;
use process::*;
pub fn shell() {
let files = ROOT_INODE.list().unwrap();
println!("Available programs: {:?}", files);
const BUF_SIZE: usize = 0x40000;
let mut buf = Vec::with_capacity(BUF_SIZE);
unsafe { buf.set_len(BUF_SIZE); }
loop {
print!(">> ");
let cmd = get_line();
if cmd == "" {
continue;
}
let name = cmd.split(' ').next().unwrap();
if let Ok(file) = ROOT_INODE.lookup(name) {
let len = file.read_at(0, &mut *buf).unwrap();
let pid = processor().manager().add(ContextImpl::new_user(&buf[..len], cmd.split(' ')));
processor().manager().wait(thread::current().id(), pid);
processor().yield_now();
} 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 {
::fs::STDIN.pop()
}
Loading…
Cancel
Save