Skip to content

Commit f9b57d2

Browse files
committed
Add read_line function to userspace lib
1 parent 08fc73d commit f9b57d2

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

userspace/src/bin/yash.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,20 @@
22
#![no_main]
33

44
use alloc::string::{String, ToString};
5-
use common::syscalls::{sys_execute, sys_exit, sys_print_programs, sys_read_input_wait, sys_wait};
6-
use userspace::{print, println};
5+
use common::syscalls::{sys_execute, sys_exit, sys_print_programs, sys_wait};
6+
use userspace::{print, println, util::read_line};
77

88
extern crate alloc;
99
extern crate userspace;
1010

11-
const DELETE: u8 = 127;
12-
1311
#[unsafe(no_mangle)]
1412
fn main() {
1513
println!();
1614
println!("### YaSH - Yet another Shell ###");
1715
println!("Type 'help' for a list of available commands.");
1816
loop {
1917
print!("$ ");
20-
let mut input = String::new();
21-
loop {
22-
let result = sys_read_input_wait();
23-
match result {
24-
b'\r' | b'\n' => {
25-
// Carriage return
26-
println!();
27-
break;
28-
}
29-
DELETE => {
30-
if input.pop().is_some() {
31-
print!("{}{}{}", 8 as char, ' ', 8 as char);
32-
}
33-
}
34-
_ => {
35-
assert!(result.is_ascii());
36-
let result = result as char;
37-
input.push(result);
38-
print!("{}", result);
39-
}
40-
}
41-
}
18+
let input = read_line();
4219
// Parse input and execute
4320
parse_command_and_execute(input);
4421
}

userspace/src/util.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1+
extern crate alloc;
2+
3+
use alloc::string::String;
4+
use common::syscalls::sys_read_input_wait;
15
use core::arch::asm;
26

7+
use crate::{print, println};
8+
9+
const DELETE: u8 = 127;
10+
311
pub fn wait(cycles: usize) {
412
for _ in 0..cycles {
513
unsafe {
614
asm!("nop");
715
}
816
}
917
}
18+
19+
pub fn read_line() -> String {
20+
let mut input = String::new();
21+
loop {
22+
let result = sys_read_input_wait();
23+
match result {
24+
b'\r' | b'\n' => {
25+
// Carriage return
26+
println!();
27+
break;
28+
}
29+
DELETE => {
30+
if input.pop().is_some() {
31+
print!("{}{}{}", 8 as char, ' ', 8 as char);
32+
}
33+
}
34+
_ => {
35+
assert!(result.is_ascii());
36+
let result = result as char;
37+
input.push(result);
38+
print!("{}", result);
39+
}
40+
}
41+
}
42+
input
43+
}

0 commit comments

Comments
 (0)