145 lines
3.9 KiB
Rust
145 lines
3.9 KiB
Rust
//! This module provides cross platform I/O facilities. It takes inspiration from the Rust `std::io` module,
|
|
//! and uses that for platform support outside of ableOS.
|
|
//! One day, we might hope for this module to be superseded by real `std` support for ableOS.
|
|
use ::std::io::{self, Read, BufRead, BufReader, IoSliceMut};
|
|
|
|
/// Print a string to the console.
|
|
fn print(x: &str) {
|
|
#[cfg(ableOS)] {
|
|
extern "C" {
|
|
fn print_char(x: u32);
|
|
}
|
|
fn print_byte(x: u8) {
|
|
unsafe { print_char(x as u32) }
|
|
}
|
|
for b in x.bytes() {
|
|
print_byte(b);
|
|
}
|
|
}
|
|
#[cfg(not(ableOS))] {
|
|
print!("{}", x)
|
|
}
|
|
}
|
|
/// Like [`print`], but starts a new line after.
|
|
pub fn println(x: &str) {
|
|
print(x);
|
|
print("\n");
|
|
}
|
|
|
|
/// Like [`println`], but meant to print to a standard error output.
|
|
pub fn eprintln(x: &str) {
|
|
#[cfg(ableOS)] {
|
|
println(x);
|
|
}
|
|
#[cfg(not(ableOS))] {
|
|
eprintln!("{}", x)
|
|
}
|
|
}
|
|
|
|
#[cfg(ableOS)]
|
|
static STDIN: ::once_cell::sync::Lazy<::std::sync::Mutex<BufReader<AbleStdinRaw>>> = ::once_cell::sync::Lazy::new(|| {
|
|
::std::sync::Mutex::new(BufReader::new(AbleStdinRaw))
|
|
});
|
|
|
|
#[cfg(ableOS)]
|
|
struct AbleStdinRaw;
|
|
#[cfg(ableOS)]
|
|
impl Read for AbleStdinRaw {
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
// TODO: detect EOF
|
|
loop {
|
|
let chr = unsafe { crate::syscalls::get_input() };
|
|
if chr != 0 {
|
|
break Ok(char::from_u32(chr as u32).unwrap().encode_utf8(buf).len())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A handle to the standard input stream of the process.
|
|
/// See [`Stdin`](::std::io::Stdin) for roughly how this should be treated.
|
|
pub struct Stdin {
|
|
#[cfg(ableOS)]
|
|
inner: &'static ::std::sync::Mutex<BufReader<AbleStdinRaw>>,
|
|
#[cfg(not(ableOS))]
|
|
inner: io::Stdin,
|
|
}
|
|
impl Stdin {
|
|
pub fn lock(&self) -> StdinLock<'_> {
|
|
#[cfg(ableOS)] {
|
|
StdinLock {
|
|
inner: self.inner.lock().unwrap()
|
|
}
|
|
}
|
|
#[cfg(not(ableOS))] {
|
|
StdinLock {
|
|
inner: self.inner.lock(),
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A locked reference to the [`Stdin`] handle.
|
|
/// See [`StdinLock`](::std::io::StdinLock) for roughly how this should be treated.
|
|
pub struct StdinLock<'a> {
|
|
#[cfg(ableOS)]
|
|
inner: ::std::sync::MutexGuard<'a, BufReader<AbleStdinRaw>>,
|
|
#[cfg(not(ableOS))]
|
|
inner: io::StdinLock<'a>,
|
|
}
|
|
|
|
impl<'a> BufRead for StdinLock<'_> {
|
|
fn fill_buf(&mut self) -> io::Result<&[u8]> {
|
|
self.inner.fill_buf()
|
|
}
|
|
|
|
fn consume(&mut self, n: usize) {
|
|
self.inner.consume(n)
|
|
}
|
|
|
|
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
self.inner.read_until(byte, buf)
|
|
}
|
|
|
|
fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
|
|
self.inner.read_line(buf)
|
|
}
|
|
}
|
|
impl Read for StdinLock<'_> {
|
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
self.inner.read(buf)
|
|
}
|
|
|
|
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
|
|
self.inner.read_vectored(bufs)
|
|
}
|
|
|
|
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
|
|
self.inner.read_to_end(buf)
|
|
}
|
|
|
|
fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
|
|
self.inner.read_to_string(buf)
|
|
}
|
|
|
|
fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
|
|
self.inner.read_exact(buf)
|
|
}
|
|
}
|
|
|
|
/// Constructs a new handle to the standard input of the current process.
|
|
/// Each handle returned is a reference to a shared global buffer whose access is synchronized via a mutex.
|
|
/// If you need more explicit control over locking, see the [`Stdin::lock`] method.
|
|
pub fn stdin() -> Stdin {
|
|
#[cfg(ableOS)] {
|
|
Stdin {
|
|
inner: &STDIN
|
|
}
|
|
}
|
|
#[cfg(not(ableOS))] {
|
|
Stdin {
|
|
inner: ::std::io::stdin(),
|
|
}
|
|
}
|
|
}
|