Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-10 09:44:01 -06:00
commit 4aa98abfe5
1625 files changed
+43592

No files matched your search

+165
View File
@@ -0,0 +1,165 @@
#! @file console.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains `console`, the standard input, output, and error the program was
#! given, and `print` and `println`, which write text to the standard output.
import system;
module std::io;
# `std::io` is written once, for every system, over `system`, which is the only
# layer that knows which system it runs on. It takes and gives `path`s, keeps what
# is open in values that close themselves, and reports what fails as an
# `io_error`, inside a `result`.
#! One of the standard streams the program was given, its input, output, or
#! error, which it reads or writes but does not own, so nothing closes it.
#!
#! The reader `input()` lends, and the writers `output()` and `error_output()`
#! lend, are over one each, so closing one of them closes nothing of the
#! console, and the next call lends it again. Only `std::io` makes one.
pub struct console : readable, writable {
var _handle: system::fd;
}
#! Reads what has arrived on the console's handle, waiting only where nothing
#! has.
#! @param this The console to read.
#! @param into Where the bytes read are written.
#! @returns How many bytes were read, which is zero only at the end of the
#! input.
pub def console::read(&mut this, into: slice<u8>): result<u64, io_error> {
val count := this._handle.read(into.data, into.length);
if count < 0 {
return system::error_of(count);
}
return count as u64;
}
#! Writes all of some bytes to the console's handle, at once.
#! @param this The console to write.
#! @param data The bytes to write.
#! @returns Why they could not all be written, where they could not.
pub def console::write(&mut this, data: span<u8>): result<void, io_error> {
return write_all(this._handle, data);
}
#! Writes text to the standard output, through `output()`, and flushes it, so
#! the text and anything given to `output()` before it are written when this
#! returns.
#! @param text The text to write, which may be a literal, a view of one, or
#! the text a `string` owns, since a string lends a view of
#! itself.
pub def print(text: strview): void {
hold_console(&mut output_held);
val out := &mut output();
_ = out.write(text);
_ = out.flush();
let_go_console(&mut output_held);
}
#! Writes text and a new line to the standard output, through `output()`, which
#! writes them both once the line ends.
#! @param text The text to write.
pub def println(text: strview): void {
hold_console(&mut output_held);
val out := &mut output();
_ = out.write(text);
_ = out.write("\n");
let_go_console(&mut output_held);
}
#! Holds the standard output for the thread calling, so what it writes there
#! through the guard, line after line, mixes with no other thread's, as `print`
#! and `println` hold it for each call. Another thread printing waits until the
#! guard closes; this one writes through the guard, not `print` or `println`,
#! while it holds it.
#! @returns The guard, lending the writer `output()` lends.
pub def output_locked(): console_guard {
hold_console(&mut output_held);
return console_guard { _word = (&mut output_held) as *mut atomic<u32> };
}
#! Holds the standard error for the thread calling, as `output_locked` holds
#! the standard output.
#! @returns The guard, lending the writer `error_output()` lends.
pub def error_output_locked(): console_guard {
hold_console(&mut error_held);
return console_guard { _word = (&mut error_held) as *mut atomic<u32>, _error = true };
}
#! Holds the standard output or error for one thread, and lets go of it where it
#! closes.
@nodiscard
pub struct console_guard : owning {
var _word: *mut atomic<u32>;
var _error: bool = false;
}
#! The writer held.
#! @param this The guard.
#! @returns The writer.
pub def console_guard::writer(&mut this): &mut writer<console> {
return this._error ? error_output() : output();
}
#! Lets go of what the guard holds.
#! @param this The guard, which holds nothing after.
pub def console_guard::close(&mut this): void {
if this._word != null {
if this._error {
_ = error_output().flush();
}
let_go_console(&mut this._word[0]);
this._word = null;
}
}
# The locks of the standard output, error, and input, each taken for a call
# writing or reading it, so lines two threads write never mix: 0 where no thread
# holds it, 1 where one does, and 2 where others may wait.
var output_held: atomic<u32>;
var error_held: atomic<u32>;
var input_held: atomic<u32>;
def hold_console(word: &mut atomic<u32>): void {
if word.exchange(1) != 0 {
while word.exchange(2) != 0 {
_ = system::futex_wait((word as *mut atomic<u32>) as *mut u32, 2, -1);
}
}
}
def let_go_console(word: &mut atomic<u32>): void {
if word.exchange(0) == 2 {
_ = system::futex_wake((word as *mut atomic<u32>) as *mut u32, 1);
}
}
# Writes every byte of the data to a handle, giving the failure that stopped it,
# where one did.
def write_all(handle: system::fd, data: span<u8>): result<void, io_error> {
var total := 0u;
while total < data.length {
val count := handle.write(data.data + total, data.length - total);
if count < 0 {
return system::error_of(count);
}
if count == 0 {
return io_error::other;
}
total += count as u64;
}
return;
}
# What an operation of `system` that gives only whether it failed gave.
def outcome(code: i64): result<void, io_error> {
if code < 0 {
return system::error_of(code);
}
return;
}