aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjakobst1n <jakob.stendahl@outlook.com>2024-06-25 22:21:48 +0200
committerjakobst1n <jakob.stendahl@outlook.com>2024-06-25 22:21:48 +0200
commit93d64ae68b2ee271b3a8215c50925f492778e5ef (patch)
tree5ad700b27c0d06345def609f5ef09145e6870505 /src
parentc82432863462bc893b5e1cc2e56fe1bfa9f4ba23 (diff)
downloadtextgraph-93d64ae68b2ee271b3a8215c50925f492778e5ef.tar.gz
textgraph-93d64ae68b2ee271b3a8215c50925f492778e5ef.zip
Remove dependency on libc crate
Diffstat (limited to 'src')
-rw-r--r--src/main.rs39
-rw-r--r--src/term.rs18
2 files changed, 37 insertions, 20 deletions
diff --git a/src/main.rs b/src/main.rs
index 3a0a72c..ab4d5e8 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,12 +1,13 @@
-#[cfg(feature = "libc")]
-use std::io::Write;
use std::io::{self, BufRead};
use std::str::FromStr;
use textgraph::graph::GraphBuilder;
use textgraph::parseopts::{parseopts, OptsBuilder};
-#[cfg(feature = "libc")]
-extern "C" fn handle_sigint(_sig: i32) {
+#[cfg(all(feature = "libc", feature = "ansi"))]
+use std::io::Write;
+
+#[cfg(all(feature = "libc", feature = "ansi"))]
+extern "C" fn handle_sigint(_sig: std::os::raw::c_int) {
print!("\x1b[?25h");
print!("\x1B[?1049l");
io::stdout().flush().unwrap();
@@ -14,21 +15,19 @@ extern "C" fn handle_sigint(_sig: i32) {
}
/// Set a signalhandler for swapping back to the the main screen on sigint
-#[cfg(feature = "libc")]
+#[cfg(all(feature = "libc", feature = "ansi"))]
fn set_filter_signalhandler() {
- let mut sig_action: libc::sigaction = unsafe { std::mem::zeroed() };
- sig_action.sa_flags = 0;
- sig_action.sa_sigaction = handle_sigint as usize;
-
unsafe {
- let mut signal_set = std::mem::zeroed();
- libc::sigemptyset(&mut signal_set);
- sig_action.sa_mask = signal_set;
-
- libc::sigaction(libc::SIGINT, &sig_action, std::ptr::null_mut());
- libc::sigaction(libc::SIGKILL, &sig_action, std::ptr::null_mut());
- libc::sigaction(libc::SIGTSTP, &sig_action, std::ptr::null_mut());
- libc::sigaction(libc::SIGSTOP, &sig_action, std::ptr::null_mut());
+ let mut action: textgraph::term::SigAction = std::mem::zeroed();
+ action.sa_flags = 0;
+ action.sa_sigaction = handle_sigint as usize;
+
+ textgraph::term::sigemptyset(&mut action.sa_mask);
+ textgraph::term::sigaction(15, &action, std::ptr::null_mut()); // 15 is SIGTERM
+ textgraph::term::sigaction(2, &action, std::ptr::null_mut()); // 2 is SIGINT
+ textgraph::term::sigaction(9, &action, std::ptr::null_mut()); // 9 is SIGKILL
+ textgraph::term::sigaction(20, &action, std::ptr::null_mut()); // 20 is SIGSTP
+ textgraph::term::sigaction(19, &action, std::ptr::null_mut()); // 19 is SIGSTOP
}
}
@@ -60,7 +59,7 @@ fn build_graph(x_values: &Vec<f64>, y_values: &Vec<f64>, opts: &OptsBuilder) ->
///
/// * `opts` - textgraph::parseopts::OptBuilder
fn filter(opts: OptsBuilder) {
- #[cfg(feature = "libc")]
+ #[cfg(all(feature = "libc", feature = "ansi"))]
{
set_filter_signalhandler();
print!("\x1b[?1049h");
@@ -77,11 +76,11 @@ fn filter(opts: OptsBuilder) {
let line = line.expect("Could not read...");
let y = f64::from_str(line.as_str());
- if let Err(_) = y {
+ if let Err(_) = y {
print!("Could not parse line as f64.");
continue;
}
- if let Ok(y) = y {
+ if let Ok(y) = y {
y_values.push(y);
x_values.push(i);
}
diff --git a/src/term.rs b/src/term.rs
index 275ab60..9f2d5e2 100644
--- a/src/term.rs
+++ b/src/term.rs
@@ -28,3 +28,21 @@ pub fn get_terminal_size() -> Result<(u16, u16), Error> {
}
Ok((ws.ws_col, ws.ws_row))
}
+
+#[repr(C)]
+pub struct SigAction {
+ pub sa_sigaction: usize,
+ pub sa_mask: SigSet,
+ pub sa_flags: c_int,
+ pub sa_restorer: Option<unsafe extern "C" fn()>,
+}
+
+#[repr(C)]
+pub struct SigSet {
+ pub __val: [u64; 16],
+}
+
+extern "C" {
+ pub fn sigemptyset(set: *mut SigSet) -> c_int;
+ pub fn sigaction(signum: c_int, act: *const SigAction, oldact: *mut SigAction) -> c_int;
+}