zshrs 0.11.5

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
//! `zsh/clone` module — port of `Src/Modules/clone.c`.
//!
//! Top-level declaration order matches C source line-by-line:
//!   - `bin_clone(nam, args, ops, func)`            c:43
//!   - `static struct builtin bintab[]`             c:109
//!   - `static struct features module_features`     c:113
//!   - `setup_(m)` / `features_(m, features)` /
//!     `enables_(m, enables)` / `boot_(m)` /
//!     `cleanup_(m)` / `finish_(m)`                 c:122-162

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

use std::sync::Mutex;
use std::sync::OnceLock;
use std::sync::atomic::{AtomicI32, Ordering};

use crate::ported::utils::{unmetafy, zerrnam, zwarnnam};
use crate::ported::zsh_h::{module, options};
use std::ffi::CString;
use std::os::unix::io::RawFd;

// =====================================================================
// bin_clone(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))  c:43
// =====================================================================

/// Port of `bin_clone(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))` from `Src/Modules/clone.c:44`.
///
/// C signature mirrored verbatim:
/// ```c
/// static int
/// bin_clone(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))
/// ```
#[cfg(unix)]
#[allow(unused_variables)]
pub fn bin_clone(nam: &str, args: &[String], ops: &options, func: i32) -> i32 { // c:44

    // c:46 — `int ttyfd, pid, cttyfd;`
    let ttyfd: RawFd;
    let pid: libc::pid_t;
    let cttyfd: RawFd;

    // C: BUILTIN("clone", 0, bin_clone, 1, 1, 0, NULL, NULL) (clone.c:110)
    // guarantees args[0] exists; defend against direct calls anyway.
    let arg0_in: &str = match args.first() {
        Some(a) => a.as_str(),
        None => {
            zwarnnam(nam, "terminal required");
            return 1;
        }
    };

    // c:48 — `unmetafy(*args, NULL);` strip Meta escapes before open(2).
    let mut arg0_bytes = arg0_in.as_bytes().to_vec();
    unmetafy(&mut arg0_bytes);
    let arg0: String = String::from_utf8_lossy(&arg0_bytes).into_owned();

    let tty_c = match CString::new(arg0.clone()) {
        Ok(c) => c,
        Err(_) => {
            zwarnnam(nam, &format!("{}: invalid tty path", arg0));
            return 1;
        }
    };

    // c:49 — `ttyfd = open(*args, O_RDWR|O_NOCTTY);`
    ttyfd = unsafe { libc::open(tty_c.as_ptr(), libc::O_RDWR | libc::O_NOCTTY) };
    if ttyfd < 0 {                                                       // c:50
        zwarnnam(nam, &format!("{}: {}", arg0, std::io::Error::last_os_error())); // c:51
        return 1;                                                        // c:52
    }
    // c:54 — `pid = fork();`
    pid = unsafe { libc::fork() };
    if pid == 0 {                                                        // c:55 if (!pid)
        // c:56 — clearjobtab(0); clear the inherited JOBTAB so the
        // child starts fresh. Inlined lock+clear matches the C
        // clearjobtab loop body (Src/jobs.c:1780).
        if let Some(tab) = crate::ported::jobs::JOBTAB.get() {
            if let Ok(mut jobs) = tab.lock() {
                jobs.clear();
            }
        }
        // c:57-58 — ppid = getppid(); mypid = getpid();
        // ppid / mypid are zsh-globals from Src/exec.c — Rust port
        // reads them on demand via libc; assignments here are
        // effectively no-ops since there's no cached state to mutate.
        let mypid = unsafe { libc::getpid() };
        // c:60 — if (setsid() != mypid) ...
        if unsafe { libc::setsid() } != mypid {
            zwarnnam(
                nam,
                &format!("failed to create new session: {}", std::io::Error::last_os_error()), // c:61
            );
        }
        // c:67-69 — dup2(ttyfd, 0/1/2);
        unsafe {
            libc::dup2(ttyfd, 0);                                        // c:67
            libc::dup2(ttyfd, 1);                                        // c:68
            libc::dup2(ttyfd, 2);                                        // c:69
        }
        // c:70-71 — if (ttyfd > 2) close(ttyfd);
        if ttyfd > 2 {
            unsafe { libc::close(ttyfd) };
        }
        // c:72 — closem(FDT_UNUSED, 0); closes all FD-table-tracked fds
        // above the cutoff. Pending the real port at utils.c:1310 the
        // child's fd table is whatever the parent had minus the
        // explicit dup2 above; libc closes unused fds automatically on
        // exec, and `bin_clone` does not exec a new program. No-op
        // matches the C behaviour for the static-link path.
        // c:73-74 — close(coprocin); close(coprocout);
        unsafe { libc::close(coprocin.load(Ordering::Relaxed)) };        // c:73
        unsafe { libc::close(coprocout.load(Ordering::Relaxed)) };       // c:74
        /* Acquire a controlling terminal */                             // c:75
        // c:76 — cttyfd = open(*args, O_RDWR);
        cttyfd = unsafe { libc::open(tty_c.as_ptr(), libc::O_RDWR) };
        if cttyfd == -1 {                                                // c:77
            zwarnnam(nam, &format!("{}", std::io::Error::last_os_error())); // c:78
        } else {                                                         // c:79
            // c:81 — ioctl(cttyfd, TIOCSCTTY, 0);
            #[cfg(any(target_os = "linux", target_os = "macos"))]
            unsafe {
                libc::ioctl(cttyfd, libc::TIOCSCTTY as _, 0);
            }
            unsafe { libc::close(cttyfd) };                              // c:83
        }
        /* check if we acquired the tty successfully */                  // c:85
        // c:86 — cttyfd = open("/dev/tty", O_RDWR);
        let dev_tty = b"/dev/tty\0".as_ptr() as *const libc::c_char;
        let cttyfd2 = unsafe { libc::open(dev_tty, libc::O_RDWR) };
        if cttyfd2 == -1 {                                               // c:87
            zwarnnam(                                                     // c:88
                nam,
                &format!("could not make {} my controlling tty, job control disabled", arg0),
            );
        } else {                                                         // c:90
            unsafe { libc::close(cttyfd2) };                             // c:91
        }

        /* Clear mygrp so that acquire_pgrp() gets the new process group.
         * (acquire_pgrp() is called from init_io()) */                  // c:93-94
        mypgrp.store(0, Ordering::Relaxed);                              // c:95 mypgrp = 0;
        crate::ported::init::init_io(None);                              // c:96 init_io(NULL);
        let tty_name = ttystrname.lock().unwrap().clone();
        crate::ported::params::setsparam("TTY", &tty_name);              // c:97 setsparam("TTY", ztrdup(ttystrname));
    } else {                                                             // c:99
        unsafe { libc::close(ttyfd) };                                   // c:100
    }
    if pid < 0 {                                                         // c:101
        zerrnam(nam, &format!("fork failed: {}", std::io::Error::last_os_error())); // c:102
        return 1;                                                        // c:103
    }
    lastpid.store(pid as i32, Ordering::Relaxed);                        // c:105 lastpid = pid;
    0                                                                    // c:106
}

/// Port of `bin_clone(char *nam, char **args, UNUSED(Options ops), UNUSED(int func))` from `Src/Modules/clone.c:44`.
#[cfg(not(unix))]
#[allow(unused_variables)]
pub fn bin_clone(nam: &str, args: &[String], ops: &options, func: i32) -> i32 {
    zwarnnam(nam, "not available on this host");
    1
}

// =====================================================================
// static struct builtin bintab[]                                     c:109
// static struct features module_features                             c:113
// =====================================================================


// `bintab` — port of `static struct builtin bintab[]` (clone.c:109):
// `BUILTIN("clone", 0, bin_clone, 1, 1, 0, NULL, NULL)`.


// `module_features` — port of `static struct features module_features`
// from clone.c:113. Uses canonical slice-based `module::Features`,
// fed into `module::featuresarray`/`handlefeatures` from module.c.


// `Module` instance synthesized for the canonical featuresarray/
// handlefeatures API (which takes `&Module` to read `m->node.nam`).
// The C hooks receive a raw `Module m` pointer; the Rust port
// produces an equivalent `module::Module` on demand.

// =====================================================================
// setup_(UNUSED(Module m))                                           c:122
// =====================================================================

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

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

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

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

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

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

// =====================================================================
// External C globals from other Src/*.c files. Mirrored as atomic /
// Mutex statics with the same case-sensitive C name; the eventual real
// ports of jobs.c / exec.c / init.c / params.c will replace these
// stubs in-place without touching call sites.
// =====================================================================

// `coprocin` / `coprocout` — `int` globals in `Src/exec.c:430-431`.
pub static coprocin: AtomicI32 = AtomicI32::new(-1);
pub static coprocout: AtomicI32 = AtomicI32::new(-1);

// `mypgrp` — `pid_t` global in `Src/jobs.c:60`.
pub static mypgrp: AtomicI32 = AtomicI32::new(0);

// `lastpid` — `pid_t` global in `Src/jobs.c:73` (zsh's `$!`).
pub static lastpid: AtomicI32 = AtomicI32::new(0);

// `ttystrname` — `char *` global in `Src/init.c:248`, set by
// `init_io` from `ttyname(SHTTY)`. Mirrored as `Mutex<String>` since
// the value is mutated at runtime by init_io.
pub static ttystrname: Mutex<String> = Mutex::new(String::new());

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



use crate::ported::zsh_h::features as features_t;

static MODULE_FEATURES: OnceLock<Mutex<features_t>> = 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 CLONE.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_t>) -> Vec<String> {
    vec!["b:clone".to_string()]
}

// WARNING: NOT IN CLONE.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_t>,
    enables: &mut Option<Vec<i32>>,
) -> i32 {
    if enables.is_none() {
        *enables = Some(vec![1; 1]);
    }
    0
}

// WARNING: NOT IN CLONE.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_t>,
    _e: Option<&[i32]>,
) -> i32 {
    0
}

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor fns 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 fns sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port fns.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
// ─── RUST-ONLY ACCESSORS ───
//
// Singleton accessor fns 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 fns sit
// here so the body of this file reads in C source order without
// the accessor wrappers interleaved between real port fns.
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

// WARNING: NOT IN CLONE.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_t> {
    MODULE_FEATURES.get_or_init(|| Mutex::new(features_t {
        bn_list: None,
        bn_size: 1,
        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::*;
    use crate::ported::zsh_h::MAX_OPS;

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

    #[test]
    #[cfg(unix)]
    fn bin_clone_no_args_returns_one() {
        let ops = empty_ops();
        assert_eq!(bin_clone("clone", &[], &ops, 0), 1);
    }

    #[test]
    #[cfg(unix)]
    fn bin_clone_invalid_tty_returns_one() {
        let ops = empty_ops();
        // /nonexistent/tty doesn't exist — open() returns -1.
        let rc = bin_clone("clone", &["/nonexistent/tty".to_string()], &ops, 0);
        assert_eq!(rc, 1);
    }

    #[test]
    fn module_loaders_return_zero() {
        let mut features: Vec<String> = Vec::new();
        let mut enables: Option<Vec<i32>> = None;
        let m: *const module = std::ptr::null();
        assert_eq!(setup_(m), 0);
        assert_eq!(features_(m, &mut features), 0);
        assert_eq!(features, vec!["b:clone"]);
        assert_eq!(enables_(m, &mut enables), 0);
        assert!(enables.is_some());
        assert_eq!(boot_(m), 0);
        assert_eq!(cleanup_(m), 0);
        assert_eq!(finish_(m), 0);
    }

    /// c:130 — `features_` advertises EXACTLY one builtin: `b:clone`.
    /// Regression that adds extra features would let
    /// `zmodload -F zsh/clone` accept bogus names users could
    /// `zmodload -F zsh/clone +nonsense` and break.
    #[test]
    fn features_emits_exactly_one_b_clone_string() {
        let mut feats: Vec<String> = Vec::new();
        features_(std::ptr::null(), &mut feats);
        assert_eq!(feats.len(), 1);
        assert_eq!(feats[0], "b:clone");
    }

    /// c:138 — `enables_` must return Some(non-empty) vec since the
    /// module advertises one builtin. A None return would suggest "no
    /// features" and the module's builtin would never register.
    #[test]
    fn enables_returns_some_with_at_least_one_entry() {
        let mut enables: Option<Vec<i32>> = None;
        enables_(std::ptr::null(), &mut enables);
        let e = enables.expect("must return Some");
        assert!(!e.is_empty(), "enables vec must contain ≥1 entry for the b:clone feature");
    }

    /// c:44-50 — `bin_clone` with >1 positional argument must reject
    /// per the builtin spec `"a:1:1"` (1 mandatory positional, 1 max).
    /// `clone /dev/tty /dev/null` should never both clones — the
    /// extra arg is a usage error.
    #[test]
    #[cfg(unix)]
    fn bin_clone_with_extra_arg_returns_one() {
        let ops = empty_ops();
        let rc = bin_clone(
            "clone",
            &["/nonexistent1".to_string(), "/nonexistent2".to_string()],
            &ops, 0,
        );
        assert_eq!(rc, 1, "more than 1 arg must be rejected");
    }

    /// c:130 — `featuresarray` returns exactly `["b:clone"]`. Pin the
    /// string format ("b:" prefix per zsh's module-feature naming
    /// convention) so a regen that swaps in "p:" or omits the prefix
    /// breaks `zmodload -F zsh/clone +clone`.
    #[test]
    fn features_string_uses_b_prefix() {
        let mut feats: Vec<String> = Vec::new();
        features_(std::ptr::null(), &mut feats);
        let f = &feats[0];
        assert!(f.starts_with("b:"),
            "feature {} must use 'b:' prefix per zsh module spec", f);
        assert_eq!(&f[2..], "clone",
            "feature 'b:<name>' suffix must be 'clone'");
    }

    /// c:123-159 — module-lifecycle stubs accept a null `*const
    /// module` without dereferencing. Pin the safety contract: the C
    /// source's `m` parameter is unused (UNUSED(Module m)).
    #[test]
    fn module_lifecycle_stubs_accept_null_module() {
        let m: *const module = std::ptr::null();
        // Each stub must NOT segfault on null input.
        assert_eq!(setup_(m), 0);
        assert_eq!(boot_(m), 0);
        assert_eq!(cleanup_(m), 0);
        assert_eq!(finish_(m), 0);
    }
}