1use crate::syscalls::{self, define_syscall};
2use safa_abi::{errors::ErrorStatus, raw::processes::TaskMetadata};
3
4use crate::{syscalls::err_from_u16, syscalls::SyscallNum, Lazy};
5
6static META: Lazy<TaskMetadata> =
7 Lazy::new(|| meta_take().expect("failed to take ownership of the task metadata"));
8
9static STDIN: Lazy<usize> = Lazy::new(|| {
12 let stdin: Option<usize> = META.stdin.into();
13 if let Some(stdin) = stdin {
14 stdin
15 } else {
16 syscalls::open("dev:/tty").expect("failed to fall back to `dev:/tty` for stdin")
17 }
18});
19
20static STDOUT: Lazy<usize> = Lazy::new(|| {
21 let stdout: Option<usize> = META.stdout.into();
22 if let Some(stdout) = stdout {
23 stdout
24 } else {
25 syscalls::open("dev:/tty").expect("failed to fall back to `dev:/tty` for stdout")
26 }
27});
28
29use syscalls::types::SyscallResult;
30static STDERR: Lazy<usize> = Lazy::new(|| {
31 let stderr: Option<usize> = META.stderr.into();
32 if let Some(stderr) = stderr {
33 stderr
34 } else {
35 syscalls::open("dev:/tty").expect("failed to fall back to `dev:/tty` for stderr")
36 }
37});
38
39define_syscall!(SyscallNum::SysMetaTake => {
40 sysmeta_take(dest_task: *mut TaskMetadata)
44});
45
46#[inline]
47pub fn meta_take() -> Result<TaskMetadata, ErrorStatus> {
48 let mut dest_meta: TaskMetadata = unsafe { core::mem::zeroed() };
49 err_from_u16!(sysmeta_take(&raw mut dest_meta), dest_meta)
50}
51
52#[cfg_attr(
54 not(any(feature = "std", feature = "rustc-dep-of-std")),
55 unsafe(no_mangle)
56)]
57#[inline(always)]
58pub extern "C" fn sysmeta_stdout() -> usize {
59 **STDOUT
60}
61
62#[cfg_attr(
64 not(any(feature = "std", feature = "rustc-dep-of-std")),
65 unsafe(no_mangle)
66)]
67#[inline(always)]
68pub extern "C" fn sysmeta_stderr() -> usize {
69 **STDERR
70}
71
72#[cfg_attr(
74 not(any(feature = "std", feature = "rustc-dep-of-std")),
75 unsafe(no_mangle)
76)]
77#[inline(always)]
78pub extern "C" fn sysmeta_stdin() -> usize {
79 **STDIN
80}