1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-23 00:16:17 +04:00

Fix console

This commit is contained in:
WangRunji 2018-07-16 22:02:03 +08:00
parent cf1a2d3450
commit 97b838981f
3 changed files with 18 additions and 57 deletions

View File

@ -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
}

View File

@ -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<LineBufGuard<'a>> {
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;
let c = getchar();
match c {
'\u{7f}' /* '\b' */ => {
if s.pop().is_some() {
print!("\u{7f}");
}
print!("{}", c as char);
if let Some(line) = buf.push_u8(c) {
return String::from(&*line);
}
' '...'\u{7e}' => {
s.push(c);
print!("{}", c);
}
'\n' | '\r' => {
print!("\n");
return s;
}
_ => {}
}
}
}

View File

@ -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 {