zshrs 0.11.18

The first compiled Unix shell — bytecode VM, worker pool, AOP intercept, Rkyv caching
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
427
//! Port of `_shadow` / `_unshadow` from
//! `Completion/Base/Utility/_shadow`.
//!
//! Full upstream body (97 lines verbatim, abridged):
//! ```text
//! sh: 1  #autoload
//! sh:35  _shadow() {
//! sh:38    local -A fsfx=( -s ${funcstack[2]}:${functrace[2]}:$((.shadow.depth+1)) )
//! sh:42    zparseopts -K -A fsfx -D s:
//! sh:43    for fname; do
//! sh:44      shadowname=${fname}@${fsfx[-s]}
//! sh:45      if (( ${+functions[$shadowname]} )); then continue
//! sh:46      elif (( ${+functions[$fname]} )); then functions -c -- $fname $shadowname
//! sh:50      elif (( ${+builtins[$fname]} )); then alias-shadow
//! sh:53      else alias-cmd
//! sh:54      fi
//! sh:60    builtin set -A .shadow.stack ${fsfx[-s]} $fnames -- ${.shadow.stack}
//! sh:62  }
//! sh:65  _unshadow() {
//! sh:73    while [[ ${.shadow.stack[1]?no shadows} != -- ]]; do
//! sh:74      fname=…; shadowname=…
//! sh:76      if function defined: unfunction; restore from shadowname
//! sh:88    done
//! sh:96  (( ARGC )) && _shadow -s ${funcstack[2]}:${functrace[2]}:1 "$@"
//! ```
//!
//! Real `shfunctab`-backed port. Each `_shadow fname` call:
//!   1. Reads the current `shfunctab` entry for `fname`.
//!   2. Copies it into `<fname>@<suffix>` so the caller can dispatch
//!      to the original via that name (the `_approximate` pattern
//!      reads `compadd@_approximate` to call through to the
//!      compadd-builtin shim).
//!   3. Records the prior entry into a per-call frame on
//!      `SHADOW_STACK` so `_unshadow` can restore.
//!
//! `_unshadow` pops the topmost frame and restores each saved
//! entry (or deletes the user override when no prior entry
//! existed at shadow time).

use crate::ported::hashtable::shfunctab_lock;
use crate::ported::params::{getaparam, setaparam};
use crate::ported::zsh_h::shfunc;
use std::sync::Mutex;

/// One shadow frame — captures the prior state for every name
/// shadowed in a single `_shadow` call.
#[derive(Default)]
struct ShadowFrame {
    /// Suffix tag (`fsfx[-s]` in upstream) used to derive
    /// `<fname>@<suffix>` backup names.
    suffix: String,
    /// One entry per name shadowed in this call: `(fname,
    /// prior_shfunc_or_None)`.
    saved: Vec<(String, Option<shfunc>)>,
}

/// Process-wide stack of shadow frames. Pushed on `_shadow`,
/// popped on `_unshadow`.
static SHADOW_STACK: Mutex<Vec<ShadowFrame>> = Mutex::new(Vec::new());

/// Mirrors `.shadow.depth` from upstream sh:33 — incremented each
/// `_shadow`, decremented each `_unshadow`. Used for suffix
/// generation when the caller doesn't pass `-s`.
static SHADOW_DEPTH: Mutex<i64> = Mutex::new(0);

const STACK_PARAM: &str = ".shadow.stack";
const DEPTH_PARAM: &str = ".shadow.depth";

fn next_suffix(explicit: Option<&str>) -> String {
    if let Some(s) = explicit {
        return s.to_string();
    }
    let mut d = SHADOW_DEPTH.lock().unwrap();
    *d += 1;
    format!("shadow_{}", *d)
}

fn shadow_with_suffix(args: &[String], suffix: &str) -> i32 {
    let mut frame = ShadowFrame {
        suffix: suffix.to_string(),
        saved: Vec::with_capacity(args.len()),
    };
    {
        let mut tab = match shfunctab_lock().write() {
            Ok(t) => t,
            Err(_) => return 1,
        };
        for fname in args {
            // sh:45 — if `<fname>@<suffix>` already exists, this
            //   is a re-shadow with the same suffix; skip.
            let backup_name = format!("{}@{}", fname, suffix);
            if tab.contains_key(&backup_name) {
                frame.saved.push((fname.clone(), None));
                continue;
            }
            let prior = tab.get_including_disabled(fname).cloned();
            if let Some(orig) = prior.as_ref() {
                // sh:47 — `functions -c -- $fname $shadowname`.
                //   Copy the original definition under the backup
                //   name so caller-side `<fname>@<suffix> …` works.
                let mut backup = orig.clone();
                backup.node.nam = backup_name.clone();
                tab.add(backup);
            }
            // sh:50-54 — builtin/command-name shadow paths are
            //   collapsed: we just record whether the name was
            //   present in shfunctab. If not (likely a builtin),
            //   `_unshadow` will simply delete whatever override
            //   the caller installed.
            frame.saved.push((fname.clone(), prior));
        }
    }
    SHADOW_STACK.lock().unwrap().push(frame);

    // Mirror state into shell-side params for diagnostic visibility.
    publish_shell_side_state();
    0
}

/// `_shadow [-s suffix] fname1 fname2 ...` — snapshot one or more
/// shfunctab entries onto the shadow stack so `_unshadow` can
/// restore them later.
pub fn _shadow(args: &[String]) -> i32 {
    if args.is_empty() {
        return 0;
    }
    // sh:42 — `zparseopts -K -A fsfx -D s:`. Parse `-s SUFFIX`
    //   (value-taking) flag.
    let (explicit_suffix, fnames): (Option<String>, Vec<String>) =
        if args.len() >= 2 && args[0] == "-s" {
            (Some(args[1].clone()), args[2..].to_vec())
        } else if let Some(rest) = args[0].strip_prefix("-s") {
            if !rest.is_empty() {
                (Some(rest.to_string()), args[1..].to_vec())
            } else if args.len() >= 2 {
                (Some(args[1].clone()), args[2..].to_vec())
            } else {
                (None, args[1..].to_vec())
            }
        } else {
            (None, args.to_vec())
        };
    let suffix = next_suffix(explicit_suffix.as_deref());
    shadow_with_suffix(&fnames, &suffix)
}

/// `_unshadow` — pop the topmost shadow frame, restoring each
/// saved entry. Returns 0 on success, 1 if the stack was empty.
pub fn _unshadow() -> i32 {
    let frame = match SHADOW_STACK.lock().unwrap().pop() {
        Some(f) => f,
        None => return 1,
    };
    {
        let mut tab = match shfunctab_lock().write() {
            Ok(t) => t,
            Err(_) => return 1,
        };
        for (fname, prior) in frame.saved.into_iter() {
            // sh:76-85 — restore the saved entry. If `prior` is
            //   None, the fn didn't exist at shadow time → remove
            //   the user's override now too.
            match prior {
                Some(orig) => {
                    tab.add(orig);
                }
                None => {
                    tab.remove(&fname);
                }
            }
            // sh:84 — drop the `<fname>@<suffix>` backup
            let backup_name = format!("{}@{}", fname, frame.suffix);
            tab.remove(&backup_name);
        }
    }
    {
        let mut d = SHADOW_DEPTH.lock().unwrap();
        if *d > 0 {
            *d -= 1;
        }
    }
    publish_shell_side_state();
    0
}

/// Mirror shadow-stack state into shell-side params so diagnostic
/// code that inspects `.shadow.stack` / `.shadow.depth` sees a
/// snapshot. Each frame is rendered as
/// `<suffix> {f@,n@}<name>* --` (matching the upstream
/// `set -A .shadow.stack ${fsfx[-s]} $fnames -- ${.shadow.stack}`
/// layout per sh:60).
fn publish_shell_side_state() {
    let stack = SHADOW_STACK.lock().unwrap();
    let depth = SHADOW_DEPTH.lock().unwrap();
    let mut flat: Vec<String> = Vec::new();
    for f in stack.iter() {
        flat.push(f.suffix.clone());
        for (name, saved) in &f.saved {
            let marker = if saved.is_some() { "f@" } else { "n@" };
            flat.push(format!("{}{}", marker, name));
        }
        flat.push("--".to_string());
    }
    setaparam(STACK_PARAM, flat);
    setaparam(DEPTH_PARAM, vec![depth.to_string()]);
}

/// Test-only helper: reset all shadow state.
#[cfg(test)]
pub fn reset_shadow_state() {
    SHADOW_STACK.lock().unwrap().clear();
    *SHADOW_DEPTH.lock().unwrap() = 0;
    setaparam(STACK_PARAM, Vec::new());
    setaparam(DEPTH_PARAM, Vec::new());
}

/// Test-only helper: peek at the current backup name for the
/// topmost frame's shadow of `fname`.
#[cfg(test)]
pub fn current_backup_name(fname: &str) -> Option<String> {
    let stack = SHADOW_STACK.lock().unwrap();
    let frame = stack.last()?;
    if frame.saved.iter().any(|(n, _)| n == fname) {
        Some(format!("{}@{}", fname, frame.suffix))
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ported::zsh_h::hashnode;

    fn make_shfunc(name: &str, body: &str) -> shfunc {
        shfunc {
            node: hashnode {
                nam: name.to_string(),
                next: None,
                flags: 0,
            },
            filename: None,
            lineno: 0,
            funcdef: None,
            redir: None,
            sticky: None,
            body: Some(body.to_string()),
        }
    }

    #[test]
    fn shadow_existing_function_creates_backup() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.remove("compadd");
            tab.add(make_shfunc("compadd", "echo original"));
        }
        assert_eq!(_shadow(&["compadd".to_string()]), 0);
        let backup = current_backup_name("compadd").unwrap();
        {
            let tab = shfunctab_lock().read().unwrap();
            let b = tab.get_including_disabled(&backup).unwrap();
            assert_eq!(b.body.as_deref(), Some("echo original"));
        }
        reset_shadow_state();
        let mut tab = shfunctab_lock().write().unwrap();
        tab.remove("compadd");
        tab.remove(&backup);
    }

    #[test]
    fn unshadow_restores_original_definition() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.remove("myfn");
            tab.add(make_shfunc("myfn", "echo original-body"));
        }
        let _ = _shadow(&["myfn".to_string()]);
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("myfn", "echo override-body"));
        }
        {
            let tab = shfunctab_lock().read().unwrap();
            assert_eq!(
                tab.get_including_disabled("myfn").unwrap().body.as_deref(),
                Some("echo override-body")
            );
        }
        assert_eq!(_unshadow(), 0);
        {
            let tab = shfunctab_lock().read().unwrap();
            assert_eq!(
                tab.get_including_disabled("myfn").unwrap().body.as_deref(),
                Some("echo original-body")
            );
        }
        reset_shadow_state();
        let mut tab = shfunctab_lock().write().unwrap();
        tab.remove("myfn");
    }

    #[test]
    fn unshadow_removes_when_no_prior_definition() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.remove("myfn_nonexistent");
        }
        let _ = _shadow(&["myfn_nonexistent".to_string()]);
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("myfn_nonexistent", "echo created"));
        }
        assert_eq!(_unshadow(), 0);
        let tab = shfunctab_lock().read().unwrap();
        assert!(tab.get_including_disabled("myfn_nonexistent").is_none());
    }

    #[test]
    fn unshadow_on_empty_stack_returns_one() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        assert_eq!(_unshadow(), 1);
    }

    #[test]
    fn shadow_and_unshadow_balances_depth() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("a", ""));
            tab.add(make_shfunc("b", ""));
        }
        let _ = _shadow(&["a".to_string(), "b".to_string()]);
        assert_eq!(*SHADOW_DEPTH.lock().unwrap(), 1);
        let _ = _unshadow();
        assert_eq!(*SHADOW_DEPTH.lock().unwrap(), 0);
        let mut tab = shfunctab_lock().write().unwrap();
        tab.remove("a");
        tab.remove("b");
    }

    #[test]
    fn explicit_suffix_via_dash_s() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("targetfn", ""));
        }
        let _ = _shadow(&["-s".to_string(), "my-suffix".to_string(), "targetfn".to_string()]);
        let backup = current_backup_name("targetfn").unwrap();
        assert_eq!(backup, "targetfn@my-suffix");
        let _ = _unshadow();
        let mut tab = shfunctab_lock().write().unwrap();
        tab.remove("targetfn");
        tab.remove(&backup);
    }

    #[test]
    fn multiple_frames_stack_lifo() {
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("x", "v1"));
        }
        let _ = _shadow(&["x".to_string()]);
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("x", "v2"));
        }
        let _ = _shadow(&["x".to_string()]);
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("x", "v3"));
        }
        assert_eq!(_unshadow(), 0);
        {
            let tab = shfunctab_lock().read().unwrap();
            assert_eq!(
                tab.get_including_disabled("x").unwrap().body.as_deref(),
                Some("v2")
            );
        }
        assert_eq!(_unshadow(), 0);
        {
            let tab = shfunctab_lock().read().unwrap();
            assert_eq!(
                tab.get_including_disabled("x").unwrap().body.as_deref(),
                Some("v1")
            );
        }
        reset_shadow_state();
        let mut tab = shfunctab_lock().write().unwrap();
        tab.remove("x");
    }

    #[test]
    fn published_state_records_frame_layout() {
        // sh:60 — verify the shell-side `.shadow.stack` mirror has
        //   the upstream `suffix fnames... --` layout.
        let _g = crate::test_util::global_state_lock();
        reset_shadow_state();
        {
            let mut tab = shfunctab_lock().write().unwrap();
            tab.add(make_shfunc("fn1", ""));
        }
        let _ = _shadow(&[
            "-s".to_string(),
            "frame1".to_string(),
            "fn1".to_string(),
        ]);
        let stack = getaparam(STACK_PARAM).unwrap_or_default();
        assert_eq!(stack, vec!["frame1", "f@fn1", "--"]);
        let _ = _unshadow();
        let mut tab = shfunctab_lock().write().unwrap();
        tab.remove("fn1");
    }
}