diff --git a/fish-rust/src/ffi.rs b/fish-rust/src/ffi.rs index de94e3b21..b39db82fc 100644 --- a/fish-rust/src/ffi.rs +++ b/fish-rust/src/ffi.rs @@ -70,6 +70,9 @@ include_cpp! { generate!("event_fire_generic") generate!("escape_string") + generate!("sig2wcs") + generate!("wcs2sig") + generate!("signal_get_desc") } impl parser_t { diff --git a/fish-rust/src/signal.rs b/fish-rust/src/signal.rs index faa646b97..15a5a1bf3 100644 --- a/fish-rust/src/signal.rs +++ b/fish-rust/src/signal.rs @@ -1,4 +1,8 @@ +use widestring::U32CStr; + +use crate::ffi; use crate::topic_monitor::{generation_t, invalid_generations, topic_monitor_principal, topic_t}; +use crate::wchar_ffi::{c_str, wstr}; /// A sigint_detector_t can be used to check if a SIGINT (or SIGHUP) has been delivered. pub struct sigchecker_t { @@ -38,3 +42,26 @@ impl sigchecker_t { tm.check(&mut gens, true /* wait */); } } + +/// Get the integer signal value representing the specified signal. +pub fn wcs2sig(s: &wstr) -> Option { + let sig = ffi::wcs2sig(c_str!(s)); + + sig.0.try_into().ok() +} + +/// Get string representation of a signal. +pub fn sig2wcs(sig: usize) -> &'static wstr { + let s = ffi::sig2wcs(i32::try_from(sig).expect("signal should be < 2^31").into()); + let s = unsafe { U32CStr::from_ptr_str(s) }; + + wstr::from_ucstr(s).expect("signal name should be valid utf-32") +} + +/// Returns a description of the specified signal. +pub fn signal_get_desc(sig: usize) -> &'static wstr { + let s = ffi::signal_get_desc(i32::try_from(sig).expect("signal should be < 2^31").into()); + let s = unsafe { U32CStr::from_ptr_str(s) }; + + wstr::from_ucstr(s).expect("signal description should be valid utf-32") +}