From b6fc81066cfcc29d4519191eae5ba19581ad2774 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Sat, 24 Feb 2024 21:24:19 -0700 Subject: Replace ctrlc with signal-hook Replaced the 'ctrlc' crate with 'signal-hook' for handling of SIGINT. The 'signal_hook::low_level::register' function is actually unsafe. However, according to https://docs.rs/signal-hook/latest/signal_hook/low_level/fn.register.html, it is only unsafe in the case of multithreaded applications. There are some race conditions as well. For instance, it appears that even when we fork to a child process, SIGINT is captured on both that process, as well as the shell. --- src/main.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index db349d3..986e006 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,4 @@ mod recite; -use ctrlc; use recite::path::prefresh; use recite::Poem; use std::io::{self, Write}; @@ -28,7 +27,6 @@ fn repl(path: &Vec<&Path>, prompt: &str) { // Main shell loop loop { // Output the prompt - io::stdout().flush().unwrap(); print!("{}", prompt); io::stdout().flush().unwrap(); @@ -83,11 +81,13 @@ fn main() { let prompt = "|> "; // Handle signals - ctrlc::set_handler(move || { - print!("\n{}", prompt); - io::stdout().flush().unwrap(); - }) - .expect("dwvsh: signals: unable to set sigint handler"); + unsafe { + signal_hook::low_level::register(signal_hook::consts::SIGINT, move || { + print!("\n{}", prompt); + io::stdout().flush().unwrap(); + }) + .unwrap(); + }; // Begin evaluating commands repl(&path, prompt); -- cgit v1.2.3