2023-07-04 04:57:28 +00:00
|
|
|
use std::process::exit;
|
2023-09-14 20:10:09 +00:00
|
|
|
use std::{io, slice, str};
|
2022-07-06 04:13:09 +00:00
|
|
|
|
2023-07-04 04:57:28 +00:00
|
|
|
use argh::EarlyExit;
|
2023-06-12 08:07:43 +00:00
|
|
|
use libc::c_char;
|
2023-05-26 06:45:38 +00:00
|
|
|
|
2023-09-13 00:35:01 +00:00
|
|
|
use crate::{ffi, StrErr, Utf8CStr};
|
2023-05-26 06:45:38 +00:00
|
|
|
|
2022-08-09 05:53:37 +00:00
|
|
|
pub fn errno() -> &'static mut i32 {
|
2022-08-15 18:53:51 +00:00
|
|
|
unsafe { &mut *libc::__errno() }
|
2022-08-09 05:53:37 +00:00
|
|
|
}
|
|
|
|
|
2022-09-15 23:56:22 +00:00
|
|
|
// When len is 0, don't care whether buf is null or not
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn slice_from_ptr<'a, T>(buf: *const T, len: usize) -> &'a [T] {
|
|
|
|
if len == 0 {
|
|
|
|
&[]
|
|
|
|
} else {
|
|
|
|
slice::from_raw_parts(buf, len)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// When len is 0, don't care whether buf is null or not
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn slice_from_ptr_mut<'a, T>(buf: *mut T, len: usize) -> &'a mut [T] {
|
|
|
|
if len == 0 {
|
|
|
|
&mut []
|
|
|
|
} else {
|
|
|
|
slice::from_raw_parts_mut(buf, len)
|
|
|
|
}
|
|
|
|
}
|
2023-05-10 01:54:38 +00:00
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
// Check libc return value and map to Result
|
|
|
|
pub trait LibcReturn
|
|
|
|
where
|
|
|
|
Self: Copy,
|
|
|
|
{
|
2023-06-12 08:07:43 +00:00
|
|
|
fn is_error(&self) -> bool;
|
|
|
|
fn check_os_err(self) -> io::Result<Self> {
|
|
|
|
if self.is_error() {
|
2023-06-23 08:50:33 +00:00
|
|
|
Err(io::Error::last_os_error())
|
|
|
|
} else {
|
|
|
|
Ok(self)
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-13 00:35:01 +00:00
|
|
|
fn as_os_err(self) -> io::Result<()> {
|
|
|
|
self.check_os_err()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
macro_rules! impl_libc_return {
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
impl LibcReturn for $t {
|
|
|
|
#[inline]
|
|
|
|
fn is_error(&self) -> bool {
|
|
|
|
*self < 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)*)
|
2023-06-12 08:07:43 +00:00
|
|
|
}
|
|
|
|
|
2023-06-23 08:50:33 +00:00
|
|
|
impl_libc_return! { i8 i16 i32 i64 isize }
|
2023-06-12 08:07:43 +00:00
|
|
|
|
|
|
|
impl<T> LibcReturn for *const T {
|
2023-06-23 08:50:33 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn is_error(&self) -> bool {
|
|
|
|
self.is_null()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> LibcReturn for *mut T {
|
2023-06-23 08:50:33 +00:00
|
|
|
#[inline]
|
2023-06-12 08:07:43 +00:00
|
|
|
fn is_error(&self) -> bool {
|
|
|
|
self.is_null()
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 01:17:26 +00:00
|
|
|
|
|
|
|
pub trait MutBytesExt {
|
|
|
|
fn patch(&mut self, from: &[u8], to: &[u8]) -> Vec<usize>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: AsMut<[u8]>> MutBytesExt for T {
|
|
|
|
fn patch(&mut self, from: &[u8], to: &[u8]) -> Vec<usize> {
|
|
|
|
ffi::mut_u8_patch(self.as_mut(), from, to)
|
|
|
|
}
|
|
|
|
}
|
2023-07-04 04:57:28 +00:00
|
|
|
|
2023-07-06 17:06:14 +00:00
|
|
|
// SAFETY: libc guarantees argc and argv are properly setup and are static
|
2023-07-04 04:57:28 +00:00
|
|
|
#[allow(clippy::not_unsafe_ptr_arg_deref)]
|
2023-07-06 17:06:14 +00:00
|
|
|
pub fn map_args(argc: i32, argv: *const *const c_char) -> Result<Vec<&'static str>, StrErr> {
|
2023-07-04 04:57:28 +00:00
|
|
|
unsafe { slice::from_raw_parts(argv, argc as usize) }
|
|
|
|
.iter()
|
2023-09-13 00:35:01 +00:00
|
|
|
.map(|s| unsafe { Utf8CStr::from_ptr(*s) }.map(|s| s.as_str()))
|
2023-07-04 04:57:28 +00:00
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait EarlyExitExt<T> {
|
2023-07-06 00:05:39 +00:00
|
|
|
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T;
|
2023-07-04 04:57:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> EarlyExitExt<T> for Result<T, EarlyExit> {
|
2023-07-06 00:05:39 +00:00
|
|
|
fn on_early_exit<F: FnOnce()>(self, print_help_msg: F) -> T {
|
2023-07-04 04:57:28 +00:00
|
|
|
match self {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(EarlyExit { output, status }) => match status {
|
|
|
|
Ok(_) => {
|
2023-07-06 00:05:39 +00:00
|
|
|
print_help_msg();
|
2023-07-04 04:57:28 +00:00
|
|
|
exit(0)
|
|
|
|
}
|
|
|
|
Err(_) => {
|
|
|
|
eprintln!("{}", output);
|
2023-07-06 00:05:39 +00:00
|
|
|
print_help_msg();
|
2023-07-04 04:57:28 +00:00
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|