From 21b0bdcbca216c6c80a5c65eab4a4d0153bf8e39 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 16 Nov 2018 00:49:42 +0800 Subject: [PATCH] separate kernel shell to a mod, remove console mod --- kernel/src/arch/riscv32/mod.rs | 2 +- kernel/src/arch/x86_64/mod.rs | 2 +- kernel/src/console.rs | 30 ----------------- kernel/src/fs.rs | 31 +----------------- kernel/src/lib.rs | 2 +- kernel/src/shell.rs | 59 ++++++++++++++++++++++++++++++++++ 6 files changed, 63 insertions(+), 63 deletions(-) delete mode 100644 kernel/src/console.rs create mode 100644 kernel/src/shell.rs diff --git a/kernel/src/arch/riscv32/mod.rs b/kernel/src/arch/riscv32/mod.rs index 963faea..22c2bf0 100644 --- a/kernel/src/arch/riscv32/mod.rs +++ b/kernel/src/arch/riscv32/mod.rs @@ -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(); diff --git a/kernel/src/arch/x86_64/mod.rs b/kernel/src/arch/x86_64/mod.rs index b739925..3f7fda5 100644 --- a/kernel/src/arch/x86_64/mod.rs +++ b/kernel/src/arch/x86_64/mod.rs @@ -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); diff --git a/kernel/src/console.rs b/kernel/src/console.rs deleted file mode 100644 index 01798ad..0000000 --- a/kernel/src/console.rs +++ /dev/null @@ -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() -} diff --git a/kernel/src/fs.rs b/kernel/src/fs.rs index 3ffb3e6..8ea584f 100644 --- a/kernel/src/fs.rs +++ b/kernel/src/fs.rs @@ -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; diff --git a/kernel/src/lib.rs b/kernel/src/lib.rs index 9c54064..1c8ae2f 100644 --- a/kernel/src/lib.rs +++ b/kernel/src/lib.rs @@ -51,7 +51,7 @@ mod syscall; mod fs; mod sync; mod trap; -mod console; +mod shell; #[allow(dead_code)] #[cfg(target_arch = "x86_64")] diff --git a/kernel/src/shell.rs b/kernel/src/shell.rs new file mode 100644 index 0000000..c227d9f --- /dev/null +++ b/kernel/src/shell.rs @@ -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() +}