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
//! Langinfo module — port of `Src/Modules/langinfo.c`.
//!
//! C source has zero `struct ...` / `enum ...` definitions. Rust
//! port matches: zero types. Three substantive functions
//! (`liitem`, `getlanginfo`, `scanlanginfo`) plus the 6 module
//! loaders.
//!
//! Provides the `${langinfo[NAME]}` magic-assoc backed by libc
//! `nl_langinfo(3)`.

/// `nl_names[]` — port of the static name-array at `langinfo.c:65`.
/// Each entry pairs with the parallel `nl_vals[]` array of `nl_item`
/// integer keys. Used by `liitem()` for name→item lookup and by
/// `scanlanginfo()` to enumerate every entry.
use std::ffi::CStr;

/// Port of `liitem(const char *name)` from `Src/Modules/langinfo.c:379`. Walks the
/// parallel `nl_names[]` / `nl_vals[]` arrays looking for `name`;
/// returns the nl_item integer when found, None otherwise.
///
/// C signature: `static nl_item *liitem(const char *name)`.
/// Rust port collapses C's pointer return to `Option<libc::nl_item>`
/// — the C call sites only need the integer value, never write
/// through the pointer.
/// Parallel `(nl_names[], nl_vals[])` arrays from
/// `langinfo.c:65,235` — paired here to keep liitem's body a
/// faithful loop-over-arrays match for C.
#[cfg(unix)]
static NL_TABLE: &[(&str, libc::nl_item)] = &[                           // c:65,235
    ("CODESET",     libc::CODESET),     ("D_T_FMT",     libc::D_T_FMT),
    ("D_FMT",       libc::D_FMT),       ("T_FMT",       libc::T_FMT),
    ("RADIXCHAR",   libc::RADIXCHAR),   ("THOUSEP",     libc::THOUSEP),
    ("YESEXPR",     libc::YESEXPR),     ("NOEXPR",      libc::NOEXPR),
    #[cfg(target_os = "linux")]
    ("CRNCYSTR",    libc::CRNCYSTR),
    ("ABDAY_1",     libc::ABDAY_1),     ("ABDAY_2",     libc::ABDAY_2),
    ("ABDAY_3",     libc::ABDAY_3),     ("ABDAY_4",     libc::ABDAY_4),
    ("ABDAY_5",     libc::ABDAY_5),     ("ABDAY_6",     libc::ABDAY_6),
    ("ABDAY_7",     libc::ABDAY_7),
    ("DAY_1",       libc::DAY_1),       ("DAY_2",       libc::DAY_2),
    ("DAY_3",       libc::DAY_3),       ("DAY_4",       libc::DAY_4),
    ("DAY_5",       libc::DAY_5),       ("DAY_6",       libc::DAY_6),
    ("DAY_7",       libc::DAY_7),
    ("ABMON_1",     libc::ABMON_1),     ("ABMON_2",     libc::ABMON_2),
    ("ABMON_3",     libc::ABMON_3),     ("ABMON_4",     libc::ABMON_4),
    ("ABMON_5",     libc::ABMON_5),     ("ABMON_6",     libc::ABMON_6),
    ("ABMON_7",     libc::ABMON_7),     ("ABMON_8",     libc::ABMON_8),
    ("ABMON_9",     libc::ABMON_9),     ("ABMON_10",    libc::ABMON_10),
    ("ABMON_11",    libc::ABMON_11),    ("ABMON_12",    libc::ABMON_12),
    ("MON_1",       libc::MON_1),       ("MON_2",       libc::MON_2),
    ("MON_3",       libc::MON_3),       ("MON_4",       libc::MON_4),
    ("MON_5",       libc::MON_5),       ("MON_6",       libc::MON_6),
    ("MON_7",       libc::MON_7),       ("MON_8",       libc::MON_8),
    ("MON_9",       libc::MON_9),       ("MON_10",      libc::MON_10),
    ("MON_11",      libc::MON_11),      ("MON_12",      libc::MON_12),
    ("T_FMT_AMPM",  libc::T_FMT_AMPM),  ("AM_STR",      libc::AM_STR),
    ("PM_STR",      libc::PM_STR),      ("ERA",         libc::ERA),
    ("ERA_D_FMT",   libc::ERA_D_FMT),   ("ERA_D_T_FMT", libc::ERA_D_T_FMT),
    ("ERA_T_FMT",   libc::ERA_T_FMT),   ("ALT_DIGITS",  libc::ALT_DIGITS),
];

#[cfg(unix)]
pub fn liitem(name: &str) -> Option<libc::nl_item> {                     // c:379
    NL_TABLE.iter().find(|(n, _)| *n == name).map(|(_, v)| *v)           // c:386 strcmp
}

/// Port of `liitem(const char *name)` from `Src/Modules/langinfo.c:379`.
/// Non-Unix fallback for `liitem` — `nl_item` is POSIX-only.
#[cfg(not(unix))]
#[allow(unused_variables)]
pub fn liitem(name: &str) -> Option<i32> {                                  // c:379
    None
}

/// Port of `getlanginfo(UNUSED(HashTable ht), const char *name)` from `Src/Modules/langinfo.c:396`. The
/// magic-assoc lookup callback for `${langinfo[NAME]}`. Looks up
/// `name` via `liitem`, runs `nl_langinfo(*elem)`, and returns
/// the resulting locale string (or `None` for unset).
///
/// C signature: `static HashNode getlanginfo(HashTable ht,
///                                            const char *name)`.
/// Rust port returns `Option<String>` matching the observable
/// "u.str + PM_UNSET" duality C builds into the Param node.
#[cfg(unix)]
/// WARNING: param names don't match C — Rust=(name) vs C=(ht, name)
pub fn getlanginfo(name: &str) -> Option<String> {                       // c:396
    // c:403-404 — `nameu = dupstring(name); unmetafy(nameu, &len);`
    let mut buf = name.as_bytes().to_vec();                              // c:403
    crate::ported::utils::unmetafy(&mut buf);                            // c:404
    let nameu = std::str::from_utf8(&buf).ok()?;
    // c:411-415 — `if (name) elem = liitem(name); else elem = NULL;`
    let elem = liitem(nameu)?;                                           // c:412
    unsafe {
        // c:416 — `listr = nl_langinfo(*elem)`.
        let ptr = libc::nl_langinfo(elem);                               // c:416
        if ptr.is_null() {
            return None;                                                 // c:421 PM_UNSET
        }
        let s = CStr::from_ptr(ptr).to_string_lossy().into_owned();
        if s.is_empty() {
            // c:421 — empty result also flags PM_UNSET.
            return None;
        }
        Some(s)                                                          // c:417 dupstring (no metafy — C uses dupstring not metafy here)
    }
}

/// Port of `getlanginfo(UNUSED(HashTable ht), const char *name)` from `Src/Modules/langinfo.c:396`.
/// Non-Unix fallback for `getlanginfo` — `nl_langinfo(3)` is
/// POSIX-only.
#[cfg(not(unix))]
/// WARNING: param names don't match C — Rust=(_name) vs C=(ht, name)
pub fn getlanginfo(_name: &str) -> Option<String> {                          // c:396
    None
}

/// Port of `scanlanginfo(UNUSED(HashTable ht), ScanFunc func, int flags)` from `Src/Modules/langinfo.c:430`. The
/// magic-assoc scan callback for `${(k)langinfo}` /
/// `${(kv)langinfo}`. Walks the `nl_names[]` array, calls
/// `nl_langinfo` for each entry, and yields every (name, value)
/// pair where the value is non-NULL.
///
/// C signature: `static void scanlanginfo(HashTable ht, ScanFunc
///                                         func, int flags)`.
/// Rust port returns the (name, value) pairs as a Vec since the
/// callback-driven C API doesn't translate cleanly.
/// WARNING: param names don't match C — Rust=() vs C=(ht, func, flags)
pub fn scanlanginfo() -> Vec<(String, String)> {                         // c:430
    let mut out = Vec::new();
    for &name in NL_NAMES {                                              // c:444 walk nl_names
        if let Some(v) = getlanginfo(name) {                             // c:446 nl_langinfo
            out.push((name.to_string(), v));                             // c:451 emit
        }
    }
    out
}

// `partab` — port of `static struct paramdef partab[]` (langinfo.c:455).


// `module_features` — port of `static struct features module_features`
// from langinfo.c:464.



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

// =====================================================================
// static struct paramdef partab[]                                   c:455
// static struct features module_features                            c:464
// =====================================================================

use crate::ported::zsh_h::module;

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

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

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

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

/// Port of `finish_(UNUSED(Module m))` from `Src/Modules/langinfo.c:508`.
#[allow(unused_variables)]
pub fn finish_(m: *const module) -> i32 {                               // c:508
    0                                                                    // c:508
}
pub static NL_NAMES: &[&str] = &[                                         // c:65 nl_names
    "CODESET", "D_T_FMT", "D_FMT", "T_FMT",
    "RADIXCHAR", "THOUSEP", "YESEXPR", "NOEXPR", "CRNCYSTR",
    "ABDAY_1", "ABDAY_2", "ABDAY_3", "ABDAY_4",
    "ABDAY_5", "ABDAY_6", "ABDAY_7",
    "DAY_1", "DAY_2", "DAY_3", "DAY_4", "DAY_5", "DAY_6", "DAY_7",
    "ABMON_1", "ABMON_2", "ABMON_3", "ABMON_4", "ABMON_5", "ABMON_6",
    "ABMON_7", "ABMON_8", "ABMON_9", "ABMON_10", "ABMON_11", "ABMON_12",
    "MON_1", "MON_2", "MON_3", "MON_4", "MON_5", "MON_6",
    "MON_7", "MON_8", "MON_9", "MON_10", "MON_11", "MON_12",
    "T_FMT_AMPM", "AM_STR", "PM_STR",
    "ERA", "ERA_D_FMT", "ERA_D_T_FMT", "ERA_T_FMT", "ALT_DIGITS",
];



use crate::ported::zsh_h::features as features_t;
use std::sync::{Mutex, OnceLock};

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 LANGINFO.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!["p:langinfo".to_string()]
}

// WARNING: NOT IN LANGINFO.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 LANGINFO.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 LANGINFO.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: 0,
        cd_list: None,
        cd_size: 0,
        mf_list: None,
        mf_size: 0,
        pd_list: None,
        pd_size: 1,
        n_abstract: 0,
    }))
}

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

    #[test]
    fn nl_names_includes_codeset() {
        assert!(NL_NAMES.contains(&"CODESET"));
        assert!(NL_NAMES.contains(&"D_T_FMT"));
    }

    #[cfg(unix)]
    #[test]
    fn getlanginfo_codeset_is_some() {
        assert!(getlanginfo("CODESET").is_some());
    }

    #[test]
    fn getlanginfo_invalid_returns_none() {
        assert!(getlanginfo("INVALID_NAME").is_none());
    }

    #[cfg(unix)]
    #[test]
    fn liitem_codeset_resolves() {
        assert!(liitem("CODESET").is_some());
        assert!(liitem("DOES_NOT_EXIST").is_none());
    }

    #[cfg(unix)]
    #[test]
    fn scanlanginfo_emits_items() {
        let v = scanlanginfo();
        assert!(!v.is_empty());
        assert!(v.iter().any(|(k, _)| k == "CODESET"));
    }

    /// c:65 — `nl_names` is the authoritative table of every POSIX
    /// nl_item name. Verify the set includes every category the
    /// langinfo(3) spec lists, not just CODESET. A regression that
    /// truncates the table would silently break `$(getlanginfo D_FMT)`
    /// and similar lookups in user scripts.
    #[test]
    fn nl_names_covers_canonical_locale_items() {
        for required in [
            "CODESET", "D_T_FMT", "D_FMT", "T_FMT", "T_FMT_AMPM",
            "AM_STR", "PM_STR", "DAY_1", "DAY_7", "ABDAY_1", "MON_1",
            "MON_12", "RADIXCHAR", "THOUSEP", "YESEXPR", "NOEXPR",
        ] {
            assert!(NL_NAMES.contains(&required),
                "NL_NAMES missing {} — port table truncated?", required);
        }
    }

    /// c:65 — every entry in NL_NAMES must be a valid nl_item name
    /// per langinfo.h: ALL_CAPS_WITH_UNDERSCORES, no leading digit.
    /// Pinning the shape catches a regression that adds spaces or
    /// lowercase entries (which would silently fail `getlanginfo` on
    /// the user-facing builtin path).
    #[test]
    fn nl_names_entries_are_uppercase_identifiers() {
        for &n in NL_NAMES {
            assert!(!n.is_empty(), "empty entry in NL_NAMES");
            assert!(n.chars().all(|c| c.is_ascii_uppercase() || c.is_ascii_digit() || c == '_'),
                "NL_NAMES entry {:?} contains non-uppercase chars", n);
            assert!(!n.starts_with(|c: char| c.is_ascii_digit()),
                "NL_NAMES entry {:?} starts with a digit", n);
        }
    }

    /// c:65 — NL_NAMES must not have duplicate entries. The C source
    /// preserves the LC_* category groupings; an accidental duplicate
    /// would silently double-emit in `scanlanginfo`.
    #[test]
    fn nl_names_has_no_duplicates() {
        let unique: std::collections::HashSet<_> = NL_NAMES.iter().copied().collect();
        assert_eq!(unique.len(), NL_NAMES.len(),
            "duplicate entry in NL_NAMES");
    }

    /// c:430 — `scanlanginfo` keys must be a subset of NL_NAMES.
    /// (The C source walks `nl_names` in order.) Anything extra
    /// would indicate a parallel hardcoded list drifted out of sync
    /// with the canonical table.
    #[cfg(unix)]
    #[test]
    fn scanlanginfo_keys_are_subset_of_nl_names() {
        for (k, _) in scanlanginfo() {
            assert!(NL_NAMES.contains(&k.as_str()),
                "scanlanginfo emitted {:?} which is not in NL_NAMES", k);
        }
    }

    /// c:396 — `getlanginfo` is case-sensitive: lowercase input must
    /// not match the uppercase canonical name. Catches a regression
    /// that adds a `.to_uppercase()` for "convenience".
    #[cfg(unix)]
    #[test]
    fn getlanginfo_is_case_sensitive() {
        assert!(getlanginfo("CODESET").is_some());
        assert!(getlanginfo("codeset").is_none(),
            "getlanginfo must be case-sensitive per the C source's strcmp lookup");
    }

    /// c:472-510 — module-lifecycle stubs all return 0 in C.
    #[test]
    fn module_lifecycle_shims_all_return_zero() {
        let m: *const crate::ported::zsh_h::module = std::ptr::null();
        assert_eq!(setup_(m), 0);
        assert_eq!(boot_(m), 0);
        assert_eq!(cleanup_(m), 0);
        assert_eq!(finish_(m), 0);
    }
}