1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Facilities for configuring trap handlers.
use std::collections::HashMap;
use std::fmt::Display;
use std::str::FromStr;
use itertools::Itertools as _;
use crate::{error, sys};
/// Type of signal that can be trapped in the shell.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TrapSignal {
/// A system signal.
Signal(sys::signal::Signal),
/// The `DEBUG` trap.
Debug,
/// The `ERR` trap.
Err,
/// The `EXIT` trap.
Exit,
/// The `RETURN` trp.
Return,
}
#[cfg(feature = "serde")]
impl serde::Serialize for TrapSignal {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(self.as_str())
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for TrapSignal {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Self::try_from(s.as_str()).map_err(serde::de::Error::custom)
}
}
impl Display for TrapSignal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl TrapSignal {
/// Returns all possible values of [`TrapSignal`].
pub fn iterator() -> impl Iterator<Item = Self> {
const SIGNALS: &[TrapSignal] = &[TrapSignal::Debug, TrapSignal::Err, TrapSignal::Exit];
let iter = itertools::chain!(
SIGNALS.iter().copied(),
sys::signal::Signal::iterator().map(TrapSignal::Signal)
);
iter
}
/// Converts [`TrapSignal`] into its corresponding signal name as a
/// [`&'static str`](str)
pub const fn as_str(self) -> &'static str {
match self {
Self::Signal(s) => s.as_str(),
Self::Debug => "DEBUG",
Self::Err => "ERR",
Self::Exit => "EXIT",
Self::Return => "RETURN",
}
}
}
/// Formats [`Iterator<Item = TrapSignal>`](TrapSignal) to the provided writer.
///
/// # Arguments
///
/// * `f` - Any type that implements [`std::io::Write`].
/// * `it` - An iterator over the signals that will be formatted into the `f`.
///
/// # Errors
///
/// Returns [`crate::error::ErrorKind::IoError`] if writing to `f` fails.
pub fn format_signals(
mut f: impl std::io::Write,
it: impl Iterator<Item = TrapSignal>,
) -> Result<(), error::Error> {
let it = it
.filter_map(|s| i32::try_from(s).ok().map(|n| (s, n)))
.sorted_by(|a, b| Ord::cmp(&a.1, &b.1))
.format_with("\n", |s, f| f(&format_args!("{}) {}", s.1, s.0)));
write!(f, "{it}")?;
Ok(())
}
// implement s.parse::<TrapSignal>()
impl FromStr for TrapSignal {
type Err = error::Error;
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
if let Ok(n) = s.parse::<i32>() {
Self::try_from(n)
} else {
Self::try_from(s)
}
}
}
// from a signal number
impl TryFrom<i32> for TrapSignal {
type Error = error::Error;
fn try_from(value: i32) -> Result<Self, Self::Error> {
// NOTE: DEBUG and ERR are real-time signals, defined based on NSIG or
// SIGRTMAX (is not available on bsd-like systems),
// and don't have persistent numbers across platforms, so we skip them
// here.
Ok(match value {
0 => Self::Exit,
value => Self::Signal(
sys::signal::Signal::try_from(value)
.map_err(|_| error::ErrorKind::InvalidSignal(value.to_string()))?,
),
})
}
}
// from a signal name
impl TryFrom<&str> for TrapSignal {
type Error = error::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
#[allow(unused_mut, reason = "only mutated on some platforms")]
let mut s = value.to_ascii_uppercase();
Ok(match s.as_str() {
"DEBUG" => Self::Debug,
"ERR" => Self::Err,
"EXIT" => Self::Exit,
"RETURN" => Self::Return,
_ => {
// Bash compatibility:
// support for signal names without the `SIG` prefix, for
// example `HUP` -> `SIGHUP`
if !s.starts_with("SIG") {
s.insert_str(0, "SIG");
}
sys::signal::Signal::from_str(s.as_str())
.map(TrapSignal::Signal)
.map_err(|_| error::ErrorKind::InvalidSignal(value.into()))?
}
})
}
}
/// Error type used when failing to convert a `TrapSignal` to a number.
#[derive(Debug, Clone, Copy)]
pub struct TrapSignalNumberError;
impl TryFrom<TrapSignal> for i32 {
type Error = TrapSignalNumberError;
fn try_from(value: TrapSignal) -> Result<Self, Self::Error> {
Ok(match value {
TrapSignal::Signal(s) => s as Self,
TrapSignal::Exit => 0,
_ => return Err(TrapSignalNumberError),
})
}
}
/// A handler for a trap signal.
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TrapHandler {
/// The source text of the command to invoke.
pub command: String,
/// Source information for where the trap handler was defined.
pub source_info: crate::SourceInfo,
}
/// Configuration for trap handlers in the shell.
#[derive(Clone, Default)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TrapHandlerConfig {
/// Registered handlers for traps; maps signal type to command.
handlers: HashMap<TrapSignal, TrapHandler>,
}
impl TrapHandlerConfig {
/// Iterates over the registered handlers for trap signals.
pub fn iter_handlers(&self) -> impl Iterator<Item = (TrapSignal, &TrapHandler)> {
self.handlers
.iter()
.map(|(signal, handler)| (*signal, handler))
}
/// Tries to find the handler associated with the given signal.
///
/// # Arguments
///
/// * `signal_type` - The type of signal to get the handler for.
pub fn get_handler(&self, signal_type: TrapSignal) -> Option<&TrapHandler> {
self.handlers.get(&signal_type)
}
/// Returns whether a handler is registered for the given signal.
pub fn handles(&self, signal_type: TrapSignal) -> bool {
self.handlers.contains_key(&signal_type)
}
/// Registers a handler for a trap signal.
///
/// # Arguments
///
/// * `signal_type` - The type of signal to register a handler for.
/// * `command` - The command to execute when the signal is trapped.
/// * `source_info` - The source info for where the trap handler was
/// defined.
pub fn register_handler(
&mut self,
signal_type: TrapSignal,
command: String,
source_info: crate::SourceInfo,
) {
let _ = self.handlers.insert(
signal_type,
TrapHandler {
command,
source_info,
},
);
}
/// Removes handlers for a trap signal.
///
/// # Arguments
///
/// * `signal_type` - The type of signal to remove handlers for.
pub fn remove_handlers(&mut self, signal_type: TrapSignal) {
self.handlers.remove(&signal_type);
}
}