rust: remove initfd, modify io::getc

master
WangRunji 6 years ago
parent cd8ecc73f2
commit 5ce1d2f788

@ -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<u8> {
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<Vec<u8>>) -> 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<Vec<u8>>) -> String {
break;
}
ESC => {
match getc() .unwrap(){
match getc() {
b'[' => {
match getc().unwrap() {
match getc() {
b'D' => {
// Left arrow
if cursor > 0 {

@ -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 <stdin> failed: {}.", fd);
}
let fd = initfd(STDOUT, "stdout:", O_WRONLY);
if fd < 0 {
panic!("open <stdout> 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");
}

Loading…
Cancel
Save