Fix console

master
WangRunji 6 years ago
parent cf1a2d3450
commit 97b838981f

@ -21,7 +21,6 @@ impl Write for SerialPort {
pub fn getchar() -> char { pub fn getchar() -> char {
match sbi::console_getchar() as u8 { match sbi::console_getchar() as u8 {
255 => 0, // null 255 => 0, // null
127 => 8, // back
c => c, c => c,
} as char } as char
} }

@ -1,64 +1,26 @@
use core::ops::Deref; 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 alloc::string::String;
use arch::io::getchar; use arch::io::getchar;
pub fn get_line() -> String { pub fn get_line() -> String {
let mut buf = LineBuf::new(); let mut s = String::new();
loop { loop {
let mut c = 0; let c = getchar();
while c == 0 { match c {
c = getchar() as u8; '\u{7f}' /* '\b' */ => {
if s.pop().is_some() {
print!("\u{7f}");
}
} }
print!("{}", c as char); ' '...'\u{7e}' => {
if let Some(line) = buf.push_u8(c) { s.push(c);
return String::from(&*line); print!("{}", c);
}
'\n' | '\r' => {
print!("\n");
return s;
}
_ => {}
} }
} }
} }

@ -23,13 +23,13 @@ pub fn shell() {
let mut buf = Box::new([0; 64 << 12]); let mut buf = Box::new([0; 64 << 12]);
loop { loop {
print!(">> ");
use console::get_line; use console::get_line;
let name = get_line(); let name = get_line();
if name == "" { if name == "" {
continue; continue;
} }
if let Ok(file) = root.borrow().lookup(name.as_str()) { if let Ok(file) = root.borrow().lookup(name.as_str()) {
println!("Running: {}", name);
let len = file.borrow().read_at(0, &mut *buf).unwrap(); let len = file.borrow().read_at(0, &mut *buf).unwrap();
process::add_user_process(name, &buf[..len]); process::add_user_process(name, &buf[..len]);
} else { } else {

Loading…
Cancel
Save