-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
117 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: "Builtins: trap" | ||
cases: | ||
- name: "trap registration" | ||
stdin: | | ||
trap "echo 1" SIGINT | ||
trap "echo 2" SIGINT | ||
trap | ||
trap "echo 3" int | ||
trap | ||
trap "echo 4" 2 | ||
trap | ||
- name: "trap EXIT" | ||
known_failure: true | ||
stdin: | | ||
trap "echo [exit]" EXIT | ||
trap | ||
- name: "trap DEBUG" | ||
stdin: | | ||
trap 'echo [command: ${BASH_COMMAND}]' DEBUG | ||
trap | ||
- name: "trap ERR" | ||
stdin: | | ||
trap "echo [err]" ERR | ||
trap |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,36 @@ | ||
use std::collections::HashMap; | ||
use std::{collections::HashMap, fmt::Display}; | ||
|
||
#[derive(Clone, Copy, Eq, Hash, PartialEq)] | ||
pub enum TrapSignal { | ||
Signal(nix::sys::signal::Signal), | ||
Debug, | ||
Err, | ||
Exit, | ||
} | ||
|
||
impl Display for TrapSignal { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
TrapSignal::Signal(s) => s.fmt(f), | ||
TrapSignal::Debug => write!(f, "DEBUG"), | ||
TrapSignal::Err => write!(f, "ERR"), | ||
TrapSignal::Exit => write!(f, "EXIT"), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Clone, Default)] | ||
pub struct TrapHandlerConfig { | ||
pub handlers: HashMap<nix::sys::signal::Signal, Vec<String>>, | ||
pub handlers: HashMap<TrapSignal, String>, | ||
pub handler_depth: i32, | ||
} | ||
|
||
impl TrapHandlerConfig { | ||
pub fn register_handler(&mut self, signal_type: nix::sys::signal::Signal, command: String) { | ||
if let Some(handlers) = self.handlers.get_mut(&signal_type) { | ||
handlers.push(command); | ||
} else { | ||
self.handlers.insert(signal_type, vec![command]); | ||
} | ||
pub fn register_handler(&mut self, signal_type: TrapSignal, command: String) { | ||
let _ = self.handlers.insert(signal_type, command); | ||
} | ||
|
||
pub fn remove_handlers(&mut self, signal_type: nix::sys::signal::Signal) { | ||
pub fn remove_handlers(&mut self, signal_type: TrapSignal) { | ||
self.handlers.remove(&signal_type); | ||
} | ||
} |