Start stdin

This commit is contained in:
2026-03-13 11:15:52 +01:00
parent f67718c3fe
commit de6ef959ce
18 changed files with 347 additions and 72 deletions

View File

@@ -0,0 +1,85 @@
use core::cell::RefCell;
use alloc::boxed::Box;
use io::{IoBase, Read, Seek, Write};
use crate::{
data_structures::circular_buffer::CircularBuffer,
println,
virtual_fs::{VirtualFileSystem, VirtualNode},
};
pub const KEYBOARD_BUFFER_SIZE: usize = 4096; // 4Ko
#[derive(Debug)]
pub struct KeyboardBuffer {
buffer: RefCell<CircularBuffer<u8, KEYBOARD_BUFFER_SIZE>>,
}
impl KeyboardBuffer {
pub fn new() -> Self {
Self {
buffer: RefCell::new(CircularBuffer::new()),
}
}
}
#[derive(Debug)]
pub struct KeyboardBufferNode<'a> {
buffer: &'a KeyboardBuffer,
}
impl<'a> KeyboardBufferNode<'a> {
pub fn new(stdin: &'a KeyboardBuffer) -> Self {
Self { buffer: stdin }
}
}
impl VirtualFileSystem for KeyboardBuffer {
fn open(
&mut self,
path: &bffs::path::Path,
) -> Result<alloc::boxed::Box<dyn crate::virtual_fs::VirtualNode + '_>, ()> {
if !path.is_empty() {
Err(())
} else {
Ok(Box::new(KeyboardBufferNode::new(self)))
}
}
}
impl IoBase for KeyboardBufferNode<'_> {
type Error = ();
}
impl Read for KeyboardBufferNode<'_> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let mut buffer = self.buffer.buffer.borrow_mut();
for i in 0..buf.len() {
buf[i] = buffer.pop().unwrap();
}
Ok(buf.len())
}
}
impl Seek for KeyboardBufferNode<'_> {
fn seek(&mut self, pos: io::SeekFrom) -> Result<u64, Self::Error> {
todo!()
}
}
impl Write for KeyboardBufferNode<'_> {
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let mut buffer = self.buffer.buffer.borrow_mut();
for input in buf {
buffer.push(*input);
}
Ok(buf.len())
}
fn flush(&mut self) -> Result<(), Self::Error> {
todo!()
}
}
impl VirtualNode for KeyboardBufferNode<'_> {}

67
src/virtual_fs/stdin.rs Normal file
View File

@@ -0,0 +1,67 @@
// use core::cell::RefCell;
// use alloc::boxed::Box;
// use io::{IoBase, Read, Seek, Write};
// use crate::virtual_fs::{VirtualFileSystem, VirtualNode};
// pub const STDIN_BUFFER_SIZE: usize = 4096; // 4Ko
// #[derive(Debug)]
// pub struct Stdin {
// buffer: RefCell<[u8; STDIN_BUFFER_SIZE]>,
// }
// impl Stdin {
// pub fn new() -> Self {
// Self {
// buffer: RefCell::new([0; _]),
// }
// }
// }
// #[derive(Debug)]
// pub struct StdinNode<'a> {
// stdin: &'a Stdin,
// }
// impl VirtualFileSystem for Stdin {
// fn open(
// &mut self,
// path: &bffs::path::Path,
// ) -> Result<alloc::boxed::Box<dyn crate::virtual_fs::VirtualNode + '_>, ()> {
// if !path.is_empty() {
// Err(())
// } else {
// Ok(Box::new(StdinNode { stdin: self }))
// }
// }
// }
// impl IoBase for StdinNode<'_> {
// type Error = ();
// }
// impl Read for StdinNode<'_> {
// fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
// todo!()
// }
// }
// impl Seek for StdinNode<'_> {
// fn seek(&mut self, pos: io::SeekFrom) -> Result<u64, Self::Error> {
// todo!()
// }
// }
// impl Write for StdinNode<'_> {
// fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
// todo!()
// }
// fn flush(&mut self) -> Result<(), Self::Error> {
// todo!()
// }
// }
// impl VirtualNode for StdinNode<'_> {}