1#![no_std]
16#![cfg_attr(feature = "cargo-clippy", allow(clippy::style))]
17#![cfg_attr(rustfmt, rustfmt_skip)]
18
19mod args;
20pub use args::Args;
21
22#[allow(unused)]
23#[cold]
24#[inline(never)]
25unsafe fn invalid_cli_args_error() -> libc::c_int {
26 libc::printf("Unable to parse C argv as utf-8 string\n\0".as_ptr() as _);
27 255
28}
29
30pub unsafe fn c_str_to_rust(ptr: *const u8) -> Result<&'static str, core::str::Utf8Error> {
34 let len = libc::strlen(ptr as *const i8);
35 let parts = core::slice::from_raw_parts(ptr, len);
36 core::str::from_utf8(parts)
37}
38
39pub unsafe fn c_str_to_rust_unchecked(ptr: *const u8) -> &'static str {
43 let len = libc::strlen(ptr as *const i8);
44 let parts = core::slice::from_raw_parts(ptr, len);
45 core::str::from_utf8_unchecked(parts)
46}
47
48extern "Rust" {
49 fn rust_main(args: args::Args) -> isize;
50}
51
52#[doc(hidden)]
53#[cfg(not(test))]
54#[no_mangle]
55pub unsafe extern fn main(argc: libc::c_int, argv: *const *const u8) -> libc::c_int {
56 match args::Args::new(argc as isize, argv) {
57 Ok(args) => rust_main(args) as _,
58 Err(_) => invalid_cli_args_error(),
59 }
60}