1use super::File;
2use crate::mm::UserBuffer;
3use crate::sbi::console_getchar;
4use crate::task::suspend_current_and_run_next;
5
6pub struct Stdin;
7
8pub struct Stdout;
9
10impl File for Stdin {
11 fn readable(&self) -> bool {
12 true
13 }
14 fn writable(&self) -> bool {
15 false
16 }
17 fn read(&self, mut user_buf: UserBuffer) -> usize {
18 assert_eq!(user_buf.len(), 1);
19 let mut c: usize;
21 loop {
22 c = console_getchar();
23 if c == 0 {
24 suspend_current_and_run_next();
25 continue;
26 } else {
27 break;
28 }
29 }
30 let ch = c as u8;
31 unsafe {
32 user_buf.buffers[0].as_mut_ptr().write_volatile(ch);
33 }
34 1
35 }
36 fn write(&self, _user_buf: UserBuffer) -> usize {
37 panic!("Cannot write to stdin!");
38 }
39}
40
41impl File for Stdout {
42 fn readable(&self) -> bool {
43 false
44 }
45 fn writable(&self) -> bool {
46 true
47 }
48 fn read(&self, _user_buf: UserBuffer) -> usize {
49 panic!("Cannot read from stdout!");
50 }
51 fn write(&self, user_buf: UserBuffer) -> usize {
52 for buffer in user_buf.buffers.iter() {
53 print!("{}", core::str::from_utf8(*buffer).unwrap());
54 }
55 user_buf.len()
56 }
57}