zshrs 0.11.40

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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
//! POSIX.1e capabilities — port of `Src/Modules/cap.c`.
//!
//! Implements `cap` / `getcap` / `setcap`. Linux-only (libcap);
//! macOS / BSD have no POSIX.1e capability sets — the stubs return
//! Unsupported.
//!
//! Structure mirrors the C source line-by-line:
//!   - `bin_cap` (cap.c:36)
//!   - `bin_getcap` (cap.c:68)
//!   - `bin_setcap` (cap.c:91)
//!   - `static struct builtin bintab[]` (cap.c:123)
//!   - module entries (cap.c:139-178)

#![allow(non_camel_case_types)]
#![allow(non_snake_case)]

use crate::ported::utils::zwarnnam;
use crate::ported::zsh_h::{features, module, options, MAX_OPS};
use std::sync::{Mutex, OnceLock};

// =====================================================================
// libcap FFI — declared in `<sys/capability.h>` (libcap), not libc.
// =====================================================================

#[cfg(all(target_os = "linux", feature = "libcap"))]
mod ffi {
    use libc::{c_char, c_int, c_void, ssize_t};

    /// `cap_t` is an opaque pointer in libcap.
    pub type CapT = *mut c_void;

    #[link(name = "cap")]
    extern "C" {
        /// `cap_get_proc` — see implementation.
        pub fn cap_get_proc() -> CapT;
        /// `cap_set_proc` — see implementation.
        pub fn cap_set_proc(cap_p: CapT) -> c_int;
        /// `cap_get_file` — see implementation.
        pub fn cap_get_file(path: *const c_char) -> CapT;
        /// `cap_set_file` — see implementation.
        pub fn cap_set_file(path: *const c_char, cap_p: CapT) -> c_int;
        /// `cap_from_text` — see implementation.
        pub fn cap_from_text(buf: *const c_char) -> CapT;
        /// `cap_to_text` — see implementation.
        pub fn cap_to_text(caps: CapT, length: *mut ssize_t) -> *mut c_char;
        /// `cap_free` — see implementation.
        pub fn cap_free(obj: *mut c_void) -> c_int;
    }
}

// =====================================================================
// Port of `bin_cap(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))` from Src/Modules/cap.c:36.
// =====================================================================

/// Port of `bin_cap(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))` from `Src/Modules/cap.c:36`.
///
/// `cap [STRING]`: with `STRING`, parse via `cap_from_text` and
/// install via `cap_set_proc`; without args, print the current
/// process's capability set.
#[cfg(all(target_os = "linux", feature = "libcap"))]
/// WARNING: param names don't match C — Rust=(argv, _ops, _func) vs C=(nam, argv, ops, func)
pub(crate) fn bin_cap(nam: &str, argv: &[String], _ops: &options, _func: i32) -> i32 {
    // c:36

    let mut ret = 0;
    if let Some(arg0) = argv.first() {
        // C: caps = cap_from_text(*argv);
        let arg_c = match CString::new(arg0.as_str()) {
            Ok(c) => c,
            Err(_) => {
                zwarnnam(nam, "invalid capability string");
                return 1;
            }
        };
        unsafe {
            let caps = ffi::cap_from_text(arg_c.as_ptr());
            if caps.is_null() {
                zwarnnam(nam, "invalid capability string");
                return 1;
            }
            // C: if (cap_set_proc(caps)) { zwarnnam(...); ret = 1; }
            if ffi::cap_set_proc(caps) != 0 {
                zwarnnam(
                    nam,
                    &format!(
                        "can't change capabilities: {}",
                        std::io::Error::last_os_error()
                    ),
                );
                ret = 1;
            }
            ffi::cap_free(caps);
        }
    } else {
        // C: caps = cap_get_proc(); if (caps) result = cap_to_text(caps, &length);
        unsafe {
            let caps = ffi::cap_get_proc();
            let result = if !caps.is_null() {
                ffi::cap_to_text(caps, std::ptr::null_mut())
            } else {
                std::ptr::null_mut()
            };
            if caps.is_null() || result.is_null() {
                zwarnnam(
                    nam,
                    &format!(
                        "can't get capabilities: {}",
                        std::io::Error::last_os_error()
                    ),
                );
                ret = 1;
            } else {
                let s = CStr::from_ptr(result).to_string_lossy();
                println!("{}", s);
            }
            if !result.is_null() {
                ffi::cap_free(result as *mut libc::c_void);
            }
            if !caps.is_null() {
                ffi::cap_free(caps);
            }
        }
    }
    ret
}

/// Port of `bin_cap()` — non-Linux stub. C uses `bin_notavail`
/// (cap.c:115); we mirror the behaviour by emitting the same
/// "not available on this host" error.
#[cfg(not(all(target_os = "linux", feature = "libcap")))]
pub(crate) fn bin_cap(nam: &str, _argv: &[String], _ops: &options, _func: i32) -> i32 {
    zwarnnam(nam, "not available on this host");
    1
}

// =====================================================================
// Port of `bin_getcap(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))` from Src/Modules/cap.c:68.
// =====================================================================

/// Port of `bin_getcap(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))` from `Src/Modules/cap.c:68`.
///
/// `getcap FILE...`: print each file's capability set as
/// `FILE CAPS`. C bails on the first error but iterates the rest;
/// the Rust port mirrors that exact loop.
#[cfg(all(target_os = "linux", feature = "libcap"))]
/// WARNING: param names don't match C — Rust=(argv, _ops, _func) vs C=(nam, argv, ops, func)
pub(crate) fn bin_getcap(nam: &str, argv: &[String], _ops: &options, _func: i32) -> i32 {
    let mut ret = 0;
    // C: do { ... } while(*++argv);
    for file in argv {
        let path_c = match CString::new(file.as_str()) {
            Ok(c) => c,
            Err(_) => {
                zwarnnam(nam, &format!("{}: invalid path", file));
                ret = 1;
                continue;
            }
        };
        unsafe {
            let caps = ffi::cap_get_file(path_c.as_ptr());
            let result = if !caps.is_null() {
                ffi::cap_to_text(caps, std::ptr::null_mut())
            } else {
                std::ptr::null_mut()
            };
            if caps.is_null() || result.is_null() {
                zwarnnam(
                    nam,
                    &format!("{}: {}", file, std::io::Error::last_os_error()),
                );
                ret = 1;
            } else {
                let s = CStr::from_ptr(result).to_string_lossy();
                println!("{} {}", file, s);
            }
            if !result.is_null() {
                ffi::cap_free(result as *mut libc::c_void);
            }
            if !caps.is_null() {
                ffi::cap_free(caps);
            }
        }
    }
    ret
}

/// Port of `bin_getcap()` — non-Linux stub.
#[cfg(not(all(target_os = "linux", feature = "libcap")))]
pub(crate) fn bin_getcap(nam: &str, _argv: &[String], _ops: &options, _func: i32) -> i32 {
    zwarnnam(nam, "not available on this host");
    1
}

// =====================================================================
// Port of `bin_setcap(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))` from Src/Modules/cap.c:91.
// =====================================================================

/// Port of `bin_setcap(char *nam, char **argv, UNUSED(Options ops), UNUSED(int func))` from `Src/Modules/cap.c:91`.
///
/// `setcap STRING FILE...`: parse `STRING` via `cap_from_text`, then
/// apply via `cap_set_file` to each remaining file argument. Mirrors
/// C's loop: free `caps` once at end, advance `argv` per iteration.
#[cfg(all(target_os = "linux", feature = "libcap"))]
/// WARNING: param names don't match C — Rust=(argv, _ops, _func) vs C=(nam, argv, ops, func)
pub(crate) fn bin_setcap(nam: &str, argv: &[String], _ops: &options, _func: i32) -> i32 {
    let mut ret = 0;
    let cap_str = match argv.first() {
        Some(s) => s.as_str(),
        None => {
            zwarnnam(nam, "invalid capability string");
            return 1;
        }
    };
    let cap_c = match CString::new(cap_str) {
        Ok(c) => c,
        Err(_) => {
            zwarnnam(nam, "invalid capability string");
            return 1;
        }
    };
    unsafe {
        let caps = ffi::cap_from_text(cap_c.as_ptr());
        if caps.is_null() {
            zwarnnam(nam, "invalid capability string");
            return 1;
        }
        // C: do { if(cap_set_file(...)) { zwarnnam; ret = 1; } } while(*++argv);
        for file in &argv[1..] {
            let path_c = match CString::new(file.as_str()) {
                Ok(c) => c,
                Err(_) => {
                    zwarnnam(nam, &format!("{}: invalid path", file));
                    ret = 1;
                    continue;
                }
            };
            if ffi::cap_set_file(path_c.as_ptr(), caps) != 0 {
                zwarnnam(
                    nam,
                    &format!("{}: {}", file, std::io::Error::last_os_error()),
                );
                ret = 1;
            }
        }
        ffi::cap_free(caps);
    }
    ret
}

/// Port of `bin_setcap()` — non-Linux stub.
#[cfg(not(all(target_os = "linux", feature = "libcap")))]
pub(crate) fn bin_setcap(nam: &str, _argv: &[String], _ops: &options, _func: i32) -> i32 {
    zwarnnam(nam, "not available on this host");
    1
}

// =====================================================================
// /* module paraphernalia */                                        c:121
// static struct builtin bintab[]                                    c:123
// static struct features module_features                            c:129
// =====================================================================

// `bintab` — port of `static struct builtin bintab[]` (cap.c:123).

// `module_features` — port of `static struct features module_features`
// from cap.c:129. Uses canonical slice-based `module::Features`.

// =====================================================================
// Module entry points (cap.c:138-178).
// =====================================================================

/// Port of `setup_(UNUSED(Module m))` from `Src/Modules/cap.c:139`.
#[allow(unused_variables)]
pub fn setup_(m: *const module) -> i32 {
    // c:139
    0 // c:154
}

/// Port of `features_(UNUSED(Module m), UNUSED(char ***features))` from `Src/Modules/cap.c:146`.
/// C body: `*features = featuresarray(m, &module_features); return 0;`
pub fn features_(m: *const module, features: &mut Vec<String>) -> i32 {
    // c:146
    *features = featuresarray(m, module_features());
    0 // c:161
}

/// Port of `enables_(UNUSED(Module m), UNUSED(int **enables))` from `Src/Modules/cap.c:154`.
/// C body: `return handlefeatures(m, &module_features, enables);`
pub fn enables_(m: *const module, enables: &mut Option<Vec<i32>>) -> i32 {
    // c:154
    handlefeatures(m, module_features(), enables) // c:168
}

/// Port of `boot_(UNUSED(Module m))` from `Src/Modules/cap.c:161`.
#[allow(unused_variables)]
pub fn boot_(m: *const module) -> i32 {
    // c:161
    0 // c:175
}

/// Port of `cleanup_(UNUSED(Module m))` from `Src/Modules/cap.c:168`.
/// C body: `return setfeatureenables(m, &module_features, NULL);`
pub fn cleanup_(m: *const module) -> i32 {
    // c:168
    setfeatureenables(m, module_features(), None) // c:175
}

/// Port of `finish_(UNUSED(Module m))` from `Src/Modules/cap.c:175`.
#[allow(unused_variables)]
pub fn finish_(m: *const module) -> i32 {
    // c:175
    0 // c:175
}

// =====================================================================
// ShellExecutor bridge — sanctioned PORT.md exception. Wires the
// internal builtin dispatcher to the canonical free ported above.
// =====================================================================

// (impl ShellExecutor block moved to src/exec_shims.rs — see file marker)

// =====================================================================
// Tests
// =====================================================================

static MODULE_FEATURES: OnceLock<Mutex<features>> = OnceLock::new();

// Local stubs for the per-module entry points. C uses generic
// `featuresarray`/`handlefeatures`/`setfeatureenables` (module.c:
// 3275/3370/3445) but those take `Builtin` + `Features` pointer
// fields the Rust port doesn't carry. The hardcoded descriptor
// list mirrors the C bintab/conddefs/mathfuncs/paramdefs.
// WARNING: NOT IN CAP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn featuresarray(_m: *const module, _f: &Mutex<features>) -> Vec<String> {
    vec![
        "b:cap".to_string(),
        "b:getcap".to_string(),
        "b:setcap".to_string(),
    ]
}

// WARNING: NOT IN CAP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn handlefeatures(_m: *const module, _f: &Mutex<features>, enables: &mut Option<Vec<i32>>) -> i32 {
    if enables.is_none() {
        *enables = Some(vec![1; 3]);
    }
    0
}

// WARNING: NOT IN CAP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn setfeatureenables(_m: *const module, _f: &Mutex<features>, _e: Option<&[i32]>) -> i32 {
    0
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor ported for `OnceLock<Mutex<T>>` / `OnceLock<
// RwLock<T>>` globals declared above. C zsh uses direct global
// access; Rust needs these wrappers because `OnceLock::get_or_init`
// is the only way to lazily construct shared state. These ported sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port ported.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor ported for `OnceLock<Mutex<T>>` / `OnceLock<
// RwLock<T>>` globals declared above. C zsh uses direct global
// access; Rust needs these wrappers because `OnceLock::get_or_init`
// is the only way to lazily construct shared state. These ported sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port ported.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// WARNING: NOT IN CAP.C — Rust-only module-framework shim.
// C uses generic featuresarray/handlefeatures/setfeatureenables from
// Src/module.c:3275/3370/3445 with C-side Builtin/Features pointers;
// Rust per-module shims hardcode the bintab/conddefs/mathfuncs/paramdefs.
fn module_features() -> &'static Mutex<features> {
    MODULE_FEATURES.get_or_init(|| {
        Mutex::new(features {
            bn_list: None,
            bn_size: 3,
            cd_list: None,
            cd_size: 0,
            mf_list: None,
            mf_size: 0,
            pd_list: None,
            pd_size: 0,
            n_abstract: 0,
        })
    })
}

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

    fn empty_ops() -> options {
        options {
            ind: [0u8; MAX_OPS],
            args: Vec::new(),
            argscount: 0,
            argsalloc: 0,
        }
    }

    #[test]
    fn test_features_returns_bintab_names() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        let mut features: Vec<String> = Vec::new();
        let rc = features_(m, &mut features);
        assert_eq!(rc, 0);
        assert_eq!(features, vec!["b:cap", "b:getcap", "b:setcap"]);
    }

    #[test]
    fn test_enables_get_then_set() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        let mut enables: Option<Vec<i32>> = None;
        let rc = enables_(m, &mut enables);
        assert_eq!(rc, 0);
        let v = enables.as_ref().unwrap();
        assert_eq!(v.len(), 3);
        let rc = enables_(m, &mut enables);
        assert_eq!(rc, 0);
    }

    #[test]
    fn test_cleanup_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        assert_eq!(cleanup_(m), 0);
    }

    #[test]
    #[cfg(not(all(target_os = "linux", feature = "libcap")))]
    fn test_bin_cap_unsupported_on_macos() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        // Without libcap, all three bin_* return 1 (notavail).
        assert_eq!(bin_cap("cap", &[], &ops, 0), 1);
        assert_eq!(bin_getcap("getcap", &["/etc/passwd".into()], &ops, 0), 1);
        assert_eq!(
            bin_setcap(
                "setcap",
                &["cap_net_admin+ep".into(), "/tmp/x".into()],
                &ops,
                0
            ),
            1
        );
    }

    /// c:146 — every feature in the list uses the canonical `b:`
    /// (builtin) prefix per zsh's module-feature naming spec. A regen
    /// that swaps in `p:` (param) would silently make `zmodload -F
    /// zsh/cap +cap` fail.
    #[test]
    fn features_all_use_b_prefix() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        let mut features: Vec<String> = Vec::new();
        features_(m, &mut features);
        for f in &features {
            assert!(
                f.starts_with("b:"),
                "feature {} must use 'b:' (builtin) prefix",
                f
            );
        }
    }

    /// c:154 — `enables_` returns a vec of length matching the
    /// feature count exactly. Mismatch means the bintab dispatcher
    /// would either OOB-read or never reach the last builtin's enable
    /// bit.
    #[test]
    fn enables_vec_length_matches_features_count() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        let mut features: Vec<String> = Vec::new();
        features_(m, &mut features);
        let mut enables: Option<Vec<i32>> = None;
        enables_(m, &mut enables);
        let e = enables.expect("enables_ must return Some");
        assert_eq!(
            e.len(),
            features.len(),
            "enables vec length must match features count"
        );
    }

    /// c:139-175 — module-lifecycle stubs all return 0 in C.
    /// Catches a regression where one of setup/boot/cleanup/finish
    /// stops being a thin pass-through and starts returning a non-zero
    /// status that would prevent module load.
    #[test]
    fn module_lifecycle_shims_all_return_zero() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        assert_eq!(setup_(m), 0);
        assert_eq!(boot_(m), 0);
        assert_eq!(cleanup_(m), 0);
        assert_eq!(finish_(m), 0);
    }

    /// c:200 — `bin_setcap` requires 2 positional args. With < 2 on
    /// a non-libcap host, the notavail stub still surfaces 1, NOT 2
    /// (the "bad usage" code) — platform-availability check fires
    /// before usage check.
    #[test]
    #[cfg(not(all(target_os = "linux", feature = "libcap")))]
    fn bin_setcap_unavailable_check_fires_before_usage_check() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_setcap("setcap", &["cap_net_admin+ep".into()], &ops, 0);
        assert_eq!(r, 1, "notavail stub must return 1 regardless of arg count");
    }

    // ─── zsh-corpus pins for cap on non-libcap host ────────────────

    /// On non-libcap host, `bin_cap` returns 1 (notavail).
    #[test]
    #[cfg(not(all(target_os = "linux", feature = "libcap")))]
    fn cap_corpus_bin_cap_notavail_returns_one() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_cap("cap", &[], &ops, 0);
        assert_eq!(r, 1, "non-libcap host = notavail");
    }

    /// On non-libcap host, `bin_getcap` returns 1.
    #[test]
    #[cfg(not(all(target_os = "linux", feature = "libcap")))]
    fn cap_corpus_bin_getcap_notavail_returns_one() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_getcap("getcap", &[], &ops, 0);
        assert_eq!(r, 1);
    }

    /// On non-libcap host, `bin_setcap` returns 1 even with full args.
    #[test]
    #[cfg(not(all(target_os = "linux", feature = "libcap")))]
    fn cap_corpus_bin_setcap_notavail_returns_one_with_full_args() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_setcap(
            "setcap",
            &["cap_net_admin+ep".into(), "/bin/ls".into()],
            &ops,
            0,
        );
        assert_eq!(r, 1);
    }

    /// Lifecycle shims all return 0.
    #[test]
    fn cap_corpus_lifecycle_all_return_zero() {
        let _g = crate::test_util::global_state_lock();
        let m: *const module = std::ptr::null();
        assert_eq!(setup_(m), 0);
        assert_eq!(boot_(m), 0);
        assert_eq!(cleanup_(m), 0);
        assert_eq!(finish_(m), 0);
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity tests for Src/Modules/cap.c builtins.
    // ═══════════════════════════════════════════════════════════════════

    /// c:62 — `bin_cap` with no args queries current caps;
    /// platform-dependent result. Pin no-panic only.
    #[test]
    fn bin_cap_no_args_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let _ = bin_cap("cap", &[], &ops, 0);
    }

    /// c:147 — `bin_getcap` with no args returns nonzero (usage error).
    #[test]
    fn bin_getcap_no_args_returns_nonzero() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_getcap("getcap", &[], &ops, 0);
        assert_ne!(r, 0, "no path arg → usage error");
    }

    /// c:205 — `bin_setcap` with no args returns nonzero.
    #[test]
    fn bin_setcap_no_args_returns_nonzero() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_setcap("setcap", &[], &ops, 0);
        assert_ne!(r, 0, "no args → usage error");
    }

    /// c:205 — `bin_setcap` with only one arg returns nonzero.
    #[test]
    fn bin_setcap_one_arg_returns_nonzero() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_setcap("setcap", &["cap_net_admin+ep".into()], &ops, 0);
        assert_ne!(r, 0, "missing path arg → usage error");
    }

    /// c:147 — `bin_getcap` on nonexistent path returns nonzero.
    #[test]
    fn bin_getcap_nonexistent_path_returns_nonzero() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_getcap("getcap", &["/__never_exists_zshrs_xyz__".into()], &ops, 0);
        assert_ne!(r, 0, "nonexistent path → error");
    }

    /// c:274 — split per-hook setup_ pin.
    #[test]
    fn cap_setup_returns_zero_pin() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(setup_(std::ptr::null()), 0);
    }

    /// c:281 — features_ returns 0.
    #[test]
    fn cap_features_returns_zero_pin() {
        let _g = crate::test_util::global_state_lock();
        let mut features = Vec::new();
        assert_eq!(features_(std::ptr::null(), &mut features), 0);
    }

    /// c:289 — enables_ no panic.
    #[test]
    fn cap_enables_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let mut e: Option<Vec<i32>> = None;
        let _ = enables_(std::ptr::null(), &mut e);
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity tests for Src/Modules/cap.c on platforms
    // without libcap (macOS, BSDs): bin_cap/bin_getcap/bin_setcap all
    // return nonzero ("not available") regardless of input.
    // ═══════════════════════════════════════════════════════════════════

    /// c:62 — `bin_cap` is deterministic for the same input.
    #[test]
    fn bin_cap_no_args_is_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let first = bin_cap("cap", &[], &ops, 0);
        for _ in 0..5 {
            assert_eq!(
                bin_cap("cap", &[], &ops, 0),
                first,
                "bin_cap must be deterministic"
            );
        }
    }

    /// c:147 — `bin_getcap` with empty path arg returns nonzero.
    #[test]
    fn bin_getcap_empty_path_returns_nonzero() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_getcap("getcap", &["".into()], &ops, 0);
        assert_ne!(r, 0, "empty path → error");
    }

    /// c:205 — `bin_setcap` with empty cap spec returns nonzero.
    #[test]
    fn bin_setcap_empty_spec_returns_nonzero() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let r = bin_setcap("setcap", &["".into(), "/tmp".into()], &ops, 0);
        assert_ne!(r, 0, "empty cap spec → error");
    }

    /// c:62,147,205 — return values fit in canonical exit-code range
    /// (signed-byte 0..256).
    #[test]
    fn cap_builtins_return_in_exit_code_range() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        for r in [
            bin_cap("cap", &[], &ops, 0),
            bin_getcap("getcap", &["/tmp".into()], &ops, 0),
            bin_setcap("setcap", &["cap=ep".into(), "/tmp".into()], &ops, 0),
        ] {
            assert!(
                (0..256).contains(&r),
                "exit code must fit in u8 range, got {}",
                r
            );
        }
    }

    /// c:147 — `bin_getcap` with multiple paths doesn't panic.
    #[test]
    fn bin_getcap_multiple_paths_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let _ = bin_getcap(
            "getcap",
            &["/tmp".into(), "/etc".into(), "/dev/null".into()],
            &ops,
            0,
        );
    }

    /// c:62 — `bin_cap` with arbitrary spec args doesn't panic.
    #[test]
    fn bin_cap_with_args_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let _ = bin_cap("cap", &["cap_net_admin+ep".into()], &ops, 0);
        let _ = bin_cap("cap", &["+all".into()], &ops, 0);
        let _ = bin_cap("cap", &["-all".into()], &ops, 0);
    }

    /// c:303 — cleanup_ idempotent.
    #[test]
    fn cap_cleanup_idempotent() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..10 {
            assert_eq!(cleanup_(std::ptr::null()), 0);
        }
    }

    /// c:310 — finish_ idempotent.
    #[test]
    fn cap_finish_idempotent() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..10 {
            assert_eq!(finish_(std::ptr::null()), 0);
        }
    }

    /// c:296 — boot_ idempotent.
    #[test]
    fn cap_boot_idempotent() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..10 {
            assert_eq!(boot_(std::ptr::null()), 0);
        }
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity tests for Src/Modules/cap.c
    // c:62 bin_cap / c:147 bin_getcap / c:205 bin_setcap /
    // c:274-310 lifecycle + Linux/non-Linux branch parity
    // ═══════════════════════════════════════════════════════════════════

    /// c:62/147/205 — every `bin_*` cap builtin returns i32 (compile-time pin).
    #[test]
    fn bin_cap_builtins_return_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let _: i32 = bin_cap("cap", &[], &ops, 0);
        let _: i32 = bin_getcap("getcap", &["/tmp".into()], &ops, 0);
        let _: i32 = bin_setcap("setcap", &["x".into(), "/tmp".into()], &ops, 0);
    }

    /// c:62 — `bin_cap` deterministic for no-args (no hidden state mutation).
    #[test]
    fn bin_cap_no_args_deterministic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let first = bin_cap("cap", &[], &ops, 0);
        for _ in 0..5 {
            assert_eq!(
                bin_cap("cap", &[], &ops, 0),
                first,
                "bin_cap no-args must be pure"
            );
        }
    }

    /// c:147 — `bin_getcap` deterministic for fixed path arg.
    #[test]
    fn bin_getcap_deterministic_for_fixed_path() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let first = bin_getcap("getcap", &["/dev/null".into()], &ops, 0);
        for _ in 0..3 {
            assert_eq!(
                bin_getcap("getcap", &["/dev/null".into()], &ops, 0),
                first,
                "bin_getcap on /dev/null must be deterministic"
            );
        }
    }

    /// c:205 — `bin_setcap` with only-flag-no-path argv is safe (no panic).
    #[test]
    fn bin_setcap_only_spec_no_path_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let _ = bin_setcap("setcap", &["cap_net_admin+ep".into()], &ops, 0);
    }

    /// c:62 — `bin_cap` with very long spec arg doesn't panic.
    #[test]
    fn bin_cap_long_spec_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        let long_spec = "cap_".to_string() + &"x".repeat(1000) + "+ep";
        let _ = bin_cap("cap", &[long_spec], &ops, 0);
    }

    /// c:274 — `setup_(null)` returns i32 (compile-time pin).
    #[test]
    fn cap_setup_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: i32 = setup_(std::ptr::null());
    }

    /// c:281 — `features_(null, &mut Vec)` returns i32 + populates Vec safely.
    #[test]
    fn cap_features_returns_i32_and_populates_vec() {
        let _g = crate::test_util::global_state_lock();
        let mut v: Vec<String> = Vec::new();
        let _: i32 = features_(std::ptr::null(), &mut v);
    }

    /// c:289 — `enables_` returns i32 with None enables-out param safe.
    #[test]
    fn cap_enables_with_none_returns_i32() {
        let _g = crate::test_util::global_state_lock();
        let mut e: Option<Vec<i32>> = None;
        let _: i32 = enables_(std::ptr::null(), &mut e);
    }

    /// c:274 — `setup_` idempotent.
    #[test]
    fn cap_setup_idempotent() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..10 {
            assert_eq!(setup_(std::ptr::null()), 0);
        }
    }

    /// c:62/147/205 — bin_cap/getcap/setcap exit codes match
    /// "0 = ok, nonzero = err" convention (no negative panics).
    #[test]
    fn bin_cap_builtins_no_negative_exit_codes() {
        let _g = crate::test_util::global_state_lock();
        let ops = empty_ops();
        for r in [
            bin_cap("cap", &[], &ops, 0),
            bin_getcap("getcap", &[], &ops, 0),
            bin_setcap("setcap", &[], &ops, 0),
        ] {
            assert!(r >= 0, "exit code must be non-negative, got {}", r);
        }
    }

    /// c:289 — `enables_` deterministic for null callback (Linux/no-Linux).
    #[test]
    fn cap_enables_deterministic_for_null_callback() {
        let _g = crate::test_util::global_state_lock();
        let mut a: Option<Vec<i32>> = None;
        let first = enables_(std::ptr::null(), &mut a);
        for _ in 0..3 {
            let mut b: Option<Vec<i32>> = None;
            assert_eq!(
                enables_(std::ptr::null(), &mut b),
                first,
                "enables_ must be deterministic for null in"
            );
        }
    }

    // ═══════════════════════════════════════════════════════════════════
    // Additional C-parity pins for Src/Modules/cap.c
    // c:36 bin_cap / c:68 bin_getcap / c:91 bin_setcap /
    // c:274-310 lifecycle hooks
    // ═══════════════════════════════════════════════════════════════════

    /// c:303 — `cleanup_` is idempotent.
    #[test]
    fn cap_cleanup_idempotent_repeated_calls() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..10 {
            assert_eq!(cleanup_(std::ptr::null()), 0);
        }
    }

    /// c:303 — `cleanup_` return type i32 (compile-time pin).
    #[test]
    fn cap_cleanup_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: i32 = cleanup_(std::ptr::null());
    }

    /// c:310 — `finish_` return type i32 (compile-time pin).
    #[test]
    fn cap_finish_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: i32 = finish_(std::ptr::null());
    }

    /// c:296 — `boot_` return type i32 (compile-time pin).
    #[test]
    fn cap_boot_returns_i32_type() {
        let _g = crate::test_util::global_state_lock();
        let _: i32 = boot_(std::ptr::null());
    }

    /// c:36 — `bin_cap` empty args non-negative.
    #[test]
    fn bin_cap_empty_args_non_negative() {
        let _g = crate::test_util::global_state_lock();
        let ops = crate::ported::zsh_h::options {
            ind: [0u8; crate::ported::zsh_h::MAX_OPS],
            args: Vec::new(),
            argscount: 0,
            argsalloc: 0,
        };
        let r = bin_cap("cap", &[], &ops, 0);
        assert!(r >= 0, "bin_cap empty args must return ≥ 0, got {}", r);
    }

    /// c:68 — `bin_getcap("")` empty path doesn't panic.
    #[test]
    fn bin_getcap_empty_path_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = crate::ported::zsh_h::options {
            ind: [0u8; crate::ported::zsh_h::MAX_OPS],
            args: Vec::new(),
            argscount: 0,
            argsalloc: 0,
        };
        let _ = bin_getcap("getcap", &[String::new()], &ops, 0);
    }

    /// c:91 — `bin_setcap` various func values don't panic.
    #[test]
    fn bin_setcap_various_func_values_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = crate::ported::zsh_h::options {
            ind: [0u8; crate::ported::zsh_h::MAX_OPS],
            args: Vec::new(),
            argscount: 0,
            argsalloc: 0,
        };
        for func in [-1, 0, 1, 100, i32::MAX] {
            let _ = bin_setcap("setcap", &[], &ops, func);
        }
    }

    /// c:36 — `bin_cap` very long arg list doesn't panic.
    #[test]
    fn bin_cap_long_arg_list_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let ops = crate::ported::zsh_h::options {
            ind: [0u8; crate::ported::zsh_h::MAX_OPS],
            args: Vec::new(),
            argscount: 0,
            argsalloc: 0,
        };
        let args: Vec<String> = (0..50).map(|i| format!("arg{}", i)).collect();
        let _ = bin_cap("cap", &args, &ops, 0);
    }

    /// c:68 — `bin_getcap` deterministic for nonexistent path.
    #[test]
    fn bin_getcap_deterministic_nonexistent_path() {
        let _g = crate::test_util::global_state_lock();
        let ops = crate::ported::zsh_h::options {
            ind: [0u8; crate::ported::zsh_h::MAX_OPS],
            args: Vec::new(),
            argscount: 0,
            argsalloc: 0,
        };
        let args = vec!["/__never_exists_xyz__".to_string()];
        let r1 = bin_getcap("getcap", &args, &ops, 0);
        let r2 = bin_getcap("getcap", &args, &ops, 0);
        assert_eq!(r1, r2, "bin_getcap nonexistent path must be deterministic");
    }

    /// c:289 — `enables_` with Some(non-empty) safe.
    #[test]
    fn cap_enables_with_some_non_empty_no_panic() {
        let _g = crate::test_util::global_state_lock();
        let mut e: Option<Vec<i32>> = Some(vec![1, 2, 3]);
        let _ = enables_(std::ptr::null(), &mut e);
    }

    /// c:281 — `features_` is deterministic across repeated calls.
    #[test]
    fn cap_features_deterministic_on_null_module() {
        let _g = crate::test_util::global_state_lock();
        let null = std::ptr::null();
        let mut v1: Vec<String> = Vec::new();
        let mut v2: Vec<String> = Vec::new();
        assert_eq!(features_(null, &mut v1), 0);
        assert_eq!(features_(null, &mut v2), 0);
        assert_eq!(
            v1, v2,
            "features_ must populate identical vec for identical input"
        );
    }

    /// c:310 — `finish_` is idempotent.
    #[test]
    fn cap_finish_idempotent_returns_zero() {
        let _g = crate::test_util::global_state_lock();
        for _ in 0..10 {
            assert_eq!(finish_(std::ptr::null()), 0);
        }
    }
}