|
|
@ -4,17 +4,17 @@
|
|
|
|
#[macro_use]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate ucore_ulib;
|
|
|
|
extern crate ucore_ulib;
|
|
|
|
use ucore_ulib::io::getc;
|
|
|
|
use ucore_ulib::io::getc;
|
|
|
|
use ucore_ulib::syscall::{sys_fork, sys_exec, sys_wait};
|
|
|
|
use ucore_ulib::syscall::{sys_exec, sys_fork, sys_wait};
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_line(buffer: &mut[u8]) -> usize {
|
|
|
|
pub fn get_line(buffer: &mut [u8]) -> usize {
|
|
|
|
let mut pos: usize=0;
|
|
|
|
let mut pos: usize = 0;
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
let ret=getc();
|
|
|
|
let ret = getc();
|
|
|
|
match ret {
|
|
|
|
match ret {
|
|
|
|
None => break,
|
|
|
|
None => break,
|
|
|
|
Some(byte) => {
|
|
|
|
Some(byte) => {
|
|
|
|
let c = byte as char;
|
|
|
|
let c = byte as char;
|
|
|
|
match c {
|
|
|
|
match c {
|
|
|
|
'\u{8}' /* '\b' */ => {
|
|
|
|
'\u{8}' /* '\b' */ => {
|
|
|
|
if pos > 0 {
|
|
|
|
if pos > 0 {
|
|
|
|
print!("\u{8} \u{8}");
|
|
|
|
print!("\u{8} \u{8}");
|
|
|
@ -34,80 +34,82 @@ pub fn get_line(buffer: &mut[u8]) -> usize {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos
|
|
|
|
pos
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const BUFFER_SIZE:usize=4096;
|
|
|
|
const BUFFER_SIZE: usize = 4096;
|
|
|
|
|
|
|
|
|
|
|
|
// IMPORTANT: Must define main() like this
|
|
|
|
// IMPORTANT: Must define main() like this
|
|
|
|
#[no_mangle]
|
|
|
|
#[no_mangle]
|
|
|
|
pub fn main() -> i32 {
|
|
|
|
pub fn main() -> i32 {
|
|
|
|
use core::mem::uninitialized;
|
|
|
|
use core::mem::uninitialized;
|
|
|
|
let mut buf:[u8;BUFFER_SIZE] = unsafe { uninitialized() };
|
|
|
|
let mut buf: [u8; BUFFER_SIZE] = unsafe { uninitialized() };
|
|
|
|
println!("Rust user shell");
|
|
|
|
println!("Rust user shell");
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
print!(">> ");
|
|
|
|
print!(">> ");
|
|
|
|
let len = get_line(&mut buf);
|
|
|
|
let len = get_line(&mut buf);
|
|
|
|
if len>BUFFER_SIZE {
|
|
|
|
if len > BUFFER_SIZE {
|
|
|
|
println!("Command is too long!");
|
|
|
|
println!("Command is too long!");
|
|
|
|
}else{
|
|
|
|
} else {
|
|
|
|
let cmd=&buf[..len];
|
|
|
|
let cmd = &buf[..len];
|
|
|
|
let mut parsed:[u8;BUFFER_SIZE+1] = unsafe { uninitialized() };
|
|
|
|
let mut parsed: [u8; BUFFER_SIZE + 1] = unsafe { uninitialized() };
|
|
|
|
let mut offset:[usize;BUFFER_SIZE+1] = unsafe { uninitialized() };
|
|
|
|
let mut offset: [usize; BUFFER_SIZE + 1] = unsafe { uninitialized() };
|
|
|
|
let mut start:usize = 0;
|
|
|
|
let mut start: usize = 0;
|
|
|
|
let mut pos:usize = 0;
|
|
|
|
let mut pos: usize = 0;
|
|
|
|
let mut is_word = false;
|
|
|
|
let mut is_word = false;
|
|
|
|
let mut parsed_pos:usize = 0;
|
|
|
|
let mut parsed_pos: usize = 0;
|
|
|
|
let mut offset_pos:usize = 0;
|
|
|
|
let mut offset_pos: usize = 0;
|
|
|
|
loop {
|
|
|
|
loop {
|
|
|
|
if pos>=cmd.len() {
|
|
|
|
if pos >= cmd.len() {
|
|
|
|
if is_word {
|
|
|
|
if is_word {
|
|
|
|
offset[offset_pos]=parsed_pos;
|
|
|
|
offset[offset_pos] = parsed_pos;
|
|
|
|
offset_pos+=1;
|
|
|
|
offset_pos += 1;
|
|
|
|
parsed[parsed_pos..parsed_pos+pos-start].copy_from_slice(&cmd[start..pos]);
|
|
|
|
parsed[parsed_pos..parsed_pos + pos - start]
|
|
|
|
parsed_pos+=pos-start;
|
|
|
|
.copy_from_slice(&cmd[start..pos]);
|
|
|
|
parsed[parsed_pos]=0;
|
|
|
|
parsed_pos += pos - start;
|
|
|
|
// parsed_pos+=1;
|
|
|
|
parsed[parsed_pos] = 0;
|
|
|
|
}
|
|
|
|
// parsed_pos+=1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
if cmd[pos]==(' ' as u8) {
|
|
|
|
}
|
|
|
|
if is_word {
|
|
|
|
if cmd[pos] == (' ' as u8) {
|
|
|
|
is_word=false;
|
|
|
|
if is_word {
|
|
|
|
offset[offset_pos]=parsed_pos;
|
|
|
|
is_word = false;
|
|
|
|
offset_pos+=1;
|
|
|
|
offset[offset_pos] = parsed_pos;
|
|
|
|
parsed[parsed_pos..parsed_pos+pos-start].copy_from_slice(&cmd[start..pos]);
|
|
|
|
offset_pos += 1;
|
|
|
|
parsed_pos+=pos-start;
|
|
|
|
parsed[parsed_pos..parsed_pos + pos - start]
|
|
|
|
parsed[parsed_pos]=0;
|
|
|
|
.copy_from_slice(&cmd[start..pos]);
|
|
|
|
parsed_pos+=1;
|
|
|
|
parsed_pos += pos - start;
|
|
|
|
}
|
|
|
|
parsed[parsed_pos] = 0;
|
|
|
|
}else{
|
|
|
|
parsed_pos += 1;
|
|
|
|
if !is_word {
|
|
|
|
}
|
|
|
|
is_word=true;
|
|
|
|
} else {
|
|
|
|
start=pos;
|
|
|
|
if !is_word {
|
|
|
|
}
|
|
|
|
is_word = true;
|
|
|
|
}
|
|
|
|
start = pos;
|
|
|
|
pos+=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if offset_pos > 0 {
|
|
|
|
pos += 1;
|
|
|
|
let pid=sys_fork();
|
|
|
|
}
|
|
|
|
if pid==0 {
|
|
|
|
if offset_pos > 0 {
|
|
|
|
let mut ptrs:[*const u8;BUFFER_SIZE] = unsafe { uninitialized() };
|
|
|
|
let pid = sys_fork();
|
|
|
|
for i in 0..offset_pos {
|
|
|
|
if pid == 0 {
|
|
|
|
ptrs[i]=unsafe {parsed.as_ptr().offset(offset[i] as isize)};
|
|
|
|
let mut ptrs: [*const u8; BUFFER_SIZE] = unsafe { uninitialized() };
|
|
|
|
}
|
|
|
|
for i in 0..offset_pos {
|
|
|
|
return sys_exec(parsed.as_ptr(),offset_pos,ptrs.as_ptr());
|
|
|
|
ptrs[i] = unsafe { parsed.as_ptr().offset(offset[i] as isize) };
|
|
|
|
}else if pid<0 {
|
|
|
|
}
|
|
|
|
panic!("pid<0")
|
|
|
|
return sys_exec(parsed.as_ptr(), offset_pos, ptrs.as_ptr());
|
|
|
|
}else{
|
|
|
|
} else if pid < 0 {
|
|
|
|
let mut code:i32 = unsafe { uninitialized() };
|
|
|
|
panic!("pid<0")
|
|
|
|
sys_wait(pid as usize,&mut code as *mut i32);
|
|
|
|
} else {
|
|
|
|
println!("\n[Process exited with code {}]",code);
|
|
|
|
let mut code: i32 = unsafe { uninitialized() };
|
|
|
|
}
|
|
|
|
sys_wait(pid as usize, &mut code as *mut i32);
|
|
|
|
}
|
|
|
|
println!("\n[Process exited with code {}]", code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|