yac
/
yacexits
Archived
3
0
Fork 0
This repository has been archived on 2024-04-25. You can view files and clone it, but cannot push or open issues or pull requests.
yacexits/src/entry/mod.rs

61 lines
1.5 KiB
Rust

mod args;
pub use args::Args;
use crate::exit;
use libc::{
c_int,
strlen,
};
use libc_print::libc_eprintln;
///Converts C string to Rust's, verifying it is UTF-8
///
///It is UB to pass non-C string as it requires \0
pub unsafe fn c_str_to_rust(
ptr: *const u8
) -> Result<&'static str, core::str::Utf8Error> {
let len = strlen(ptr as *const i8);
let parts = core::slice::from_raw_parts(ptr, len);
core::str::from_utf8(parts)
}
///Converts C string to Rust's one assuming it is UTF-8
///
///It is UB to pass non-C string as it requires \0
pub unsafe fn c_str_to_rust_unchecked(ptr: *const u8) -> &'static str {
let len = strlen(ptr as *const i8);
let parts = core::slice::from_raw_parts(ptr, len);
core::str::from_utf8_unchecked(parts)
}
extern "Rust" {
fn rust_main(args: args::Args) -> Result<u32, (alloc::string::String, u32)>;
}
#[doc(hidden)]
#[cfg(not(test))]
#[no_mangle]
pub unsafe extern fn main(argc: c_int, argv: *const *const u8) -> c_int {
match Args::new(argc as isize, argv) {
Ok(args) => {
let argv0 = &args
.into_iter()
.next()
.unwrap_or_else(|| {
libc_eprintln!("Unable to ascertain argv[0].");
exit(71);
});
rust_main(args).unwrap_or_else(|(err, code)| {
libc_eprintln!("{}: {}", argv0, err);
code
}) as _
},
Err(_) => {
libc_eprintln!("Arguments are not valid UTF-8.");
65
},
}
}