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