implemented the trap builtin

This commit is contained in:
2026-02-19 16:39:18 -05:00
parent a5d756431c
commit 982d11f21b
11 changed files with 340 additions and 129 deletions

View File

@@ -5,15 +5,10 @@ use std::{
use nix::unistd::{gethostname, getppid, User};
use crate::{
exec_input,
jobs::JobTab,
libsh::{
builtin::trap::TrapTarget, exec_input, jobs::JobTab, libsh::{
error::{ShErr, ShErrKind, ShResult},
utils::VecDequeExt,
},
parse::{ConjunctNode, NdRule, Node, ParsedSrc},
prelude::*,
shopt::ShOpts,
}, parse::{ConjunctNode, NdRule, Node, ParsedSrc}, prelude::*, shopt::ShOpts
};
pub struct Fern {
@@ -292,6 +287,7 @@ impl Deref for ShFunc {
pub struct LogTab {
functions: HashMap<String, ShFunc>,
aliases: HashMap<String, String>,
traps: HashMap<TrapTarget, String>,
}
impl LogTab {
@@ -301,6 +297,18 @@ impl LogTab {
pub fn insert_func(&mut self, name: &str, src: ShFunc) {
self.functions.insert(name.into(), src);
}
pub fn insert_trap(&mut self, target: TrapTarget, command: String) {
self.traps.insert(target, command);
}
pub fn get_trap(&self, target: TrapTarget) -> Option<String> {
self.traps.get(&target).cloned()
}
pub fn remove_trap(&mut self, target: TrapTarget) {
self.traps.remove(&target);
}
pub fn traps(&self) -> &HashMap<TrapTarget, String> {
&self.traps
}
pub fn get_func(&self, name: &str) -> Option<ShFunc> {
self.functions.get(name).cloned()
}
@@ -682,8 +690,12 @@ impl VarTab {
/// A table of metadata for the shell
#[derive(Default, Debug)]
pub struct MetaTab {
// command running duration
runtime_start: Option<Instant>,
runtime_stop: Option<Instant>,
// pending system messages
system_msg: Vec<String>
}
impl MetaTab {
@@ -703,6 +715,15 @@ impl MetaTab {
None
}
}
pub fn post_system_message(&mut self, message: String) {
self.system_msg.push(message);
}
pub fn pop_system_message(&mut self) -> Option<String> {
self.system_msg.pop()
}
pub fn system_msg_pending(&self) -> bool {
!self.system_msg.is_empty()
}
}
/// Read from the job table