mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-09 13:26:48 +00:00
201 lines
4.7 KiB
Rust
201 lines
4.7 KiB
Rust
use crate::prelude::*;
|
|
use crate::tty::{get_n_tty, Tty};
|
|
|
|
use super::file_handle::FileLike;
|
|
use super::file_table::FileDescripter;
|
|
use super::utils::{InodeMode, InodeType, IoEvents, Metadata, Poller, SeekFrom};
|
|
|
|
pub const FD_STDIN: FileDescripter = 0;
|
|
pub const FD_STDOUT: FileDescripter = 1;
|
|
pub const FD_STDERR: FileDescripter = 2;
|
|
|
|
pub struct Stdin {
|
|
console: Option<Arc<Tty>>,
|
|
}
|
|
|
|
pub struct Stdout {
|
|
console: Option<Arc<Tty>>,
|
|
}
|
|
|
|
pub struct Stderr {
|
|
console: Option<Arc<Tty>>,
|
|
}
|
|
|
|
impl FileLike for Stdin {
|
|
fn poll(&self, mask: IoEvents, poller: Option<&Poller>) -> IoEvents {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.poll(mask, poller)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn read(&self, buf: &mut [u8]) -> Result<usize> {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.read(buf)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn ioctl(&self, cmd: super::utils::IoctlCmd, arg: usize) -> Result<i32> {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.ioctl(cmd, arg)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn seek(&self, seek_from: SeekFrom) -> Result<usize> {
|
|
// TODO: do real seek
|
|
Ok(0)
|
|
}
|
|
|
|
fn metadata(&self) -> Metadata {
|
|
Metadata {
|
|
dev: 0,
|
|
ino: 0,
|
|
size: 0,
|
|
blk_size: 1024,
|
|
blocks: 0,
|
|
atime: Default::default(),
|
|
mtime: Default::default(),
|
|
ctime: Default::default(),
|
|
type_: InodeType::CharDevice,
|
|
mode: InodeMode::from_bits_truncate(0o620),
|
|
nlinks: 1,
|
|
uid: 0,
|
|
gid: 0,
|
|
rdev: 0,
|
|
}
|
|
}
|
|
|
|
fn as_any_ref(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
}
|
|
impl FileLike for Stdout {
|
|
fn ioctl(&self, cmd: super::utils::IoctlCmd, arg: usize) -> Result<i32> {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.ioctl(cmd, arg)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn write(&self, buf: &[u8]) -> Result<usize> {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.write(buf)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn seek(&self, seek_from: SeekFrom) -> Result<usize> {
|
|
// TODO: do real seek
|
|
Ok(0)
|
|
}
|
|
|
|
fn metadata(&self) -> Metadata {
|
|
Metadata {
|
|
dev: 0,
|
|
ino: 0,
|
|
size: 0,
|
|
blk_size: 1024,
|
|
blocks: 0,
|
|
atime: Default::default(),
|
|
mtime: Default::default(),
|
|
ctime: Default::default(),
|
|
type_: InodeType::CharDevice,
|
|
mode: InodeMode::from_bits_truncate(0o620),
|
|
nlinks: 1,
|
|
uid: 0,
|
|
gid: 0,
|
|
rdev: 0,
|
|
}
|
|
}
|
|
|
|
fn as_any_ref(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl FileLike for Stderr {
|
|
fn ioctl(&self, cmd: super::utils::IoctlCmd, arg: usize) -> Result<i32> {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.ioctl(cmd, arg)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn write(&self, buf: &[u8]) -> Result<usize> {
|
|
if let Some(console) = self.console.as_ref() {
|
|
console.write(buf)
|
|
} else {
|
|
todo!()
|
|
}
|
|
}
|
|
|
|
fn seek(&self, seek_from: SeekFrom) -> Result<usize> {
|
|
// TODO: do real seek
|
|
Ok(0)
|
|
}
|
|
|
|
fn metadata(&self) -> Metadata {
|
|
Metadata {
|
|
dev: 0,
|
|
ino: 0,
|
|
size: 0,
|
|
blk_size: 1024,
|
|
blocks: 0,
|
|
atime: Default::default(),
|
|
mtime: Default::default(),
|
|
ctime: Default::default(),
|
|
type_: InodeType::CharDevice,
|
|
mode: InodeMode::from_bits_truncate(0o620),
|
|
nlinks: 1,
|
|
uid: 0,
|
|
gid: 0,
|
|
rdev: 0,
|
|
}
|
|
}
|
|
|
|
fn as_any_ref(&self) -> &dyn Any {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl Stdin {
|
|
/// FIXME: console should be file under devfs.
|
|
/// reimplement the function when devfs is enabled.
|
|
pub fn new_with_default_console() -> Self {
|
|
let console = get_n_tty();
|
|
Self {
|
|
console: Some(console.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Stdout {
|
|
/// FIXME: console should be file under devfs.
|
|
/// reimplement the function when devfs is enabled.
|
|
pub fn new_with_default_console() -> Self {
|
|
let console = get_n_tty();
|
|
Self {
|
|
console: Some(console.clone()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Stderr {
|
|
/// FIXME: console should be file under devfs.
|
|
/// reimplement the function when devfs is enabled.
|
|
pub fn new_with_default_console() -> Self {
|
|
let console = get_n_tty();
|
|
Self {
|
|
console: Some(console.clone()),
|
|
}
|
|
}
|
|
}
|