cornerlibstone 0.1.0

Lib Cornerstone — a zero-dependency foundation for building command-line tools in Rust: argument parsing, nested subcommands, themeable help in multiple presets, structured errors and exit codes, config loading, interactive prompts, shell-completion and man/markdown generation, logging, JSON output, and root/destructive-operation guards.
Documentation
//! Signal handling: install a SIGINT (Ctrl-C) handler that flips a global flag
//! instead of killing the process mid-write. Long-running command bodies poll
//! [`interrupted`] at safe points and exit cleanly, so partial files aren't left
//! corrupt. Zero dependencies — uses a raw `sigaction` on unix.

use std::sync::atomic::{AtomicBool, Ordering};

static INTERRUPTED: AtomicBool = AtomicBool::new(false);

/// Whether an interrupt (Ctrl-C) has been received since the handler was set.
pub fn interrupted() -> bool {
    INTERRUPTED.load(Ordering::Relaxed)
}

/// Clear the interrupt flag (e.g. between phases).
pub fn reset() {
    INTERRUPTED.store(false, Ordering::Relaxed);
}

/// Convenience: return a cancellation error if interrupted, else `Ok(())`.
/// Call at safe checkpoints inside a loop: `sig::check()?;`
pub fn check() -> crate::error::Result<()> {
    if interrupted() {
        Err(crate::error::Error::cancelled("interrupted"))
    } else {
        Ok(())
    }
}

#[cfg(unix)]
extern "C" fn handle(_sig: i32) {
    INTERRUPTED.store(true, Ordering::Relaxed);
}

/// Install the SIGINT handler. Idempotent; safe to call once at startup. The app
/// calls this automatically unless disabled.
#[cfg(unix)]
pub fn install() {
    #[repr(C)]
    struct SigAction {
        sa_handler: extern "C" fn(i32),
        sa_mask: [u64; 16],
        sa_flags: i32,
        sa_restorer: usize,
    }
    extern "C" {
        fn sigaction(signum: i32, act: *const SigAction, oldact: *mut SigAction) -> i32;
    }
    const SIGINT: i32 = 2;
    let act = SigAction {
        sa_handler: handle,
        sa_mask: [0; 16],
        sa_flags: 0,
        sa_restorer: 0,
    };
    unsafe {
        sigaction(SIGINT, &act, std::ptr::null_mut());
    }
    restore_sigpipe();
}

/// Restore the default SIGPIPE behavior. Rust sets SIGPIPE to `SIG_IGN` at
/// startup, which turns a closed downstream pipe (e.g. `mytool | head`) into an
/// `EPIPE` error that a later `println!` unwraps into a panic. Resetting to
/// `SIG_DFL` makes the process terminate quietly on a broken pipe, exactly like
/// a well-behaved Unix filter. Without this, every tool built on the library
/// would crash noisily whenever its stdout is closed early.
#[cfg(unix)]
pub fn restore_sigpipe() {
    extern "C" {
        fn signal(signum: i32, handler: usize) -> usize;
    }
    const SIGPIPE: i32 = 13;
    const SIG_DFL: usize = 0;
    unsafe {
        signal(SIGPIPE, SIG_DFL);
    }
}

#[cfg(not(unix))]
pub fn install() {
    // No-op on non-unix for now; command bodies can still poll `interrupted`
    // (which simply stays false).
}

#[cfg(not(unix))]
pub fn restore_sigpipe() {}