diff --git a/rust/src/io.rs b/rust/src/io.rs index f586150..370101b 100644 --- a/rust/src/io.rs +++ b/rust/src/io.rs @@ -1,7 +1,6 @@ use alloc::string::String; use alloc::vec::Vec; use core::fmt::{self, Write}; -use core::option::Option; use crate::syscall::{sys_read, sys_write}; @@ -25,13 +24,15 @@ pub fn print(args: fmt::Arguments) { StdOut.write_fmt(args).unwrap(); } -pub fn getc() -> Option { +pub fn getc() -> u8 { let mut c = 0u8; - let ret = sys_read(STDIN, &mut c, 1); - match ret { - 1 => Some(c), - 0 => None, - _ => panic!(), + loop { + let len = sys_read(STDIN, &mut c, 1); + match len { + 1 => return c, + 0 => continue, + _ => panic!("read stdin len = {}", len), + } } } @@ -47,7 +48,7 @@ pub fn get_line(history: &mut Vec>) -> String { let mut line_vec = Vec::with_capacity(512); let mut history_index = history.len(); loop { - match getc().unwrap() { + match getc() { BS | DEL => { // Backspace if cursor > 0 { @@ -78,9 +79,9 @@ pub fn get_line(history: &mut Vec>) -> String { break; } ESC => { - match getc() .unwrap(){ + match getc() { b'[' => { - match getc().unwrap() { + match getc() { b'D' => { // Left arrow if cursor > 0 { diff --git a/rust/src/lang_items.rs b/rust/src/lang_items.rs index f125f8c..a5421cb 100644 --- a/rust/src/lang_items.rs +++ b/rust/src/lang_items.rs @@ -1,5 +1,4 @@ -use crate::syscall::{sys_close, sys_dup2, sys_exit, sys_open}; -use crate::io::{O_RDONLY, O_WRONLY, STDIN, STDOUT}; +use crate::syscall::sys_exit; use crate::ALLOCATOR; use core::alloc::Layout; @@ -11,21 +10,6 @@ fn main() { panic!("No main() linked"); } -fn initfd(fd2: usize, path: &str, open_flags: usize) -> i32 { - let fd1 = sys_open(path, open_flags); - if fd1 < 0 { - return fd1; - } - let mut ret = fd1; - let fd1 = fd1 as usize; - if fd1 != fd2 { - sys_close(fd2); - ret = sys_dup2(fd1, fd2); - sys_close(fd1); - } - return ret; -} - fn init_heap() { const HEAP_SIZE: usize = 0x1000; static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE]; @@ -34,15 +18,6 @@ fn init_heap() { #[no_mangle] pub extern "C" fn _start(_argc: isize, _argv: *const *const u8) -> ! { - let fd = initfd(STDIN, "stdin:", O_RDONLY); - if fd < 0 { - panic!("open failed: {}.", fd); - } - let fd = initfd(STDOUT, "stdout:", O_WRONLY); - if fd < 0 { - panic!("open failed: {}.", fd); - } - init_heap(); main(); sys_exit(0) @@ -53,9 +28,7 @@ fn eh_personality() {} #[panic_handler] fn panic(info: &PanicInfo) -> ! { - let location = info.location().unwrap(); - let message = info.message().unwrap(); - println!("\n\nPANIC in {} at line {}\n {}", location.file(), location.line(), message); + println!("\n\n{}", info); sys_exit(1) } @@ -66,5 +39,5 @@ fn oom(_: Layout) -> ! { #[no_mangle] pub extern "C" fn abort() -> ! { - sys_exit(2) + panic!("abort"); }