Skip to content

Commit

Permalink
ERR and DEBUG without numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
39555 committed Oct 23, 2024
1 parent 827abca commit 5e66425
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 31 deletions.
11 changes: 6 additions & 5 deletions brush-core/src/builtins/kill.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::Parser;
use std::io::Write;

use crate::traps::{self, TrapSignal};
use crate::traps::TrapSignal;
use crate::{builtins, commands, error};

/// Signal a job or process.
Expand Down Expand Up @@ -91,7 +91,9 @@ fn print_signals(
PrintSignal::Name(s.as_str().strip_prefix("SIG").unwrap_or(s.as_str()))
})
} else {
TrapSignal::try_from(s.as_str()).map(|s| PrintSignal::Num(i32::from(s)))
TrapSignal::try_from(s.as_str()).map(|sig| {
i32::try_from(sig).map_or(PrintSignal::Name(sig.as_str()), PrintSignal::Num)
})
};

match signal {
Expand All @@ -108,10 +110,9 @@ fn print_signals(
}
}
} else {
return traps::format_signals(
return crate::traps::format_signals(
context.stdout(),
TrapSignal::iterator()
.filter(|s| !matches!(s, TrapSignal::Err | TrapSignal::Debug | TrapSignal::Exit)),
TrapSignal::iterator().filter(|s| matches!(s, TrapSignal::Signal(_))),
)
.map(|()| builtins::ExitCode::Success);
}
Expand Down
7 changes: 5 additions & 2 deletions brush-core/src/builtins/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ impl builtins::Command for TrapCommand {
mut context: commands::ExecutionContext<'_>,
) -> Result<builtins::ExitCode, crate::error::Error> {
if self.list_signals {
crate::traps::format_signals(context.stdout(), TrapSignal::iterator())
.map(|()| builtins::ExitCode::Success)
crate::traps::format_signals(
context.stdout(),
TrapSignal::iterator().filter(|s| matches!(s, TrapSignal::Signal(_))),
)
.map(|()| builtins::ExitCode::Success)
} else if self.print_trap_commands || self.args.is_empty() {
if !self.args.is_empty() {
for signal_type in &self.args {
Expand Down
3 changes: 2 additions & 1 deletion brush-core/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ impl Pattern {
}

if self.multiline {
// Set option for multiline matching + set option for allowing '.' pattern to match newline.
// Set option for multiline matching + set option for allowing '.' pattern to match
// newline.
regex_str.push_str("(?ms)");
}

Expand Down
50 changes: 29 additions & 21 deletions brush-core/src/traps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ pub fn format_signals(
it: impl Iterator<Item = TrapSignal>,
) -> Result<(), error::Error> {
let it = it
.sorted_by(|a, b| Ord::cmp(&i32::from(*a), &i32::from(*b)))
.format_with("\n", |s, f| f(&format_args!("{}) {s}", i32::from(s))));
.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(())
}
Expand All @@ -85,9 +86,10 @@ impl FromStr for TrapSignal {
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 {
32 => TrapSignal::Debug,
33 => TrapSignal::Err,
0 => TrapSignal::Exit,
#[cfg(unix)]
value => TrapSignal::Signal(
Expand All @@ -104,37 +106,43 @@ impl TryFrom<i32> for TrapSignal {
impl TryFrom<&str> for TrapSignal {
type Error = error::Error;
fn try_from(value: &str) -> Result<Self, Self::Error> {
#[allow(unused_mut)] // on not unix platforms
let mut s = value.to_ascii_uppercase();
// Bash compatibility:
// support for signal names without the `SIG` prefix, for example `HUP` -> `SIGHUP`
if !s.starts_with("SIG") {
s.insert_str(0, "SIG");
}

Ok(match s.as_str() {
"SIGDEBUG" => TrapSignal::Debug,
"SIGERR" => TrapSignal::Err,
"SIGEXIT" => TrapSignal::Exit,
"DEBUG" => TrapSignal::Debug,
"ERR" => TrapSignal::Err,
"EXIT" => TrapSignal::Exit,

#[cfg(unix)]
_ => nix::sys::signal::Signal::from_str(s.as_str())
.map(TrapSignal::Signal)
.map_err(|_| error::Error::InvalidSignal(value.into()))?,
_ => {
// Bash compatibility:
// support for signal names without the `SIG` prefix, for example `HUP` -> `SIGHUP`
if !s.starts_with("SIG") {
s.insert_str(0, "SIG");
}
nix::sys::signal::Signal::from_str(s.as_str())
.map(TrapSignal::Signal)
.map_err(|_| error::Error::InvalidSignal(value.into()))?
}
#[cfg(not(unix))]
_ => return Err(error::Error::InvalidSignal(value.into())),
})
}
}

impl From<TrapSignal> for i32 {
fn from(value: TrapSignal) -> Self {
match value {
#[derive(Debug, Clone, Copy)]
pub struct DoesntHaveANumber;

impl TryFrom<TrapSignal> for i32 {
type Error = DoesntHaveANumber;
fn try_from(value: TrapSignal) -> Result<Self, Self::Error> {
Ok(match value {
#[cfg(unix)]
TrapSignal::Signal(s) => s as i32,
TrapSignal::Debug => 32,
TrapSignal::Err => 33,
TrapSignal::Exit => 0,
}
_ => return Err(DoesntHaveANumber),
})
}
}

Expand Down
2 changes: 0 additions & 2 deletions brush-shell/tests/cases/builtins/kill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,4 @@ cases:
kill -l int
kill -l SIGHUP
kill -l EXIT
kill -l ERR
kill -l DEBUG

0 comments on commit 5e66425

Please sign in to comment.