Fix rust/sh and force musl-cross-make toolchain for biscuit

master
Jiajie Chen 6 years ago
parent f0d05da78f
commit f0441d1fe3

@ -29,11 +29,7 @@ if (${ARCH} STREQUAL i386)
endif ()
set(CMAKE_C_FLAGS "-m32 -mno-red-zone")
elseif (${ARCH} STREQUAL x86_64)
if(APPLE)
set(PREFIX x86_64-linux-musl-)
else ()
set(PREFIX musl-)
endif ()
set(PREFIX x86_64-linux-musl-)
set(CMAKE_C_FLAGS "-m64 -mno-red-zone")
elseif (${ARCH} STREQUAL riscv32)
set(PREFIX riscv64-unknown-elf-)

@ -7,6 +7,7 @@ extern crate alloc;
extern crate rcore_user;
use alloc::vec::Vec;
use core::ptr;
use rcore_user::io::get_line;
use rcore_user::syscall::{sys_exec, sys_fork, sys_wait};
@ -15,9 +16,11 @@ use rcore_user::syscall::{sys_exec, sys_fork, sys_wait};
#[no_mangle]
pub fn main() -> i32 {
println!("Rust user shell");
let mut history = Vec::new();
loop {
print!(">> ");
let cmd = get_line();
let cmd = get_line(&mut history);
// split cmd, make argc & argv
let cmd = cmd.replace(' ', "\0") + "\0";
let ptrs: Vec<*const u8> = cmd.split('\0')
@ -29,7 +32,7 @@ pub fn main() -> i32 {
let pid = sys_fork();
assert!(pid >= 0);
if pid == 0 {
return sys_exec(ptrs[0], ptrs.len(), ptrs.as_ptr());
return sys_exec(ptrs[0], ptrs.as_ptr(), ptr::null());
} else {
let mut code: i32 = 0;
sys_wait(pid as usize, &mut code);

@ -1,4 +1,5 @@
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::{self, Write};
use core::option::Option;
@ -34,33 +35,140 @@ pub fn getc() -> Option<u8> {
}
}
pub fn get_line() -> String {
let mut s = String::new();
const BEL: u8 = 0x07u8;
const BS: u8 = 0x08u8;
const LF: u8 = 0x0au8;
const CR: u8 = 0x0du8;
const ESC: u8 = 0x1bu8;
const DEL: u8 = 0x7fu8;
pub fn get_line(history: &mut Vec<Vec<u8>>) -> String {
let mut cursor = 0;
let mut line_vec = Vec::with_capacity(512);
let mut history_index = history.len();
loop {
let ret = getc();
match ret {
None => return s,
Some(byte) => {
let c = byte as char;
match c {
'\x08' | '\x7f' /* '\b' */ => {
if s.pop().is_some() {
print!("\x08 \x08");
}
match getc().unwrap() {
BS | DEL => {
// Backspace
if cursor > 0 {
cursor -= 1;
line_vec.remove(cursor);
putc(BS);
for byte in &line_vec[cursor..] {
putc(*byte);
}
' '...'\x7e' => {
s.push(c);
print!("{}", c);
putc(b' ');
for _i in cursor..line_vec.len() {
putc(ESC);
putc(b'[');
putc(b'D');
}
'\n' | '\r' => {
print!("\n");
return s;
putc(ESC);
putc(b'[');
putc(b'D');
} else {
putc(BEL);
}
}
CR | LF => {
// Return
putc(CR);
putc(LF);
break;
}
ESC => {
match getc() .unwrap(){
b'[' => {
match getc().unwrap() {
b'D' => {
// Left arrow
if cursor > 0 {
cursor -= 1;
putc(ESC);
putc(b'[');
putc(b'D');
} else {
putc(BEL);
}
}
b'C' => {
// Right arrow
if cursor < line_vec.len() {
cursor += 1;
putc(ESC);
putc(b'[');
putc(b'C');
} else {
putc(BEL);
}
}
direction @ b'A' | direction @ b'B' => {
if direction == b'A' && history_index > 0 {
// Up arrow
history_index -= 1;
} else if direction == b'B' && history.len() > 0 // usize underflow
&& history_index < history.len() - 1
{
// Down arrow
history_index += 1;
} else {
putc(BEL);
continue;
}
for _ in 0..line_vec.len() {
putc(ESC);
putc(b'[');
putc(b'D');
}
for _ in 0..line_vec.len() {
putc(b' ');
}
for _ in 0..line_vec.len() {
putc(ESC);
putc(b'[');
putc(b'D');
}
line_vec = history[history_index].clone();
cursor = line_vec.len();
for byte in &line_vec {
putc(*byte);
}
}
_ => {
putc(BEL);
}
}
}
_ => {}
_ => {
putc(BEL);
}
}
}
byte if byte.is_ascii_graphic() || byte == b' ' => {
line_vec.insert(cursor, byte);
for byte in &line_vec[cursor..] {
putc(*byte);
}
cursor += 1;
for _i in cursor..line_vec.len() {
putc(ESC);
putc(b'[');
putc(b'D');
}
}
_ => {
// unrecognized characters
putc(BEL);
}
}
}
if line_vec.len() > 0 {
history.push(line_vec.clone());
}
String::from_utf8(line_vec).unwrap_or_default()
}
pub fn putc(c: u8) {

@ -38,8 +38,8 @@ pub fn sys_exit(code: usize) -> ! {
}
pub fn sys_exec(name: *const u8, argc: usize, argv: *const *const u8) -> i32 {
sys_call(SyscallId::Exec, name as usize, argc, argv as usize, 0, 0, 0)
pub fn sys_exec(name: *const u8, argv: *const *const u8, envp: *const *const u8) -> i32 {
sys_call(SyscallId::Exec, name as usize, argv as usize, envp as usize, 0, 0, 0)
}
pub fn sys_write(fd: usize, base: *const u8, len: usize) -> i32 {
@ -107,27 +107,27 @@ pub fn sys_set_priority(priority: usize) -> i32 {
#[allow(dead_code)]
enum SyscallId {
Exit = 60,
Fork = 57,
Wait = 61,
Exec = 59,
Clone = 56,
Yield = 24,
Sleep = 35,
Kill = 62,
GetTime = 96,
GetPid = 39,
Mmap = 9,
Munmap = 11,
Open = 2,
Close = 3,
Read = 0,
Write = 1,
Seek = 8,
Open = 2,
Close = 3,
Fstat = 4,
Seek = 8,
Mmap = 9,
Munmap = 11,
Yield = 24,
Dup2 = 33,
Sleep = 35,
GetPid = 39,
Clone = 56,
Fork = 57,
Exec = 59,
Exit = 60,
Wait = 61,
Kill = 62,
Fsync = 74,
GetCwd = 79,
GetDirEntry = 78,
Dup2 = 33,
GetCwd = 79,
GetTime = 96,
SetPriority = 141,
}

Loading…
Cancel
Save