Skip to main content

os/fs/
stdio.rs

1use super::File;
2use crate::drivers::chardev::CharDevice;
3use crate::drivers::chardev::UART;
4use crate::mm::UserBuffer;
5
6pub struct Stdin;
7pub struct Stdout;
8
9impl File for Stdin {
10    fn readable(&self) -> bool {
11        true
12    }
13    fn writable(&self) -> bool {
14        false
15    }
16    fn read(&self, mut user_buf: UserBuffer) -> usize {
17        assert_eq!(user_buf.len(), 1);
18        //println!("before UART.read() in Stdin::read()");
19        let ch = UART.read();
20        unsafe {
21            user_buf.buffers[0].as_mut_ptr().write_volatile(ch);
22        }
23        1
24    }
25    fn write(&self, _user_buf: UserBuffer) -> usize {
26        panic!("Cannot write to stdin!");
27    }
28}
29
30impl File for Stdout {
31    fn readable(&self) -> bool {
32        false
33    }
34    fn writable(&self) -> bool {
35        true
36    }
37    fn read(&self, _user_buf: UserBuffer) -> usize {
38        panic!("Cannot read from stdout!");
39    }
40    fn write(&self, user_buf: UserBuffer) -> usize {
41        for buffer in user_buf.buffers.iter() {
42            print!("{}", core::str::from_utf8(*buffer).unwrap());
43        }
44        user_buf.len()
45    }
46}