From 97b838981f120d38e90b8d84acf8ebe64d8685a2 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Mon, 16 Jul 2018 22:02:03 +0800 Subject: [PATCH] Fix console --- kernel/src/arch/riscv32/io.rs | 1 - kernel/src/console.rs | 72 +++++++++-------------------------- kernel/src/fs.rs | 2 +- 3 files changed, 18 insertions(+), 57 deletions(-) diff --git a/kernel/src/arch/riscv32/io.rs b/kernel/src/arch/riscv32/io.rs index aa9129d3..a0743a34 100644 --- a/kernel/src/arch/riscv32/io.rs +++ b/kernel/src/arch/riscv32/io.rs @@ -21,7 +21,6 @@ impl Write for SerialPort { pub fn getchar() -> char { match sbi::console_getchar() as u8 { 255 => 0, // null - 127 => 8, // back c => c, } as char } diff --git a/kernel/src/console.rs b/kernel/src/console.rs index 284e3b0b..9f6efbd7 100644 --- a/kernel/src/console.rs +++ b/kernel/src/console.rs @@ -1,64 +1,26 @@ use core::ops::Deref; - -pub struct LineBuf { - buf: [u8; BUF_SIZE], - len: usize, -} - -pub struct LineBufGuard<'a>(&'a str); - -const BUF_SIZE: usize = 256; - -impl LineBuf { - pub const fn new() -> Self { - LineBuf { - buf: [0; BUF_SIZE], - len: 0, - } - } - - /// Put a char received from serial. Return str if get a line. - pub fn push_u8<'a>(&'a mut self, c: u8) -> Option> { - use alloc::str; - match c { - b' '...128 if self.len != BUF_SIZE => { - self.buf[self.len] = c; - self.len += 1; - } - 8 /* '\b' */ if self.len != 0 => { - self.len -= 1; - } - b'\n' | b'\r' => { - let s = str::from_utf8(&self.buf[..self.len]).unwrap(); - self.len = 0; - return Some(LineBufGuard(s)); - } - _ => {} - } - None - } -} - -impl<'a> Deref for LineBufGuard<'a> { - type Target = str; - fn deref(&self) -> &str { - self.0 - } -} - use alloc::string::String; use arch::io::getchar; pub fn get_line() -> String { - let mut buf = LineBuf::new(); + let mut s = String::new(); loop { - let mut c = 0; - while c == 0 { - c = getchar() as u8; - } - print!("{}", c as char); - if let Some(line) = buf.push_u8(c) { - return String::from(&*line); + let c = getchar(); + 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; + } + _ => {} } } } \ No newline at end of file diff --git a/kernel/src/fs.rs b/kernel/src/fs.rs index dd652f25..fff54173 100644 --- a/kernel/src/fs.rs +++ b/kernel/src/fs.rs @@ -23,13 +23,13 @@ pub fn shell() { let mut buf = Box::new([0; 64 << 12]); loop { + print!(">> "); use console::get_line; let name = get_line(); if name == "" { continue; } if let Ok(file) = root.borrow().lookup(name.as_str()) { - println!("Running: {}", name); let len = file.borrow().read_at(0, &mut *buf).unwrap(); process::add_user_process(name, &buf[..len]); } else {