use std::collections::HashMap;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};
use brush_core::traps::TrapSignal;
use brush_core::variables::ShellVariable;
use brush_core::{ExecutionResult, Shell, SourceInfo, builtins};
#[must_use]
pub fn has_silent_commands(text: &str) -> bool {
if text.contains("[[") {
return true;
}
let chars: Vec<char> = text.chars().collect();
chars
.windows(2)
.enumerate()
.any(|(i, w)| w == ['(', '('] && (i == 0 || chars[i - 1] != '$'))
}
const PROBE: &str = "__agshell_probe";
const RUN_VAR: &str = "__AGSHELL_RUN";
static NEXT_RUN: AtomicU64 = AtomicU64::new(1);
static LOGS: Mutex<Option<HashMap<u64, Vec<Event>>>> = Mutex::new(None);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Kind {
Debug,
Err,
}
#[derive(Debug, Clone)]
struct Event {
kind: Kind,
status: i32,
subshell: bool,
function: bool,
text: String,
}
#[derive(Debug, Clone)]
pub struct Inner {
pub text: String,
pub status: i32,
pub unhandled: bool,
pub inferred: bool,
pub subshell: bool,
pub function: bool,
}
struct Probe;
impl builtins::SimpleCommand for Probe {
fn get_content(
_name: &str,
_content_type: builtins::ContentType,
_options: &builtins::ContentOptions,
) -> Result<String, brush_core::Error> {
Ok("agshell internal trace hook".into())
}
fn execute<SE: brush_core::ShellExtensions, I: Iterator<Item = S>, S: AsRef<str>>(
context: brush_core::ExecutionContext<'_, SE>,
args: I,
) -> Result<ExecutionResult, brush_core::Error> {
let args: Vec<String> = args.map(|a| a.as_ref().to_string()).collect();
let (Some(kind), Some(run)) = (args.get(1), args.get(2)) else {
return Ok(ExecutionResult::success());
};
let Ok(run) = run.parse::<u64>() else {
return Ok(ExecutionResult::success());
};
let event = Event {
kind: if kind == "err" { Kind::Err } else { Kind::Debug },
status: i32::from(context.shell.last_exit_status()),
subshell: context.shell.is_subshell(),
function: context.shell.in_function(),
text: args.get(3).cloned().unwrap_or_default(),
};
if let Ok(mut logs) = LOGS.lock()
&& let Some(log) = logs.get_or_insert_with(HashMap::new).get_mut(&run)
{
log.push(event);
}
Ok(ExecutionResult::success())
}
}
pub struct Installed {
run: u64,
debug: Option<brush_core::traps::TrapHandler>,
err: Option<brush_core::traps::TrapHandler>,
functrace: bool,
errtrace: bool,
}
pub fn install<SE: brush_core::ShellExtensions>(shell: &mut Shell<SE>) -> Installed {
shell.register_builtin_if_unset(PROBE, builtins::simple_builtin::<Probe, SE>());
let run = NEXT_RUN.fetch_add(1, Ordering::Relaxed);
if let Ok(mut logs) = LOGS.lock() {
logs.get_or_insert_with(HashMap::new).insert(run, Vec::new());
}
let _ = shell.set_env_global(RUN_VAR, ShellVariable::new(run.to_string()));
let installed = Installed {
run,
debug: shell.traps().get_handler(TrapSignal::Debug).cloned(),
err: shell.traps().get_handler(TrapSignal::Err).cloned(),
functrace: shell.options().shell_functions_inherit_debug_and_return_traps,
errtrace: shell.options().shell_functions_inherit_err_trap,
};
shell.options_mut().shell_functions_inherit_debug_and_return_traps = true;
shell.options_mut().shell_functions_inherit_err_trap = true;
let source = || SourceInfo::from("agshell");
shell.traps_mut().register_handler(
TrapSignal::Debug,
format!("{PROBE} dbg \"${RUN_VAR}\" \"$BASH_COMMAND\""),
source(),
);
shell.traps_mut().register_handler(
TrapSignal::Err,
format!("{PROBE} err \"${RUN_VAR}\" \"$BASH_COMMAND\""),
source(),
);
installed
}
pub fn finish<SE: brush_core::ShellExtensions>(
shell: &mut Shell<SE>,
installed: Installed,
ambiguous: bool,
) -> Vec<Inner> {
shell.traps_mut().remove_handlers(TrapSignal::Debug);
shell.traps_mut().remove_handlers(TrapSignal::Err);
if let Some(h) = installed.debug {
shell.traps_mut().register_handler(TrapSignal::Debug, h.command, h.source_info);
}
if let Some(h) = installed.err {
shell.traps_mut().register_handler(TrapSignal::Err, h.command, h.source_info);
}
shell.options_mut().shell_functions_inherit_debug_and_return_traps = installed.functrace;
shell.options_mut().shell_functions_inherit_err_trap = installed.errtrace;
let _ = shell.env_mut().unset(RUN_VAR);
let events = LOGS
.lock()
.ok()
.and_then(|mut logs| logs.as_mut().and_then(|m| m.remove(&installed.run)))
.unwrap_or_default();
interpret(&events, ambiguous)
}
fn interpret(events: &[Event], ambiguous: bool) -> Vec<Inner> {
let events: Vec<&Event> = events
.iter()
.filter(|e| !(e.kind == Kind::Debug && e.text.starts_with(PROBE)))
.collect();
let same = |a: &Event, b: &Event| a.subshell == b.subshell && a.function == b.function;
let mut out = Vec::new();
for (i, e) in events.iter().enumerate() {
if e.kind != Kind::Debug {
continue;
}
let rest = &events[i + 1..];
let next = rest.iter().position(|n| same(e, n));
let (status, unhandled, inferred) = match next.map(|at| (at, rest[at])) {
Some((_, n)) if n.kind == Kind::Err && ambiguous => {
out.push(Inner {
text: e.text.clone(),
status: 0,
unhandled: false,
inferred: true,
subshell: e.subshell,
function: e.function,
});
out.push(Inner {
text: format!("(failure at or after: {})", e.text),
status: n.status,
unhandled: n.status != 0,
inferred: false,
subshell: e.subshell,
function: e.function,
});
continue;
}
Some((_, n)) if n.kind == Kind::Err => (n.status, n.status != 0, false),
Some((at, n)) if rest[..at].is_empty() => (n.status, false, false),
_ => (0, false, true),
};
out.push(Inner {
text: e.text.clone(),
status,
unhandled,
inferred,
subshell: e.subshell,
function: e.function,
});
}
out
}