From 5ce1d2f7887b026ab5c5eb60f5aa4fb57390d349 Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 15 Mar 2019 01:49:27 +0800 Subject: [PATCH] rust: remove initfd, modify io::getc --- rust/src/io.rs | 21 +++++++++++---------- rust/src/lang_items.rs | 33 +++------------------------------ 2 files changed, 14 insertions(+), 40 deletions(-) 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"); }