zshrs 0.12.23

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
//! Port of `_arg_compile` from
//! `Completion/Base/Utility/_arg_compile` (zsh 5.9, 199 lines).
//!
//! A simple compiler for `_arguments` descriptions. The first argument
//! is the NAME of an array parameter into which the compiled parse is
//! written (upstream uses `${(P)}` / `eval $safe[reply]'=(…)'`). The
//! remaining arguments form a series of `phrases`, each beginning with
//! one of the keywords `argument`, `option`, or `help`.
//!
//! Upstream flow this port mirrors 1:1:
//! ```text
//! sh: 92  local -h argspec dspec helpspec prelude xor
//! sh: 93  local -h -A amap dmap safe
//! sh: 95  [[ -n "$1" ]] || return 1
//! sh: 96  [[ ${(tP)${1}} = *-local ]] && { NAME CONFLICT; return 1 }
//! sh: 97  safe[reply]="$1"; shift
//! sh:104-110  consume the prelude (anything before the first phrase)
//! sh:114-193  consume argument/option/help phrases, building argspec/helpspec
//! sh:195  eval $safe[reply]'=( prelude argspec [-- helpspec] "$@" )'
//! sh:199  return 0
//! ```

use crate::ported::params::{paramtab, setaparam};
use crate::ported::modules::parameter::paramtypestr;
use std::collections::HashMap;
use std::collections::VecDeque;

/// Successive `${2:s/join/-/:s/close/-/…}` HOW rewrite (sh:148).
/// Each `:s/A/B/` replaces the FIRST occurrence of `A` with `B`, in
/// order: join→`-`, close→`-`, next→``, split→``, loose→`+`,
/// assign→`=`, none→``.
fn rewrite_follow(how: &str) -> String {
    let mut s = how.to_string();
    for (from, to) in [
        ("join", "-"),
        ("close", "-"),
        ("next", ""),
        ("split", ""),
        ("loose", "+"),
        ("assign", "="),
        ("none", ""),
    ] {
        s = s.replacen(from, to, 1);
    }
    s
}

/// sh:126 — POS is `<1->` (an integer ≥ 1) or `*`.
fn is_position(s: &str) -> bool {
    if s == "*" {
        return true;
    }
    // `<1->` matches the numeric VALUE ≥ 1 (so "0" does not match).
    !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit()) && s.parse::<u64>().map_or(false, |n| n >= 1)
}

/// `_arg_compile` — compile arg-specs into the caller-named array.
pub fn _arg_compile(args: &[String]) -> i32 {
    // sh:95 — `[[ -n "$1" ]] || return 1`.
    let reply = match args.first() {
        Some(r) if !r.is_empty() => r.clone(),
        _ => return 1,
    };

    // sh:96 — `[[ ${(tP)${1}} = *-local ]] && { NAME CONFLICT; return 1 }`.
    // `${(tP)${1}}` is the TYPE string of the param NAMED by $1; a
    // `*-local` type means the caller already declared it local and we
    // must not clobber it.
    {
        let tab = paramtab().read().unwrap();
        if let Some(pm) = tab.get(&reply) {
            if paramtypestr(pm).ends_with("-local") {
                eprintln!("NAME CONFLICT: {}", reply);
                return 1;
            }
        }
    }

    // sh:97 — `safe[reply]="$1"; shift`.
    let mut rest: VecDeque<String> = args[1..].iter().cloned().collect();

    // sh:92-93 locals.
    let mut argspec: Vec<String> = Vec::new();
    let mut helpspec: Vec<String> = Vec::new(); // sh:101
    let mut prelude: Vec<String> = Vec::new(); // sh:102

    // sh:104-110 — consume and save anything before the argument phrases.
    while let Some(head) = rest.front() {
        match head.as_str() {
            "argument" | "help" | "option" => break, // sh:107
            _ => {
                // sh:108 — prelude+=($1); shift
                prelude.push(rest.pop_front().unwrap());
            }
        }
    }

    // sh:114-193 — consume all phrases and build argspec/helpspec.
    while let Some(head) = rest.front().cloned() {
        // sh:116-117 — amap=(); dspec=()
        let mut amap: HashMap<String, String> = HashMap::new();
        let mut dspec: Vec<String> = Vec::new();

        match head.as_str() {
            // sh:121 — argument [POS] [means MSG] [action ACT]
            "argument" => {
                rest.pop_front(); // sh:122 shift
                while let Some(k) = rest.front().cloned() {
                    if is_position(&k) {
                        // sh:126 — amap[position]="$1"; shift
                        amap.insert("position".to_string(), k);
                        rest.pop_front();
                    } else if k == "means" || k == "action" {
                        // sh:127 — amap[$1]="$2"; shift 2
                        rest.pop_front();
                        let v = rest.pop_front().unwrap_or_default();
                        amap.insert(k, v);
                    } else if k == "argument" || k == "option" || k == "help" {
                        break; // sh:128
                    } else {
                        // sh:129
                        eprintln!("SYNTAX ERROR at {}", vecdeque_join(&rest));
                        return 1;
                    }
                }
                // sh:132-135
                if !amap.is_empty() {
                    argspec.push(format!(
                        "{}:{}:{}",
                        amap.get("position").cloned().unwrap_or_default(),
                        amap.get("means").cloned().unwrap_or_default(),
                        amap.get("action").cloned().unwrap_or_default(),
                    ));
                }
            }

            // sh:139 — option OPT [follow HOW] [explain STR] {unless XOR}
            //          {[through PAT] [means MSG] [action ACT]}
            "option" => {
                // sh:140 — amap[option]="$2"; shift 2
                rest.pop_front(); // "option"
                let opt = rest.pop_front().unwrap_or_default();
                amap.insert("option".to_string(), opt);
                let mut dmap: HashMap<String, String> = HashMap::new(); // sh:141
                let mut xor: Vec<String> = Vec::new(); // sh:142
                'opt_outer: while let Some(k) = rest.front().cloned() {
                    // sh:145 — (( ${+amap[$1]} || ${+dmap[through]} )) && break
                    if amap.contains_key(&k) || dmap.contains_key("through") {
                        break;
                    }
                    match k.as_str() {
                        "follow" => {
                            // sh:147-149
                            rest.pop_front();
                            let v = rest.pop_front().unwrap_or_default();
                            amap.insert("follow".to_string(), rewrite_follow(&v));
                        }
                        "explain" => {
                            // sh:150 — amap[explain]="[$2]"; shift 2
                            rest.pop_front();
                            let v = rest.pop_front().unwrap_or_default();
                            amap.insert("explain".to_string(), format!("[{}]", v));
                        }
                        "unless" => {
                            // sh:151 — xor+=("${(@)=2}"); shift 2
                            rest.pop_front();
                            let v = rest.pop_front().unwrap_or_default();
                            xor.extend(v.split_whitespace().map(|s| s.to_string()));
                        }
                        "through" | "means" | "action" => {
                            // sh:152-161 — inner loop collecting dmap entries
                            while let Some(k2) = rest.front().cloned() {
                                // sh:155 — (( ${+dmap[$1]} )) && break 2
                                // A repeated through/means/action key ends the
                                // WHOLE option phrase and, crucially, SKIPS the
                                // sh:165 dspec append for this iteration (the C
                                // `break 2` jumps past it).
                                if dmap.contains_key(&k2) {
                                    break 'opt_outer;
                                }
                                match k2.as_str() {
                                    "through" | "means" | "action" => {
                                        // sh:157 — dmap[$1]=":${2}"; shift 2
                                        rest.pop_front();
                                        let v = rest.pop_front().unwrap_or_default();
                                        dmap.insert(k2, format!(":{}", v));
                                    }
                                    "argument" | "option" | "help" | "follow" | "explain"
                                    | "unless" => {
                                        break; // sh:158 (break 1 — inner only)
                                    }
                                    _ => {
                                        // sh:159
                                        eprintln!("SYNTAX ERROR at {}", vecdeque_join(&rest));
                                        return 1;
                                    }
                                }
                            }
                        }
                        "argument" | "option" | "help" => break, // sh:162
                        _ => {
                            // sh:163
                            eprintln!("SYNTAX ERROR at {}", vecdeque_join(&rest));
                            return 1;
                        }
                    }
                    // sh:165-168 — if (( $#dmap )) dspec+=(through means|: action|:)
                    if !dmap.is_empty() {
                        dspec.push(format!(
                            "{}{}{}",
                            dmap.get("through").cloned().unwrap_or_default(),
                            // sh:167 — ${dmap[means]:-:} / ${dmap[action]:-:}
                            non_empty_or_colon(dmap.get("means")),
                            non_empty_or_colon(dmap.get("action")),
                        ));
                    }
                }
                // sh:170-173
                if !amap.is_empty() {
                    // sh:172 — "${xor:+($xor)}${amap[option]}${amap[follow]}
                    //           ${amap[explain]}${dspec}"
                    let xor_prefix = if xor.is_empty() {
                        String::new()
                    } else {
                        format!("({})", xor.join(" "))
                    };
                    argspec.push(format!(
                        "{}{}{}{}{}",
                        xor_prefix,
                        amap.get("option").cloned().unwrap_or_default(),
                        amap.get("follow").cloned().unwrap_or_default(),
                        amap.get("explain").cloned().unwrap_or_default(),
                        // `${dspec}` in double quotes joins the array with a space.
                        dspec.join(" "),
                    ));
                }
            }

            // sh:176 — help PAT [means MSG] action ACT
            "help" => {
                // sh:177 — amap[pattern]="$2"; shift 2
                rest.pop_front(); // "help"
                let pat = rest.pop_front().unwrap_or_default();
                amap.insert("pattern".to_string(), pat);
                while let Some(k) = rest.front().cloned() {
                    // sh:180 — (( ${+amap[$1]} )) && break
                    if amap.contains_key(&k) {
                        break;
                    }
                    match k.as_str() {
                        "means" | "action" => {
                            // sh:182 — amap[$1]="$2"; shift 2
                            rest.pop_front();
                            let v = rest.pop_front().unwrap_or_default();
                            amap.insert(k, v);
                        }
                        "argument" | "option" | "help" => break, // sh:183
                        _ => {
                            // sh:184
                            eprintln!("SYNTAX ERROR at {}", vecdeque_join(&rest));
                            return 1;
                        }
                    }
                }
                // sh:187-190
                if !amap.is_empty() {
                    helpspec.push(format!(
                        "{}:{}:{}",
                        amap.get("pattern").cloned().unwrap_or_default(),
                        amap.get("means").cloned().unwrap_or_default(),
                        amap.get("action").cloned().unwrap_or_default(),
                    ));
                }
            }

            // sh:191 — (*) break
            _ => break,
        }
    }

    // sh:195 — eval $safe[reply]'=( "${prelude[@]}" "${argspec[@]}"
    //           ${helpspec:+"-- ${helpspec[@]}"} "$@" )'
    let mut result: Vec<String> = Vec::new();
    result.extend(prelude);
    result.extend(argspec);
    if !helpspec.is_empty() {
        // `"-- ${helpspec[@]}"` glues the `-- ` prefix to the FIRST
        // helpspec element (zsh joins adjacent literal text to the
        // boundary element of an `[@]` expansion inside double quotes).
        let mut first = String::from("-- ");
        first.push_str(&helpspec[0]);
        result.push(first);
        result.extend(helpspec[1..].iter().cloned());
    }
    // Remaining `"$@"` (anything after the last phrase) passes through.
    result.extend(rest.into_iter());

    setaparam(&reply, result);

    0 // sh:199
}

/// sh:167 — `${dmap[key]:-:}` — the value if present and non-empty,
/// else a bare colon.
fn non_empty_or_colon(v: Option<&String>) -> String {
    match v {
        Some(s) if !s.is_empty() => s.clone(),
        _ => ":".to_string(),
    }
}

/// Render the remaining args for a `SYNTAX ERROR at "$@"` diagnostic.
fn vecdeque_join(rest: &VecDeque<String>) -> String {
    rest.iter().cloned().collect::<Vec<_>>().join(" ")
}

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

    fn compile(spec: &[&str]) -> Vec<String> {
        let args: Vec<String> = spec.iter().map(|s| s.to_string()).collect();
        let rc = _arg_compile(&args);
        assert_eq!(rc, 0, "compile should succeed");
        crate::ported::params::getaparam(spec[0]).unwrap_or_default()
    }

    #[test]
    fn empty_name_returns_one() {
        let _g = crate::test_util::global_state_lock();
        assert_eq!(_arg_compile(&[]), 1);
        assert_eq!(_arg_compile(&["".to_string()]), 1);
    }

    #[test]
    fn prelude_passes_through() {
        let _g = crate::test_util::global_state_lock();
        let out = compile(&["args", "-s", "-v"]);
        assert_eq!(out, vec!["-s", "-v"]);
    }

    #[test]
    fn argument_phrase_builds_pos_means_action() {
        let _g = crate::test_util::global_state_lock();
        // argument 1 means "file" action _files
        let out = compile(&[
            "args", "argument", "1", "means", "file", "action", "_files",
        ]);
        assert_eq!(out, vec!["1:file:_files"]);
    }

    #[test]
    fn option_phrase_with_follow_and_explain() {
        let _g = crate::test_util::global_state_lock();
        // option -d follow close means "debug level"
        // follow close => "-"; dspec entry = through("")+means(":debug level")
        //   +action(":") => ":debug level:"; argspec = "-d" + "-" + ":debug level:"
        let out = compile(&[
            "args", "option", "-d", "follow", "close", "means", "debug level",
        ]);
        assert_eq!(out, vec!["-d-:debug level:"]);
    }

    #[test]
    fn option_with_explain_and_unless_xor() {
        let _g = crate::test_util::global_state_lock();
        // option -a explain foo unless -b
        // -> "(-b)-a[foo]"
        let out = compile(&[
            "args", "option", "-a", "explain", "foo", "unless", "-b",
        ]);
        assert_eq!(out, vec!["(-b)-a[foo]"]);
    }

    #[test]
    fn help_phrase_and_trailing_marker() {
        let _g = crate::test_util::global_state_lock();
        // help '*=name*' means "function name" action '->funcs'
        let out = compile(&[
            "args",
            "help",
            "*=name*",
            "means",
            "function name",
            "action",
            "->funcs",
        ]);
        // helpspec => "*=name*:function name:->funcs", prefixed by "-- "
        assert_eq!(out, vec!["-- *=name*:function name:->funcs"]);
    }

    #[test]
    fn mixed_argument_then_help_phrase() {
        let _g = crate::test_util::global_state_lock();
        // argument means "profile file" action _files
        // help *=dirs* action _dir_list
        let out = compile(&[
            "args",
            "argument",
            "means",
            "profile file",
            "action",
            "_files",
            "help",
            "*=dirs*",
            "action",
            "_dir_list",
        ]);
        assert_eq!(
            out,
            vec![":profile file:_files", "-- *=dirs*::_dir_list"]
        );
    }

    #[test]
    fn syntax_error_returns_one() {
        let _g = crate::test_util::global_state_lock();
        // A bare non-keyword token inside a phrase is a syntax error
        // (sh:129/159/163/184) — here `bogus` after the argument phrase's
        // means/action slots.
        let rc = _arg_compile(&[
            "args".to_string(),
            "argument".to_string(),
            "means".to_string(),
            "m".to_string(),
            "bogus".to_string(),
        ]);
        assert_eq!(rc, 1);
    }
}