zshrs 0.12.59

The first JIT-compiled Unix shell — bytecode VM, Cranelift JIT, 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
//! Per-shell output formats for builtins whose listing differs between
//! the shells zshrs stands in for.
//!
//! **zshrs-only — no zsh C counterpart.** `Src/builtin.c` prints one
//! format, zsh's. A drop-in has to print what ITS shell prints, because
//! these strings are parsed by scripts (`export -p` is specified to be
//! re-inputtable) and read by people (`times`, `kill -l`).
//!
//! Every format here was captured from the reference binary in an empty
//! environment, and is re-checked against it by the parity harness.

use crate::extensions::emulation_startup::{personality, Personality};

/// How a shell renders one `times` field.
///
/// Captured with `<shell> -c times`:
///
/// | shell | output |
/// |-------|--------|
/// | zsh 5.9 | `0m0.00s 0m0.00s` |
/// | bash 5.3 | `0m0.008s 0m0.011s` |
/// | ksh93u+m | `0m00.000s 0m00.000s` |
/// | mksh | `0m00.00s 0m00.00s` |
/// | dash | `0m0.000000s 0m0.000000s` |
pub fn times_field(ticks: i64, clktck: i64) -> String {
    let clktck = if clktck <= 0 { 100 } else { clktck };
    let mins = ticks / (60 * clktck);
    let p = personality();
    // zsh computes the seconds field as `X/clktck % clktck`
    // (c:Src/builtin.c:7315-7318) — modulo the CLOCK TICK, not 60 — so a
    // shell that has burned 125s of CPU prints `2m25.00s`. Every other
    // shell here takes `% 60` and prints `2m5s`. The zsh arm keeps the
    // upstream arithmetic verbatim; the drop-ins take their own.
    let secs = if p == Personality::Zsh {
        (ticks / clktck) % clktck
    } else {
        (ticks / clktck) % 60
    };
    match p {
        // bash: three decimals (milliseconds).
        Personality::Bash => {
            let ms = (ticks * 1000 / clktck) % 1000;
            format!("{mins}m{secs}.{ms:03}s")
        }
        // dash: six decimals (microseconds).
        Personality::Dash => {
            let us = (ticks * 1_000_000 / clktck) % 1_000_000;
            format!("{mins}m{secs}.{us:06}s")
        }
        // ksh93u+m: milliseconds AND a zero-padded seconds field. (The
        // legacy 93u+ 2012 line printed a labelled `user\t0m0.00s`
        // instead; zshrs follows the maintained fork, as it does for the
        // default alias set.)
        Personality::Ksh93 => {
            let ms = (ticks * 1000 / clktck) % 1000;
            format!("{mins}m{secs:02}.{ms:03}s")
        }
        // mksh: centiseconds, but the SECONDS field is zero-padded to two.
        Personality::Mksh | Personality::Pdksh => {
            let cs = (ticks * 100 / clktck) % 100;
            format!("{mins}m{secs:02}.{cs:02}s")
        }
        // zsh, ksh93 and the POSIX shells: centiseconds, unpadded seconds.
        // c:Src/builtin.c:7315-7318.
        _ => {
            let cs = (ticks * 100 / clktck) % 100;
            format!("{mins}m{secs}.{cs:02}s")
        }
    }
}

/// `kill -l`, rendered the way this personality's shell renders it.
///
/// Returns `None` for native zsh, whose own single-line listing stays in
/// `Src/jobs.c`'s port. Captured from each reference in an empty
/// environment; the SIGNAL SET itself comes from the running system, not
/// a table, because names and numbers differ between macOS and Linux
/// (macOS has `EMT`/`INFO`, Linux has `PWR`/`STKFLT`).
///
/// | shell | shape |
/// |-------|-------|
/// | bash 5.3 | `%2d) SIG%s`, five per row, tab-separated |
/// | ksh93u+m | one bare name per line |
/// | dash | signal `0` first, then one bare name per line |
/// | mksh | two columns of `%2d %6s %s` with the strsignal text |
pub fn kill_list(sigs: &[(i32, String)]) -> Option<String> {
    let mut out = String::new();
    match personality() {
        Personality::Zsh => return None,
        // bash: `%2d) SIG%s` five to a row, tab after every entry
        // including the last on a row, so the final row ends in a tab.
        Personality::Bash => {
            for (i, (num, name)) in sigs.iter().enumerate() {
                out.push_str(&format!("{num:2}) SIG{name}"));
                if i % 5 == 4 {
                    out.push('\n');
                } else {
                    out.push('\t');
                }
            }
            if !sigs.len().is_multiple_of(5) {
                out.push('\n');
            }
        }
        // dash lists signal 0 as well, before the real signals.
        Personality::Dash => {
            out.push_str("0\n");
            for (_, name) in sigs {
                out.push_str(name);
                out.push('\n');
            }
        }
        // ksh93u+m: one bare name per line, no number and no `SIG`. It
        // also keeps the historical `IOT` spelling for signal 6, where
        // every other shell here says `ABRT` — the only name that differs
        // across the whole list.
        Personality::Ksh93 | Personality::Sh | Personality::Csh => {
            let ksh = personality() == Personality::Ksh93;
            for (num, name) in sigs {
                if ksh && *num == 6 {
                    out.push_str("IOT");
                } else {
                    out.push_str(name);
                }
                out.push('\n');
            }
        }
        // mksh: two side-by-side columns, the left half then the right,
        // each `%2d %6s %s` with the system's signal description. The
        // split point is the midpoint, rounded up.
        Personality::Mksh | Personality::Pdksh => {
            let half = sigs.len().div_ceil(2);
            let left_width = 40usize;
            for i in 0..half {
                let (num, name) = &sigs[i];
                let cell = format!("{num:2} {name:>6} {}", signal_description(*num));
                match sigs.get(i + half) {
                    Some((rnum, rname)) => out.push_str(&format!(
                        "{cell:<left_width$}{rnum:2} {rname:>6} {}\n",
                        signal_description(*rnum)
                    )),
                    None => {
                        out.push_str(cell.trim_end());
                        out.push('\n');
                    }
                }
            }
        }
    }
    Some(out)
}

/// The system's description for a signal — mksh prints `strsignal(3)`'s
/// text ("Hangup", "Broken pipe"), which is locale- and platform-defined
/// rather than something zshrs can tabulate.
fn signal_description(num: i32) -> String {
    // SAFETY: `strsignal` returns a pointer to a static, NUL-terminated
    // string for any int; it is not thread-safe against a concurrent
    // `setlocale`, which the shell does not do while listing signals.
    let p = unsafe { libc::strsignal(num) };
    if p.is_null() {
        return String::new();
    }
    let text = unsafe { std::ffi::CStr::from_ptr(p) }
        .to_string_lossy()
        .into_owned();
    // macOS's strsignal appends the number ("Hangup: 1"); glibc's does
    // not, and neither does mksh's own listing. Strip a trailing
    // `": <digits>"` so both platforms render mksh's text.
    match text.rsplit_once(": ") {
        Some((head, tail)) if !tail.is_empty() && tail.bytes().all(|b| b.is_ascii_digit()) => {
            head.to_string()
        }
        _ => text,
    }
}

/// The header line a shell prints before its `hash` listing, if any.
///
/// Only bash has one: `hits\tcommand`, then `%7d\t%s` per entry. ksh,
/// mksh and zsh print `name=path` with no header; dash prints the bare
/// path.
pub fn hash_header() -> Option<&'static str> {
    match personality() {
        Personality::Bash => Some("hits\tcommand"),
        _ => None,
    }
}

/// One `hash` listing row, or `None` to use zsh's `name=path` form.
///
/// bash's first column is a per-command hit counter. zshrs keeps no such
/// counter, so it reports 0 — correct for a freshly hashed entry (which
/// is what `hash NAME; hash` shows) and low by however many times the
/// command has since run. The alternative, omitting the column, would
/// break the format for every script that reads it.
pub fn hash_entry(name: &str, path: &str) -> Option<String> {
    match personality() {
        Personality::Bash => Some(format!("{:>4}\t{path}", 0)),
        // dash prints the resolved path alone, no name and no `=`.
        Personality::Dash => Some(path.to_string()),
        _ => {
            let _ = name;
            None
        }
    }
}

/// One row of a shell's `set -o` listing.
///
/// `zsh` names the zshrs option that carries the state, `invert` flips it
/// (the Korn shells list the POSITIVE sense — `clobber on` where zsh has
/// `noclobber` off), and `default_on` is used for names no zsh option
/// backs, taken from the reference shell's own fresh-shell output.
struct SetOpt {
    name: &'static str,
    zsh: Option<&'static str>,
    invert: bool,
    default_on: bool,
}

const fn o(name: &'static str, zsh: &'static str) -> SetOpt {
    SetOpt {
        name,
        zsh: Some(zsh),
        invert: false,
        default_on: false,
    }
}
const fn inv(name: &'static str, zsh: &'static str) -> SetOpt {
    SetOpt {
        name,
        zsh: Some(zsh),
        invert: true,
        default_on: false,
    }
}
const fn fixed(name: &'static str, default_on: bool) -> SetOpt {
    SetOpt {
        name,
        zsh: None,
        invert: false,
        default_on,
    }
}

/// dash's `set -o`, in dash's own (non-alphabetical) order.
const DASH_SET_O: &[SetOpt] = &[
    o("errexit", "errexit"),
    o("noglob", "noglob"),
    o("ignoreeof", "ignoreeof"),
    o("interactive", "interactive"),
    o("monitor", "monitor"),
    o("noexec", "noexec"),
    o("stdin", "stdin"),
    o("xtrace", "xtrace"),
    o("verbose", "verbose"),
    o("vi", "vi"),
    o("emacs", "emacs"),
    o("noclobber", "noclobber"),
    o("allexport", "allexport"),
    o("notify", "notify"),
    o("nounset", "nounset"),
    o("nolog", "nolog"),
    fixed("debug", false),
];

/// ksh93u+m's `set -o`, alphabetical. Note the POSITIVE spellings —
/// `clobber`, `glob`, `exec`, `log`, `unset` are the inverses of zsh's
/// `noclobber`, `noglob`, `noexec`, `nolog`, `nounset`.
const KSH93_SET_O: &[SetOpt] = &[
    o("allexport", "allexport"),
    fixed("backslashctrl", true),
    o("bgnice", "bgnice"),
    o("braceexpand", "braceexpand"),
    inv("clobber", "noclobber"),
    o("emacs", "emacs"),
    o("errexit", "errexit"),
    inv("exec", "noexec"),
    fixed("functrace", false),
    inv("glob", "noglob"),
    fixed("globcasedetect", false),
    fixed("globstar", false),
    fixed("gmacs", false),
    fixed("histexpand", false),
    fixed("histreedit", false),
    fixed("histverify", false),
    o("ignoreeof", "ignoreeof"),
    o("interactive", "interactive"),
    fixed("keyword", false),
    fixed("letoctal", false),
    inv("log", "nolog"),
    o("login_shell", "loginshell"),
    o("markdirs", "markdirs"),
    o("monitor", "monitor"),
    fixed("multiline", true),
    o("notify", "notify"),
    o("pipefail", "pipefail"),
    fixed("posix", false),
    o("privileged", "privileged"),
    fixed("rc", false),
    o("restricted", "restricted"),
    fixed("showme", false),
    o("trackall", "trackall"),
    inv("unset", "nounset"),
    o("verbose", "verbose"),
    o("vi", "vi"),
    fixed("viraw", true),
    o("xtrace", "xtrace"),
];

/// mksh's `set -o`, alphabetical, printed in four COLUMN-MAJOR columns.
const MKSH_SET_O: &[SetOpt] = &[
    o("allexport", "allexport"),
    o("bgnice", "bgnice"),
    o("braceexpand", "braceexpand"),
    fixed("emacs", true),
    o("errexit", "errexit"),
    fixed("gmacs", false),
    o("ignoreeof", "ignoreeof"),
    fixed("inherit-xtrace", true),
    o("interactive", "interactive"),
    fixed("keyword", false),
    o("login", "login"),
    o("markdirs", "markdirs"),
    o("monitor", "monitor"),
    o("noclobber", "noclobber"),
    o("noexec", "noexec"),
    o("noglob", "noglob"),
    fixed("nohup", true),
    o("nolog", "nolog"),
    o("notify", "notify"),
    o("nounset", "nounset"),
    o("physical", "physical"),
    o("pipefail", "pipefail"),
    fixed("posix", false),
    o("privileged", "privileged"),
    o("restricted", "restricted"),
    fixed("sh", false),
    o("stdin", "stdin"),
    fixed("trackall", true),
    fixed("utf8-mode", false),
    o("verbose", "verbose"),
    o("vi", "vi"),
    fixed("vi-esccomplete", false),
    fixed("vi-tabcomplete", true),
    fixed("viraw", false),
    o("xtrace", "xtrace"),
];

impl SetOpt {
    fn state(&self) -> bool {
        match self.zsh {
            Some(name) => {
                let on = crate::ported::options::opt_state_get(name).unwrap_or(self.default_on);
                if self.invert {
                    !on
                } else {
                    on
                }
            }
            None => self.default_on,
        }
    }
}

/// True when an ERROR in a POSIX special builtin must abort a
/// non-interactive shell, as POSIX requires ("If there is an error in a
/// special built-in utility ... the shell shall abort").
///
/// Measured with `{ CMD; } >/dev/null 2>&1; echo AFTER`: ksh93u+m, mksh
/// and dash all abort for `readonly -X`, `set -X` and `shift 99`; bash
/// and zsh never do (bash only under `set -o posix`). The boundary is an
/// ERROR, not a non-zero status — `eval false`, `trap "" BOGUSSIG` and
/// `unset novar` all continue in every one of them.
pub fn special_builtin_error_aborts() -> bool {
    matches!(
        personality(),
        Personality::Ksh93
            | Personality::Mksh
            | Personality::Pdksh
            | Personality::Dash
            | Personality::Sh
    )
}

/// The status the shell exits with when [`special_builtin_error_aborts`]
/// fires. `usage_error` distinguishes a bad option or operand from an
/// otherwise-failing special builtin, because ksh93 uses 2 for the first
/// and the builtin's own status for the second (`readonly -X` exits 2,
/// `shift 99` exits 1). dash uses 2 for both; mksh uses 1.
pub fn special_builtin_abort_status(usage_error: bool, builtin_status: i32) -> i32 {
    match personality() {
        Personality::Dash | Personality::Sh => 2,
        Personality::Mksh | Personality::Pdksh => 1,
        Personality::Ksh93 if usage_error => 2,
        _ => builtin_status,
    }
}

/// True for the shells that ALWAYS single-quote a value in their
/// `alias`, `export -p` and `readonly -p` listings.
///
/// dash does (`alias yy=1` lists as `yy='1'`, `export FOO=bar` as
/// `export FOO='bar'`); ksh93, mksh and zsh quote only when the value
/// needs it, and bash has its own `alias NAME='v'` / `declare -x`
/// spellings handled separately.
pub fn alias_always_quotes() -> bool {
    matches!(personality(), Personality::Dash | Personality::Sh)
}

/// `set -o` rendered the way this personality's shell renders it, or
/// `None` for zsh and bash (whose listings are handled by their own
/// existing paths).
///
/// All three Bourne/Korn shells head the listing with
/// "Current option settings"; they differ in column width and, for mksh,
/// in laying the names out in four COLUMN-MAJOR columns.
pub fn set_o_listing() -> Option<String> {
    set_o_render(false)
}

/// `set +o` — the REUSABLE form, one `set -o NAME` / `set +o NAME` per
/// line. Implemented for the Bourne legs, whose shape is a
/// straightforward inversion of the listing above.
///
/// ksh93u+m and mksh instead emit a single compact line of long options
/// naming only the NON-DEFAULT settings (`set --default --braceexpand
/// --multiline --trackall --viraw`, `set -o .reset -o braceexpand`); that
/// form is not modelled, so those legs keep zsh's output.
pub fn set_plus_o_listing() -> Option<String> {
    match personality() {
        Personality::Dash | Personality::Sh => set_o_render(true),
        // ksh93u+m and mksh emit ONE line: a reset word followed by the
        // options that are on and are NOT part of the shell's built-in
        // default set — `set --default --braceexpand --multiline
        // --trackall --viraw`, `set -o .reset -o braceexpand`. Adding
        // `set -e` adds `--errexit` / `-o errexit` to each, which is how
        // the default sets below were derived.
        Personality::Ksh93 => Some(compact_set_plus_o(
            KSH93_SET_O,
            KSH93_DEFAULT_ON,
            "set --default",
            "--",
        )),
        Personality::Mksh | Personality::Pdksh => Some(compact_set_plus_o(
            MKSH_SET_O,
            MKSH_DEFAULT_ON,
            "set -o .reset",
            "-o ",
        )),
        _ => None,
    }
}

/// Options ksh93u+m already has on after `set --default`, so `set +o`
/// never re-lists them. Derived by diffing a fresh shell's `set -o`
/// (on: backslashctrl braceexpand clobber exec glob log multiline
/// trackall unset viraw) against its `set +o` (which names only
/// braceexpand, multiline, trackall, viraw).
const KSH93_DEFAULT_ON: &[&str] = &["backslashctrl", "clobber", "exec", "glob", "log", "unset"];

/// The same for mksh, derived the same way: a fresh shell lists only
/// `braceexpand` after `-o .reset`, so everything else that is on is part
/// of the reset state.
const MKSH_DEFAULT_ON: &[&str] = &[
    "emacs",
    "inherit-xtrace",
    "nohup",
    "trackall",
    "vi-tabcomplete",
];

/// Render the Korn shells' one-line `set +o`: a reset word, then each
/// non-default option that is currently on, in table (alphabetical) order.
fn compact_set_plus_o(table: &[SetOpt], defaults: &[&str], reset: &str, prefix: &str) -> String {
    let mut out = String::from(reset);
    for e in table {
        if e.state() && !defaults.contains(&e.name) {
            out.push(' ');
            out.push_str(prefix);
            out.push_str(e.name);
        }
    }
    out.push('\n');
    out
}

fn set_o_render(reusable: bool) -> Option<String> {
    let (table, width) = match personality() {
        Personality::Dash | Personality::Sh => (DASH_SET_O, 15usize),
        Personality::Ksh93 => (KSH93_SET_O, 24),
        Personality::Mksh | Personality::Pdksh => (MKSH_SET_O, 14),
        _ => return None,
    };
    let on = |b: bool| if b { "on" } else { "off" };
    if reusable {
        // `set -o NAME` when the option is on, `set +o NAME` when off —
        // the form the shell can read back in.
        let mut out = String::new();
        for e in table {
            out.push_str(&format!(
                "set {}o {}\n",
                if e.state() { "-" } else { "+" },
                e.name
            ));
        }
        return Some(out);
    }
    let mut out = String::from("Current option settings\n");
    if matches!(personality(), Personality::Mksh | Personality::Pdksh) {
        // Four columns, filled top-to-bottom then left-to-right, with the
        // row count taken from the first column.
        let rows = table.len().div_ceil(4);
        for r in 0..rows {
            let mut line = String::new();
            for c in 0..4 {
                let Some(e) = table.get(c * rows + r) else {
                    continue;
                };
                // mksh pads the state to five columns, not four: `off` is
                // followed by two spaces before the next name.
                line.push_str(&format!("{:<width$} {:<5}", e.name, on(e.state())));
            }
            out.push_str(line.trim_end());
            out.push('\n');
        }
        return Some(out);
    }
    for e in table {
        out.push_str(&format!("{:<width$} {}\n", e.name, on(e.state())));
    }
    Some(out)
}

// Unit tests for the per-shell formats live in
// tests/builtin_output_parity.rs, not here: asserting them means moving
// the process-global personality, and the library test binary runs its
// tests in parallel threads that share it — doing it inline made unrelated
// compsys / zle / hist tests fail depending on scheduling.