yosh 0.2.7

A POSIX-compliant shell implemented in Rust
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
use std::collections::{BTreeSet, HashMap};

/// Action to take when a trap fires.
#[derive(Debug, Clone, PartialEq)]
pub enum TrapAction {
    Default,
    Ignore,
    Command(String),
}

/// Saved parent traps for display in subshells (POSIX: $(trap) shows parent traps).
type SavedTraps = (Option<TrapAction>, HashMap<i32, TrapAction>);

/// Storage for shell trap settings.
#[derive(Debug, Clone, Default)]
pub struct TrapStore {
    pub exit_trap: Option<TrapAction>,
    pub signal_traps: HashMap<i32, TrapAction>,
    saved_traps: Option<Box<SavedTraps>>,
}

impl TrapStore {
    /// Convert a signal name or number string to a signal number.
    /// Returns None for unknown signals.
    pub fn signal_name_to_number(name: &str) -> Option<i32> {
        // Try numeric parse first
        if let Ok(n) = name.parse::<i32>() {
            return Some(n);
        }
        // "EXIT" is trap-specific (signal 0)
        if name.eq_ignore_ascii_case("EXIT") {
            return Some(0);
        }
        // Delegate to the canonical signal table
        crate::signal::signal_name_to_number(name).ok()
    }

    /// Convert a signal number to its canonical name.
    fn signal_number_to_name(num: i32) -> &'static str {
        if num == 0 {
            return "EXIT";
        }
        crate::signal::signal_number_to_name(num).unwrap_or("UNKNOWN")
    }

    /// Set a trap for the given condition (signal name or number).
    /// Delegates to [`Self::set_trap_with`] using [`crate::signal::is_ignored_on_entry`]
    /// as the ignored-on-entry predicate.
    pub fn set_trap(&mut self, condition: &str, action: TrapAction) -> Result<(), String> {
        self.set_trap_with(condition, action, &crate::signal::is_ignored_on_entry)
    }

    /// Set a trap for the given condition, using `is_ignored` to decide whether
    /// to silently no-op (POSIX §2.11: ignored-on-entry signals cannot be trapped
    /// or reset). Exposed for unit testing so tests can inject a synthetic
    /// predicate without mutating process signal state.
    pub(crate) fn set_trap_with(
        &mut self,
        condition: &str,
        action: TrapAction,
        is_ignored: &dyn Fn(i32) -> bool,
    ) -> Result<(), String> {
        let num = Self::signal_name_to_number(condition)
            .ok_or_else(|| format!("invalid signal name: {}", condition))?;
        if num == 0 {
            // EXIT pseudo-signal is always settable.
            self.exit_trap = Some(action);
            return Ok(());
        }
        if is_ignored(num) {
            // POSIX §2.11: silent no-op.
            return Ok(());
        }
        self.signal_traps.insert(num, action);
        Ok(())
    }

    /// Get the trap action for the given condition (signal name or number).
    #[allow(dead_code)]
    pub fn get_trap(&self, condition: &str) -> Option<&TrapAction> {
        let num = Self::signal_name_to_number(condition)?;
        if num == 0 {
            self.exit_trap.as_ref()
        } else {
            self.signal_traps.get(&num)
        }
    }

    /// Remove/reset the trap for the given condition.
    /// Delegates to [`Self::remove_trap_with`].
    pub fn remove_trap(&mut self, condition: &str) {
        self.remove_trap_with(condition, &crate::signal::is_ignored_on_entry)
    }

    /// Remove/reset a trap with an injected ignored-on-entry predicate.
    /// Silent no-op for ignored-on-entry signals per POSIX §2.11.
    pub(crate) fn remove_trap_with(&mut self, condition: &str, is_ignored: &dyn Fn(i32) -> bool) {
        let Some(num) = Self::signal_name_to_number(condition) else {
            return;
        };
        if num == 0 {
            self.exit_trap = None;
            return;
        }
        if is_ignored(num) {
            return;
        }
        self.signal_traps.remove(&num);
    }

    /// Reset all non-ignored traps to default (POSIX subshell behavior).
    /// Command traps are removed. Ignore traps are preserved.
    ///
    /// Crate-private: external fork-time call sites must use
    /// [`Self::reset_for_subshell`] or [`Self::reset_for_command_sub`], which
    /// additionally handle `saved_traps`. This stays accessible to the
    /// surrounding helpers and unit tests.
    pub(crate) fn reset_non_ignored(&mut self) {
        if matches!(self.exit_trap, Some(TrapAction::Command(_))) {
            self.exit_trap = None;
        }
        self.signal_traps
            .retain(|_, action| matches!(action, TrapAction::Ignore));
    }

    /// Reset traps for an immediate subshell (literal `(...)`, pipeline child,
    /// background `&`). Clears `saved_traps` so a nested `trap` builtin reflects
    /// the post-reset state of the subshell rather than an enclosing
    /// command-substitution's parent snapshot.
    ///
    /// Distinct from [`Self::reset_for_command_sub`]: command substitution
    /// (`$(...)`) preserves parent traps in `saved_traps` for the POSIX
    /// `traps=$(trap)` save/restore pattern (POSIX §2.14 RATIONALE), but
    /// non-command-sub subshells must show their own (reset) state.
    pub fn reset_for_subshell(&mut self) {
        self.reset_non_ignored();
        self.saved_traps = None;
    }

    /// Test-only helper: inspect whether `saved_traps` is populated.
    /// Avoids bumping the field's visibility purely for unit tests.
    #[cfg(test)]
    pub(crate) fn saved_traps_is_some(&self) -> bool {
        self.saved_traps.is_some()
    }

    /// Reset traps for command substitution context.
    /// Saves parent traps so `trap` (no args) in `$(trap)` shows parent traps (POSIX).
    pub fn reset_for_command_sub(&mut self) {
        self.saved_traps = Some(Box::new((
            self.exit_trap.clone(),
            self.signal_traps.clone(),
        )));
        self.reset_non_ignored();
    }

    /// Return signal numbers that have TrapAction::Ignore disposition.
    pub fn ignored_signals(&self) -> Vec<i32> {
        self.signal_traps
            .iter()
            .filter(|(_, action)| matches!(action, TrapAction::Ignore))
            .map(|(&num, _)| num)
            .collect()
    }

    /// Get the trap action for a signal by number (not EXIT).
    pub fn get_signal_trap(&self, sig: i32) -> Option<&TrapAction> {
        self.signal_traps.get(&sig)
    }

    /// Print all active traps in a format suitable for re-input.
    /// Format: `trap -- 'cmd' SIGNAME` or `trap -- '' SIGNAME`.
    /// Default actions are skipped. Exit trap first, then signals sorted by number.
    /// In subshells, displays the saved parent traps (POSIX requirement).
    pub fn display_all(&self) {
        let (exit_trap, signal_traps) = if let Some(saved) = &self.saved_traps {
            (&saved.0, &saved.1)
        } else {
            (&self.exit_trap, &self.signal_traps)
        };

        // Exit trap first
        if let Some(action) = exit_trap {
            match action {
                TrapAction::Command(cmd) => println!("trap -- '{}' EXIT", cmd),
                TrapAction::Ignore => println!("trap -- '' EXIT"),
                TrapAction::Default => {}
            }
        }

        // Union signal_traps keys with ignored-on-entry signals (POSIX §2.11:
        // these must appear in `trap` output even though we didn't install a
        // TrapAction for them). BTreeSet gives deterministic sort-by-number.
        let mut keys: BTreeSet<i32> = signal_traps.keys().copied().collect();
        if let Some(entry_set) = crate::signal::ignored_on_entry_set_opt() {
            for &sig in entry_set {
                keys.insert(sig);
            }
        }
        for num in keys {
            let name = Self::signal_number_to_name(num);
            match signal_traps.get(&num) {
                Some(TrapAction::Command(cmd)) => println!("trap -- '{}' SIG{}", cmd, name),
                Some(TrapAction::Ignore) => println!("trap -- '' SIG{}", name),
                Some(TrapAction::Default) => {}
                None => {
                    // Ignored-on-entry with no explicit trap — display as ''.
                    println!("trap -- '' SIG{}", name);
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_trap_store_default() {
        let store = TrapStore::default();
        assert!(store.exit_trap.is_none());
        assert!(store.signal_traps.is_empty());
    }

    #[test]
    fn test_trap_store_set_exit() {
        let mut store = TrapStore::default();
        store
            .set_trap("EXIT", TrapAction::Command("echo bye".to_string()))
            .unwrap();
        assert!(matches!(
            store.get_trap("EXIT"),
            Some(TrapAction::Command(_))
        ));
    }

    #[test]
    fn test_trap_store_set_signal() {
        let mut store = TrapStore::default();
        store.set_trap("INT", TrapAction::Ignore).unwrap();
        assert!(matches!(store.get_trap("INT"), Some(TrapAction::Ignore)));
        store.set_trap("INT", TrapAction::Default).unwrap();
        assert!(matches!(store.get_trap("INT"), Some(TrapAction::Default)));
    }

    #[test]
    fn test_trap_store_signal_name_to_number() {
        assert_eq!(TrapStore::signal_name_to_number("EXIT"), Some(0));
        assert_eq!(TrapStore::signal_name_to_number("HUP"), Some(1));
        assert_eq!(TrapStore::signal_name_to_number("INT"), Some(2));
        assert_eq!(TrapStore::signal_name_to_number("QUIT"), Some(3));
        assert_eq!(TrapStore::signal_name_to_number("TERM"), Some(15));
        assert_eq!(TrapStore::signal_name_to_number("0"), Some(0));
        assert_eq!(TrapStore::signal_name_to_number("2"), Some(2));
        assert_eq!(TrapStore::signal_name_to_number("INVALID"), None);
    }

    #[test]
    fn test_trap_store_remove() {
        let mut store = TrapStore::default();
        store
            .set_trap("EXIT", TrapAction::Command("echo bye".to_string()))
            .unwrap();
        store.remove_trap("EXIT");
        assert!(store.exit_trap.is_none());
    }

    #[test]
    fn test_trap_store_reset_non_ignored() {
        let mut store = TrapStore::default();
        store
            .set_trap("INT", TrapAction::Command("echo caught".to_string()))
            .unwrap();
        store.set_trap("HUP", TrapAction::Ignore).unwrap();
        store
            .set_trap("TERM", TrapAction::Command("echo term".to_string()))
            .unwrap();
        store.reset_non_ignored();
        assert!(!store.signal_traps.contains_key(&2));
        assert_eq!(store.signal_traps.get(&1), Some(&TrapAction::Ignore));
        assert!(!store.signal_traps.contains_key(&15));
    }

    // -----------------------------------------------------------------------
    // Sub-project 5 — Task 2: Silent-ignore via dependency-injected predicate
    // -----------------------------------------------------------------------

    #[test]
    fn test_set_trap_with_ignored_predicate_is_silent() {
        // Simulate SIGINT (2) as ignored-on-entry via an injected predicate.
        let mut store = TrapStore::default();
        let is_ignored = |sig: i32| sig == 2;
        let result = store.set_trap_with(
            "INT",
            TrapAction::Command("echo caught".to_string()),
            &is_ignored,
        );
        assert!(
            result.is_ok(),
            "silent-ignore must return Ok(()), got {:?}",
            result
        );
        assert!(
            store.signal_traps.is_empty(),
            "signal_traps should remain empty when set on ignored-on-entry; got {:?}",
            store.signal_traps
        );
    }

    #[test]
    fn test_set_trap_with_non_ignored_predicate_inserts_normally() {
        // Regression: when predicate returns false, behaviour matches the
        // original set_trap — insertion into signal_traps.
        let mut store = TrapStore::default();
        let never_ignored = |_sig: i32| false;
        let result = store.set_trap_with(
            "INT",
            TrapAction::Command("echo x".to_string()),
            &never_ignored,
        );
        assert!(result.is_ok());
        assert!(matches!(
            store.signal_traps.get(&2),
            Some(TrapAction::Command(_))
        ));
    }

    #[test]
    fn test_set_trap_exit_signal_bypasses_ignored_check() {
        // EXIT (signal 0) is always settable regardless of the predicate.
        let mut store = TrapStore::default();
        let always_ignored = |_sig: i32| true;
        let result = store.set_trap_with(
            "EXIT",
            TrapAction::Command("echo bye".to_string()),
            &always_ignored,
        );
        assert!(result.is_ok());
        assert!(matches!(store.exit_trap, Some(TrapAction::Command(_))));
    }

    #[test]
    fn test_remove_trap_with_ignored_predicate_is_silent() {
        // Pre-populate signal_traps with SIGINT=Ignore, then attempt to remove
        // with SIGINT marked ignored-on-entry — the entry must remain.
        let mut store = TrapStore::default();
        store.signal_traps.insert(2, TrapAction::Ignore);
        let is_ignored = |sig: i32| sig == 2;
        store.remove_trap_with("INT", &is_ignored);
        assert_eq!(
            store.signal_traps.get(&2),
            Some(&TrapAction::Ignore),
            "remove_trap on ignored-on-entry signal must be silent no-op"
        );
    }

    #[test]
    fn test_trap_store_get_signal_trap() {
        let mut store = TrapStore::default();
        store
            .set_trap("INT", TrapAction::Command("echo caught".to_string()))
            .unwrap();
        assert!(matches!(
            store.get_signal_trap(2),
            Some(TrapAction::Command(_))
        ));
        assert!(store.get_signal_trap(15).is_none());
    }

    #[test]
    fn test_reset_for_subshell_clears_saved_traps() {
        let mut store = TrapStore::default();
        store
            .set_trap("INT", TrapAction::Command("echo parent".into()))
            .unwrap();
        store.reset_for_command_sub();
        assert!(
            store.saved_traps_is_some(),
            "precondition: reset_for_command_sub must populate saved_traps"
        );
        store.reset_for_subshell();
        assert!(
            !store.saved_traps_is_some(),
            "saved_traps must be cleared by reset_for_subshell"
        );
        assert!(
            store.signal_traps.is_empty(),
            "signal_traps must be reset by reset_for_subshell"
        );
    }

    #[test]
    fn test_reset_for_subshell_preserves_ignored() {
        // POSIX §2.11: Ignore-action traps must survive subshell entry.
        let mut store = TrapStore::default();
        store.set_trap("HUP", TrapAction::Ignore).unwrap();
        store
            .set_trap("INT", TrapAction::Command("x".into()))
            .unwrap();
        store.reset_for_subshell();
        assert_eq!(
            store.signal_traps.get(&1),
            Some(&TrapAction::Ignore),
            "HUP Ignore must be preserved"
        );
        assert!(
            !store.signal_traps.contains_key(&2),
            "INT Command must be cleared"
        );
    }

    #[test]
    fn test_reset_for_subshell_with_no_saved_traps_is_safe() {
        // Calling reset_for_subshell on a store without a prior
        // reset_for_command_sub must not panic and must reset signal_traps.
        let mut store = TrapStore::default();
        store
            .set_trap("INT", TrapAction::Command("x".into()))
            .unwrap();
        // saved_traps is None at this point.
        store.reset_for_subshell();
        assert!(!store.saved_traps_is_some());
        assert!(store.signal_traps.is_empty());
    }
}