acid/firmware2/src/ffi/inout/mod.rs

135 lines
3.1 KiB
Rust
Raw Normal View History

2026-01-05 04:16:08 +01:00
//! This would've been called `io`, but rust-analyzer refuses to see
//! the submodules of this module when it's named `io`.
2026-01-06 22:52:41 +01:00
#![allow(unused_variables)]
2026-01-07 03:33:22 +01:00
use core::{
ffi::{CStr, VaList, c_char, c_int, c_long, c_size_t, c_void},
ptr::null_mut,
};
2026-01-05 04:16:08 +01:00
2026-01-07 03:33:22 +01:00
use alloc::string::String;
use log::{info, warn};
use printf_compat::output::fmt_write;
2026-01-05 04:16:08 +01:00
2026-01-07 03:33:22 +01:00
use crate::ffi::{
inout::file::{FILE, STDERR, STDOUT},
string::__xkbc_memcpy,
};
2026-01-05 04:16:08 +01:00
2026-01-06 22:52:41 +01:00
pub mod dir;
pub mod file;
2026-01-05 04:16:08 +01:00
// File management
#[unsafe(no_mangle)]
2026-01-06 22:52:41 +01:00
pub unsafe extern "C" fn __xkbc_fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE {
2026-01-07 03:33:22 +01:00
warn!(
"The xkbcommon library is attempting to open a file at path: {:?}",
unsafe { CStr::from_ptr(filename) }
);
null_mut()
2026-01-05 04:16:08 +01:00
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __xkbc_fclose(file: *mut FILE) -> c_int {
todo!()
}
#[unsafe(no_mangle)]
2026-01-06 22:52:41 +01:00
pub unsafe extern "C" fn __xkbc_fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int {
2026-01-05 04:16:08 +01:00
todo!()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __xkbc_ftell(stream: *mut FILE) -> c_long {
todo!()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __xkbc_fread(
ptr: *mut c_void,
size: c_size_t,
nobj: c_size_t,
stream: *mut FILE,
) -> c_size_t {
todo!()
}
// Printing
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __xkbc_fprintf(
stream: *mut FILE,
format: *const c_char,
mut args: ...
) -> c_int {
unsafe { __xkbc_vfprintf(stream, format, args.as_va_list()) }
}
#[unsafe(no_mangle)]
2026-01-06 22:52:41 +01:00
pub unsafe extern "C" fn __xkbc_vfprintf(
stream: *mut FILE,
format: *const c_char,
ap: VaList,
) -> c_int {
2026-01-05 04:16:08 +01:00
if stream == STDOUT || stream == STDERR {
2026-01-07 03:33:22 +01:00
let string = ::alloc::format!(
"vfprintf({:?}, {:?}, {:?})",
stream,
unsafe { CStr::from_ptr(format) },
ap
);
2026-01-05 04:16:08 +01:00
info!("{}", string);
string.len() as c_int
} else {
-1
}
}
#[unsafe(no_mangle)]
2026-01-06 22:52:41 +01:00
pub unsafe extern "C" fn __xkbc_vasprintf(
strp: *mut *mut c_char,
fmt: *const c_char,
ap: VaList,
) -> c_int {
2026-01-05 04:16:08 +01:00
todo!()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __xkbc_snprintf(
2026-01-07 03:33:22 +01:00
string: *mut c_char,
size: c_size_t,
2026-01-05 04:16:08 +01:00
format: *const c_char,
2026-01-07 03:33:22 +01:00
mut args: ...
2026-01-05 04:16:08 +01:00
) -> c_int {
2026-01-07 03:33:22 +01:00
unsafe { __xkbc_vsnprintf(string, size, format, args.as_va_list()) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn __xkbc_vsnprintf(
string: *mut c_char,
// Length in bytes **including the terminating null byte**.
size: c_size_t,
format: *const c_char,
ap: VaList,
) -> c_int {
if size == 0 {
return 0;
}
let mut rust_buffer = String::new();
unsafe {
printf_compat::format(format, ap, fmt_write(&mut rust_buffer));
let string_length = core::cmp::min(rust_buffer.len(), size - 1);
// __xkbc_strncpy would be preferrable, if it was available
__xkbc_memcpy(
string as *mut _,
rust_buffer.as_ptr() as *mut _,
string_length,
);
*string.add(string_length) = 0; // Add terminating null byte.
string_length as c_int
}
2026-01-05 04:16:08 +01:00
}