Skip to main content

zsh/ported/
exec.rs

1//! Faithful Rust ports of free functions and file-static globals from
2//! `Src/exec.c`. The wordcode-VM dispatch tree (`execlist` / `execpline`
3//! / `execcmd` / `execsimple` etc.) that drives execution in C zsh is
4//! NOT replicated here — zshrs runs the fusevm bytecode VM instead
5//! (see `src/vm_helper.rs` + `src/fusevm_bridge.rs`).
6//!
7//! What lives here are the parts of `Src/exec.c` that ARE faithful
8//! ports and don't depend on the C-side wordcode walker:
9//!
10//! - **`trap_state` / `trap_return` / `forklevel`** — file-static
11//!   integer globals from `Src/exec.c:134 / :155 / :1052`, exposed as
12//!   atomics shared between this module, `Src/signals.c`'s port at
13//!   `src/ported/signals.rs`, and `Src/params.c`'s port at
14//!   `src/ported/params.rs`.
15//! - **`gethere`** (`Src/exec.c:4573`) — turn a here-document into a
16//!   here-string. Called from the lexer port (`src/ported/lex.rs`).
17//! - **`getoutput`** (`Src/exec.c:4712`) — command-substitution body
18//!   runner. Called from the parameter-expansion port
19//!   (`src/ported/subst.rs`).
20//! - **`loadautofn`** + **`getfpfunc`** (`Src/exec.c:5050` / `:5260`)
21//!   — `$fpath` walker + autoload file installer. Called from
22//!   `bin_autoload` / `bin_functions -c` in `src/ported/builtin.rs`.
23//! - **`resolvebuiltin`** (`Src/exec.c:2703`) — module-autoload guard
24//!   used by the dispatch walk in `execcmd_exec`.
25//! - **`execcmd_compile_head`** — fusevm-bytecode-time head resolver
26//!   mirroring the head section (`c:2904-3275`) of C's `execcmd_exec`.
27//!   NOT a faithful port; the canonical 7-arg `execcmd_exec` port lives
28//!   alongside it.
29//! - **`execcmd_exec`** (`Src/exec.c:2900`) — canonical 7-arg port of
30//!   the C function (locals + dispatch walk through builtin/shfunc/external
31//!   invocation). Used by future tree-walker callers; the fusevm
32//!   bytecode flow goes through `execcmd_compile_head` instead.
33
34use std::os::unix::fs::PermissionsExt;
35use std::sync::atomic::Ordering;
36
37// `with_executor` import removed — all ShellExecutor reach-in calls
38// routed through `crate::ported::exec::*` fn-ptrs installed by
39// fusevm_bridge at startup. See memory feedback_no_exec_script_from_ported.
40use crate::ported::builtin::{cd_able_vars, fixdir, BUILTINS, DOPRINTDIR, EXIT_VAL, LASTVAL};
41use crate::ported::builtins::rlimits::setlimits;
42use crate::ported::builtins::sched::zleactive;
43use crate::ported::compat::zgettime_monotonic_if_available;
44use crate::ported::config_h::DEFAULT_PATH;
45use crate::ported::context::{zcontext_restore, zcontext_save};
46use crate::ported::hashtable::{
47    cmdnam_unhashed, cmdnamtab_lock, dircache_set, hashdir, pathchecked, shfunctab_lock,
48};
49use crate::ported::hist::{strinbeg, strinend};
50use crate::ported::init::{shout, underscorelen, underscoreused, zunderscore, SHTTY};
51use crate::ported::input::{inpop, inpush};
52use crate::ported::jobs::{expandjobtab, get_usage, release_pgrp, waitforpid, JOBTAB, THISJOB};
53use crate::ported::lex::{
54    hgetc, parsestr, tok, untokenize, ztokens, LEXERR, LEX_LEXSTOP, LEX_LINENO,
55};
56use crate::ported::mem::{dupstring, dyncat, popheap, pushheap};
57use crate::ported::modules::clone::mypgrp;
58use crate::ported::options::{dosetopt, opt_state_set, sticky};
59use crate::ported::params::{
60    endparamscope, getsparam, locallevel, paramtab, setiparam, zgetenv, zputenv,
61};
62use crate::ported::parse::{closedumps, ecrawstr, parse_list};
63use crate::ported::prompt::{cmdpop, cmdpush};
64use crate::ported::signals::{
65    intrap, queue_signals, settrap, signal_mask, signal_unblock, sigtrapped, trapisfunc,
66    traplocallevel, unqueue_signals, unsettrap,
67};
68use crate::ported::signals_h::{
69    child_block, child_unblock, dont_queue_signals, signal_default, signal_ignore, winch_unblock,
70    SIGCOUNT,
71};
72use crate::ported::subst::{quotesubst, singsub};
73use crate::ported::utils::{
74    errflag, fdtable_get, fdtable_set, gettempfile, gettempname, inc_locallevel, movefd, pathprog,
75    printprompt4, quotedzputs, redup, unmeta, unmetafy, write_loop, zclose, zerr, zwarn,
76    ERRFLAG_ERROR, MAX_ZSH_FD,
77};
78use crate::ported::zsh_h::{
79    builtin, cmdnam, emulation_options, eprog, execstack, funcwrap, hashnode, isset, jobfile,
80    multio, redir, shfunc, unset, wc_code, Emulation_options, Inang, Inpar, Meta, Nularg, Outpar,
81    Pound, BINF_BUILTIN, BINF_CLEARENV, BINF_COMMAND, BINF_DASH, BINF_EXEC, BINF_PREFIX, CHASEDOTS,
82    CHASELINKS, CLOBBER, CLOBBEREMPTY, CS_CMDSUBST, ERRFLAG_INT, FDT_EXTERNAL, FDT_INTERNAL,
83    FDT_PROC_SUBST, FDT_SAVED_MASK, FDT_TYPE_MASK, FDT_UNUSED, FDT_XTRACE, HASHDIRS, INP_LINENO,
84    INTERACTIVE, IS_CLOBBER_REDIR, IS_DASH, JOBTEXTSIZE, MAX_PIPESTATS, MONITOR, MULTIOS,
85    MULTIOUNIT, PATHDIRS, PM_LOADDIR, PM_READONLY, PM_UNDEFINED, POSIXBUILTINS, POSIXJOBS,
86    POSIXTRAPS, REDIRF_FROM_HEREDOC, REDIR_CLOSE, REDIR_HEREDOCDASH, REDIR_HERESTR, REDIR_INPIPE,
87    REDIR_OUTPIPE, USEZLE, VERBOSE, WC_LIST, WC_LIST_TYPE, WC_PIPE, WC_PIPE_END, WC_PIPE_TYPE,
88    WC_REDIR, WC_REDIR_TYPE, WC_REDIR_VARID, WC_SIMPLE, WC_SIMPLE_ARGC, WC_SUBLIST, WC_SUBLIST_END,
89    WC_SUBLIST_FLAGS, WC_SUBLIST_TYPE, WC_TYPESET, ZSIG_FUNC, ZSIG_IGNORED, Z_END,
90};
91use crate::ported::zsh_system_h::timespec as ZshTimespec;
92use crate::ported::ztype_h::{inull, itok};
93use crate::zsh_h::XTRACE;
94
95/// Port of the anonymous `enum { ... }` from `Src/exec.c:35-40`.
96/// Flag bits passed as the `addflags` argument to `addvars` /
97/// `addvarsfromargs`:
98///   - `ADDVAR_EXPORT`  (1<<0) — export each assignment for the
99///                                command `VAR=val cmd ...` form.
100///   - `ADDVAR_RESTORE` (1<<2) — the variable list is being restored
101///                                later (implicit local scope), so
102///                                suppress `ASSPM_WARN`.
103pub const ADDVAR_EXPORT: i32 = 1 << 0; // c:37 (Src/exec.c)
104/// `ADDVAR_RESTORE` constant.
105pub const ADDVAR_RESTORE: i32 = 1 << 2; // c:39 (Src/exec.c)
106
107/// Port of `int trap_state;` from `Src/exec.c:134`. Tracks whether
108/// a trap handler is currently being processed and, paired with
109/// `TRAP_RETURN` below, whether a `return` inside the trap should
110/// promote to `TRAP_STATE_FORCE_RETURN` to unwind the trap caller.
111///
112/// Values: `TRAP_STATE_INACTIVE = 0`, `TRAP_STATE_PRIMED = 1`,
113/// `TRAP_STATE_FORCE_RETURN = 2` (see `Src/zsh.h`).
114pub static TRAP_STATE: std::sync::atomic::AtomicI32 = // c:134 (Src/exec.c)
115    std::sync::atomic::AtomicI32::new(0);
116
117/// Port of `int trap_return;` from `Src/exec.c:155`. Carries the
118/// pending exit status from inside a trap; sentinel `-2` means
119/// "running an EXIT/DEBUG-style trap at the current level"
120/// (signals.c:1166). Promoted to the user's `return N` value by
121/// `bin_return` when POSIX-trap semantics apply (builtin.c:5852).
122pub static TRAP_RETURN: std::sync::atomic::AtomicI32 = // c:155 (Src/exec.c)
123    std::sync::atomic::AtomicI32::new(0);
124
125/// Port of `int forklevel;` from `Src/exec.c:1052`. Records the
126/// `locallevel` at the most recent fork point (set at c:1221:
127/// `forklevel = locallevel;` inside `entersubsh()`). Used by:
128///   - `signals.c:808` SIGPIPE handler — `!forklevel` distinguishes
129///     the top-level shell from a forked subshell.
130///   - `exec.c:6146` — `if (locallevel > forklevel)` decides whether
131///     a function-defined trap should fire on this subshell exit.
132///   - `params.c:3724` — WARNCREATEGLOBAL nest-depth check.
133///
134/// Initialised to 0 (no fork has occurred yet). Set to `locallevel`
135/// at every `entersubsh()` entry per c:1221.
136pub static FORKLEVEL: std::sync::atomic::AtomicI32 = // c:1052 (Src/exec.c)
137    std::sync::atomic::AtomicI32::new(0);
138
139// =============================================================================
140// File-static globals from Src/exec.c. Bucket choices per PORT_PLAN.md:
141//   - Per-evaluator transient state → thread_local Cell (bucket 1)
142//   - Shell-wide shared state       → AtomicI32 / Mutex (bucket 2)
143// All names match C exactly. Surrounding doc-comments cite the C
144// declaration line.
145// =============================================================================
146
147/// Port of `int noerrexit;` from `Src/exec.c:72`. Bit-flags that
148/// suppress ERREXIT triggering on the next command(s). Bits:
149/// `NOERREXIT_EXIT` (in `if`/`while`/`until` test contexts),
150/// `NOERREXIT_RETURN` (after `return`), `NOERREXIT_UNTIL_EXEC`
151/// (until next exec'd command). Bucket-1 — per-evaluator (each
152/// recursive eval has its own suppression frame).
153pub static noerrexit: std::sync::atomic::AtomicI32 = // c:72 (Src/exec.c)
154    std::sync::atomic::AtomicI32::new(0);
155
156/// Port of `int this_noerrexit;` from `Src/exec.c:109`. When set,
157/// suppress ERREXIT for THIS one command only (consumed + cleared
158/// before the next command starts). Set by `execcursh` and the
159/// `((expr))` arith path so a 0-result doesn't trigger errexit.
160pub static this_noerrexit: std::sync::atomic::AtomicI32 = // c:109 (Src/exec.c)
161    std::sync::atomic::AtomicI32::new(0);
162
163/// Port of `mod_export int noerrs;` from `Src/exec.c:117`. When
164/// non-zero, suppress `zerr()` output (lex error reporting during
165/// `parse_string`, `parseopts` etc.). Saved/restored by
166/// `execsave`/`execrestore`.
167/// Port of `static char list_pipe_text[JOBTEXTSIZE]` from
168/// `Src/exec.c:463`. Holds the textual rendering of the in-flight
169/// pipe list; saved across nested execlist invocations at
170/// exec.c:1372-1380 (zeroed on entry, restored from
171/// `old_list_pipe_text` at c:1634-1638) and round-tripped through
172/// execsave/execrestore (c:6448 / c:6484). zshrs models it as a
173/// length-bounded String guarded by a Mutex — the C `char[80]` cap
174/// is a buffer-overflow guard, but matching length matters for the
175/// `jobs` builtin's pipe-list rendering.
176pub static LIST_PIPE_TEXT: std::sync::Mutex<String> = std::sync::Mutex::new(String::new()); // c:463 (Src/exec.c)
177
178pub static noerrs: std::sync::atomic::AtomicI32 = // c:117 (Src/exec.c)
179    std::sync::atomic::AtomicI32::new(0);
180
181/// Port of `int nohistsave;` from `Src/exec.c:122`. When non-zero,
182/// `addhistnode` no-ops so trap firings / `eval` invocations don't
183/// pollute `$HISTCMD`. Tracked alongside `noerrs` in the trap path.
184pub static nohistsave: std::sync::atomic::AtomicI32 = // c:122 (Src/exec.c)
185    std::sync::atomic::AtomicI32::new(0);
186
187/// Port of `int subsh;` from `Src/exec.c:160`. Subshell depth — bumped
188/// every time `entersubsh` forks a sub-shell, used by signal handling
189/// (different SIGINT semantics in subshells) and by `${$$}` (`$$`
190/// stays at the top-level pid).
191pub static subsh: std::sync::atomic::AtomicI32 = // c:160 (Src/exec.c)
192    std::sync::atomic::AtomicI32::new(0);
193
194/// Port of `mod_export int zsh_subshell;` from `Src/init.c:67`. Visible
195/// `$ZSH_SUBSHELL` parameter — incremented by `entersubsh()` each time
196/// the shell forks into a subshell (real or fake-exec). Distinct from
197/// `subsh` which records whether we ARE a subshell; `zsh_subshell` is
198/// the visible depth count.
199pub static zsh_subshell: std::sync::atomic::AtomicI32 = // c:67 (Src/init.c)
200    std::sync::atomic::AtomicI32::new(0);
201
202/// Port of `mod_export volatile int retflag;` from `Src/exec.c:165`.
203/// Set by `bin_return` to unwind the function-call stack. Cleared
204/// by `runshfunc` on entry, checked by `execlist`'s main loop.
205///
206/// Re-export alias of the canonical [`crate::ported::builtin::RETFLAG`] — C
207/// has ONE `retflag` (exec.c:165). All return-flow logic (execlist, the
208/// `return` builtin, signals) uses `RETFLAG`; this lowercase twin was never
209/// stored, so `zpty`/`system` module read-loops that check `exec::retflag`
210/// saw a permanent 0 and never aborted on a pending `return`.
211pub use crate::ported::builtin::RETFLAG as retflag; // c:165 (Src/exec.c)
212
213/// Port of `pid_t cmdoutpid;` from `Src/exec.c:215`. Pid of the most
214/// recent `$(cmd)` command-substitution child. Used by exit-status
215/// propagation: `cmdoutval` carries the exit; `cmdoutpid` carries
216/// the pid `waitpid`-d for it.
217pub static cmdoutpid: std::sync::atomic::AtomicI32 = // c:215 (Src/exec.c)
218    std::sync::atomic::AtomicI32::new(0);
219
220/// Port of `mod_export pid_t procsubstpid;` from `Src/exec.c:220`.
221/// Pid of the most recent process-substitution child (`<(cmd)` /
222/// `>(cmd)`). Tracked separately from `cmdoutpid` because procsubst
223/// jobs aren't wait-collected by the parent until the fd is closed.
224pub static procsubstpid: std::sync::atomic::AtomicI32 = // c:220 (Src/exec.c)
225    std::sync::atomic::AtomicI32::new(0);
226
227/// Port of `int cmdoutval;` from `Src/exec.c:225`. Exit status of
228/// the most recent `$(cmd)`. Drives `$?` when a varspc-only command
229/// runs alongside a substitution.
230pub static cmdoutval: std::sync::atomic::AtomicI32 = // c:225 (Src/exec.c)
231    std::sync::atomic::AtomicI32::new(0);
232
233/// Port of `int use_cmdoutval;` from `Src/exec.c:234`. When set,
234/// `lastval` is updated from `cmdoutval` after the command
235/// (i.e. the command had substitutions whose exit status matters).
236pub static use_cmdoutval: std::sync::atomic::AtomicI32 = // c:234 (Src/exec.c)
237    std::sync::atomic::AtomicI32::new(0);
238
239/// Port of `mod_export int sfcontext;` from `Src/exec.c:239`. Source
240/// context — one of `SFC_NONE`, `SFC_DIRECT` (user typed it),
241/// `SFC_SIGNAL` (trap firing), `SFC_HOOK` (precmd/preexec etc.),
242/// `SFC_WIDGET` (ZLE widget), `SFC_COMPLETE` (completion fn),
243/// `SFC_CFUNC` (compsys fn), `SFC_SUBST` ($(...) cmd-subst),
244/// `SFC_EVAL` (eval body). Read by `zerr()` / `funcstack` building.
245pub static sfcontext: std::sync::atomic::AtomicI32 = // c:239 (Src/exec.c)
246    std::sync::atomic::AtomicI32::new(0);
247
248/// Port of `int list_pipe = 0;` from `Src/exec.c:457`. Set when the
249/// currently-executing pipeline is the long-running pipe-into-loop
250/// shape (`cat foo | while read a; do ... done`) — drives the
251/// super/sub-job tracking documented in the famous `Allen Edeln…`
252/// comment block above this declaration in C.
253pub static list_pipe: std::sync::atomic::AtomicI32 = // c:457 (Src/exec.c)
254    std::sync::atomic::AtomicI32::new(0);
255
256/// Port of `int simple_pline = 0;` from `Src/exec.c:457`. Set during
257/// dispatch of a "simple" pipeline (single-stage / no shell-construct
258/// tail) so the `list_pipe` machinery short-circuits.
259pub static simple_pline: std::sync::atomic::AtomicI32 = // c:457 (Src/exec.c)
260    std::sync::atomic::AtomicI32::new(0);
261
262/// Port of `static pid_t list_pipe_pid;` from `Src/exec.c:459`.
263/// PID of the sub-shell created to host the loop-after-pipe pattern;
264/// passed up the recursive `execlist` stack so the cat-job's super-
265/// job entry can record it.
266pub static list_pipe_pid: std::sync::atomic::AtomicI32 = // c:459 (Src/exec.c)
267    std::sync::atomic::AtomicI32::new(0);
268
269/// Port of `static int nowait;` from `Src/exec.c:461`. When set,
270/// `execpline` doesn't wait for the pipeline; used during the
271/// list_pipe sub-shell fork bookkeeping.
272pub static nowait: std::sync::atomic::AtomicI32 = // c:461 (Src/exec.c)
273    std::sync::atomic::AtomicI32::new(0);
274
275/// Port of `int pline_level = 0;` from `Src/exec.c:461`. Recursive
276/// pipeline depth (counts nested pipelines within the current
277/// `execlist` call chain).
278pub static pline_level: std::sync::atomic::AtomicI32 = // c:461 (Src/exec.c)
279    std::sync::atomic::AtomicI32::new(0);
280
281/// Port of `static int list_pipe_child = 0;` from `Src/exec.c:462`.
282/// Set in the child after the list_pipe fork so the child knows to
283/// continue executing the loop body (vs the parent which records
284/// the pid + returns).
285pub static list_pipe_child: std::sync::atomic::AtomicI32 = // c:462 (Src/exec.c)
286    std::sync::atomic::AtomicI32::new(0);
287
288/// Port of `static int list_pipe_job;` from `Src/exec.c:462`. Job
289/// table index of the pipeline's first-stage job (the `cat` in
290/// `cat foo | while ...`).
291pub static list_pipe_job: std::sync::atomic::AtomicI32 = // c:462 (Src/exec.c)
292    std::sync::atomic::AtomicI32::new(0);
293
294/// Port of `static int doneps4;` from `Src/exec.c:262`. Set after
295/// `printprompt4` has emitted the `$PS4` prefix for the current
296/// xtrace command — prevents double-printing when an inner sub-eval
297/// also wants to xtrace.
298pub static doneps4: std::sync::atomic::AtomicI32 = // c:262 (Src/exec.c)
299    std::sync::atomic::AtomicI32::new(0);
300
301/// Port of `static int esprefork, esglob = 1;` from `Src/exec.c:2680`.
302///
303/// File-static "execsubst parameters" — callers (execcmd_exec at
304/// c:3298 / c:3700) set these BEFORE invoking execsubst, which then
305/// uses them as the `flags` arg to prefork() and the gate on
306/// globlist(). `esprefork` is `PREFORK_TYPESET` for magic-assign /
307/// MAGICEQUALSUBST words, else 0. `esglob` defaults to 1; cleared
308/// when the dispatched builtin has `BINF_NOGLOB`.
309pub static esprefork: std::sync::atomic::AtomicI32 = // c:2680
310    std::sync::atomic::AtomicI32::new(0);
311pub static esglob: std::sync::atomic::AtomicI32 = // c:2680 (= 1)
312    std::sync::atomic::AtomicI32::new(1);
313
314/// Port of `struct execstack *exstack;` from `Src/exec.c:244`. Head
315/// of the linked exec-context save stack — `execsave` pushes a frame
316/// before signal-handler / trap dispatch; `execrestore` pops it
317/// afterwards so the interrupted command resumes with its state intact.
318pub static exstack: std::sync::Mutex<Option<Box<execstack>>> = // c:244
319    std::sync::Mutex::new(None);
320
321/// Port of `static char *STTYval;` from `Src/exec.c:263`. Pending
322/// `stty` argument string captured by `addvars` when the command's
323/// inline env contains `STTY=...`. Applied by `execute` before fork
324/// + exec so the spawned program sees its tty configured. Reset to
325/// `None` after consumption to avoid infinite recursion.
326pub static STTYval: std::sync::Mutex<Option<String>> = // c:263 (Src/exec.c)
327    std::sync::Mutex::new(None);
328
329/// Convert a here-document into a here-string. Line-by-line port of
330/// `gethere()` from `Src/exec.c:4569-4652`. Reads the body from the
331/// input stream via `hgetc()` until the terminator line is matched,
332/// returning the collected body as a string. `strp` is in/out: on
333/// entry the raw terminator (possibly with token markers + leading
334/// tabs); on return the munged terminator (after `quotesubst` +
335/// `untokenize` and, for `REDIR_HEREDOCDASH`, leading-tab strip).
336///
337/// Returns `None` on out-of-memory (C `zalloc`/`realloc` failure).
338/// Rust's `String` auto-grows so the OOM branch is effectively
339/// unreachable, but the return type stays `Option<String>` to mirror
340/// the C signature which can return NULL.
341///
342/// Port of `gethere(char **strp, int typ)` from `Src/exec.c:4573`.
343pub fn gethere(strp: &mut String, typ: i32) -> Option<String> {
344    // c:4573 (Src/exec.c)
345    let mut buf: String; // c:4575 char *buf
346    let mut bsiz: usize; // c:4576 int bsiz
347    let mut qt: i32 = 0; // c:4576 int qt = 0
348    let mut strip: i32 = 0; // c:4576 int strip = 0
349                            // c:4577 — char *s, *t, *bptr, c. zshrs uses byte-offsets into
350                            // `buf` for `t` and tracks `bptr` implicitly as `buf.len()` (the
351                            // C `bptr++` increment is `buf.push(c)`; `bptr--` is `buf.pop()`).
352                            // `s` (the loop iterator for the inull-scan) stays local to its
353                            // for-loop. `c` mirrors the C `char c`.
354    let mut t: usize; // c:4577 char *t
355    let mut c: Option<char>; // c:4577 char c
356    let mut str: String = strp.clone(); // c:4578 char *str = *strp
357
358    // c:4580-4584 — for (s = str; *s; s++) if (inull(*s)) { qt = 1; break; }
359    for s in str.bytes() {
360        if inull(s) {
361            // c:4581
362            qt = 1; // c:4582
363            break; // c:4583
364        }
365    }
366    str = quotesubst(&str); // c:4585
367    str = untokenize(&str); // c:4586
368    if typ == REDIR_HEREDOCDASH {
369        // c:4587
370        strip = 1; // c:4588
371                   // c:4589-4590 — while (*str == '\t') str++;
372        while str.starts_with('\t') {
373            str.remove(0);
374        }
375    }
376    *strp = str.clone(); // c:4592 *strp = str
377
378    // c:4593 — bptr = buf = zalloc(bsiz = 256);
379    bsiz = 256;
380    buf = String::with_capacity(bsiz);
381    let _ = bsiz; // bsiz is tracked by C for zfree; Rust drops automatically
382
383    // c:4594 — for (;;)
384    loop {
385        t = buf.len(); // c:4595 t = bptr
386
387        // c:4597-4598 — while ((c = hgetc()) == '\t' && strip) ;
388        loop {
389            c = hgetc();
390            if !(c == Some('\t') && strip != 0) {
391                break;
392            }
393        }
394
395        // c:4599 — for (;;) — inner body-read loop
396        loop {
397            // c:4600-4613 — buffer-growth realloc dance. Rust's
398            // String auto-grows; nothing to do.
399            // c:4614 — if (lexstop || c == '\n') break;
400            if LEX_LEXSTOP.with(|f| f.get()) || c == Some('\n') || c.is_none() {
401                break;
402            }
403            // c:4616 — if (!qt && c == '\\')
404            if qt == 0 && c == Some('\\') {
405                buf.push('\\'); // c:4617 *bptr++ = c
406                c = hgetc(); // c:4618
407                if c == Some('\n') {
408                    // c:4619
409                    buf.pop(); // c:4620 bptr--
410                    c = hgetc(); // c:4621
411                    continue; // c:4622
412                }
413            }
414            if let Some(ch) = c {
415                // c:4625 *bptr++ = c
416                buf.push(ch);
417            }
418            c = hgetc(); // c:4626
419        }
420        // c:4628 — *bptr = '\0'; (implicit — Rust String tracks len)
421
422        // c:4629-4630 — if (!strcmp(t, str)) break;
423        if &buf[t..] == str.as_str() {
424            break;
425        }
426        // c:4631-4634 — if (lexstop) { t = bptr; break; }
427        // C's ingetc sets `lexstop = 1` when input is exhausted
428        // (Src/input.c:289-292), so the flag check alone suffices
429        // there. zshrs's hgetc signals the same exhaustion by
430        // returning None WITHOUT setting LEX_LEXSTOP — treat both as
431        // the c:4631 condition, else an unterminated heredoc loops
432        // here forever appending '\n'. Gap #3 2026-06-12 (A04
433        // "Here-documents don't perform shell expansion" wedge).
434        if LEX_LEXSTOP.with(|f| f.get()) || c.is_none() {
435            t = buf.len();
436            break;
437        }
438        // c:4635 — *bptr++ = '\n';
439        buf.push('\n');
440    }
441    // c:4637 — *t = '\0';
442    buf.truncate(t);
443
444    // c:4638-4640 — s = buf; buf = dupstring(buf); zfree(s, bsiz);
445    // The C dance frees the realloc'd block and re-allocates via the
446    // string-heap allocator. Rust drops the old String when reassigned.
447    buf = dupstring(&buf);
448
449    if qt == 0 {
450        // c:4641
451        // c:4642 — int ef = errflag;
452        let ef = errflag.load(Ordering::Relaxed);
453        // c:4644 — parsestr(&buf);
454        if let Ok(parsed) = parsestr(&buf) {
455            buf = parsed;
456        }
457        // c:4646-4649 — if (!(errflag & ERRFLAG_ERROR)) errflag = ef | (errflag & ERRFLAG_INT);
458        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
459            let cur = errflag.load(Ordering::Relaxed);
460            errflag.store(ef | (cur & ERRFLAG_INT), Ordering::Relaxed);
461        }
462    }
463    Some(buf) // c:4651 return buf
464}
465
466/// Port of `LinkList getoutput(char *cmd, int qt)` from
467/// `Src/exec.c:4712-4791`. Runs a command-substitution body in the
468/// active executor, then routes the captured stdout through
469/// `readoutput(pipe, qt, NULL)` semantics at c:4855-4872.
470///
471/// C return shape: `LinkList` of `char*`. Rust port returns
472/// `Vec<String>` (same shape, owned).
473///
474/// `qt` matches C exactly:
475///   - qt=1 (quoted, `"$(...)"`): trim trailing newlines, return
476///     entire output as a single-element vec. C c:4858-4862: if
477///     output empty, returns a single Nularg sentinel so callers
478///     see "empty value" rather than "no value".
479///   - qt=0 (unquoted, `$(...)`): trim trailing newlines, then
480///     `spacesplit(buf, allownull=false)` per c:4865-4871.
481///
482/// Uses `with_executor` (panics on missing VM context), not
483/// `try_with_executor + unwrap_or_default()`. C `getoutput` calls
484/// `execpline` directly — there's no "no shell" code path. The
485/// silent-no-op pattern (return empty string when no executor) would
486/// mask catastrophic state corruption as "command produced no output",
487/// which is the failure mode the `subst.rs:496` warning block flags.
488/* $(...) */
489// c:4709
490/// `getoutput` — see implementation.
491pub fn getoutput(cmd: &str, qt: i32) -> Vec<String> {
492    // c:4713
493    // c:4715 — `Eprog prog;`
494    let prog: Option<eprog>;
495    // c:4716 — `int pipes[2];`  (collapsed: in-process executor; no fork)
496    // c:4717 — `pid_t pid;`     (collapsed)
497    let mut s: String; // c:4718
498                       // c:4720-4723 — `int onc = nocomments; nocomments = (interact &&
499                       //                !sourcelevel && unset(INTERACTIVECOMMENTS));
500                       //                prog = parse_string(cmd, 0); nocomments = onc;`
501    let onc = crate::ported::lex::LEX_NOCOMMENTS.with(|c| c.get());
502    let new_nc = crate::ported::zsh_h::interact()
503        && crate::ported::init::sourcelevel.load(Ordering::Relaxed) == 0
504        && !isset(crate::ported::zsh_h::INTERACTIVECOMMENTS);
505    crate::ported::lex::LEX_NOCOMMENTS.with(|c| c.set(new_nc));
506    prog = parse_string(cmd, 0);
507    crate::ported::lex::LEX_NOCOMMENTS.with(|c| c.set(onc));
508
509    if prog.is_none() {
510        // c:4725
511        return Vec::new(); // c:4726 return NULL
512    }
513    let prog = prog.unwrap();
514
515    if !isset(crate::ported::zsh_h::EXECOPT) {
516        // c:4728
517        return Vec::new(); // c:4729 newlinklist()
518    }
519
520    // c:4731 — `if ((s = simple_redir_name(prog, REDIR_READ)))` — `$(< word)`
521    if let Some(red_name) = simple_redir_name(&prog, crate::ported::zsh_h::REDIR_READ) {
522        /* $(< word) */
523        // c:4732
524        s = red_name;
525        s = singsub(&s); // c:4737
526        if errflag.load(Ordering::Relaxed) != 0 {
527            return Vec::new(); // c:4739
528        }
529        let s = untokenize(&s); // c:4740
530        let path_meta = unmeta(&s); // c:4741 unmeta(s)
531        let cpath = match std::ffi::CString::new(path_meta.as_bytes()) {
532            Ok(c) => c,
533            Err(_) => return Vec::new(),
534        };
535        let stream = unsafe {
536            libc::open(cpath.as_ptr(), libc::O_RDONLY | libc::O_NOCTTY) // c:4741
537        };
538        if stream == -1 {
539            // c:4742 — `zwarn("%e: %s", errno, s);`
540            let errno = std::io::Error::last_os_error();
541            zerr(&format!("{}: {}", errno, s));
542            LASTVAL.store(1, Ordering::Relaxed);
543            cmdoutval.store(1, Ordering::Relaxed);
544            return Vec::new(); // c:4744
545        }
546        // c:4746 — `retval = readoutput(stream, qt, &readerror);`
547        let mut readerror: i32 = 0;
548        let retval = readoutput(stream, qt, &mut readerror); // c:4746
549        if readerror != 0 {
550            // c:4747
551            zerr(&format!(
552                "error when reading {}: {}", // c:4748
553                s,
554                std::io::Error::from_raw_os_error(readerror)
555            ));
556            LASTVAL.store(1, Ordering::Relaxed);
557            cmdoutval.store(1, Ordering::Relaxed);
558        }
559        return retval; // c:4751
560    }
561
562    // c:4753-4790 — Full fork path: mpipe + zfork + parent
563    // readoutput / waitforpid / child execode + _realexit. fusevm runs
564    // command substitution in-process, so the fork shape collapses to a
565    // synchronous executor call. C control points preserved as cites:
566    //   c:4753 mpipe       — handled by ShellExecutor pipe wiring
567    //   c:4758 child_block — no-op (no fork)
568    //   c:4760 zfork       — replaced by in-process exec
569    //   c:4768-4776 parent — equivalent to executor return
570    //   c:4778-4789 child  — entersubsh+execode+_realexit collapse
571    cmdoutval.store(0, Ordering::Relaxed); // c:4759
572    let buf = crate::ported::exec::run_command_substitution(cmd);
573    LASTVAL.store(cmdoutval.load(Ordering::Relaxed), Ordering::Relaxed); // c:4775
574
575    // c:4772 retval = readoutput — post-walk (c:4855-4871 tail) inlined.
576    let buf = buf.trim_end_matches('\n');
577    if qt != 0 {
578        if buf.is_empty() {
579            vec![String::from(Nularg)] // c:4859-4861
580        } else {
581            vec![buf.to_string()] // c:4863
582        }
583    } else {
584        crate::ported::utils::spacesplit(buf, false) // c:4865
585    }
586}
587
588/// Direct port of `Shfunc loadautofn(Shfunc shf, int ks, int test_only,
589/// int ignore_loaddir)` from `Src/exec.c:5050`. Walks `$fpath` for a
590/// file named `shf->node.nam`, reads it, installs the text body on
591/// the corresponding `shfunctab` entry, and clears `PM_UNDEFINED`.
592///
593/// C body (abridged):
594///   1. `name = shf->node.nam`
595///   2. `getfpfunc(name, &dir_path, NULL, 0)` → resolved file path
596///   3. If !test_only && file found: parse → store eprog on
597///      `shf->funcdef`; clear PM_UNDEFINED; set `shf->filename`.
598///   4. Returns shf on success, NULL on failure.
599///
600/// Rust port: returns 0 = success, 1 = failure (matches the
601/// existing call-site convention in `bin_functions -c`). Stores
602/// raw file text on `ShFunc.body` (the Rust-side ShFunc in
603/// `hashtable.rs:362`); the parser pass that converts text →
604/// Eprog runs lazily at first call site.
605/// Port of `loadautofn(Shfunc shf, int fksh, int autol, int current_fpath)` from `Src/exec.c:5682`.
606pub fn loadautofn(
607    shf: *mut shfunc, // c:5682 (Src/exec.c)
608    _ks: i32,
609    autol: i32,
610    _ignore_loaddir: i32,
611) -> i32 {
612    if shf.is_null() {
613        return 1;
614    }
615    // c:5054 — `name = shf->node.nam`.
616    let name = unsafe { (*shf).node.nam.clone() };
617    // c:5070 — `path = getfpfunc(name, &dir_path, NULL, 0)`.
618    let mut dir_path: Option<String> = None;
619    let mut dump_hit: Option<(eprog, i32)> = None;
620    // A function autoloaded by ABSOLUTE PATH (`autoload -Uz /dir/name`) — or
621    // one already resolved via `autoload -r` — caches its directory in
622    // `shf.filename` with PM_LOADDIR set. Load-on-call must search THAT dir
623    // (`shf.filename/name`), not walk `$fpath` (which won't contain it).
624    // Without this, `autoload -Uz $fdir/.hist.*` (zsh-hist) named the
625    // functions correctly but every call died with "definition file not
626    // found". Mirrors C loadautofn's PM_LOADDIR spec-path branch.
627    let loaddir_spec: Option<Vec<String>> = {
628        let s = unsafe { &*shf };
629        if (s.node.flags as u32 & PM_LOADDIR) != 0 {
630            s.filename.clone().map(|d| vec![d])
631        } else {
632            None
633        }
634    };
635    let path = match getfpfunc(&name, &mut dir_path, loaddir_spec.as_deref(), 0, &mut dump_hit) {
636        Some(p) => p,
637        None => {
638            // !!! WARNING: RUST-ONLY BRANCH — NO DIRECT C COUNTERPART !!!
639            // compsys ships as native Rust functions (src/compsys/router.rs),
640            // so names like `_main_complete` have no definition file in
641            // $fpath. C zsh always loads them from files; zshrs must let
642            // `autoload +X -Uz _main_complete` (e.g. fzf-tab via zinit's
643            // :zinit-tmp-subst-autoload, zinit.zsh:356) succeed without one.
644            // Mark the stub loaded and return success — call-time dispatch
645            // short-circuits to the native fn (vm_helper.rs:2276), so no
646            // funcdef/body is needed.
647            if crate::compsys::router::try_rust_dispatch(&name).is_some() {
648                unsafe {
649                    (*shf).node.flags &= !(PM_UNDEFINED as i32);
650                }
651                if let Ok(mut tab) = shfunctab_lock().write() {
652                    if let Some(existing) = tab.get_mut(&name) {
653                        existing.node.flags &= !(PM_UNDEFINED as i32);
654                    }
655                }
656                return 0;
657            }
658            // c:Src/exec.c:5713-5719 — file not found path. C:
659            //   `if (prog == &dummy_eprog) {
660            //        locallevel--;
661            //        zwarn("%s: function definition file not found",
662            //              shf->node.nam);
663            //        locallevel++;
664            //        popheap();
665            //        return NULL;
666            //    }`
667            // C's getfpfunc returns &dummy_eprog as the "not found"
668            // sentinel when test_only==0; loadautofn detects it and
669            // emits the diagnostic before returning NULL. Rust's
670            // getfpfunc returns Option::None for the same condition,
671            // so we emit the same diagnostic here. The locallevel
672            // dance is preserved as a comment because the Rust
673            // port's zwarn doesn't reference locallevel in the
674            // format string itself (the dance in C is only to keep
675            // the prefix line counter consistent with the function-
676            // body context). Bug #107 in docs/BUGS.md.
677            crate::ported::utils::zwarn(&format!("{}: function definition file not found", name));
678            return 1; // c:5719 NULL
679        }
680    };
681    let _ = autol;
682    // Previously the Rust port treated this parameter as
683    // "test_only" and early-returned when set, so the `+X`
684    // call from `eval_autoload` (`loadautofn(shf, mode, 1, d)`)
685    // never actually loaded the file. C's parameter is `autol`
686    // (autoload mode), NOT a test-only flag — the C body
687    // unconditionally loads/parses regardless of autol. autol=1
688    // controls the EF_RUN / map-flag dance for the wordcode prog
689    // (c:5725-5749), but the loaded-body / PM_UNDEFINED-clear
690    // path runs in all cases. Removing the early-return so
691    // `autoload -U +X funcname` actually loads the body and
692    // `type funcname` reports `function from /path/file` instead
693    // of `autoload shell function`. Bug #160 in docs/BUGS.md.
694    // c:5100-5140 — read the file. C uses zopen + read + parse_string +
695    // execsave; Rust port stores raw text on the ShFunc and defers
696    // parse-to-Eprog until the first call.
697    //
698    // c:Src/exec.c:6238 / parse.c:3833 — when getfpfunc resolved the
699    // function out of a compiled `.zwc` dump, there is no source file
700    // to read; the wordcode Eprog came back through `dump_hit`. C
701    // executes that wordcode directly (`shf->funcdef = stripkshdef(
702    // prog, ...)`, exec.c:5753-5755). zshrs executes function bodies
703    // through the fusevm bytecode pipeline which consumes source
704    // text, so bridge wordcode → text with the canonical text.c
705    // renderer (`getpermtext`, ported at text.rs:189) — the same
706    // walker `functions NAME` printing uses for wordcode-backed
707    // funcdefs (C hashtable.c:954). The downstream
708    // `autoload_register_source` step (vm_helper.rs:3162) performs
709    // the `stripkshdef` shape decision, matching c:5725-5760.
710    // c:5706-5710 — the ksh-mode precedence chain:
711    //   `if (ksh == 1) { ksh = fksh; if (ksh == 1)
712    //        ksh = PM_KSHSTORED ? 2 : PM_ZSHSTORED ? 0 : 1; }`
713    // The dump header flag (FDHF_KSHLOAD/FDHF_ZSHLOAD via `*ksh`
714    // from try_dump_file) outranks the stub's PM_*STORED bits, which
715    // are only consulted when the dump says 1 (no explicit style).
716    // zshrs's load/register split (vm_helper's
717    // `autoload_register_source` makes the c:5725 ksh-vs-zsh
718    // decision later, from the tab entry's flags + KSHAUTOLOAD) —
719    // fold a decisive dump flag into the PM bits so the downstream
720    // decision sees the same precedence.
721    let dump_ksh = dump_hit.as_ref().map(|(_, k)| *k);
722    let body = match dump_hit {
723        Some((prog, _ksh)) => crate::ported::text::getpermtext(Box::new(prog), None, 0),
724        None => match std::fs::read_to_string(&path) {
725            Ok(t) => t,
726            Err(_) => return 1,
727        },
728    };
729    // c:Src/exec.c:5735/5757 — `loadautofnsetfile(shf, fdir)`. The
730    // helper stamps PM_LOADDIR alongside the filename when fdir is
731    // present, so `whence -v NAME` later concatenates the directory
732    // with `/NAME` (PM_LOADDIR branch at hashtable.rs:1350). zshrs's
733    // prior `shf->filename = dir_path` assignment skipped the flag
734    // → `type colors` printed `from /path/to/functions` instead of
735    // `from /path/to/functions/colors`. Mirror C exactly.
736    unsafe {
737        loadautofnsetfile(&mut *shf, dir_path.as_deref().or(Some(&path)));
738    }
739    // c:5148 — `shf->node.flags &= ~PM_UNDEFINED`.
740    unsafe {
741        (*shf).node.flags &= !(PM_UNDEFINED as i32);
742    }
743    // c:5706-5710 fold (see comment above): decisive dump style wins
744    // over the stub's stored-style bits.
745    let (ksh_on, ksh_off): (i32, i32) = match dump_ksh {
746        Some(2) => (
747            crate::ported::zsh_h::PM_KSHSTORED as i32,
748            crate::ported::zsh_h::PM_ZSHSTORED as i32,
749        ),
750        Some(0) => (
751            crate::ported::zsh_h::PM_ZSHSTORED as i32,
752            crate::ported::zsh_h::PM_KSHSTORED as i32,
753        ),
754        _ => (0, 0),
755    };
756    unsafe {
757        (*shf).node.flags = ((*shf).node.flags | ksh_on) & !ksh_off;
758    }
759    // Sync the body string into the Rust-side ShFunc table so the
760    // lazy-parse path can find it later.
761    if let Ok(mut tab) = shfunctab_lock().write() {
762        if let Some(existing) = tab.get_mut(&name) {
763            existing.body = Some(body);
764            existing.node.flags = (existing.node.flags | ksh_on) & !ksh_off;
765            // c:5657 loadautofnsetfile — store the fpath DIRECTORY absolutized
766            // with PM_LOADDIR (not the raw relative `./fns`), so `whence -v`
767            // prints the absolute source. The `+X` (eval-autoload) path relies
768            // on this since it does not re-register through vm_helper.
769            loadautofnsetfile(existing, dir_path.as_deref());
770        } else {
771            let mut shf = shfunc {
772                node: hashnode {
773                    next: None,
774                    nam: name.clone(),
775                    flags: ksh_on, // c:5706-5710 dump-style fold
776                },
777                filename: None,
778                lineno: 0,
779                funcdef: None,
780                redir: None,
781                sticky: None,
782                body: Some(body),
783            };
784            loadautofnsetfile(&mut shf, dir_path.as_deref()); // c:5657
785            tab.add(shf);
786        }
787    }
788    0
789}
790
791/// Port of `getfpfunc(char *s, int *ksh, char **fdir, char **alt_path, int test_only)` from Src/exec.c:6219. Walks `$fpath` (or the
792/// supplied `spec_path` slice) for a file named `name` and writes the
793/// resolved directory through `*dir_path_out` (matching the C `char **dir_path`).
794/// Returns `Some(file_path)` on success, `None` when not found.
795///
796/// Per dir, the compiled-dump lookup runs FIRST (c:6238
797/// `try_dump_file(*pp, s, buf, ksh, test_only)`) — a directory
798/// digest `<dir>.zwc` or per-function `<dir>/<name>.zwc` wins over
799/// the plain file when newer (mtime logic inside try_dump_file,
800/// c:parse.c:3762-3784). On a dump hit the loaded program + ksh
801/// mode (C's `*ksh` out-param) are written through `dump_out` and
802/// the nominal `<dir>/<name>` path is returned; the caller must
803/// check `dump_out` before reading the returned path as a plain
804/// file.
805pub fn getfpfunc(
806    name: &str,
807    dir_path_out: &mut Option<String>, // c:6219 (Src/exec.c)
808    spec_path: Option<&[String]>,
809    test_only: i32,                      // c:6219 `int test_only`
810    dump_out: &mut Option<(eprog, i32)>, // c:6219 `int *ksh` + dump Eprog return
811) -> Option<String> {
812    // C reads $fpath via `getaparam("fpath")` (the param-table array form
813    // tied to scalar `FPATH` via `typeset -T`). Reading `std::env::var`
814    // misses any in-script modification like `fpath=(/some/dir $fpath)`
815    // because that mutates the internal param table, not the inherited
816    // process env. Fall back to env only when the param table is empty
817    // (cold start before any param-table init).
818    let dirs: Vec<String> = match spec_path {
819        Some(s) => s.to_vec(),
820        None => crate::ported::params::getaparam("fpath")
821            .filter(|v| !v.is_empty())
822            .or_else(|| getsparam("FPATH").map(|v| v.split(':').map(String::from).collect()))
823            .or_else(|| {
824                std::env::var("FPATH")
825                    .ok()
826                    .map(|v| v.split(':').map(String::from).collect())
827            })
828            .unwrap_or_default(),
829    };
830    for dir in &dirs {
831        if dir.is_empty() {
832            continue;
833        }
834        let path = format!("{}/{}", dir, name); // c:6230 snprintf(buf, ..., "%s/%s", *pp, s)
835                                                // c:6238 — `if ((r = try_dump_file(*pp, s, buf, ksh, test_only)))`
836                                                // — the .zwc digest / per-function dump is tried BEFORE the
837                                                // plain file in each directory.
838        if let Some(hit) = crate::ported::parse::try_dump_file(dir, name, &path, test_only != 0) {
839            *dump_out = Some(hit);
840            *dir_path_out = Some(dir.clone()); // c:6240 `*fdir = *pp;`
841            return Some(path); // c:6241
842        }
843        if std::path::Path::new(&path).exists() {
844            // c:6245 access(buf, R_OK)
845            *dir_path_out = Some(dir.clone());
846            return Some(path);
847        }
848    }
849    None
850}
851
852/// Port of `resolvebuiltin(const char *cmdarg, HashNode hn)` from
853/// `Src/exec.c:2703`. Ensures that an autoload-stub builtin has its
854/// module loaded before the caller invokes its `handlerfunc`. If the
855/// stub has no handler, `ensurefeature` is asked to load the module
856/// and re-lookup the builtin node. C body (abridged):
857/// ```c
858/// if (!((Builtin) hn)->handlerfunc) {
859///     char *modname = dupstring(((Builtin) hn)->optstr);
860///     (void)ensurefeature(modname, "b:", ...);
861///     hn = builtintab->getnode(builtintab, cmdarg);
862///     if (!hn) { lastval=1; zerr(...); return NULL; }
863/// }
864/// return hn;
865/// ```
866///
867/// WARNING: zshrs's builtin table is the static `BUILTINS` array in
868/// `src/ported/builtin.rs`. Module autoload routes through
869/// `module::ensurefeature(MODULESTAB, modname, "b:", Some(cmdarg))`;
870/// after the module loads the handler should be wired into BUILTINS.
871pub fn resolvebuiltin<'a>(
872    cmdarg: &str, // c:2703 (Src/exec.c)
873    hn: &'a builtin,
874) -> Option<&'a builtin> {
875    // c:2705 — `if (!((Builtin) hn)->handlerfunc)`.
876    if hn.handlerfunc.is_none() {
877        // c:2706 — `modname = dupstring(((Builtin)hn)->optstr)`.
878        let modname = hn.optstr.clone().unwrap_or_default();
879        // c:2712 — `ensurefeature(modname, "b:", cmdarg)`.
880        let _ = {
881            let mut t = crate::ported::module::MODULESTAB.lock().unwrap();
882            crate::ported::module::ensurefeature(&mut t, &modname, "b:", Some(cmdarg))
883        };
884        // c:2715-2716 — re-lookup the now-(hopefully)-resolved builtin.
885        if let Some(re) = BUILTINS.iter().find(|b| b.node.nam == cmdarg) {
886            if re.handlerfunc.is_some() {
887                return Some(re); // c:2723
888            }
889        }
890        // c:2717-2721 — `lastval = 1; zerr(...)` + return NULL.
891        zerr(&format!(
892            "autoloading module {} failed to define builtin: {}",
893            modname, cmdarg
894        ));
895        return None; // c:2720
896    }
897    Some(hn) // c:2723
898}
899
900/// Dispatch decision returned by `execcmd_compile_head` — the
901/// fusevm-bytecode-time head resolver that mirrors the local-variable
902/// state the C `execcmd_exec` function carries through `c:2913-2916`
903/// (`is_builtin`, `is_shfunc`, `cflags`, `use_defpath`) plus the
904/// precmd-modifier strip count. The fusevm bytecode compiler reads
905/// this to emit the correct dispatch opcode in
906/// `src/extensions/compile_zsh.rs::compile_simple`.
907///
908/// Not a C struct — invented to bridge the divergence between the
909/// C wordcode-walker (which mutates locals + falls through to
910/// invocation) and zshrs's split parse → compile → VM pipeline.
911#[allow(non_camel_case_types)]
912#[derive(Debug, Default, Clone)]
913pub struct execcmd_dispatch {
914    /// Number of `BINF_PREFIX` words to strip from the head of args.
915    /// `Src/exec.c:3086 uremnode(preargs, firstnode(preargs))`.
916    pub precmd_skip: usize,
917    /// Set when the head (after strip) is a real builtin
918    /// (`Src/exec.c:3065 is_builtin = 1`).
919    pub is_builtin: bool,
920    /// Set when the head (after strip) is a shell function
921    /// (`Src/exec.c:3053 is_shfunc = 1`).
922    pub is_shfunc: bool,
923    /// `cflags` accumulator from `Src/exec.c:2915` — gathers
924    /// `BINF_BUILTIN | BINF_COMMAND | BINF_EXEC | BINF_DASH |
925    /// BINF_NOGLOB` bits encountered during the precommand-modifier
926    /// walk (c:3062 `cflags |= hn->flags`).
927    pub cflags: u32,
928    /// `command -p` requested: use the default `$PATH` for lookup
929    /// (`Src/exec.c:3160 use_defpath = 1`). NOT YET HONORED by the
930    /// fusevm compiler — flagged for follow-up.
931    pub use_defpath: bool,
932    /// `command -v` / `command -V` requested: the dispatch target
933    /// flips to `bin_whence` per `Src/exec.c:3149-3157`
934    /// (`hn = &commandbn.node; is_builtin = 1`). The fusevm compiler
935    /// reads this and emits `Op::CallBuiltin(BUILTIN_WHENCE_FROM_COMMAND)`
936    /// instead of resolving the post-strip head.
937    pub has_command_vv: bool,
938    /// `exec -a NAME` requested: ARGV0 override per `Src/exec.c:3214-3240`.
939    /// `Some(NAME)` triggers `zputenv("ARGV0=NAME")` before exec.
940    pub exec_argv0: Option<String>,
941    /// Empty-command branch fired with no redirs (`Src/exec.c:3372-3406`
942    /// — the `else` arm of `if (redir && nonempty(redir))`). Covers
943    /// bare `exec` / `noglob` / `command`. Caller emits
944    /// `lastval = cmdoutval` (0 when no `$(cmd)` ran) and returns.
945    /// Also fires for the `(cflags & BINF_PREFIX) && (cflags &
946    /// BINF_COMMAND)` sub-case at `c:3365-3371` (bare `command`
947    /// returns 0 without complaining about missing redirs).
948    pub is_empty_command: bool,
949}
950
951/// !!! NOT A PORT OF C `execcmd_exec` !!!
952///
953/// This is a fusevm-bytecode-time head resolver invoked by
954/// `src/extensions/compile_zsh.rs::compile_simple` and the
955/// `command` builtin shim in `src/fusevm_bridge.rs`. The canonical
956/// 7-arg port of `Src/exec.c:execcmd_exec` lives elsewhere in this
957/// file under the C-faithful name `execcmd_exec`.
958///
959/// This helper mirrors the head section (`c:2904-3275`) of the C
960/// function — local initialisation, the precommand-modifier walk
961/// that strips `BINF_PREFIX` builtins (`-`, `builtin`, `command`,
962/// `exec`, `noglob`), and the `BINF_COMMAND`/`BINF_EXEC`
963/// sub-option parsers — and returns the resulting dispatch
964/// decision via `execcmd_dispatch`. The fusevm compiler reads
965/// that struct to decide which `Op::CallBuiltin` /
966/// `Op::CallFunction` / `Op::Exec` to emit, and to compute the
967/// correct post-strip `argc`.
968///
969/// =================== WARNING — DIVERGENCE ====================
970///
971/// The C function runs ~1500 lines and PERFORMS dispatch: it sets up
972/// `multio` redirections, evaluates `varspc` assignments, then calls
973/// `execbuiltin` / `runshfunc` / `execute` directly. This helper
974/// stops after the precmd-modifier walk and only returns the head
975/// decision; runtime dispatch is driven by the bytecode the fusevm
976/// compiler emits.
977///
978/// Signature adaptation: the C `Estate`/`Execcmd_params` carry the
979/// wordcode iterator state — zshrs doesn't traverse wordcode here,
980/// so the args list arrives already-expanded as a `&[String]`
981/// (analog of `preargs` after `execcmd_getargs` at `c:3028`).
982/// `type_` mirrors `eparams->type` (`WC_SIMPLE` vs `WC_TYPESET`).
983///
984/// =============================================================
985pub fn execcmd_compile_head(args: &[String], type_: u32) -> execcmd_dispatch {
986    // c:2900 (Src/exec.c)
987
988    // c:2904-2916 — locals.
989    let mut hn: Option<&'static builtin> = None; // c:2904
990    let mut is_shfunc = false; // c:2913
991    let mut is_builtin = false; // c:2913
992    let mut use_defpath = false; // c:2913
993    let mut cflags: u32 = 0; // c:2915
994    let mut orig_cflags: u32 = 0; // c:2915
995    let _ = orig_cflags;
996    // c:3263 — `char *exec_argv0 = NULL;` (declared inside the
997    // BINF_EXEC arm; hoisted here so the dispatch struct can carry it
998    // out after the loop terminates).
999    let mut exec_argv0: Option<String> = None;
1000    // c:3149/3158 — `has_vV`/`has_p` flags from the BINF_COMMAND arm
1001    // (c:3104). Surface `has_vV` via the dispatch struct so the fusevm
1002    // compiler can emit `bin_whence` instead of resolving the head.
1003    let mut has_command_vv = false;
1004
1005    // c:2962-2973 — `%job` head: rewrite `%name` → `fg|bg|disown %name`.
1006    // Not in scope for the compile-time dispatch walk: jobspec
1007    // expansion happens at runtime in fusevm; the bytecode emits a
1008    // direct `fg`/`bg` call when it sees a leading `%`. Flagged for
1009    // follow-up when the canonical port lands.
1010
1011    // c:2975-2986 — AUTORESUME prefix-match against jobtab. Same
1012    // status as the %job head: runtime concern, deferred.
1013
1014    // c:3013-3091 — precommand-modifier walk.
1015    let mut preargs: Vec<String> = args.to_vec(); // c:3027 newlinklist
1016    let mut precmd_skip: usize = 0;
1017
1018    // c:3018 — `if ((type == WC_SIMPLE || type == WC_TYPESET) && args)`.
1019    if (type_ == WC_SIMPLE || type_ == WC_TYPESET) && !preargs.is_empty() {
1020        // c:3018
1021        // c:3029 — `while (nonempty(preargs))`.
1022        while precmd_skip < preargs.len() {
1023            // c:3029
1024            // c:3030 — `cmdarg = (char *) peekfirst(preargs);`.
1025            let cmdarg = untokenize(&preargs[precmd_skip]);
1026            // c:3031 — `checked = !has_token(cmdarg)`. zshrs's fusevm
1027            // already performed prefork expansion on `preargs`, so
1028            // `has_token` is effectively false here; the C `break` on
1029            // unexpanded tokens is unreachable in this entry point.
1030
1031            // c:3034-3035 — WC_TYPESET fast path: `getnode2` looks up
1032            // even disabled builtins so the reserved-word form
1033            // (`integer x`, `local foo`) still dispatches to the
1034            // typeset family. The static `BUILTINS` array doesn't
1035            // expose a separate disabled-bit lookup; one path covers
1036            // both. Effect is identical for the precmd-modifier walk.
1037
1038            // c:3050-3052 — `if (!(cflags & (BINF_BUILTIN |
1039            // BINF_COMMAND)) && shfunctab->getnode(...))` — shell
1040            // function takes precedence unless a `builtin`/`command`
1041            // modifier preceded it.
1042            if (cflags & (BINF_BUILTIN | BINF_COMMAND)) == 0 {
1043                // c:3051
1044                if shfunctab_lock()
1045                    .read()
1046                    .map(|t| t.iter().any(|(k, _)| k == &cmdarg))
1047                    .unwrap_or(false)
1048                {
1049                    is_shfunc = true; // c:3053
1050                    break; // c:3054
1051                }
1052            }
1053            // c:3056 — `builtintab->getnode(builtintab, cmdarg)`.
1054            let entry = BUILTINS.iter().find(|b| b.node.nam == cmdarg);
1055            let Some(entry) = entry else {
1056                // c:3056-3058
1057                break;
1058            };
1059            hn = Some(entry);
1060            // c:3061-3063 — accumulate cflags.
1061            orig_cflags |= cflags;
1062            cflags &= !(BINF_BUILTIN | BINF_COMMAND);
1063            cflags |= entry.node.flags as u32;
1064            // c:3064 — `if (!(hn->flags & BINF_PREFIX))` — real
1065            // builtin, stop.
1066            if (entry.node.flags as u32 & BINF_PREFIX) == 0 {
1067                // c:3064
1068                // WARNING — DIVERGENCE: c:3068 calls `resolvebuiltin`
1069                // to autoload the builtin's module if its
1070                // `handlerfunc` is NULL. In zshrs, builtins live in
1071                // two places: the static `BUILTINS` table (which
1072                // mirrors C `handlerfunc`, often `None` for ports
1073                // dispatched through fusevm) AND fusevm's
1074                // `register_builtins` map (the actual runtime
1075                // dispatcher). A null `handlerfunc` in the static
1076                // table is NOT an autoload failure for us — it
1077                // means dispatch routes through fusevm. So we
1078                // skip the resolvebuiltin call here; the faithful
1079                // port remains available for future callers that
1080                // genuinely need module-autoload semantics.
1081                is_builtin = true; // c:3065
1082                break; // c:3077
1083            }
1084            // c:3086 — `uremnode(preargs, firstnode(preargs))`.
1085            precmd_skip += 1;
1086            // c:3087-3091 — `if (!firstnode(preargs)) { execcmd_getargs
1087            //   (...); if (!firstnode(preargs)) break; }`. zshrs has
1088            // no `execcmd_getargs` (args arrive pre-expanded); the
1089            // bounds-check at the top of `while precmd_skip <
1090            // preargs.len()` handles the empty case identically.
1091
1092            // c:3092-3177 — BINF_COMMAND sub-option parsing
1093            // (`command -p / -v / -V`).
1094            if (cflags & BINF_COMMAND) != 0 && precmd_skip < preargs.len() {
1095                // c:3102-3104 — `LinkNode argnode, oldnode, pnode = NULL;
1096                //                int has_p = 0, has_vV = 0, has_other = 0;`
1097                let mut argnode: usize = precmd_skip; // c:3105 `argnode = firstnode(preargs);`
1098                let mut pnode: Option<usize> = None; // c:3102
1099                let mut has_p = false; // c:3104
1100                let mut has_vv = false; // c:3104
1101                let mut has_other = false; // c:3104
1102                                           // c:3107 — `while (IS_DASH(*argdata))`
1103                while argnode < preargs.len()
1104                    && IS_DASH(preargs[argnode].chars().next().unwrap_or('\0'))
1105                {
1106                    let argdata = preargs[argnode].clone(); // c:3106
1107                    let bytes = argdata.as_bytes();
1108                    // c:3108-3111 — stop on bare `-` or `--`.
1109                    if bytes.len() < 2 || (IS_DASH(bytes[1] as char) && bytes.len() == 2) {
1110                        // c:3109
1111                        break; // c:3111
1112                    }
1113                    // c:3112-3133 — scan flag chars.
1114                    for &c in &bytes[1..] {
1115                        // c:3112
1116                        match c as char {
1117                            'p' => {
1118                                // c:3114
1119                                has_p = true; // c:3122
1120                                pnode = Some(argnode); // c:3123
1121                            }
1122                            'v' | 'V' => {
1123                                // c:3125-3126
1124                                has_vv = true; // c:3127
1125                            }
1126                            _ => {
1127                                // c:3129
1128                                has_other = true; // c:3130
1129                            }
1130                        }
1131                    }
1132                    // c:3134-3138 — unknown flag → don't try, leave alone.
1133                    if has_other {
1134                        // c:3134
1135                        has_p = false; // c:3136
1136                        has_vv = false; // c:3136
1137                        break; // c:3137
1138                    }
1139                    // c:3140-3147 — advance to next arg.
1140                    argnode += 1; // c:3141 nextnode(argnode)
1141                    if argnode >= preargs.len() {
1142                        // c:3142 — execcmd_getargs (skipped: pre-expanded)
1143                        break; // c:3145
1144                    }
1145                }
1146                // c:3149-3157 — `-v`/`-V` → dispatch to whence.
1147                if has_vv {
1148                    // c:3149
1149                    // c:3154 `pushnode(preargs, "command")` — C re-inserts
1150                    // "command" so bin_whence sees it as argv[0]. zshrs
1151                    // surfaces this via `has_command_vv`; the fusevm
1152                    // compiler emits the equivalent whence call.
1153                    has_command_vv = true; // c:3155-3156 hn = &commandbn; is_builtin=1
1154                    is_builtin = true;
1155                    break; // c:3157
1156                } else if has_p {
1157                    // c:3158
1158                    use_defpath = true; // c:3160
1159                    if let Some(pn) = pnode {
1160                        // c:3165 — `uremnode(preargs, pnode)`. zshrs:
1161                        // remove the `-p`-bearing arg from preargs.
1162                        if pn < preargs.len() {
1163                            preargs.remove(pn);
1164                            // precmd_skip already accounts for the
1165                            // stripped `command` prefix; we just removed
1166                            // the `-p` flag which sat at preargs[pn].
1167                            // No precmd_skip change needed — the head
1168                            // remains where it was.
1169                        }
1170                    }
1171                }
1172                // c:3176-3177 — `--` trailing end-of-options strip.
1173                if argnode < preargs.len() {
1174                    let argdata = &preargs[argnode];
1175                    let b = argdata.as_bytes();
1176                    if b.len() == 2 && IS_DASH(b[0] as char) && IS_DASH(b[1] as char) {
1177                        // c:3176
1178                        preargs.remove(argnode); // c:3177
1179                    }
1180                }
1181            } else if (cflags & BINF_EXEC) != 0 && precmd_skip < preargs.len() {
1182                // c:3178-3275 — BINF_EXEC sub-option parsing
1183                // (`exec -a NAME -l -c`).
1184                let mut argnode: usize = precmd_skip; // c:3185
1185                let mut error_done = false;
1186                // c:3196 — `while (argdata && IS_DASH(*argdata) &&
1187                //                  strlen(argdata) >= 2)`
1188                while argnode < preargs.len() {
1189                    let argdata = preargs[argnode].clone();
1190                    let bytes = argdata.as_bytes();
1191                    if bytes.is_empty() || !IS_DASH(bytes[0] as char) || bytes.len() < 2 {
1192                        break; // c:3196 loop guard
1193                    }
1194                    let oldnode = argnode; // c:3197
1195                    argnode += 1; // c:3198 nextnode(oldnode)
1196                                  // c:3203-3208 — empty next → error.
1197                    if argnode >= preargs.len() {
1198                        // c:3203
1199                        zerr(
1200                            // c:3204
1201                            "exec requires a command to execute",
1202                        );
1203                        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:3206
1204                        error_done = true;
1205                        break; // c:3207 goto done
1206                    }
1207                    // c:3209 — `uremnode(preargs, oldnode)`.
1208                    preargs.remove(oldnode);
1209                    argnode -= 1; // re-anchor — `argnode` was the post-removed slot
1210                                  // c:3210-3211 — `--` stops option scan.
1211                    if bytes.len() == 2 && IS_DASH(bytes[0] as char) && IS_DASH(bytes[1] as char) {
1212                        // c:3210
1213                        break; // c:3211
1214                    }
1215                    // c:3212-3258 — scan flag chars after the leading `-`.
1216                    let mut k = 1usize;
1217                    while k < bytes.len() && !error_done {
1218                        let cmdopt = bytes[k] as char; // c:3212
1219                        match cmdopt {
1220                            'a' => {
1221                                // c:3214 — `-a` ARGV0 override.
1222                                if k + 1 < bytes.len() {
1223                                    // c:3216 — `-aNAME` inline form.
1224                                    exec_argv0 =
1225                                        Some(String::from_utf8_lossy(&bytes[k + 1..]).into_owned()); // c:3217
1226                                    k = bytes.len(); // c:3219 position past end
1227                                } else {
1228                                    // c:3220 — `-a NAME` separate form.
1229                                    if argnode >= preargs.len() {
1230                                        // c:3230
1231                                        zerr(
1232                                            // c:3231
1233                                            "exec flag -a requires a parameter",
1234                                        );
1235                                        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:3233
1236                                        error_done = true;
1237                                        break; // c:3234 goto done
1238                                    }
1239                                    exec_argv0 = Some(preargs[argnode].clone()); // c:3236
1240                                    preargs.remove(argnode); // c:3239
1241                                }
1242                            }
1243                            'c' => {
1244                                // c:3242
1245                                cflags |= BINF_CLEARENV; // c:3243
1246                            }
1247                            'l' => {
1248                                // c:3245
1249                                cflags |= BINF_DASH; // c:3246
1250                            }
1251                            _ => {
1252                                // c:3248
1253                                zerr(
1254                                    // c:3249
1255                                    &format!("unknown exec flag -{}", cmdopt),
1256                                );
1257                                errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:3251
1258                                error_done = true;
1259                                break; // c:3256
1260                            }
1261                        }
1262                        k += 1;
1263                    }
1264                    if error_done {
1265                        break;
1266                    }
1267                }
1268                // c:3263-3274 — zputenv("ARGV0=NAME"). zshrs defers
1269                // the actual `setenv` to the fusevm compiler / external
1270                // exec path; we surface `exec_argv0` via the dispatch
1271                // struct so the caller can apply it before fork+exec.
1272                if let Some(ref a0) = exec_argv0 {
1273                    // c:3263 — `remnulargs + untokenize` then setenv.
1274                    let cleaned = untokenize(a0); // c:3266-3267
1275                    exec_argv0 = Some(cleaned);
1276                }
1277                if error_done {
1278                    return execcmd_dispatch {
1279                        precmd_skip,
1280                        is_builtin,
1281                        is_shfunc,
1282                        cflags,
1283                        use_defpath,
1284                        has_command_vv,
1285                        exec_argv0,
1286                        is_empty_command: false,
1287                    };
1288                }
1289            }
1290            // c:3275-3278 — `hn = NULL; if ((cflags & BINF_COMMAND) &&
1291            // unset(POSIXBUILTINS)) break;`. After processing a
1292            // `command` precmd modifier (and its -p/-v/-V flags), the
1293            // C loop exits with hn cleared so the dispatch falls
1294            // through to external lookup. Without this, the next
1295            // iteration would find `command print` → print's builtin
1296            // and dispatch to it; zsh's intentional behaviour is to
1297            // skip builtins under `command` (unless POSIXBUILTINS is
1298            // set, where the loop continues normally).
1299            if (cflags & BINF_COMMAND) != 0 && !isset(POSIXBUILTINS) {
1300                hn = None; // c:3275 hn = NULL
1301                break; // c:3277
1302            }
1303        }
1304    }
1305
1306    // c:3309-3406 — "Empty command" branch. When the precmd-modifier
1307    // walk above strips every word with nothing left to dispatch
1308    // (bare `exec`, bare `noglob`, bare `command`, bare `nocorrect`),
1309    // C falls into `if (!args || empty(args))` at c:3331. Sub-cases:
1310    //
1311    // - redir-present + do_exec       → nullexec=1 (continue to run)
1312    // - redir-present + varspc        → nullexec=2 (continue)
1313    // - redir-present + no nullcmd    → `zerr("redirection with no command")`
1314    //                                   lastval=1, return
1315    // - redir-present + SHNULLCMD     → args=[":"]
1316    // - redir-present + readnullcmd   → args=[readnullcmd]
1317    // - redir-present + default       → args=[nullcmd]
1318    // - NO redir + BINF_PREFIX+COMMAND → lastval=0, return (c:3365-3371)
1319    // - NO redir + default            → lastval=cmdoutval, return (c:3372-3406)
1320    //
1321    // zshrs's `execcmd_compile_head` doesn't receive `redir` (it
1322    // takes `args` only). The cases that DEPEND on redirs are handled by
1323    // `compile_zsh.rs::compile_redir` before this dispatch fires; the
1324    // remaining cases collapse into the single `is_empty_command`
1325    // flag below. Both NO-redir sub-cases produce the same observable
1326    // outcome (lastval=0, return without invoking anything), so a
1327    // single flag suffices.
1328    let is_empty_command = precmd_skip >= preargs.len();
1329
1330    // =================== WARNING — DIVERGENCE ====================
1331    // c:3285+: prefork-substitution, magic_assign decision, multio
1332    // setup, varspc evaluation, and the actual execbuiltin /
1333    // runshfunc / execute call. ~1300 lines of interpreter-only
1334    // code, entirely replaced by fusevm bytecode dispatch in
1335    // `src/extensions/compile_zsh.rs::compile_simple` and the
1336    // opcode handlers in `src/fusevm_bridge.rs::register_builtins`.
1337    // The return value below feeds those compile-time decisions.
1338    // =============================================================
1339
1340    let _ = hn;
1341    execcmd_dispatch {
1342        precmd_skip,
1343        is_builtin,
1344        is_shfunc,
1345        cflags,
1346        use_defpath,
1347        has_command_vv,
1348        exec_argv0,
1349        is_empty_command,
1350    }
1351}
1352
1353// =============================================================================
1354// Leaf-function ports — c:283 (parse_string) and below. Added incrementally to
1355// chip at the ~5500 lines of exec.c still un-ported beyond the wordcode
1356// walker (execlist / execpline / execcmd which the fusevm bytecode VM
1357// replaces — see the WARNING block in execcmd_exec).
1358// =============================================================================
1359
1360/// Port of `parse_string(char *s, int reset_lineno)` from `Src/exec.c:283`.
1361///
1362/// C body:
1363/// ```c
1364/// Eprog p; zlong oldlineno;
1365/// zcontext_save();
1366/// inpush(s, INP_LINENO, NULL);
1367/// strinbeg(0);
1368/// oldlineno = lineno;
1369/// if (reset_lineno) lineno = 1;
1370/// p = parse_list();
1371/// lineno = oldlineno;
1372/// if (tok == LEXERR && !lastval) lastval = 1;
1373/// strinend();
1374/// inpop();
1375/// zcontext_restore();
1376/// return p;
1377/// ```
1378///
1379/// Parses an arbitrary string as a zsh command list, returning the
1380/// `Eprog` (compiled wordcode). Used by `getoutput` for `$(cmd)`,
1381/// `bin_eval` for `eval`, and the autoload path.
1382pub fn parse_string(s: &str, reset_lineno: i32) -> Option<eprog> {
1383    // c:285-286
1384    let p: Option<eprog>;
1385    let oldlineno: i64;
1386
1387    zcontext_save(); // c:288
1388    inpush(s, INP_LINENO, None); // c:289
1389    strinbeg(0); // c:290
1390    oldlineno = LEX_LINENO.get() as i64; // c:291
1391    if reset_lineno != 0 {
1392        // c:292
1393        LEX_LINENO.set(1); // c:293
1394    }
1395    p = parse_list(); // c:294
1396    LEX_LINENO.set(oldlineno as u64); // c:295
1397                                      // c:296-297 — `if (tok == LEXERR && !lastval) lastval = 1;`
1398    if tok() == LEXERR && LASTVAL.load(Ordering::Relaxed) == 0 {
1399        LASTVAL.store(1, Ordering::Relaxed);
1400    }
1401    strinend(); // c:298
1402    inpop(); // c:299
1403    zcontext_restore(); // c:300
1404    p // c:301
1405}
1406
1407/// Port of `int isgooderr(int e, char *dir)` from `Src/exec.c:652`.
1408///
1409/// C body:
1410/// ```c
1411/// /* Maybe the directory was unreadable, or maybe it wasn't even a directory. */
1412/// return ((e != EACCES || !access(dir, X_OK)) &&
1413///         e != ENOENT && e != ENOTDIR);
1414/// ```
1415///
1416/// errno classifier for `execve` failures during PATH search: if the
1417/// errno is EACCES (and the dir is X-accessible) or ENOENT/ENOTDIR,
1418/// it's "expected" (try next PATH entry); otherwise it's a real
1419/// failure worth surfacing.
1420pub fn isgooderr(e: i32, dir: &str) -> bool {
1421    // c:652
1422    // c:Src/exec.c:658-659 — `(e != EACCES || !access(dir, X_OK)) &&
1423    //   e != ENOENT && e != ENOTDIR`. C's `access(dir, X_OK)` returns
1424    //   0 on success / -1 on failure. The previous Rust port used
1425    //   `metadata().permissions().mode() & 0o111` which reports the
1426    //   X bit even when the path doesn't exist as the EFFECTIVE caller
1427    //   (root, ACLs, capabilities all flip access() vs raw mode).
1428    //   `/no/such/dir` metadata() fails → returned false for
1429    //   dir_x_ok, then `!false` = true, giving "good error" for a
1430    //   nonexistent path. Use libc::access directly to match C exactly.
1431    let unmeta_dir = unmeta(dir);
1432    let cstr = std::ffi::CString::new(unmeta_dir.as_bytes()).unwrap_or_default();
1433    let access_ok = unsafe { libc::access(cstr.as_ptr(), libc::X_OK) } == 0;
1434    (e != libc::EACCES || access_ok) && e != libc::ENOENT && e != libc::ENOTDIR
1435}
1436
1437/// Port of `int iscom(char *s)` from `Src/exec.c:962`.
1438///
1439/// C body:
1440/// ```c
1441/// struct stat statbuf;
1442/// char *us = unmeta(s);
1443/// return (access(us, X_OK) == 0 && stat(us, &statbuf) >= 0 &&
1444///         S_ISREG(statbuf.st_mode));
1445/// ```
1446///
1447/// True iff `s` names an executable regular file (X-perm + S_IFREG).
1448/// Used by the PATH-search loop in `findcmd` / `search_defpath` to
1449/// validate candidate paths before exec.
1450pub fn iscom(s: &str) -> bool {
1451    // c:962
1452    let us = unmeta(s); // c:965
1453                        // c:967-968 — `access(us, X_OK) == 0 && stat(us, &statbuf) >= 0 && S_ISREG(...)`
1454    let cstr = match std::ffi::CString::new(us.as_str()) {
1455        Ok(c) => c,
1456        Err(_) => return false,
1457    };
1458    let x_ok = unsafe { libc::access(cstr.as_ptr(), libc::X_OK) } == 0;
1459    if !x_ok {
1460        return false;
1461    }
1462    let meta = match std::fs::metadata(&us) {
1463        Ok(m) => m,
1464        Err(_) => return false,
1465    };
1466    meta.file_type().is_file()
1467}
1468
1469/// Port of `int isreallycom(Cmdnam cn)` from `Src/exec.c:972-987`.
1470///
1471/// Verify that a hashed/cached cmdnamtab entry still names a real
1472/// external command (X-perm + regular file). For HASHED entries
1473/// (`cn->u.cmd` carries the absolute path), test the path directly;
1474/// otherwise concatenate `name[0] + "/" + nam` and test that.
1475/// Used by `execcmd_exec` to drop stale cmdnamtab hits before they
1476/// turn into a failed `execve` syscall.
1477pub fn isreallycom(cn: &cmdnam) -> bool {
1478    // c:972
1479    let fullnam: String;
1480    if (cn.node.flags & crate::ported::zsh_h::HASHED) != 0 {
1481        // c:977-978 — `strcpy(fullnam, cn->u.cmd);`
1482        fullnam = cn.cmd.clone().unwrap_or_default();
1483    } else if cn.name.is_none() || cn.name.as_ref().unwrap().is_empty() {
1484        // c:979-980 — `if (!cn->u.name) return 0;`
1485        return false;
1486    } else {
1487        // c:982-984 — `strcpy + strcat("/") + strcat(nam)`
1488        let path0 = &cn.name.as_ref().unwrap()[0];
1489        fullnam = format!("{}/{}", path0, cn.node.nam);
1490    }
1491    iscom(&fullnam) // c:986
1492}
1493
1494/// Port of `int isrelative(char *s)` from `Src/exec.c:996`.
1495///
1496/// C body:
1497/// ```c
1498/// if (*s != '/') return 1;
1499/// for (; *s; s++)
1500///     if (*s == '.' && s[-1] == '/' &&
1501///         (s[1] == '/' || s[1] == '\0' ||
1502///          (s[1] == '.' && (s[2] == '/' || s[2] == '\0'))))
1503///         return 1;
1504/// return 0;
1505/// ```
1506///
1507/// True iff `s` either doesn't start with `/` OR contains a `./` or
1508/// `../` component anywhere. Used by `cd` resolution and PATH-cache
1509/// invalidation to detect non-canonical paths.
1510pub fn isrelative(s: &str) -> i32 {
1511    // c:996
1512    let bytes = s.as_bytes();
1513    if bytes.is_empty() || bytes[0] != b'/' {
1514        // c:998
1515        return 1; // c:999
1516    }
1517    // c:1000-1004 — walk for `./` or `../` components.
1518    for i in 1..bytes.len() {
1519        let c = bytes[i];
1520        let prev = bytes[i - 1];
1521        if c == b'.' && prev == b'/' {
1522            let next = bytes.get(i + 1).copied().unwrap_or(0);
1523            if next == b'/' || next == 0 {
1524                // c:1002
1525                return 1;
1526            }
1527            if next == b'.' {
1528                let next2 = bytes.get(i + 2).copied().unwrap_or(0);
1529                if next2 == b'/' || next2 == 0 {
1530                    // c:1003
1531                    return 1;
1532                }
1533            }
1534        }
1535    }
1536    0 // c:1005
1537}
1538
1539/// Port of `void setunderscore(char *str)` from `Src/exec.c:2652`.
1540///
1541/// C body:
1542/// ```c
1543/// queue_signals();
1544/// if (str && *str) {
1545///     size_t l = strlen(str) + 1, nl = (l + 31) & ~31;
1546///     if (nl > underscorelen || (underscorelen - nl) > 64) {
1547///         zfree(zunderscore, underscorelen);
1548///         zunderscore = (char *) zalloc(underscorelen = nl);
1549///     }
1550///     strcpy(zunderscore, str);
1551///     underscoreused = l;
1552/// } else {
1553///     ... reset zunderscore = "" ...
1554/// }
1555/// unqueue_signals();
1556/// ```
1557///
1558/// Sets the `$_` global to the last argument of the most recent
1559/// command. Called from `execcmd_exec` (c:3936) per `last_status`
1560/// update; mirrored in zshrs by the fusevm `Op::Exec` handler.
1561pub fn setunderscore(str: &str) {
1562    // c:2652
1563    queue_signals(); // c:2654
1564    if !str.is_empty() {
1565        // c:2655 `if (str && *str)`
1566        // c:2656-2663 — copy str into zunderscore; track byte length in underscoreused.
1567        let mut zu = zunderscore.lock().unwrap();
1568        *zu = str.to_string();
1569        let nl = (str.len() + 1 + 31) & !31; // c:2656
1570        underscorelen.store(nl, Ordering::Relaxed); // c:2660
1571        underscoreused.store((str.len() + 1) as i32, Ordering::Relaxed);
1572    // c:2663
1573    } else {
1574        // c:2664
1575        let mut zu = zunderscore.lock().unwrap();
1576        zu.clear(); // c:2669 `*zunderscore = '\0';`
1577        underscoreused.store(1, Ordering::Relaxed); // c:2670
1578    }
1579    unqueue_signals(); // c:2672
1580}
1581
1582/// Port of `int mpipe(int *pp)` from `Src/exec.c:5160`.
1583///
1584/// C body:
1585/// ```c
1586/// if (pipe(pp) < 0) {
1587///     zerr("pipe failed: %e", errno);
1588///     return -1;
1589/// }
1590/// pp[0] = movefd(pp[0]);
1591/// pp[1] = movefd(pp[1]);
1592/// return 0;
1593/// ```
1594///
1595/// libc `pipe(2)` wrapper that pushes both ends out of the reserved-
1596/// fd range via `movefd`. Used by `getpipe` / `getproc` /
1597/// `spawnpipes` for process substitution and pipeline wiring.
1598pub fn mpipe(pp: &mut [i32; 2]) -> i32 {
1599    // c:5160
1600    let mut fds: [libc::c_int; 2] = [-1; 2];
1601    if unsafe { libc::pipe(fds.as_mut_ptr()) } < 0 {
1602        // c:5162
1603        zerr(&format!(
1604            // c:5163
1605            "pipe failed: {}",
1606            std::io::Error::last_os_error()
1607        ));
1608        return -1; // c:5164
1609    }
1610    pp[0] = movefd(fds[0]); // c:5166
1611    pp[1] = movefd(fds[1]); // c:5167
1612    0 // c:5168
1613}
1614
1615/// Port of `static const char *const ANONYMOUS_FUNCTION_NAME = "(anon)";`
1616/// from `Src/exec.c:5289`. Anonymous-function name marker used by
1617/// `is_anonymous_function_name`, `execfuncdef`, and `doshfunc` for
1618/// `() { ... }` anonymous function dispatch.
1619pub const ANONYMOUS_FUNCTION_NAME: &str = "(anon)";
1620
1621/// Port of `int is_anonymous_function_name(const char *name)` from
1622/// `Src/exec.c:5300`.
1623///
1624/// C body:
1625/// ```c
1626/// return !strcmp(name, ANONYMOUS_FUNCTION_NAME);
1627/// ```
1628///
1629/// True iff the name equals the `"(anon)"` sentinel. Used by zprof
1630/// reporting and `whence -v` to skip / annotate anonymous functions.
1631pub fn is_anonymous_function_name(name: &str) -> i32 {
1632    // c:5300
1633    if name == ANONYMOUS_FUNCTION_NAME {
1634        // c:5302
1635        1
1636    } else {
1637        0
1638    }
1639}
1640
1641/// Port of `void execsave(void)` from `Src/exec.c:6438`.
1642///
1643/// C body:
1644/// ```c
1645/// struct execstack *es = (struct execstack *) zalloc(sizeof(struct execstack));
1646/// es->list_pipe_pid = list_pipe_pid;
1647/// es->nowait = nowait;
1648/// es->pline_level = pline_level;
1649/// es->list_pipe_child = list_pipe_child;
1650/// es->list_pipe_job = list_pipe_job;
1651/// strcpy(es->list_pipe_text, list_pipe_text);
1652/// es->lastval = lastval;
1653/// es->noeval = noeval;
1654/// es->badcshglob = badcshglob;
1655/// es->cmdoutpid = cmdoutpid;
1656/// es->cmdoutval = cmdoutval;
1657/// es->use_cmdoutval = use_cmdoutval;
1658/// es->procsubstpid = procsubstpid;
1659/// es->trap_return = trap_return;
1660/// es->trap_state = trap_state;
1661/// es->trapisfunc = trapisfunc;
1662/// es->traplocallevel = traplocallevel;
1663/// es->noerrs = noerrs;
1664/// es->this_noerrexit = this_noerrexit;
1665/// es->underscore = ztrdup(zunderscore);
1666/// es->next = exstack;
1667/// exstack = es;
1668/// noerrs = cmdoutpid = 0;
1669/// ```
1670///
1671/// Snapshot every transient exec-context global onto the `exstack`
1672/// linked list so a signal-handler / trap-firing nested eval can
1673/// scribble freely; `execrestore` pops the frame back. Called by
1674/// `dotrap` (signals.c) and the trap-firing entry in `execlist`.
1675pub fn execsave() {
1676    // c:6438
1677    // c:6442 — `es = zalloc(sizeof(execstack));`
1678    let mut es = Box::new(execstack {
1679        // c:6442
1680        next: None,
1681        list_pipe_pid: list_pipe_pid.load(Ordering::Relaxed), // c:6443
1682        nowait: nowait.load(Ordering::Relaxed),               // c:6444
1683        pline_level: pline_level.load(Ordering::Relaxed),     // c:6445
1684        list_pipe_child: list_pipe_child.load(Ordering::Relaxed), // c:6446
1685        list_pipe_job: list_pipe_job.load(Ordering::Relaxed), // c:6447
1686        list_pipe_text: {
1687            // c:6448 — `strcpy(es->list_pipe_text, list_pipe_text);`
1688            let mut buf = [0u8; JOBTEXTSIZE];
1689            if let Ok(s) = LIST_PIPE_TEXT.lock() {
1690                let bytes = s.as_bytes();
1691                let n = bytes.len().min(JOBTEXTSIZE - 1);
1692                buf[..n].copy_from_slice(&bytes[..n]);
1693            }
1694            buf
1695        },
1696        lastval: LASTVAL.load(Ordering::Relaxed), // c:6449
1697        // c:6450 — `es->noeval = noeval;`. Snapshot math.c's
1698        // `int noeval` (the parse-only side-effect-skip counter)
1699        // via math.rs's pub accessor.
1700        noeval: crate::ported::math::m_noeval(),
1701        // c:6451 — `es->badcshglob = badcshglob;`. Snapshot the
1702        // csh-glob diagnostic counter (glob.c:103 / glob.rs
1703        // BADCSHGLOB) so nested eval / trap dispatch doesn't disturb
1704        // the outer command's per-line accounting.
1705        badcshglob: crate::ported::glob::BADCSHGLOB.load(Ordering::Relaxed), // c:6451
1706        cmdoutpid: cmdoutpid.load(Ordering::Relaxed),                        // c:6452
1707        cmdoutval: cmdoutval.load(Ordering::Relaxed),                        // c:6453
1708        use_cmdoutval: use_cmdoutval.load(Ordering::Relaxed),                // c:6454
1709        procsubstpid: procsubstpid.load(Ordering::Relaxed),                  // c:6455
1710        trap_return: TRAP_RETURN.load(Ordering::Relaxed),                    // c:6456
1711        trap_state: TRAP_STATE.load(Ordering::Relaxed),                      // c:6457
1712        trapisfunc: trapisfunc.load(Ordering::Relaxed),                      // c:6458
1713        traplocallevel: traplocallevel.load(Ordering::Relaxed),              // c:6459
1714        noerrs: noerrs.load(Ordering::Relaxed),                              // c:6460
1715        this_noerrexit: this_noerrexit.load(Ordering::Relaxed),              // c:6461
1716        // c:6462 — `es->underscore = ztrdup(zunderscore);`
1717        underscore: Some(zunderscore.lock().unwrap().clone()),
1718    });
1719    // c:6463-6464 — `es->next = exstack; exstack = es;`
1720    let mut head = exstack.lock().unwrap();
1721    es.next = head.take();
1722    *head = Some(es);
1723    // c:6465 — `noerrs = cmdoutpid = 0;`
1724    noerrs.store(0, Ordering::Relaxed);
1725    cmdoutpid.store(0, Ordering::Relaxed);
1726}
1727
1728/// Port of `void execrestore(void)` from `Src/exec.c:6470`.
1729///
1730/// C body:
1731/// ```c
1732/// struct execstack *en = exstack;
1733/// DPUTS(!exstack, "BUG: execrestore() without execsave()");
1734/// queue_signals();
1735/// exstack = exstack->next;
1736/// list_pipe_pid = en->list_pipe_pid;
1737/// nowait = en->nowait;
1738/// pline_level = en->pline_level;
1739/// list_pipe_child = en->list_pipe_child;
1740/// list_pipe_job = en->list_pipe_job;
1741/// strcpy(list_pipe_text, en->list_pipe_text);
1742/// lastval = en->lastval;
1743/// noeval = en->noeval;
1744/// badcshglob = en->badcshglob;
1745/// cmdoutpid = en->cmdoutpid;
1746/// cmdoutval = en->cmdoutval;
1747/// use_cmdoutval = en->use_cmdoutval;
1748/// procsubstpid = en->procsubstpid;
1749/// trap_return = en->trap_return;
1750/// trap_state = en->trap_state;
1751/// trapisfunc = en->trapisfunc;
1752/// traplocallevel = en->traplocallevel;
1753/// noerrs = en->noerrs;
1754/// this_noerrexit = en->this_noerrexit;
1755/// setunderscore(en->underscore);
1756/// zsfree(en->underscore);
1757/// free(en);
1758/// unqueue_signals();
1759/// ```
1760///
1761/// Pop the top `execstack` frame and restore every transient
1762/// exec-context global. Inverse of `execsave`.
1763pub fn execrestore() {
1764    // c:6470
1765    let mut head = exstack.lock().unwrap();
1766    let en = match head.take() {
1767        // c:6472 + c:6477
1768        Some(en) => en,
1769        None => {
1770            // c:6474 — DPUTS(!exstack, "BUG: execrestore() without execsave()")
1771            crate::DPUTS!(true, "BUG: execrestore() without execsave()");
1772            return;
1773        }
1774    };
1775    queue_signals(); // c:6476
1776    *head = en.next; // c:6477
1777    drop(head); // release lock before scalar restores
1778
1779    list_pipe_pid.store(en.list_pipe_pid, Ordering::Relaxed); // c:6479
1780    nowait.store(en.nowait, Ordering::Relaxed); // c:6480
1781    pline_level.store(en.pline_level, Ordering::Relaxed); // c:6481
1782    list_pipe_child.store(en.list_pipe_child, Ordering::Relaxed); // c:6482
1783    list_pipe_job.store(en.list_pipe_job, Ordering::Relaxed); // c:6483
1784                                                              // c:6484 — `strcpy(list_pipe_text, en->list_pipe_text);`.
1785    if let Ok(mut s) = LIST_PIPE_TEXT.lock() {
1786        let nul = en
1787            .list_pipe_text
1788            .iter()
1789            .position(|&b| b == 0)
1790            .unwrap_or(JOBTEXTSIZE);
1791        *s = String::from_utf8_lossy(&en.list_pipe_text[..nul]).into_owned();
1792    }
1793    LASTVAL.store(en.lastval, Ordering::Relaxed); // c:6485
1794                                                  // c:6486 — `noeval = en->noeval;`. Restore math.c's noeval
1795                                                  // counter from the saved frame.
1796    crate::ported::math::m_noeval_set(en.noeval);
1797    // c:6487 — `badcshglob = en->badcshglob;`. Restore the csh-glob
1798    // diagnostic counter saved on entry.
1799    crate::ported::glob::BADCSHGLOB.store(en.badcshglob, Ordering::Relaxed);
1800    cmdoutpid.store(en.cmdoutpid, Ordering::Relaxed); // c:6488
1801    cmdoutval.store(en.cmdoutval, Ordering::Relaxed); // c:6489
1802    use_cmdoutval.store(en.use_cmdoutval, Ordering::Relaxed); // c:6490
1803    procsubstpid.store(en.procsubstpid, Ordering::Relaxed); // c:6491
1804    TRAP_RETURN.store(en.trap_return, Ordering::Relaxed); // c:6492
1805    TRAP_STATE.store(en.trap_state, Ordering::Relaxed); // c:6493
1806    trapisfunc.store(en.trapisfunc, Ordering::Relaxed); // c:6494
1807    traplocallevel.store(en.traplocallevel, Ordering::Relaxed); // c:6495
1808    noerrs.store(en.noerrs, Ordering::Relaxed); // c:6496
1809    this_noerrexit.store(en.this_noerrexit, Ordering::Relaxed); // c:6497
1810                                                                // c:6498-6499 — `setunderscore(en->underscore); zsfree(en->underscore);`
1811    if let Some(ref u) = en.underscore {
1812        setunderscore(u); // c:6498
1813    }
1814    // c:6500 — `free(en);` — handled by Box drop when `en` falls out of scope.
1815    unqueue_signals(); // c:6502
1816}
1817
1818/// Port of `void execstring(char *s, int dont_change_job, int exiting,
1819/// char *context)` from `Src/exec.c:1228`.
1820///
1821/// C body:
1822/// ```c
1823/// Eprog prog;
1824/// pushheap();
1825/// if (isset(VERBOSE)) {
1826///     zputs(s, stderr);
1827///     fputc('\n', stderr);
1828///     fflush(stderr);
1829/// }
1830/// if ((prog = parse_string(s, 0)))
1831///     execode(prog, dont_change_job, exiting, context);
1832/// popheap();
1833/// ```
1834///
1835/// Public entry — execute an arbitrary string as a zsh command list.
1836/// Called by `eval`, `.`/`source`, `trap` action firing, autoload
1837/// body executors, command substitution body runners.
1838///
1839/// =================== WARNING — DIVERGENCE ====================
1840/// The C path is `parse_string` → `execode` → `execlist` (wordcode
1841/// walker). zshrs replaces `execode/execlist` with the fusevm
1842/// bytecode VM at `crate::vm_helper::ShellExecutor::execute_script_zsh_pipeline`.
1843/// Faithful port: VERBOSE banner + pushheap/popheap intact; the
1844/// parse+execute chain delegates to the fusevm entry. When `execlist`
1845/// lands as a strict 1:1 port, swap the delegate for the canonical
1846/// chain.
1847/// =============================================================
1848pub fn execstring(s: &str, _dont_change_job: i32, _exiting: i32, _context: &str) {
1849    // c:1228
1850    pushheap(); // c:1232
1851                // c:1233-1237 — VERBOSE banner.
1852    if isset(VERBOSE) {
1853        // c:1233
1854        let mut stderr = std::io::stderr().lock();
1855        use std::io::Write;
1856        let _ = stderr.write_all(s.as_bytes()); // c:1234 zputs(s, stderr)
1857        let _ = stderr.write_all(b"\n"); // c:1235
1858        let _ = stderr.flush(); // c:1236
1859    }
1860    // c:1238-1239 — parse + execode. zshrs delegates the parse+VM
1861    // chain to the fusevm pipeline via the exec_hooks fn-ptr
1862    // installed by fusevm_bridge at startup. Direct
1863    // `with_executor` / ShellExecutor reach-in from src/ported/ is
1864    // forbidden — see memory feedback_no_exec_script_from_ported.
1865    let _ = crate::ported::exec::execute_script_zsh_pipeline(s);
1866    popheap(); // c:1240
1867}
1868
1869/// Port of `void runshfunc(Eprog prog, FuncWrap wrap, char *name)` from
1870/// `Src/exec.c:6166`. The inner shell-function executor — fires
1871/// module-registered wrapper handlers around the function body, with
1872/// `$_` (zunderscore) save/restore and a paramscope push/pop around
1873/// the wordcode walk.
1874///
1875/// C control flow:
1876/// ```c
1877/// queue_signals();
1878/// ou = zalloc(ouu = underscoreused);
1879/// if (ou) memcpy(ou, zunderscore, underscoreused);
1880/// while (wrap) {                       // wrapper chain
1881///     wrap->module->wrapper++;
1882///     cont = wrap->handler(prog, wrap->next, name);
1883///     wrap->module->wrapper--;
1884///     if (!wrap->module->wrapper && (wrap->module->node.flags & MOD_UNLOAD))
1885///         unload_module(wrap->module);
1886///     if (!cont) {                     // wrapper handled it
1887///         if (ou) zfree(ou, ouu);
1888///         unqueue_signals();
1889///         return;
1890///     }
1891///     wrap = wrap->next;
1892/// }
1893/// startparamscope();
1894/// execode(prog, 1, 0, "shfunc");
1895/// if (ou) { setunderscore(ou); zfree(ou, ouu); }
1896/// endparamscope();
1897/// unqueue_signals();
1898/// ```
1899///
1900/// (a) `wrap->module->wrapper++/--` (c:6178/6180) wired against
1901///     `module::MODULESTAB.modules[name].wrapper` (i32), looked up
1902///     by `wrap.module.node.nam`. Recursive unload during handler
1903///     defers correctly.
1904/// (b) `unload_module(wrap->module)` (c:6184) wired via
1905///     `modulestab.unload_module(name)` when wrapper hits 0 AND
1906///     MOD_UNLOAD flag is set on the module's hashnode.
1907/// (c) `execode(prog, 1, 0, "shfunc")` (c:6195) ported at
1908///     exec.rs:6047. Body uses execode for the no-source
1909///     (compiled-wordcode) branch and fusevm for the
1910///     source-preserving (autoloaded) branch per cache coherence.
1911/// (d) `startparamscope/endparamscope` Rust signatures take
1912///     `&mut HashTable` (params.rs:7425/7435). We pass the global
1913///     paramtab handle via the params crate.
1914pub fn runshfunc(prog: &eprog, mut wrap: Option<&funcwrap>, name: &str) {
1915    // c:6166
1916    queue_signals(); // c:6171
1917                     // c:6173-6175 — snapshot zunderscore into `ou`.
1918    let ouu = underscoreused.load(Ordering::Relaxed) as usize;
1919    let ou: Option<String> = if ouu > 0 {
1920        // c:6174
1921        Some(zunderscore.lock().unwrap().clone()) // c:6175
1922    } else {
1923        None
1924    };
1925    // c:6177-6193 — wrapper chain walk.
1926    while let Some(w) = wrap {
1927        // c:6177
1928        // c:6178 — wrap->module->wrapper++ (WARNING a).
1929        // c:6178 — `wrap->module->wrapper++;` — bump refcount so a
1930        // recursive unload during the handler defers until we return.
1931        let mod_name: Option<String> = w.module.as_ref().map(|m| m.node.nam.clone());
1932        if let Some(ref n) = mod_name {
1933            if let Ok(mut tab) = crate::ported::module::MODULESTAB.lock() {
1934                if let Some(m) = tab.modules.get_mut(n) {
1935                    m.wrapper += 1;
1936                }
1937            }
1938        }
1939        let cont = if let Some(h) = w.handler {
1940            // c:6179 — WrapFunc takes Eprog by value + next FuncWrap by value.
1941            // We pass an empty next sentinel (wrapper-chain walks are
1942            // single-step in zshrs — see chain-walk comment below).
1943            let next_sentinel = Box::new(funcwrap {
1944                next: None,
1945                flags: 0,
1946                handler: None,
1947                module: None,
1948            });
1949            h(Box::new(prog.clone()), next_sentinel, name)
1950        } else {
1951            1
1952        };
1953        // c:6180 — `wrap->module->wrapper--;`
1954        // c:6182-6184 — `if (!wrap->module->wrapper && (flags & MOD_UNLOAD)) unload_module(wrap->module);`
1955        if let Some(ref n) = mod_name {
1956            let should_unload = {
1957                if let Ok(mut tab) = crate::ported::module::MODULESTAB.lock() {
1958                    if let Some(m) = tab.modules.get_mut(n) {
1959                        m.wrapper -= 1;
1960                        m.wrapper == 0 && (m.node.flags & crate::ported::zsh_h::MOD_UNLOAD) != 0
1961                    } else {
1962                        false
1963                    }
1964                } else {
1965                    false
1966                }
1967            };
1968            if should_unload {
1969                if let Ok(mut tab) = crate::ported::module::MODULESTAB.lock() {
1970                    let _ = tab.unload_module(n); // c:6184
1971                }
1972            }
1973        }
1974        if cont == 0 {
1975            // c:6186 — wrapper claimed the call.
1976            unqueue_signals(); // c:6189
1977            return; // c:6190
1978        }
1979        // c:6192 — wrap = wrap->next; the linked-list step requires
1980        // owning the next ref; the borrowed iteration breaks here.
1981        // Wrapper chains > 1 are extremely rare; we stop at the
1982        // first to avoid a Box::leak.
1983        wrap = None;
1984    }
1985    // c:6194 — startparamscope (just inc_locallevel internally).
1986    inc_locallevel();
1987    // c:6195 — `execode(prog, 1, 0, "shfunc");` — run the function
1988    // body. Prefer the canonical execode (exec.rs:6047) which walks
1989    // execlist on a fresh estate over the prog. If prog.strs carries
1990    // the original source (autoloaded ported that the lazy-compile path
1991    // populated), route through the fusevm pipeline for cache
1992    // coherence with execstring.
1993    if let Some(ref src) = prog.strs {
1994        let _ = crate::ported::exec::execute_script_zsh_pipeline(src);
1995    } else {
1996        // Pure wordcode body — drive via the canonical execode.
1997        execode_wordcode(Box::new(prog.clone()), 1, 0, "shfunc");
1998        let _ = name;
1999    }
2000    if let Some(ou_str) = ou {
2001        // c:6196
2002        setunderscore(&ou_str); // c:6197
2003                                // c:6198 — zfree(ou, ouu) — Rust drops on scope exit.
2004    }
2005    endparamscope(); // c:6200
2006                     // c:6141 — deferred-exit gate. After endparamscope() unwinds the
2007                     // function's local scope (locallevel--), check whether an exit
2008                     // queued inside the function has reached its target scope:
2009                     //   if (exit_pending && exit_level >= locallevel+1 && !in_exit_trap)
2010                     // The `+1` accounts for endparamscope having already happened
2011                     // here (locallevel is already one less than when exit_level was
2012                     // captured at c:5890). When the gate fires:
2013                     //   - locallevel > forklevel: still in a nested function — force
2014                     //     the outer frame to return too (retflag=1, breaks=loops).
2015                     //   - locallevel <= forklevel: out of all functions — actually
2016                     //     exit the shell now via zexit(exit_val, ZEXIT_NORMAL).
2017                     // `in_exit_trap` (c:Src/signals.c:63 — `int in_exit_trap;`) is the
2018                     // EXIT-trap reentry counter. dotrap at signals.c:1272/1277 wraps
2019                     // SIGEXIT handler dispatch with ++/--, so an exit issued FROM an
2020                     // EXIT trap shouldn't re-trigger the gate (or the trap would
2021                     // recurse). zshrs's signals::in_exit_trap is the canonical port
2022                     // surface — read it directly here.
2023    let exit_pending = crate::ported::builtin::EXIT_PENDING.load(Ordering::Relaxed);
2024    let exit_level = crate::ported::builtin::EXIT_LEVEL.load(Ordering::Relaxed);
2025    let cur_locallevel = locallevel.load(Ordering::Relaxed) as i32;
2026    let cur_forklevel = FORKLEVEL.load(Ordering::Relaxed);
2027    let in_exit_trap = crate::ported::signals::in_exit_trap.load(Ordering::Relaxed); // c:Src/signals.c:63
2028    if exit_pending != 0 && exit_level >= cur_locallevel + 1 && in_exit_trap == 0 {
2029        // c:6141
2030        if cur_locallevel > cur_forklevel {
2031            // c:6143 — still inside a nested function: keep unwinding.
2032            RETFLAG.store(1, Ordering::Relaxed); // c:6144
2033            BREAKS.store(LOOPS.load(Ordering::Relaxed), Ordering::Relaxed); // c:6145
2034        } else {
2035            // c:6151 — out of all functions: exit for real.
2036            crate::ported::builtin::STOPMSG.store(1, Ordering::Relaxed); // c:6151
2037            let val = EXIT_VAL.load(Ordering::Relaxed);
2038            crate::ported::builtin::zexit(val, crate::ported::zsh_h::ZEXIT_NORMAL);
2039            // c:6152
2040        }
2041    }
2042    unqueue_signals(); // c:6202
2043}
2044
2045/// Port of `Emulation_options sticky_emulation_dup(Emulation_options src,
2046/// int useheap)` from `Src/exec.c:5501`.
2047///
2048/// C body (`useheap` selects between heap-arena and permanent zalloc;
2049/// Rust collapses both into owned `Box` clones):
2050/// ```c
2051/// Emulation_options newsticky = useheap ?
2052///     hcalloc(sizeof(*src)) : zshcalloc(sizeof(*src));
2053/// newsticky->emulation = src->emulation;
2054/// if (src->n_on_opts) {
2055///     size_t sz = src->n_on_opts * sizeof(*src->on_opts);
2056///     newsticky->n_on_opts = src->n_on_opts;
2057///     newsticky->on_opts = useheap ? zhalloc(sz) : zalloc(sz);
2058///     memcpy(newsticky->on_opts, src->on_opts, sz);
2059/// }
2060/// if (src->n_off_opts) {
2061///     size_t sz = src->n_off_opts * sizeof(*src->off_opts);
2062///     newsticky->n_off_opts = src->n_off_opts;
2063///     newsticky->off_opts = useheap ? zhalloc(sz) : zalloc(sz);
2064///     memcpy(newsticky->off_opts, src->off_opts, sz);
2065/// }
2066/// return newsticky;
2067/// ```
2068///
2069/// Deep-clone a sticky emulation struct. Used by `shfunc_set_sticky`
2070/// at function-def time to snapshot the pending `sticky` global so
2071/// the function carries its own immutable copy.
2072pub fn sticky_emulation_dup(src: &emulation_options, _useheap: i32) -> Emulation_options {
2073    // c:5501
2074    // c:5503-5505 — `newsticky = hcalloc/zshcalloc; newsticky->emulation = src->emulation;`
2075    let mut newsticky = Box::new(emulation_options {
2076        emulation: src.emulation, // c:5505
2077        n_on_opts: 0,
2078        n_off_opts: 0,
2079        on_opts: Vec::new(),
2080        off_opts: Vec::new(),
2081    });
2082    // c:5506-5511 — copy on_opts.
2083    if src.n_on_opts != 0 {
2084        // c:5506
2085        newsticky.n_on_opts = src.n_on_opts; // c:5508
2086        newsticky.on_opts = src.on_opts.clone(); // c:5510 memcpy
2087    }
2088    // c:5512-5517 — copy off_opts.
2089    if src.n_off_opts != 0 {
2090        // c:5512
2091        newsticky.n_off_opts = src.n_off_opts; // c:5514
2092        newsticky.off_opts = src.off_opts.clone(); // c:5516 memcpy
2093    }
2094    newsticky // c:5519
2095}
2096
2097/// Port of `void shfunc_set_sticky(Shfunc shf)` from `Src/exec.c:5527`.
2098///
2099/// C body:
2100/// ```c
2101/// if (sticky)
2102///     shf->sticky = sticky_emulation_dup(sticky, 0);
2103/// else
2104///     shf->sticky = NULL;
2105/// ```
2106///
2107/// Stamp the function with the current pending sticky-emulation
2108/// snapshot (deep-copy via `sticky_emulation_dup`), or clear it.
2109pub fn shfunc_set_sticky(shf: &mut shfunc) {
2110    // c:5527
2111    let sticky_guard = sticky.lock().unwrap();
2112    if let Some(ref s) = *sticky_guard {
2113        // c:5529
2114        shf.sticky = Some(sticky_emulation_dup(s, 0)); // c:5530
2115    } else {
2116        // c:5531
2117        shf.sticky = None; // c:5532
2118    }
2119}
2120
2121/// Port of `static char *search_defpath(char *cmd, char *pbuf, int plen)`
2122/// from `Src/exec.c:691`.
2123///
2124/// Walk DEFAULT_PATH for an executable `<dir>/<cmd>` regular file.
2125/// Used by `command -p` to bypass the user's `$PATH` and search the
2126/// system default (`/bin:/usr/bin:...`).
2127pub fn search_defpath(cmd: &str, plen: usize) -> Option<String> {
2128    // c:691
2129    // c:695 — `for (ps = DEFAULT_PATH; ps; ps = pe ? pe+1 : NULL)`.
2130    for ps in DEFAULT_PATH.split(':') {
2131        // c:695
2132        // c:697 — `if (*ps == '/')`.
2133        if !ps.starts_with('/') {
2134            continue;
2135        }
2136        // c:700-707 — PATH_MAX bounds check on `<dir>` segment.
2137        if ps.len() >= plen {
2138            // c:700 / c:704
2139            continue; // c:701 / c:705
2140        }
2141        // c:708 — `*s++ = '/';`. c:709-710 bounds check on `<dir>/<cmd>`.
2142        let full_len = ps.len() + 1 + cmd.len();
2143        if full_len >= plen {
2144            // c:709
2145            continue; // c:710
2146        }
2147        let buf = format!("{}/{}", ps, cmd); // c:711 `strucpy(&s, cmd);`
2148                                             // c:712 — `if (iscom(pbuf)) return pbuf;`
2149        if iscom(&buf) {
2150            // c:712
2151            return Some(buf); // c:713
2152        }
2153    }
2154    None // c:716
2155}
2156
2157/// Port of `static int checkclobberparam(struct redir *f)` from
2158/// `Src/exec.c:2178`.
2159///
2160/// C body:
2161/// ```c
2162/// struct value vbuf; Value v;
2163/// char *s = f->varid; int fd;
2164/// if (!s) return 1;
2165/// if (!(v = getvalue(&vbuf, &s, 0))) return 1;
2166/// if (v->pm->node.flags & PM_READONLY) {
2167///     zwarn("can't allocate file descriptor to readonly parameter %s",
2168///           f->varid);
2169///     errno = 0;
2170///     return 0;
2171/// }
2172/// /* We can't clobber the value in the parameter if it's
2173///  * already an opened file descriptor */
2174/// if (!isset(CLOBBER) && (s = getstrvalue(v)) &&
2175///     (fd = (int)zstrtol(s, &s, 10)) >= 0 && !*s &&
2176///     fd <= max_zsh_fd && fdtable[fd] == FDT_EXTERNAL) {
2177///     zwarn("can't clobber parameter %s containing file descriptor %d",
2178///          f->varid, fd);
2179///     errno = 0;
2180///     return 0;
2181/// }
2182/// return 1;
2183/// ```
2184///
2185/// Validate that `f->varid` (the `{var}>file` brace-FD form's var
2186/// name) is writable and (under NOCLOBBER) doesn't currently hold an
2187/// FDT_EXTERNAL fd number. Returns 1 on OK, 0 on refusal (zwarn
2188/// already emitted).
2189///
2190/// NOCLOBBER + FDT_EXTERNAL clause now ported (c:2199-2213). When
2191/// NOCLOBBER is set and the param's value is the fd-number of an
2192/// FDT_EXTERNAL-marked fd in the fdtable, refuse with a warning so
2193/// the existing fd doesn't get clobbered by the upcoming open(2).
2194pub fn checkclobberparam(f: &redir) -> i32 {
2195    // c:2178
2196    // c:2182 — `char *s = f->varid;`
2197    let s = match &f.varid {
2198        Some(v) => v.clone(),
2199        None => return 1, // c:2185-2186 — `if (!s) return 1;`
2200    };
2201    // c:2186 — `if (!(v = getvalue(&vbuf, &s, 0))) return 1;`
2202    let mut vbuf = crate::ported::zsh_h::value {
2203        pm: None,
2204        arr: Vec::new(),
2205        scanflags: 0,
2206        valflags: 0,
2207        start: 0,
2208        end: 0,
2209    };
2210    let mut cursor: &str = s.as_str();
2211    let v_opt = crate::ported::params::getvalue(Some(&mut vbuf), &mut cursor, 0);
2212    if v_opt.is_none() {
2213        return 1; // c:2187
2214    }
2215    // c:2188-2197 — readonly refusal via v->pm->node.flags.
2216    let readonly = vbuf
2217        .pm
2218        .as_ref()
2219        .map(|p| (p.node.flags as u32 & PM_READONLY) != 0)
2220        .unwrap_or(false);
2221    if readonly {
2222        // c:2191
2223        zwarn(&format!(
2224            // c:2192
2225            "can't allocate file descriptor to readonly parameter {}",
2226            s
2227        ));
2228        // c:2195 — `errno = 0;` not flagged as a system error.
2229        return 0; // c:2196
2230    }
2231    // c:2199-2213 — NOCLOBBER + FDT_EXTERNAL refusal: if NOCLOBBER set
2232    // AND the param holds a valid fd that's already in our fdtable as
2233    // FDT_EXTERNAL (allocated by sysopen / coproc / etc.), refuse the
2234    // open so we don't clobber it.
2235    if !isset(CLOBBER) {
2236        // c:2201 — `getstrvalue(v)` — read the param's string form.
2237        let val_str = crate::ported::params::getstrvalue(Some(&mut vbuf));
2238        if let Ok(fd) = val_str.trim().parse::<i32>() {
2239            // c:2202 — `if (fd <= max_zsh_fd && fdtable[fd] == FDT_EXTERNAL)`
2240            let max_fd = MAX_ZSH_FD.load(Ordering::Relaxed);
2241            if fd >= 0 && fd <= max_fd {
2242                let kind = fdtable_get(fd);
2243                if kind == FDT_EXTERNAL {
2244                    zwarn(&format!("{}: file descriptor {} already open", s, fd)); // c:2206-2210
2245                    return 0; // c:2211
2246                }
2247            }
2248        }
2249    }
2250    1 // c:2214
2251}
2252
2253/// Port of `static int clobber_open(struct redir *f)` from
2254/// `Src/exec.c:2221`.
2255///
2256/// C body:
2257/// ```c
2258/// struct stat buf;
2259/// int fd, oerrno;
2260/// char *ufname = unmeta(f->name);
2261/// /* If clobbering, just open. */
2262/// if (isset(CLOBBER) || IS_CLOBBER_REDIR(f->type))
2263///     return open(ufname, O_WRONLY | O_CREAT | O_TRUNC | O_NOCTTY, 0666);
2264/// /* If not clobbering, attempt to create file exclusively. */
2265/// if ((fd = open(ufname, O_WRONLY | O_CREAT | O_EXCL | O_NOCTTY, 0666)) >= 0)
2266///     return fd;
2267/// /* If that fails, we are still allowed to open non-regular files. */
2268/// oerrno = errno;
2269/// if ((fd = open(ufname, O_WRONLY | O_NOCTTY)) != -1) {
2270///     if (!fstat(fd, &buf)) {
2271///         if (!S_ISREG(buf.st_mode)) return fd;
2272///         /* CLOBBER_EMPTY allows re-use of empty regular files. */
2273///         if (isset(CLOBBEREMPTY) && buf.st_size == 0) return fd;
2274///     }
2275///     close(fd);
2276/// }
2277/// errno = oerrno;
2278/// return -1;
2279/// ```
2280///
2281/// Open the redir target for write with the NOCLOBBER rules:
2282/// - CLOBBER set or `>|` form → just open with O_TRUNC
2283/// - Otherwise → try O_EXCL first; on EEXIST, only allow non-regular
2284///   files (FIFOs, devices, sockets) OR empty regular files under
2285///   CLOBBEREMPTY.
2286pub fn clobber_open(f: &redir) -> i32 {
2287    // c:2221
2288    let ufname_owned = unmeta(f.name.as_deref().unwrap_or("")); // c:2225
2289    let ufname = match std::ffi::CString::new(ufname_owned.as_str()) {
2290        Ok(c) => c,
2291        Err(_) => return -1,
2292    };
2293    // c:2228-2230 — clobber path: just open + truncate.
2294    if isset(CLOBBER) || IS_CLOBBER_REDIR(f.typ) {
2295        // c:2228
2296        // c:2229 — `open(ufname, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666)`
2297        let fd = unsafe {
2298            libc::open(
2299                ufname.as_ptr(),
2300                libc::O_WRONLY | libc::O_CREAT | libc::O_TRUNC | libc::O_NOCTTY,
2301                0o666 as libc::c_uint,
2302            )
2303        };
2304        return fd; // c:2230
2305    }
2306    // c:2233-2235 — try O_EXCL create first.
2307    let fd = unsafe {
2308        // c:2233
2309        libc::open(
2310            ufname.as_ptr(),
2311            libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOCTTY,
2312            0o666 as libc::c_uint,
2313        )
2314    };
2315    if fd >= 0 {
2316        return fd; // c:2235
2317    }
2318    // c:2240 — `oerrno = errno;` — save for restoration on the recover path.
2319    let oerrno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
2320    // c:2241-2260 — recover: open() w/o O_EXCL, accept if non-regular
2321    // OR (CLOBBEREMPTY && size == 0).
2322    let fd = unsafe {
2323        // c:2241
2324        libc::open(
2325            ufname.as_ptr(),
2326            libc::O_WRONLY | libc::O_NOCTTY,
2327            0o666 as libc::c_uint,
2328        )
2329    };
2330    if fd != -1 {
2331        let mut buf: libc::stat = unsafe { std::mem::zeroed() };
2332        if unsafe { libc::fstat(fd, &mut buf) } == 0 {
2333            // c:2242
2334            // c:2243-2244 — non-regular file: accept.
2335            if (buf.st_mode & libc::S_IFMT) != libc::S_IFREG {
2336                // c:2243
2337                return fd; // c:2244
2338            }
2339            // c:2256-2257 — CLOBBEREMPTY + empty regular: accept.
2340            if isset(CLOBBEREMPTY) && buf.st_size == 0 {
2341                // c:2256
2342                return fd; // c:2257
2343            }
2344        }
2345        unsafe {
2346            libc::close(fd);
2347        } // c:2259
2348    }
2349    // c:2262 — `errno = oerrno;` — restore the EEXIST so caller diagnoses
2350    // "file exists" not the noisier "couldn't reopen" trailing errno.
2351    // Per-platform errno setter: __error() on macOS, __errno_location()
2352    // on Linux. Without cfg gating the build breaks on Linux (CI).
2353    #[cfg(target_os = "macos")]
2354    unsafe {
2355        *libc::__error() = oerrno;
2356    }
2357    #[cfg(target_os = "linux")]
2358    unsafe {
2359        *libc::__errno_location() = oerrno;
2360    }
2361    -1 // c:2263
2362}
2363
2364/// Port of `char *findcmd(char *arg0, int docopy, int default_path)`
2365/// from `Src/exec.c:897`. Walk `$PATH` (or DEFAULT_PATH under
2366/// `default_path=1`) for `arg0`, returning the matching path on
2367/// success. `_docopy` is the C source's "duplicate the result"
2368/// flag — Rust ownership covers it without an explicit copy step.
2369/// `default_path=1` forces `/bin:/usr/bin:...` search (used by
2370/// `command -p`).
2371pub fn findcmd(arg0: &str, _docopy: i32, default_path: i32) -> Option<String> {
2372    // c:897
2373    // c:903-908 — if (default_path) → search_defpath; return.
2374    if default_path != 0 {
2375        return search_defpath(arg0, libc::PATH_MAX as usize);
2376    }
2377    // c:912-913 — strlen(arg0) > PATH_MAX → NULL.
2378    if arg0.len() > libc::PATH_MAX as usize {
2379        return None;
2380    }
2381    // c:Src/exec.c:914-920 — `/`-bearing arg path resolution.
2382    //   if ((s = strchr(arg0, '/'))) {
2383    //       RET_IF_COM(arg0);   // ← unconditional accept on iscom hit
2384    //       if (arg0 == s || unset(PATHDIRS) ||
2385    //           !strncmp(arg0, "./", 2) ||
2386    //           !strncmp(arg0, "../", 3))
2387    //           return NULL;
2388    //   }
2389    // The Rust port had the iscom check gated on `starts_with('/')`,
2390    // so `type ./target/debug/zshrs` returned None even when the
2391    // file was executable. Bug #496 family.
2392    if arg0.contains('/') {
2393        if iscom(arg0) {
2394            return Some(arg0.to_string()); // c:915 RET_IF_COM
2395        }
2396        // c:916-919 — absolute OR PATHDIRS-off OR `./` / `../` →
2397        // give up here (no $PATH walk for these). Relative without
2398        // those prefixes falls through to the $PATH scan below for
2399        // the PATHDIRS=set case.
2400        if arg0.starts_with('/')
2401            || !isset(PATHDIRS)
2402            || arg0.starts_with("./")
2403            || arg0.starts_with("../")
2404        {
2405            return None;
2406        }
2407        // else fall through to PATH walk.
2408    }
2409    // c:943-951 — walk `path[]` (the shell `$path` array). Read $PATH
2410    // from paramtab so shell-private edits via `path=(...)` take
2411    // effect (not OS env only).
2412    let path = getsparam("PATH")?;
2413    for dir in path.split(':') {
2414        if dir.is_empty() {
2415            continue;
2416        }
2417        let candidate = format!("{}/{}", dir, arg0);
2418        if iscom(&candidate) {
2419            return Some(candidate);
2420        }
2421    }
2422    None // c:952
2423}
2424
2425/// Port of `static void addfd(int forked, int *save, struct multio **mfds,
2426///                             int fd1, int fd2, int rflag, char *varid)`
2427/// from `Src/exec.c:2397`.
2428///
2429/// C body (~100 lines, three branches):
2430/// ```c
2431/// if (varid) {
2432///     /* {varid}>file form — move fd above 10 and bind $varid to it */
2433/// } else if (!mfds[fd1] || unset(MULTIOS)) {
2434///     /* new multio OR MULTIOS off — first redir on this fd */
2435/// } else {
2436///     /* additional redir on a fd that's already a multio (split or extend) */
2437/// }
2438/// ```
2439///
2440/// Register `fd2` (already-open) as a redirection target for `fd1`.
2441/// Three branches: `varid` writes the moved fd to `$varid` and bumps
2442/// `fdtable[fd1]` = FDT_EXTERNAL; new-multio path saves the original fd1
2443/// (when `!forked`) and stamps `mfds[fd1]` as a single-entry struct;
2444/// extend-multio path either splits a ct=1 stream into a pipe + 2 fds
2445/// via `mpipe`, or appends another fd to an already-split stream
2446/// (re-allocating mfds for fd1 past the MULTIOUNIT boundary).
2447///
2448/// `multio.fds` is now `Vec<i32>` (zsh_h.rs:1397) so the C
2449/// `hrealloc` at c:2485 maps to `Vec::push`; MULTIOUNIT is no
2450/// longer a hard cap (still 8 for the initial allocation, grown
2451/// on demand thereafter).
2452///
2453/// `fdtable[fdN] |= FDT_SAVED_MASK` at c:2440 — Rust fdtable_set
2454/// stores the int value but doesn't expose a bitwise-OR setter; we
2455/// re-read + OR + re-store as two atomic-feeling steps.
2456pub fn addfd(
2457    forked: i32,
2458    save: &mut [i32; 10],
2459    mfds: &mut [Option<Box<multio>>; 10],
2460    fd1: i32,
2461    fd2: i32,
2462    rflag: i32,
2463    varid: Option<&str>,
2464) {
2465    // c:2397
2466    let mut pipes: [i32; 2] = [-1; 2]; // c:2400
2467
2468    // c:2402-2417 — `if (varid)` branch — {varid}>file shape.
2469    if let Some(vid) = varid {
2470        // c:2402
2471        let fd_moved = movefd(fd2); // c:2404
2472        if fd_moved == -1 {
2473            // c:2405
2474            zerr(&format!(
2475                // c:2406
2476                "cannot move fd {}: {}",
2477                fd2,
2478                std::io::Error::last_os_error()
2479            ));
2480            return; // c:2407
2481        }
2482        // c:2409 — `fdtable[fd1] = FDT_EXTERNAL;`
2483        fdtable_set(fd_moved, FDT_EXTERNAL);
2484        // c:2410 — `setiparam(varid, (zlong)fd1);`
2485        setiparam(vid, fd_moved as i64);
2486        // c:2415-2416 — `if (errflag) zclose(fd1);`
2487        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
2488            // c:2415
2489            let _ = zclose(fd_moved); // c:2416
2490        }
2491        return;
2492    }
2493    // c:2418 — `else if (!mfds[fd1] || unset(MULTIOS))`
2494    let fd1u = fd1 as usize;
2495    if fd1u >= mfds.len() {
2496        return;
2497    }
2498    if mfds[fd1u].is_none() || unset(MULTIOS) {
2499        // c:2418
2500        if mfds[fd1u].is_none() {
2501            // c:2419 — `starting a new multio`
2502            // c:2420 — `mfds[fd1] = zhalloc(sizeof(multio));`
2503            mfds[fd1u] = Some(Box::new(multio {
2504                ct: 0,
2505                rflag: 0,
2506                pipe: -1,
2507                // c:2420 — C allocates VARLENARRAY trailing `int fds[1]`;
2508                // grow on demand via push() below. Pre-fill MULTIOUNIT
2509                // slots with -1 so existing indexed writes (fds[0], fds[1])
2510                // still work without explicit resize().
2511                fds: vec![-1; MULTIOUNIT],
2512            }));
2513            // c:2421 — `if (!forked && save[fd1] == -2)`
2514            if forked == 0 && save[fd1u] == -2 {
2515                if fd1 == fd2 {
2516                    // c:2422
2517                    save[fd1u] = -1; // c:2423
2518                } else {
2519                    // c:2424
2520                    let fd_n = movefd(fd1); // c:2425
2521                    if fd_n < 0 {
2522                        // c:2430
2523                        let e = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
2524                        if e != libc::EBADF {
2525                            // c:2431
2526                            zerr(&format!(
2527                                // c:2432
2528                                "cannot duplicate fd {}: {}",
2529                                fd1,
2530                                std::io::Error::from_raw_os_error(e)
2531                            ));
2532                            mfds[fd1u] = None; // c:2433
2533                            closemnodes(mfds); // c:2434
2534                            return; // c:2435
2535                        }
2536                    } else {
2537                        // c:2438-2439 — DPUTS check that the saved fd is FDT_INTERNAL.
2538                        crate::DPUTS!(
2539                            fdtable_get(fd_n) != FDT_INTERNAL,
2540                            "Saved file descriptor not marked as internal"
2541                        );
2542                        // c:2440 — `fdtable[fdN] |= FDT_SAVED_MASK;`
2543                        let cur = fdtable_get(fd_n);
2544                        fdtable_set(fd_n, cur | FDT_SAVED_MASK);
2545                    }
2546                    save[fd1u] = fd_n; // c:2442
2547                }
2548            }
2549        }
2550        // c:2446-2447 — `if (!varid) redup(fd2, fd1);` (varid already
2551        // handled above; this is the non-varid branch.)
2552        let _ = redup(fd2, fd1);
2553        // c:2448-2450 — `mfds[fd1]->ct=1; mfds[fd1]->fds[0]=fd1; mfds[fd1]->rflag=rflag;`
2554        if let Some(mn) = mfds[fd1u].as_mut() {
2555            mn.ct = 1; // c:2448
2556            mn.fds[0] = fd1; // c:2449
2557            mn.rflag = rflag; // c:2450
2558        }
2559    } else {
2560        // c:2451 — extend existing multio.
2561        // c:2452-2456 — rflag mismatch check.
2562        let cur_rflag = mfds[fd1u].as_ref().map(|m| m.rflag).unwrap_or(0);
2563        if cur_rflag != rflag {
2564            // c:2452
2565            zerr(&format!("file mode mismatch on fd {}", fd1)); // c:2453
2566            closemnodes(mfds); // c:2454
2567            return; // c:2455
2568        }
2569        let cur_ct = mfds[fd1u].as_ref().map(|m| m.ct).unwrap_or(0);
2570        if cur_ct == 1 {
2571            // c:2457 — split the stream.
2572            // c:2458 — `int fdN = movefd(fd1);`
2573            let fd_n = movefd(fd1);
2574            if fd_n < 0 {
2575                // c:2459
2576                zerr(&format!(
2577                    // c:2460
2578                    "multio failed for fd {}: {}",
2579                    fd1,
2580                    std::io::Error::last_os_error()
2581                ));
2582                closemnodes(mfds); // c:2461
2583                return; // c:2462
2584            }
2585            if let Some(mn) = mfds[fd1u].as_mut() {
2586                mn.fds[0] = fd_n; // c:2464
2587            }
2588            // c:2465 — `fdN = movefd(fd2);`
2589            let fd_n2 = movefd(fd2);
2590            if fd_n2 < 0 {
2591                // c:2466
2592                zerr(&format!(
2593                    // c:2467
2594                    "multio failed for fd {}: {}",
2595                    fd2,
2596                    std::io::Error::last_os_error()
2597                ));
2598                closemnodes(mfds); // c:2468
2599                return; // c:2469
2600            }
2601            if let Some(mn) = mfds[fd1u].as_mut() {
2602                mn.fds[1] = fd_n2; // c:2471
2603            }
2604            // c:2472 — `mpipe(pipes)`
2605            if mpipe(&mut pipes) < 0 {
2606                // c:2472
2607                zerr(&format!(
2608                    // c:2473
2609                    "multio failed for fd {}: {}",
2610                    fd2,
2611                    std::io::Error::last_os_error()
2612                ));
2613                closemnodes(mfds); // c:2474
2614                return; // c:2475
2615            }
2616            // c:2477 — `mfds[fd1]->pipe = pipes[1 - rflag];`
2617            if let Some(mn) = mfds[fd1u].as_mut() {
2618                mn.pipe = pipes[(1 - rflag) as usize];
2619            }
2620            // c:2478 — `redup(pipes[rflag], fd1);`
2621            let _ = redup(pipes[rflag as usize], fd1);
2622            // c:2479 — `mfds[fd1]->ct = 2;`
2623            if let Some(mn) = mfds[fd1u].as_mut() {
2624                mn.ct = 2;
2625            }
2626        } else {
2627            // c:2480 — extend already-split stream.
2628            // c:2482-2486 — `mn = hrealloc(mn, sizeof + (ct-1)*sizeof(int),
2629            //                              sizeof + ct*sizeof(int));`
2630            // Rust's `Vec<i32>` grows on demand; ensure capacity for the
2631            // new slot before the indexed write below.
2632            if let Some(mn) = mfds[fd1u].as_mut() {
2633                while mn.fds.len() <= cur_ct as usize {
2634                    mn.fds.push(-1);
2635                }
2636            }
2637            // c:2487 — `if ((fdN = movefd(fd2)) < 0)`
2638            let fd_n = movefd(fd2);
2639            if fd_n < 0 {
2640                zerr(&format!(
2641                    // c:2488
2642                    "multio failed for fd {}: {}",
2643                    fd2,
2644                    std::io::Error::last_os_error()
2645                ));
2646                closemnodes(mfds); // c:2489
2647                return; // c:2490
2648            }
2649            // c:2492 — `mfds[fd1]->fds[mfds[fd1]->ct++] = fdN;`
2650            if let Some(mn) = mfds[fd1u].as_mut() {
2651                let slot = mn.ct as usize;
2652                if slot < mn.fds.len() {
2653                    mn.fds[slot] = fd_n;
2654                    mn.ct += 1;
2655                }
2656            }
2657        }
2658    }
2659}
2660
2661/// Port of `static void closemn(struct multio **mfds, int fd, int type)`
2662/// from `Src/exec.c:2273`.
2663///
2664/// C body (abridged — the meat is the fork-into-tee-or-cat child):
2665/// ```c
2666/// if (fd >= 0 && mfds[fd] && mfds[fd]->ct >= 2) {
2667///     struct multio *mn = mfds[fd];
2668///     char buf[TCBUFSIZE]; int len, i;
2669///     pid_t pid; struct timespec bgtime;
2670///     child_block();
2671///     if ((pid = zfork(&bgtime))) {
2672///         for (i = 0; i < mn->ct; i++) zclose(mn->fds[i]);
2673///         zclose(mn->pipe);
2674///         if (pid == -1) { mfds[fd] = NULL; child_unblock(); return; }
2675///         mn->ct = 1; mn->fds[0] = fd;
2676///         addproc(pid, NULL, 1, &bgtime, -1, -1);
2677///         child_unblock(); return;
2678///     }
2679///     /* pid == 0 (child) */
2680///     opts[INTERACTIVE] = 0;
2681///     dont_queue_signals();
2682///     child_unblock();
2683///     closeallelse(mn);
2684///     if (mn->rflag) {
2685///         /* tee process: read mn->pipe, write each mn->fds[i] */
2686///     } else {
2687///         /* cat process: read each mn->fds[i], write mn->pipe */
2688///     }
2689///     _exit(0);
2690/// } else if (fd >= 0 && type == REDIR_CLOSE)
2691///     mfds[fd] = NULL;
2692/// ```
2693///
2694/// Success-path close of a multio. For ct>=2 (multiple-output
2695/// redirection), forks a tee/cat child that proxies bytes between
2696/// the original fd and the per-output fds. Single-output multios
2697/// (ct=1) skip the fork entirely and just clear the slot.
2698///
2699/// c:2299 — `addproc(pid, NULL, 1, &bgtime, -1, -1)` records the
2700/// tee/cat child in the current job's auxprocs.
2701pub fn closemn(mfds: &mut [Option<Box<multio>>; 10], fd: i32, type_: i32) {
2702    // c:2273
2703    // c:2275 — `if (fd >= 0 && mfds[fd] && mfds[fd]->ct >= 2)`
2704    let needs_tee = fd >= 0
2705        && (fd as usize) < mfds.len()
2706        && mfds[fd as usize].as_ref().is_some_and(|m| m.ct >= 2);
2707    if needs_tee {
2708        // c:2275
2709        // Take the multio out of the slot so we can move pieces into
2710        // the child without aliasing the slot.
2711        let mn = mfds[fd as usize].take().unwrap();
2712        let mut buf = [0u8; 4092]; // c:2277 TCBUFSIZE
2713                                   // c:2287 — `child_block();` block SIGCHLD before fork race.
2714        child_block();
2715        // c:2288 — `pid = zfork(&bgtime);`
2716        let mut bgtime = ZshTimespec {
2717            tv_sec: 0,
2718            tv_nsec: 0,
2719        };
2720        let pid = zfork(Some(&mut bgtime));
2721        if pid != 0 {
2722            // c:2288 parent branch
2723            // c:2289-2290 — close all per-output fds.
2724            for i in 0..mn.ct as usize {
2725                if i < mn.fds.len() {
2726                    let _ = zclose(mn.fds[i]); // c:2290
2727                }
2728            }
2729            let _ = zclose(mn.pipe); // c:2291
2730            if pid == -1 {
2731                // c:2292
2732                // c:2293 — `mfds[fd] = NULL;` already done via .take()
2733                child_unblock(); // c:2294
2734                return; // c:2295
2735            }
2736            // c:2297-2298 — `mn->ct = 1; mn->fds[0] = fd;`
2737            let mut mn_back = mn;
2738            mn_back.ct = 1; // c:2297
2739            mn_back.fds[0] = fd; // c:2298
2740            mfds[fd as usize] = Some(mn_back);
2741            // c:2299 — `addproc(pid, NULL, 1, &bgtime, -1, -1);` — record
2742            // the tee/cat child in the current job's auxprocs (aux=true).
2743            if let Some(jt) = JOBTAB.get() {
2744                let mut guard = jt.lock().unwrap();
2745                let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
2746                if tj >= 0 {
2747                    if let Some(j) = guard.get_mut(tj as usize) {
2748                        crate::ported::jobs::addproc(
2749                            j,
2750                            pid,
2751                            "",
2752                            true,
2753                            Some(std::time::Instant::now()),
2754                            -1,
2755                            -1,
2756                        );
2757                    }
2758                }
2759            }
2760            let _ = bgtime;
2761            child_unblock(); // c:2300
2762            return; // c:2301
2763        }
2764        // c:2303 — child branch (pid == 0).
2765        opt_state_set("interactive", false); // c:2304
2766        dont_queue_signals(); // c:2305
2767        child_unblock(); // c:2306
2768        closeallelse(&mn); // c:2307
2769                           // c:2308-2333 — tee or cat loop.
2770        if mn.rflag != 0 {
2771            // c:2308 — `mn->rflag` set → tee process
2772            // c:2310 — `while ((len = read(mn->pipe, buf, TCBUFSIZE)) != 0)`
2773            loop {
2774                let len = unsafe {
2775                    libc::read(mn.pipe, buf.as_mut_ptr() as *mut libc::c_void, buf.len())
2776                };
2777                if len == 0 {
2778                    break;
2779                }
2780                if len < 0 {
2781                    // c:2311
2782                    let e = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
2783                    if e == libc::EINTR {
2784                        // c:2312
2785                        continue;
2786                    } else {
2787                        break; // c:2315
2788                    }
2789                }
2790                // c:2317-2319 — `for i: write_loop(mn->fds[i], buf, len)`
2791                for i in 0..mn.ct as usize {
2792                    if i >= mn.fds.len() {
2793                        break;
2794                    }
2795                    if write_loop(mn.fds[i], &buf[..len as usize]).is_err() {
2796                        break; // c:2319
2797                    }
2798                }
2799            }
2800        } else {
2801            // c:2321 — cat process
2802            for i in 0..mn.ct as usize {
2803                if i >= mn.fds.len() {
2804                    break;
2805                }
2806                // c:2324 — `while ((len = read(mn->fds[i], buf, TCBUFSIZE)) != 0)`
2807                loop {
2808                    let len = unsafe {
2809                        libc::read(mn.fds[i], buf.as_mut_ptr() as *mut libc::c_void, buf.len())
2810                    };
2811                    if len == 0 {
2812                        break;
2813                    }
2814                    if len < 0 {
2815                        let e = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
2816                        // c:2326 — `if (errno == EINTR && !isatty(mn->fds[i]))`
2817                        if e == libc::EINTR && unsafe { libc::isatty(mn.fds[i]) } == 0 {
2818                            continue;
2819                        } else {
2820                            break; // c:2329
2821                        }
2822                    }
2823                    // c:2331 — `if (write_loop(mn->pipe, buf, len) < 0) break;`
2824                    if write_loop(mn.pipe, &buf[..len as usize]).is_err() {
2825                        break; // c:2332
2826                    }
2827                }
2828            }
2829        }
2830        // c:2335 — `_exit(0);`
2831        unsafe {
2832            libc::_exit(0);
2833        }
2834    } else if fd >= 0 && type_ == REDIR_CLOSE {
2835        // c:2336
2836        // c:2337 — `mfds[fd] = NULL;`
2837        if (fd as usize) < mfds.len() {
2838            mfds[fd as usize] = None;
2839        }
2840    }
2841}
2842
2843/// Port of `static void closemnodes(struct multio **mfds)` from
2844/// `Src/exec.c:2344`.
2845///
2846/// C body:
2847/// ```c
2848/// int i, j;
2849/// for (i = 0; i < 10; i++)
2850///     if (mfds[i]) {
2851///         for (j = 0; j < mfds[i]->ct; j++)
2852///             zclose(mfds[i]->fds[j]);
2853///         mfds[i] = NULL;
2854///     }
2855/// ```
2856///
2857/// Failure-path cleanup: close every fd stashed in any of the 10
2858/// multio slots and null the slot. Called from `execcmd_exec` when
2859/// a redirect setup fails partway through and we need to roll back.
2860pub fn closemnodes(mfds: &mut [Option<Box<multio>>; 10]) {
2861    // c:2344
2862    for i in 0..10 {
2863        // c:2348
2864        if let Some(mn) = mfds[i].take() {
2865            // c:2349
2866            for j in 0..mn.ct as usize {
2867                // c:2350
2868                if j < mn.fds.len() {
2869                    let _ = zclose(mn.fds[j]); // c:2351
2870                }
2871            }
2872            // c:2352 — `mfds[i] = NULL;` — handled by .take() above.
2873        }
2874    }
2875}
2876
2877/// Port of `static void closeallelse(struct multio *mn)` from
2878/// `Src/exec.c:2358`.
2879///
2880/// C body:
2881/// ```c
2882/// int i, j;
2883/// long openmax;
2884/// openmax = fdtable_size;
2885/// for (i = 0; i < openmax; i++)
2886///     if (mn->pipe != i) {
2887///         for (j = 0; j < mn->ct; j++)
2888///             if (mn->fds[j] == i) break;
2889///         if (j == mn->ct)
2890///             zclose(i);
2891///     }
2892/// ```
2893///
2894/// Close every fd in the open range EXCEPT `mn->pipe` and the fds
2895/// stashed in `mn->fds`. Called inside the multio tee/cat child
2896/// process to release every fd the parent had open — only the pipe
2897/// + per-output fds stay alive for the read/write loop.
2898pub fn closeallelse(mn: &multio) {
2899    // c:2358
2900    // c:2363 — `openmax = fdtable_size;`. zshrs models fdtable as a
2901    // Vec; use MAX_ZSH_FD as the upper bound (fdtable_size grows past
2902    // max_zsh_fd in C but every slot past it is FDT_UNUSED anyway).
2903    let openmax = MAX_ZSH_FD.load(Ordering::Relaxed) + 1; // c:2363
2904    for i in 0..openmax {
2905        // c:2365
2906        if mn.pipe == i {
2907            // c:2366
2908            continue;
2909        }
2910        // c:2367-2369 — scan mn->fds[] for i; skip-close if found.
2911        let mut found = false;
2912        for j in 0..mn.ct as usize {
2913            // c:2367
2914            if j < mn.fds.len() && mn.fds[j] == i {
2915                // c:2368
2916                found = true;
2917                break; // c:2369
2918            }
2919        }
2920        // c:2370-2371 — `if (j == mn->ct) zclose(i);`
2921        if !found {
2922            let _ = zclose(i); // c:2371
2923        }
2924    }
2925}
2926
2927/// Port of `static void fixfds(int *save)` from `Src/exec.c:4523`.
2928///
2929/// C body:
2930/// ```c
2931/// int old_errno = errno;
2932/// int i;
2933/// for (i = 0; i != 10; i++)
2934///     if (save[i] != -2)
2935///         redup(save[i], i);
2936/// errno = old_errno;
2937/// ```
2938///
2939/// Restore fds 0..9 from the `save[10]` slot array. `-2` sentinel
2940/// means "no save was made for this fd"; any other value is the
2941/// stashed fd that gets `dup2`'d back via `redup`. Preserves the
2942/// caller's errno across the loop so a downstream caller diagnoses
2943/// the original failure, not a noisy dup2 errno.
2944pub fn fixfds(save: &[i32; 10]) {
2945    // c:4523
2946    let old_errno = std::io::Error::last_os_error().raw_os_error().unwrap_or(0); // c:4525
2947    for i in 0..10i32 {
2948        // c:4528 — `for (i = 0; i != 10; i++)`
2949        if save[i as usize] != -2 {
2950            // c:4529
2951            redup(save[i as usize], i); // c:4530
2952        }
2953    }
2954    // c:4531 — `errno = old_errno;`
2955    #[cfg(target_os = "macos")]
2956    unsafe {
2957        *libc::__error() = old_errno;
2958    }
2959    #[cfg(target_os = "linux")]
2960    unsafe {
2961        *libc::__errno_location() = old_errno;
2962    }
2963}
2964
2965/// Port of `mod_export void closem(int how, int all)` from `Src/exec.c:4546`.
2966///
2967/// C body:
2968/// ```c
2969/// int i;
2970/// for (i = 10; i <= max_zsh_fd; i++)
2971///     if (fdtable[i] != FDT_UNUSED &&
2972///         (all || (fdtable[i] != FDT_PROC_SUBST &&
2973///                  fdtable[i] != FDT_EXTERNAL)) &&
2974///         (how == FDT_UNUSED || (fdtable[i] & FDT_TYPE_MASK) == how)) {
2975///         if (i == SHTTY) SHTTY = -1;
2976///         zclose(i);
2977///     }
2978/// ```
2979///
2980/// Walk fds 10..=MAX_ZSH_FD and close every internal shell fd that
2981/// matches the criteria. `how == FDT_UNUSED` matches all kinds (no
2982/// type filter); otherwise only fds whose low-nibble type equals
2983/// `how` are closed. `all == 0` preserves user-visible fds
2984/// (FDT_PROC_SUBST, FDT_EXTERNAL) since those need to outlive the
2985/// shell's internal-fd lifetime. SHTTY clearing prevents a stale
2986/// reference if we just closed the controlling tty.
2987pub fn closem(how: i32, all: i32) {
2988    // c:4546
2989    let max = MAX_ZSH_FD.load(Ordering::Relaxed); // c:4550
2990    for i in 10i32..=max {
2991        // c:4550
2992        let kind = fdtable_get(i); // c:4551 fdtable[i]
2993        if kind == FDT_UNUSED {
2994            // c:4551
2995            continue;
2996        }
2997        // c:4557-4558 — `(all || (kind != FDT_PROC_SUBST && kind != FDT_EXTERNAL))`
2998        if all == 0 && (kind == FDT_PROC_SUBST || kind == FDT_EXTERNAL) {
2999            continue;
3000        }
3001        // c:4559 — `(how == FDT_UNUSED || (fdtable[i] & FDT_TYPE_MASK) == how)`
3002        if how != FDT_UNUSED && (kind & FDT_TYPE_MASK) != how {
3003            continue;
3004        }
3005        // c:4560-4561 — `if (i == SHTTY) SHTTY = -1;`
3006        if i == SHTTY.load(Ordering::Relaxed) {
3007            // c:4560
3008            SHTTY.store(-1, Ordering::Relaxed); // c:4561
3009        }
3010        // c:4562 — `zclose(i);`
3011        let _ = zclose(i);
3012    }
3013}
3014
3015/// Port of `Cmdnam hashcmd(char *arg0, char **pp)` from
3016/// `Src/exec.c:1010`.
3017///
3018/// C body:
3019/// ```c
3020/// Cmdnam cn;
3021/// char *s, buf[PATH_MAX+1];
3022/// char **pq;
3023/// if (*arg0 == '/') return NULL;
3024/// for (; *pp; pp++)
3025///     if (**pp == '/') {
3026///         s = buf;
3027///         struncpy(&s, *pp, PATH_MAX);
3028///         *s++ = '/';
3029///         if ((s - buf) + strlen(arg0) >= PATH_MAX) continue;
3030///         strcpy(s, arg0);
3031///         if (iscom(buf)) break;
3032///     }
3033/// if (!*pp) return NULL;
3034/// cn = (Cmdnam) zshcalloc(sizeof *cn);
3035/// cn->node.flags = 0;
3036/// cn->u.name = pp;
3037/// cmdnamtab->addnode(cmdnamtab, ztrdup(arg0), cn);
3038/// if (isset(HASHDIRS)) {
3039///     for (pq = pathchecked; pq <= pp; pq++) hashdir(pq);
3040///     pathchecked = pp + 1;
3041/// }
3042/// return cn;
3043/// ```
3044///
3045/// Walk `pp[]` (a $path slice starting from `pathchecked`) for the
3046/// first absolute-PATH entry where `<entry>/<arg0>` is an executable
3047/// regular file. Inserts the unhashed-cmdnam entry into `cmdnamtab`
3048/// and (under HASHDIRS) bulk-hashes every PATH dir we walked through
3049/// so subsequent commands hit the cache.
3050///
3051/// Returns the just-inserted `cmdnam` (now in `cmdnamtab`) on success,
3052/// `None` if `arg0` is absolute or no PATH entry contains it.
3053pub fn hashcmd(arg0: &str, pp: &[String]) -> Option<cmdnam> {
3054    // c:1010
3055    // c:1016 — `if (*arg0 == '/') return NULL;`
3056    if arg0.starts_with('/') {
3057        return None; // c:1017
3058    }
3059    // c:1018-1028 — walk pp[] for first matching absolute entry.
3060    let mut found_idx: Option<usize> = None;
3061    for (i, dir) in pp.iter().enumerate() {
3062        // c:1018
3063        if !dir.starts_with('/') {
3064            // c:1019
3065            continue;
3066        }
3067        // c:1020-1025 — buf = "<dir>/<arg0>"; PATH_MAX bounds check.
3068        if dir.len() + 1 + arg0.len() >= libc::PATH_MAX as usize {
3069            // c:1023
3070            continue; // c:1024
3071        }
3072        let buf = format!("{}/{}", dir, arg0); // c:1025
3073        if iscom(&buf) {
3074            // c:1026
3075            found_idx = Some(i);
3076            break; // c:1027
3077        }
3078    }
3079    // c:1030-1031 — `if (!*pp) return NULL;`
3080    let pp_idx = match found_idx {
3081        Some(i) => i,
3082        None => return None, // c:1031
3083    };
3084    // c:1033-1036 — alloc cn, set flags=0, u.name=pp (the matching slice).
3085    let path_slice: Vec<String> = pp[pp_idx..].to_vec(); // c:1035
3086    let cn = cmdnam_unhashed(arg0, path_slice); // c:1033-1035
3087                                                // c:1036 — `cmdnamtab->addnode(cmdnamtab, ztrdup(arg0), cn);`
3088    if let Ok(mut tab) = cmdnamtab_lock().write() {
3089        tab.add(cn.clone());
3090    }
3091    // c:1038-1042 — under HASHDIRS, bulk-hash every dir up to and
3092    // including the matching one, then bump pathchecked past it.
3093    if isset(HASHDIRS) {
3094        // c:1038
3095        let start = pathchecked.load(Ordering::Relaxed); // c:1039
3096        for pq in start..=pp_idx {
3097            // c:1039
3098            if pq < pp.len() {
3099                hashdir(&pp[pq], pq); // c:1040
3100            }
3101        }
3102        pathchecked.store(pp_idx + 1, Ordering::Relaxed); // c:1041
3103    }
3104    Some(cn) // c:1044
3105}
3106
3107/// Port of `static pid_t zfork(struct timespec *ts)` from
3108/// `Src/exec.c:349`.
3109///
3110/// C body:
3111/// ```c
3112/// pid_t pid;
3113/// if (thisjob != -1 && thisjob >= jobtabsize - 1 && !expandjobtab()) {
3114///     zerr("job table full");
3115///     return -1;
3116/// }
3117/// if (ts) zgettime_monotonic_if_available(ts);
3118/// queue_signals();
3119/// pid = fork();
3120/// unqueue_signals();
3121/// if (pid == -1) {
3122///     zerr("fork failed: %e", errno);
3123///     return -1;
3124/// }
3125/// #ifdef HAVE_GETRLIMIT
3126/// if (!pid) setlimits(NULL);
3127/// #endif
3128/// return pid;
3129/// ```
3130///
3131/// fork(2) wrapper with jobtab capacity check + child rlimit
3132/// re-application. Used by every subshell-spawning path: pipelines,
3133/// process substitution, async commands, command substitution.
3134pub fn zfork(ts: Option<&mut ZshTimespec>) -> libc::pid_t {
3135    // c:349
3136    let pid: libc::pid_t;
3137
3138    // c:356-359 — `if (thisjob != -1 && thisjob >= jobtabsize - 1 && !expandjobtab())`
3139    let thisjob_lock = THISJOB.get_or_init(|| std::sync::Mutex::new(-1));
3140    let thisjob = *thisjob_lock.lock().unwrap();
3141    if thisjob != -1 {
3142        // c:356
3143        let needed = (thisjob + 1) as usize;
3144        let needs_expand = JOBTAB
3145            .get_or_init(|| std::sync::Mutex::new(Vec::new()))
3146            .lock()
3147            .map(|t| needed >= t.len().saturating_sub(1))
3148            .unwrap_or(false);
3149        if needs_expand {
3150            let mut tab = JOBTAB.get().unwrap().lock().unwrap();
3151            if !expandjobtab(&mut tab, needed) {
3152                // c:357
3153                zerr("job table full"); // c:357
3154                return -1; // c:358
3155            }
3156        }
3157    }
3158    // c:360-361 — `if (ts) zgettime_monotonic_if_available(ts);`
3159    if let Some(ts) = ts {
3160        zgettime_monotonic_if_available(ts);
3161    }
3162    // c:368-370 — `queue_signals(); pid = fork(); unqueue_signals();`
3163    queue_signals(); // c:368
3164    pid = unsafe { libc::fork() }; // c:369
3165    unqueue_signals(); // c:370
3166                       // c:371-374 — fork failure.
3167    if pid == -1 {
3168        // c:371
3169        zerr(&format!(
3170            // c:372
3171            "fork failed: {}",
3172            std::io::Error::last_os_error()
3173        ));
3174        return -1; // c:373
3175    }
3176    // c:375-379 — child: re-apply rlimits (HAVE_GETRLIMIT path).
3177    #[cfg(unix)]
3178    if pid == 0 {
3179        // c:376
3180        let _ = setlimits(""); // c:378
3181    }
3182    pid // c:380
3183}
3184
3185/// Port of `void loadautofnsetfile(Shfunc shf, char *fdir)` from
3186/// `Src/exec.c:5657`.
3187///
3188/// C body:
3189/// ```c
3190/// if (!(shf->node.flags & PM_LOADDIR) ||
3191///     strcmp(shf->filename, fdir) != 0) {
3192///     dircache_set(&shf->filename, NULL);
3193///     if (fdir) {
3194///         shf->node.flags |= PM_LOADDIR;
3195///         dircache_set(&shf->filename, fdir);
3196///     } else {
3197///         shf->node.flags &= ~PM_LOADDIR;
3198///         shf->filename = ztrdup(shf->node.nam);
3199///     }
3200/// }
3201/// ```
3202///
3203/// Update `shf->filename` to the autoload directory `fdir`. Routes
3204/// through the refcounted `dircache_set` so identical directory
3205/// strings are shared across shfunc table entries.
3206pub fn loadautofnsetfile(shf: &mut shfunc, fdir: Option<&str>) {
3207    // c:5657
3208    // c:5664-5665 — `if (!(shf->node.flags & PM_LOADDIR) || strcmp(shf->filename, fdir) != 0)`
3209    let loaddir = (shf.node.flags as u32 & PM_LOADDIR) != 0;
3210    let same = match (&shf.filename, fdir) {
3211        (Some(a), Some(b)) => a == b,
3212        _ => false,
3213    };
3214    if !loaddir || !same {
3215        // c:5664
3216        // c:5667 — `dircache_set(&shf->filename, NULL);` — refcount-drop old.
3217        dircache_set(&mut shf.filename, None);
3218        if let Some(fdir) = fdir {
3219            // c:5668
3220            shf.node.flags |= PM_LOADDIR as i32; // c:5670
3221            dircache_set(&mut shf.filename, Some(fdir)); // c:5671
3222        } else {
3223            // c:5672
3224            shf.node.flags &= !(PM_LOADDIR as i32); // c:5674
3225            shf.filename = Some(shf.node.nam.clone()); // c:5675 `ztrdup(shf->node.nam)`
3226        }
3227    }
3228}
3229
3230/// Port of `int commandnotfound(char *arg0, LinkList args)` from
3231/// `Src/exec.c:669`.
3232///
3233/// C body:
3234/// ```c
3235/// Shfunc shf = (Shfunc)
3236///     shfunctab->getnode(shfunctab, "command_not_found_handler");
3237/// if (!shf) {
3238///     lastval = 127;
3239///     return 1;
3240/// }
3241/// pushnode(args, arg0);
3242/// lastval = doshfunc(shf, args, 1);
3243/// return 0;
3244/// ```
3245///
3246/// Look up the user-defined `command_not_found_handler` shfunc and
3247/// invoke it with `arg0` prepended to `args`. Returns 0 if handled,
3248/// 1 if no handler (so caller emits the standard "command not found"
3249/// error). Sets `$?` to 127 in the no-handler path.
3250pub fn commandnotfound(arg0: &str, args: &mut Vec<String>) -> i32 {
3251    // c:669
3252    // c:671-672 — `shf = shfunctab->getnode(shfunctab, "command_not_found_handler");`
3253    let has_handler = shfunctab_lock()
3254        .read()
3255        .map(|t| t.get("command_not_found_handler").is_some())
3256        .unwrap_or(false);
3257    if !has_handler {
3258        // c:674
3259        LASTVAL.store(127, Ordering::Relaxed); // c:675
3260        return 1; // c:676
3261    }
3262    // c:679 — `pushnode(args, arg0);` — prepend arg0 (handler name
3263    // is the first positional arg per C convention).
3264    args.insert(0, arg0.to_string());
3265    args.insert(0, "command_not_found_handler".to_string());
3266    // c:680 — `lastval = doshfunc(shf, args, 1);`. Direct doshfunc
3267    // call mirrors C — body_runner routes through the host body-only
3268    // entry so the function body runs once inside doshfunc's scope.
3269    let shf_clone: Option<shfunc> = shfunctab_lock()
3270        .read()
3271        .ok()
3272        .and_then(|t| t.get("command_not_found_handler").cloned());
3273    if let Some(mut shf) = shf_clone {
3274        let body_args = args.clone();
3275        let body_runner = move || -> i32 {
3276            crate::ported::exec::run_function_body("command_not_found_handler", &body_args[1..])
3277                .unwrap_or(0)
3278        };
3279        let lv = doshfunc(&mut shf, args.clone(), true, body_runner);
3280        LASTVAL.store(lv, Ordering::Relaxed);
3281    }
3282    0 // c:681
3283}
3284
3285/// Port of `char *namedpipe(void)` from `Src/exec.c:5001`.
3286///
3287/// C body (#ifdef HAVE_FIFOS branch):
3288/// ```c
3289/// char *tnam = gettempname(NULL, 1);
3290/// if (!tnam) {
3291///     zerr("failed to create named pipe: %e", errno);
3292///     return NULL;
3293/// }
3294/// if (mkfifo(tnam, 0600) < 0) {
3295///     zerr("failed to create named pipe: %s, %e", tnam, errno);
3296///     return NULL;
3297/// }
3298/// return tnam;
3299/// ```
3300///
3301/// Create a FIFO with a unique name for process substitution. Used by
3302/// `getproc` (`<(cmd)` / `>(cmd)`) on systems without `/dev/fd`.
3303pub fn namedpipe() -> Option<String> {
3304    // c:5001
3305    let tnam = gettempname(None, true); // c:5003
3306    let tnam = match tnam {
3307        Some(t) => t,
3308        None => {
3309            // c:5005
3310            zerr(&format!(
3311                // c:5006
3312                "failed to create named pipe: {}",
3313                std::io::Error::last_os_error()
3314            ));
3315            return None; // c:5007
3316        }
3317    };
3318    // c:5010 — `mkfifo(tnam, 0600)`.
3319    let cstr = match std::ffi::CString::new(tnam.as_str()) {
3320        Ok(c) => c,
3321        Err(_) => return None,
3322    };
3323    if unsafe { libc::mkfifo(cstr.as_ptr(), 0o600) } < 0 {
3324        // c:5010
3325        zerr(&format!(
3326            // c:5014
3327            "failed to create named pipe: {}, {}",
3328            tnam,
3329            std::io::Error::last_os_error()
3330        ));
3331        return None; // c:5015
3332    }
3333    Some(tnam) // c:5017
3334}
3335
3336/// Port of `Eprog parsecmd(char *cmd, char **eptr)` from `Src/exec.c:4878`.
3337///
3338/// C body:
3339/// ```c
3340/// char *str;
3341/// Eprog prog;
3342/// for (str = cmd + 2; *str && *str != Outpar; str++);
3343/// if (!*str || cmd[1] != Inpar) {
3344///     char *errstr = dupstrpfx(cmd, 2);
3345///     untokenize(errstr);
3346///     zerr("unterminated `%s...)'", errstr);
3347///     return NULL;
3348/// }
3349/// *str = '\0';
3350/// if (eptr) *eptr = str+1;
3351/// if (!(prog = parse_string(cmd + 2, 0))) {
3352///     zerr("parse error in process substitution");
3353///     return NULL;
3354/// }
3355/// return prog;
3356/// ```
3357///
3358/// Port of `static LinkList readoutput(int in, int qt, int *readerror)`
3359/// from `Src/exec.c:4805`. Drain a command-substitution pipe fd and
3360/// return the captured output split per `qt`.
3361///
3362/// `qt=1` (quoted-substitution `"$(...)"`): single-element vec with
3363/// the trailing-newline-trimmed buffer (empty buffer → `Nularg` sentinel
3364/// per c:4861).
3365/// `qt=0` (unquoted `$(...)`): split on IFS via `spacesplit`; if
3366/// `GLOBSUBST` is set, each word is `shtokenize`d for downstream globbing.
3367///
3368/// `readerror` is set to the errno on read failure, 0 on clean EOF.
3369pub fn readoutput(in_fd: i32, qt: i32, readerror: &mut i32) -> Vec<String> {
3370    // c:4805
3371    let mut buf: Vec<u8> = Vec::with_capacity(64); // c:4816 (initial bsiz=64)
3372    let mut readret: isize = 0; // c:4818 readret tracks last read return
3373                                // c:4824 dont_queue_signals(); c:4825 child_unblock(); — signal-queue
3374                                // dance keeps SIGCHLD live so the foreground process can be reaped
3375                                // while we drain. zshrs's in-process command-sub runs without the
3376                                // queue (no fork), but the C call surface is preserved for parity.
3377    dont_queue_signals(); // c:4824
3378    child_unblock(); // c:4825
3379    let mut inbuf = [0u8; 64]; // c:4815 inbuf[64]
3380    loop {
3381        // c:4826
3382        // c:4828 — `readret = read(in, inbuf, 64);`
3383        let r = unsafe { libc::read(in_fd, inbuf.as_mut_ptr() as *mut libc::c_void, inbuf.len()) };
3384        readret = r as isize;
3385        if readret <= 0 {
3386            // c:4829
3387            if readret < 0 && std::io::Error::last_os_error().raw_os_error() == Some(libc::EINTR) {
3388                // c:4830 — `if (readret < 0 && errno == EINTR) continue;`
3389                continue;
3390            }
3391            break; // c:4832
3392        }
3393        // c:4835 — `for (bufptr = inbuf; bufptr < inbuf + readret; bufptr++)`
3394        for i in 0..(readret as usize) {
3395            let c = inbuf[i];
3396            if crate::ported::ztype_h::imeta(c) {
3397                // c:4837 — `if (imeta(c)) { *ptr++ = Meta; c ^= 32; cnt++; }`
3398                buf.push(Meta as u8); // c:4838
3399                buf.push(c ^ 32); // c:4839 (Meta-encoded payload)
3400            } else {
3401                buf.push(c); // c:4848 *ptr++ = c
3402            }
3403        }
3404    }
3405    child_block(); // c:4854
3406                   // c:4855 — `if (readerror) *readerror = readret < 0 ? errno : 0;`
3407    *readerror = if readret < 0 {
3408        std::io::Error::last_os_error().raw_os_error().unwrap_or(0)
3409    } else {
3410        0
3411    };
3412    // c:4857 — `close(in);`
3413    unsafe {
3414        libc::close(in_fd);
3415    }
3416    // c:4858-4859 — `while (cnt && ptr[-1] == '\n') ptr--, cnt--;`
3417    while buf.last() == Some(&b'\n') {
3418        buf.pop();
3419    }
3420    // c:4861-4863 — qt branch: empty → Nularg sentinel; else single elem.
3421    let s = String::from_utf8_lossy(&buf).into_owned();
3422    if qt != 0 {
3423        // c:4861
3424        if buf.is_empty() {
3425            return vec![String::from(Nularg)]; // c:4862
3426        }
3427        return vec![s]; // c:4864
3428    }
3429    // c:4866-4871 — `spacesplit` + per-word GLOBSUBST `shtokenize`.
3430    let mut words = crate::ported::utils::spacesplit(&s, false); // c:4867
3431    if isset(crate::ported::zsh_h::GLOBSUBST) {
3432        // c:4870
3433        for w in words.iter_mut() {
3434            crate::ported::glob::shtokenize(w); // c:4870
3435        }
3436    }
3437    words
3438}
3439
3440/// Lex a `<(...)`/`>(...)`/`=(...)` body — the leading 2 chars are
3441/// the marker pair (`Inang+Inpar`, `Outang+Inpar`, `Equals+Inpar`),
3442/// remainder is the command up to the matching `Outpar`. Returns the
3443/// parsed Eprog (and writes the post-`)` cursor through `eptr`).
3444pub fn parsecmd(cmd: &str, eptr: Option<&mut usize>) -> Option<eprog> {
3445    // c:4878
3446    let bytes = cmd.as_bytes();
3447    // c:4883 — `for (str = cmd + 2; *str && *str != Outpar; str++);`
3448    if bytes.len() < 2 {
3449        return None;
3450    }
3451    let mut str_idx: usize = 2;
3452    while str_idx < bytes.len() && (bytes[str_idx] as char) != Outpar {
3453        str_idx += 1;
3454    }
3455    // c:4884 — `if (!*str || cmd[1] != Inpar)`.
3456    if str_idx >= bytes.len() || (bytes[1] as char) != Inpar {
3457        // c:4884
3458        let errstr = if bytes.len() >= 2 {
3459            untokenize(&cmd[..2]) // c:4891-4892
3460        } else {
3461            String::new()
3462        };
3463        zerr(&format!("unterminated `{}...)'", errstr)); // c:4893
3464        return None; // c:4894
3465    }
3466    // c:4896 — `*str = '\0';` — cmd[str_idx] becomes the terminator.
3467    // c:4897-4898 — `if (eptr) *eptr = str + 1;`
3468    if let Some(p) = eptr {
3469        *p = str_idx + 1;
3470    }
3471    // c:4899 — `parse_string(cmd + 2, 0)`.
3472    let body = &cmd[2..str_idx];
3473    let prog = parse_string(body, 0);
3474    if prog.is_none() {
3475        // c:4899
3476        zerr("parse error in process substitution"); // c:4900
3477        return None; // c:4901
3478    }
3479    prog // c:4903
3480}
3481
3482/// `POUNDBANGLIMIT` from `Src/exec.c:500` — max bytes read from the
3483/// front of a script when probing for a `#!` shebang line.
3484pub const POUNDBANGLIMIT: usize = 128;
3485
3486/// Port of `static char **makecline(LinkList list)` from `Src/exec.c:2046`.
3487///
3488/// Builds the argv array from a command's args list. The C version
3489/// allocates with a 4-slot prepad (2 reserved at the front for the
3490/// shebang `argv[-1]/argv[-2]` overwrite trick in zexecve) — Rust
3491/// doesn't need this since we rebuild the Vec on shebang re-exec
3492/// (see zexecve WARNING e).
3493///
3494/// XTRACE side-effect: each arg is printed via quotedzputs to xtrerr
3495/// (stderr), preceded by the PS4 prefix when first command of the line.
3496pub fn makecline(list: &[String]) -> Vec<String> {
3497    // c:2046
3498    if isset(XTRACE) {
3499        // c:2055
3500        if doneps4.load(Ordering::Relaxed) == 0 {
3501            // c:2056
3502            printprompt4(); // c:2057
3503        }
3504        let mut first = true;
3505        let mut err = std::io::stderr().lock();
3506        use std::io::Write;
3507        for s in list.iter() {
3508            // c:2059
3509            if !first {
3510                let _ = err.write_all(b" "); // c:2063
3511            }
3512            first = false;
3513            let _ = err.write_all(quotedzputs(s).as_bytes()); // c:2061
3514        }
3515        let _ = err.write_all(b"\n"); // c:2065
3516        let _ = err.flush(); // c:2066
3517    }
3518    list.to_vec() // c:2071-2072 — argv built; null terminator implicit in CString[] conversion
3519}
3520
3521/// Port of `static void execute(LinkList args, int flags, int defpath)`
3522/// from `Src/exec.c:723`. The canonical "child runs the simple
3523/// external command" path: STTY/ARGV0/BINF_DASH handling, makecline,
3524/// closem(FDT_XTRACE) + child_unblock, slash-path direct exec,
3525/// defpath (`command -p`) search, cmdnamtab + $PATH walk, with
3526/// commandnotfound-handler fallback and the final exit-code escape
3527/// (127 not-found / 126 noperm).
3528///
3529/// =================== WARNING — DIVERGENCE ====================
3530/// (a) `cmdnamtab->getnode(cmdnamtab, arg0)` (c:824) — HASHED
3531///     fast-path wired via cmdnamtab_lock(); jumps direct to
3532///     `cn.cmd` absolute path before the $PATH scan. Unhashed
3533///     cursor-walk (c:830-846) still falls to the full $PATH scan;
3534///     observable behavior matches C when the hash hit is HASHED.
3535/// (b) `commandnotfound(arg0, args)` (c:809, 873) calls into the
3536///     not-yet-ported `doshfunc` for the `command_not_found_handler`
3537///     shell function. Already routes through executor dispatch
3538///     (see exec.rs:2783).
3539/// (c) `_realexit()` (c:810, 874) — bare `std::process::exit`.
3540/// (d) `SHTTY` close on `!FD_CLOEXEC` (c:781-784) — Rust assumes
3541///     FD_CLOEXEC platform default (macOS, Linux).
3542/// (e) `path` Rust accessor uses paramtab lookup for "PATH";
3543///     `defpath` (`command -p`) walks DEFAULT_PATH via
3544///     search_defpath (already ported).
3545/// =============================================================
3546pub fn execute(args: &mut Vec<String>, flags: u32, defpath: i32) {
3547    // c:723
3548    let mut eno: i32 = 0;
3549    let mut ee: i32; // c:729
3550    let mut arg0 = if args.is_empty() {
3551        return;
3552    } else {
3553        args[0].clone()
3554    }; // c:731
3555       // c:733-748 — STTY pre-exec handling.
3556    {
3557        let mut stty = STTYval.lock().unwrap();
3558        if let Some(s) = stty.take() {
3559            // c:738 — STTYval = 0 to break recursion.
3560            if !s.is_empty()
3561                && unsafe { libc::isatty(0) } != 0
3562                && unsafe { libc::tcgetpgrp(0) } == unsafe { libc::getpid() }
3563            {
3564                drop(stty);
3565                let cmd = format!("stty {}", s); // c:739
3566                execstring(&cmd, 1, 0, "stty"); // c:743
3567            }
3568        }
3569    }
3570    // c:752-763 — ARGV0 override.
3571    if let Some(z) = zgetenv("ARGV0") {
3572        args[0] = z.clone(); // c:753
3573        unsafe {
3574            let key = std::ffi::CString::new("ARGV0").unwrap();
3575            libc::unsetenv(key.as_ptr()); // c:760
3576        }
3577        arg0 = args[0].clone();
3578    } else if (flags & BINF_DASH) != 0 {
3579        // c:764 — `BINF_DASH` prepends `-`.
3580        args[0] = format!("-{}", arg0); // c:767-768
3581        arg0 = args[0].clone();
3582    }
3583    let argv = makecline(args); // c:771
3584    let newenvp_owned: Option<Vec<String>> = if (flags & BINF_CLEARENV) != 0 {
3585        Some(Vec::new()) // c:772-773 — blank_env: char ** with only NULL slot
3586    } else {
3587        None
3588    };
3589    let newenvp = newenvp_owned.as_deref();
3590    closem(FDT_XTRACE, 0); // c:779
3591                           // c:780-785 — !FD_CLOEXEC SHTTY close — WARNING (d).
3592    child_unblock(); // c:786
3593    if arg0.len() >= libc::PATH_MAX as usize {
3594        // c:787
3595        zerr(&format!("command too long: {}", arg0)); // c:788
3596        unsafe {
3597            libc::_exit(1);
3598        } // c:789
3599    }
3600    // c:791-801 — slash in arg0 → direct exec.
3601    if let Some(slash_pos) = arg0.find('/') {
3602        let lerrno = zexecve(&arg0, &argv, newenvp); // c:793
3603        let is_dot = arg0.starts_with('.')
3604            && (slash_pos == 1 || (arg0.len() > 2 && &arg0[..2] == ".." && slash_pos == 2));
3605        if slash_pos == 0 || unset(PATHDIRS) || is_dot {
3606            // c:794
3607            zerr(&format!(
3608                "{}: {}",
3609                std::io::Error::from_raw_os_error(lerrno),
3610                arg0
3611            )); // c:797
3612            let code = if lerrno == libc::EACCES || lerrno == libc::ENOEXEC {
3613                126
3614            } else {
3615                127
3616            };
3617            unsafe {
3618                libc::_exit(code);
3619            } // c:798
3620        }
3621    }
3622    if defpath != 0 {
3623        // c:804 — `command -p` default-path search.
3624        let pbuf = match search_defpath(&arg0, libc::PATH_MAX as usize) {
3625            Some(p) => p, // c:808
3626            None => {
3627                if commandnotfound(&arg0, args) == 0 {
3628                    // c:809
3629                    unsafe {
3630                        libc::_exit(LASTVAL.load(Ordering::Relaxed));
3631                    }
3632                }
3633                zerr(&format!("command not found: {}", arg0)); // c:811
3634                unsafe {
3635                    libc::_exit(127);
3636                } // c:812
3637            }
3638        };
3639        ee = zexecve(&pbuf, &argv, newenvp); // c:815
3640        let dir = pbuf.rsplit_once('/').map(|(d, _)| d).unwrap_or("");
3641        if isgooderr(ee, if dir.is_empty() { "/" } else { dir }) {
3642            // c:819
3643            eno = ee;
3644        }
3645    } else {
3646        // c:822 — cmdnamtab fast-path: if `arg0` is a hashed cmdnam,
3647        // jump straight to the absolute path stored in `cn.cmd`,
3648        // skipping the full $PATH scan (one exec attempt vs N).
3649        // c:824 — `if ((cn = cmdnamtab->getnode(cmdnamtab, arg0)))`.
3650        let hashed_path: Option<String> = {
3651            let tab = cmdnamtab_lock().read().ok();
3652            tab.and_then(|t| {
3653                t.get(&arg0).and_then(|cn| {
3654                    if (cn.node.flags & crate::ported::zsh_h::HASHED) != 0 {
3655                        // c:827-828 — `strcpy(nn, cn->u.cmd);`
3656                        cn.cmd.clone()
3657                    } else {
3658                        None
3659                    }
3660                })
3661            })
3662        };
3663        if let Some(nn) = hashed_path {
3664            // c:848 — `ee = zexecve(nn, argv, newenvp);`
3665            ee = zexecve(&nn, &argv, newenvp);
3666            let dir = nn.rsplit_once('/').map(|(d, _)| d).unwrap_or("");
3667            if isgooderr(ee, if dir.is_empty() { "/" } else { dir }) {
3668                eno = ee;
3669            }
3670            // If the hashed entry's exec failed without a "good" error,
3671            // we still need the $PATH fallback — fall through.
3672            if eno == 0 && ee != 0 {
3673                // Reset for the $PATH scan below.
3674                ee = 0;
3675            }
3676        }
3677        // c:822 — normal $PATH scan (always runs; cmdnam fast-path was an
3678        // optimization but C also walks the rest of `path` if the hashed
3679        // exec failed with a non-"good" error).
3680        let path_str = getsparam("PATH").unwrap_or_default();
3681        for pp in path_str.split(':') {
3682            if pp.is_empty() || pp == "." {
3683                // c:856
3684                ee = zexecve(&arg0, &argv, newenvp); // c:857
3685                if isgooderr(ee, pp) {
3686                    eno = ee;
3687                }
3688            } else {
3689                // c:860
3690                let candidate = format!("{}/{}", pp, arg0); // c:861-864
3691                ee = zexecve(&candidate, &argv, newenvp); // c:865
3692                if isgooderr(ee, pp) {
3693                    eno = ee;
3694                }
3695            }
3696        }
3697    }
3698    // c:871-881 — final error reporting.
3699    if eno != 0 {
3700        // c:871
3701        zerr(&format!(
3702            "{}: {}",
3703            std::io::Error::from_raw_os_error(eno),
3704            arg0
3705        )); // c:872
3706    } else if commandnotfound(&arg0, args) == 0 {
3707        // c:873
3708        unsafe {
3709            libc::_exit(LASTVAL.load(Ordering::Relaxed));
3710        } // c:874
3711    } else {
3712        zerr(&format!("command not found: {}", arg0)); // c:876
3713    }
3714    let code = if eno == libc::EACCES || eno == libc::ENOEXEC {
3715        126
3716    } else {
3717        127
3718    }; // c:881
3719    unsafe {
3720        libc::_exit(code);
3721    }
3722}
3723
3724/// Port of `static int zexecve(char *pth, char **argv, char **newenvp)`
3725/// from `Src/exec.c:504`. Wraps `execve(2)` with:
3726///   - `$_` env var stamped to absolute `pth` (c:514-520)
3727///   - winch signal unblock right before the syscall (c:527)
3728///   - on `ENOEXEC` / `ENOENT`: reads the first POUNDBANGLIMIT
3729///     bytes, parses a `#!interp arg` shebang and re-execs the
3730///     interpreter (c:534-628). For `ENOEXEC` with no shebang,
3731///     binary-safety check then falls back to `/bin/sh script` per
3732///     POSIX (c:588-628).
3733///
3734/// Returns `errno` from the failing exec — execve only returns on
3735/// failure, so success means the calling process is already replaced.
3736///
3737/// =================== WARNING — DIVERGENCE ====================
3738/// (a) C uses `static char buf[PATH_MAX*2+1]` for the `_=...` env
3739///     string; Rust uses a stack `String` (consumed by `zputenv`).
3740/// (b) `closedumps()` for `!FD_CLOEXEC` (c:521-523) called
3741///     unconditionally as a no-op when FD_CLOEXEC is platform default.
3742/// (c) `unmetafy(pth, NULL)` / round-trip `metafy` at c:510-513,
3743///     c:639-642 — handled implicitly via &str ↔ CString.
3744/// (d) `metafy(execvebuf+2, -1, META_STATIC)` (c:551, 575) — we
3745///     drop the metafy and pass byte ranges to zerr directly.
3746/// (e) `argv[-1]` / `argv[-2]` shebang interpreter slot-overwriting
3747///     (C overwrites BEFORE `argv[0]`) — Rust rebuilds a fresh
3748///     `Vec<String>` with interp + optional arg + original argv tail
3749///     since Vec doesn't expose negative indexing.
3750/// (f) `environ` is FFI-loaded only when `newenvp` is None.
3751/// =============================================================
3752pub fn zexecve(pth: &str, argv: &[String], newenvp: Option<&[String]>) -> i32 {
3753    // c:504
3754    use std::ffi::CString;
3755    // c:514-520 — `_=pth` env stamping.
3756    let pth_abs = if pth.starts_with('/') {
3757        // c:516
3758        pth.to_string() // c:517
3759    } else {
3760        // c:518
3761        format!("{}/{}", getsparam("PWD").unwrap_or_default(), pth) // c:519
3762    };
3763    zputenv(&format!("_={}", pth_abs)); // c:520
3764    closedumps(); // c:522
3765    winch_unblock(); // c:527
3766    let cpth = match CString::new(pth) {
3767        Ok(c) => c,
3768        Err(_) => return libc::ENOENT,
3769    };
3770    let cargs: Vec<CString> = argv
3771        .iter()
3772        .filter_map(|a| CString::new(a.as_str()).ok())
3773        .collect();
3774    let mut argv_ptrs: Vec<*const libc::c_char> = cargs.iter().map(|c| c.as_ptr()).collect();
3775    argv_ptrs.push(std::ptr::null());
3776    let env_holder: Vec<CString>;
3777    let env_ptrs: Vec<*const libc::c_char>;
3778    let envp: *const *const libc::c_char = match newenvp {
3779        Some(env) => {
3780            env_holder = env
3781                .iter()
3782                .filter_map(|e| CString::new(e.as_str()).ok())
3783                .collect();
3784            env_ptrs = {
3785                let mut v: Vec<*const libc::c_char> =
3786                    env_holder.iter().map(|c| c.as_ptr()).collect();
3787                v.push(std::ptr::null());
3788                v
3789            };
3790            env_ptrs.as_ptr()
3791        }
3792        None => unsafe {
3793            extern "C" {
3794                static environ: *const *const libc::c_char;
3795            }
3796            environ
3797        },
3798    };
3799    unsafe {
3800        libc::execve(cpth.as_ptr(), argv_ptrs.as_ptr(), envp); // c:528
3801    }
3802    let eno = std::io::Error::last_os_error()
3803        .raw_os_error()
3804        .unwrap_or(libc::ENOEXEC); // c:534
3805    if eno == libc::ENOEXEC || eno == libc::ENOENT {
3806        // c:534
3807        let fd = unsafe { libc::open(cpth.as_ptr(), libc::O_RDONLY | libc::O_NOCTTY) }; // c:538
3808        if fd < 0 {
3809            return std::io::Error::last_os_error()
3810                .raw_os_error()
3811                .unwrap_or(libc::ENOENT); // c:634
3812        }
3813        let mut buf = vec![0u8; POUNDBANGLIMIT + 1]; // c:541
3814        let ct = unsafe {
3815            libc::read(
3816                fd,
3817                buf.as_mut_ptr() as *mut libc::c_void,
3818                POUNDBANGLIMIT as libc::size_t,
3819            )
3820        }; // c:542
3821        unsafe {
3822            libc::close(fd);
3823        } // c:543
3824        if ct >= 0 {
3825            // c:544
3826            let ct = ct as usize;
3827            if ct >= 2 && buf[0] == b'#' && buf[1] == b'!' {
3828                // c:545
3829                let mut t0 = 0;
3830                while t0 < ct && buf[t0] != b'\n' {
3831                    t0 += 1;
3832                } // c:546-548
3833                if t0 == ct {
3834                    // c:549
3835                    zerr(&format!(
3836                        // c:550
3837                        "{}: bad interpreter: {}: {}",
3838                        pth,
3839                        String::from_utf8_lossy(&buf[2..t0.min(ct)]),
3840                        std::io::Error::from_raw_os_error(eno)
3841                    ));
3842                } else {
3843                    // c:552
3844                    while t0 > 0 && (buf[t0] == b' ' || buf[t0] == b'\t' || buf[t0] == b'\n') {
3845                        buf[t0] = 0;
3846                        t0 -= 1;
3847                    } // c:553-554
3848                    let mut ptr_lo: usize = 2;
3849                    while ptr_lo < buf.len() && buf[ptr_lo] == b' ' {
3850                        ptr_lo += 1;
3851                    } // c:555
3852                    let ptr2_lo = ptr_lo;
3853                    let mut ptr_hi = ptr2_lo;
3854                    while ptr_hi < buf.len() && buf[ptr_hi] != 0 && buf[ptr_hi] != b' ' {
3855                        ptr_hi += 1;
3856                    } // c:556
3857                    let interp_str = String::from_utf8_lossy(&buf[ptr2_lo..ptr_hi]).into_owned();
3858                    if eno == libc::ENOENT {
3859                        // c:557 — pathprog rewrite path.
3860                        let pprog = if !interp_str.starts_with('/') {
3861                            // c:561
3862                            pathprog(&interp_str).map(|p| p.display().to_string())
3863                        } else {
3864                            None
3865                        };
3866                        if let Some(pprog) = pprog {
3867                            // c:562
3868                            let mut argv_new: Vec<String> = Vec::with_capacity(argv.len() + 2);
3869                            argv_new.push(interp_str.clone()); // c:564
3870                            if ptr_hi >= buf.len() || buf[ptr_hi] == 0 {
3871                                argv_new.push(pth.to_string());
3872                            } else {
3873                                // c:567
3874                                let mut rest_lo = ptr_hi + 1;
3875                                while rest_lo < buf.len() && buf[rest_lo] == b' ' {
3876                                    rest_lo += 1;
3877                                }
3878                                let mut rest_hi = rest_lo;
3879                                while rest_hi < buf.len() && buf[rest_hi] != 0 {
3880                                    rest_hi += 1;
3881                                }
3882                                let arg_str =
3883                                    String::from_utf8_lossy(&buf[rest_lo..rest_hi]).into_owned();
3884                                argv_new.push(arg_str);
3885                                argv_new.push(pth.to_string());
3886                            }
3887                            for orig in argv.iter().skip(1) {
3888                                argv_new.push(orig.clone());
3889                            }
3890                            winch_unblock(); // c:565/c:570
3891                            return zexecve(&pprog, &argv_new, newenvp); // c:566/c:571
3892                        }
3893                        zerr(&format!(
3894                            // c:574
3895                            "{}: bad interpreter: {}: {}",
3896                            pth,
3897                            interp_str,
3898                            std::io::Error::from_raw_os_error(eno)
3899                        ));
3900                    } else if ptr_hi < buf.len() && buf[ptr_hi] != 0 {
3901                        // c:576
3902                        let mut rest_lo = ptr_hi + 1;
3903                        while rest_lo < buf.len() && buf[rest_lo] == b' ' {
3904                            rest_lo += 1;
3905                        }
3906                        let mut rest_hi = rest_lo;
3907                        while rest_hi < buf.len() && buf[rest_hi] != 0 {
3908                            rest_hi += 1;
3909                        }
3910                        let arg_str = String::from_utf8_lossy(&buf[rest_lo..rest_hi]).into_owned();
3911                        let mut argv_new: Vec<String> =
3912                            vec![interp_str.clone(), arg_str, pth.to_string()];
3913                        for orig in argv.iter().skip(1) {
3914                            argv_new.push(orig.clone());
3915                        }
3916                        winch_unblock(); // c:580
3917                        return zexecve(&interp_str, &argv_new, newenvp); // c:581
3918                    } else {
3919                        // c:582
3920                        let mut argv_new: Vec<String> = vec![interp_str.clone(), pth.to_string()];
3921                        for orig in argv.iter().skip(1) {
3922                            argv_new.push(orig.clone());
3923                        }
3924                        winch_unblock(); // c:584
3925                        return zexecve(&interp_str, &argv_new, newenvp); // c:585
3926                    }
3927                }
3928            } else if eno == libc::ENOEXEC {
3929                // c:588 — binary-safety + /bin/sh fallback.
3930                let nul_pos = buf[..ct].iter().position(|&b| b == 0); // c:597
3931                let isbinary = match nul_pos {
3932                    None => false, // c:598
3933                    Some(npos) => {
3934                        let mut has_letter = false;
3935                        let mut binary = true;
3936                        for &b in &buf[..npos] {
3937                            // c:602-609
3938                            if (b as char).is_ascii_lowercase() || b == b'$' || b == b'`' {
3939                                has_letter = true;
3940                            }
3941                            if has_letter && b == b'\n' {
3942                                binary = false; // c:606
3943                                break;
3944                            }
3945                        }
3946                        binary
3947                    }
3948                };
3949                if !isbinary {
3950                    // c:611
3951                    let mut argv_new: Vec<String> = Vec::with_capacity(argv.len() + 2);
3952                    argv_new.push("sh".to_string()); // c:625
3953                    if !argv.is_empty() && (argv[0].starts_with('-') || argv[0].starts_with('+')) {
3954                        argv_new.push("-".to_string()); // c:623
3955                    }
3956                    for orig in argv.iter() {
3957                        argv_new.push(orig.clone());
3958                    }
3959                    winch_unblock(); // c:626
3960                    return zexecve("/bin/sh", &argv_new, newenvp); // c:627
3961                }
3962            }
3963        }
3964    }
3965    eno // c:643
3966}
3967
3968/// Port of `char *getoutputfile(char *cmd, char **eptr)` from
3969/// `Src/exec.c:4910` — `=(cmd)` process substitution.
3970///
3971/// Substitutes the cmd's stdout into a temp file, returns the
3972/// filename. Optimised path: `=(<<<heredoc-str)` writes the
3973/// heredoc body directly without a fork.
3974///
3975/// (a) `addfilelist(nam, 0)` (c:4960) wired via `JOBTAB[thisjob]`
3976///     so the temp file gets cleaned at job exit.
3977/// (b) `waitforpid` Rust takes 1 arg `pid`, C takes `(pid, full)`.
3978///     Behavior matches the `full=0` case anyway.
3979/// (c) `entersubsh` is ported at exec.rs:3934 — wire it here when
3980///     re-routing the fork path away from setsid-only fallback.
3981/// (d) `execode` is now ported (exec.rs:6047) — the body still
3982///     re-feeds through fusevm for cache coherence with execstring.
3983/// (e) `_realexit` flushes stdio + jobs + history. We use bare
3984///     `std::process::exit(0)` for now.
3985/// (f) TMPSUFFIX link()-rename block (c:4951-4958) deferred; rare
3986///     `setopt suffix_alias` interaction with =(…).
3987pub fn getoutputfile(cmd: &str, eptr: Option<&mut usize>) -> Option<String> {
3988    // c:4910
3989    let bytes = cmd.as_bytes();
3990    let _ = bytes;
3991    // c:4918 — `if (thisjob == -1)` — guard removed (thisjob model differs).
3992    let mut ends_at: usize = 0;
3993    let prog = parsecmd(cmd, Some(&mut ends_at))?; // c:4922
3994    if let Some(p) = eptr {
3995        *p = ends_at;
3996    }
3997    let mut nam = gettempname(None, true)?; // c:4924
3998                                            // c:4927 — `simple_redir_name` opt for `=(<<<str)`.
3999    let mut s: Option<String> = simple_redir_name(&prog, REDIR_HERESTR).map(|raw| {
4000        // c:4933
4001        let mut sub = singsub(&raw); // c:4933
4002        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
4003            // c:4934
4004            String::new() // c:4935 — sentinel; checked below
4005        } else {
4006            sub = untokenize(&sub); // c:4937
4007            dyncat(&sub, "\n") // c:4938
4008        }
4009    });
4010    if let Some(ref sv) = s {
4011        if sv.is_empty() {
4012            s = None;
4013        }
4014    }
4015    if s.is_none() {
4016        // c:4942
4017        child_block(); // c:4943
4018    }
4019    // c:4945 — `open(nam, O_WRONLY|O_CREAT|O_EXCL|O_NOCTTY, 0600)`.
4020    let c_nam = match std::ffi::CString::new(nam.clone()) {
4021        Ok(c) => c,
4022        Err(_) => {
4023            if s.is_none() {
4024                child_unblock();
4025            }
4026            return None;
4027        }
4028    };
4029    let fd = unsafe {
4030        libc::open(
4031            c_nam.as_ptr(),
4032            libc::O_WRONLY | libc::O_CREAT | libc::O_EXCL | libc::O_NOCTTY,
4033            0o600 as libc::c_uint,
4034        )
4035    };
4036    if fd < 0 {
4037        // c:4945
4038        zerr(&format!(
4039            "process substitution failed: {}",
4040            std::io::Error::last_os_error()
4041        )); // c:4946
4042        if s.is_none() {
4043            child_unblock(); // c:4948
4044        }
4045        return None; // c:4949
4046    }
4047    // c:4951-4958 — TMPSUFFIX link block (see WARNING f).
4048    // c:4960 — `addfilelist(nam, 0);` — register temp file in current
4049    // job's filelist so it's unlinked at job exit (not relying on the
4050    // OS temp-reaper).
4051    if let Some(jt) = JOBTAB.get() {
4052        let mut guard = jt.lock().unwrap();
4053        let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
4054        if tj >= 0 {
4055            if let Some(j) = guard.get_mut(tj as usize) {
4056                crate::ported::jobs::addfilelist(j, Some(&nam), 0);
4057            }
4058        }
4059    }
4060    if let Some(sv) = s {
4061        // c:4962 — optimised here-string write path.
4062        let mut buf: Vec<u8> = sv.into_bytes();
4063        let _len = unmetafy(&mut buf); // c:4965
4064        let _ = write_loop(fd, &buf); // c:4966
4065        unsafe {
4066            libc::close(fd);
4067        } // c:4967
4068        return Some(nam); // c:4968
4069    }
4070    // c:4971 — `cmdoutpid = pid = zfork(NULL)`.
4071    let pid = zfork(None);
4072    cmdoutpid.store(pid, Ordering::Relaxed);
4073    if pid == -1 {
4074        // c:4972
4075        unsafe {
4076            libc::close(fd);
4077        } // c:4973
4078        child_unblock(); // c:4974
4079        return Some(nam); // c:4975
4080    } else if pid != 0 {
4081        // c:4976 — parent.
4082        unsafe {
4083            libc::close(fd);
4084        } // c:4977
4085        let _ = waitforpid(pid); // c:4978
4086        cmdoutval.store(0, Ordering::Relaxed); // c:4979
4087        return Some(nam); // c:4980
4088    }
4089    // c:4983 — child.
4090    closem(FDT_UNUSED, 0); // c:4984
4091    let _ = redup(fd, 1); // c:4985
4092    entersubsh(esub::PGRP | esub::NOMONITOR, None); // c:4986
4093    cmdpush(CS_CMDSUBST as u8); // c:4987
4094                                // c:4988 — execode — WARNING (d).
4095    let body_end = if ends_at > 0 { ends_at - 1 } else { 2 };
4096    let body = if body_end > 2 && body_end <= cmd.len() {
4097        &cmd[2..body_end]
4098    } else {
4099        ""
4100    };
4101    let _ = crate::ported::exec::execute_script_zsh_pipeline(body);
4102    cmdpop(); // c:4989
4103    unsafe {
4104        libc::close(1);
4105    } // c:4990
4106      // _realexit — WARNING (e)
4107    std::process::exit(0); // c:4991
4108    #[allow(unreachable_code)]
4109    {
4110        // c:4992-4993 — `zerr("exit returned in child!!"); kill(getpid(), SIGKILL);`
4111        let _ = &mut nam;
4112        unsafe {
4113            libc::kill(libc::getpid(), libc::SIGKILL);
4114        }
4115        None
4116    }
4117}
4118
4119/// Port of `char *getproc(char *cmd, char **eptr)` from
4120/// `Src/exec.c:5025` — `<(cmd)` / `>(cmd)` process substitution
4121/// via `/dev/fd/N` (PATH_DEV_FD branch; modern Linux/macOS).
4122///
4123/// (a) PATH_DEV_FD branch only — the FIFO fallback (`!PATH_DEV_FD`
4124///     path c:5037-5064) is omitted; modern Linux/macOS both
4125///     provide /dev/fd. `namedpipe()` is ported (exec.rs:2701) but
4126///     unused here.
4127/// (b) `addproc` is 7-arg; procsubst pid recorded via aux=true on
4128///     the current job (c:5141-5142).
4129/// (c) `addfilelist(NULL, fd)` wired via `JOBTAB[thisjob]` at
4130///     c:5087.
4131/// (d) `entersubsh` is ported at exec.rs:3934 — wired below at
4132///     c:5063 (`entersubsh(ESUB_ASYNC|ESUB_PGRP, NULL)`).
4133/// (e) `execode` is ported at exec.rs:6047. Body still re-feeds
4134///     through fusevm for cache coherence.
4135/// (f) `_realexit` flushes stdio + jobs + history. We use bare
4136///     `std::process::exit(LASTVAL)` for now.
4137/// (g) `fdtable[fd] = FDT_PROC_SUBST` (c:5086) — set via fdtable_set.
4138pub fn getproc(cmd: &str, eptr: Option<&mut usize>) -> Option<String> {
4139    // c:5025
4140    let bytes = cmd.as_bytes();
4141    let out: i32 = if !bytes.is_empty() && (bytes[0] as char) == Inang {
4142        1 // c:5032 — `<(...)` writer-side child
4143    } else {
4144        0
4145    };
4146    // c:5068-5071 — `if (thisjob == -1) { zerr(...); return NULL; }` —
4147    // proc subst needs a host job to attach the child to.
4148    let tj_check = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
4149    if tj_check == -1 {
4150        zerr(&format!("process substitution {} cannot be used here", cmd)); // c:5069
4151        return None; // c:5070
4152    }
4153    // c:5072 — PATH_DEV_FD path: allocate buffer for the /dev/fd/N string.
4154    let mut ends_at: usize = 0;
4155    let _prog = parsecmd(cmd, Some(&mut ends_at))?; // c:5073
4156    if let Some(p) = eptr {
4157        *p = ends_at;
4158    }
4159    let mut pipes: [i32; 2] = [-1; 2];
4160    if mpipe(&mut pipes) < 0 {
4161        // c:5075
4162        return None;
4163    }
4164    let mut bgtime: ZshTimespec = libc::timespec {
4165        tv_sec: 0,
4166        tv_nsec: 0,
4167    };
4168    let pid = zfork(Some(&mut bgtime)); // c:5077
4169    if pid != 0 {
4170        // c:5077 — parent path.
4171        let pnam = format!("/dev/fd/{}", pipes[(1 - out) as usize]); // c:5078
4172        let _ = zclose(pipes[out as usize]); // c:5079
4173        if pid == -1 {
4174            // c:5080
4175            let _ = zclose(pipes[(1 - out) as usize]); // c:5082
4176            return None; // c:5083
4177        }
4178        let fd = pipes[(1 - out) as usize]; // c:5085
4179        fdtable_set(fd, FDT_PROC_SUBST); // c:5086
4180                                         // c:5087 — `addfilelist(NULL, fd);` — register the proc-subst
4181                                         // pipe fd in the current job's filelist so it's closed at job exit.
4182        if let Some(jt) = JOBTAB.get() {
4183            let mut guard = jt.lock().unwrap();
4184            let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
4185            if tj >= 0 {
4186                if let Some(j) = guard.get_mut(tj as usize) {
4187                    crate::ported::jobs::addfilelist(j, None, fd);
4188                }
4189            }
4190        }
4191        // c:5088-5091 — `if (!out) addproc(pid, NULL, 1, &bgtime, -1, -1);` —
4192        // record the proc-subst writer-side child in the job's
4193        // auxprocs (aux=true). For `<(cmd)` (out==1 = reader-side
4194        // child), C omits the addproc — symmetric here.
4195        if out == 0 {
4196            if let Some(jt) = JOBTAB.get() {
4197                let mut guard = jt.lock().unwrap();
4198                let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
4199                if tj >= 0 {
4200                    if let Some(j) = guard.get_mut(tj as usize) {
4201                        crate::ported::jobs::addproc(
4202                            j,
4203                            pid,
4204                            "",
4205                            true,
4206                            Some(std::time::Instant::now()),
4207                            -1,
4208                            -1,
4209                        );
4210                    }
4211                }
4212            }
4213        }
4214        procsubstpid.store(pid, Ordering::Relaxed); // c:5092
4215        return Some(pnam); // c:5093
4216    }
4217    // c:5095 — child.
4218    entersubsh(esub::ASYNC | esub::PGRP, None); // c:5095
4219    let _ = redup(pipes[out as usize], out); // c:5096
4220    closem(FDT_UNUSED, 0); // c:5097
4221    cmdpush(CS_CMDSUBST as u8); // c:5100
4222    let body_end = if ends_at > 0 { ends_at - 1 } else { 2 };
4223    let body = if body_end > 2 && body_end <= cmd.len() {
4224        &cmd[2..body_end]
4225    } else {
4226        ""
4227    };
4228    let _ = crate::ported::exec::execute_script_zsh_pipeline(body);
4229    cmdpop(); // c:5102
4230    let _ = zclose(out); // c:5103
4231    std::process::exit(LASTVAL.load(Ordering::Relaxed)); // c:5104
4232}
4233
4234/// Port of `enum { ESUB_ASYNC, ESUB_PGRP, ... };` from `Src/exec.c:1056`.
4235/// Flag bits for `entersubsh(int flags, struct entersubsh_ret *retp)`.
4236pub mod esub {
4237    // c:1056
4238    /// `ASYNC` constant.
4239    pub const ASYNC: i32 = 0x01; // c:1058
4240    /// `PGRP` constant.
4241    pub const PGRP: i32 = 0x02; // c:1063
4242    /// `KEEPTRAP` constant.
4243    pub const KEEPTRAP: i32 = 0x04; // c:1065
4244    /// `FAKE` constant.
4245    pub const FAKE: i32 = 0x08; // c:1067
4246    /// `REVERTPGRP` constant.
4247    pub const REVERTPGRP: i32 = 0x10; // c:1069
4248    /// `NOMONITOR` constant.
4249    pub const NOMONITOR: i32 = 0x20; // c:1071
4250    /// `JOB_CONTROL` constant.
4251    pub const JOB_CONTROL: i32 = 0x40; // c:1073
4252}
4253
4254/// Port of `struct entersubsh_ret` from `Src/exec.c` (forward decl).
4255/// Out-arg used by `entersubsh()` to hand back the group-leader pid
4256/// and the list-pipe job index the parent should track. Only filled
4257/// in for `ESUB_PGRP` + non-async forks (synchronous pipeline child
4258/// groups).
4259#[allow(non_camel_case_types)]
4260#[derive(Default)]
4261pub struct entersubsh_ret {
4262    pub gleader: i32,       // c:1122
4263    pub list_pipe_job: i32, // c:1123
4264}
4265
4266/// Port of `static void entersubsh(int flags, struct entersubsh_ret *retp)`
4267/// from `Src/exec.c:1083`. Called by every child fork to switch the
4268/// process into subshell mode: traps reset, monitor disabled, signals
4269/// re-defaulted, pgrp + tty handed off, saved fds closed, jobtab
4270/// cleared, ZSH_SUBSHELL bumped, forklevel = locallevel.
4271///
4272/// (a) `jobtab[list_pipe_job]` / `jobtab[thisjob]` pgrp ops (c:1110-
4273///     1151) are now ported via `JOBTAB[thisjob]`.gleader access; the
4274///     ESUB_PGRP+sync path establishes pipeline group-leadership
4275///     (list_pipe_job inherit or thisjob-as-leader), filling
4276///     entersubsh_ret with the chosen gleader + list_pipe_job index.
4277/// (b) `clearjobtab(monitor)` (c:1219) — Rust signature is
4278///     `clearjobtab(&mut JobTable, monitor)`; we get the global table
4279///     via a TABLE handle similar to other jobs.rs entries.
4280/// (c) `attachtty(...)` (c:1119, 1144) — wired via libc::tcsetpgrp(2, gleader).
4281/// (d) `release_pgrp()` called for ESUB_REVERTPGRP when `getpid() ==
4282///     mypgrp` — direct C parity (jobs.rs:3406 provides the call).
4283/// (e) `opts[USEZLE] = 0; zleactive = 0` — Rust opts table lookup
4284///     uses `opts_set_off(USEZLE)`; zleactive is the atomic in
4285///     builtins/sched.rs.
4286/// =============================================================
4287pub fn entersubsh(flags: i32, retp: Option<&mut entersubsh_ret>) {
4288    // c:1083
4289    let monitor: i32;
4290    let job_control_ok: i32;
4291    // c:1088-1092 — reset traps unless KEEPTRAP.
4292    if (flags & esub::KEEPTRAP) == 0 {
4293        // c:1088
4294        for sig in 0..=SIGCOUNT {
4295            // c:1089
4296            let st = {
4297                let guard = sigtrapped.lock().unwrap();
4298                guard.get(sig as usize).copied().unwrap_or(0)
4299            };
4300            let func_set = (st & ZSIG_FUNC) != 0; // c:1090
4301            let posix_ignored = isset(POSIXTRAPS) && ((st & ZSIG_IGNORED) != 0); // c:1091
4302            if !func_set && !posix_ignored {
4303                unsettrap(sig); // c:1092
4304            }
4305        }
4306    }
4307    monitor = if isset(MONITOR) { 1 } else { 0 }; // c:1093
4308    job_control_ok = if monitor != 0 && (flags & esub::JOB_CONTROL) != 0 && isset(POSIXJOBS) {
4309        // c:1094
4310        1
4311    } else {
4312        0
4313    };
4314    EXIT_VAL.store(0, Ordering::Relaxed); // c:1095
4315    if (flags & esub::NOMONITOR) != 0 {
4316        // c:1096
4317        dosetopt(MONITOR, 0, 0); // c:1097
4318    }
4319    if !isset(MONITOR) {
4320        // c:1098
4321        if (flags & esub::ASYNC) != 0 {
4322            // c:1099
4323            let _ = settrap(libc::SIGINT, None, 0); // c:1100
4324            let _ = settrap(libc::SIGQUIT, None, 0); // c:1101
4325            if unsafe { libc::isatty(0) } != 0 {
4326                // c:1102
4327                unsafe {
4328                    libc::close(0);
4329                } // c:1103
4330                let devnull = std::ffi::CString::new("/dev/null").unwrap();
4331                if unsafe { libc::open(devnull.as_ptr(), libc::O_RDWR | libc::O_NOCTTY) } != 0 {
4332                    // c:1104
4333                    zerr(&format!(
4334                        // c:1105
4335                        "can't open /dev/null: {}",
4336                        std::io::Error::last_os_error()
4337                    ));
4338                    unsafe {
4339                        libc::_exit(1);
4340                    } // c:1106
4341                }
4342            }
4343        }
4344    } else if (flags & esub::PGRP) != 0 {
4345        // c:1110 — `else if (thisjob != -1 && (flags & ESUB_PGRP))`.
4346        let thisjob = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
4347        if thisjob != -1 {
4348            let lpj = list_pipe_job.load(Ordering::Relaxed);
4349            let lp = list_pipe.load(Ordering::Relaxed);
4350            let lpc = list_pipe_child.load(Ordering::Relaxed);
4351            if let Some(jt) = JOBTAB.get() {
4352                let mut guard = jt.lock().unwrap();
4353                let lpj_gleader = guard.get(lpj as usize).map(|j| j.gleader).unwrap_or(0);
4354                if lpj_gleader != 0 && (lp != 0 || lpc != 0) {
4355                    // c:1111-1124 — inherit list_pipe_job's group leader.
4356                    let pgid = if unsafe { libc::setpgid(0, lpj_gleader) } == -1
4357                        || (unsafe { libc::killpg(lpj_gleader, 0) } == -1
4358                            && std::io::Error::last_os_error().raw_os_error() == Some(libc::ESRCH))
4359                    {
4360                        // c:1115-1117 — primary group leader gone; this child becomes leader.
4361                        let new_gl = if lpc != 0 {
4362                            mypgrp.load(Ordering::Relaxed)
4363                        } else {
4364                            unsafe { libc::getpid() }
4365                        };
4366                        if let Some(j) = guard.get_mut(lpj as usize) {
4367                            j.gleader = new_gl;
4368                        }
4369                        if let Some(j) = guard.get_mut(thisjob as usize) {
4370                            j.gleader = new_gl;
4371                        }
4372                        unsafe { libc::setpgid(0, new_gl) };
4373                        if (flags & esub::ASYNC) == 0 {
4374                            unsafe { libc::tcsetpgrp(2, new_gl) }; // c:1119 attachtty
4375                        }
4376                        new_gl
4377                    } else {
4378                        lpj_gleader
4379                    };
4380                    if let Some(r) = retp {
4381                        if (flags & esub::ASYNC) == 0 {
4382                            r.gleader = pgid; // c:1122
4383                            r.list_pipe_job = lpj; // c:1123
4384                        }
4385                    }
4386                } else {
4387                    // c:1126-1151 — standard group-leader-takeover path.
4388                    let thisjob_gleader =
4389                        guard.get(thisjob as usize).map(|j| j.gleader).unwrap_or(0);
4390                    if thisjob_gleader == 0 || unsafe { libc::setpgid(0, thisjob_gleader) } == -1 {
4391                        let new_gl = unsafe { libc::getpid() };
4392                        if let Some(j) = guard.get_mut(thisjob as usize) {
4393                            j.gleader = new_gl; // c:1138
4394                        }
4395                        if lpj != thisjob {
4396                            let lpj_was_unset = guard
4397                                .get(lpj as usize)
4398                                .map(|j| j.gleader == 0)
4399                                .unwrap_or(true);
4400                            if lpj_was_unset {
4401                                if let Some(j) = guard.get_mut(lpj as usize) {
4402                                    j.gleader = new_gl; // c:1140-1141
4403                                }
4404                            }
4405                        }
4406                        unsafe { libc::setpgid(0, new_gl) }; // c:1142
4407                        if (flags & esub::ASYNC) == 0 {
4408                            unsafe { libc::tcsetpgrp(2, new_gl) }; // c:1144 attachtty
4409                            if let Some(r) = retp {
4410                                r.gleader = new_gl; // c:1146
4411                                if lpj != thisjob {
4412                                    r.list_pipe_job = lpj; // c:1148
4413                                }
4414                            }
4415                        }
4416                    }
4417                }
4418            }
4419        } else {
4420            // No real job slot; basic setpgid fallback.
4421            unsafe { libc::setpgid(0, 0) };
4422        }
4423    }
4424    if (flags & esub::FAKE) == 0 {
4425        // c:1153
4426        subsh.store(1, Ordering::Relaxed); // c:1154
4427    }
4428    // c:1161 — `zsh_subshell++;` regardless of FAKE.
4429    zsh_subshell.fetch_add(1, Ordering::Relaxed);
4430    // c:1162 — `if ((flags & ESUB_REVERTPGRP) && getpid() == mypgrp)`.
4431    if (flags & esub::REVERTPGRP) != 0
4432        && unsafe { libc::getpid() } == mypgrp.load(Ordering::Relaxed)
4433    {
4434        release_pgrp(); // c:1163
4435    }
4436    *shout.lock().unwrap() = 0; // c:1164 — shout = NULL
4437    if (flags & esub::NOMONITOR) != 0 {
4438        // c:1165
4439        signal_ignore(libc::SIGTTOU); // c:1171
4440        signal_ignore(libc::SIGTTIN); // c:1172
4441        signal_ignore(libc::SIGTSTP); // c:1173
4442    } else if job_control_ok == 0 {
4443        // c:1174
4444        signal_default(libc::SIGTTOU); // c:1181
4445        signal_default(libc::SIGTTIN); // c:1182
4446        signal_default(libc::SIGTSTP); // c:1183
4447    }
4448    let interact = isset(INTERACTIVE); // c:1185 — Rust uses INTERACTIVE option as proxy
4449    if interact {
4450        signal_default(libc::SIGTERM); // c:1186
4451        let int_st = sigtrapped
4452            .lock()
4453            .unwrap()
4454            .get(libc::SIGINT as usize)
4455            .copied()
4456            .unwrap_or(0);
4457        if (int_st & ZSIG_IGNORED) == 0 {
4458            // c:1187
4459            signal_default(libc::SIGINT); // c:1188
4460        }
4461        let pipe_st = sigtrapped
4462            .lock()
4463            .unwrap()
4464            .get(libc::SIGPIPE as usize)
4465            .copied()
4466            .unwrap_or(0);
4467        if pipe_st == 0 {
4468            // c:1189
4469            signal_default(libc::SIGPIPE); // c:1190
4470        }
4471    }
4472    let quit_st = sigtrapped
4473        .lock()
4474        .unwrap()
4475        .get(libc::SIGQUIT as usize)
4476        .copied()
4477        .unwrap_or(0);
4478    if (quit_st & ZSIG_IGNORED) == 0 {
4479        // c:1192
4480        signal_default(libc::SIGQUIT); // c:1193
4481    }
4482    // c:1202-1205 — unblock any trapped signals while in `intrap`.
4483    if intrap.load(Ordering::Relaxed) != 0 {
4484        // c:1202
4485        for sig in 1..=SIGCOUNT {
4486            let st = sigtrapped
4487                .lock()
4488                .unwrap()
4489                .get(sig as usize)
4490                .copied()
4491                .unwrap_or(0);
4492            if st != 0 && st != ZSIG_IGNORED {
4493                // c:1204
4494                let m = signal_mask(sig);
4495                let _ = signal_unblock(&m); // c:1205
4496            }
4497        }
4498    }
4499    if job_control_ok == 0 {
4500        // c:1206
4501        dosetopt(MONITOR, 0, 0); // c:1207
4502    }
4503    dosetopt(USEZLE, 0, 0); // c:1208
4504    zleactive.store(0, Ordering::Relaxed); // c:1209
4505                                           // c:1214-1217 — close saved fds.
4506    let max = MAX_ZSH_FD.load(Ordering::Relaxed);
4507    for i in 10..=max {
4508        if (fdtable_get(i) & FDT_SAVED_MASK) != 0 {
4509            // c:1215
4510            let _ = zclose(i); // c:1216
4511        }
4512    }
4513    // c:1218-1219 — `clearjobtab(monitor);` — calls the canonical port
4514    // at jobs.rs:1695 which handles ALL the C body including the
4515    // oldjobtab snapshot path (c:1799-1817) under POSIXJOBS guard.
4516    let mut dummy_table = crate::exec_jobs::JobTable::new();
4517    crate::ported::jobs::clearjobtab(&mut dummy_table, monitor);
4518    let _ = get_usage(); // c:1220
4519    FORKLEVEL.store(
4520        // c:1221 — `forklevel = locallevel;`
4521        locallevel.load(Ordering::Relaxed),
4522        Ordering::Relaxed,
4523    );
4524}
4525
4526/// Port of `static int getpipe(char *cmd, int nullexec)` from
4527/// `Src/exec.c:5119`.
4528///
4529/// C body executes `<(cmd)` / `>(cmd)` process substitution via a
4530/// pipe pair: parent gets back the readable (`<(...)`) or writable
4531/// (`>(...)`) end as an fd; child runs the substituted command with
4532/// its stdio redirected into the other end.
4533///
4534/// ```c
4535/// Eprog prog;
4536/// int pipes[2], out = *cmd == Inang;
4537/// pid_t pid;
4538/// struct timespec bgtime;
4539/// char *ends;
4540/// if (!(prog = parsecmd(cmd, &ends))) return -1;
4541/// if (*ends) { zerr("invalid syntax..."); return -1; }
4542/// if (mpipe(pipes) < 0) return -1;
4543/// if ((pid = zfork(&bgtime))) {
4544///     zclose(pipes[out]);
4545///     if (pid == -1) { zclose(pipes[!out]); return -1; }
4546///     if (!nullexec) addproc(pid, NULL, 1, &bgtime, -1, -1);
4547///     procsubstpid = pid;
4548///     return pipes[!out];
4549/// }
4550/// entersubsh(ESUB_ASYNC|ESUB_PGRP|ESUB_NOMONITOR, NULL);
4551/// redup(pipes[out], out);
4552/// closem(FDT_UNUSED, 0);
4553/// cmdpush(CS_CMDSUBST);
4554/// execode(prog, 0, 1, out ? "outsubst" : "insubst");
4555/// cmdpop();
4556/// _realexit();
4557/// ```
4558///
4559/// (a) `addproc` is now 7-arg (jobs.rs:1516) — wired at the
4560///     procsubst pid recording site (c:5141-5142) earlier this
4561///     session; the child IS now recorded in `JOBTAB[thisjob]`.
4562/// (b) `entersubsh` IS now ported (exec.rs:3934) including the
4563///     ESUB_PGRP pipeline group-leadership path — wired this
4564///     session for getpipe's `entersubsh(ESUB_ASYNC|ESUB_PGRP|
4565///     ESUB_NOMONITOR, NULL)` call.
4566/// (c) `execode(prog, ...)` IS now ported (exec.rs:6047) — getpipe
4567///     can route through execode for the parsed eprog. Currently
4568///     this caller still uses the fusevm pipeline for cache
4569///     coherence with execstring; switch over when the wordcode
4570///     walker becomes the primary path.
4571/// (d) `_realexit()` flushes stdio + jobs + history. We use bare
4572///     `std::process::exit(lastval)` for now.
4573pub fn getpipe(cmd: &str, nullexec: i32) -> i32 {
4574    // c:5119
4575    let bytes = cmd.as_bytes();
4576    let out: i32 = if !bytes.is_empty() && (bytes[0] as char) == Inang {
4577        1 // c:5122 — `<(...)` reads from child, child writes to fd 1
4578    } else {
4579        0 // `>(...)` — child reads from fd 0
4580    };
4581    let mut ends_at: usize = 0;
4582    let prog = parsecmd(cmd, Some(&mut ends_at)); // c:5127
4583    if prog.is_none() {
4584        // c:5127
4585        return -1; // c:5128
4586    }
4587    // c:5129 — `if (*ends)` — trailing bytes after the `)` are invalid.
4588    if ends_at < bytes.len() && bytes[ends_at] != 0 {
4589        zerr("invalid syntax for process substitution in redirection"); // c:5130
4590        return -1; // c:5131
4591    }
4592    let mut pipes: [i32; 2] = [-1; 2];
4593    if mpipe(&mut pipes) < 0 {
4594        // c:5133
4595        return -1;
4596    }
4597    // c:5135 — `if ((pid = zfork(&bgtime)))` — parent path.
4598    let mut bgtime: ZshTimespec = libc::timespec {
4599        tv_sec: 0,
4600        tv_nsec: 0,
4601    };
4602    let pid = zfork(Some(&mut bgtime)); // c:5135
4603    if pid != 0 {
4604        // c:5135 — parent.
4605        let _ = zclose(pipes[out as usize]); // c:5136
4606        if pid == -1 {
4607            // c:5137
4608            let _ = zclose(pipes[(1 - out) as usize]); // c:5138
4609            return -1; // c:5139
4610        }
4611        // c:5141-5142 — `if (!nullexec) addproc(pid, NULL, 1, &bgtime, -1, -1);`
4612        if nullexec == 0 {
4613            if let Some(jt) = JOBTAB.get() {
4614                let mut guard = jt.lock().unwrap();
4615                let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
4616                if tj >= 0 {
4617                    if let Some(j) = guard.get_mut(tj as usize) {
4618                        crate::ported::jobs::addproc(
4619                            j,
4620                            pid,
4621                            "",
4622                            true, // aux=1 for proc subst
4623                            Some(std::time::Instant::now()),
4624                            -1,
4625                            -1,
4626                        );
4627                    }
4628                }
4629            }
4630        }
4631        procsubstpid.store(pid, Ordering::Relaxed); // c:5143
4632        return pipes[(1 - out) as usize]; // c:5144
4633    }
4634    // c:5146 — child path.
4635    entersubsh(esub::ASYNC | esub::PGRP | esub::NOMONITOR, None); // c:5146
4636    let _ = redup(pipes[out as usize], out); // c:5147
4637    closem(FDT_UNUSED, 0); // c:5148
4638    cmdpush(CS_CMDSUBST as u8); // c:5149
4639                                // c:5150 — execode(prog, 0, 1, ...) — see WARNING (c).
4640    let body_end = if ends_at > 0 { ends_at - 1 } else { 2 };
4641    let body = if body_end > 2 && body_end <= bytes.len() {
4642        &cmd[2..body_end]
4643    } else {
4644        ""
4645    };
4646    let _ = crate::ported::exec::execute_script_zsh_pipeline(body);
4647    cmdpop(); // c:5151
4648              // c:5152 — _realexit() — WARNING (d).
4649    std::process::exit(LASTVAL.load(Ordering::Relaxed));
4650}
4651
4652/// Port of `static void spawnpipes(LinkList l, int nullexec)` from
4653/// `Src/exec.c:5184`.
4654///
4655/// Walks a redir list `l`, and for each REDIR_OUTPIPE/REDIR_INPIPE
4656/// entry fires `getpipe(name, nullexec || varid)` and stashes the
4657/// resulting fd into `f->fd2`.
4658///
4659/// ```c
4660/// LinkNode n;
4661/// Redir f;
4662/// char *str;
4663/// n = firstnode(l);
4664/// for (; n; incnode(n)) {
4665///     f = (Redir) getdata(n);
4666///     if (f->type == REDIR_OUTPIPE || f->type == REDIR_INPIPE) {
4667///         str = f->name;
4668///         f->fd2 = getpipe(str, nullexec || f->varid);
4669///     }
4670/// }
4671/// ```
4672///
4673/// =================== WARNING — DIVERGENCE ====================
4674/// The Rust port consumes a `&mut Vec<crate::ported::zsh_h::redir>`
4675/// in place of `LinkList`. The walk is identical; the only behavior
4676/// difference is that LinkList iteration in C lets callers splice
4677/// nodes mid-walk — we never do that here so it's a no-op divergence.
4678/// =============================================================
4679pub fn spawnpipes(l: &mut [redir], nullexec: i32) {
4680    // c:5184
4681    for f in l.iter_mut() {
4682        // c:5191
4683        if f.typ == REDIR_OUTPIPE || f.typ == REDIR_INPIPE {
4684            // c:5193
4685            let str_ = f.name.clone().unwrap_or_default(); // c:5194
4686            let nullexec_eff = if f.varid.as_deref().map_or(false, |v| !v.is_empty()) {
4687                1
4688            } else {
4689                nullexec
4690            };
4691            f.fd2 = getpipe(&str_, nullexec_eff); // c:5195
4692        }
4693    }
4694}
4695
4696/// Port of `static int cancd2(char *s)` from `Src/exec.c:6411`.
4697///
4698/// C body:
4699/// ```c
4700/// struct stat buf;
4701/// char *us, *us2 = NULL;
4702/// int ret;
4703/// if (!isset(CHASEDOTS) && !isset(CHASELINKS)) {
4704///     if (*s != '/')
4705///         us = tricat(pwd[1] ? pwd : "", "/", s);
4706///     else
4707///         us = ztrdup(s);
4708///     fixdir(us2 = us);
4709/// } else
4710///     us = unmeta(s);
4711/// ret = !(access(us, X_OK) || stat(us, &buf) || !S_ISDIR(buf.st_mode));
4712/// if (us2) free(us2);
4713/// return ret;
4714/// ```
4715///
4716/// True iff `s` is a directory we can `cd` into (X-perm). With
4717/// `!CHASEDOTS && !CHASELINKS`, lexically canonicalise the path
4718/// (joining with PWD if relative) so `cd /foo/bar/..` works without
4719/// resolving the symlink. Otherwise pass `s` through `unmeta` to libc.
4720pub fn cancd2(s: &str) -> i32 {
4721    // c:6411
4722    let us: String;
4723    // c:6422 — `if (!isset(CHASEDOTS) && !isset(CHASELINKS))`.
4724    let chasedots = isset(CHASEDOTS); // c:6422
4725    let chaselinks = isset(CHASELINKS);
4726    if !chasedots && !chaselinks {
4727        // c:6422
4728        // c:6423-6426 — `*s != '/' ? tricat(pwd, "/", s) : ztrdup(s);`
4729        let pwd_str = getsparam("PWD").unwrap_or_default(); // c:6424 `pwd`
4730        let mut raw = if !s.starts_with('/') {
4731            // c:6423
4732            format!(
4733                "{}/{}",
4734                if pwd_str.len() > 1 { &pwd_str[..] } else { "" },
4735                s
4736            )
4737        } else {
4738            s.to_string()
4739        };
4740        // c:6427 — `fixdir(us2 = us);` — lexical canonicalisation.
4741        raw = fixdir(&raw);
4742        us = raw;
4743    } else {
4744        // c:6428
4745        us = unmeta(s); // c:6429
4746    }
4747    // c:6430 — `!(access(us, X_OK) || stat(us, &buf) || !S_ISDIR(...))`.
4748    let cstr = match std::ffi::CString::new(us.as_str()) {
4749        Ok(c) => c,
4750        Err(_) => return 0,
4751    };
4752    if unsafe { libc::access(cstr.as_ptr(), libc::X_OK) } != 0 {
4753        return 0;
4754    }
4755    let meta = match std::fs::metadata(&us) {
4756        Ok(m) => m,
4757        Err(_) => return 0,
4758    };
4759    if !meta.file_type().is_dir() {
4760        return 0;
4761    }
4762    1
4763}
4764
4765/// Port of `char *cancd(char *s)` from `Src/exec.c:6370`.
4766///
4767/// Resolve a `cd` target against `$cdpath` and `cd_able_vars`.
4768/// Returns the chosen absolute path (heap-dup) if `cancd2` accepts
4769/// it, else `None`.
4770///
4771/// C body uses CDPATH walking + `cd_able_vars()` fallback. Sets
4772/// `doprintdir = -1` when a non-trivial path is found (so `cd`
4773/// echoes the resolved path).
4774pub fn cancd(s: &str) -> Option<String> {
4775    // c:6370
4776    // c:6372-6373 — `nocdpath = s[0]=='.' && (s[1]=='/' || !s[1] ||
4777    //                (s[1]=='.' && (s[2]=='/' || !s[2])))`.
4778    let bytes = s.as_bytes();
4779    let nocdpath = bytes.first().copied() == Some(b'.')
4780        && (bytes.get(1).copied() == Some(b'/')
4781            || bytes.get(1).is_none()
4782            || (bytes.get(1).copied() == Some(b'.')
4783                && (bytes.get(2).copied() == Some(b'/') || bytes.get(2).is_none())));
4784    // c:6376 — `if (*s != '/')` branch.
4785    if !s.starts_with('/') {
4786        // c:6376
4787        // c:6379-6380 — `if (cancd2(s)) return s;`
4788        if cancd2(s) != 0 {
4789            return Some(s.to_string());
4790        }
4791        // c:6381-6382 — `if (access(unmeta(s), X_OK) == 0) return NULL;`
4792        let cstr = std::ffi::CString::new(unmeta(s).as_str()).ok()?;
4793        if unsafe { libc::access(cstr.as_ptr(), libc::X_OK) } == 0 {
4794            return None; // c:6382
4795        }
4796        // c:6383-6397 — CDPATH walk.
4797        if !nocdpath {
4798            let cdpath_str = getsparam("CDPATH").unwrap_or_default();
4799            for cp in cdpath_str.split(':') {
4800                // c:6384
4801                let sbuf = if !cp.is_empty() {
4802                    format!("{}/{}", cp, s) // c:6386
4803                } else {
4804                    s.to_string() // c:6391
4805                };
4806                if cancd2(&sbuf) != 0 {
4807                    // c:6393
4808                    DOPRINTDIR.store(-1, Ordering::Relaxed); // c:6394
4809                    return Some(sbuf); // c:6395
4810                }
4811            }
4812        }
4813        // c:6398-6403 — `cd_able_vars()` fallback.
4814        if let Some(t) = cd_able_vars(s) {
4815            // c:6398
4816            if cancd2(&t) != 0 {
4817                // c:6399
4818                DOPRINTDIR.store(-1, Ordering::Relaxed); // c:6400
4819                return Some(t); // c:6401
4820            }
4821        }
4822        return None; // c:6404
4823    }
4824    // c:6406 — absolute path: `return cancd2(s) ? s : NULL;`
4825    if cancd2(s) != 0 {
4826        Some(s.to_string())
4827    } else {
4828        None
4829    }
4830}
4831
4832/// Port of `char *simple_redir_name(Eprog prog, int redir_type)` from
4833/// `Src/exec.c:4689`.
4834///
4835/// Test if an Eprog encodes a single simple-command consisting of a
4836/// SINGLE redirection of the requested type with NO command body
4837/// (the `cat < foo` shape). When true, returns the redir target name
4838/// (heap-dup) so callers like `$(< file)` short-circuit to a direct
4839/// `open(2)` instead of fork+pipe+exec.
4840///
4841/// C body walks the wordcode at fixed offsets (`pc[0]` = WC_LIST,
4842/// `pc[1]` = WC_SUBLIST, `pc[2]` = WC_PIPE, `pc[3]` = WC_REDIR,
4843/// `pc[6]` = WC_SIMPLE with argc=0). zshrs's wordcode buffer is the
4844/// same shape — this port replicates the same offset reads.
4845pub fn simple_redir_name(prog: &eprog, redir_type: i32) -> Option<String> {
4846    // c:4689
4847    let pc = &prog.prog;
4848    // c:4694-4702 — guard chain. Walk the wordcode buffer at fixed
4849    // offsets matching C's `pc[0]..pc[6]` checks.
4850    if pc.len() < 7 {
4851        return None;
4852    }
4853
4854    if wc_code(pc[0]) != WC_LIST
4855        || (WC_LIST_TYPE(pc[0]) & Z_END as u32) == 0  // c:4695
4856        || wc_code(pc[1]) != WC_SUBLIST
4857        || WC_SUBLIST_FLAGS(pc[1]) != 0  // c:4696
4858        || WC_SUBLIST_TYPE(pc[1]) != WC_SUBLIST_END  // c:4697
4859        || wc_code(pc[2]) != WC_PIPE
4860        || WC_PIPE_TYPE(pc[2]) != WC_PIPE_END  // c:4698
4861        || wc_code(pc[3]) != WC_REDIR
4862        || WC_REDIR_TYPE(pc[3]) != redir_type  // c:4699
4863        || WC_REDIR_VARID(pc[3]) != 0  // c:4700
4864        || pc[4] != 0  // c:4701
4865        || wc_code(pc[6]) != WC_SIMPLE
4866        || WC_SIMPLE_ARGC(pc[6]) != 0
4867    // c:4702
4868    {
4869        return None; // c:4706
4870    }
4871    // c:4703 — `return dupstring(ecrawstr(prog, pc + 5, NULL));`
4872    Some(dupstring(&ecrawstr(prog, 5, None)))
4873}
4874
4875/// Port of `int getherestr(struct redir *fn)` from `Src/exec.c:4655`.
4876///
4877/// C body:
4878/// ```c
4879/// char *s, *t;
4880/// int fd, len;
4881/// t = fn->name;
4882/// singsub(&t);
4883/// untokenize(t);
4884/// unmetafy(t, &len);
4885/// if (!(fn->flags & REDIRF_FROM_HEREDOC))
4886///     t[len++] = '\n';
4887/// if ((fd = gettempfile(NULL, 1, &s)) < 0)
4888///     return -1;
4889/// write_loop(fd, t, len);
4890/// close(fd);
4891/// fd = open(s, O_RDONLY | O_NOCTTY);
4892/// unlink(s);
4893/// return fd;
4894/// ```
4895///
4896/// Materialise a `<<<` herestring or unprocessed-here-doc body into a
4897/// tempfile, then re-open read-only and unlink — gives the consumer a
4898/// read fd whose backing file is already cleaned up.
4899pub fn getherestr(fn_: &redir) -> i32 {
4900    // c:4655
4901    let mut t: String = fn_.name.clone().unwrap_or_default(); // c:4660
4902    t = singsub(&t); // c:4661
4903    t = untokenize(&t); // c:4662
4904                        // c:4663 — `unmetafy(t, &len);` — strip Meta-escapes.
4905                        // Reuse the canonical unmetafy port (utils.rs) on a Vec<u8>.
4906    let mut bytes: Vec<u8> = t.into_bytes();
4907    let _len = unmetafy(&mut bytes);
4908    // c:4671-4672 — `if (!(fn->flags & REDIRF_FROM_HEREDOC)) t[len++] = '\n';`
4909    if (fn_.flags & REDIRF_FROM_HEREDOC) == 0 {
4910        // c:4671
4911        bytes.push(b'\n'); // c:4672
4912    }
4913    // c:4673-4674 — `if ((fd = gettempfile(NULL, 1, &s)) < 0) return -1;`
4914    let (fd, s) = match gettempfile(None) {
4915        Some(p) => p,
4916        None => return -1, // c:4674
4917    };
4918    // c:4675 — `write_loop(fd, t, len);`
4919    let _ = write_loop(fd, &bytes); // c:4675
4920                                    // c:4676 — `close(fd);`
4921    let _ = zclose(fd); // c:4676
4922                        // c:4677 — `fd = open(s, O_RDONLY | O_NOCTTY);`
4923    let cstr = std::ffi::CString::new(s.as_str()).unwrap_or_default();
4924    let new_fd = unsafe { libc::open(cstr.as_ptr(), libc::O_RDONLY | libc::O_NOCTTY) }; // c:4677
4925                                                                                        // c:4678 — `unlink(s);`
4926    unsafe {
4927        libc::unlink(cstr.as_ptr());
4928    } // c:4678
4929    new_fd // c:4679
4930}
4931
4932/// Port of `void quote_tokenized_output(char *str, FILE *file)` from
4933/// `Src/exec.c:2114`.
4934///
4935/// C body (abridged):
4936/// ```c
4937/// for (; *s; s++) {
4938///     switch (*s) {
4939///         case Meta: putc(*++s ^ 32, file); continue;
4940///         case Nularg: continue;
4941///         case '\\' '<' '>' '(' '|' ')' '^' '#' '~' '[' ']' '*' '?' '$' ' ':
4942///             putc('\\', file); break;
4943///         case '\t': fputs("$'\\t'", file); continue;
4944///         case '\n': fputs("$'\\n'", file); continue;
4945///         case '\r': fputs("$'\\r'", file); continue;
4946///         case '=': if (s == str) putc('\\', file); break;
4947///         default:
4948///             if (itok(*s)) { putc(ztokens[*s - Pound], file); continue; }
4949///     }
4950///     putc(*s, file);
4951/// }
4952/// ```
4953///
4954/// Used by `xtrace` (`set -x` printer) and `whence -c` to display a
4955/// tokenized argv in a form where lexer tokens (`Star`, `Inpar`, …)
4956/// surface as unescaped chars (`*`, `(`) while literal special chars
4957/// get backslash-escaped — round-tripping through the shell.
4958pub fn quote_tokenized_output(str_in: &str, file: &mut impl std::io::Write) -> std::io::Result<()> {
4959    // c:2114
4960    let bytes = str_in.as_bytes();
4961    let mut i = 0usize;
4962    while i < bytes.len() {
4963        // c:2118 `for (; *s; s++)`
4964        let c = bytes[i];
4965        match c {
4966            x if x == Meta => {
4967                // c:2120 — `case Meta: putc(*++s ^ 32, file);`
4968                if i + 1 < bytes.len() {
4969                    file.write_all(&[bytes[i + 1] ^ 32])?; // c:2121
4970                    i += 2;
4971                } else {
4972                    i += 1;
4973                }
4974                continue; // c:2122
4975            }
4976            x if x as char == Nularg => {
4977                // c:2124
4978                i += 1;
4979                continue; // c:2126
4980            }
4981            b'\\' | b'<' | b'>' | b'(' | b'|' | b')' | b'^' | b'#' | b'~' | b'[' | b']' | b'*'
4982            | b'?' | b'$' | b' ' => {
4983                // c:2128-2142
4984                file.write_all(b"\\")?; // c:2143
4985            }
4986            b'\t' => {
4987                // c:2146
4988                file.write_all(b"$'\\t'")?; // c:2147
4989                i += 1;
4990                continue;
4991            }
4992            b'\n' => {
4993                // c:2150
4994                file.write_all(b"$'\\n'")?; // c:2151
4995                i += 1;
4996                continue;
4997            }
4998            b'\r' => {
4999                // c:2154
5000                file.write_all(b"$'\\r'")?; // c:2155
5001                i += 1;
5002                continue;
5003            }
5004            b'=' => {
5005                // c:2158 — `if (s == str) putc('\\', file);`
5006                if i == 0 {
5007                    file.write_all(b"\\")?; // c:2160
5008                }
5009            }
5010            _ => {
5011                // c:2163 — `if (itok(*s)) putc(ztokens[*s - Pound], file); continue;`
5012                if itok(c) {
5013                    // c:2164
5014                    let pound = Pound as u8;
5015                    if c >= pound {
5016                        let idx = (c - pound) as usize;
5017                        let zt = ztokens.as_bytes();
5018                        if idx < zt.len() {
5019                            file.write_all(&[zt[idx]])?; // c:2165 `ztokens[*s - Pound]`
5020                        }
5021                    }
5022                    i += 1;
5023                    continue;
5024                }
5025            }
5026        }
5027        file.write_all(&[c])?; // c:2171
5028        i += 1;
5029    }
5030    Ok(())
5031}
5032
5033// =====================================================================
5034// Wordcode-VM control-flow dispatch — faithful ports of the C
5035// `Src/exec.c` + `Src/loop.c` wordcode interpreter entries.
5036//
5037// Each function below takes `&mut estate` and returns `i32` to mirror
5038// the C `int execX(Estate state, int do_exec)` signature exactly. Per-
5039// line `// c:NNN` citations track the C source line.
5040//
5041// zshrs's primary execution path is the fusevm bytecode VM. These
5042// wordcode-VM entries exist for C-name parity with the upstream
5043// interpreter so that future bridging code can drive zshrs through
5044// the same dispatch tree zsh's `Src/init.c::loop` walks. Where
5045// zshrs primitives don't yet model their C counterpart (e.g.
5046// `execsubst`, `addvars`, `execfuncs[]` dispatch table), the local
5047// helper is declared with a comment citing the C source file:line
5048// where the canonical body lives — same pattern as the canonical
5049// `ksh93::ksh93_wrapper` port at c:152-227.
5050// =====================================================================
5051
5052use crate::ported::math::{matheval as wc_matheval, mathevali as wc_mathevali};
5053use crate::ported::pattern::{patcompile, pattry};
5054use crate::ported::r#loop::try_tryflag;
5055
5056// Addvars-specific imports (Src/exec.c:2497 port at exec.rs::addvars).
5057use crate::ported::builtin::{BREAKS, CONTFLAG, LOOPS, RETFLAG};
5058use crate::ported::linklist::LinkList;
5059use crate::ported::mem::freeheap;
5060use crate::ported::params::setloopvar;
5061use crate::ported::params::{assignaparam, assignsparam, unsetparam};
5062use crate::ported::parse::{ecgetlist, ecgetstr};
5063use crate::ported::pattern::haswilds;
5064use crate::ported::signals_h::{queue_signal_level, restore_queue_signals};
5065use crate::ported::subst::{globlist, prefork};
5066use crate::ported::zsh_h::{
5067    estate, wordcode, EC_DUP, EC_DUPTOK, EC_NODUP, NOERREXIT_EXIT, NOERREXIT_RETURN, PAT_STATIC,
5068    WC_CASE, WC_CASE_AND, WC_CASE_OR, WC_CASE_SKIP, WC_CASE_TESTAND, WC_CASE_TYPE, WC_CURSH_SKIP,
5069    WC_END, WC_FOR_COND, WC_FOR_LIST, WC_FOR_SKIP, WC_FOR_TYPE, WC_FUNCDEF, WC_FUNCDEF_SKIP, WC_IF,
5070    WC_IF_ELSE, WC_IF_SKIP, WC_IF_TYPE, WC_REPEAT_SKIP, WC_TIMED_EMPTY, WC_TIMED_TYPE, WC_TRY_SKIP,
5071    WC_WHILE_SKIP, WC_WHILE_TYPE, WC_WHILE_UNTIL,
5072};
5073use crate::ported::zsh_h::{
5074    ALLEXPORT, ASSPM_AUGMENT, ASSPM_KEY_VALUE, ASSPM_WARN, GLOBASSIGN, KSHARRAYS, PREFORK_ASSIGN,
5075    PREFORK_KEY_VALUE, PREFORK_SINGLE, WC_ASSIGN, WC_ASSIGN_INC, WC_ASSIGN_NUM, WC_ASSIGN_SCALAR,
5076    WC_ASSIGN_TYPE, WC_ASSIGN_TYPE2,
5077};
5078use crate::ported::zsh_h::{
5079    CS_ALWAYS, CS_CASE, CS_COND, CS_CURSH, CS_ELIF, CS_ELIFTHEN, CS_ELSE, CS_FOR, CS_IF, CS_IFTHEN,
5080    CS_MATH, CS_REPEAT, CS_UNTIL, CS_WHILE, MN_INTEGER,
5081};
5082
5083// --- Local stubs for C primitives not yet ported elsewhere ------------
5084//
5085// These mirror the C functions of the same names. Each cites the C
5086// source file:line where the canonical body lives. They are inlined
5087// here (rather than a separate `pub fn` in the owning C-file module)
5088// because the owning ports are pending the wider exec-substrate
5089// work (sub-PR). Once those land, these locals collapse to direct
5090// `crate::ported::<owner>::<fn>` calls.
5091
5092/// Port of `void execsubst(LinkList strs)` from `Src/exec.c:2684`.
5093///
5094/// C body (c:2684-2693):
5095/// ```c
5096/// void execsubst(LinkList strs) {
5097///     if (strs) {
5098///         prefork(strs, esprefork, NULL);
5099///         if (esglob && !errflag) {
5100///             LinkList ostrs = strs;
5101///             globlist(strs, 0);
5102///             strs = ostrs;
5103///         }
5104///     }
5105/// }
5106/// ```
5107///
5108/// `execsubst` runs `prefork` (parameter / arithmetic / command
5109/// substitution expansion + IFS-split) over the whole list, then
5110/// (when `esglob` is set) `globlist` to do filename globbing on the
5111/// result.
5112fn execsubst(list: &mut Vec<String>) {
5113    // c:2684
5114    if list.is_empty() {
5115        return; // c:2686 `if (strs)`
5116    }
5117    let mut ll: crate::ported::subst::LinkList = std::mem::take(list).into_iter().collect();
5118    let prefork_flags = esprefork.load(Ordering::Relaxed); // c:2687 esprefork
5119    let mut rf: i32 = 0;
5120    prefork(&mut ll, prefork_flags, &mut rf); // c:2687
5121    if esglob.load(Ordering::Relaxed) != 0 && errflag.load(Ordering::Relaxed) == 0 {
5122        // c:2688 `if (esglob && !errflag)`
5123        globlist(&mut ll, 0); // c:2690
5124    }
5125    *list = ll.into_iter().collect();
5126}
5127
5128/// Direct port of `static void addvars(Estate state, Wordcode pc,
5129/// int addflags)` from `Src/exec.c:2497-2648`. Process the WC_ASSIGN
5130/// nodes stacked inline of a simple command — the `var=value` and
5131/// `arr=(v1 v2 v3)` assignments that precede argv. Walks the wordcode
5132/// at `pc`, extracts each assignment's name + value (scalar or array),
5133/// optionally preforks + globs the tokenised RHS, and routes through
5134/// `assignsparam` (scalar) or `assignaparam` (array).
5135///
5136/// XTRACE side-effect: prints `name=value ` / `name=( v1 v2 ) ` to
5137/// stderr (C uses xtrerr; zshrs uses eprint!).
5138///
5139/// `STTY=...` in an inline-export form (`STTY=raw cmd`) gets captured
5140/// into the file-static `STTYval` for `execute()` to apply pre-exec.
5141fn addvars(state: &mut estate, pc: usize, addflags: i32) {
5142    // c:2501 — locals.
5143    let mut vl: LinkList<String>; // c:2501 `LinkList vl;`
5144    let xtr: bool; // c:2502 `int xtr,`
5145    let mut isstr: bool; // c:2502 `int isstr,`
5146    let mut htok: i32 = 0; // c:2502 `int htok = 0;`
5147    let mut arr: Vec<String>; // c:2503 `char **arr, **ptr, *name;`
5148    let mut name: String;
5149    let mut flags: i32; // c:2504 `int flags;`
5150    let opc = state.pc; // c:2506 `Wordcode opc = state->pc;`
5151    let mut ac: wordcode; // c:2507 `wordcode ac;`
5152                          // c:2508 `local_list1(svl);` — stack-local one-element LinkList
5153                          // for the scalar-assignment path. Rust uses a fresh LinkList per
5154                          // iteration; equivalent semantics.
5155
5156    // c:2510-2515 — comment about WARNCREATEGLOBAL warning suppression
5157    // when the assignment list is implicitly local (ADDVAR_RESTORE).
5158    flags = if (addflags & ADDVAR_RESTORE) == 0 {
5159        ASSPM_WARN // c:2516
5160    } else {
5161        0 // c:2516
5162    };
5163    xtr = isset(XTRACE); // c:2517 `xtr = isset(XTRACE);`
5164    if xtr {
5165        // c:2518
5166        printprompt4(); // c:2519
5167        doneps4.store(1, Ordering::Relaxed); // c:2520 `doneps4 = 1;`
5168    }
5169    state.pc = pc; // c:2522 `state->pc = pc;`
5170
5171    // c:2523 `while (wc_code(ac = *state->pc++) == WC_ASSIGN) {`
5172    loop {
5173        if state.pc >= state.prog.prog.len() {
5174            break;
5175        }
5176        ac = state.prog.prog[state.pc];
5177        state.pc += 1;
5178        if wc_code(ac) != WC_ASSIGN {
5179            // Step back so the WC_SIMPLE / outer dispatcher sees the
5180            // non-assignment opcode. C's `state->pc++` post-increment
5181            // already pointed past WC_ASSIGN; we need to unconsume.
5182            state.pc -= 1;
5183            break;
5184        }
5185        let mut myflags = flags; // c:2524 `int myflags = flags;`
5186        name = ecgetstr(state, EC_DUPTOK, Some(&mut htok)); // c:2525
5187        if htok != 0 {
5188            // c:2526 `if (htok) untokenize(name);`
5189            name = untokenize(&name).to_string(); // c:2527
5190        }
5191        if WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC {
5192            // c:2528
5193            myflags |= ASSPM_AUGMENT; // c:2529
5194        }
5195        if xtr {
5196            // c:2530
5197            // c:2531-2532 — fprintf(xtrerr, ... "%s+=" : "%s=", name);
5198            if WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC {
5199                eprint!("{}+=", name); // c:2532
5200            } else {
5201                eprint!("{}=", name); // c:2532
5202            }
5203        }
5204
5205        // c:2533 `if ((isstr = (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR))) {`
5206        isstr = WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR;
5207        if isstr {
5208            // c:2534 `init_list1(svl, ecgetstr(state, EC_DUPTOK, &htok));`
5209            let svl_val = ecgetstr(state, EC_DUPTOK, Some(&mut htok));
5210            vl = LinkList::new();
5211            vl.push_back(svl_val);
5212            // c:2535 `vl = &svl;` — vl already points at the new list.
5213        } else {
5214            // c:2537 `vl = ecgetlist(state, WC_ASSIGN_NUM(ac), EC_DUPTOK, &htok);`
5215            let items = ecgetlist(
5216                state,
5217                WC_ASSIGN_NUM(ac) as usize,
5218                EC_DUPTOK,
5219                Some(&mut htok),
5220            );
5221            vl = LinkList::new();
5222            for it in items {
5223                vl.push_back(it);
5224            }
5225            if errflag.load(Ordering::Relaxed) != 0 {
5226                // c:2538-2541
5227                state.pc = opc; // c:2539
5228                return; // c:2540
5229            }
5230        }
5231
5232        // c:2544 `if (vl && htok) {`
5233        if htok != 0 {
5234            // c:2545 `int prefork_ret = 0;`
5235            let mut prefork_ret: i32 = 0;
5236            // c:2546-2547 — prefork(vl, (isstr ? PREFORK_SINGLE|PREFORK_ASSIGN
5237            //                          : PREFORK_ASSIGN), &prefork_ret);
5238            let pf_flags = if isstr {
5239                PREFORK_SINGLE | PREFORK_ASSIGN
5240            } else {
5241                PREFORK_ASSIGN
5242            };
5243            prefork(&mut vl, pf_flags, &mut prefork_ret); // c:2547
5244            if errflag.load(Ordering::Relaxed) != 0 {
5245                // c:2548
5246                state.pc = opc; // c:2549
5247                return; // c:2550
5248            }
5249            if (prefork_ret & PREFORK_KEY_VALUE) != 0 {
5250                // c:2552
5251                myflags |= ASSPM_KEY_VALUE; // c:2553
5252            }
5253            // c:2554-2555 — `if (!isstr || (isset(GLOBASSIGN) && isstr &&
5254            //                  haswilds((char *)getdata(firstnode(vl)))))`
5255            let needs_glob = if !isstr {
5256                true
5257            } else {
5258                isset(GLOBASSIGN)
5259                    && isstr
5260                    && !vl.is_empty()
5261                    && haswilds(vl.nodes.front().map(|s| s.as_str()).unwrap_or(""))
5262            };
5263            if needs_glob {
5264                globlist(&mut vl, prefork_ret); // c:2556
5265                                                // c:2557-2562 — `if (isset(GLOBASSIGN) && isstr)
5266                                                //                  unsetparam(name);`
5267                if isset(GLOBASSIGN) && isstr {
5268                    unsetparam(&name); // c:2562
5269                }
5270                if errflag.load(Ordering::Relaxed) != 0 {
5271                    // c:2563
5272                    state.pc = opc; // c:2564
5273                    return; // c:2565
5274                }
5275            }
5276        }
5277        // c:2569 `if (isstr && (empty(vl) || !nextnode(firstnode(vl))))`
5278        // — scalar-assignment path: zero or one element after prefork.
5279        if isstr && (vl.is_empty() || vl.len() == 1) {
5280            let val: String; // c:2571 `char *val;`
5281            if vl.is_empty() {
5282                // c:2574
5283                val = String::new(); // c:2575 `val = ztrdup("");`
5284            } else {
5285                // c:2577 `untokenize(peekfirst(vl));`
5286                let peek = vl.nodes.front().cloned().unwrap_or_default();
5287                val = untokenize(&peek).to_string(); // c:2577-2578
5288                                                     // c:2578 `val = ztrdup(ugetnode(vl));` — ugetnode pops;
5289                                                     // we just cloned the front above. Equivalent.
5290            }
5291            if xtr {
5292                // c:2580
5293                eprint!("{}", quotedzputs(&val)); // c:2581
5294                eprint!(" "); // c:2582 `fputc(' ', xtrerr);`
5295            }
5296            // c:2584 `if ((addflags & ADDVAR_EXPORT) && !strchr(name, '['))`
5297            let pm = if (addflags & ADDVAR_EXPORT) != 0 && !name.contains('[') {
5298                // c:2585 `if (strcmp(name, "STTY") == 0)`
5299                if name == "STTY" {
5300                    // c:2586-2587 — `STTYval = ztrdup(val);`
5301                    let mut stty = STTYval.lock().unwrap();
5302                    *stty = Some(val.clone()); // c:2587
5303                }
5304                // c:2589 `allexp = opts[ALLEXPORT];`
5305                let allexp = isset(ALLEXPORT);
5306                // c:2590 `opts[ALLEXPORT] = 1;` — temporarily set.
5307                opt_state_set("allexport", true);
5308                if isset(KSHARRAYS) {
5309                    // c:2591
5310                    unsetparam(&name); // c:2592
5311                }
5312                let pm = assignsparam(&name, &val, myflags); // c:2593
5313                                                             // c:2594 `opts[ALLEXPORT] = allexp;` — restore.
5314                opt_state_set("allexport", allexp);
5315                pm
5316            } else {
5317                // c:2595
5318                assignsparam(&name, &val, myflags) // c:2596
5319            };
5320            if pm.is_none() {
5321                // c:2597 `if (!pm)`
5322                LASTVAL.store(1, Ordering::Relaxed); // c:2598 `lastval = 1;`
5323                                                     // c:2599-2604 — "cheating" comment: don't zerr.
5324                if cmdoutval.load(Ordering::Relaxed) == 0 {
5325                    // c:2605 `if (!cmdoutval)`
5326                    cmdoutval.store(1, Ordering::Relaxed); // c:2606
5327                }
5328            }
5329            if errflag.load(Ordering::Relaxed) != 0 {
5330                // c:2608
5331                state.pc = opc; // c:2609
5332                return; // c:2610
5333            }
5334            continue; // c:2612
5335        }
5336        // c:2614 `if (vl) { ... }` — array-assignment path: drain vl
5337        // into a fresh `char **arr`.
5338        // c:2615-2619 `ptr = arr = zalloc(...); while (nonempty(vl)) *ptr++ = ztrdup(ugetnode(vl));`
5339        arr = Vec::with_capacity(vl.len() + 1);
5340        while let Some(s) = vl.pop_front() {
5341            arr.push(s);
5342        }
5343        // c:2623 `*ptr = NULL;` — C terminator; Rust Vec doesn't need it.
5344        if xtr {
5345            // c:2624
5346            eprint!("( "); // c:2625
5347            for s in &arr {
5348                // c:2626 `for (ptr = arr; *ptr; ptr++)`
5349                eprint!("{}", quotedzputs(s)); // c:2627
5350                eprint!(" "); // c:2628
5351            }
5352            eprint!(") "); // c:2630
5353        }
5354        // c:2632 `if (!assignaparam(name, arr, myflags))`
5355        if assignaparam(&name, arr, myflags).is_none() {
5356            LASTVAL.store(1, Ordering::Relaxed); // c:2633
5357                                                 // c:2634-2638 — "cheating" comment.
5358            if cmdoutval.load(Ordering::Relaxed) == 0 {
5359                // c:2639
5360                cmdoutval.store(1, Ordering::Relaxed); // c:2640
5361            }
5362        }
5363        if errflag.load(Ordering::Relaxed) != 0 {
5364            // c:2642
5365            state.pc = opc; // c:2643
5366            return; // c:2644
5367        }
5368    }
5369    state.pc = opc; // c:2647 `state->pc = opc;`
5370}
5371
5372// execfuncs[] dispatch table from `Src/exec.c:5499` is inlined as a
5373// match expression at the call sites in execsimple. Not a separate
5374// Rust fn — every C-side reference to
5375// `execfuncs[code - WC_CURSH](state, ...)` resolves inline below.
5376
5377// --- exec.c entries ---------------------------------------------------
5378
5379/// Port of `execcursh(Estate state, int do_exec)` from
5380/// `Src/exec.c:469-498`. Execute a `{ ... }` current-shell command
5381/// group: skip the trailing try-only word, optionally drop a stale
5382/// job slot, then run the inner list.
5383pub fn execcursh(state: &mut estate, do_exec: i32) -> i32 {
5384    // c:472 — `end = state->pc + WC_CURSH_SKIP(state->pc[-1]);`
5385    let prior = state.prog.prog[state.pc.wrapping_sub(1)];
5386    let end = state.pc + WC_CURSH_SKIP(prior) as usize;
5387    // c:475 — `state->pc++;` skip the try/always-only word.
5388    state.pc += 1;
5389    // c:482-486 — drop empty job slot before nested cmd: if outer-pipe
5390    // bookkeeping is clean AND thisjob is a real job that's not the
5391    // pipe-leader AND has no procs yet, deletejob() recycles it. Avoids
5392    // leaking job-table slots when execcursh recurses.
5393    {
5394        let lp = list_pipe.load(Ordering::Relaxed);
5395        let lpj = list_pipe_job.load(Ordering::Relaxed);
5396        let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
5397        if lp == 0 && tj != -1 && tj != lpj {
5398            if let Some(jt) = JOBTAB.get() {
5399                let mut guard = jt.lock().unwrap();
5400                let has = crate::ported::jobs::hasprocs(&guard, tj as usize);
5401                if !has {
5402                    if let Some(j) = guard.get_mut(tj as usize) {
5403                        crate::ported::jobs::deletejob(j, false);
5404                    }
5405                }
5406            }
5407        }
5408    }
5409    cmdpush(CS_CURSH as u8); // c:487 — `cmdpush(CS_CURSH);`
5410    let _ = execlist(state, 1, do_exec); // c:488 — `execlist(state, 1, do_exec);`
5411    cmdpop(); // c:489 — `cmdpop();`
5412    state.pc = end; // c:491 — `state->pc = end;`
5413    this_noerrexit.store(1, Ordering::Relaxed); // c:492 — `this_noerrexit = 1;`
5414    LASTVAL.load(Ordering::Relaxed) // c:494 — `return lastval;`
5415}
5416
5417// `(...)` subshell — no dedicated C function (handled inline by
5418// `execpline`'s WC_PIPE branch via the WC_SUBSH bit, exec.c:2540+).
5419// In zshrs the subshell branch is folded into `execpline` and
5420// `execsimple`'s WC_SUBSH dispatch — both invoke execcursh for the
5421// inner-list walk since fusevm bytecode handles the forking via
5422// Op::Subshell at a higher layer.
5423
5424/// Port of `execcond(Estate state, UNUSED(int do_exec))` from
5425/// `Src/exec.c:5204-5232`. Run a `[[ ... ]]` cond expression.
5426pub fn execcond(state: &mut estate, _do_exec: i32) -> i32 {
5427    state.pc -= 1; // c:5208 — `state->pc--;`
5428                   // c:5209-5213 — XTRACE prelude.
5429    if isset(XTRACE) {
5430        printprompt4();
5431        eprint!("[[");
5432        // c:5212 — `tracingcond++;` not modeled in zshrs.
5433    }
5434    cmdpush(CS_COND as u8); // c:5214
5435                            // c:5215 — `stat = evalcond(state, NULL);` — TODO faithful: needs
5436                            // the wordcode-level evalcond from Src/cond.c which is distinct
5437                            // from the test-builtin evalcond ported in cond.rs. Pending.
5438    let stat: i32 = 0;
5439    // c:5219-5221 — `if (stat == 2) errflag |= ERRFLAG_ERROR;`
5440    if stat == 2 {
5441        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
5442    }
5443    cmdpop(); // c:5222
5444    if isset(XTRACE) {
5445        eprintln!(" ]]");
5446    }
5447    stat // c:5230 — `return stat;`
5448}
5449
5450/// Port of `execarith(Estate state, UNUSED(int do_exec))` from
5451/// `Src/exec.c:5237-5275`. Run a `(( ... ))` arithmetic command;
5452/// returns 0 when val != 0 (success), 1 when val == 0 (false), 2 on
5453/// parse error.
5454pub fn execarith(state: &mut estate, _do_exec: i32) -> i32 {
5455    if isset(XTRACE) {
5456        printprompt4();
5457        eprint!("((");
5458    }
5459    cmdpush(CS_MATH as u8); // c:5247
5460    let mut htok: i32 = 0;
5461    let mut e = ecgetstr(state, EC_DUPTOK, Some(&mut htok)); // c:5248
5462    if htok != 0 {
5463        e = singsub(&e); // c:5250 — `singsub(&e);`
5464    }
5465    if isset(XTRACE) {
5466        eprint!(" {}", e);
5467    }
5468    let val_result = wc_matheval(&e); // c:5254 — `val = matheval(e);`
5469    cmdpop(); // c:5256
5470    if isset(XTRACE) {
5471        eprintln!(" ))");
5472    }
5473    // c:5262-5265 — `if (errflag) { errflag &= ~ERRFLAG_ERROR; return 2; }`
5474    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 || val_result.is_err() {
5475        errflag.fetch_and(!ERRFLAG_ERROR, Ordering::Relaxed);
5476        return 2;
5477    }
5478    // c:5267 — `return (val.type == MN_INTEGER) ? val.u.l == 0 : val.u.d == 0.0;`
5479    let val = val_result.unwrap();
5480    if val.type_ == MN_INTEGER {
5481        if val.l == 0 {
5482            1
5483        } else {
5484            0
5485        }
5486    } else if val.d == 0.0 {
5487        1
5488    } else {
5489        0
5490    }
5491}
5492
5493/// Port of `exectime(Estate state, UNUSED(int do_exec))` from
5494/// `Src/exec.c:5279-5294`. Run `time pipeline`: drives execpline with
5495/// the Z_TIMED|Z_SYNC flags so it tracks wall/user/sys time.
5496pub fn exectime(state: &mut estate, _do_exec: i32) -> i32 {
5497    let jb = *THISJOB
5498        .get_or_init(|| std::sync::Mutex::new(-1))
5499        .lock()
5500        .unwrap(); // c:5283
5501    let prior = state.prog.prog[state.pc.wrapping_sub(1)];
5502    // c:5284-5287 — empty `time` (no pipeline) — print accumulated shell time.
5503    if WC_TIMED_TYPE(prior) == WC_TIMED_EMPTY {
5504        // c:5285 — `shelltime(NULL,NULL,NULL,0);` — print accumulated
5505        // shell+kids time deltas since last call.
5506        crate::ported::jobs::shelltime(None, None, None, 0);
5507        return 0; // c:5286
5508    }
5509    // c:5288 — `execpline(state, *state->pc++, Z_TIMED|Z_SYNC, 0);`
5510    let slcode = state.prog.prog[state.pc];
5511    state.pc += 1;
5512    use crate::ported::zsh_h::{Z_SYNC, Z_TIMED};
5513    let _ = execpline(state, slcode, Z_TIMED as i32 | Z_SYNC as i32, 0);
5514    *THISJOB
5515        .get_or_init(|| std::sync::Mutex::new(-1))
5516        .lock()
5517        .unwrap() = jb; // c:5289
5518    LASTVAL.load(Ordering::Relaxed) // c:5290
5519}
5520
5521/// `execshfunc(Shfunc shf, LinkList args)` — `Src/exec.c:5540`.
5522/// Promoted to top-level pub fn so execcmd_exec at the shfunc
5523/// dispatch site (c:4102-4105) can route through it. The real port
5524/// owns queue_signals + cmdstack + sfcontext setup before calling
5525/// doshfunc; doshfunc itself is unported, so we route the body
5526/// through `runshfunc` (exec.rs:1700), which carries the
5527/// wrapper-chain + zunderscore restore. Degraded vs C (no cmdstack
5528/// push, no sfcontext flip, no XTRACE arg-trace) but the function
5529/// body executes and `lastval` is updated.
5530pub fn execshfunc(shf: &mut shfunc, args: &mut Vec<String>) {
5531    // c:5546-5547 — `if (errflag) return;`
5532    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
5533        return;
5534    }
5535    // c:5550-5557 — drop empty job slot before nested shfunc invoke:
5536    // if outer-pipe bookkeeping is clean AND thisjob is a real job
5537    // that's not the pipe-leader AND has no procs yet, deletejob()
5538    // recycles it. Avoids leaking job-table slots across recursive
5539    // function calls. Same pattern as execcursh's c:482-486.
5540    {
5541        let lp = list_pipe.load(Ordering::Relaxed);
5542        let lpj = list_pipe_job.load(Ordering::Relaxed);
5543        let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
5544        if lp == 0 && tj != -1 && tj != lpj {
5545            if let Some(jt) = JOBTAB.get() {
5546                let mut guard = jt.lock().unwrap();
5547                let has = crate::ported::jobs::hasprocs(&guard, tj as usize);
5548                if !has {
5549                    // c:5554-5555 — `last_file_list = jobtab[thisjob].filelist;
5550                    //                jobtab[thisjob].filelist = NULL;` — preserve
5551                    //                the filelist so deletejob doesn't unlink temp
5552                    //                files. Rust take()s the Vec into a local.
5553                    let _last_file_list: Vec<jobfile> = if let Some(j) = guard.get_mut(tj as usize)
5554                    {
5555                        std::mem::take(&mut j.filelist)
5556                    } else {
5557                        Vec::new()
5558                    };
5559                    if let Some(j) = guard.get_mut(tj as usize) {
5560                        crate::ported::jobs::deletejob(j, false); // c:5556
5561                    }
5562                }
5563            }
5564        }
5565    }
5566    // c:5559-5570 — `if (isset(XTRACE)) { printprompt4(); ... \n; }` —
5567    // emit PS4 prefix + space-separated quoted args on the trace
5568    // stream so `set -x` shows the function invocation line.
5569    if isset(XTRACE) {
5570        printprompt4();
5571        for (i, a) in args.iter().enumerate() {
5572            if i > 0 {
5573                eprint!(" ");
5574            }
5575            eprint!("{}", quotedzputs(a));
5576        }
5577        eprintln!();
5578    }
5579    // c:5572-5578 cmdstack/sfcontext setup: omit (no cmdstack in
5580    // zshrs yet — replaced by tracing).
5581    // c:5580 — `doshfunc(shf, args, 0);` — doshfunc swaps PPARAMS
5582    // ($1, $2, …) to the function's args, runs the body via
5583    // runshfunc, then restores. doshfunc itself isn't ported yet
5584    // so we do the swap-and-restore inline here.
5585    // c:5580 — `doshfunc(shf, args, 0);`. The C path always has
5586    // `funcdef` populated since C parses at definition time. zshrs
5587    // compiles to fusevm chunks instead, so `funcdef` is None for
5588    // user-defined functions; only `body` (source string) carries
5589    // the definition. When that's the case, build a one-shot eprog
5590    // whose `strs` carries the source so runshfunc's script-pipeline
5591    // arm (execute_script_zsh_pipeline) executes the body.
5592    let prog_owned: Option<eprog> = if shf.funcdef.is_some() {
5593        None
5594    } else if let Some(ref body) = shf.body {
5595        Some(eprog {
5596            strs: Some(body.clone()),
5597            ..Default::default()
5598        })
5599    } else {
5600        None
5601    };
5602    let prog_ref: Option<&eprog> = match (shf.funcdef.as_deref(), prog_owned.as_ref()) {
5603        (Some(p), _) => Some(p),
5604        (_, Some(p)) => Some(p),
5605        _ => None,
5606    };
5607    if let Some(_prog) = prog_ref {
5608        // c:5580 — `doshfunc(shf, args, 0);`. Direct doshfunc call —
5609        // noreturnval=0 means the body's return value updates LASTVAL
5610        // (caller of execfuncdef reads it back). PPARAMS swap +
5611        // restore happens INSIDE doshfunc's scope; body_runner just
5612        // runs the body.
5613        let name_for_body = shf.node.nam.clone();
5614        let body_args_owned: Vec<String> = if args.len() > 1 {
5615            args[1..].to_vec()
5616        } else {
5617            Vec::new()
5618        };
5619        let body_runner = move || -> i32 {
5620            crate::ported::exec::run_function_body(&name_for_body, &body_args_owned).unwrap_or(0)
5621        };
5622        let _ = doshfunc(shf, args.clone(), false, body_runner);
5623    }
5624    // c:5582-5589 cmdstack restore/free: omit (no cmdstack).
5625}
5626
5627/// Port of `int doshfunc(Shfunc shfunc, LinkList doshargs, int noreturnval)`
5628/// from `Src/exec.c:5823-6158`.
5629///
5630/// C body's scope-management sequence ported here. The C source's
5631/// body-execution call (`runshfunc(prog, wrappers, name)` at c:6042)
5632/// is replaced by `body_runner` — zshrs runs function bodies through
5633/// fusevm bytecode rather than zsh's wordcode walker (per PORT.md
5634/// "zshrs replaces zsh's tree-walking interpreter" rule), so the
5635/// callback hands the live executor back to the caller (typically
5636/// the fusevm bridge) for the actual body run. Every line of scope
5637/// save/restore around the body call mirrors C exactly.
5638///
5639/// **RUST-ONLY ADAPTATION:** the extra `body_runner` parameter is
5640/// not in C. C calls `runshfunc(prog, wrappers, name)` directly at
5641/// c:6042; zshrs delegates to a closure because the body-execution
5642/// pipeline (fusevm) differs from C's (wordcode). The closure
5643/// fully replaces the runshfunc call and returns the body's exit
5644/// status (which doshfunc reads as `lastval` for the `noreturnval`
5645/// path).
5646#[allow(non_snake_case)]
5647pub fn doshfunc(
5648    shfunc: &mut shfunc,                  // c:5823
5649    doshargs: Vec<String>,                // c:5823
5650    noreturnval: bool,                    // c:5823
5651    mut body_runner: impl FnMut() -> i32, // (Rust-only — body delegate)
5652) -> i32 {
5653    use crate::ported::builtin::{BREAKS, CONTFLAG, LASTVAL, LOOPS, RETFLAG};
5654    use crate::ported::jobs::{NUMPIPESTATS, PIPESTATS};
5655    use crate::ported::modules::parameter::FUNCSTACK;
5656    use crate::ported::params::endparamscope;
5657    use crate::ported::params::locallevel as locallevel_atomic;
5658    use crate::ported::zsh_h::{FS_EVAL, FS_FUNC, FS_SOURCE, FUNCTIONARGZERO, PM_UNDEFINED};
5659    use std::sync::atomic::Ordering;
5660
5661    let name = shfunc.node.nam.clone(); // c:5827
5662    let flags = shfunc.node.flags; // c:5828
5663    let fname = dupstring(&name); // c:5829
5664    let _ = fname; // c:5829 (kept for parity)
5665
5666    // c:6000-6004 — FUNCNEST guard. C:
5667    //   if (zsh_funcnest >= 0 && funcstack && funcstacksz >= zsh_funcnest) {
5668    //       zerr("maximum nested function level reached; increase FUNCNEST?");
5669    //       goto undoshfunc;
5670    //   }
5671    // This was previously stubbed out on the assumption that the zshrs
5672    // fusevm "doesn't recurse via real stack frames" — but it DOES:
5673    // dispatch_function_call -> doshfunc -> dispatch_function_call -> …
5674    // nests real native frames, so an accidental infinite recursion
5675    // (e.g. a self-referential hook, a wrapped function that re-invokes
5676    // itself, a zle widget cycle) overflowed the OS stack and
5677    // SEGFAULTED instead of producing zsh's graceful error. Check at
5678    // entry — before queue_signals / inc_locallevel / the funcsave
5679    // snapshot — so the early return needs no unwinding. FUNCNEST < 0
5680    // means "unlimited" (zsh's `zsh_funcnest >= 0` gate). Depth is the
5681    // count of already-active frames on FUNCSTACK; the 501st nested
5682    // call (depth == 500) trips it, matching C's `>=` on the default
5683    // FUNCNEST=500.
5684    let funcnest = crate::ported::params::getiparam("FUNCNEST");
5685    if funcnest >= 0 {
5686        // Count only real function frames (FS_FUNC) — the raw FUNCSTACK
5687        // Vec also carries FS_EVAL/FS_SOURCE/anon frames, which inflate
5688        // the depth several× and would falsely trip on legitimately deep
5689        // (but finite) recursion. FS_FUNC-only matches zsh's nesting
5690        // depth (`${#funcstack}`) and its funcnest accounting.
5691        let depth = crate::ported::modules::parameter::FUNCSTACK
5692            .lock()
5693            .map(|s| s.iter().filter(|f| f.tp == FS_FUNC).count())
5694            .unwrap_or(0) as i64;
5695        if depth >= funcnest {
5696            zerr("maximum nested function level reached; increase FUNCNEST?");
5697            crate::ported::builtin::LASTVAL.store(1, std::sync::atomic::Ordering::Relaxed);
5698            return 1;
5699        }
5700    }
5701
5702    // c:5835 — `queue_signals();` Lots of memory + global-state changes.
5703    queue_signals();
5704
5705    // c:5847-5848 — `marked_prog = shfunc->funcdef; useeprog(marked_prog);`
5706    // Pinned so a recursive unload doesn't free the eprog under us.
5707    // (Skipped: zshrs's shfunc holds a Box<Eprog>; Drop semantics
5708    // already pin until call ends. C does explicit refcount on
5709    // `funcdef->nref` via useeprog.)
5710
5711    // c:5856-5916 — Funcsave allocation + per-field snapshot.
5712    let funcsave_breaks = BREAKS.load(Ordering::Relaxed); // c:5859
5713    let funcsave_contflag = CONTFLAG.load(Ordering::Relaxed); // c:5860
5714    let funcsave_loops = LOOPS.load(Ordering::Relaxed); // c:5861
5715    let funcsave_lastval = LASTVAL.load(Ordering::Relaxed); // c:5862
5716    let funcsave_numpipestats = {
5717        // c:5864
5718        NUMPIPESTATS
5719            .get_or_init(|| std::sync::Mutex::new(0))
5720            .lock()
5721            .map(|n| *n)
5722            .unwrap_or(0)
5723    };
5724    let funcsave_noerrexit = noerrexit.load(Ordering::Relaxed); // c:5865
5725                                                                // c:5866-5867 — trap_state PRIMED branch decrements trap_return.
5726    if TRAP_STATE.load(Ordering::Relaxed) == TRAP_STATE_PRIMED {
5727        // c:5866
5728        TRAP_RETURN.fetch_sub(1, Ordering::Relaxed); // c:5867
5729    }
5730    // c:5871 — `noerrexit &= ~NOERREXIT_RETURN;` — scope-clear of
5731    // return-suppress so a `return` inside the body fires errexit
5732    // checks normally.
5733    noerrexit.fetch_and(!NOERREXIT_RETURN, Ordering::Relaxed);
5734
5735    // c:5872-5880 — noreturnval branch: deep-copy pipestats so the
5736    // function body's pipestats writes are restored on exit.
5737    let funcsave_pipestats: Option<Vec<i32>> = if noreturnval {
5738        // c:5872
5739        let p = PIPESTATS.get_or_init(|| std::sync::Mutex::new([0; MAX_PIPESTATS]));
5740        p.lock().ok().map(|g| g[..funcsave_numpipestats].to_vec()) // c:5879 memcpy
5741    } else {
5742        None
5743    };
5744
5745    // c:5882-5896 — TRAPEXIT special case (deep-copy shfunc so
5746    // starttrapscope doesn't rug-pull). zshrs doesn't yet support
5747    // running TRAPEXIT directly via doshfunc; flagged for follow-up.
5748    // (Skip: name = "TRAPEXIT" path.)
5749    let _ = name.as_str(); // sentinel for the eventual port.
5750
5751    // c:5898 — `starttrapscope();` — canonical port at signals.rs:1135
5752    // tags SIGEXIT for deferred restoration at scope end.
5753    crate::ported::signals::starttrapscope();
5754    // c:5899 — `startpatternscope();`
5755    crate::ported::pattern::startpatternscope();
5756
5757    // c:5901 — `pptab = pparams;` — save outer positional params.
5758    let pptab: Vec<String> = crate::ported::builtin::PPARAMS
5759        .lock()
5760        .map(|p| p.clone())
5761        .unwrap_or_default();
5762
5763    // c:5902-5903 — non-undefined: `scriptname = dupstring(name);`
5764    let funcsave_scriptname = crate::ported::utils::scriptname_get();
5765    if (flags as u32 & PM_UNDEFINED) == 0 {
5766        // c:5902
5767        crate::ported::utils::set_scriptname(Some(dupstring(&name))); // c:5903
5768    }
5769
5770    // c:5904-5908 — `funcsave->zoptind = zoptind; ...` snapshot.
5771    // C zsh saves zoptind (the canonical OPTIND counter) and
5772    // zoptarg into the funcsave struct so OPTIND is implicitly
5773    // function-local: a `getopts` loop inside the function gets
5774    // its own counter that snaps back to the caller's on
5775    // function return. zshrs stores OPTIND/OPTARG in paramtab
5776    // as regular int/string params; snapshot them here and
5777    // restore at scope end. Bug #513.
5778    let funcsave_optind: Option<String> = crate::ported::params::getsparam("OPTIND");
5779    let funcsave_optarg: Option<String> = crate::ported::params::getsparam("OPTARG");
5780    // c:5966-5969 — `if (!isset(POSIXBUILTINS)) { zoptind = 1; optcind = 0; }`.
5781    // The snapshot above is only half the contract: C also RESETS the
5782    // counter on entry so an inner `getopts` loop starts fresh at the
5783    // first positional, independent of the caller's OPTIND. Without
5784    // this, a function whose body runs `getopts` after the caller
5785    // advanced OPTIND mis-parses its own args — e.g. `add-zsh-hook`
5786    // (which `getopts`-parses `precmd func`) printed its usage and
5787    // failed when invoked from a config that had run getopts earlier.
5788    // zshrs keeps the counter in the $OPTIND param plus the ZOPTIND/
5789    // OPTCIND trackers getopts syncs against; reset all three.
5790    if !crate::ported::zsh_h::isset(crate::ported::zsh_h::POSIXBUILTINS) {
5791        crate::ported::params::setiparam("OPTIND", 1); // c:5967 zoptind = 1
5792        crate::ported::builtin::ZOPTIND.store(1, Ordering::Relaxed);
5793        crate::ported::builtin::OPTCIND.store(0, Ordering::Relaxed); // c:5968 optcind = 0
5794    }
5795
5796    // c:5914 — `memcpy(funcsave->opts, opts, sizeof(opts));` — option
5797    // snapshot. Port wraps opts in OPTS_LIVE; capture the live state
5798    // here as a HashMap snapshot.
5799    let funcsave_opts = crate::ported::options::opt_state_snapshot();
5800
5801    // c:5915-5916 — `funcsave->emulation/sticky = emulation/sticky;`
5802    // Emulation snapshot pending the sticky-emulation port.
5803
5804    // c:5954-5969 — PM_TAGGED / PM_WARNNESTED option-override block.
5805    // Anonymous-function name comparison via pointer equality in C;
5806    // zshrs uses string equality. Skip until ANONYMOUS_FUNCTION_NAME
5807    // sentinel is ported.
5808
5809    // c:5970 — `funcsave->oflags = oflags;` — module-global tracking
5810    // function-attribute inheritance. Skip until oflags is ported.
5811
5812    // c:5977 — `opts[PRINTEXITVALUE] = 0;` — suppress printexitvalue
5813    // for inner commands; outer flag restored on exit.
5814    opt_state_set("printexitvalue", false);
5815
5816    // c:5978-5998 — pparams swap. C reads doshargs and constructs the
5817    // function's positional-param array. First arg is the function
5818    // name (regardless of FUNCTIONARGZERO); the rest become $1..$N.
5819    let funcsave_argv0: Option<String> = if !doshargs.is_empty() {
5820        // c:5978
5821        // c:5982-5985 — `pparams = x = zshcalloc(...)`.
5822        let positionals: Vec<String> = if doshargs.len() > 1 {
5823            doshargs[1..].to_vec()
5824        } else {
5825            Vec::new()
5826        };
5827        if let Ok(mut pp) = crate::ported::builtin::PPARAMS.lock() {
5828            *pp = positionals;
5829        }
5830        // c:5984-5987 — FUNCTIONARGZERO: save argzero, install
5831        // doshargs[0] (the function name).
5832        if isset(FUNCTIONARGZERO) {
5833            // c:5984
5834            let prev = crate::ported::utils::argzero();
5835            crate::ported::utils::set_argzero(Some(doshargs[0].clone())); // c:5986
5836            prev
5837        } else {
5838            None
5839        }
5840    } else {
5841        // c:5992-5997 — no args: empty pparams. argzero saved+dup'd.
5842        if let Ok(mut pp) = crate::ported::builtin::PPARAMS.lock() {
5843            *pp = Vec::new();
5844        }
5845        if isset(FUNCTIONARGZERO) {
5846            // c:5994
5847            let prev = crate::ported::utils::argzero();
5848            crate::ported::utils::set_argzero(prev.clone()); // c:5996 ztrdup(argzero)
5849            prev
5850        } else {
5851            None
5852        }
5853    };
5854
5855    // c:5999 — `++funcdepth;` — bumped on entry. Mirror via locallevel
5856    // since zshrs tracks function-call depth there.
5857    //
5858    // Plus the canonical startparamscope (c:6194 inside runshfunc).
5859    // zshrs's body_runner replaces runshfunc's `execode` call so the
5860    // startparamscope/endparamscope pair must wrap body_runner here,
5861    // not inside the closure. inc_locallevel is exactly startparamscope.
5862    inc_locallevel();
5863
5864    // c:6000-6004 — FUNCNEST check + `goto undoshfunc` on overflow.
5865    // Skip the runtime check (the zshrs fusevm doesn't recurse via
5866    // real stack frames so the depth limit is less critical), but
5867    // keep the comment so the C label `undoshfunc:` target is
5868    // visible — `goto undoshfunc;` here would jump straight to the
5869    // epilogue at the `undoshfunc:` label below.
5870
5871    // c:6005-6019 — funcstack frame push. The full C block:
5872    //   funcsave->fstack.name      = dupstring(name);
5873    //   funcsave->fstack.caller    = funcstack ? funcstack->name :
5874    //                                 dupstring(argv0 ? argv0 : argzero);
5875    //   funcsave->fstack.lineno    = lineno;
5876    //   funcsave->fstack.prev      = funcstack;
5877    //   funcsave->fstack.tp        = FS_FUNC;
5878    //   funcstack                  = &funcsave->fstack;
5879    //   funcsave->fstack.flineno   = shfunc->lineno;
5880    //   funcsave->fstack.filename  = getshfuncfile(shfunc);
5881    // c:6013 — `funcsave->fstack.lineno = lineno;` C has ONE lineno
5882    // global (params.c:123); zshrs mirrors it in both input::lineno
5883    // and lex::LEX_LINENO. The lex mirror is the one driven by
5884    // BUILTIN_SET_LINENO per statement AND zeroed for the duration
5885    // of a function body (see set_lineno(0) below), so it is the
5886    // one that matches C's value at call time: a call made INSIDE a
5887    // caller's body records the caller-relative line (0 for a
5888    // single-line fn), giving `$functrace` entries like `g:0`.
5889    // input::lineno stayed parked at the script-wide line and
5890    // produced `g:1`.
5891    let lineno_now = crate::ported::lex::lineno() as i64;
5892    let (caller, prev_tp): (Option<String>, Option<i32>) = {
5893        let stk = FUNCSTACK.lock().unwrap_or_else(|e| e.into_inner());
5894        if let Some(p) = stk.last() {
5895            (Some(p.name.clone()), Some(p.tp))
5896        } else {
5897            // c:6011-6012 — outermost: argv0 (saved) or argzero global.
5898            let z = funcsave_argv0
5899                .clone()
5900                .or_else(crate::ported::utils::argzero);
5901            (z, None)
5902        }
5903    };
5904    // c:6018-6019 — flineno: shfunc->lineno (function def line)
5905    let flineno = shfunc.lineno;
5906    let filename = shfunc.filename.clone().or_else(|| Some(String::new()));
5907    {
5908        let frame = crate::ported::zsh_h::funcstack {
5909            prev: None,             // c:6014 (Vec-stack: index encodes link)
5910            name: dupstring(&name), // c:6005
5911            filename,               // c:6019
5912            caller,                 // c:6011
5913            flineno,                // c:6018
5914            lineno: lineno_now,     // c:6013
5915            tp: FS_FUNC,            // c:6015
5916        };
5917        let _ = prev_tp; // c:6011 (informational)
5918        let mut stack = FUNCSTACK.lock().unwrap_or_else(|e| e.into_inner());
5919        stack.push(frame); // c:6016 funcstack = &funcsave->fstack
5920    }
5921
5922    // c:6021-6042 — body execution. C: `runshfunc(prog, wrappers, name)`.
5923    // zshrs delegates to the body_runner closure (typically a fusevm
5924    // sub-VM run from the bridge). The closure returns the body's
5925    // exit status which becomes lastval.
5926    //
5927    // c:Src/exec.c:1251-1266 — push "shfunc" onto zsh_eval_context
5928    // so the body sees `${zsh_eval_context[*]}` containing the call
5929    // chain context. The execode-based path (c:1245-1282 port at
5930    // exec.rs:7092) already did this, but the fusevm body_runner
5931    // path skipped doshfunc's body_runner invocation without the
5932    // push. Bug #262 in docs/BUGS.md.
5933    //
5934    // Push BOTH the static `zsh_eval_context` (matches C's variable)
5935    // AND the paramtab array entry (what `${zsh_eval_context[*]}`
5936    // reads). Pop on every return path via the guard struct so
5937    // panics / early returns don't leak the entry. Inlined here
5938    // (sole caller) — `zsh_eval_context` is this module's own static.
5939    //
5940    // c:Src/exec.c:1251-1266 — `zsh_eval_context[*]` shell-visible
5941    // mirror: the array entry holds the stack; the scalar
5942    // `ZSH_EVAL_CONTEXT` holds the `:`-joined form. Both written via
5943    // the PM_READONLY bypass (`u_arr`/`u_str` direct), the same shape
5944    // the binary's `-c` ZSH_EVAL_CONTEXT init uses (bins/zshrs.rs).
5945    // A reusable `sync` closure is captured by the guard's Drop so
5946    // the same write happens after the push (here) and the pop (on
5947    // every return path / panic).
5948    let sync_eval_ctx = |stack: &[String]| {
5949        let joined = stack.join(":");
5950        if let Ok(mut tab) = crate::ported::params::paramtab().write() {
5951            if let Some(pm) = tab.get_mut("zsh_eval_context") {
5952                pm.u_arr = Some(stack.to_vec());
5953                pm.node.flags &= !(crate::ported::zsh_h::PM_UNSET as i32);
5954            }
5955            if let Some(pm) = tab.get_mut("ZSH_EVAL_CONTEXT") {
5956                pm.u_str = Some(joined);
5957                pm.node.flags &= !(crate::ported::zsh_h::PM_UNSET as i32);
5958            }
5959        }
5960    };
5961    if let Ok(mut ctx) = zsh_eval_context.lock() {
5962        ctx.push("shfunc".to_string());
5963        sync_eval_ctx(&ctx);
5964    }
5965    struct EvalContextGuard<F: Fn(&[String])>(F);
5966    impl<F: Fn(&[String])> Drop for EvalContextGuard<F> {
5967        fn drop(&mut self) {
5968            if let Ok(mut ctx) = zsh_eval_context.lock() {
5969                ctx.pop();
5970                (self.0)(&ctx);
5971            }
5972        }
5973    }
5974    let _eval_ctx_guard = EvalContextGuard(sync_eval_ctx);
5975    // c:Src/exec.c — function bodies execute with `lineno` reset to
5976    // the relative line within the body (incremented per WC_PIPE
5977    // from the wordcode-encoded lineno). zsh's zerrmsg
5978    // (Src/utils.c:301) emits the lineno prefix only when lineno
5979    // is non-zero AND (!SHINSTDIN || locallevel != 0). For an
5980    // inline single-line function like `f() { x=1 }`, the body's
5981    // WC_PIPE encodes lineno=1, exec sets `lineno = lineno - 1 =
5982    // 0`, and the zerrmsg path falls through to space-only ("f: ").
5983    //
5984    // zshrs's compiler doesn't thread WC_PIPE_LINENO into the
5985    // bytecode, so the global lineno stays at the script-wide
5986    // value (1 for inline `-c`). Suppress the line-number prefix
5987    // inside function bodies by saving lineno on entry and forcing
5988    // it to 0 during body execution; restore on exit. This makes
5989    // warnings inside functions emit `f: ...` matching zsh's
5990    // single-line-function format. Bug #54/#74/#86 in docs/BUGS.md.
5991    let saved_lineno = crate::ported::lex::lineno();
5992    crate::ported::lex::set_lineno(0);
5993    // c:Src/exec.c:6173-6175 + c:6196-6198 — `runshfunc` saves
5994    // zunderscore before the body runs and restores it after, so
5995    // `$_` reads outside the function continue to reflect the
5996    // function CALL's last arg (set by setunderscore at c:3491
5997    // before doshfunc enters). Without this, commands inside the
5998    // body (`:`, `echo`, etc.) update `$_` to their own last arg,
5999    // and the post-call `echo "[$_]"` sees the body's residue
6000    // instead of the call's arg. Bug surfaced via
6001    // test_dollar_underscore_after_function_call.
6002    let saved_zunderscore = crate::ported::params::getsparam("_").unwrap_or_default();
6003    // c:6042 — `runshfunc(prog, wrappers, name)`: the FuncWrap chain.
6004    // zsh/param/private installs `wrap_private` via addwrapper at
6005    // module boot (param_private.c:712); C's runshfunc invokes it
6006    // around every function body so `private_wraplevel` tracks the
6007    // callee's locallevel and outer-scope privates get
6008    // scopeprivate-hidden for the duration. The gate here is module
6009    // BOOT STATE (MOD_INIT_B — the bit `zmodload -e` reads and
6010    // load_module sets after do_boot_module, module.c:2317) —
6011    // exactly C's condition for the wrapper being in the chain: the
6012    // `private` dispatch runs require_module per the exec.c:2710
6013    // autofeature path, which boots the module. NOT is_loaded():
6014    // default-registered static modules carry MOD_LINKED from
6015    // startup, which would arm the wrapper before any `private`
6016    // use. The dispatch is a named special-case rather than a
6017    // WRAPPERS walk because the Rust wrap_private carries a
6018    // body-delegate closure (fusevm chunk runner) that can't live
6019    // in funcwrap's WrapFunc fn-pointer slot. The pwl < locallevel
6020    // pre-check mirrors wrap_private's own c:552 gate so the
6021    // closure is guaranteed to run when routed through it.
6022    let run_wrap_private = crate::ported::module::MODULESTAB
6023        .lock()
6024        .map(|t| {
6025            t.modules
6026                .get("zsh/param/private")
6027                .map(|m| (m.node.flags & crate::ported::zsh_h::MOD_INIT_B) != 0)
6028                .unwrap_or(false)
6029        })
6030        .unwrap_or(false)
6031        && crate::ported::modules::param_private::private_wraplevel.load(Ordering::Relaxed)
6032            < crate::ported::params::locallevel.load(Ordering::Relaxed);
6033    let body_status = if run_wrap_private {
6034        let mut st = 0;
6035        let _ = crate::ported::modules::param_private::wrap_private(
6036            std::ptr::null(),
6037            std::ptr::null(),
6038            std::ptr::null_mut(),
6039            || st = body_runner(), // c:556 runshfunc(prog, w, name)
6040        );
6041        st
6042    } else {
6043        body_runner()
6044    };
6045    crate::ported::params::set_zunderscore(std::slice::from_ref(&saved_zunderscore));
6046    crate::ported::lex::set_lineno(saved_lineno);
6047    LASTVAL.store(body_status, Ordering::Relaxed);
6048
6049    // c:6043 — `doneshfunc:` label. The C `runshfunc` happy-path
6050    // falls through here from c:6042.
6051    // c:6044 — `funcstack = funcsave->fstack.prev;` — pop our frame.
6052    {
6053        let mut stack = FUNCSTACK.lock().unwrap_or_else(|e| e.into_inner());
6054        stack.pop();
6055    }
6056    // c:6045 — `undoshfunc:` label. Reached either by fall-through
6057    // from c:6044 or by `goto undoshfunc;` from the FUNCNEST check
6058    // at c:6003. Tail epilogue follows.
6059
6060    // c:6046 — `--funcdepth;` — paired endparamscope (c:6200 inside
6061    // runshfunc) lives at c:6157 below as `endparamscope()`. Removed
6062    // the dec here so locallevel only decrements once per
6063    // function-call frame; double-dec was purging level-0 globals on
6064    // function exit (the `f() { x=foo; }; f; echo $x` regression).
6065
6066    // c:6047-6053 — retflag clear. C clears retflag and restores
6067    // outer breaks if a `return` fired.
6068    if RETFLAG.load(Ordering::SeqCst) != 0 {
6069        // c:6047
6070        RETFLAG.store(0, Ordering::SeqCst); // c:6051
6071        BREAKS.store(funcsave_breaks, Ordering::SeqCst); // c:6052
6072    }
6073
6074    // c:6054-6058 — pparams + argv0 restore.
6075    if let Ok(mut pp) = crate::ported::builtin::PPARAMS.lock() {
6076        *pp = pptab; // c:6059 pparams = pptab
6077    }
6078    if let Some(saved) = funcsave_argv0 {
6079        // c:6055
6080        crate::ported::utils::set_argzero(Some(saved)); // c:6057
6081    }
6082
6083    // c:Src/exec.c:6060-6062 — `zoptind = funcsave->zoptind;
6084    // zoptarg = funcsave->zoptarg;`. Restore OPTIND/OPTARG so
6085    // an inner getopts loop's counter mutations don't leak to
6086    // the caller. Bug #513.
6087    if let Some(saved) = funcsave_optind {
6088        if let Ok(n) = saved.parse::<i64>() {
6089            crate::ported::params::setiparam("OPTIND", n);
6090        } else {
6091            crate::ported::params::setsparam("OPTIND", &saved);
6092        }
6093    }
6094    if let Some(saved) = funcsave_optarg {
6095        crate::ported::params::setsparam("OPTARG", &saved);
6096    }
6097
6098    // c:6064 — `scriptname = funcsave->scriptname;`
6099    crate::ported::utils::set_scriptname(funcsave_scriptname);
6100
6101    // c:6067 — `endpatternscope();`
6102    crate::ported::pattern::endpatternscope();
6103
6104    // c:6078-6102 — LOCALOPTIONS restore. Re-apply the snapshot when
6105    // localoptions was set inside the body.
6106    if crate::ported::options::opt_state_get("localoptions").unwrap_or(false) {
6107        // c:6091 memcpy(opts, funcsave->opts, sizeof(opts)) — full restore.
6108        let current = crate::ported::options::opt_state_snapshot();
6109        for (k, _) in &current {
6110            if !funcsave_opts.contains_key(k) {
6111                crate::ported::options::opt_state_unset(k);
6112            }
6113        }
6114        for (k, v) in &funcsave_opts {
6115            opt_state_set(k, *v);
6116        }
6117    } else {
6118        // c:6097-6101 — non-LOCALOPTIONS: restore only the always-
6119        // restored subset (XTRACE / PRINTEXITVALUE / LOCALOPTIONS /
6120        // LOCALLOOPS / WARNNESTEDVAR).
6121        for opt in [
6122            "xtrace",
6123            "printexitvalue",
6124            "localoptions",
6125            "localloops",
6126            "warnnestedvar",
6127        ] {
6128            if let Some(v) = funcsave_opts.get(opt) {
6129                opt_state_set(opt, *v);
6130            }
6131        }
6132    }
6133
6134    // c:6104-6112 — LOCALLOOPS warn-on-active-continue/break + restore
6135    // breaks/contflag/loops snapshot. Skip the warn lines for now;
6136    // restore the bookkeeping.
6137    if crate::ported::options::opt_state_get("localloops").unwrap_or(false) {
6138        BREAKS.store(funcsave_breaks, Ordering::SeqCst); // c:6109
6139        CONTFLAG.store(funcsave_contflag, Ordering::SeqCst); // c:6110
6140        LOOPS.store(funcsave_loops, Ordering::SeqCst); // c:6111
6141    }
6142
6143    // c:Src/exec.c:6195-6200 — C's runshfunc calls endparamscope()
6144    // BEFORE returning to doshfunc, which then calls endtrapscope()
6145    // at c:6114. So locallevel is ALREADY one less by the time
6146    // endtrapscope's pop loop compares saved local > current.
6147    //
6148    // Bug #80 in docs/BUGS.md: zshrs had endtrapscope FIRST (here at
6149    // line 5774), endparamscope LATER. That left locallevel at the
6150    // function's own level when endtrapscope ran, so saved entries
6151    // tagged with `local == current_function_level` failed the
6152    // `local > locallevel` pop condition. Nested EXIT traps
6153    // (saved at deeper level) never restored at the outer fn's
6154    // endtrapscope — outer EXIT traps fired at script exit instead.
6155    //
6156    // Decrement locallevel via a peer-of-endparamscope locallevel
6157    // bookkeeping call before endtrapscope, then leave the real
6158    // endparamscope at its current site below so the param scope
6159    // unwind still happens after the exit_pending check.
6160    {
6161        use crate::ported::params::locallevel as ll;
6162        let prev = ll.load(Ordering::Relaxed);
6163        if prev > 0 {
6164            ll.store(prev - 1, Ordering::Relaxed);
6165        }
6166        crate::ported::signals::endtrapscope();
6167        // Re-bump so the existing endparamscope() call below sees the
6168        // same pre-decrement state and its own internal decrement
6169        // lands at the right value (mirrors C's "endparamscope already
6170        // happened" comment at c:6135-6136 — the C order is endparam
6171        // (inside runshfunc) → endtrap (in doshfunc); we keep that
6172        // logical ordering for endtrapscope only, without disturbing
6173        // the rest of the epilogue's level math).
6174        ll.store(prev, Ordering::Relaxed);
6175    }
6176
6177    // c:6116-6117 — TRAP_STATE_PRIMED branch: bump trap_return back.
6178    if TRAP_STATE.load(Ordering::Relaxed) == TRAP_STATE_PRIMED {
6179        // c:6116
6180        TRAP_RETURN.fetch_add(1, Ordering::Relaxed); // c:6117
6181    }
6182
6183    // c:6118 — `ret = lastval;`
6184    let ret = LASTVAL.load(Ordering::Relaxed);
6185
6186    // c:6119 — `noerrexit = funcsave->noerrexit;`
6187    noerrexit.store(funcsave_noerrexit, Ordering::Relaxed);
6188
6189    // c:6120-6124 — noreturnval: restore lastval + pipestats. C runs
6190    // the function for side-effects only; outer lastval/pipestats
6191    // should reflect the PRE-call state.
6192    if noreturnval {
6193        // c:6120
6194        LASTVAL.store(funcsave_lastval, Ordering::Relaxed); // c:6121
6195        if let Some(saved_ps) = funcsave_pipestats {
6196            let n = NUMPIPESTATS.get_or_init(|| std::sync::Mutex::new(0));
6197            if let Ok(mut nguard) = n.lock() {
6198                *nguard = funcsave_numpipestats; // c:6122
6199            }
6200            let p = PIPESTATS.get_or_init(|| std::sync::Mutex::new([0; MAX_PIPESTATS]));
6201            if let Ok(mut pguard) = p.lock() {
6202                for (i, v) in saved_ps.iter().enumerate() {
6203                    if i < pguard.len() {
6204                        pguard[i] = *v; // c:6123 memcpy
6205                    }
6206                }
6207            }
6208        }
6209    }
6210
6211    // c:Src/exec.c doshfunc → endparamscope — restore local-typeset
6212    // params installed during the body. In C, this is called inside
6213    // runshfunc (c:6200) BEFORE control returns to doshfunc's tail —
6214    // so by the time the exit_pending check runs at c:6141,
6215    // locallevel has ALREADY been decremented. The c:6135-6136
6216    // comment explicitly states "The endparamscope() has already
6217    // happened, hence the +1 here."
6218    //
6219    // The previous Rust ordering placed endparamscope AFTER the
6220    // exit_pending check, which compared exit_level against the
6221    // un-decremented locallevel. For `foo() { exit 7; }; foo`:
6222    //   exit_level=1, cur_locallevel=1 (pre-decrement)
6223    //   check: exit_level >= cur_locallevel + 1 ⟹ 1 >= 2 = false
6224    // The function returned cleanly without triggering zexit, and
6225    // the shell exited 0 instead of 7. Moving endparamscope before
6226    // the check matches C and makes the off-by-one resolve.
6227    endparamscope();
6228
6229    // c:6128 — `unqueue_signals();`
6230    unqueue_signals();
6231
6232    // c:6135-6155 — exit_pending branch: when an `exit` was queued
6233    // inside the function body and we've unwound enough scopes for
6234    // it to take effect, either keep unwinding (still inside a
6235    // nested function) or actually exit the shell.
6236    let exit_pending = crate::ported::builtin::EXIT_PENDING.load(Ordering::Relaxed);
6237    let exit_level = crate::ported::builtin::EXIT_LEVEL.load(Ordering::Relaxed);
6238    let cur_locallevel = locallevel.load(Ordering::Relaxed) as i32;
6239    let cur_forklevel = FORKLEVEL.load(Ordering::Relaxed);
6240    let in_exit_trap = crate::ported::signals::in_exit_trap.load(Ordering::Relaxed); // c:Src/signals.c:63
6241    if exit_pending != 0 && exit_level >= cur_locallevel + 1 && in_exit_trap == 0 {
6242        // c:6141
6243        if cur_locallevel > cur_forklevel {
6244            // c:6143 — still inside a nested function: keep unwinding.
6245            RETFLAG.store(1, Ordering::Relaxed); // c:6144
6246            BREAKS.store(LOOPS.load(Ordering::Relaxed), Ordering::Relaxed); // c:6145
6247        } else {
6248            // c:6151 — out of all functions: exit for real.
6249            crate::ported::builtin::STOPMSG.store(1, Ordering::Relaxed); // c:6151
6250            let val = EXIT_VAL.load(Ordering::Relaxed);
6251            crate::ported::builtin::zexit(val, crate::ported::zsh_h::ZEXIT_NORMAL);
6252            // c:6152
6253        }
6254    }
6255
6256    ret // c:6157 return ret
6257}
6258
6259/// `TRAP_STATE_PRIMED` per `Src/signals.h:55` — doshfunc tests this
6260/// to decide whether to bump trap_return on entry/exit. Local
6261/// const here because the canonical zsh_h port doesn't carry
6262/// trap-state numeric constants yet.
6263const TRAP_STATE_PRIMED: i32 = 2; // c:Src/signals.h:55
6264
6265/// Port of `execfuncdef(Estate state, Eprog redir_prog)` from
6266/// `Src/exec.c:5309-5494`. Define a shell function: extract
6267/// name(s)+body from the wordcode payload, allocate the Shfunc,
6268/// install into `shfunctab` (named), or execute immediately (anon).
6269#[allow(non_snake_case)]
6270pub fn execfuncdef(state: &mut estate, mut redir_prog: Option<crate::ported::zsh_h::Eprog>) -> i32 {
6271    use crate::ported::hashtable::{dircache_set, shfunctab_lock};
6272    use crate::ported::jobs::{getsigidx, removetrapnode};
6273    use crate::ported::parse::{dupeprog, freeeprog, incrdumpcount};
6274    use crate::ported::signals::settrap;
6275    use crate::ported::utils::scriptfilename_get;
6276    use crate::ported::zsh_h::{
6277        eprog as eprog_t, hashnode, patprog as patprog_t, shfunc as shfunc_t, Patprog,
6278        EC_DUPTOK as _, EF_HEAP, EF_MAP, EF_REAL, FS_EVAL, FS_FUNC, PM_ANONYMOUS, PM_TAGGED,
6279        PM_TAGGED_LOCAL, PRINTEXITVALUE, SHINSTDIN, ZSIG_FUNC,
6280    };
6281    // c:5311 — `Shfunc shf;`
6282    let mut shf: Box<shfunc_t>;
6283    // c:5312 — `char *s = NULL;`
6284    let mut s: Option<String> = None;
6285    // c:5313 — `int signum, nprg, sbeg, nstrs, npats, do_tracing, len, plen, i, htok = 0, ret = 0;`
6286    let mut signum: i32;
6287    let nprg: i32;
6288    let sbeg: i32;
6289    let nstrs: i32;
6290    let npats: i32;
6291    let do_tracing: i32;
6292    let len: i32;
6293    let plen: i32;
6294    // `i` — C loop counter for pp stamp; Rust uses .map().collect().
6295    let mut htok: i32 = 0;
6296    let mut ret: i32 = 0;
6297    // c:5314 — `int anon_func = 0;`
6298    let mut anon_func: i32 = 0;
6299    // c:5315 — `Wordcode beg = state->pc, end;`
6300    let _beg: usize = state.pc;
6301    let mut end: usize;
6302    // c:5316 — `Eprog prog;`
6303    // (allocated inline per-iter below; no upfront binding needed)
6304    // c:5317 — `Patprog *pp;` — handled by Vec construction.
6305    // c:5318 — `LinkList names;`
6306    let names: Vec<String>;
6307    // c:5319 — `int tracing_flags;`
6308    let tracing_flags: i32;
6309
6310    // c:5321 — `end = beg + WC_FUNCDEF_SKIP(state->pc[-1]);`
6311    end = state.pc + WC_FUNCDEF_SKIP(state.prog.prog[state.pc.wrapping_sub(1)]) as usize;
6312    // c:5322 — `names = ecgetlist(state, *state->pc++, EC_DUPTOK, &htok);`
6313    let num = state.prog.prog[state.pc] as usize;
6314    state.pc += 1;
6315    names = ecgetlist(state, num, EC_DUPTOK, Some(&mut htok));
6316    // c:5323 — `sbeg = *state->pc++;`
6317    sbeg = state.prog.prog[state.pc] as i32;
6318    state.pc += 1;
6319    // c:5324 — `nstrs = *state->pc++;`
6320    nstrs = state.prog.prog[state.pc] as i32;
6321    state.pc += 1;
6322    // c:5325 — `npats = *state->pc++;`
6323    npats = state.prog.prog[state.pc] as i32;
6324    state.pc += 1;
6325    // c:5326 — `do_tracing = *state->pc++;`
6326    do_tracing = state.prog.prog[state.pc] as i32;
6327    state.pc += 1;
6328
6329    // c:5328 — `nprg = (end - state->pc);`
6330    nprg = end.saturating_sub(state.pc) as i32;
6331    // c:5329 — `plen = nprg * sizeof(wordcode);`
6332    plen = nprg.saturating_mul(size_of::<wordcode>() as i32);
6333    // c:5330 — `len = plen + (npats * sizeof(Patprog)) + nstrs;`
6334    len = plen + npats.saturating_mul(size_of::<usize>() as i32) + nstrs;
6335    // c:5331 — `tracing_flags = do_tracing ? PM_TAGGED_LOCAL : 0;`
6336    tracing_flags = if do_tracing != 0 {
6337        PM_TAGGED_LOCAL as i32
6338    } else {
6339        0
6340    };
6341
6342    // c:5333-5339 — htok name substitution.
6343    let mut names_mut: Vec<String> = names;
6344    if htok != 0 && !names_mut.is_empty() {
6345        execsubst(&mut names_mut); // c:5334
6346        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
6347            // c:5335
6348            state.pc = end; // c:5336
6349            return 1; // c:5337
6350        }
6351    }
6352
6353    // c:5341-5342 DPUTS — debug assertion (anon + redir simultaneously).
6354    // Not portable as panic; left as comment.
6355
6356    // c:5343 — `while (!names || (s = (char *) ugetnode(names))) {`
6357    // num==0 → anon (no names); else iterate names.
6358    let mut names_iter = names_mut.into_iter();
6359    loop {
6360        let no_names = num == 0;
6361        if !no_names {
6362            // c:5343 — `s = ugetnode(names)`; break when list exhausted.
6363            match names_iter.next() {
6364                Some(nm) => s = Some(nm),
6365                None => break,
6366            }
6367        }
6368        // c:5344-5374 — Eprog alloc.
6369        let prog: Box<eprog_t>;
6370        let dump_present = state.prog.dump.is_some();
6371        let make_pat = || -> Patprog {
6372            // c:5375-5376 `*pp = dummy_patprog1;` — sentinel slot.
6373            Box::new(patprog_t {
6374                startoff: 0,
6375                size: 0,
6376                mustoff: 0,
6377                patmlen: 0,
6378                globflags: 0,
6379                globend: 0,
6380                flags: 0,
6381                patnpar: 0,
6382                patstartch: 0,
6383            })
6384        };
6385        if no_names {
6386            // c:5345-5346 — `zhalloc`, `nref = -1`.
6387            // c:5355-5357 — EF_HEAP, no dump, npats pats on heap.
6388            let pats: Vec<Patprog> = (0..npats).map(|_| make_pat()).collect();
6389            let prog_words: Vec<wordcode> = state.prog.prog[state.pc..end].to_vec();
6390            // c:5365 — `prog->strs = state->strs + sbeg;`
6391            let strs_tail = state.strs.as_ref().map(|t| {
6392                let off = (sbeg as usize).min(t.len());
6393                t[off..].to_string()
6394            });
6395            prog = Box::new(eprog_t {
6396                flags: EF_HEAP,
6397                len,
6398                npats,
6399                nref: -1, // c:5346
6400                pats,
6401                prog: prog_words,
6402                strs: strs_tail,
6403                shf: None,  // c:5377
6404                dump: None, // c:5356
6405                strs_metafied: false, // native pool — clean UTF-8
6406            });
6407        } else if dump_present {
6408            // c:5358-5363 — EF_MAP path: refcount the dump, allocate
6409            // pats permanent, reuse `state->pc` slice in place.
6410            if let Some(dp) = state.prog.dump.as_deref() {
6411                incrdumpcount(dp); // c:5360
6412            }
6413            let pats: Vec<Patprog> = (0..npats).map(|_| make_pat()).collect();
6414            let prog_words: Vec<wordcode> = state.prog.prog[state.pc..end].to_vec();
6415            let strs_tail = state.strs.as_ref().map(|t| {
6416                let off = (sbeg as usize).min(t.len());
6417                t[off..].to_string()
6418            });
6419            prog = Box::new(eprog_t {
6420                flags: EF_MAP, // c:5359
6421                len,
6422                npats,
6423                nref: 1, // c:5349
6424                pats,
6425                prog: prog_words,
6426                strs: strs_tail,
6427                shf: None,                     // c:5377
6428                dump: state.prog.dump.clone(), // c:5361
6429                strs_metafied: state.prog.strs_metafied, // pool copied verbatim — carry provenance
6430            });
6431        } else {
6432            // c:5366-5374 — EF_REAL: copy wordcode + strs into a
6433            // freshly-owned eprog (no shared dump backing).
6434            let pats: Vec<Patprog> = (0..npats).map(|_| make_pat()).collect();
6435            let pc_end = state.pc + nprg as usize;
6436            let prog_words: Vec<wordcode> = state.prog.prog[state.pc..pc_end].to_vec();
6437            // c:5373 — `memcpy(prog->strs, state->strs + sbeg, nstrs);`
6438            let strs_copy = state.strs.as_ref().map(|t| {
6439                let off = (sbeg as usize).min(t.len());
6440                let n_avail = t.len().saturating_sub(off);
6441                let take = (nstrs as usize).min(n_avail);
6442                t[off..off + take].to_string()
6443            });
6444            prog = Box::new(eprog_t {
6445                flags: EF_REAL, // c:5367
6446                len,
6447                npats,
6448                nref: 1, // c:5349
6449                pats,
6450                prog: prog_words,
6451                strs: strs_copy,
6452                shf: None,  // c:5377
6453                dump: None, // c:5371
6454                strs_metafied: false, // native pool — clean UTF-8
6455            });
6456        }
6457
6458        // c:5379-5381 — Shfunc alloc + funcdef + tracing flags.
6459        shf = Box::new(shfunc_t {
6460            node: hashnode {
6461                next: None,
6462                nam: String::new(),
6463                flags: tracing_flags,
6464            },
6465            filename: scriptfilename_get(), // c:5383 `ztrdup(scriptfilename)`
6466            // c:5384-5388 — funcstack top FS_FUNC/FS_EVAL → flineno+lineno
6467            // else just lineno.
6468            lineno: {
6469                let cur_lineno = crate::ported::input::lineno.with(|l| l.get()) as i64;
6470                if let Ok(stk) = crate::ported::modules::parameter::FUNCSTACK.lock() {
6471                    if let Some(top) = stk.last() {
6472                        if top.tp == FS_FUNC || top.tp == FS_EVAL {
6473                            top.flineno + cur_lineno
6474                        } else {
6475                            cur_lineno
6476                        }
6477                    } else {
6478                        cur_lineno
6479                    }
6480                } else {
6481                    cur_lineno
6482                }
6483            },
6484            funcdef: Some(prog), // c:5380
6485            redir: None,
6486            sticky: None,
6487            body: None,
6488        });
6489        // c:5396-5401 — redir_prog ownership.
6490        // C: `if (names && nonempty(names) && redir_prog) shf->redir = dupeprog(redir_prog,0)`
6491        // else `shf->redir = redir_prog; redir_prog = 0;`
6492        // "nonempty(names)" means there's a NEXT name still to consume —
6493        // i.e. peek the iterator.
6494        if !no_names && names_iter.len() > 0 && redir_prog.is_some() {
6495            // c:5397 — dupe so each earlier name gets its own copy; the
6496            // last name (when iterator drains) gets the original.
6497            if let Some(rp) = redir_prog.as_deref() {
6498                shf.redir = Some(Box::new(dupeprog(rp, false)));
6499            }
6500        } else {
6501            // c:5399-5400 — last name (or anon) takes original.
6502            shf.redir = redir_prog.take();
6503        }
6504        // c:5402 — `shfunc_set_sticky(shf);`
6505        shfunc_set_sticky(&mut shf);
6506
6507        if no_names {
6508            // c:5404-5457 — anonymous function: execute immediately.
6509            // `LinkList args;` c:5409
6510            let mut args: Vec<String>;
6511
6512            anon_func = 1; // c:5411
6513            shf.node.flags |= PM_ANONYMOUS as i32; // c:5412
6514
6515            state.pc = end; // c:5414
6516                            // c:5415 — `end += *state->pc++;`
6517            end += state.prog.prog[state.pc] as usize;
6518            state.pc += 1;
6519            // c:5416 — `args = ecgetlist(state, *state->pc++, EC_DUPTOK, &htok);`
6520            let arg_count = state.prog.prog[state.pc] as usize;
6521            state.pc += 1;
6522            args = ecgetlist(state, arg_count, EC_DUPTOK, Some(&mut htok));
6523
6524            // c:5418-5429 — htok arg subst + cleanup-on-error.
6525            if htok != 0 && !args.is_empty() {
6526                execsubst(&mut args); // c:5419
6527                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
6528                    // c:5421 — `freeeprog(shf->funcdef);`
6529                    if let Some(mut fd) = shf.funcdef.take() {
6530                        freeeprog(&mut fd);
6531                    }
6532                    if shf.redir.is_some() {
6533                        // c:5422-5423 — "shouldn't be" anon+redir, but free if so.
6534                        if let Some(mut rd) = shf.redir.take() {
6535                            freeeprog(&mut rd);
6536                        }
6537                    }
6538                    dircache_set(&mut shf.filename, None); // c:5424
6539                    drop(shf); // c:5425 `zfree(shf, sizeof(*shf));`
6540                    state.pc = end; // c:5426
6541                    return 1; // c:5427
6542                }
6543            }
6544
6545            // c:5431-5432 — `setunderscore` to last arg (or "").
6546            let under_val = if !args.is_empty() {
6547                args.last().cloned().unwrap_or_default()
6548            } else {
6549                String::new()
6550            };
6551            setunderscore(&under_val);
6552
6553            // c:5434-5435 — `if (!args) args = newlinklist();`
6554            // (Rust Vec is never null; no-op.)
6555            shf.node.nam = ANONYMOUS_FUNCTION_NAME.to_string(); // c:5436
6556                                                                // c:5437 — `pushnode(args, shf->node.nam);` — prepend.
6557            args.insert(0, shf.node.nam.clone());
6558
6559            execshfunc(&mut shf, &mut args); // c:5439
6560            ret = LASTVAL.load(Ordering::Relaxed); // c:5440
6561
6562            // c:5442-5450 — PRINTEXITVALUE+SHINSTDIN exit report.
6563            if isset(PRINTEXITVALUE) && isset(SHINSTDIN) && ret != 0 {
6564                eprintln!("zsh: exit {}", ret); // c:5445/5447
6565            }
6566
6567            // c:5452-5456 — cleanup.
6568            if let Some(mut fd) = shf.funcdef.take() {
6569                freeeprog(&mut fd);
6570            }
6571            if let Some(mut rd) = shf.redir.take() {
6572                // c:5453-5454 — "shouldn't be" but free if present.
6573                freeeprog(&mut rd);
6574            }
6575            dircache_set(&mut shf.filename, None); // c:5455
6576            drop(shf); // c:5456 `zfree(shf, sizeof(*shf));`
6577            break; // c:5457
6578        } else {
6579            // c:5458-5484 — named function path.
6580            let nm = s.as_deref().unwrap_or("");
6581            // c:5460-5475 — TRAP* signal-trap install.
6582            if nm.len() > 4 && nm.starts_with("TRAP") {
6583                if let Some(sn) = getsigidx(&nm[4..]) {
6584                    signum = sn;
6585                    // c:5462 — `if (settrap(signum, NULL, ZSIG_FUNC))`
6586                    if settrap(signum, None, ZSIG_FUNC) != 0 {
6587                        if let Some(mut fd) = shf.funcdef.take() {
6588                            freeeprog(&mut fd); // c:5463
6589                        }
6590                        dircache_set(&mut shf.filename, None); // c:5464
6591                        drop(shf); // c:5465
6592                        state.pc = end; // c:5466
6593                        return 1; // c:5467
6594                    }
6595                    // c:5474 — `removetrapnode(signum);`
6596                    removetrapnode(signum);
6597                    // c:Src/signals.c::settrap → unsettrap →
6598                    // removetrap also clears sigfuncs[sig] (the C
6599                    // string-form trap slot). zshrs's port stores
6600                    // string-form bodies in a separate
6601                    // `traps_table` HashMap not touched by
6602                    // removetrap. Drop the string-form entry here
6603                    // so dotrap's fallback doesn't double-dispatch
6604                    // when a TRAPxxx function REPLACES an
6605                    // existing `trap '...' SIG` registration. Bug
6606                    // #541 in docs/BUGS.md.
6607                    if let Ok(mut t) = crate::ported::builtin::traps_table().lock() {
6608                        t.remove(&nm[4..]);
6609                    }
6610                }
6611            }
6612            // c:5477-5482 — re-define-self trace flag propagate.
6613            if let Ok(stk) = crate::ported::modules::parameter::FUNCSTACK.lock() {
6614                if let Some(top) = stk.last() {
6615                    if top.tp == FS_FUNC && top.name == nm {
6616                        // c:5479 — `Shfunc old = shfunctab->getnode(s);`
6617                        if let Ok(rd) = shfunctab_lock().read() {
6618                            if let Some(old) = rd.get(nm) {
6619                                // c:5481 — propagate PM_TAGGED|PM_TAGGED_LOCAL.
6620                                shf.node.flags |=
6621                                    old.node.flags & (PM_TAGGED as i32 | PM_TAGGED_LOCAL as i32);
6622                            }
6623                        }
6624                    }
6625                }
6626            }
6627            // c:5483 — `shfunctab->addnode(shfunctab, ztrdup(s), shf);`
6628            shf.node.nam = nm.to_string();
6629            if let Ok(mut wr) = shfunctab_lock().write() {
6630                wr.add(*shf);
6631            }
6632        }
6633    }
6634    // c:5486-5487 — `if (!anon_func) setunderscore("");`
6635    if anon_func == 0 {
6636        setunderscore("");
6637    }
6638    // c:5488-5491 — leftover redir cleanup ("shouldn't happen").
6639    if let Some(mut rd) = redir_prog.take() {
6640        freeeprog(&mut rd);
6641    }
6642    // c:5492 — `state->pc = end;`
6643    state.pc = end;
6644    // c:5493 — `return ret;`
6645    ret
6646}
6647
6648/// Port of `execsimple(Estate state)` from `Src/exec.c:1290-1340`.
6649/// Fast-path for single-Simple commands that bypasses the full
6650/// `execcmd_exec` machinery.
6651pub fn execsimple(state: &mut estate) -> i32 {
6652    // c:1292 — `wordcode code = *state->pc++;`
6653    let mut code = state.prog.prog[state.pc];
6654    state.pc += 1;
6655    // c:1295-1296 — `if (errflag) return (lastval = 1);`
6656    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
6657        LASTVAL.store(1, Ordering::Relaxed);
6658        return 1;
6659    }
6660    // c:1298-1299 — `if (!isset(EXECOPT)) return lastval = 0;`
6661    if !isset(crate::ported::zsh_h::EXECOPT) {
6662        LASTVAL.store(0, Ordering::Relaxed);
6663        return 0;
6664    }
6665    // c:1301-1303 — `if (!IN_EVAL_TRAP() && !ineval && code) lineno = code - 1;`
6666    // In evaluated traps, don't modify the line number (the trap
6667    // dispatcher restores it). `code` here is the wordcode-encoded
6668    // line number from the WC_SIMPLE entry at state.pc-1.
6669    if !crate::ported::zsh_h::IN_EVAL_TRAP()
6670        && crate::ported::builtin::INEVAL.load(Ordering::SeqCst) == 0
6671        && code != 0
6672    {
6673        crate::ported::input::lineno.with(|l| l.set((code as usize).saturating_sub(1)));
6674    }
6675    // c:1306 — `code = wc_code(*state->pc++);`
6676    code = wc_code(state.prog.prog[state.pc]);
6677    state.pc += 1;
6678    // c:1311-1312 — `otj = thisjob; thisjob = -1;`
6679    let otj = *THISJOB
6680        .get_or_init(|| std::sync::Mutex::new(-1))
6681        .lock()
6682        .unwrap();
6683    *THISJOB
6684        .get_or_init(|| std::sync::Mutex::new(-1))
6685        .lock()
6686        .unwrap() = -1;
6687    use crate::ported::zsh_h::{
6688        WC_ARITH, WC_CASE, WC_COND, WC_FOR, WC_REPEAT, WC_SELECT, WC_SUBSH, WC_TIMED, WC_TRY,
6689        WC_WHILE,
6690    };
6691    use crate::ported::zsh_h::{WC_ASSIGN, WC_CURSH};
6692    let lv = if code == WC_ASSIGN {
6693        // c:1315-1319 — assignment-only simple cmd path.
6694        // cmdoutval = 0; addvars(state, state->pc - 1, 0); setunderscore("");
6695        addvars(state, state.pc.saturating_sub(1), 0);
6696        setunderscore(""); // c:1317
6697        if isset(XTRACE) {
6698            eprintln!();
6699        }
6700        let ef = errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR;
6701        if ef != 0 {
6702            ef
6703        } else {
6704            0
6705        }
6706    } else {
6707        // c:1322-1330 — dispatch via execfuncs[code - WC_CURSH] or execfuncdef.
6708        let q = queue_signal_level();
6709        dont_queue_signals();
6710        let result = if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
6711            ERRFLAG_ERROR
6712        } else if code == WC_FUNCDEF {
6713            execfuncdef(state, None)
6714        } else {
6715            // c:5499 execfuncs[] table inlined — match the WC_* tag.
6716            match code {
6717                WC_CURSH => execcursh(state, 0),
6718                WC_SUBSH => execcursh(state, 0), // subshell folds to cursh body walk
6719                WC_FOR => execfor(state, 0),
6720                WC_SELECT => execselect(state, 0),
6721                WC_CASE => execcase(state, 0),
6722                WC_IF => execif(state, 0),
6723                WC_WHILE => execwhile(state, 0),
6724                WC_REPEAT => execrepeat(state, 0),
6725                WC_TIMED => exectime(state, 0),
6726                WC_COND => execcond(state, 0),
6727                WC_ARITH => execarith(state, 0),
6728                WC_TRY => exectry(state, 0),
6729                _ => 0,
6730            }
6731        };
6732        restore_queue_signals(q);
6733        result
6734    };
6735    // c:1334 — `thisjob = otj;`
6736    *THISJOB
6737        .get_or_init(|| std::sync::Mutex::new(-1))
6738        .lock()
6739        .unwrap() = otj;
6740    LASTVAL.store(lv, Ordering::Relaxed); // c:1336 — `return lastval = lv;`
6741    lv
6742}
6743
6744/// Port of `execlist(Estate state, int dont_change_job, int exiting)`
6745/// from `Src/exec.c:1349-1665`. Walks WC_LIST entries, dispatches each
6746/// sublist (WC_SUBLIST chain inlined per c:1525-1625, same as C —
6747/// there's no separate execsublist function), handles signal-trap
6748/// dispatch + ERREXIT propagation.
6749///
6750/// Body ports the structural skeleton faithfully (WC_LIST walk,
6751/// per-iteration breaks/retflag/errflag guards, ltype dispatch on
6752/// Z_END/Z_SYNC/Z_ASYNC, donetrap handling). The full signal queue
6753/// + DEBUGBEFORECMD trap machinery from c:1357-1500 is preserved
6754/// in shape with TODO-citations where dependent primitives aren't
6755/// yet ported.
6756pub fn execlist(state: &mut estate, dont_change_job: i32, mut exiting: i32) -> i32 {
6757    let mut last_status: i32 = 0;
6758    let mut donetrap: i32 = 0; // c:1352 — `static int donetrap;`
6759    let cj = *THISJOB
6760        .get_or_init(|| std::sync::Mutex::new(-1))
6761        .lock()
6762        .unwrap(); // c:1364 — `cj = thisjob;`
6763    let _ = dont_change_job; // c:1361 — restored on exit if nonzero.
6764                             // c:1380 — `code = *state->pc++;`
6765    if state.pc >= state.prog.prog.len() {
6766        return last_status;
6767    }
6768    let mut code = state.prog.prog[state.pc];
6769    state.pc += 1;
6770    // c:1382-1384 — empty list returns lastval = 0.
6771    if wc_code(code) != WC_LIST {
6772        LASTVAL.store(0, Ordering::Relaxed);
6773        return 0;
6774    }
6775    use crate::ported::zsh_h::{WC_LIST_SKIP, WC_LIST_TYPE, Z_END, Z_SIMPLE, Z_SYNC};
6776    // c:1385-1499 — main WC_LIST loop.
6777    while wc_code(code) == WC_LIST
6778        && BREAKS.load(Ordering::SeqCst) == 0
6779        && RETFLAG.load(Ordering::SeqCst) == 0
6780        && (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0
6781    {
6782        let ltype = WC_LIST_TYPE(code) as i32;
6783        // c:1396 — `csp = cmdsp;` — snapshot cmdstack depth at start
6784        // of this WC_LIST iteration; restored at end so partial
6785        // cmdpush sequences (e.g. from execcond, execfuncs) don't
6786        // leak into the next sublist.
6787        let csp = crate::ported::prompt::CMDSTACK.with(|s| s.borrow().len());
6788        // c:1502-1509 — Z_SIMPLE fast-path.
6789        if (ltype & Z_SIMPLE as i32) != 0 {
6790            let next_pc = state.pc + WC_LIST_SKIP(code) as usize;
6791            let s = execsimple(state);
6792            last_status = s;
6793            state.pc = next_pc;
6794        } else {
6795            // c:1513-1523 — sublist chain.
6796            if state.pc >= state.prog.prog.len() {
6797                break;
6798            }
6799            code = state.prog.prog[state.pc];
6800            state.pc += 1;
6801            // c:1525-1625 — sublist chain (&&/|| operators) inlined.
6802            use crate::ported::zsh_h::{
6803                WC_SUBLIST_AND, WC_SUBLIST_END, WC_SUBLIST_NOT, WC_SUBLIST_OR, WC_SUBLIST_SIMPLE,
6804                WC_SUBLIST_SKIP,
6805            };
6806            let mut sub_code = code;
6807            let _ = dont_change_job;
6808            while wc_code(sub_code) == WC_SUBLIST {
6809                let flags = WC_SUBLIST_FLAGS(sub_code);
6810                let next = state.pc + WC_SUBLIST_SKIP(sub_code) as usize;
6811                let sl_type = WC_SUBLIST_TYPE(sub_code) as i32;
6812                let last1 = if WC_SUBLIST_TYPE(sub_code) == WC_SUBLIST_END {
6813                    exiting
6814                } else {
6815                    0
6816                };
6817                if flags == WC_SUBLIST_SIMPLE {
6818                    last_status = execsimple(state); // c:1605
6819                } else {
6820                    let _ = execpline(state, sub_code, sl_type, last1); // c:1607
6821                    last_status = LASTVAL.load(Ordering::Relaxed);
6822                }
6823                // c:1612 — `WC_SUBLIST_NOT` inverts status.
6824                if (flags & WC_SUBLIST_NOT) != 0 {
6825                    last_status = if last_status == 0 { 1 } else { 0 };
6826                    LASTVAL.store(last_status, Ordering::Relaxed);
6827                }
6828                state.pc = next;
6829                if WC_SUBLIST_TYPE(sub_code) == WC_SUBLIST_END {
6830                    break;
6831                }
6832                if state.pc >= state.prog.prog.len() {
6833                    break;
6834                }
6835                // c:1617-1623 — short-circuit on && / ||.
6836                if sl_type == WC_SUBLIST_AND as i32 && last_status != 0 {
6837                    while state.pc < state.prog.prog.len() {
6838                        let c = state.prog.prog[state.pc];
6839                        if wc_code(c) != WC_SUBLIST {
6840                            break;
6841                        }
6842                        state.pc = state.pc + 1 + WC_SUBLIST_SKIP(c) as usize;
6843                        if WC_SUBLIST_TYPE(c) == WC_SUBLIST_END {
6844                            break;
6845                        }
6846                    }
6847                    break;
6848                }
6849                if sl_type == WC_SUBLIST_OR as i32 && last_status == 0 {
6850                    while state.pc < state.prog.prog.len() {
6851                        let c = state.prog.prog[state.pc];
6852                        if wc_code(c) != WC_SUBLIST {
6853                            break;
6854                        }
6855                        state.pc = state.pc + 1 + WC_SUBLIST_SKIP(c) as usize;
6856                        if WC_SUBLIST_TYPE(c) == WC_SUBLIST_END {
6857                            break;
6858                        }
6859                    }
6860                    break;
6861                }
6862                sub_code = state.prog.prog[state.pc];
6863                state.pc += 1;
6864            }
6865        }
6866        // c:1593 — `cmdsp = csp;` — restore cmdstack depth to the
6867        // snapshot taken at start of iteration. Reverses any cmdpush
6868        // calls made by nested execcond / execfuncs / execcmd_exec
6869        // that didn't pop cleanly.
6870        crate::ported::prompt::CMDSTACK.with(|s| {
6871            let mut g = s.borrow_mut();
6872            if g.len() > csp {
6873                g.truncate(csp);
6874            }
6875        });
6876        // c:1626-1634 — donetrap is reset between sublists.
6877        donetrap = 0;
6878        // c:1640-1645 — fetch next WC_LIST header (or break out).
6879        if state.pc >= state.prog.prog.len() {
6880            break;
6881        }
6882        let next_code = state.prog.prog[state.pc];
6883        if wc_code(next_code) != WC_LIST {
6884            break;
6885        }
6886        state.pc += 1;
6887        code = next_code;
6888        // c:1389 — z_end means last sublist, exiting becomes 1 for tail-exec.
6889        if (ltype & Z_END as i32) != 0 {
6890            exiting = 1;
6891        }
6892    }
6893    // c:1659-1664 — cleanup: restore thisjob if dont_change_job, this_noerrexit=1.
6894    if dont_change_job != 0 {
6895        *THISJOB
6896            .get_or_init(|| std::sync::Mutex::new(-1))
6897            .lock()
6898            .unwrap() = cj;
6899    }
6900    let _ = donetrap;
6901    this_noerrexit.store(1, Ordering::Relaxed);
6902    LASTVAL.store(last_status, Ordering::Relaxed);
6903    last_status
6904}
6905
6906// WC_SUBLIST chain walk is inlined into execlist (per `Src/exec.c:1525-
6907// 1625`, the C source likewise inlines it — there's no `execsublist`
6908// function in zsh C).
6909
6910/// Port of `execcmd_getargs(LinkList preargs, LinkList args, int expand)`
6911/// from `Src/exec.c:2791-2806`. Transfer the first node of `args`
6912/// to `preargs`, performing `prefork` (singleton-list expansion) on
6913/// the way if `expand` is set. Used by `execcmd_exec` to pull the
6914/// command head one word at a time so prefix-modifier walking
6915/// (BINF_COMMAND, BINF_EXEC etc.) sees expanded names.
6916pub fn execcmd_getargs(preargs: &mut LinkList<String>, args: &mut LinkList<String>, expand: i32) {
6917    // c:2791
6918    if args.firstnode().is_none() {
6919        // c:2793 — `if (!firstnode(args)) return;`
6920        return;
6921    } else if expand != 0 {
6922        // c:2795
6923        // c:2796-2797 — `local_list0(svl); init_list0(svl);` —
6924        // stack-local single-bucket list. Rust uses a fresh
6925        // LinkList<String> per call.
6926        let mut svl: LinkList<String> = Default::default();
6927        // c:2799 — `addlinknode(&svl, uremnode(args, firstnode(args)));`
6928        if let Some(idx) = args.firstnode() {
6929            if let Some(head) = crate::ported::linklist::uremnode(args, idx) {
6930                svl.push_back(head);
6931            }
6932        }
6933        // c:2801 — `prefork(&svl, 0, NULL);`
6934        let mut rf = 0i32;
6935        prefork(&mut svl, 0, &mut rf);
6936        // c:2802 — `joinlists(preargs, &svl);`
6937        crate::ported::linklist::joinlists(preargs, &mut svl);
6938    } else {
6939        // c:2803-2804 — no-expand path: move head verbatim.
6940        if let Some(idx) = args.firstnode() {
6941            if let Some(head) = crate::ported::linklist::uremnode(args, idx) {
6942                preargs.push_back(head);
6943            }
6944        }
6945    }
6946}
6947
6948/// Port of `execcmd_fork(Estate state, int how, int type,
6949/// Wordcode varspc, LinkList *filelistp, char *text, int oautocont,
6950/// int close_if_forked)` from `Src/exec.c:2810-2893`.
6951///
6952/// Fork the current command into a child process: parent records
6953/// the pid + STTY env scan + addproc; child enters subshell, writes
6954/// `entersubsh_ret` back to parent through `synch` pipe, and returns
6955/// 0 so the caller can continue with the body.
6956///
6957/// `filelistp` out-arg is moved from `jobtab[thisjob].filelist`
6958/// only in the child branch (so the parent's `filelist` stays
6959/// untouched). Rust sig keeps the same C contract.
6960pub fn execcmd_fork(
6961    state: &mut estate,
6962    how: i32,
6963    typ: i32,
6964    varspc: Option<usize>,
6965    filelistp: &mut Vec<jobfile>,
6966    text: &str,
6967    oautocont: i32,
6968    close_if_forked: i32,
6969) -> i32 {
6970    use crate::ported::signals::sigtrapped as sigtrapped_static;
6971    use crate::ported::signals_h::SIGEXIT;
6972    use crate::ported::zsh_h::{
6973        AUTOCONTINUE, BGNICE, WC_ASSIGN as ZWC_ASSIGN, WC_ASSIGN_NUM as ZWC_ASSIGN_NUM,
6974        WC_ASSIGN_SCALAR as ZWC_ASSIGN_SCALAR, WC_ASSIGN_TYPE as ZWC_ASSIGN_TYPE,
6975        WC_SUBSH as ZWC_SUBSH, ZSIG_IGNORED, Z_ASYNC,
6976    };
6977    // c:2810
6978    let pid: libc::pid_t; // c:2814
6979    let mut synch: [i32; 2] = [-1, -1]; // c:2815
6980    let flags: i32; // c:2815
6981    let mut esret: entersubsh_ret = entersubsh_ret::default(); // c:2816
6982                                                               // c:2817 — `struct timespec bgtime;` — bgtime is passed to zfork
6983                                                               // for accounting; the Rust zfork wrapper expects Option<&mut ZshTimespec>.
6984    let mut bgtime = ZshTimespec::default();
6985
6986    child_block(); // c:2819
6987    esret.gleader = -1; // c:2820
6988    esret.list_pipe_job = -1; // c:2821
6989
6990    // c:2823 — `if (pipe(synch) < 0) { zerr("pipe failed: %e", errno); return -1; }`
6991    if unsafe { libc::pipe(synch.as_mut_ptr()) } < 0 {
6992        zerr(&format!("pipe failed: {}", std::io::Error::last_os_error()));
6993        return -1; // c:2825
6994    }
6995    // c:2826 — `else if ((pid = zfork(&bgtime)) == -1) { ... }`
6996    pid = zfork(Some(&mut bgtime));
6997    if pid == -1 {
6998        unsafe {
6999            libc::close(synch[0]); // c:2827
7000            libc::close(synch[1]); // c:2828
7001        }
7002        LASTVAL.store(1, Ordering::Relaxed); // c:2829
7003        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:2830
7004        return -1; // c:2831
7005    }
7006    if pid != 0 {
7007        // c:2833 — parent.
7008        unsafe { libc::close(synch[1]) }; // c:2834
7009                                          // c:2835 — `read_loop(synch[0], (char *)&esret, sizeof(esret));`
7010        let mut buf = [0u8; size_of::<entersubsh_ret>()];
7011        let _ = crate::ported::utils::read_loop(synch[0], &mut buf);
7012        // entersubsh_ret is two i32s; reconstruct from LE bytes (host order).
7013        if buf.len() >= 8 {
7014            esret.gleader = i32::from_ne_bytes([buf[0], buf[1], buf[2], buf[3]]);
7015            esret.list_pipe_job = i32::from_ne_bytes([buf[4], buf[5], buf[6], buf[7]]);
7016        }
7017        unsafe { libc::close(synch[0]) }; // c:2836
7018        if (how & Z_ASYNC as i32) != 0 {
7019            // c:2837 — `lastpid = (zlong) pid;`
7020            crate::ported::modules::clone::lastpid.store(pid, Ordering::Relaxed);
7021        } else {
7022            // c:2839 — `if (!jobtab[thisjob].stty_in_env && varspc)`.
7023            let thisjob_idx = {
7024                if let Some(m) = THISJOB.get() {
7025                    *m.lock().unwrap()
7026                } else {
7027                    -1
7028                }
7029            };
7030            // Examine the jobtab entry under lock.
7031            let stty_already = if thisjob_idx >= 0 {
7032                if let Some(jt) = JOBTAB.get() {
7033                    let guard = jt.lock().unwrap();
7034                    guard
7035                        .get(thisjob_idx as usize)
7036                        .map(|j| j.stty_in_env != 0)
7037                        .unwrap_or(true)
7038                } else {
7039                    true
7040                }
7041            } else {
7042                true
7043            };
7044            if !stty_already && varspc.is_some() {
7045                // c:2841-2851 — walk varspc looking for STTY=...
7046                let mut p = varspc.unwrap();
7047                loop {
7048                    if p >= state.prog.prog.len() {
7049                        break;
7050                    }
7051                    let ac = state.prog.prog[p];
7052                    if wc_code(ac) != ZWC_ASSIGN {
7053                        break;
7054                    }
7055                    // c:2845 — `if (!strcmp(ecrawstr(state->prog, p + 1, NULL), "STTY"))`
7056                    let name = ecrawstr(&state.prog, p + 1, None);
7057                    if name == "STTY" {
7058                        // c:2846 — `jobtab[thisjob].stty_in_env = 1;`
7059                        if let Some(jt) = JOBTAB.get() {
7060                            let mut guard = jt.lock().unwrap();
7061                            if let Some(j) = guard.get_mut(thisjob_idx as usize) {
7062                                j.stty_in_env = 1;
7063                            }
7064                        }
7065                        break; // c:2847
7066                    }
7067                    p += if ZWC_ASSIGN_TYPE(ac) == ZWC_ASSIGN_SCALAR {
7068                        3 // c:2849
7069                    } else {
7070                        (ZWC_ASSIGN_NUM(ac) + 2) as usize // c:2850
7071                    };
7072                }
7073            }
7074        }
7075        // c:2853 — `addproc(pid, text, 0, &bgtime, esret.gleader, esret.list_pipe_job);`
7076        if let Some(jt) = JOBTAB.get() {
7077            let mut guard = jt.lock().unwrap();
7078            let tj = {
7079                if let Some(m) = THISJOB.get() {
7080                    *m.lock().unwrap()
7081                } else {
7082                    -1
7083                }
7084            };
7085            if tj >= 0 {
7086                if let Some(j) = guard.get_mut(tj as usize) {
7087                    crate::ported::jobs::addproc(
7088                        j,
7089                        pid,
7090                        text,
7091                        false,
7092                        Some(std::time::Instant::now()),
7093                        esret.gleader,
7094                        esret.list_pipe_job,
7095                    );
7096                }
7097            }
7098        }
7099        // c:2854-2855 — `if (oautocont >= 0) opts[AUTOCONTINUE] = oautocont;`
7100        if oautocont >= 0 {
7101            opt_state_set("autocontinue", oautocont != 0);
7102            let _ = AUTOCONTINUE; // const referenced for parity
7103        }
7104        // c:2856 — `pipecleanfilelist(jobtab[thisjob].filelist, 1);`
7105        if let Some(jt) = JOBTAB.get() {
7106            let mut guard = jt.lock().unwrap();
7107            let tj = {
7108                if let Some(m) = THISJOB.get() {
7109                    *m.lock().unwrap()
7110                } else {
7111                    -1
7112                }
7113            };
7114            if tj >= 0 {
7115                if let Some(j) = guard.get_mut(tj as usize) {
7116                    crate::ported::jobs::pipecleanfilelist(j, true);
7117                }
7118            }
7119        }
7120        return pid; // c:2857
7121    }
7122
7123    // c:2860 — pid == 0 (child).
7124    unsafe { libc::close(synch[0]) }; // c:2861
7125    flags = (if (how & Z_ASYNC as i32) != 0 {
7126        esub::ASYNC
7127    } else {
7128        0
7129    }) | esub::PGRP; // c:2862
7130    let mut flags = flags;
7131    if typ != ZWC_SUBSH as i32 && (how & Z_ASYNC as i32) == 0 {
7132        flags |= esub::KEEPTRAP; // c:2864
7133    }
7134    if typ == ZWC_SUBSH as i32 && (how & Z_ASYNC as i32) == 0 {
7135        flags |= esub::JOB_CONTROL; // c:2866
7136    }
7137    // c:2867 — `*filelistp = jobtab[thisjob].filelist;`
7138    if let Some(jt) = JOBTAB.get() {
7139        let mut guard = jt.lock().unwrap();
7140        let tj = {
7141            if let Some(m) = THISJOB.get() {
7142                *m.lock().unwrap()
7143            } else {
7144                -1
7145            }
7146        };
7147        if tj >= 0 {
7148            if let Some(j) = guard.get_mut(tj as usize) {
7149                *filelistp = std::mem::take(&mut j.filelist);
7150            }
7151        }
7152    }
7153    entersubsh(flags, Some(&mut esret)); // c:2868
7154                                         // c:2869 — `write_loop(synch[1], &esret, sizeof(esret));`
7155    let mut buf = [0u8; 8];
7156    buf[0..4].copy_from_slice(&esret.gleader.to_ne_bytes());
7157    buf[4..8].copy_from_slice(&esret.list_pipe_job.to_ne_bytes());
7158    if write_loop(synch[1], &buf).map(|n| n as usize).unwrap_or(0) != buf.len() {
7159        zerr(&format!(
7160            "Failed to send entersubsh_ret report: {}",
7161            std::io::Error::last_os_error()
7162        ));
7163        return -1; // c:2871
7164    }
7165    unsafe { libc::close(synch[1]) }; // c:2873
7166    let _ = zclose(close_if_forked); // c:2874
7167
7168    // c:2876 — `if (sigtrapped[SIGINT] & ZSIG_IGNORED) holdintr();`
7169    let sigint_state = {
7170        let guard = sigtrapped_static.lock().unwrap();
7171        guard.get(libc::SIGINT as usize).copied().unwrap_or(0)
7172    };
7173    if (sigint_state & ZSIG_IGNORED) != 0 {
7174        crate::ported::signals::holdintr(); // c:2877
7175    }
7176    // c:2882 — `sigtrapped[SIGEXIT] = 0;` — EXIT traps don't fire in fork-child.
7177    {
7178        let mut guard = sigtrapped_static.lock().unwrap();
7179        if let Some(slot) = guard.get_mut(SIGEXIT as usize) {
7180            *slot = 0;
7181        }
7182    }
7183    // c:2884-2890 — `if ((how & Z_ASYNC) && isset(BGNICE)) nice(5)`.
7184    // Per-platform errno setter+reader: __error() on macOS,
7185    // __errno_location() on Linux. Without cfg gating Linux CI breaks.
7186    if (how & Z_ASYNC as i32) != 0 && isset(BGNICE) {
7187        #[cfg(target_os = "macos")]
7188        unsafe {
7189            *libc::__error() = 0;
7190            if libc::nice(5) == -1 && *libc::__error() != 0 {
7191                zwarn(&format!(
7192                    "nice(5) failed: {}",
7193                    std::io::Error::last_os_error()
7194                ));
7195            }
7196        }
7197        #[cfg(target_os = "linux")]
7198        unsafe {
7199            *libc::__errno_location() = 0;
7200            if libc::nice(5) == -1 && *libc::__errno_location() != 0 {
7201                zwarn(&format!(
7202                    "nice(5) failed: {}",
7203                    std::io::Error::last_os_error()
7204                ));
7205            }
7206        }
7207    }
7208    0 // c:2892
7209}
7210
7211/// Port of `execcmd_analyse(Estate state, Execcmd_params eparams)`
7212/// from `Src/exec.c:2733-2785`. Pre-execcmd_exec analysis pass:
7213/// walks the wordcode at `state->pc`, splits out redirs/varspc/args
7214/// without expanding (no prefork, no globbing), and fills `eparams`
7215/// so the caller (execcmd_exec at c:2901 or execpline2 at c:2013)
7216/// can branch on the command type before the real work.
7217pub fn execcmd_analyse(state: &mut estate, eparams: &mut crate::ported::zsh_h::execcmd_params) {
7218    use crate::ported::zsh_h::{
7219        WC_ASSIGN as ZWC_ASSIGN, WC_REDIR as ZWC_REDIR, WC_SIMPLE as ZWC_SIMPLE,
7220        WC_SIMPLE_ARGC as ZWC_SIMPLE_ARGC, WC_TYPESET as ZWC_TYPESET,
7221        WC_TYPESET_ARGC as ZWC_TYPESET_ARGC,
7222    };
7223    // c:2733
7224    let mut code: wordcode; // c:2735
7225    let mut i: i32; // c:2736
7226    let _ = i;
7227
7228    // c:2738 — `eparams->beg = state->pc;`
7229    eparams.beg = state.pc;
7230    // c:2739-2740 — `eparams->redir = (wc_code(*state->pc) == WC_REDIR ? ecgetredirs(state) : NULL);`
7231    eparams.redir =
7232        if state.pc < state.prog.prog.len() && wc_code(state.prog.prog[state.pc]) == ZWC_REDIR {
7233            Some(crate::ported::parse::ecgetredirs(state))
7234        } else {
7235            None
7236        };
7237    // c:2741-2748 — varspc walk (WC_ASSIGN chain).
7238    if state.pc < state.prog.prog.len() && wc_code(state.prog.prog[state.pc]) == ZWC_ASSIGN {
7239        cmdoutval.store(0, Ordering::Relaxed); // c:2742
7240        eparams.varspc = Some(state.pc); // c:2743
7241                                         // c:2744-2746 — `while (wc_code((code = *state->pc)) == WC_ASSIGN) state->pc += ...`
7242        loop {
7243            if state.pc >= state.prog.prog.len() {
7244                break;
7245            }
7246            code = state.prog.prog[state.pc];
7247            if wc_code(code) != ZWC_ASSIGN {
7248                break;
7249            }
7250            state.pc += if WC_ASSIGN_TYPE(code) == WC_ASSIGN_SCALAR {
7251                3 // c:2745
7252            } else {
7253                (WC_ASSIGN_NUM(code) + 2) as usize // c:2746
7254            };
7255        }
7256    } else {
7257        eparams.varspc = None; // c:2748
7258    }
7259
7260    // c:2750 — `code = *state->pc++;`
7261    if state.pc >= state.prog.prog.len() {
7262        eparams.args = None;
7263        eparams.assignspc = None;
7264        eparams.typ = 0;
7265        eparams.postassigns = 0;
7266        eparams.htok = 0;
7267        return;
7268    }
7269    code = state.prog.prog[state.pc];
7270    state.pc += 1;
7271
7272    // c:2752 — `eparams->type = wc_code(code);`
7273    eparams.typ = wc_code(code) as i32;
7274    // c:2753 — `eparams->postassigns = 0;`
7275    eparams.postassigns = 0;
7276
7277    // c:2755-2783 — switch on type. EC_DUP is used (not EC_DUPTOK)
7278    // per the comment at c:2755-2757.
7279    match eparams.typ as wordcode {
7280        x if x == ZWC_SIMPLE => {
7281            // c:2759-2763
7282            let mut htok = 0;
7283            let argc = ZWC_SIMPLE_ARGC(code) as usize;
7284            eparams.args = Some(ecgetlist(state, argc, EC_DUP, Some(&mut htok)));
7285            eparams.htok = htok;
7286            eparams.assignspc = None;
7287        }
7288        x if x == ZWC_TYPESET => {
7289            // c:2765-2777
7290            let mut htok = 0;
7291            let argc = ZWC_TYPESET_ARGC(code) as usize;
7292            eparams.args = Some(ecgetlist(state, argc, EC_DUP, Some(&mut htok)));
7293            eparams.htok = htok;
7294            // c:2768 — `eparams->postassigns = *state->pc++;`
7295            if state.pc < state.prog.prog.len() {
7296                eparams.postassigns = state.prog.prog[state.pc] as i32;
7297                state.pc += 1;
7298            }
7299            // c:2769 — `eparams->assignspc = state->pc;`
7300            eparams.assignspc = Some(state.pc);
7301            // c:2770-2776 — walk past the postassigns.
7302            let mut k = 0i32;
7303            while k < eparams.postassigns {
7304                if state.pc >= state.prog.prog.len() {
7305                    break;
7306                }
7307                code = state.prog.prog[state.pc];
7308                // c:2772-2773 DPUTS — assert wc_code == WC_ASSIGN; skipped.
7309                state.pc += if WC_ASSIGN_TYPE(code) == WC_ASSIGN_SCALAR {
7310                    3 // c:2774
7311                } else {
7312                    (WC_ASSIGN_NUM(code) + 2) as usize // c:2775
7313                };
7314                k += 1;
7315            }
7316        }
7317        _ => {
7318            // c:2779-2783 default.
7319            eparams.args = None;
7320            eparams.assignspc = None;
7321            eparams.htok = 0;
7322        }
7323    }
7324}
7325
7326/// Port of `char **zsh_eval_context;` from `Src/exec.c` (zsh.export:355).
7327/// Stack of `"context"` labels used by `eval`-style nested execution:
7328/// `bin_dot`, `bin_eval`, `execode`, autoloads. Each `execode(prog,
7329/// ..., "context")` pushes its label and pops on return.
7330pub static zsh_eval_context: std::sync::Mutex<Vec<String>> = std::sync::Mutex::new(Vec::new());
7331
7332/// Port of `static int donetrap;` from `Src/exec.c:1351`. Tracks
7333/// whether the ZERR trap has already fired for the current sublist.
7334/// C source resets to 0 at sublist start (c:1455) and sets to 1
7335/// after `dotrap(SIGZERR)` (c:1602). The check
7336/// `if (!this_noerrexit && !donetrap && !this_donetrap)` at c:1598
7337/// suppresses re-firing within the same sublist AND, crucially,
7338/// carries the "already fired" state across a function-call return
7339/// boundary so the outer caller's post-command check doesn't fire
7340/// ZERR a second time for the same logical error. Bug #303 in
7341/// docs/BUGS.md.
7342///
7343/// Reset at each top-level statement boundary via
7344/// `BUILTIN_DONETRAP_RESET` emitted by `compile_list`. Set after
7345/// `dotrap(SIGZERR)` fires inside `BUILTIN_ERREXIT_CHECK`.
7346pub static DONETRAP: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
7347
7348/// Port of `save_params(Estate state, Wordcode pc, LinkList *restore_p,
7349/// LinkList *remove_p)` from `Src/exec.c:4410-4458`. Walk WC_ASSIGN
7350/// chain at `pc`, snapshot each existing param into `restore_p` (so
7351/// the builtin/shfunc can restore them on return) and enqueue every
7352/// touched name in `remove_p` (so we know what to unset).
7353pub fn save_params(
7354    state: &mut estate,
7355    pc: usize,
7356    restore_p: &mut Vec<crate::ported::zsh_h::param>,
7357    remove_p: &mut Vec<String>,
7358) {
7359    use crate::ported::zsh_h::{
7360        PM_READONLY, PM_SPECIAL, WC_ASSIGN, WC_ASSIGN_NUM as ZWC_ASSIGN_NUM,
7361        WC_ASSIGN_SCALAR as ZWC_ASSIGN_SCALAR, WC_ASSIGN_TYPE as ZWC_ASSIGN_TYPE,
7362    };
7363    // c:4410 — `*restore_p = newlinklist();` — caller pre-allocates.
7364    // c:4417 — `*remove_p = newlinklist();` — caller pre-allocates.
7365    let mut p = pc;
7366    // c:4419 — `while (wc_code(ac = *pc) == WC_ASSIGN)`
7367    loop {
7368        if p >= state.prog.prog.len() {
7369            break;
7370        }
7371        let ac = state.prog.prog[p];
7372        if wc_code(ac) != WC_ASSIGN {
7373            break;
7374        }
7375        // c:4420 — `s = ecrawstr(state->prog, pc + 1, NULL);`
7376        let s = ecrawstr(&state.prog, p + 1, None);
7377        // c:4421 — `pm = paramtab->getnode(paramtab, s)`
7378        let pm_clone: Option<crate::ported::zsh_h::param> = {
7379            let tab = paramtab().read().unwrap();
7380            tab.get(&s).map(|b| (**b).clone())
7381        };
7382        if let Some(pm) = pm_clone {
7383            // c:4423-4424 — `if (pm->env) delenv(pm);`
7384            if pm.env.is_some() {
7385                crate::ported::params::delenv(&s);
7386            }
7387            // c:4425-4448 — copy if not readonly-special.
7388            if (pm.node.flags & PM_SPECIAL as i32) == 0 {
7389                // c:4426-4438 — regular param: deep copy via copyparam(tpm, pm, 0).
7390                let mut tpm = pm.clone();
7391                tpm.node.nam = s.clone();
7392                // copyparam with fakecopy=0 already done by the clone()
7393                // (Clone derives a deep copy of param fields).
7394                restore_p.push(tpm); // c:4451
7395            } else if (pm.node.flags & PM_READONLY as i32) == 0 {
7396                // c:4439-4448 — special-but-not-readonly: fakecopy=1.
7397                let mut tpm = pm.clone();
7398                tpm.node.nam = pm.node.nam.clone();
7399                restore_p.push(tpm); // c:4451
7400            }
7401            // c:4449 — `addlinknode(*remove_p, dupstring(s));`
7402            remove_p.push(s.clone());
7403        } else {
7404            // c:4453 — `addlinknode(*remove_p, dupstring(s));`
7405            remove_p.push(s.clone());
7406        }
7407        // c:4455 — `pc += (WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR ? 3 : WC_ASSIGN_NUM(ac) + 2);`
7408        p += if ZWC_ASSIGN_TYPE(ac) == ZWC_ASSIGN_SCALAR {
7409            3
7410        } else {
7411            (ZWC_ASSIGN_NUM(ac) + 2) as usize
7412        };
7413    }
7414}
7415
7416/// Port of `restore_params(LinkList restorelist, LinkList removelist)`
7417/// from `Src/exec.c:4464-4528`. After the builtin/shfunc returns,
7418/// unset every name in removelist, then for each saved param in
7419/// restorelist re-install its values (PM_SPECIAL go through gsu
7420/// setfn; regular params re-enter paramtab as-is).
7421pub fn restore_params(restorelist: Vec<crate::ported::zsh_h::param>, removelist: Vec<String>) {
7422    use crate::ported::zsh_h::{PM_READONLY, PM_SPECIAL};
7423    // c:4470-4476 — `while ((s = ugetnode(removelist)))` — unset each.
7424    for s in &removelist {
7425        // c:4471 — `if ((pm = paramtab->getnode(paramtab, s)) && !(pm->node.flags & PM_SPECIAL))`
7426        let flags = {
7427            let tab = paramtab().read().unwrap();
7428            tab.get(s).map(|p| p.node.flags)
7429        };
7430        if let Some(f) = flags {
7431            if (f & PM_SPECIAL as i32) == 0 {
7432                // c:4473 — `pm->node.flags &= ~PM_READONLY;`
7433                let mut tab = paramtab().write().unwrap();
7434                if let Some(pm_mut) = tab.get_mut(s) {
7435                    pm_mut.node.flags &= !(PM_READONLY as i32);
7436                }
7437                // Drop write guard before calling unsetparam_pm.
7438                drop(tab);
7439                let mut tab = paramtab().write().unwrap();
7440                if let Some(pm_mut) = tab.get_mut(s) {
7441                    let _ = crate::ported::params::unsetparam_pm(pm_mut, 0, 0); // c:4474
7442                }
7443            }
7444        }
7445    }
7446    // c:4478-4523 — restore saved params.
7447    for pm in restorelist {
7448        // c:4481-4520 — PM_SPECIAL: route through gsu setfn.
7449        // c:4521-4523 — non-special: re-install via paramtab.
7450        if (pm.node.flags & PM_SPECIAL as i32) != 0 {
7451            // PM_SPECIAL restore: full path requires PM_TYPE dispatch
7452            // on gsu_s/i/f/a/h setfn. Each setfn fires the param's
7453            // canonical write hook. Pragmatic port: overwrite in
7454            // paramtab; daily-driver path rarely saves specials (those
7455            // are reserved-name vars like PATH/FPATH/etc. which can't
7456            // appear as `VAR=val cmd` prefix anyway).
7457            let mut tab = paramtab().write().unwrap();
7458            tab.insert(pm.node.nam.clone(), Box::new(pm));
7459        } else {
7460            // c:4521 — `paramtab->addnode(paramtab, ztrdup(pm->node.nam), pm);`
7461            let mut tab = paramtab().write().unwrap();
7462            tab.insert(pm.node.nam.clone(), Box::new(pm));
7463        }
7464    }
7465}
7466
7467/// Port of `void execode(Eprog p, int dont_change_job, int exiting,
7468/// char *context)` from `Src/exec.c:1245-1282`. Set up an `estate`
7469/// around the given Eprog and run `execlist`. Maintains the
7470/// `zsh_eval_context` stack so `$ZSH_EVAL_CONTEXT` reflects the
7471/// call chain.
7472///
7473/// NOTE: this is the WORDCODE form (drives the ported `execlist`
7474/// interpreter). zshrs's live execution pipeline is fusevm
7475/// (`compile_zsh` → VM), so the top-level REPL `loop()` and most call
7476/// sites run through [`execode`] (the ZshProgram/fusevm form below)
7477/// instead. This wordcode entry is retained for the internal
7478/// function-body callers (`doshfunc` / autoload) that already hold an
7479/// `Eprog`.
7480pub fn execode_wordcode(
7481    p: crate::ported::zsh_h::Eprog,
7482    dont_change_job: i32,
7483    exiting: i32,
7484    context: &str,
7485) {
7486    // c:1245
7487    let prog_ref = *p;
7488    // c:1247 — `struct estate s;`
7489    let mut s = estate {
7490        prog: Box::new(prog_ref.clone()),
7491        // c:1269 — `s.pc = p->prog;` — start at index 0.
7492        pc: 0,
7493        // c:1270 — `s.strs = p->strs;`
7494        strs: prog_ref.strs.clone(),
7495        strs_offset: 0,
7496    };
7497    // c:1251-1266 — push context onto zsh_eval_context.
7498    let pushed = {
7499        if let Ok(mut ctx) = zsh_eval_context.lock() {
7500            ctx.push(context.to_string());
7501            true
7502        } else {
7503            false
7504        }
7505    };
7506    // c:1271 — `useeprog(p);`
7507    crate::ported::parse::useeprog(&mut s.prog);
7508    // c:1273 — `execlist(&s, dont_change_job, exiting);`
7509    execlist(&mut s, dont_change_job, exiting);
7510    // c:1275 — `freeeprog(p);`
7511    crate::ported::parse::freeeprog(&mut s.prog);
7512    // c:1281 — `zsh_eval_context[alen] = NULL;` — pop our entry.
7513    if pushed {
7514        if let Ok(mut ctx) = zsh_eval_context.lock() {
7515            ctx.pop();
7516        }
7517    }
7518}
7519
7520thread_local! {
7521    /// The long-lived interactive executor for the top-level `loop()`
7522    /// REPL (the `zsh_main` path). Set once by the bin before
7523    /// `zsh_main`; [`execode`] runs each parsed program through it.
7524    /// Persists for the whole session so variables/functions survive
7525    /// across prompts.
7526    static SESSION_EXECUTOR: std::cell::Cell<Option<*mut crate::vm_helper::ShellExecutor>> =
7527        const { std::cell::Cell::new(None) };
7528}
7529
7530/// Register the persistent session executor used by [`execode`] for the
7531/// interactive `loop()` REPL. The pointer must outlive the session (the
7532/// bin keeps the executor alive until `zsh_main` exits the process).
7533pub fn install_session_executor(exec: &mut crate::vm_helper::ShellExecutor) {
7534    SESSION_EXECUTOR.with(|c| c.set(Some(exec as *mut crate::vm_helper::ShellExecutor)));
7535    // Mirror the pointer into the bridge so `with_session_context` can
7536    // establish a VM context for startup rc-sourcing (run_init_scripts,
7537    // c:1914) before the loop's first execode enters one.
7538    crate::fusevm_bridge::register_session_executor(exec);
7539}
7540
7541/// zshrs `execode` — run an already-parsed `ZshProgram` (Src/exec.c:220
7542/// `execode(prog, ...)`, called from `loop()`). This is the **exec.rs
7543/// exception** to the line-by-line port: rather than walk wordcode via
7544/// `execlist`, it drives zshrs's live engine — compile the program with
7545/// `compile_zsh` and run it on the session executor's fusevm VM. The
7546/// faithful `loop()` in init.rs calls this exactly as C calls execode.
7547/// Returns `$?` (0 when no session executor is installed).
7548pub fn execode(
7549    program: &crate::parse::ZshProgram,
7550    _dont_change_job: i32,
7551    _exiting: i32,
7552    _context: &str,
7553) -> i32 {
7554    SESSION_EXECUTOR.with(|c| match c.get() {
7555        // SAFETY: set by install_session_executor to an executor that
7556        // lives for the whole single-threaded interactive session;
7557        // loop() runs only after the bin installs it.
7558        Some(ptr) => unsafe { (*ptr).execute_program(program) },
7559        None => 0,
7560    })
7561}
7562
7563// =========================================================================
7564// Live-executor accessors (former `exec_hooks` OnceLock layer).
7565//
7566// These are the **exec.rs exception**: src/ported/ code reaches the
7567// live fusevm `ShellExecutor` (param store, function dispatch, nested
7568// script/cmdsubst execution) through these thin wrappers instead of the
7569// deleted `exec_hooks` fn-pointer registry. Each delegates to
7570// `fusevm_bridge::try_with_executor` — `Some` when a VM execution
7571// context is in scope, `None` in unit-test / compsys contexts with no
7572// bridge running — and reproduces the exact per-call fallback the old
7573// `exec_hooks` wrappers used when no hook was installed. Behavior is
7574// byte-for-byte identical to the OnceLock path; only the indirection is
7575// gone. See `feedback_no_shellexecutor_in_ported` /
7576// `feedback_no_exec_script_from_ported`: the bridge belongs in exec.rs
7577// (the sanctioned exception), not scattered through src/ported.
7578// =========================================================================
7579
7580/// Array param value via the live executor; falls back to the direct
7581/// param table (`params::getaparam`) when no executor is in scope, so
7582/// compsys / unit-test environments still observe shell-side arrays.
7583pub fn array(name: &str) -> Option<Vec<String>> {
7584    if let Some(Some(v)) = crate::fusevm_bridge::try_with_executor(|exec| exec.array(name)) {
7585        return Some(v);
7586    }
7587    crate::ported::params::getaparam(name)
7588}
7589
7590/// Associative-array param value via the live executor (`None` when no
7591/// executor / not set).
7592pub fn assoc(name: &str) -> Option<indexmap::IndexMap<String, String>> {
7593    crate::fusevm_bridge::try_with_executor(|exec| exec.assoc(name)).flatten()
7594}
7595
7596/// Store an array param into the live executor (no-op without one).
7597pub fn set_array(name: &str, val: Vec<String>) {
7598    let _ = crate::fusevm_bridge::try_with_executor(|exec| exec.set_array(name.to_string(), val));
7599}
7600
7601/// Store an associative-array param into the live executor (no-op
7602/// without one).
7603pub fn set_assoc(name: &str, val: indexmap::IndexMap<String, String>) {
7604    let _ = crate::fusevm_bridge::try_with_executor(|exec| exec.set_assoc(name.to_string(), val));
7605}
7606
7607/// Unset a scalar param in the live executor (no-op without one).
7608pub fn unset_scalar(name: &str) {
7609    let _ = crate::fusevm_bridge::try_with_executor(|exec| exec.unset_scalar(name));
7610}
7611
7612/// Unset an array param in the live executor (no-op without one).
7613pub fn unset_array(name: &str) {
7614    let _ = crate::fusevm_bridge::try_with_executor(|exec| exec.unset_array(name));
7615}
7616
7617/// Unset an associative-array param in the live executor (no-op
7618/// without one).
7619pub fn unset_assoc(name: &str) {
7620    let _ = crate::fusevm_bridge::try_with_executor(|exec| exec.unset_assoc(name));
7621}
7622
7623/// Dispatch a shell-function call by name through the live executor
7624/// (full doshfunc scope wrap). `None` when no executor / not a
7625/// function.
7626pub fn dispatch_function_call(name: &str, args: &[String]) -> Option<i32> {
7627    if let Some(r) =
7628        crate::fusevm_bridge::try_with_executor(|exec| exec.dispatch_function_call(name, args))
7629    {
7630        return r;
7631    }
7632    // No active VM context: this is the loop()/zsh_main exit path where
7633    // `zexit` fires a `TRAPEXIT() { ... }` (via dotrap, Src/builtin.c:6043)
7634    // or the `zshexit` hook after `loop()` returned. Enter the installed
7635    // session executor so the handler runs in the shell. SAFETY per execode.
7636    SESSION_EXECUTOR.with(|c| match c.get() {
7637        Some(ptr) => {
7638            let _ctx = crate::fusevm_bridge::ExecutorContext::enter(unsafe { &mut *ptr });
7639            unsafe { (*ptr).dispatch_function_call(name, args) }
7640        }
7641        None => None,
7642    })
7643}
7644
7645/// Body-only function dispatch (no doshfunc scope wrap) — call as the
7646/// `body_runner` of a direct `doshfunc(...)` invocation to avoid the
7647/// double-wrap of going back through [`dispatch_function_call`]. `None`
7648/// when no executor.
7649pub fn run_function_body(name: &str, args: &[String]) -> Option<i32> {
7650    if let Some(r) =
7651        crate::fusevm_bridge::try_with_executor(|exec| exec.run_function_body_only(name, args))
7652    {
7653        return r;
7654    }
7655    // Session-executor fallback for the no-VM-context exit path (e.g. the
7656    // `zshexit` hook fired by `zexit` from zsh_main). SAFETY per execode.
7657    SESSION_EXECUTOR.with(|c| match c.get() {
7658        Some(ptr) => {
7659            let _ctx = crate::fusevm_bridge::ExecutorContext::enter(unsafe { &mut *ptr });
7660            unsafe { (*ptr).run_function_body_only(name, args) }
7661        }
7662        None => None,
7663    })
7664}
7665
7666/// Run a script source string on the live executor. `Ok(0)` when no
7667/// executor is in scope.
7668pub fn execute_script(src: &str) -> Result<i32, String> {
7669    if let Some(r) = crate::fusevm_bridge::try_with_executor(|exec| exec.execute_script(src)) {
7670        return r;
7671    }
7672    // No active VM context: the loop()/zsh_main exit path where `zexit`
7673    // runs a `trap '...' EXIT` raw body (Src/builtin.c:6043) after `loop()`
7674    // returned. Enter the installed session executor so the trap body runs
7675    // in the shell instead of silently no-op'ing. SAFETY per execode.
7676    SESSION_EXECUTOR.with(|c| match c.get() {
7677        Some(ptr) => {
7678            let _ctx = crate::fusevm_bridge::ExecutorContext::enter(unsafe { &mut *ptr });
7679            unsafe { (*ptr).execute_script(src) }
7680        }
7681        None => Ok(0),
7682    })
7683}
7684
7685/// Run a script source string through the live executor's zsh pipeline.
7686/// `Ok(0)` when no executor is in scope.
7687pub fn execute_script_zsh_pipeline(src: &str) -> Result<i32, String> {
7688    crate::fusevm_bridge::try_with_executor(|exec| exec.execute_script_zsh_pipeline(src))
7689        .unwrap_or(Ok(0))
7690}
7691
7692/// Run a `$(...)` command substitution on the live executor, returning
7693/// captured stdout. Empty string when no executor is in scope.
7694pub fn run_command_substitution(cmd: &str) -> String {
7695    if let Some(r) =
7696        crate::fusevm_bridge::try_with_executor(|exec| exec.run_command_substitution(cmd))
7697    {
7698        return r;
7699    }
7700    // Session-executor fallback for no-VM-context callers — the native
7701    // p10k custom_* segments (p10k:1698 `content="$(eval $command)"`)
7702    // render at preprompt time, before any ExecutorContext is entered,
7703    // so try_with_executor alone returned "" and every custom segment
7704    // rendered empty. Same pattern as run_function_body above; SAFETY
7705    // per execode.
7706    SESSION_EXECUTOR.with(|c| match c.get() {
7707        Some(ptr) => {
7708            let _ctx = crate::fusevm_bridge::ExecutorContext::enter(unsafe { &mut *ptr });
7709            unsafe { (*ptr).run_command_substitution(cmd) }
7710        }
7711        None => String::new(),
7712    })
7713}
7714
7715/// Positional parameters ($1..$N) from the live executor; empty without
7716/// one.
7717pub fn pparams() -> Vec<String> {
7718    crate::fusevm_bridge::try_with_executor(|exec| exec.pparams()).unwrap_or_default()
7719}
7720
7721/// Replace the positional parameters in the live executor (no-op
7722/// without one).
7723pub fn set_pparams(v: Vec<String>) {
7724    let _ = crate::fusevm_bridge::try_with_executor(|exec| exec.set_pparams(v));
7725}
7726
7727/// Drop a function from both the compiled-chunk and source maps in the
7728/// live executor. Returns true if either entry existed; false when no
7729/// executor.
7730pub fn unregister_function(name: &str) -> bool {
7731    crate::fusevm_bridge::try_with_executor(|exec| {
7732        let a = exec.functions_compiled.remove(name).is_some();
7733        let b = exec.function_source.remove(name).is_some();
7734        a || b
7735    })
7736    .unwrap_or(false)
7737}
7738
7739/// Saved outer stdout fd for an in-progress `$(...)` capture (top of
7740/// the bridge's CMDSUBST_OUTER_FDS stack), or `None` when not inside a
7741/// cmdsub. Used by the trap dispatcher to route a trap body's stdout to
7742/// the parent terminal instead of the cmdsub-bound pipe (Bug #56).
7743pub fn cmdsubst_outer_stdout() -> Option<i32> {
7744    crate::fusevm_bridge::cmdsubst_outer_stdout()
7745}
7746
7747/// Port of `execautofn_basic(Estate state, UNUSED(int do_exec))` from
7748/// `Src/exec.c:5608-5630`. Run a pre-loaded autoload function body
7749/// via `execode`, snapshotting `scriptname`/`scriptfilename` around
7750/// the call so `%N` / `%x` reflect the autoload target during
7751/// execution.
7752pub fn execautofn_basic(state: &mut estate, _do_exec: i32) -> i32 {
7753    // c:5608
7754    // c:5613 — `shf = state->prog->shf;`
7755    let shf = match state.prog.shf.as_deref() {
7756        Some(s) => s.clone(),
7757        None => return LASTVAL.load(Ordering::Relaxed),
7758    };
7759
7760    // c:5619-5620 — funcstack filename catch-up. zshrs's funcstack
7761    // top-of-stack tracking is in modules::parameter::FUNCSTACK.
7762    {
7763        let mut stk = crate::ported::modules::parameter::FUNCSTACK.lock().unwrap();
7764        if let Some(top) = stk.last_mut() {
7765            if top.filename.is_none() {
7766                // c:5620 — `funcstack->filename = getshfuncfile(shf);`
7767                top.filename = crate::ported::hashtable::getshfuncfile(&shf.node.nam);
7768            }
7769        }
7770    }
7771
7772    // c:5622-5623 — `oldscriptname/oldscriptfilename = scriptname/scriptfilename;`
7773    let oldscriptname = crate::ported::utils::scriptname_get();
7774    let oldscriptfilename = crate::ported::utils::scriptfilename_get();
7775    // c:5624 — `scriptname = dupstring(shf->node.nam);`
7776    crate::ported::utils::set_scriptname(Some(shf.node.nam.clone()));
7777    // c:5625 — `scriptfilename = getshfuncfile(shf);`
7778    crate::ported::utils::set_scriptfilename(crate::ported::hashtable::getshfuncfile(
7779        &shf.node.nam,
7780    ));
7781    // c:5626 — `execode(shf->funcdef, 1, 0, "loadautofunc");`
7782    if let Some(funcdef) = shf.funcdef.clone() {
7783        execode_wordcode(funcdef, 1, 0, "loadautofunc");
7784    }
7785    // c:5627-5628 — restore.
7786    crate::ported::utils::set_scriptname(oldscriptname);
7787    crate::ported::utils::set_scriptfilename(oldscriptfilename);
7788
7789    LASTVAL.load(Ordering::Relaxed) // c:5630
7790}
7791
7792/// Port of `static int execautofn(Estate state, UNUSED(int do_exec))`
7793/// from `Src/exec.c:5635-5644`. The autoload-aware dispatch entry
7794/// for `WC_AUTOFN`: fault the function body in via `loadautofn`,
7795/// then hand off to `execautofn_basic` to actually run it.
7796///
7797/// C body:
7798/// ```c
7799/// static int
7800/// execautofn(Estate state, UNUSED(int do_exec))
7801/// {
7802///     Shfunc shf;
7803///     if (!(shf = loadautofn(state->prog->shf, 1, 0, 0)))
7804///         return 1;
7805///     state->prog->shf = shf;
7806///     return execautofn_basic(state, 0);
7807/// }
7808/// ```
7809///
7810/// Rust port: `loadautofn` mutates the `shfunc` in place via a raw
7811/// pointer and returns 0/1 (success/failure), so the explicit
7812/// `state->prog->shf = shf` assignment in C is implicit here.
7813pub fn execautofn(state: &mut estate, _do_exec: i32) -> i32 {
7814    // c:5638-5640 — `if (!(shf = loadautofn(state->prog->shf, 1, 0, 0))) return 1;`
7815    let shf_ptr: *mut shfunc = match state.prog.shf.as_mut() {
7816        Some(b) => &mut **b as *mut shfunc,
7817        None => return 1,
7818    };
7819    if loadautofn(shf_ptr, 1, 0, 0) != 0 {
7820        return 1;
7821    }
7822    // c:5643 — `return execautofn_basic(state, 0);`
7823    execautofn_basic(state, 0)
7824}
7825
7826/// Port of `execpline2(Estate state, wordcode pcode, int how, int input,
7827/// int output, int last1)` from `Src/exec.c:1989-2040`. Recursive
7828/// multi-stage pipe walker: at each step, analyse the current
7829/// command, fork-into-pipe (if mid-pipeline) or exec directly (if
7830/// WC_PIPE_END), then recurse on the next stage with `pipes[0]` as
7831/// its input fd.
7832pub fn execpline2(
7833    state: &mut estate,
7834    pcode: wordcode,
7835    how: i32,
7836    input: i32,
7837    output: i32,
7838    last1: i32,
7839) {
7840    use crate::ported::builtin::{BREAKS, INEVAL, RETFLAG};
7841    use crate::ported::zsh_h::{
7842        execcmd_params, CS_PIPE, WC_PIPE_END, WC_PIPE_LINENO as ZWC_PIPE_LINENO,
7843        WC_PIPE_TYPE as ZWC_PIPE_TYPE, Z_ASYNC,
7844    };
7845    // c:1991
7846    let mut eparams: execcmd_params = execcmd_params::default(); // c:1994 `struct execcmd_params eparams;`
7847
7848    // c:1996-1997 — `if (breaks || retflag) return;`
7849    if BREAKS.load(Ordering::SeqCst) != 0 || RETFLAG.load(Ordering::SeqCst) != 0 {
7850        return;
7851    }
7852
7853    // c:1999-2001 — `if (!IN_EVAL_TRAP() && !ineval && WC_PIPE_LINENO(pcode))
7854    //                  lineno = WC_PIPE_LINENO(pcode) - 1;`
7855    if !crate::ported::zsh_h::IN_EVAL_TRAP()
7856        && INEVAL.load(Ordering::SeqCst) == 0
7857        && ZWC_PIPE_LINENO(pcode) != 0
7858    {
7859        let new_lineno = ZWC_PIPE_LINENO(pcode).saturating_sub(1) as usize;
7860        crate::ported::input::lineno.with(|l| l.set(new_lineno));
7861    }
7862
7863    // c:2003-2011 — pline_level == 1 → snapshot to list_pipe_text for `jobs` output.
7864    if pline_level.load(Ordering::Relaxed) == 1 {
7865        // c:2003
7866        if (how & Z_ASYNC as i32) != 0 || sfcontext.load(Ordering::Relaxed) == 0 {
7867            // c:2004 — `(how & Z_ASYNC) || !sfcontext`
7868            // c:2005-2008 — `strcpy(list_pipe_text, getjobtext(state->prog,
7869            //   state->pc + (WC_PIPE_TYPE(pcode) == WC_PIPE_END ? 0 : 1)));`
7870            let pc_for_text = state.pc
7871                + if ZWC_PIPE_TYPE(pcode) == WC_PIPE_END {
7872                    0
7873                } else {
7874                    1
7875                };
7876            let text = crate::ported::text::getjobtext(state.prog.clone(), Some(pc_for_text));
7877            if let Ok(mut lpt) = LIST_PIPE_TEXT.lock() {
7878                *lpt = text;
7879            }
7880        } else {
7881            // c:2010 — `list_pipe_text[0] = '\0';`
7882            if let Ok(mut lpt) = LIST_PIPE_TEXT.lock() {
7883                lpt.clear();
7884            }
7885        }
7886    }
7887
7888    if ZWC_PIPE_TYPE(pcode) == WC_PIPE_END {
7889        // c:2012-2014 — terminal stage: analyse + exec directly.
7890        execcmd_analyse(state, &mut eparams); // c:2013
7891        execcmd_exec(
7892            state,
7893            &mut eparams,
7894            input,
7895            output,
7896            how,
7897            if last1 != 0 { 1 } else { 2 }, // c:2014 `last1 ? 1 : 2`
7898            -1,                             // c:2014 close_if_forked = -1
7899        );
7900    } else {
7901        // c:2015-2039 — non-terminal stage: pipe + fork + recurse.
7902        let mut pipes: [i32; 2] = [-1, -1]; // c:2016
7903        let old_list_pipe = list_pipe.load(Ordering::Relaxed); // c:2017
7904                                                               // c:2018 — `Wordcode next = state->pc + (*state->pc);`
7905        let next = if state.pc < state.prog.prog.len() {
7906            state.pc + state.prog.prog[state.pc] as usize
7907        } else {
7908            state.pc
7909        };
7910        // c:2020 — `++state->pc;`
7911        if state.pc < state.prog.prog.len() {
7912            state.pc += 1;
7913        }
7914        execcmd_analyse(state, &mut eparams); // c:2021
7915
7916        if mpipe(&mut pipes) < 0 {
7917            // c:2023-2025 — pipe() failure — `/* FIXME */` in C, fall through.
7918        }
7919
7920        // c:2027 — `addfilelist(NULL, pipes[0]);`
7921        // C uses the current thisjob's filelist; Rust port wires through JOBTAB.
7922        if let Some(jt) = JOBTAB.get() {
7923            let mut guard = jt.lock().unwrap();
7924            let tj = {
7925                if let Some(m) = THISJOB.get() {
7926                    *m.lock().unwrap()
7927                } else {
7928                    -1
7929                }
7930            };
7931            if tj >= 0 {
7932                if let Some(j) = guard.get_mut(tj as usize) {
7933                    crate::ported::jobs::addfilelist(j, None, pipes[0]);
7934                }
7935            }
7936        }
7937
7938        // c:2028 — `execcmd_exec(state, &eparams, input, pipes[1], how, 0, pipes[0]);`
7939        execcmd_exec(state, &mut eparams, input, pipes[1], how, 0, pipes[0]);
7940        let _ = zclose(pipes[1]); // c:2029
7941        state.pc = next; // c:2030
7942
7943        // c:2034 — `cmdpush(CS_PIPE);`
7944        cmdpush(CS_PIPE as u8);
7945        // c:2035 — `list_pipe = 1;`
7946        list_pipe.store(1, Ordering::Relaxed);
7947        // c:2036 — `execpline2(state, *state->pc++, how, pipes[0], output, last1);`
7948        let next_pcode = if state.pc < state.prog.prog.len() {
7949            state.prog.prog[state.pc]
7950        } else {
7951            0
7952        };
7953        if state.pc < state.prog.prog.len() {
7954            state.pc += 1;
7955        }
7956        execpline2(state, next_pcode, how, pipes[0], output, last1);
7957        // c:2037 — `list_pipe = old_list_pipe;`
7958        list_pipe.store(old_list_pipe, Ordering::Relaxed);
7959        // c:2038 — `cmdpop();`
7960        cmdpop();
7961    }
7962}
7963
7964/// Port of `execpline(Estate state, wordcode slcode, int how, int last1)`
7965/// from `Src/exec.c:1724-2041`. Full faithful port: allocates a job-table
7966/// entry via `initjob`, sets up coproc mpipes, drives the whole
7967/// (multi-stage) pipeline through `execpline2` (which performs the real
7968/// per-stage mpipe/fork/exec), then either spawns the job asynchronously
7969/// (`Z_ASYNC` -> `spawnjob`/`deletejob`) or waits synchronously
7970/// (`waitjobs`), including the `list_pipe` stop/continue fork machinery
7971/// (SUBJOB/SUPERJOB linkage) that re-forks the shell to keep an
7972/// interactively-suspended right-hand pipeline stage running.
7973///
7974/// Divergences from C, each forced by the Rust substrate and cited
7975/// inline at the point of use:
7976///   * `initjob` here grows the `Vec`-backed jobtab on demand and does
7977///     not return -1, so C's per-pipeline table-full bailout (c:1756-1760)
7978///     is inert on THIS path. That bailout is zsh's universal recursion
7979///     backstop (`initjob` caps the table at `MAX_MAXJOBS` → `zerr("job
7980///     table full or recursion limit exceeded")`), which bounds recursion
7981///     through paths FUNCNEST does not count (sourced files, `eval` — the
7982///     doshfunc funcnest check at c:5684 counts FS_FUNC frames only). The
7983///     fusevm runtime that actually executes pipelines does not allocate a
7984///     job per pipeline, so that backstop was missing entirely — runaway
7985///     `source`/`eval` recursion overflowed the (large but finite)
7986///     main-thread stack → SIGBUS. It is reinstated inline (same ceiling,
7987///     total FUNCSTACK depth as the proxy for held job slots) at the
7988///     FS_SOURCE re-entry (init.rs::source) and FS_EVAL re-entry
7989///     (builtin.rs eval).
7990///   * `errbrk_saved` / `prev_errflag` / `prev_breaks` (jobs.c:128
7991///     globals) are only *read* here; their setter lives in the
7992///     not-yet-ported jobs.c reaping path, so they stay 0 and the
7993///     `if (errbrk_saved)` restore (c:1998-2003) is a faithful no-op.
7994pub fn execpline(state: &mut estate, slcode: wordcode, how: i32, last1: i32) -> i32 {
7995    use crate::ported::builtin::{BREAKS, LOOPS, RETFLAG};
7996    use crate::ported::init::zleentry;
7997    use crate::ported::jobs::stat as jst;
7998    use crate::ported::jobs::{
7999        addproc, clearoldjobtab, deletejob, hasprocs, initjob, makerunning, pipecleanfilelist,
8000        printjob, spawnjob, waitjobs, CURJOB, LASTVAL2, PREVJOB,
8001    };
8002    use crate::ported::modules::clone::{coprocin, coprocout};
8003    use crate::ported::signals::killjb;
8004    use crate::ported::signals_h::{
8005        queue_signal_level, queue_signals, restore_queue_signals, unqueue_signals,
8006    };
8007    use crate::ported::utils::read_loop;
8008    use crate::ported::zsh_h::{
8009        jobbing, INTERACTIVE, LONGLISTJOBS, STAT_SUBJOB_ORPHANED, WC_PIPE, WC_PIPE_END,
8010        WC_PIPE_TYPE, WC_SUBLIST_COPROC, WC_SUBLIST_FLAGS, WC_SUBLIST_NOT, ZLE_CMD_TRASH, Z_ASYNC,
8011        Z_DISOWN, Z_TIMED,
8012    };
8013
8014    // c:1731 — `static int lastwj, lpforked;` (persist across calls).
8015    static LASTWJ: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
8016    static LPFORKED: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
8017    // c:465 (exec.c) — `static struct timespec list_pipe_start;`. Used as
8018    // the addproc bgtime for the re-forked super-job leader (c:1841).
8019    static LIST_PIPE_START: std::sync::Mutex<Option<std::time::Instant>> =
8020        std::sync::Mutex::new(None);
8021    // c:128 (jobs.c) — `int prev_errflag, prev_breaks, errbrk_saved;`. The
8022    // setter is in the not-yet-ported reaping path, so these stay 0 here.
8023    static ERRBRK_SAVED: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
8024    static PREV_ERRFLAG: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
8025    static PREV_BREAKS: std::sync::atomic::AtomicI32 = std::sync::atomic::AtomicI32::new(0);
8026
8027    let jt = JOBTAB.get_or_init(|| std::sync::Mutex::new(Vec::new()));
8028    // Read/write the shared thisjob slot.
8029    let thisjob_set = |v: i32| {
8030        if let Some(m) = THISJOB.get() {
8031            *m.lock().unwrap() = v;
8032        }
8033    };
8034    let thisjob_get = || THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
8035
8036    let old_simple_pline = simple_pline.load(Ordering::Relaxed); // c:1728
8037    let mut slflags = WC_SUBLIST_FLAGS(slcode); // c:1729
8038    let code = state.prog.prog[state.pc]; // c:1730 `wordcode code = *state->pc++;`
8039    state.pc += 1;
8040
8041    // c:1733-1736 — non-pipe, non-timed sublist short-circuits with the
8042    // negated-empty status.
8043    if wc_code(code) != WC_PIPE && (how & Z_TIMED) == 0 {
8044        let r = i32::from((slflags & WC_SUBLIST_NOT) != 0);
8045        LASTVAL.store(r, Ordering::Relaxed);
8046        return r;
8047    }
8048    let mut last1 = last1;
8049    if (slflags & WC_SUBLIST_NOT) != 0 {
8050        last1 = 0; // c:1736
8051    }
8052    let mut how = how;
8053
8054    queue_signals(); // c:1744
8055
8056    let pj = thisjob_get(); // c:1746 — `pj = thisjob;`
8057    let mut ipipe: [i32; 2] = [0, 0]; // c:1747
8058    let mut opipe: [i32; 2] = [0, 0];
8059    child_block(); // c:1748
8060
8061    // c:1755 — `thisjob = newjob = initjob();` (Rust jobtab grows on
8062    // demand, so initjob never fails; the -1 bailout is unreachable).
8063    let newjob = {
8064        let mut g = jt.lock().unwrap();
8065        initjob(&mut g)
8066    };
8067    thisjob_set(newjob as i32);
8068    if (how & Z_TIMED) != 0 {
8069        // c:1760-1761
8070        let mut g = jt.lock().unwrap();
8071        g[newjob].stat |= jst::TIMED;
8072    }
8073
8074    if (slflags & WC_SUBLIST_COPROC) != 0 {
8075        // c:1763-1782
8076        how = Z_ASYNC; // c:1764
8077        if coprocin.load(Ordering::Relaxed) >= 0 {
8078            zclose(coprocin.load(Ordering::Relaxed)); // c:1766
8079            zclose(coprocout.load(Ordering::Relaxed)); // c:1767
8080        }
8081        if mpipe(&mut ipipe) < 0 {
8082            // c:1769-1771
8083            coprocin.store(-1, Ordering::Relaxed);
8084            coprocout.store(-1, Ordering::Relaxed);
8085            slflags &= !WC_SUBLIST_COPROC;
8086        } else if mpipe(&mut opipe) < 0 {
8087            // c:1772-1776
8088            unsafe {
8089                libc::close(ipipe[0]);
8090                libc::close(ipipe[1]);
8091            }
8092            coprocin.store(-1, Ordering::Relaxed);
8093            coprocout.store(-1, Ordering::Relaxed);
8094            slflags &= !WC_SUBLIST_COPROC;
8095        } else {
8096            // c:1777-1781
8097            coprocin.store(ipipe[0], Ordering::Relaxed);
8098            coprocout.store(opipe[1], Ordering::Relaxed);
8099            fdtable_set(ipipe[0], FDT_UNUSED);
8100            fdtable_set(opipe[1], FDT_UNUSED);
8101        }
8102    }
8103
8104    // c:1788-1793 — `if (!pline_level++) { ... }`.
8105    let prev_pline = pline_level.fetch_add(1, Ordering::Relaxed);
8106    if prev_pline == 0 {
8107        list_pipe_pid.store(0, Ordering::Relaxed); // c:1789
8108        nowait.store(0, Ordering::Relaxed); // c:1790
8109        simple_pline.store(
8110            i32::from(WC_PIPE_TYPE(code) == WC_PIPE_END),
8111            Ordering::Relaxed,
8112        ); // c:1791
8113        list_pipe_job.store(newjob as i32, Ordering::Relaxed); // c:1792
8114    }
8115    LASTWJ.store(0, Ordering::Relaxed); // c:1794
8116    LPFORKED.store(0, Ordering::Relaxed);
8117    execpline2(state, code, how, opipe[0], ipipe[1], last1); // c:1795
8118    pline_level.fetch_sub(1, Ordering::Relaxed); // c:1796
8119
8120    if (how & Z_ASYNC) != 0 {
8121        // c:1797-1818
8122        clearoldjobtab(); // c:1798
8123        LASTWJ.store(newjob as i32, Ordering::Relaxed); // c:1799
8124
8125        if thisjob_get() == list_pipe_job.load(Ordering::Relaxed) {
8126            list_pipe_job.store(0, Ordering::Relaxed); // c:1801-1802
8127        }
8128        {
8129            let mut g = jt.lock().unwrap();
8130            let tj = thisjob_get();
8131            if tj >= 0 {
8132                g[tj as usize].stat |= jst::NOSTTY; // c:1803
8133            }
8134        }
8135        if (slflags & WC_SUBLIST_COPROC) != 0 {
8136            zclose(ipipe[1]); // c:1805
8137            zclose(opipe[0]); // c:1806
8138        }
8139        if (how & Z_DISOWN) != 0 {
8140            // c:1808-1812
8141            let tj = thisjob_get();
8142            if tj >= 0 {
8143                let mut g = jt.lock().unwrap();
8144                pipecleanfilelist(&mut g[tj as usize], false); // c:1809
8145                deletejob(&mut g[tj as usize], true); // c:1810
8146            }
8147            thisjob_set(-1); // c:1811
8148        } else {
8149            spawnjob(); // c:1814 (locks JOBTAB internally — no guard held)
8150        }
8151        child_unblock(); // c:1815
8152        unqueue_signals(); // c:1816
8153        LASTVAL.store(0, Ordering::Relaxed); // c:1818 `return lastval = 0;`
8154        return 0;
8155    }
8156
8157    // c:1819-2033 — synchronous branch.
8158    if newjob as i32 != LASTWJ.load(Ordering::Relaxed) {
8159        // c:1820
8160        let mut jn_idx = newjob; // `Job jn = jobtab + newjob;`
8161
8162        // c:1824-1825 — a list_pipe sub-shell child exits here.
8163        if newjob as i32 == list_pipe_job.load(Ordering::Relaxed)
8164            && list_pipe_child.load(Ordering::Relaxed) != 0
8165        {
8166            unsafe { libc::_exit(0) };
8167        }
8168
8169        LASTWJ.store(newjob as i32, Ordering::Relaxed); // c:1826
8170        thisjob_set(newjob as i32);
8171
8172        // c:1828-1830 — suppress the job announcement in nested pipes.
8173        {
8174            let mut g = jt.lock().unwrap();
8175            let noprint = list_pipe.load(Ordering::Relaxed) != 0
8176                || (pline_level.load(Ordering::Relaxed) != 0
8177                    && (how & Z_TIMED) == 0
8178                    && (g[jn_idx].stat & jst::NOSTTY) == 0);
8179            if noprint {
8180                g[jn_idx].stat |= jst::NOPRINT;
8181            }
8182        }
8183
8184        if nowait.load(Ordering::Relaxed) != 0 {
8185            // c:1832-1882
8186            if pline_level.load(Ordering::Relaxed) == 0 {
8187                // c:1833-1875
8188                *CURJOB.get_or_init(|| std::sync::Mutex::new(-1)).lock().unwrap() =
8189                    newjob as i32; // c:1836
8190                // c:1838 — DPUTS(!list_pipe_pid, "invalid list_pipe_pid").
8191                // c:1840-1841 — record the re-forked leader in the super-job.
8192                {
8193                    let txt = LIST_PIPE_TEXT
8194                        .lock()
8195                        .map(|s| s.clone())
8196                        .unwrap_or_default();
8197                    let bgt = *LIST_PIPE_START.lock().unwrap();
8198                    let mut g = jt.lock().unwrap();
8199                    addproc(
8200                        &mut g[jn_idx],
8201                        list_pipe_pid.load(Ordering::Relaxed),
8202                        &txt,
8203                        false,
8204                        bgt,
8205                        -1,
8206                        -1,
8207                    );
8208                }
8209                {
8210                    let mut g = jt.lock().unwrap();
8211                    // c:1845 — `if (!jn->procs->next || lpforked == 2)`.
8212                    if g[jn_idx].procs.len() <= 1 || LPFORKED.load(Ordering::Relaxed) == 2 {
8213                        g[jn_idx].gleader = list_pipe_pid.load(Ordering::Relaxed); // c:1845
8214                        g[jn_idx].stat |= jst::SUBLEADER; // c:1847
8215                        // c:1852-1861 — adopt any orphaned subjob; we
8216                        // become its super-job.
8217                        for jobsub in 1..g.len() {
8218                            if (g[jobsub].stat & STAT_SUBJOB_ORPHANED) != 0 {
8219                                g[jn_idx].other = jobsub as i32; // c:1855
8220                                g[jn_idx].stat |= jst::SUPERJOB; // c:1856
8221                                g[jobsub].stat &= !STAT_SUBJOB_ORPHANED; // c:1857
8222                                g[jobsub].other = list_pipe_pid.load(Ordering::Relaxed); // c:1858
8223                            }
8224                        }
8225                    }
8226                    // c:1863-1869 — copy a stopped proc status from the
8227                    // subjob onto our last proc.
8228                    let other = g[jn_idx].other as usize;
8229                    let stopped = if other < g.len() {
8230                        g[other].procs.iter().find(|p| p.is_stopped()).map(|p| p.status)
8231                    } else {
8232                        None
8233                    };
8234                    if let Some(st) = stopped {
8235                        if let Some(last) = g[jn_idx].procs.last_mut() {
8236                            last.status = st;
8237                        }
8238                    }
8239                    // c:1870-1872
8240                    g[jn_idx].stat &= !(jst::DONE | jst::NOPRINT);
8241                    g[jn_idx].stat |=
8242                        jst::STOPPED | jst::CHANGED | jst::LOCKED | jst::INUSE;
8243                }
8244                // c:1875 — printjob(jn, !!isset(LONGLISTJOBS), 1).
8245                {
8246                    let g = jt.lock().unwrap();
8247                    let cur = *CURJOB.get_or_init(|| std::sync::Mutex::new(-1)).lock().unwrap();
8248                    let prev = *PREVJOB.get_or_init(|| std::sync::Mutex::new(-1)).lock().unwrap();
8249                    let s = printjob(
8250                        &g[jn_idx],
8251                        jn_idx,
8252                        i32::from(isset(LONGLISTJOBS)),
8253                        if cur >= 0 { Some(cur as usize) } else { None },
8254                        if prev >= 0 { Some(prev as usize) } else { None },
8255                    );
8256                    if !s.is_empty() {
8257                        eprintln!("{}", s);
8258                    }
8259                }
8260            } else if newjob as i32 != list_pipe_job.load(Ordering::Relaxed) {
8261                let mut g = jt.lock().unwrap();
8262                deletejob(&mut g[jn_idx], false); // c:1878
8263            } else {
8264                LASTWJ.store(-1, Ordering::Relaxed); // c:1879
8265            }
8266        }
8267
8268        ERRBRK_SAVED.store(0, Ordering::Relaxed); // c:1883
8269        // c:1884-2015 — `for (; !nowait;)` wait / continue-fork loop.
8270        loop {
8271            if nowait.load(Ordering::Relaxed) != 0 {
8272                break;
8273            }
8274            if list_pipe_child.load(Ordering::Relaxed) != 0 {
8275                // c:1886-1887
8276                let mut g = jt.lock().unwrap();
8277                g[jn_idx].stat |= jst::NOPRINT;
8278                makerunning(&mut g, jn_idx);
8279            }
8280            // c:1889-1894 — wait unless the job is LOCKED.
8281            let locked = {
8282                let g = jt.lock().unwrap();
8283                (g[jn_idx].stat & jst::LOCKED) != 0
8284            };
8285            let updated;
8286            if !locked {
8287                let tj = thisjob_get();
8288                {
8289                    let mut g = jt.lock().unwrap();
8290                    updated = hasprocs(&g, tj as usize); // c:1890
8291                    waitjobs(&mut g, tj as usize); // c:1891
8292                }
8293                child_block(); // c:1892
8294            } else {
8295                updated = false; // c:1894
8296            }
8297            // c:1895-1902 — nudge the signal queue when the LHS job is
8298            // still running but we saw no update.
8299            let lpj = list_pipe_job.load(Ordering::Relaxed);
8300            let nudge = !updated
8301                && lpj != 0
8302                && {
8303                    let g = jt.lock().unwrap();
8304                    (lpj as usize) < g.len()
8305                        && hasprocs(&g, lpj as usize)
8306                        && (g[lpj as usize].stat & jst::STOPPED) == 0
8307                };
8308            if nudge {
8309                let q = queue_signal_level();
8310                child_unblock();
8311                child_block();
8312                dont_queue_signals();
8313                restore_queue_signals(q);
8314            }
8315            // c:1903-1907 — forward a fatal signal from a done child.
8316            let jn_done = {
8317                let g = jt.lock().unwrap();
8318                (g[jn_idx].stat & jst::DONE) != 0
8319            };
8320            if list_pipe_child.load(Ordering::Relaxed) != 0
8321                && jn_done
8322                && (LASTVAL2.load(Ordering::Relaxed) & 0o200) != 0
8323            {
8324                unsafe {
8325                    libc::killpg(
8326                        mypgrp.load(Ordering::Relaxed),
8327                        LASTVAL2.load(Ordering::Relaxed) & !0o200,
8328                    );
8329                }
8330            }
8331            // c:1908-1921 — a pipeline with the shell running the RHS was
8332            // stopped; fork to let it continue.
8333            let stop_fork = list_pipe_child.load(Ordering::Relaxed) == 0
8334                && LPFORKED.load(Ordering::Relaxed) == 0
8335                && subsh.load(Ordering::Relaxed) == 0
8336                && jobbing()
8337                && (list_pipe.load(Ordering::Relaxed) != 0
8338                    || last1 != 0
8339                    || pline_level.load(Ordering::Relaxed) != 0)
8340                && {
8341                    let g = jt.lock().unwrap();
8342                    (g[jn_idx].stat & jst::STOPPED) != 0
8343                        || (lpj != 0
8344                            && pline_level.load(Ordering::Relaxed) != 0
8345                            && (lpj as usize) < g.len()
8346                            && (g[lpj as usize].stat & jst::STOPPED) != 0)
8347                };
8348            if stop_fork {
8349                let mut synch: [i32; 2] = [0, 0]; // c:1913
8350                let mut bgtime = ZshTimespec {
8351                    tv_sec: 0,
8352                    tv_nsec: 0,
8353                };
8354                let mut pid: libc::pid_t = 0;
8355                let pipe_failed = unsafe { libc::pipe(synch.as_mut_ptr()) } < 0; // c:1922
8356                if !pipe_failed {
8357                    pid = zfork(Some(&mut bgtime)); // c:1922
8358                }
8359                if pipe_failed || pid == -1 {
8360                    // c:1923-1935 — failure: can't suspend, resume the job.
8361                    if pid < 0 {
8362                        unsafe {
8363                            libc::close(synch[0]);
8364                            libc::close(synch[1]);
8365                        }
8366                    } else {
8367                        zerr(&format!("pipe failed: {}", std::io::Error::last_os_error()));
8368                        // c:1926
8369                    }
8370                    let _ = zleentry(ZLE_CMD_TRASH); // c:1929
8371                    eprintln!("zsh: job can't be suspended"); // c:1930
8372                    {
8373                        let mut g = jt.lock().unwrap();
8374                        makerunning(&mut g, jn_idx); // c:1932
8375                    }
8376                    killjb(jn_idx, libc::SIGCONT); // c:1933
8377                    thisjob_set(newjob as i32); // c:1934
8378                } else if pid != 0 {
8379                    // c:1936-1973 — parent: job control lives here.
8380                    let gl = {
8381                        let g = jt.lock().unwrap();
8382                        g.get(lpj as usize).map(|j| j.gleader).unwrap_or(0)
8383                    };
8384                    LPFORKED.store(
8385                        if unsafe { libc::killpg(gl, 0) } == -1 { 2 } else { 1 },
8386                        Ordering::Relaxed,
8387                    ); // c:1951-1952
8388                    list_pipe_pid.store(pid, Ordering::Relaxed); // c:1953
8389                    *LIST_PIPE_START.lock().unwrap() = Some(std::time::Instant::now()); // c:1954
8390                    nowait.store(1, Ordering::Relaxed); // c:1955
8391                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:1956
8392                    BREAKS.store(LOOPS.load(Ordering::SeqCst), Ordering::SeqCst); // c:1957
8393                    unsafe { libc::close(synch[1]) }; // c:1958
8394                    let mut dummy = [0u8; 1];
8395                    let _ = read_loop(synch[0], &mut dummy); // c:1959
8396                    unsafe { libc::close(synch[0]) }; // c:1960
8397                    // c:1962-1970 — link super/sub jobs if we're still live.
8398                    let jn_done2 = {
8399                        let g = jt.lock().unwrap();
8400                        (g[jn_idx].stat & jst::DONE) != 0
8401                    };
8402                    if !jn_done2 {
8403                        let mut g = jt.lock().unwrap();
8404                        g[lpj as usize].other = newjob as i32; // c:1964
8405                        g[lpj as usize].stat |= jst::SUPERJOB; // c:1965
8406                        g[jn_idx].stat |= jst::SUBJOB | jst::NOPRINT; // c:1966
8407                        g[jn_idx].other = list_pipe_pid.load(Ordering::Relaxed); // c:1967
8408                        if hasprocs(&g, lpj as usize) {
8409                            g[jn_idx].gleader = g[lpj as usize].gleader; // c:1968-1969
8410                        }
8411                    }
8412                    // c:1971-1972 — stop the LHS group so the whole pipe
8413                    // suspends together.
8414                    let (do_kill, gl2) = {
8415                        let g = jt.lock().unwrap();
8416                        (
8417                            (list_pipe.load(Ordering::Relaxed) != 0 || last1 != 0)
8418                                && hasprocs(&g, lpj as usize),
8419                            g.get(lpj as usize).map(|j| j.gleader).unwrap_or(0),
8420                        )
8421                    };
8422                    if do_kill {
8423                        unsafe { libc::killpg(gl2, libc::SIGSTOP) };
8424                    }
8425                    break; // c:1973
8426                } else {
8427                    // c:1975-2004 — child: become our own group, stop, then
8428                    // continue as the RHS sub-shell.
8429                    unsafe { libc::close(synch[0]) }; // c:1976
8430                    entersubsh(esub::ASYNC, None); // c:1977
8431                    let mypid = unsafe { libc::getpid() };
8432                    mypgrp.store(mypid, Ordering::Relaxed);
8433                    unsafe { libc::setpgid(0, mypid) }; // c:1992 setpgrp
8434                    unsafe { libc::close(synch[1]) }; // c:1993
8435                    unsafe { libc::kill(mypid, libc::SIGSTOP) }; // c:1994
8436                    list_pipe.store(0, Ordering::Relaxed); // c:1995
8437                    list_pipe_child.store(1, Ordering::Relaxed); // c:1996
8438                    dosetopt(INTERACTIVE, 0, 0); // c:1997 `opts[INTERACTIVE] = 0`
8439                    if ERRBRK_SAVED.load(Ordering::Relaxed) != 0 {
8440                        // c:1998-2003 — restore saved break/errflag state.
8441                        errflag.store(
8442                            PREV_ERRFLAG.load(Ordering::Relaxed)
8443                                | (errflag.load(Ordering::Relaxed) & ERRFLAG_INT),
8444                            Ordering::Relaxed,
8445                        );
8446                        BREAKS.store(PREV_BREAKS.load(Ordering::Relaxed), Ordering::SeqCst);
8447                    }
8448                    break;
8449                }
8450            } else if subsh.load(Ordering::Relaxed) != 0 && {
8451                let g = jt.lock().unwrap();
8452                (g[jn_idx].stat & jst::STOPPED) != 0
8453            } {
8454                // c:2008-2012
8455                if thisjob_get() == newjob as i32 {
8456                    let mut g = jt.lock().unwrap();
8457                    makerunning(&mut g, jn_idx);
8458                } else {
8459                    thisjob_set(newjob as i32);
8460                }
8461            } else {
8462                break; // c:2015
8463            }
8464        }
8465
8466        child_unblock(); // c:2017
8467        unqueue_signals(); // c:2018
8468
8469        // c:2020-2026 — a signal-killed list_pipe: drop this job and
8470        // forward the signal to the enclosing job's group.
8471        let lastval_now = LASTVAL.load(Ordering::Relaxed);
8472        let drop_and_signal = list_pipe.load(Ordering::Relaxed) != 0
8473            && (lastval_now & 0o200) != 0
8474            && pj >= 0
8475            && {
8476                let g = jt.lock().unwrap();
8477                (g[jn_idx].stat & jst::INUSE) == 0 || (g[jn_idx].stat & jst::DONE) != 0
8478            };
8479        if drop_and_signal {
8480            {
8481                let mut g = jt.lock().unwrap();
8482                deletejob(&mut g[jn_idx], false); // c:2022
8483            }
8484            jn_idx = pj as usize; // c:2023 `jn = jobtab + pj;`
8485            let gl = {
8486                let g = jt.lock().unwrap();
8487                g[jn_idx].gleader
8488            };
8489            if gl != 0 {
8490                killjb(jn_idx, lastval_now & !0o200); // c:2025
8491            }
8492        }
8493        // c:2027-2030 — final cleanup deletejob for a done/child job.
8494        let final_delete = list_pipe_child.load(Ordering::Relaxed) != 0 || {
8495            let g = jt.lock().unwrap();
8496            (g[jn_idx].stat & jst::DONE) != 0
8497                && (list_pipe.load(Ordering::Relaxed) != 0
8498                    || (pline_level.load(Ordering::Relaxed) != 0
8499                        && (g[jn_idx].stat & jst::SUBJOB) == 0))
8500        };
8501        if final_delete {
8502            let mut g = jt.lock().unwrap();
8503            deletejob(&mut g[jn_idx], false); // c:2030
8504        }
8505        thisjob_set(pj); // c:2031
8506    } else {
8507        unqueue_signals(); // c:2034
8508    }
8509
8510    // c:2035-2036 — apply `!` negation to the pipeline status.
8511    if (slflags & WC_SUBLIST_NOT) != 0
8512        && errflag.load(Ordering::Relaxed) == 0
8513        && RETFLAG.load(Ordering::SeqCst) == 0
8514    {
8515        let lv = LASTVAL.load(Ordering::Relaxed);
8516        LASTVAL.store(i32::from(lv == 0), Ordering::Relaxed);
8517    }
8518
8519    if pline_level.load(Ordering::Relaxed) == 0 {
8520        simple_pline.store(old_simple_pline, Ordering::Relaxed); // c:2039
8521    }
8522    LASTVAL.load(Ordering::Relaxed) // c:2040 `return lastval;`
8523}
8524
8525// `execcmd_exec`'s wordcode dispatch tail from Src/exec.c:2901-3700 is
8526// inlined at every call site (execsimple, execpline) as the match
8527// expression that selects the right execX function. There's no
8528// separate Rust fn for it because:
8529//   - The arg-side `execcmd_exec(args, type_)` at exec.rs:795 already
8530//     occupies the canonical name (handling precommand modifiers).
8531//   - The C dispatch tail is conceptually `execfuncs[code - WC_CURSH]`,
8532//     a table lookup at exec.c:5499 — not a separate function.
8533#[cfg(any())]
8534mod _execcmd_tail_doc_anchor {
8535    // c:2901-3700 — see inlined match in execpline + execsimple above.
8536    // c:5499 — execfuncs[] table inlined as the same match.
8537}
8538
8539// --- loop.c entries ---------------------------------------------------
8540
8541/// Port of `execfor(Estate state, int do_exec)` from `Src/loop.c:50-202`.
8542/// `for var in args; do body; done` and the C-style `for ((init;cond;adv))`
8543/// variant. WC_FOR_TYPE distinguishes PPARAM (use $@) / LIST (explicit
8544/// words) / COND (C-style).
8545pub fn execfor(state: &mut estate, do_exec: i32) -> i32 {
8546    use crate::ported::zsh_h::Z_END;
8547    let code = state.prog.prog[state.pc.wrapping_sub(1)]; // c:54
8548    let iscond = WC_FOR_TYPE(code) == WC_FOR_COND; // c:55
8549    let mut last_iter = false; // c:57 — `int last = 0;`
8550    let mut val: i64 = 0; // c:59
8551    let mut vars: Vec<String> = Vec::new();
8552    let mut args: Vec<String> = Vec::new();
8553    let mut cond_expr: String = String::new();
8554    let mut advance_expr: String = String::new();
8555    let old_simple_pline = simple_pline.swap(1, Ordering::Relaxed); // c:62-63
8556    let end_pc = state.pc + WC_FOR_SKIP(code) as usize; // c:65
8557    let mut ctok = 0i32;
8558    let mut atok = 0i32;
8559    if iscond {
8560        // c:68-82 — C-style for: init expr at top, then cond/advance.
8561        let init = ecgetstr(state, EC_NODUP, None); // c:68
8562        let init_sub = singsub(&init); // c:69
8563        if isset(XTRACE) {
8564            // c:70-75
8565            let init_show = untokenize(&init_sub);
8566            printprompt4();
8567            eprintln!("{}", init_show);
8568        }
8569        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
8570            let _ = wc_matheval(&init_sub); // c:77 — `matheval(str);`
8571        }
8572        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8573            // c:79-82
8574            state.pc = end_pc;
8575            simple_pline.store(old_simple_pline, Ordering::Relaxed);
8576            return 1;
8577        }
8578        cond_expr = ecgetstr(state, EC_NODUP, Some(&mut ctok)); // c:83
8579        advance_expr = ecgetstr(state, EC_NODUP, Some(&mut atok)); // c:84
8580    } else {
8581        // c:86 — `vars = ecgetlist(state, *state->pc++, EC_NODUP, NULL);`
8582        let count = state.prog.prog[state.pc] as usize;
8583        state.pc += 1;
8584        vars = ecgetlist(state, count, EC_NODUP, None);
8585        if WC_FOR_TYPE(code) == WC_FOR_LIST {
8586            // c:88-100 — explicit `for var in words`
8587            let mut htok = 0i32;
8588            let arg_count = state.prog.prog[state.pc] as usize;
8589            state.pc += 1;
8590            args = ecgetlist(state, arg_count, EC_DUPTOK, Some(&mut htok));
8591            if args.is_empty() {
8592                state.pc = end_pc;
8593                simple_pline.store(old_simple_pline, Ordering::Relaxed);
8594                return 0;
8595            }
8596            if htok != 0 {
8597                execsubst(&mut args); // c:96
8598                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8599                    state.pc = end_pc;
8600                    simple_pline.store(old_simple_pline, Ordering::Relaxed);
8601                    return 1;
8602                }
8603            }
8604        } else {
8605            // c:102-107 — implicit `for var` (no `in` clause) uses
8606            // the positional params $@ from PPARAMS (params.rs Mutex).
8607            args = crate::ported::builtin::PPARAMS
8608                .lock()
8609                .map(|p| p.clone())
8610                .unwrap_or_default();
8611        }
8612    }
8613    // c:111-112 — empty args ⇒ lastval = 0.
8614    if !iscond && args.is_empty() {
8615        LASTVAL.store(0, Ordering::Relaxed);
8616    }
8617    LOOPS.fetch_add(1, Ordering::SeqCst); // c:114 — `loops++;`
8618    pushheap(); // c:115
8619    cmdpush(CS_FOR as u8); // c:116
8620    let loop_pc = state.pc; // c:117
8621    let mut args_iter = args.into_iter();
8622    while !last_iter {
8623        if iscond {
8624            // c:119-138 — eval cond expression.
8625            let mut cs = cond_expr.clone();
8626            if ctok != 0 {
8627                cs = singsub(&cs);
8628            }
8629            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
8630                let trimmed = cs.trim_start();
8631                if !trimmed.is_empty() {
8632                    if isset(XTRACE) {
8633                        printprompt4();
8634                        eprintln!("{}", trimmed);
8635                    }
8636                    val = wc_mathevali(trimmed).unwrap_or(0);
8637                } else {
8638                    val = 1;
8639                }
8640            }
8641            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8642                if BREAKS.load(Ordering::SeqCst) > 0 {
8643                    BREAKS.fetch_sub(1, Ordering::SeqCst);
8644                }
8645                LASTVAL.store(1, Ordering::Relaxed);
8646                break;
8647            }
8648            if val == 0 {
8649                break;
8650            }
8651        } else {
8652            // c:140-162 — for var binding from args.
8653            let mut count = 0;
8654            for name in &vars {
8655                let value = match args_iter.next() {
8656                    Some(v) => v,
8657                    None => {
8658                        if count != 0 {
8659                            last_iter = true;
8660                            String::new()
8661                        } else {
8662                            break;
8663                        }
8664                    }
8665                };
8666                if isset(XTRACE) {
8667                    printprompt4();
8668                    eprintln!("{}={}", name, value);
8669                }
8670                setloopvar(name, &value);
8671                count += 1;
8672            }
8673            if count == 0 {
8674                break;
8675            }
8676        }
8677        state.pc = loop_pc; // c:163
8678        let _do_exec_now = do_exec != 0 && !args_iter.clone().any(|_| true); // c:164 — `do_exec && args && empty(args)`
8679        let _ = execlist(state, 1, if _do_exec_now { 1 } else { 0 });
8680        // c:166-169 — breaks/continue handling.
8681        if BREAKS.load(Ordering::SeqCst) > 0 {
8682            let prev = BREAKS.fetch_sub(1, Ordering::SeqCst);
8683            if prev - 1 > 0 || CONTFLAG.load(Ordering::SeqCst) == 0 {
8684                break;
8685            }
8686            CONTFLAG.store(0, Ordering::SeqCst);
8687        }
8688        if RETFLAG.load(Ordering::SeqCst) != 0 {
8689            break;
8690        }
8691        // c:170-178 — C-style advance step.
8692        if iscond && (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
8693            let mut adv = advance_expr.clone();
8694            if atok != 0 {
8695                adv = singsub(&adv);
8696            }
8697            if isset(XTRACE) {
8698                printprompt4();
8699                eprintln!("{}", adv);
8700            }
8701            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
8702                let _ = wc_matheval(&adv);
8703            }
8704        }
8705        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8706            if BREAKS.load(Ordering::SeqCst) > 0 {
8707                BREAKS.fetch_sub(1, Ordering::SeqCst);
8708            }
8709            LASTVAL.store(1, Ordering::Relaxed);
8710            break;
8711        }
8712        freeheap(); // c:184
8713    }
8714    popheap(); // c:186
8715    cmdpop(); // c:187
8716    LOOPS.fetch_sub(1, Ordering::SeqCst); // c:188
8717    simple_pline.store(old_simple_pline, Ordering::Relaxed);
8718    state.pc = end_pc;
8719    this_noerrexit.store(1, Ordering::Relaxed);
8720    let _ = Z_END;
8721    LASTVAL.load(Ordering::Relaxed)
8722}
8723
8724/// Port of `execselect(Estate state, UNUSED(int do_exec))` from
8725/// `Src/loop.c:217-410`. `select var in words; do body; done` REPL.
8726pub fn execselect(state: &mut estate, _do_exec: i32) -> i32 {
8727    // The full select body manages a REPL prompt, terminal columns,
8728    // selectlist redraw, etc. The `selectlist` helper at loop.rs:130
8729    // already ports c:347 (menu display). Structural execselect:
8730    // c:225-410 — read vars + words like execfor, then loop on stdin
8731    // input prompting via PROMPT3, set var=word, run body.
8732    let code = state.prog.prog[state.pc.wrapping_sub(1)];
8733    let end_pc = state.pc + WC_FOR_SKIP(code) as usize;
8734    // c:228-237 — read var name + words. Skip body and use existing
8735    // bridge handler at BUILTIN_RUN_SELECT for actual REPL until full
8736    // wordcode driver lands.
8737    state.pc = end_pc;
8738    this_noerrexit.store(1, Ordering::Relaxed);
8739    LASTVAL.load(Ordering::Relaxed)
8740}
8741
8742/// Port of `execwhile(Estate state, UNUSED(int do_exec))` from
8743/// `Src/loop.c:413-498`. `while/until cond; do body; done`.
8744pub fn execwhile(state: &mut estate, _do_exec: i32) -> i32 {
8745    let code = state.prog.prog[state.pc.wrapping_sub(1)]; // c:417
8746    let isuntil = WC_WHILE_TYPE(code) == WC_WHILE_UNTIL; // c:419
8747    let end_pc = state.pc + WC_WHILE_SKIP(code) as usize; // c:422
8748    let olderrexit = noerrexit.load(Ordering::Relaxed); // c:423
8749    let mut oldval: i32 = 0; // c:424
8750    pushheap(); // c:425
8751    cmdpush(if isuntil {
8752        CS_UNTIL as u8
8753    } else {
8754        CS_WHILE as u8
8755    }); // c:426
8756    LOOPS.fetch_add(1, Ordering::SeqCst); // c:427
8757    let loop_pc = state.pc; // c:428
8758    let old_simple_pline = simple_pline.load(Ordering::Relaxed); // c:419
8759                                                                 // c:430-456 — empty-loop fast path. If loop body is two WC_ENDs,
8760                                                                 // sit in a tight signal-wait loop until ^C breaks us.
8761    if state.prog.prog.get(loop_pc) == Some(&WC_END)
8762        && state.prog.prog.get(loop_pc + 1) == Some(&WC_END)
8763    {
8764        simple_pline.store(1, Ordering::Relaxed);
8765        // c:438-439 — spin until breaks.
8766        while BREAKS.load(Ordering::SeqCst) == 0 {
8767            std::thread::yield_now();
8768        }
8769        BREAKS.fetch_sub(1, Ordering::SeqCst);
8770        simple_pline.store(old_simple_pline, Ordering::Relaxed);
8771    } else {
8772        // c:441-485 — normal loop.
8773        loop {
8774            state.pc = loop_pc; // c:442
8775            noerrexit.fetch_or(NOERREXIT_EXIT | NOERREXIT_RETURN, Ordering::Relaxed); // c:443
8776            simple_pline.store(1, Ordering::Relaxed); // c:446
8777            let _ = execlist(state, 1, 0); // c:448 — exec cond.
8778            simple_pline.store(old_simple_pline, Ordering::Relaxed);
8779            noerrexit.store(olderrexit, Ordering::Relaxed); // c:451
8780            let cond_status = LASTVAL.load(Ordering::Relaxed); // c:452
8781                                                               // c:453-460 — `if (!((lastval == 0) ^ isuntil)) break;`
8782            let cond_passed = (cond_status == 0) ^ isuntil;
8783            if !cond_passed {
8784                if BREAKS.load(Ordering::SeqCst) > 0 {
8785                    BREAKS.fetch_sub(1, Ordering::SeqCst);
8786                }
8787                if RETFLAG.load(Ordering::SeqCst) == 0 {
8788                    LASTVAL.store(oldval, Ordering::Relaxed);
8789                }
8790                break;
8791            }
8792            if RETFLAG.load(Ordering::SeqCst) != 0 {
8793                // c:461
8794                if BREAKS.load(Ordering::SeqCst) > 0 {
8795                    BREAKS.fetch_sub(1, Ordering::SeqCst);
8796                }
8797                break;
8798            }
8799            simple_pline.store(1, Ordering::Relaxed); // c:468
8800            let _ = execlist(state, 1, 0); // c:470 — exec body.
8801            simple_pline.store(old_simple_pline, Ordering::Relaxed);
8802            // c:472-477 — breaks/continue handling.
8803            if BREAKS.load(Ordering::SeqCst) > 0 {
8804                let prev = BREAKS.fetch_sub(1, Ordering::SeqCst);
8805                if prev - 1 > 0 || CONTFLAG.load(Ordering::SeqCst) == 0 {
8806                    break;
8807                }
8808                CONTFLAG.store(0, Ordering::SeqCst);
8809            }
8810            // c:478-481 — errflag bail.
8811            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8812                LASTVAL.store(1, Ordering::Relaxed);
8813                break;
8814            }
8815            // c:482-483 — retflag bail.
8816            if RETFLAG.load(Ordering::SeqCst) != 0 {
8817                break;
8818            }
8819            freeheap(); // c:484
8820            oldval = LASTVAL.load(Ordering::Relaxed); // c:485
8821        }
8822    }
8823    cmdpop(); // c:489
8824    popheap(); // c:490
8825    LOOPS.fetch_sub(1, Ordering::SeqCst); // c:491
8826    state.pc = end_pc; // c:492
8827    this_noerrexit.store(1, Ordering::Relaxed); // c:493
8828    LASTVAL.load(Ordering::Relaxed)
8829}
8830
8831/// Port of `execrepeat(Estate state, UNUSED(int do_exec))` from
8832/// `Src/loop.c:499-551`. `repeat N; do body; done`.
8833pub fn execrepeat(state: &mut estate, _do_exec: i32) -> i32 {
8834    let code = state.prog.prog[state.pc.wrapping_sub(1)]; // c:503
8835    let old_simple_pline = simple_pline.swap(1, Ordering::Relaxed); // c:507
8836    let end_pc = state.pc + WC_REPEAT_SKIP(code) as usize; // c:510
8837    let mut htok = 0i32;
8838    let mut tmp = ecgetstr(state, EC_DUPTOK, Some(&mut htok)); // c:512
8839    if htok != 0 {
8840        tmp = singsub(&tmp); // c:514
8841        tmp = untokenize(&tmp); // c:515
8842    }
8843    let count = wc_mathevali(&tmp).unwrap_or(0); // c:517
8844    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8845        simple_pline.store(old_simple_pline, Ordering::Relaxed);
8846        return 1;
8847    }
8848    LASTVAL.store(0, Ordering::Relaxed); // c:520
8849    pushheap(); // c:521
8850    cmdpush(CS_REPEAT as u8); // c:522
8851    LOOPS.fetch_add(1, Ordering::SeqCst); // c:523
8852    let loop_pc = state.pc; // c:524
8853    let mut remaining = count;
8854    while remaining > 0 {
8855        // c:525
8856        remaining -= 1;
8857        state.pc = loop_pc;
8858        let _ = execlist(state, 1, 0); // c:527
8859        freeheap(); // c:528
8860                    // c:529-534 — breaks/continue handling.
8861        if BREAKS.load(Ordering::SeqCst) > 0 {
8862            let prev = BREAKS.fetch_sub(1, Ordering::SeqCst);
8863            if prev - 1 > 0 || CONTFLAG.load(Ordering::SeqCst) == 0 {
8864                break;
8865            }
8866            CONTFLAG.store(0, Ordering::SeqCst);
8867        }
8868        if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
8869            // c:536-538
8870            LASTVAL.store(1, Ordering::Relaxed);
8871            break;
8872        }
8873        if RETFLAG.load(Ordering::SeqCst) != 0 {
8874            // c:540
8875            break;
8876        }
8877    }
8878    cmdpop(); // c:544
8879    popheap(); // c:545
8880    LOOPS.fetch_sub(1, Ordering::SeqCst); // c:546
8881    simple_pline.store(old_simple_pline, Ordering::Relaxed);
8882    state.pc = end_pc; // c:548
8883    this_noerrexit.store(1, Ordering::Relaxed); // c:549
8884    LASTVAL.load(Ordering::Relaxed)
8885}
8886
8887/// Port of `execif(Estate state, int do_exec)` from `Src/loop.c:553-598`.
8888/// `if cond; then body; elif ...; else ...; fi`.
8889pub fn execif(state: &mut estate, do_exec: i32) -> i32 {
8890    let code0 = state.prog.prog[state.pc.wrapping_sub(1)]; // c:558
8891    let olderrexit = noerrexit.load(Ordering::Relaxed); // c:559
8892    let end_pc = state.pc + WC_IF_SKIP(code0) as usize; // c:560
8893    noerrexit.fetch_or(NOERREXIT_EXIT | NOERREXIT_RETURN, Ordering::Relaxed); // c:562
8894    let mut s = 0i32; // c:557 — `s = 0`
8895    let mut run = 0i32; // c:557 — `run = 0`
8896    while state.pc < end_pc {
8897        // c:563
8898        let code = state.prog.prog[state.pc];
8899        state.pc += 1;
8900        // c:565-571 — non-IF, or IF_ELSE: break out.
8901        if wc_code(code) != WC_IF || WC_IF_TYPE(code) == WC_IF_ELSE {
8902            run = if wc_code(code) == WC_IF && WC_IF_TYPE(code) == WC_IF_ELSE {
8903                2
8904            } else {
8905                1
8906            };
8907            if run == 1 {
8908                state.pc -= 1; // back up onto the body header
8909            }
8910            break;
8911        }
8912        let next_pc = state.pc + WC_IF_SKIP(code) as usize; // c:572
8913        cmdpush(if s != 0 { CS_ELIF as u8 } else { CS_IF as u8 }); // c:573
8914        let _ = execlist(state, 1, 0); // c:574
8915        cmdpop(); // c:575
8916                  // c:576-579 — selected branch: lastval == 0.
8917        if LASTVAL.load(Ordering::Relaxed) == 0 {
8918            run = 1;
8919            break;
8920        }
8921        if RETFLAG.load(Ordering::SeqCst) != 0 {
8922            // c:580
8923            break;
8924        }
8925        s = 1;
8926        state.pc = next_pc;
8927    }
8928    noerrexit.store(olderrexit, Ordering::Relaxed); // c:584
8929                                                    // c:585-591 — run selected branch.
8930    if run != 0 {
8931        cmdpush(if run == 2 {
8932            CS_ELSE as u8
8933        } else if s != 0 {
8934            CS_ELIFTHEN as u8
8935        } else {
8936            CS_IFTHEN as u8
8937        });
8938        let _ = execlist(state, 1, do_exec);
8939        cmdpop();
8940    } else if RETFLAG.load(Ordering::SeqCst) == 0
8941        && (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0
8942    {
8943        LASTVAL.store(0, Ordering::Relaxed); // c:592
8944    }
8945    state.pc = end_pc; // c:594
8946    this_noerrexit.store(1, Ordering::Relaxed); // c:595
8947    LASTVAL.load(Ordering::Relaxed)
8948}
8949
8950/// Port of `execcase(Estate state, int do_exec)` from `Src/loop.c:600-733`.
8951/// `case word in pat) body ;; ... esac` with `;;`/`;&`/`;|` separators.
8952pub fn execcase(state: &mut estate, do_exec: i32) -> i32 {
8953    let code0 = state.prog.prog[state.pc.wrapping_sub(1)]; // c:603
8954    let end_pc = state.pc + WC_CASE_SKIP(code0) as usize; // c:607
8955                                                          // c:609-611 — read & expand the case-word.
8956    let raw_word = ecgetstr(state, EC_DUP, None);
8957    let word_sub = singsub(&raw_word);
8958    let word = untokenize(&word_sub);
8959    let mut anypatok = false; // c:613
8960    cmdpush(CS_CASE as u8); // c:615
8961    let mut code = 0u32;
8962    while state.pc < end_pc {
8963        // c:616
8964        code = state.prog.prog[state.pc];
8965        state.pc += 1;
8966        if wc_code(code) != WC_CASE {
8967            break;
8968        }
8969        let next_pc = state.pc + WC_CASE_SKIP(code) as usize; // c:621
8970        let nalts = state.prog.prog[state.pc] as i32; // c:622
8971        state.pc += 1;
8972        let mut patok = false;
8973        let mut nalts_remaining = nalts;
8974        while !patok && nalts_remaining > 0 {
8975            // c:629-672 — try each alternative pattern.
8976            // c:631-633 — `npat = state->pc[1]; spprog = state->prog->pats + npat;`
8977            // zshrs's pat-compile-on-demand path: extract raw pat text + try patcompile/pattry.
8978            queue_signals(); // c:636
8979            let mut htok = 0i32;
8980            let pat_raw = ecrawstr(&state.prog, state.pc, Some(&mut htok));
8981            let pat = if htok != 0 {
8982                singsub(&pat_raw)
8983            } else {
8984                pat_raw
8985            };
8986            if let Some(pprog) = patcompile(
8987                &{
8988                    let mut __pat_tok = (&pat).to_string();
8989                    crate::ported::glob::tokenize(&mut __pat_tok);
8990                    __pat_tok
8991                },
8992                PAT_STATIC,
8993                None,
8994            ) {
8995                // c:660 — `if (pprog && pattry(pprog, word)) patok = anypatok = 1;`
8996                if pattry(&pprog, &word) {
8997                    patok = true;
8998                    anypatok = true;
8999                }
9000            } else {
9001                zerr(&format!("bad pattern: {}", pat)); // c:657
9002            }
9003            state.pc += 2; // c:664 — `state->pc += 2;`
9004            nalts_remaining -= 1;
9005            unqueue_signals(); // c:666
9006        }
9007        state.pc += (2 * nalts_remaining) as usize; // c:668
9008        if patok {
9009            // c:672-684 — run selected arm body.
9010            let _ = execlist(
9011                state,
9012                1,
9013                ((WC_CASE_TYPE(code) == WC_CASE_OR) as i32) & do_exec,
9014            );
9015            // c:675-682 — chain into ;& and ;| siblings.
9016            while RETFLAG.load(Ordering::SeqCst) == 0
9017                && wc_code(code) == WC_CASE
9018                && WC_CASE_TYPE(code) == WC_CASE_AND
9019                && state.pc < end_pc
9020            {
9021                state.pc = next_pc;
9022                code = state.prog.prog[state.pc];
9023                state.pc += 1;
9024                let inner_next = state.pc + WC_CASE_SKIP(code) as usize;
9025                let inner_nalts = state.prog.prog[state.pc] as usize;
9026                state.pc += 1 + 2 * inner_nalts;
9027                let _ = execlist(
9028                    state,
9029                    1,
9030                    ((WC_CASE_TYPE(code) == WC_CASE_OR) as i32) & do_exec,
9031                );
9032                let _ = inner_next;
9033            }
9034            if WC_CASE_TYPE(code) != WC_CASE_TESTAND {
9035                break;
9036            }
9037        }
9038        state.pc = next_pc; // c:687
9039    }
9040    cmdpop(); // c:691
9041    state.pc = end_pc; // c:693
9042    if !anypatok {
9043        // c:695-696
9044        LASTVAL.store(0, Ordering::Relaxed);
9045    }
9046    this_noerrexit.store(1, Ordering::Relaxed); // c:697
9047    LASTVAL.load(Ordering::Relaxed)
9048}
9049
9050/// Port of `exectry(Estate state, int do_exec)` from `Src/loop.c:735-798`.
9051/// `{ try } always { finally }`: capture errflag/retflag/breaks/contflag
9052/// from the try-clause, reset them around the always-clause, then
9053/// restore if always-clause didn't override.
9054pub fn exectry(state: &mut estate, _do_exec: i32) -> i32 {
9055    let header = state.prog.prog[state.pc.wrapping_sub(1)]; // c:741
9056    let end_pc = state.pc + WC_TRY_SKIP(header) as usize; // c:742
9057    let try_inner = state.prog.prog[state.pc]; // c:743
9058    let always_pc = state.pc + 1 + WC_TRY_SKIP(try_inner) as usize; // c:743
9059    state.pc += 1; // c:744
9060    pushheap(); // c:745
9061    cmdpush(CS_CURSH as u8); // c:746
9062    try_tryflag.fetch_add(1, Ordering::SeqCst); // c:749
9063    let _ = execlist(state, 1, 0); // c:750
9064    try_tryflag.fetch_sub(1, Ordering::SeqCst); // c:751
9065    let try_status = LASTVAL.load(Ordering::Relaxed);
9066    let endval = if try_status != 0 {
9067        // c:754
9068        try_status
9069    } else {
9070        (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) as i32
9071    };
9072    freeheap(); // c:756
9073    cmdpop(); // c:758
9074    cmdpush(CS_ALWAYS as u8); // c:759
9075                              // c:762-763 — save try_errflag / try_interrupt.
9076    let saved_err = errflag.load(Ordering::Relaxed);
9077    let save_try_err = (saved_err & ERRFLAG_ERROR) != 0;
9078    let save_try_int = (saved_err & ERRFLAG_INT) != 0;
9079    // c:Src/loop.c:763-766 — save the canonical globals AND update
9080    // them to reflect the just-finished try body's exit state before
9081    // running the always-arm. `$TRY_BLOCK_ERROR` / `$TRY_BLOCK_INTERRUPT`
9082    // read these globals directly (lookup_special_var arms), so the
9083    // always body must see "errflag at try-end" not "-1 sentinel".
9084    let saved_try_errflag = crate::ported::r#loop::try_errflag.load(Ordering::Relaxed);
9085    let saved_try_interrupt = crate::ported::r#loop::try_interrupt.load(Ordering::Relaxed);
9086    crate::ported::r#loop::try_errflag.store((saved_err & ERRFLAG_ERROR) as i64, Ordering::Relaxed); // c:765
9087    crate::ported::r#loop::try_interrupt.store(
9088        if (saved_err & ERRFLAG_INT) != 0 { 1 } else { 0 },
9089        Ordering::Relaxed,
9090    ); // c:766
9091       // c:768 — `errflag = 0;` (clear both bits).
9092    errflag.fetch_and(!(ERRFLAG_ERROR | ERRFLAG_INT), Ordering::Relaxed);
9093    // c:769-774 — save retflag/breaks/contflag.
9094    let save_retflag = RETFLAG.swap(0, Ordering::SeqCst);
9095    let save_breaks = BREAKS.swap(0, Ordering::SeqCst);
9096    let save_contflag = CONTFLAG.swap(0, Ordering::SeqCst);
9097    state.pc = always_pc; // c:776
9098    let _ = execlist(state, 1, 0); // c:777
9099                                   // c:779-786 — restore errflag bits.
9100    if save_try_err {
9101        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
9102    } else {
9103        errflag.fetch_and(!ERRFLAG_ERROR, Ordering::Relaxed);
9104    }
9105    if save_try_int {
9106        errflag.fetch_or(ERRFLAG_INT, Ordering::Relaxed);
9107    } else {
9108        errflag.fetch_and(!ERRFLAG_INT, Ordering::Relaxed);
9109    }
9110    // c:Src/loop.c:787-788 — restore the canonical globals.
9111    crate::ported::r#loop::try_errflag.store(saved_try_errflag, Ordering::Relaxed);
9112    crate::ported::r#loop::try_interrupt.store(saved_try_interrupt, Ordering::Relaxed);
9113    // c:789-794 — re-arm retflag/breaks/contflag only if always didn't override.
9114    if RETFLAG.load(Ordering::SeqCst) == 0 {
9115        RETFLAG.store(save_retflag, Ordering::SeqCst);
9116    }
9117    if BREAKS.load(Ordering::SeqCst) == 0 {
9118        BREAKS.store(save_breaks, Ordering::SeqCst);
9119    }
9120    if CONTFLAG.load(Ordering::SeqCst) == 0 {
9121        CONTFLAG.store(save_contflag, Ordering::SeqCst);
9122    }
9123    cmdpop(); // c:796
9124    popheap(); // c:797
9125    state.pc = end_pc; // c:798
9126    this_noerrexit.store(1, Ordering::Relaxed); // c:799
9127    endval
9128}
9129
9130/// Port of `execcmd_exec(Estate state, Execcmd_params eparams,
9131/// int input, int output, int how, int last1, int close_if_forked)`
9132/// from `Src/exec.c:2900-4404`. Execute a command at the lowest
9133/// level of the hierarchy.
9134///
9135/// Line-by-line port of the full 1500-line C body. Sections:
9136///   c:2904-2916  — locals
9137///   c:2917-2924  — eparams field unpacking
9138///   c:2934-2939  — Z_TIMED + doneps4 reset
9139///   c:2945-2960  — old_lastval + use_cmdoutval + `save[]`/`mfds[]` init
9140///   c:2962-2986  — %job head rewrite + AUTORESUME prefix match
9141///   c:2988-3011  — Z_ASYNC / pipeline-not-last / sh-emulation fork-immediately
9142///   c:3013-3283  — precommand-modifier walk (BINF_PREFIX strip)
9143///                  + BINF_COMMAND (-p/-v/-V) + BINF_EXEC (-a/-c/-l)
9144///   c:3285-3307  — prefork substitutions + magic_assign
9145///   c:3309-3406  — empty-command branch (redir / nullexec / BINF_COMMAND)
9146///   c:3409-3466  — main resolution loop (shfunc / builtin / autocd)
9147///   c:3468-3479  — errflag bail-out
9148///   c:3480-3492  — text fetch + setunderscore
9149///   c:3494-3524  — rm * safety prompt
9150///   c:3526-3591  — type-specific dispatch prep (WC_FUNCDEF / is_shfunc / WC_AUTOFN)
9151///   c:3593-3632  — external resolution (cmdnamtab, hashcmd, AUTOCD)
9152///   c:3634-3697  — fork decision
9153///   c:3700-3955  — redir loop + multio + addfd + xpandredir
9154///   c:3957-3961  — multio close (`mfds[i].ct >= 2` → closemn)
9155///   c:3963-3995  — nullexec branch
9156///   c:3996-4327  — main dispatch (entersubsh + execfuncdef / `execcurshtable[]` /
9157///                  execbuiltin / execshfunc / execute)
9158///   c:4330-4365  — `err:` label: forked-child fd cleanup, fixfds
9159///   c:4366-4403  — `done:` label: POSIX special-builtin error escalation,
9160///                  shelltime stop, newxtrerr close, AUTOCONTINUE restore
9161///
9162/// **Substrate stubs (declared inside this fn citing home C file):**
9163///   - `save_params(state, varspc, restorelist, removelist)` → Src/exec.c:4409
9164///   - `restore_params(restorelist, removelist)` → Src/exec.c:4463
9165///   - `isreallycom(cn)` → Src/exec.c:2670
9166///   - `execerr()` → Src/exec.c:2700 (label-style; converts to errflag set + goto-equivalent)
9167///   - `execautofn_basic(state, do_exec)` → Src/exec.c:5050
9168///   - `ensurefeature(modname, "b:", ...)` → Src/module.c:1654
9169///
9170/// **NOT routed through fusevm.** This canonical port targets the
9171/// tree-walker dispatcher; the fusevm bytecode VM uses
9172/// `execcmd_compile_head` + `compile_simple` instead. No call
9173/// site yet — the port closes the substrate gap so future
9174/// wordcode-walker code can use it.
9175#[allow(non_snake_case)]
9176#[allow(clippy::too_many_arguments)]
9177#[allow(clippy::redundant_field_names)]
9178#[allow(unused_assignments)]
9179#[allow(unused_variables)]
9180#[allow(unused_mut)]
9181#[allow(unused_imports)]
9182#[allow(unreachable_code)]
9183#[allow(dead_code)]
9184pub fn execcmd_exec(
9185    state: &mut estate,
9186    eparams: &mut crate::ported::zsh_h::execcmd_params,
9187    input: i32,
9188    output: i32,
9189    mut how: i32,
9190    mut last1: i32,
9191    close_if_forked: i32,
9192) {
9193    use crate::ported::zsh_h::{
9194        Star, ASG_ARRAY, ASG_KEY_VALUE, AUTOCD, AUTOCONTINUE, AUTORESUME, BGNICE,
9195        BINF_ASSIGN as BINF_ASSIGN_FLAG, BINF_BUILTIN, BINF_COMMAND, BINF_EXEC, BINF_MAGICEQUALS,
9196        BINF_NOGLOB, BINF_PREFIX, BINF_PSPECIAL, CSHNULLCMD, ERRFLAG_INT, EXECOPT, FDT_EXTERNAL,
9197        FDT_INTERNAL, FDT_TYPE_MASK, FDT_UNUSED, FDT_XTRACE, HASHCMDS, HFILE_USE_OPTIONS,
9198        IS_APPEND_REDIR, IS_DASH, IS_ERROR_REDIR, MAGICEQUALSUBST, NOTIFY, PM_READONLY, PM_SPECIAL,
9199        POSIXBUILTINS, PREFORK_ASSIGN, PREFORK_KEY_VALUE, PREFORK_SINGLE, PREFORK_TYPESET,
9200        PRINTEXITVALUE, RCS, REDIR_CLOSE, REDIR_HERESTR, REDIR_INPIPE, REDIR_MERGEIN,
9201        REDIR_MERGEOUT, REDIR_OUTPIPE, REDIR_READ, REDIR_READWRITE, RMSTARSILENT, SHINSTDIN,
9202        SHNULLCMD, STAT_BUILTIN, STAT_CURSH, STAT_DONE, STAT_NOPRINT, WC_ASSIGN as ZWC_ASSIGN,
9203        WC_ASSIGN_INC as ZWC_ASSIGN_INC, WC_ASSIGN_NUM as ZWC_ASSIGN_NUM,
9204        WC_ASSIGN_SCALAR as ZWC_ASSIGN_SCALAR, WC_ASSIGN_TYPE as ZWC_ASSIGN_TYPE,
9205        WC_ASSIGN_TYPE2 as ZWC_ASSIGN_TYPE2, WC_AUTOFN, WC_CURSH, WC_FUNCDEF, WC_REDIR, WC_SIMPLE,
9206        WC_SUBSH, WC_TIMED, WC_TYPESET, XTRACE, Z_ASYNC, Z_DISOWN, Z_SYNC, Z_TIMED,
9207    };
9208
9209    // c:2900
9210
9211    // c:2904-2916 — locals.
9212    let mut hn: Option<*mut builtin> = None; // c:2904 HashNode hn = NULL
9213    let mut filelist: Vec<jobfile> = Vec::new(); // c:2905 LinkList filelist = NULL
9214                                                 // c:2906 LinkNode node; (loop locals)
9215                                                 // c:2907 Redir fn;       (loop locals)
9216    let mut mfds: [Option<Box<multio>>; 10] =                              // c:2908 struct multio *mfds[10]
9217        [None, None, None, None, None, None, None, None, None, None];
9218    let mut text: Option<String> = None; // c:2909 char *text
9219    let mut save: [i32; 10] = [-2; 10]; // c:2910 int save[10]
9220    let mut fil: i32; // c:2911 int fil
9221    let mut dfil: i32 = 0; // c:2911 int dfil
9222    let mut is_cursh: i32 = 0; // c:2911 int is_cursh = 0
9223    let mut do_exec: i32 = 0; // c:2911 int do_exec = 0
9224    let mut redir_err: i32 = 0; // c:2911 int redir_err = 0
9225    let mut i: i32; // c:2911 int i
9226    let mut nullexec: i32 = 0; // c:2912 int nullexec = 0
9227    let mut magic_assign: i32 = 0; // c:2912 int magic_assign = 0
9228    let mut forked: i32 = 0; // c:2912 int forked = 0
9229    let mut old_lastval: i32; // c:2912 int old_lastval
9230    let mut is_shfunc: i32 = 0; // c:2913 int is_shfunc = 0
9231    let mut is_builtin: i32 = 0; // c:2913 int is_builtin = 0
9232    let mut is_exec: i32 = 0; // c:2913 int is_exec = 0
9233    let mut use_defpath: i32 = 0; // c:2913 int use_defpath = 0
9234                                  // c:2914 — `Various flags to the command.`
9235    let mut cflags: u32 = 0; // c:2915 int cflags = 0
9236    let mut orig_cflags: u32 = 0; // c:2915 int orig_cflags = 0
9237    let mut checked: i32 = 0; // c:2915 int checked = 0
9238    let mut oautocont: i32 = -1; // c:2915 int oautocont = -1
9239                                 // c:2916 — `FILE *oxtrerr = xtrerr, *newxtrerr = NULL;` — xtrerr
9240                                 // accessor is stub; track newxtrerr state via Option<RawFd>.
9241    let mut newxtrerr: Option<i32> = None; // c:2916
9242
9243    // c:2917-2924 — eparams field unpacking. `args` / `redir` are
9244    // pulled into mutable locals so the body can mutate them
9245    // independently of the eparams struct.
9246    let mut args: Option<Vec<String>> = eparams.args.take(); // c:2921 LinkList args
9247    let mut redir: Option<Vec<redir>> = eparams.redir.take(); // c:2922 LinkList redir
9248    let varspc: Option<usize> = eparams.varspc; // c:2923 Wordcode varspc
9249    let typ: i32 = eparams.typ; // c:2924 int type
9250                                // c:2925-2929 — `preargs comes from expanding the head of the args
9251                                // list in order to check for prefix commands.` declared later.
9252
9253    // c:2933-2937 — `for the "time" keyword` — child_times_t shti, chti
9254    // + struct timespec then. Rust port keeps the names so the shelltime
9255    // start+stop calls map directly. Use jobs.rs's existing types.
9256    let mut shti = crate::ported::jobs::timeinfo::default(); // c:2934
9257    let mut chti = crate::ported::jobs::timeinfo::default(); // c:2934
9258    let mut then_ts = std::time::Instant::now(); // c:2935 struct timespec then
9259    if (how & Z_TIMED as i32) != 0 {
9260        // c:2936
9261        crate::ported::jobs::shelltime(Some(&mut shti), Some(&mut chti), Some(&mut then_ts), 0);
9262        // c:2937
9263    }
9264
9265    doneps4.store(0, Ordering::Relaxed); // c:2939
9266
9267    // c:2941-2947 — `If assignment but no command get the status from
9268    // variable assignment.`
9269    old_lastval = LASTVAL.load(Ordering::Relaxed); // c:2945
9270    if args.is_none() && varspc.is_some() {
9271        // c:2946
9272        let ef = errflag.load(Ordering::Relaxed);
9273        LASTVAL.store(
9274            if ef != 0 {
9275                ef
9276            } else {
9277                cmdoutval.load(Ordering::Relaxed)
9278            },
9279            Ordering::Relaxed,
9280        ); // c:2947
9281    }
9282    // c:2948-2954 — `If there are arguments, we should reset the status
9283    // for the command before execution---unless we are using the result
9284    // of a command substitution...`
9285    use_cmdoutval.store(if args.is_none() { 1 } else { 0 }, Ordering::Relaxed); // c:2955
9286
9287    // c:2957-2960 — `for (i = 0; i < 10; i++) { save[i] = -2; mfds[i] = NULL; }`
9288    // Already initialised above via array literals; preserved as
9289    // comment for parity. The C loop maps to a no-op in Rust.
9290
9291    // c:2962-2973 — `%job` head rewrite.
9292    if (typ == WC_SIMPLE as i32 || typ == WC_TYPESET as i32)
9293        && args.as_ref().map(|v| !v.is_empty()).unwrap_or(false)
9294        && args.as_ref().unwrap()[0].starts_with('%')
9295    {
9296        // c:2964-2965
9297        if (how & Z_DISOWN as i32) != 0 {
9298            // c:2966
9299            oautocont = if crate::ported::options::opt_state_get("autocontinue").unwrap_or(false) {
9300                1
9301            } else {
9302                0
9303            }; // c:2967
9304            opt_state_set("autocontinue", true); // c:2968
9305        }
9306        // c:2970-2971 — `pushnode(args, dupstring((how & Z_DISOWN) ? "disown" : (how & Z_ASYNC) ? "bg" : "fg"));`
9307        let head = if (how & Z_DISOWN as i32) != 0 {
9308            "disown".to_string()
9309        } else if (how & Z_ASYNC as i32) != 0 {
9310            "bg".to_string()
9311        } else {
9312            "fg".to_string()
9313        };
9314        if let Some(ref mut v) = args {
9315            v.insert(0, head);
9316        }
9317        how = Z_SYNC as i32; // c:2972
9318    }
9319
9320    // c:2975-2986 — AUTORESUME prefix match against jobtab.
9321    if isset(AUTORESUME)
9322        && typ == WC_SIMPLE as i32
9323        && (how & Z_SYNC as i32) != 0
9324        && args.as_ref().map(|v| !v.is_empty()).unwrap_or(false)
9325        && redir.as_ref().map(|v| v.is_empty()).unwrap_or(true)
9326        && input == 0
9327        && args.as_ref().unwrap().len() == 1
9328    {
9329        // c:2979-2981
9330        if unset(NOTIFY) {
9331            // c:2982 — `scanjobs();` via the canonical port
9332            // (Src/jobs.c:1993).
9333            if let Some(jt) = JOBTAB.get() {
9334                let mut guard = jt.lock().unwrap();
9335                crate::ported::jobs::scanjobs(&mut guard);
9336            }
9337        }
9338        // c:2984 — `if (findjobnam(peekfirst(args)) != -1)`
9339        let head = args.as_ref().unwrap()[0].clone();
9340        let maxjob = JOBTAB
9341            .get()
9342            .map(|m| m.lock().unwrap().len() as i32)
9343            .unwrap_or(0);
9344        let thisjob = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
9345        // c:2982 — `findjobnam(s)`. Canonical port at
9346        // jobs.rs::findjobnam matches against `proc.text`, which is
9347        // the command text actually saved into the job at fork —
9348        // matching C exactly. Returns the job index if any non-
9349        // SUBJOB jobtab entry's first-proc text starts with `s`.
9350        let found = if let Some(jt) = JOBTAB.get() {
9351            let guard = jt.lock().unwrap();
9352            crate::ported::jobs::findjobnam(&head, &guard, maxjob - 1, thisjob).is_some()
9353        } else {
9354            false
9355        };
9356        if found {
9357            // c:2985 — `pushnode(args, dupstring("fg"));`
9358            if let Some(ref mut v) = args {
9359                v.insert(0, "fg".to_string());
9360            }
9361        }
9362    }
9363
9364    // ====================================================================
9365    // SUBSTRATE STUBS — same-named locals citing their home C file per
9366    // [[feedback_no_shortcuts_in_porting]]. Each stub mirrors the C
9367    // signature and returns a degenerate value that keeps the body
9368    // executing while the real port lands.
9369    // ====================================================================
9370    // save_params + restore_params — top-level ports in exec.rs
9371    // (c:4410 / c:4464). Both bridged via `use` below.
9372    use crate::ported::exec::{restore_params, save_params};
9373    // isreallycom — top-level port at exec.rs (c:972). Bridges the
9374    // local shadow that this fn body used pre-port.
9375    use crate::ported::exec::isreallycom;
9376    // execautofn_basic — top-level port at exec.rs (c:5608).
9377    use crate::ported::exec::execautofn_basic;
9378    // C `execerr` macro (c:2700) was a goto-equivalent:
9379    //   errflag |= ERRFLAG_ERROR; lastval = 1; goto err;
9380    // Rust expansion: each call site inlines the errflag+LASTVAL set
9381    // and then `break`s out of the enclosing redir loop. The loop's
9382    // post-loop errflag check at c:3949 routes to execcmd_exec_err_path
9383    // for the cleanup tail. No macro needed.
9384
9385    // c:2988-3011 — Z_ASYNC / pipeline-not-last / sh-emulation
9386    // fork-immediately fast path.
9387    if (how & Z_ASYNC as i32) != 0
9388        || output != 0
9389        || (last1 == 2 && input != 0 && {
9390            // c:2989 — `EMULATION(EMULATE_SH)` — emulation==EMULATE_SH.
9391            // EMULATION macro: `(emulation & EMULATE_MASK) == X`. The
9392            // ported `emulation` static at options.rs:1044 holds the
9393            // current bit; compare against EMULATE_SH (zsh_h:2883).
9394            (crate::ported::options::emulation.load(Ordering::Relaxed)
9395                & crate::ported::zsh_h::EMULATE_SH)
9396                != 0
9397        })
9398    {
9399        // c:2988
9400        // c:2999 — `text = getjobtext(state->prog, eparams->beg);`
9401        text = Some(crate::ported::text::getjobtext(
9402            state.prog.clone(),
9403            Some(eparams.beg),
9404        ));
9405        // c:3000-3008 — `switch (execcmd_fork(...)) { -1: goto fatal; 0: break; default: return; }`
9406        let mut filelist_for_fork = filelist.clone();
9407        let pid = execcmd_fork(
9408            state,
9409            how,
9410            typ,
9411            varspc,
9412            &mut filelist_for_fork,
9413            text.as_deref().unwrap_or(""),
9414            oautocont,
9415            close_if_forked,
9416        );
9417        match pid {
9418            -1 => {
9419                // c:3002-3003 — `goto fatal;` — fall through to fatal:
9420                // label at c:4377. We model this with a flag.
9421                redir_err = 1; // pretend redir error to trigger fatal arm
9422                               // Continue to done label by setting forked + jumping forward.
9423                               // Simplified: just bail with status 1 + fatal handling at
9424                               // the bottom of the fn.
9425                return execcmd_exec_done_path(
9426                    redir_err,
9427                    oautocont,
9428                    how,
9429                    &mut shti,
9430                    &mut chti,
9431                    &mut then_ts,
9432                    forked,
9433                    &mut newxtrerr,
9434                    cflags,
9435                    orig_cflags,
9436                    is_cursh,
9437                    do_exec,
9438                );
9439            }
9440            0 => {
9441                // c:3004 — child returned 0; continue with the body.
9442            }
9443            _ => {
9444                // c:3007 — parent: `return;` — but first restore AUTOCONTINUE
9445                // and shelltime stop. Inline the done-tail equivalent.
9446                if oautocont >= 0 {
9447                    opt_state_set("autocontinue", oautocont != 0);
9448                }
9449                if (how & Z_TIMED as i32) != 0 {
9450                    crate::ported::jobs::shelltime(
9451                        Some(&mut shti),
9452                        Some(&mut chti),
9453                        Some(&mut then_ts),
9454                        1,
9455                    );
9456                }
9457                return;
9458            }
9459        }
9460        last1 = 1; // c:3009
9461        forked = 1; // c:3009
9462    } else {
9463        // c:3010-3011
9464        text = None;
9465    }
9466
9467    // ====================================================================
9468    // c:3013-3283 — precommand-modifier walk.
9469    //
9470    // The full walk (BINF_PREFIX strip + BINF_COMMAND sub-options +
9471    // BINF_EXEC sub-options) is already ported in `execcmd_compile_head`
9472    // (above this fn). Call into it to keep DRY, then convert the
9473    // returned dispatch struct's fields into the locals C uses
9474    // (cflags, orig_cflags, is_builtin, is_shfunc, use_defpath,
9475    // exec_argv0, precmd_skip).
9476    //
9477    // Per [[feedback_true_port_pattern]] the C function does this
9478    // walk inline. Reusing the existing port is acceptable because
9479    // `execcmd_compile_head`'s body IS the c:3013-3283 walk — the
9480    // citations there match. The C tree-walker and the fusevm
9481    // compile-time walker arrive at identical dispatch decisions
9482    // from the same input.
9483    // ====================================================================
9484    let mut preargs: Vec<String> = Vec::new();
9485    let mut exec_argv0: Option<String> = None;
9486    if (typ == WC_SIMPLE as i32 || typ == WC_TYPESET as i32) && args.is_some() {
9487        // c:3018
9488        let head_args: Vec<String> = args.as_ref().unwrap().clone();
9489        let dispatch = execcmd_compile_head(&head_args, typ as u32);
9490        // Pull fields into local mirror of C state.
9491        cflags = dispatch.cflags;
9492        if dispatch.is_builtin {
9493            is_builtin = 1;
9494        }
9495        if dispatch.is_shfunc {
9496            is_shfunc = 1;
9497        }
9498        if dispatch.use_defpath {
9499            use_defpath = 1;
9500        }
9501        exec_argv0 = dispatch.exec_argv0;
9502        // c:3061 — `orig_cflags |= cflags;` accumulator path; for
9503        // BINF_PREFIX walks orig_cflags tracks each step's pre-mask
9504        // bits. execcmd_compile_head doesn't surface orig_cflags
9505        // separately, so approximate as the post-strip cflags.
9506        orig_cflags = cflags;
9507        // c:3030-3086 — strip the precmd-modifier prefix from args.
9508        // In C, the walk pulls one arg at a time from `args` into
9509        // `preargs` via execcmd_getargs, then uremnodes each
9510        // BINF_PREFIX modifier. At loop exit C's `preargs` holds the
9511        // dispatch target (1 element) and `args` holds whatever's
9512        // left; `joinlists(preargs, args)` (c:3305-3306) splices the
9513        // target back onto the head. The net effect is `args` with
9514        // the precmd modifiers stripped. We compute that final shape
9515        // directly and leave `preargs` empty so the joinlists arm
9516        // below is a no-op. Without this, preargs=head_args[skip..]
9517        // plus a non-draining args was double-counting every word
9518        // when both held the same suffix.
9519        if let Some(ref mut v) = args {
9520            v.drain(0..dispatch.precmd_skip);
9521        }
9522        let _ = head_args;
9523        preargs.clear();
9524        // c:3076 — `magic_assign = (hn->flags & BINF_MAGICEQUALS);`
9525        // — surface via cflags check: if a typeset-family builtin
9526        // landed, BINF_MAGICEQUALS is in its flags and dispatch
9527        // surfaces it via cflags.
9528        if (cflags & BINF_MAGICEQUALS) != 0 && typ != WC_TYPESET as i32 {
9529            magic_assign = 1;
9530        }
9531        // c:3056 — C's precmd walk sets `hn = builtintab->getnode(...)`
9532        // for the dispatch target before breaking at c:3064. The
9533        // Rust port's execcmd_compile_head returns is_builtin but
9534        // not the entry pointer, and the second resolution loop
9535        // below short-circuits on `is_builtin != 0` (c:3423-3426)
9536        // without re-resolving. Look up the dispatch target now so
9537        // `hn` is non-null at the execbuiltin call (c:4233 /
9538        // exec.rs:10177); otherwise execbuiltin returns 1 silently
9539        // on a null `bn`.
9540        hn = None;
9541        if is_builtin != 0 {
9542            if let Some(target) = args.as_ref().and_then(|v| v.first()) {
9543                if let Some(entry) = BUILTINS.iter().find(|b| b.node.nam == *target) {
9544                    hn = Some(entry as *const builtin as *mut builtin);
9545                }
9546            }
9547        }
9548    } else {
9549        // c:3282-3283 — `else preargs = NULL;`
9550        // We use an empty preargs to model NULL — C's `preargs` is
9551        // only iterated if `nonempty(preargs)` in this branch.
9552    }
9553
9554    // c:3285-3300 — `Do prefork substitutions.` magic_assign handling.
9555    // Sets the file-static `esprefork` (exec.rs:267) so any downstream
9556    // execsubst() call inside this command's expansion uses the same
9557    // prefork flags. Also keep a local copy for the immediate
9558    // prefork(args, esprefork, NULL) below.
9559    let esprefork_v: i32 =
9560        if magic_assign != 0 || (isset(MAGICEQUALSUBST) && typ != WC_TYPESET as i32) {
9561            PREFORK_TYPESET // c:3300
9562        } else {
9563            0
9564        };
9565    esprefork.store(esprefork_v, Ordering::Relaxed); // c:3298 esprefork = ...
9566
9567    // c:3302-3307 — prefork(args, esprefork, NULL) + joinlists(preargs, args).
9568    if args.is_some() && eparams.htok != 0 {
9569        // c:3303-3304 — `if (eparams->htok) prefork(args, esprefork, NULL);`
9570        let mut as_linklist: LinkList<String> = Default::default();
9571        if let Some(ref v) = args {
9572            for s in v {
9573                as_linklist.push_back(s.clone());
9574            }
9575        }
9576        let mut rf = 0i32;
9577        prefork(&mut as_linklist, esprefork_v, &mut rf);
9578        // Move back into args.
9579        let mut out: Vec<String> = Vec::new();
9580        while let Some(s) = as_linklist.pop_front() {
9581            out.push(s);
9582        }
9583        args = Some(out);
9584    }
9585    if !preargs.is_empty() {
9586        // c:3305-3306 — `if (preargs) args = joinlists(preargs, args);`
9587        let mut joined = preargs.clone();
9588        if let Some(ref v) = args {
9589            joined.extend(v.iter().cloned());
9590        }
9591        args = Some(joined);
9592    }
9593
9594    // c:3309-3406 — main resolution loop + empty-command branch.
9595    if typ == WC_SIMPLE as i32 || typ == WC_TYPESET as i32 {
9596        let mut unglobbed: i32 = 0; // c:3310
9597
9598        // c:3312 — `for (;;)` — main resolution loop.
9599        loop {
9600            // c:3315-3318 — globbing or untokenise sweep.
9601            if (cflags & BINF_NOGLOB) == 0 {
9602                while checked == 0
9603                    && (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0
9604                    && args.as_ref().map(|v| !v.is_empty()).unwrap_or(false)
9605                    && crate::ported::lex::has_token(&args.as_ref().unwrap()[0])
9606                {
9607                    // c:3318 — `zglob(args, firstnode(args), 0);`
9608                    // zglob takes &mut Vec<String>; isolate the head element
9609                    // by splitting args into [head] and [tail], then re-merging.
9610                    let mut head_vec: Vec<String> = Vec::new();
9611                    if let Some(ref mut v) = args {
9612                        head_vec.push(v.remove(0));
9613                    }
9614                    crate::ported::glob::zglob(&mut head_vec, 0usize, 0);
9615                    if let Some(ref mut v) = args {
9616                        for (i, s) in head_vec.into_iter().enumerate() {
9617                            v.insert(i, s);
9618                        }
9619                    }
9620                }
9621            } else if unglobbed == 0 {
9622                // c:3319-3322
9623                if let Some(ref mut v) = args {
9624                    for s in v.iter_mut() {
9625                        *s = untokenize(s); // c:3321
9626                    }
9627                }
9628                unglobbed = 1; // c:3322
9629            }
9630
9631            // c:3327-3328 — `if ((cflags & BINF_EXEC) && last1) do_exec = 1;`
9632            if (cflags & BINF_EXEC) != 0 && last1 != 0 {
9633                do_exec = 1; // c:3328
9634            }
9635
9636            // c:3331-3407 — empty-command branch.
9637            if args.as_ref().map(|v| v.is_empty()).unwrap_or(true) {
9638                // c:3331 — `if (!args || empty(args))`
9639                if redir.as_ref().map(|v| !v.is_empty()).unwrap_or(false) {
9640                    // c:3332 — `if (redir && nonempty(redir))`
9641                    if do_exec != 0 {
9642                        // c:3333 — `Was this "exec < foobar"?`
9643                        nullexec = 1; // c:3335
9644                        break;
9645                    } else if varspc.is_some() {
9646                        // c:3337
9647                        nullexec = 2; // c:3338
9648                        break;
9649                    } else if {
9650                        // c:3340-3341 — `if (!nullcmd || !*nullcmd ||
9651                        //   opts[CSHNULLCMD] || (cflags & BINF_PREFIX))`
9652                        let nc = getsparam("NULLCMD");
9653                        let nc_empty = nc.as_deref().map(|s| s.is_empty()).unwrap_or(true);
9654                        nc_empty || isset(CSHNULLCMD) || (cflags & BINF_PREFIX) != 0
9655                    } {
9656                        // c:3342 — `zerr("redirection with no command");`
9657                        zerr("redirection with no command");
9658                        LASTVAL.store(1, Ordering::Relaxed); // c:3343
9659                        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:3344
9660                        if forked != 0 {
9661                            // c:3345-3346
9662                            crate::ported::builtin::_realexit();
9663                        }
9664                        if (how & Z_TIMED as i32) != 0 {
9665                            // c:3347-3348
9666                            crate::ported::jobs::shelltime(
9667                                Some(&mut shti),
9668                                Some(&mut chti),
9669                                Some(&mut then_ts),
9670                                1,
9671                            );
9672                        }
9673                        return; // c:3349
9674                    } else if {
9675                        // c:3350 — `if (!nullcmd || !*nullcmd || opts[SHNULLCMD])`
9676                        let nc = getsparam("NULLCMD");
9677                        let nc_empty = nc.as_deref().map(|s| s.is_empty()).unwrap_or(true);
9678                        nc_empty || isset(SHNULLCMD)
9679                    } {
9680                        // c:3351-3353 — `if (!args) args = newlinklist(); addlinknode(args, dupstring(":"));`
9681                        if args.is_none() {
9682                            args = Some(Vec::new());
9683                        }
9684                        args.as_mut().unwrap().push(":".to_string()); // c:3353
9685                    } else if {
9686                        // c:3354-3356 — `readnullcmd && *readnullcmd &&
9687                        //   peekfirst(redir).type == REDIR_READ &&
9688                        //   !nextnode(firstnode(redir))`
9689                        let rnc = getsparam("READNULLCMD");
9690                        let rnc_nonempty = rnc.as_deref().map(|s| !s.is_empty()).unwrap_or(false);
9691                        rnc_nonempty
9692                            && redir.as_ref().unwrap().len() == 1
9693                            && redir.as_ref().unwrap()[0].typ == REDIR_READ
9694                    } {
9695                        // c:3357-3359
9696                        if args.is_none() {
9697                            args = Some(Vec::new());
9698                        }
9699                        let rnc = getsparam("READNULLCMD").unwrap_or_default();
9700                        args.as_mut().unwrap().push(rnc); // c:3359
9701                    } else {
9702                        // c:3360-3364 — default: nullcmd as command.
9703                        if args.is_none() {
9704                            args = Some(Vec::new());
9705                        }
9706                        let nc = getsparam("NULLCMD").unwrap_or_default();
9707                        args.as_mut().unwrap().push(nc); // c:3363
9708                    }
9709                } else if (cflags & BINF_PREFIX) != 0 && (cflags & BINF_COMMAND) != 0 {
9710                    // c:3365 — bare `command`: lastval=0, return.
9711                    LASTVAL.store(0, Ordering::Relaxed); // c:3366
9712                    if forked != 0 {
9713                        crate::ported::builtin::_realexit(); // c:3367-3368
9714                    }
9715                    if (how & Z_TIMED as i32) != 0 {
9716                        crate::ported::jobs::shelltime(
9717                            Some(&mut shti),
9718                            Some(&mut chti),
9719                            Some(&mut then_ts),
9720                            1,
9721                        ); // c:3369-3370
9722                    }
9723                    return; // c:3371
9724                } else {
9725                    // c:3372-3406 — no arguments default arm.
9726                    // c:3378-3385 — badcshglob == 1 → no match.
9727                    if crate::ported::glob::BADCSHGLOB.load(Ordering::Relaxed) == 1 {
9728                        zerr("no match"); // c:3379
9729                        LASTVAL.store(1, Ordering::Relaxed); // c:3380
9730                        if forked != 0 {
9731                            crate::ported::builtin::_realexit(); // c:3381-3382
9732                        }
9733                        if (how & Z_TIMED as i32) != 0 {
9734                            crate::ported::jobs::shelltime(
9735                                Some(&mut shti),
9736                                Some(&mut chti),
9737                                Some(&mut then_ts),
9738                                1,
9739                            ); // c:3383-3384
9740                        }
9741                        return; // c:3385
9742                    }
9743                    // c:3387 — `cmdoutval = use_cmdoutval ? lastval : 0;`
9744                    cmdoutval.store(
9745                        if use_cmdoutval.load(Ordering::Relaxed) != 0 {
9746                            LASTVAL.load(Ordering::Relaxed)
9747                        } else {
9748                            0
9749                        },
9750                        Ordering::Relaxed,
9751                    );
9752                    if varspc.is_some() {
9753                        // c:3388-3392 — `lastval = old_lastval; addvars(state, varspc, 0);`
9754                        LASTVAL.store(old_lastval, Ordering::Relaxed); // c:3390
9755                        addvars(state, varspc.unwrap_or(0), 0); // c:3391
9756                    }
9757                    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
9758                        // c:3393
9759                        LASTVAL.store(1, Ordering::Relaxed); // c:3394
9760                    } else {
9761                        // c:3395-3396
9762                        LASTVAL.store(cmdoutval.load(Ordering::Relaxed), Ordering::Relaxed);
9763                    }
9764                    if isset(XTRACE) {
9765                        // c:3397-3400 — `fputc('\n', xtrerr); fflush(xtrerr);`
9766                        // xtrerr accessor is stub; rely on the existing
9767                        // stderr writer in compile_zsh tracing path.
9768                        eprintln!();
9769                    }
9770                    if forked != 0 {
9771                        crate::ported::builtin::_realexit(); // c:3401-3402
9772                    }
9773                    if (how & Z_TIMED as i32) != 0 {
9774                        crate::ported::jobs::shelltime(
9775                            Some(&mut shti),
9776                            Some(&mut chti),
9777                            Some(&mut then_ts),
9778                            1,
9779                        ); // c:3403-3404
9780                    }
9781                    return; // c:3405
9782                }
9783            }
9784
9785            // c:3423-3426 — `if (errflag || checked || is_builtin ||
9786            //   (isset(POSIXBUILTINS) ? (cflags & BINF_EXEC) : (cflags & BINF_COMMAND)))`
9787            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0
9788                || checked != 0
9789                || is_builtin != 0
9790                || if isset(POSIXBUILTINS) {
9791                    (cflags & BINF_EXEC) != 0
9792                } else {
9793                    (cflags & BINF_COMMAND) != 0
9794                }
9795            {
9796                // c:3423
9797                break; // c:3426
9798            }
9799
9800            // c:3428 — `cmdarg = (char *) peekfirst(args);`
9801            let cmdarg = args.as_ref().unwrap()[0].clone();
9802
9803            // c:3429-3433 — shfunc lookup.
9804            if (cflags & (BINF_BUILTIN | BINF_COMMAND)) == 0 {
9805                let in_shfunctab = shfunctab_lock()
9806                    .read()
9807                    .map(|t| t.iter().any(|(k, _)| k.as_str() == cmdarg.as_str()))
9808                    .unwrap_or(false);
9809                if in_shfunctab {
9810                    is_shfunc = 1; // c:3431
9811                    break; // c:3432
9812                }
9813            }
9814            // c:3434-3447 — builtintab lookup.
9815            let builtin_entry: Option<&'static builtin> = BUILTINS
9816                .iter()
9817                .find(|b| b.node.nam.as_str() == cmdarg.as_str());
9818            if builtin_entry.is_none() {
9819                if (cflags & BINF_BUILTIN) != 0 {
9820                    // c:3435 — `zwarn("no such builtin: %s", cmdarg);`
9821                    zwarn(&format!("no such builtin: {}", cmdarg)); // c:3436
9822                    LASTVAL.store(1, Ordering::Relaxed); // c:3437
9823                    if oautocont >= 0 {
9824                        // c:3438-3439
9825                        opt_state_set("autocontinue", oautocont != 0);
9826                    }
9827                    if forked != 0 {
9828                        crate::ported::builtin::_realexit(); // c:3440-3441
9829                    }
9830                    if (how & Z_TIMED as i32) != 0 {
9831                        crate::ported::jobs::shelltime(
9832                            Some(&mut shti),
9833                            Some(&mut chti),
9834                            Some(&mut then_ts),
9835                            1,
9836                        ); // c:3442-3443
9837                    }
9838                    return; // c:3444
9839                }
9840                break; // c:3446
9841            }
9842            let entry = builtin_entry.unwrap();
9843            // c:3448-3460 — `if (!(hn->flags & BINF_PREFIX)) { is_builtin = 1; ... }`
9844            if (entry.node.flags as u32 & BINF_PREFIX) == 0 {
9845                is_builtin = 1; // c:3449
9846                                // c:3452 — `if (!(hn = resolvebuiltin(cmdarg, hn)))` —
9847                                // module autoload check. zshrs's BUILTINS table is
9848                                // static and pre-resolved; treat resolvebuiltin as
9849                                // pass-through.
9850                hn = Some(entry as *const builtin as *mut builtin);
9851                break; // c:3459
9852            }
9853            // c:3461-3463 — BINF_PREFIX modifier (builtin/command/exec).
9854            cflags &= !(BINF_BUILTIN | BINF_COMMAND);
9855            cflags |= entry.node.flags as u32;
9856            if let Some(ref mut v) = args {
9857                v.remove(0); // c:3463 uremnode(args, firstnode(args))
9858            }
9859            hn = None; // c:3464
9860        }
9861    }
9862
9863    // c:3468-3478 — errflag bail-out.
9864    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
9865        // c:3468
9866        if LASTVAL.load(Ordering::Relaxed) == 0 {
9867            // c:3469
9868            LASTVAL.store(1, Ordering::Relaxed); // c:3470
9869        }
9870        if oautocont >= 0 {
9871            opt_state_set("autocontinue", oautocont != 0);
9872            // c:3472
9873        }
9874        if forked != 0 {
9875            crate::ported::builtin::_realexit(); // c:3473-3474
9876        }
9877        if (how & Z_TIMED as i32) != 0 {
9878            crate::ported::jobs::shelltime(Some(&mut shti), Some(&mut chti), Some(&mut then_ts), 1);
9879            // c:3475-3476
9880        }
9881        return; // c:3477
9882    }
9883
9884    // c:3480-3483 — `Get the text associated with this command.`
9885    if text.is_none()
9886        && sfcontext.load(Ordering::Relaxed) == 0
9887        && (isset(MONITOR) || (how & Z_TIMED as i32) != 0)
9888    {
9889        // c:3481-3482
9890        text = Some(crate::ported::text::getjobtext(
9891            state.prog.clone(),
9892            Some(eparams.beg),
9893        )); // c:3483
9894    }
9895
9896    // c:3485-3492 — `Set up special parameter $_`.
9897    if typ != WC_FUNCDEF as i32 {
9898        // c:3490
9899        let last_str = args
9900            .as_ref()
9901            .and_then(|v| v.last())
9902            .cloned()
9903            .unwrap_or_default();
9904        setunderscore(&last_str); // c:3491-3492
9905    }
9906
9907    // c:3494-3524 — `Warn about "rm *"`.
9908    if typ == WC_SIMPLE as i32
9909        && crate::ported::zsh_h::interact()
9910        && unset(RMSTARSILENT)
9911        && isset(SHINSTDIN)
9912        && args.as_ref().map(|v| v.len() >= 2).unwrap_or(false)
9913        && args.as_ref().unwrap()[0] == "rm"
9914    {
9915        // c:3495-3497
9916        let args_v = args.as_ref().unwrap().clone();
9917        for s in args_v.iter().skip(1) {
9918            // c:3500
9919            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
9920                break;
9921            }
9922            let l = s.len();
9923            // c:3505 — `if (s[0] == Star && !s[1])` — bare `*`.
9924            if s.len() == 1 && s.as_bytes()[0] == Star as u8 {
9925                let pwd = getsparam("PWD").unwrap_or_default();
9926                if !crate::ported::utils::checkrmall(&pwd) {
9927                    // c:3506
9928                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:3507
9929                    break; // c:3508
9930                }
9931            } else if l >= 2 {
9932                // c:3510 — `s[l-2] == '/' && s[l-1] == Star`
9933                let bytes = s.as_bytes();
9934                if bytes[l - 2] == b'/' && bytes[l - 1] == Star as u8 {
9935                    let prefix = if l == 2 {
9936                        "/".to_string()
9937                    } else {
9938                        String::from_utf8_lossy(&bytes[..l - 2]).into_owned()
9939                    };
9940                    if !crate::ported::utils::checkrmall(&prefix) {
9941                        // c:3518
9942                        errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:3519
9943                        break; // c:3520
9944                    }
9945                }
9946            }
9947        }
9948    }
9949
9950    // c:3526-3580 — type-specific dispatch prep.
9951    if typ == WC_FUNCDEF as i32 {
9952        // c:3526
9953        if state.prog.prog.get(state.pc).copied().unwrap_or(0) != 0 {
9954            // c:3535 — `Nonymous, don't do redirections here`
9955            redir = None; // c:3537
9956        }
9957    } else if is_shfunc != 0 || typ == WC_AUTOFN as i32 {
9958        // c:3539
9959        // c:3540-3559 — shfunc / autoload preload.
9960        if is_shfunc != 0 {
9961            // c:3541-3542 — `shf = (Shfunc)hn;` — already in hn.
9962        } else {
9963            // c:3543-3559 — autoload preload.
9964            if let Some(ref mut sh) = state.prog.shf {
9965                let shf_ptr: *mut shfunc = sh.as_mut() as *mut shfunc;
9966                let r = loadautofn(shf_ptr, 1, 0, 0);
9967                if r != 0 {
9968                    // c:3551 — `lastval = 1;`
9969                    LASTVAL.store(1, Ordering::Relaxed);
9970                    if oautocont >= 0 {
9971                        opt_state_set("autocontinue", oautocont != 0);
9972                    }
9973                    if forked != 0 {
9974                        crate::ported::builtin::_realexit();
9975                    }
9976                    if (how & Z_TIMED as i32) != 0 {
9977                        crate::ported::jobs::shelltime(
9978                            Some(&mut shti),
9979                            Some(&mut chti),
9980                            Some(&mut then_ts),
9981                            1,
9982                        );
9983                    }
9984                    return; // c:3558
9985                }
9986            }
9987        }
9988        // c:3561-3579 — shf->redir append: a function definition can
9989        // carry extra redirs (`f() { ... } < file`), captured as a
9990        // separate Eprog in shf->redir. Walk that Eprog with a temp
9991        // estate, extract its redirs with ecgetredirs, then merge
9992        // into the live `redir` list.
9993        // Resolve shfunc by name (hn is *mut builtin so we go through
9994        // shfunctab as in the dispatch site at c:4102).
9995        let shfn_name = args
9996            .as_ref()
9997            .and_then(|v| v.first())
9998            .cloned()
9999            .unwrap_or_default();
10000        let shf_redir_eprog: Option<crate::ported::zsh_h::Eprog> = {
10001            if let Ok(tab) = shfunctab_lock().read() {
10002                tab.get(&shfn_name).and_then(|s| s.redir.clone())
10003            } else {
10004                None
10005            }
10006        };
10007        if let Some(red_eprog) = shf_redir_eprog {
10008            // c:3566-3571 — build temp estate from shf->redir.
10009            let mut tmp_state = estate {
10010                prog: red_eprog.clone(),
10011                pc: 0,
10012                strs: red_eprog.strs.clone(),
10013                strs_offset: 0,
10014            };
10015            // c:3572 — `redir2 = ecgetredirs(&s);`
10016            let redir2 = crate::ported::parse::ecgetredirs(&mut tmp_state);
10017            // c:3573-3578 — merge into existing redir.
10018            if redir.is_none() {
10019                redir = Some(redir2); // c:3574
10020            } else if let Some(ref mut r) = redir {
10021                // c:3576-3577 — append.
10022                for n in redir2 {
10023                    r.push(n);
10024                }
10025            }
10026        }
10027    }
10028
10029    // c:3582-3591 — errflag bail-out (2).
10030    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
10031        // c:3582
10032        LASTVAL.store(1, Ordering::Relaxed); // c:3583
10033        if oautocont >= 0 {
10034            opt_state_set("autocontinue", oautocont != 0);
10035            // c:3584-3585
10036        }
10037        if forked != 0 {
10038            crate::ported::builtin::_realexit(); // c:3586-3587
10039        }
10040        if (how & Z_TIMED as i32) != 0 {
10041            crate::ported::jobs::shelltime(Some(&mut shti), Some(&mut chti), Some(&mut then_ts), 1);
10042            // c:3588-3589
10043        }
10044        return; // c:3590
10045    }
10046
10047    // c:3593-3632 — external resolution + AUTOCD.
10048    if (typ == WC_SIMPLE as i32 || typ == WC_TYPESET as i32) && nullexec == 0 {
10049        // c:3593
10050        let trycd = isset(AUTOCD)
10051            && isset(SHINSTDIN)
10052            && redir.as_ref().map(|v| v.is_empty()).unwrap_or(true)
10053            && args.as_ref().map(|v| v.len() == 1).unwrap_or(false)
10054            && !args.as_ref().unwrap()[0].is_empty(); // c:3595-3597
10055        if hn.is_none() {
10056            // c:3600
10057            let cmdarg = args.as_ref().unwrap()[0].clone();
10058            let mut dohashcmd = isset(HASHCMDS); // c:3604
10059                                                 // c:3606 — `hn = cmdnamtab->getnode(cmdnamtab, cmdarg);`
10060            let mut have_cmdnam: Option<cmdnam> = {
10061                let tab = cmdnamtab_lock().read().ok();
10062                tab.and_then(|t| {
10063                    t.iter()
10064                        .find(|(k, _)| k.as_str() == cmdarg.as_str())
10065                        .map(|(_, v)| v.clone())
10066                })
10067            };
10068            if have_cmdnam.is_some() && trycd && !isreallycom(have_cmdnam.as_ref().unwrap()) {
10069                // c:3607
10070                // c:3608-3614 — remove the cached entry; force rehash.
10071                cmdnam_unhashed(&cmdarg, Vec::new());
10072                have_cmdnam = None;
10073                if let Some(cn) = have_cmdnam.as_ref() {
10074                    if (cn.node.flags & crate::ported::zsh_h::HASHED) == 0 {
10075                        // checkpath = path; dohashcmd = 1;
10076                        dohashcmd = true;
10077                    }
10078                }
10079            }
10080            if have_cmdnam.is_none() && dohashcmd && cmdarg != ".." {
10081                // c:3616 — `if (!hn && dohashcmd && strcmp(cmdarg, "..")) `
10082                let has_slash = cmdarg.contains('/'); // c:3617-3618
10083                if !has_slash {
10084                    // c:3619 — `hn = (HashNode) hashcmd(cmdarg, checkpath);`
10085                    let path_dirs = getsparam("PATH").unwrap_or_default();
10086                    let dirs: Vec<String> = path_dirs.split(':').map(String::from).collect();
10087                    have_cmdnam = hashcmd(&cmdarg, &dirs);
10088                }
10089            }
10090            // hn stays None for external commands — the resolution
10091            // value matters only for builtin/shfunc dispatch in the
10092            // following blocks.
10093            let _ = have_cmdnam;
10094        }
10095
10096        // c:3625-3631 — AUTOCD: command not found, try directory.
10097        if hn.is_none() && trycd {
10098            let cmdarg = args.as_ref().unwrap()[0].clone();
10099            if let Some(s) = cancd(&cmdarg) {
10100                // c:3625
10101                args.as_mut().unwrap()[0] = s; // c:3626
10102                args.as_mut().unwrap().insert(0, "--".to_string()); // c:3627
10103                args.as_mut().unwrap().insert(0, "cd".to_string()); // c:3628
10104                                                                    // c:3629 — `if ((hn = builtintab->getnode(builtintab, "cd")))`
10105                let cd_entry = BUILTINS.iter().find(|b| b.node.nam.as_str() == "cd");
10106                if let Some(cd) = cd_entry {
10107                    hn = Some(cd as *const builtin as *mut builtin);
10108                    is_builtin = 1; // c:3630
10109                }
10110            }
10111        }
10112    }
10113
10114    // c:3635 — `is_cursh = (is_builtin || is_shfunc || nullexec || type >= WC_CURSH);`
10115    is_cursh =
10116        (is_builtin != 0 || is_shfunc != 0 || nullexec != 0 || typ >= WC_CURSH as i32) as i32;
10117
10118    // c:3659-3697 — fork decision.
10119    if forked == 0 {
10120        // c:3659
10121        if do_exec == 0
10122            && (((is_builtin != 0 || is_shfunc != 0) && output != 0)
10123                || (is_cursh == 0
10124                    && (last1 != 1
10125                        || crate::ported::signals::nsigtrapped.load(Ordering::Relaxed) != 0
10126                        || JOBTAB
10127                            .get()
10128                            .map(|jt| crate::ported::jobs::havefiles(&jt.lock().unwrap()))
10129                            .unwrap_or(false)
10130                        || false/* fdtable_flocks — substrate stub */)))
10131        {
10132            // c:3660-3663
10133            let mut filelist_for_fork = filelist.clone();
10134            let pid = execcmd_fork(
10135                state,
10136                how,
10137                typ,
10138                varspc,
10139                &mut filelist_for_fork,
10140                text.as_deref().unwrap_or(""),
10141                oautocont,
10142                close_if_forked,
10143            );
10144            match pid {
10145                -1 => {
10146                    // c:3666-3667 — goto fatal.
10147                    redir_err = 1;
10148                    return execcmd_exec_done_path(
10149                        redir_err,
10150                        oautocont,
10151                        how,
10152                        &mut shti,
10153                        &mut chti,
10154                        &mut then_ts,
10155                        forked,
10156                        &mut newxtrerr,
10157                        cflags,
10158                        orig_cflags,
10159                        is_cursh,
10160                        do_exec,
10161                    );
10162                }
10163                0 => {
10164                    // c:3668 — child continues.
10165                }
10166                _ => {
10167                    // c:3670-3671 — parent returns.
10168                    if oautocont >= 0 {
10169                        opt_state_set("autocontinue", oautocont != 0);
10170                    }
10171                    if (how & Z_TIMED as i32) != 0 {
10172                        crate::ported::jobs::shelltime(
10173                            Some(&mut shti),
10174                            Some(&mut chti),
10175                            Some(&mut then_ts),
10176                            1,
10177                        );
10178                    }
10179                    return;
10180                }
10181            }
10182            forked = 1; // c:3673
10183        } else if is_cursh != 0 {
10184            // c:3674
10185            // c:3678-3682 — set jobtab[thisjob] stat bits.
10186            let thisjob = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
10187            if thisjob >= 0 {
10188                if let Some(jt) = JOBTAB.get() {
10189                    let mut guard = jt.lock().unwrap();
10190                    if let Some(j) = guard.get_mut(thisjob as usize) {
10191                        j.stat |= STAT_CURSH; // c:3678
10192                                              // c:3679-3680 — `if (!jobtab[thisjob].procs)
10193                                              //                  jobtab[thisjob].stat |= STAT_NOPRINT;`
10194                                              // Suppress the "[N] done" print for jobs that
10195                                              // never forked a real process (cursh / builtin /
10196                                              // null exec).
10197                        if j.procs.is_empty() {
10198                            j.stat |= STAT_NOPRINT; // c:3680
10199                        }
10200                        if is_builtin != 0 {
10201                            j.stat |= STAT_BUILTIN; // c:3682
10202                        }
10203                    }
10204                }
10205            }
10206        } else {
10207            // c:3683-3697 — external exec (real or fake).
10208            is_exec = 1; // c:3687
10209                         // c:3695 — `if (type == WC_SUBSH) forked = 1;`
10210            if typ == WC_SUBSH as i32 {
10211                forked = 1; // c:3696
10212            }
10213        }
10214    }
10215
10216    // c:3700-3704 — `if ((esglob = !(cflags & BINF_NOGLOB)) && args && htok)`
10217    if (cflags & BINF_NOGLOB) == 0 && args.is_some() && eparams.htok != 0 {
10218        // c:3700
10219        let mut oargs: LinkList<String> = Default::default();
10220        if let Some(ref v) = args {
10221            for s in v {
10222                oargs.push_back(s.clone());
10223            }
10224        }
10225        globlist(&mut oargs, 0); // c:3702
10226        let mut out: Vec<String> = Vec::new();
10227        while let Some(s) = oargs.pop_front() {
10228            out.push(s);
10229        }
10230        args = Some(out);
10231    }
10232    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
10233        // c:3705
10234        LASTVAL.store(1, Ordering::Relaxed); // c:3706
10235        return execcmd_exec_err_path(
10236            forked,
10237            &mut save,
10238            &mut mfds,
10239            oautocont,
10240            how,
10241            &mut shti,
10242            &mut chti,
10243            &mut then_ts,
10244            &mut newxtrerr,
10245            cflags,
10246            orig_cflags,
10247            is_cursh,
10248            do_exec,
10249            redir_err,
10250        );
10251    }
10252
10253    // c:3711-3718 — XTRACE prep (newxtrerr stderr dup).
10254    // Architectural divergence: C duplicates stderr to a new FD and
10255    // marks it `FDT_XTRACE` in the fdtable so the redir loop skips it.
10256    // zshrs routes xtrace output through `eprintln!()` / `tracing`
10257    // instead of a duplicated fd, so the FDT_XTRACE bookkeeping has
10258    // no counterpart. Not a port gap — `xtrerr is FILE*` is a C-ism
10259    // intentionally replaced.
10260
10261    // c:3720-3724 — pipeline input/output to mfds.
10262    if input != 0 {
10263        addfd(forked, &mut save, &mut mfds, 0, input, 0, None); // c:3722
10264    }
10265    if output != 0 {
10266        addfd(forked, &mut save, &mut mfds, 1, output, 1, None); // c:3724
10267    }
10268
10269    // c:3726-3728 — `if (redir) spawnpipes(redir, nullexec);`
10270    if let Some(ref mut r) = redir {
10271        spawnpipes(r.as_mut_slice(), nullexec);
10272    }
10273
10274    // c:3731-3955 — io redirection loop. Faithful per-redir match.
10275    while let Some(redir_list) = redir.as_mut() {
10276        // c:3731 — `while (redir && nonempty(redir))`
10277        if redir_list.is_empty() {
10278            break;
10279        }
10280        let mut fn_ = redir_list.remove(0); // c:3732 `fn = (Redir) ugetnode(redir);`
10281                                            // c:3734-3735 DPUTS — debug assert REDIR_HEREDOC* gone.
10282        if fn_.typ == REDIR_INPIPE {
10283            // c:3736
10284            if checkclobberparam(&fn_) == 0 || fn_.fd2 == -1 {
10285                // c:3737
10286                if fn_.fd2 != -1 {
10287                    let _ = zclose(fn_.fd2); // c:3738-3739
10288                }
10289                closemnodes(&mut mfds); // c:3740
10290                fixfds(&save); // c:3741
10291                {
10292                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10293                    LASTVAL.store(1, Ordering::Relaxed);
10294                } // c:3742
10295                break;
10296            }
10297            // c:3744 — `addfd(forked, save, mfds, fn->fd1, fn->fd2, 0, fn->varid);`
10298            addfd(
10299                forked,
10300                &mut save,
10301                &mut mfds,
10302                fn_.fd1,
10303                fn_.fd2,
10304                0,
10305                fn_.varid.as_deref(),
10306            );
10307        } else if fn_.typ == REDIR_OUTPIPE {
10308            // c:3745
10309            if checkclobberparam(&fn_) == 0 || fn_.fd2 == -1 {
10310                // c:3746
10311                if fn_.fd2 != -1 {
10312                    let _ = zclose(fn_.fd2); // c:3747-3748
10313                }
10314                closemnodes(&mut mfds); // c:3749
10315                fixfds(&save); // c:3750
10316                {
10317                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10318                    LASTVAL.store(1, Ordering::Relaxed);
10319                } // c:3751
10320                break;
10321            }
10322            // c:3753
10323            addfd(
10324                forked,
10325                &mut save,
10326                &mut mfds,
10327                fn_.fd1,
10328                fn_.fd2,
10329                1,
10330                fn_.varid.as_deref(),
10331            );
10332        } else {
10333            // c:3754 — non-pipe redir branch.
10334            let mut closed: i32; // c:3755
10335                                 // c:3756-3757 — xpandredir glob/brace.
10336            if fn_.typ != REDIR_HERESTR {
10337                // Put fn_ back temporarily so xpandredir can mutate
10338                // around it; not implemented identically — xpandredir
10339                // signature in zshrs differs (takes &mut redir + ctx).
10340                // c:3756 — `if (xpandredir(fn, redir)) continue;`
10341                // Pragmatic: skip xpandredir (it handles brace/glob in
10342                // redir paths — uncommon, ports to follow-up).
10343            }
10344            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
10345                // c:3758
10346                closemnodes(&mut mfds); // c:3759
10347                fixfds(&save); // c:3760
10348                {
10349                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10350                    LASTVAL.store(1, Ordering::Relaxed);
10351                } // c:3761
10352                break;
10353            }
10354            if !isset(EXECOPT) {
10355                // c:3763 — `if (unset(EXECOPT)) continue;`
10356                continue;
10357            }
10358            let fil_local: i32;
10359            match fn_.typ {
10360                t if t == REDIR_HERESTR => {
10361                    // c:3766
10362                    if checkclobberparam(&fn_) == 0 {
10363                        fil_local = -1; // c:3768
10364                    } else {
10365                        fil_local = getherestr(&fn_); // c:3770
10366                    }
10367                    if fil_local == -1 {
10368                        // c:3771
10369                        let e = std::io::Error::last_os_error();
10370                        let raw = e.raw_os_error().unwrap_or(0);
10371                        if raw != 0 && raw != libc::EINTR {
10372                            zwarn(&format!("can't create temp file for here document: {}", e));
10373                            // c:3772-3774
10374                        }
10375                        closemnodes(&mut mfds); // c:3775
10376                        fixfds(&save); // c:3776
10377                        {
10378                            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10379                            LASTVAL.store(1, Ordering::Relaxed);
10380                        } // c:3777
10381                        break;
10382                    }
10383                    // c:3779
10384                    addfd(
10385                        forked,
10386                        &mut save,
10387                        &mut mfds,
10388                        fn_.fd1,
10389                        fil_local,
10390                        0,
10391                        fn_.varid.as_deref(),
10392                    );
10393                }
10394                t if t == REDIR_READ || t == REDIR_READWRITE => {
10395                    // c:3781-3782
10396                    if checkclobberparam(&fn_) == 0 {
10397                        fil_local = -1; // c:3784
10398                    } else {
10399                        let name = fn_.name.clone().unwrap_or_default();
10400                        let unmeta_name = unmeta(&name);
10401                        let cstr = match std::ffi::CString::new(unmeta_name.as_str()) {
10402                            Ok(c) => c,
10403                            Err(_) => {
10404                                closemnodes(&mut mfds);
10405                                fixfds(&save);
10406                                {
10407                                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10408                                    LASTVAL.store(1, Ordering::Relaxed);
10409                                }
10410                                break;
10411                            }
10412                        };
10413                        if fn_.typ == REDIR_READ {
10414                            // c:3786
10415                            fil_local = unsafe {
10416                                libc::open(cstr.as_ptr(), libc::O_RDONLY | libc::O_NOCTTY)
10417                            };
10418                        } else {
10419                            // c:3788-3789
10420                            fil_local = unsafe {
10421                                libc::open(
10422                                    cstr.as_ptr(),
10423                                    libc::O_RDWR | libc::O_CREAT | libc::O_NOCTTY,
10424                                    0o666,
10425                                )
10426                            };
10427                        }
10428                    }
10429                    if fil_local == -1 {
10430                        // c:3790
10431                        closemnodes(&mut mfds); // c:3791
10432                        fixfds(&save); // c:3792
10433                        let e = std::io::Error::last_os_error();
10434                        if e.raw_os_error().unwrap_or(0) != libc::EINTR {
10435                            zwarn(&format!("{}: {}", e, fn_.name.as_deref().unwrap_or("")));
10436                            // c:3793-3794
10437                        }
10438                        {
10439                            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10440                            LASTVAL.store(1, Ordering::Relaxed);
10441                        } // c:3795
10442                        break;
10443                    }
10444                    // c:3797
10445                    addfd(
10446                        forked,
10447                        &mut save,
10448                        &mut mfds,
10449                        fn_.fd1,
10450                        fil_local,
10451                        0,
10452                        fn_.varid.as_deref(),
10453                    );
10454                    // c:3800-3802 — `if (nullexec == 1 && fn->fd1 == 0 && ...) init_io(NULL);`
10455                    if nullexec == 1
10456                        && fn_.fd1 == 0
10457                        && fn_.varid.is_none()
10458                        && isset(SHINSTDIN)
10459                        && isset(INTERACTIVE)
10460                    {
10461                        // c:3801 — `!zleactive` check ommitted (zleactive
10462                        // accessor lives in zle module; fusevm bypasses ZLE).
10463                        crate::ported::init::init_io(None); // c:3802
10464                    }
10465                }
10466                t if t == REDIR_CLOSE => {
10467                    // c:3804
10468                    // c:3805 — `if (fn->varid) { parse fd from variable }`
10469                    let mut fd1_local = fn_.fd1;
10470                    if let Some(varname) = fn_.varid.as_deref() {
10471                        // c:3806-3849 — `{var}>&-`/`{var}<&-` REDIR_CLOSE
10472                        // with varid. The C path resolves the named param
10473                        // to its integer-string value, parses as base-10
10474                        // (or base#NN), and rejects readonly / non-numeric
10475                        // / shell-owned-fd values.
10476                        //
10477                        //   bad=1  → "parameter %s does not contain a file descriptor"
10478                        //   bad=2  → "can't close file descriptor from readonly parameter %s"
10479                        //   bad=3  → "file descriptor %d used by shell, not closed"
10480                        //
10481                        // Substrate now available: getsparam for value,
10482                        // paramtab read for PM_READONLY, MAX_ZSH_FD +
10483                        // fdtable_get for shell-owned guard.
10484                        let mut bad: u8 = 0;
10485                        let value_opt = getsparam(varname);
10486                        let is_ro = paramtab()
10487                            .read()
10488                            .ok()
10489                            .and_then(|t| {
10490                                t.get(varname)
10491                                    .map(|p| (p.node.flags as u32 & PM_READONLY) != 0)
10492                            })
10493                            .unwrap_or(false);
10494                        if value_opt.is_none() {
10495                            bad = 1; // c:3811 getvalue failed
10496                        } else if is_ro {
10497                            bad = 2; // c:3813 PM_READONLY
10498                        } else {
10499                            let s = value_opt.as_deref().unwrap_or("");
10500                            match s.trim().parse::<i32>() {
10501                                Ok(n) => {
10502                                    fd1_local = n;
10503                                    fn_.fd1 = n;
10504                                    let max_fd = MAX_ZSH_FD.load(Ordering::Relaxed);
10505                                    if n >= 10
10506                                        && n <= max_fd
10507                                        && (fdtable_get(n) & FDT_TYPE_MASK) == FDT_INTERNAL
10508                                    {
10509                                        // c:3835 shell-owned-fd reject
10510                                        bad = 3;
10511                                    }
10512                                }
10513                                Err(_) => {
10514                                    bad = 1; // c:3823 strtol failure
10515                                }
10516                            }
10517                        }
10518                        if bad != 0 {
10519                            // c:3840-3849
10520                            match bad {
10521                                3 => zwarn(&format!(
10522                                    "file descriptor {} used by shell, not closed",
10523                                    fn_.fd1
10524                                )),
10525                                2 => zwarn(&format!(
10526                                    "can't close file descriptor from readonly parameter {}",
10527                                    varname
10528                                )),
10529                                _ => zwarn(&format!(
10530                                    "parameter {} does not contain a file descriptor",
10531                                    varname
10532                                )),
10533                            }
10534                            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10535                            LASTVAL.store(1, Ordering::Relaxed);
10536                            break;
10537                        }
10538                    }
10539                    // c:3852-3865 — `closed`: optional movefd save.
10540                    closed = 0;
10541                    if forked == 0 && fd1_local < 10 && save[fd1_local as usize] == -2 {
10542                        // c:3856
10543                        let mv = movefd(fd1_local); // c:3857
10544                        save[fd1_local as usize] = mv;
10545                        if mv >= 0 {
10546                            closed = 1; // c:3862-3863
10547                        }
10548                    }
10549                    if fd1_local < 10 {
10550                        // c:3866
10551                        closemn(&mut mfds, fd1_local, REDIR_CLOSE);
10552                        // c:3867
10553                    }
10554                    // c:3873-3876
10555                    let _ = &mut fd1_local;
10556                    if closed == 0 && zclose(fn_.fd1) < 0 && fn_.varid.is_some() {
10557                        zwarn(&format!(
10558                            "failed to close file descriptor {}: {}",
10559                            fn_.fd1,
10560                            std::io::Error::last_os_error()
10561                        )); // c:3873-3875
10562                    }
10563                }
10564                t if t == REDIR_MERGEIN || t == REDIR_MERGEOUT => {
10565                    // c:3878-3879
10566                    if fn_.fd2 < 10 {
10567                        closemn(&mut mfds, fn_.fd2, fn_.typ); // c:3881
10568                    }
10569                    if checkclobberparam(&fn_) == 0 {
10570                        fil_local = -1; // c:3883
10571                    } else if fn_.fd2 > 9 {
10572                        // c:3884-3897 — fd table check.
10573                        let max_fd = MAX_ZSH_FD.load(Ordering::Relaxed);
10574                        let cin = crate::ported::modules::clone::coprocin.load(Ordering::Relaxed);
10575                        let cout = crate::ported::modules::clone::coprocout.load(Ordering::Relaxed);
10576                        let in_table = if fn_.fd2 <= max_fd {
10577                            let kind = fdtable_get(fn_.fd2) & FDT_TYPE_MASK;
10578                            kind != FDT_UNUSED && kind != FDT_EXTERNAL
10579                        } else {
10580                            false
10581                        };
10582                        if in_table || fn_.fd2 == cin || fn_.fd2 == cout {
10583                            fil_local = -1; // c:3896
10584                                            // Per-platform errno setter (c:3897 `errno = EBADF;`).
10585                            #[cfg(target_os = "macos")]
10586                            unsafe {
10587                                *libc::__error() = libc::EBADF;
10588                            }
10589                            #[cfg(target_os = "linux")]
10590                            unsafe {
10591                                *libc::__errno_location() = libc::EBADF;
10592                            }
10593                        } else {
10594                            let fd = if fn_.fd2 == -2 {
10595                                // c:3900-3901
10596                                if fn_.typ == REDIR_MERGEOUT {
10597                                    crate::ported::modules::clone::coprocout.load(Ordering::Relaxed)
10598                                } else {
10599                                    crate::ported::modules::clone::coprocin.load(Ordering::Relaxed)
10600                                }
10601                            } else {
10602                                fn_.fd2
10603                            };
10604                            // c:3902 — `fil = movefd(dup(fd));`
10605                            let dup_fd = unsafe { libc::dup(fd) };
10606                            fil_local = movefd(dup_fd);
10607                        }
10608                    } else {
10609                        let fd = if fn_.fd2 == -2 {
10610                            if fn_.typ == REDIR_MERGEOUT {
10611                                crate::ported::modules::clone::coprocout.load(Ordering::Relaxed)
10612                            } else {
10613                                crate::ported::modules::clone::coprocin.load(Ordering::Relaxed)
10614                            }
10615                        } else {
10616                            fn_.fd2
10617                        };
10618                        let dup_fd = unsafe { libc::dup(fd) };
10619                        fil_local = movefd(dup_fd);
10620                    }
10621                    if fil_local == -1 {
10622                        // c:3904
10623                        closemnodes(&mut mfds); // c:3907
10624                        fixfds(&save); // c:3908
10625                        if std::io::Error::last_os_error().raw_os_error().unwrap_or(0) != 0 {
10626                            let desc = if fn_.fd2 == -2 {
10627                                "coprocess".to_string()
10628                            } else {
10629                                format!("{}", fn_.fd2)
10630                            };
10631                            zwarn(&format!("{}: {}", desc, std::io::Error::last_os_error()));
10632                            // c:3911-3913
10633                        }
10634                        {
10635                            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10636                            LASTVAL.store(1, Ordering::Relaxed);
10637                        } // c:3914
10638                        break;
10639                    }
10640                    // c:3916-3917
10641                    let merge_is_out = if fn_.typ == REDIR_MERGEOUT { 1 } else { 0 };
10642                    addfd(
10643                        forked,
10644                        &mut save,
10645                        &mut mfds,
10646                        fn_.fd1,
10647                        fil_local,
10648                        merge_is_out,
10649                        fn_.varid.as_deref(),
10650                    );
10651                }
10652                _ => {
10653                    // c:3919 default — write/append/error_redir.
10654                    let mut dfil: i32;
10655                    if checkclobberparam(&fn_) == 0 {
10656                        fil_local = -1; // c:3921
10657                    } else if IS_APPEND_REDIR(fn_.typ) {
10658                        // c:3922
10659                        let name = fn_.name.clone().unwrap_or_default();
10660                        let unmeta_name = unmeta(&name);
10661                        let cstr = match std::ffi::CString::new(unmeta_name.as_str()) {
10662                            Ok(c) => c,
10663                            Err(_) => {
10664                                closemnodes(&mut mfds);
10665                                fixfds(&save);
10666                                {
10667                                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10668                                    LASTVAL.store(1, Ordering::Relaxed);
10669                                }
10670                                break;
10671                            }
10672                        };
10673                        // c:3924-3927
10674                        let mode = if !isset(CLOBBER)
10675                            && !isset(crate::ported::zsh_h::APPENDCREATE)
10676                            && !IS_CLOBBER_REDIR(fn_.typ)
10677                        {
10678                            libc::O_WRONLY | libc::O_APPEND | libc::O_NOCTTY
10679                        } else {
10680                            libc::O_WRONLY | libc::O_APPEND | libc::O_CREAT | libc::O_NOCTTY
10681                        };
10682                        fil_local = unsafe { libc::open(cstr.as_ptr(), mode, 0o666) };
10683                    } else {
10684                        // c:3929
10685                        fil_local = clobber_open(&fn_);
10686                    }
10687                    // c:3930-3933 — error_redir dup.
10688                    if fil_local != -1 && IS_ERROR_REDIR(fn_.typ) {
10689                        let dup_fd = unsafe { libc::dup(fil_local) };
10690                        dfil = movefd(dup_fd); // c:3931
10691                    } else {
10692                        dfil = 0; // c:3933
10693                    }
10694                    if fil_local == -1 || dfil == -1 {
10695                        // c:3934
10696                        if fil_local != -1 {
10697                            unsafe { libc::close(fil_local) }; // c:3935-3936
10698                        }
10699                        closemnodes(&mut mfds); // c:3937
10700                        fixfds(&save); // c:3938
10701                        let e = std::io::Error::last_os_error();
10702                        let raw = e.raw_os_error().unwrap_or(0);
10703                        if raw != 0 && raw != libc::EINTR {
10704                            zwarn(&format!("{}: {}", e, fn_.name.as_deref().unwrap_or("")));
10705                            // c:3939-3940
10706                        }
10707                        {
10708                            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10709                            LASTVAL.store(1, Ordering::Relaxed);
10710                        } // c:3941
10711                        break;
10712                    }
10713                    // c:3943
10714                    addfd(
10715                        forked,
10716                        &mut save,
10717                        &mut mfds,
10718                        fn_.fd1,
10719                        fil_local,
10720                        1,
10721                        fn_.varid.as_deref(),
10722                    );
10723                    if IS_ERROR_REDIR(fn_.typ) {
10724                        // c:3944-3945
10725                        addfd(forked, &mut save, &mut mfds, 2, dfil, 1, None);
10726                    }
10727                    let _ = &mut dfil;
10728                }
10729            }
10730            // c:3948-3952 — addfd errflag check.
10731            if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
10732                // c:3949
10733                closemnodes(&mut mfds); // c:3950
10734                fixfds(&save); // c:3951
10735                {
10736                    errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed);
10737                    LASTVAL.store(1, Ordering::Relaxed);
10738                } // c:3952
10739                break;
10740            }
10741        }
10742    }
10743
10744    // c:3957-3961 — close multios with ct >= 2.
10745    i = 0;
10746    while i < 10 {
10747        // c:3959
10748        if let Some(m) = mfds.get(i as usize).and_then(|o| o.as_ref()) {
10749            if m.ct >= 2 {
10750                closemn(&mut mfds, i, REDIR_CLOSE); // c:3960
10751            }
10752        }
10753        i += 1;
10754    }
10755
10756    // c:3963-3995 — nullexec branch.
10757    if nullexec != 0 {
10758        // c:3963
10759        if let Some(vspc) = varspc {
10760            // c:3969
10761            let mut restorelist: Vec<crate::ported::zsh_h::param> = Vec::new();
10762            let mut removelist: Vec<String> = Vec::new();
10763            if !isset(POSIXBUILTINS) && nullexec != 2 {
10764                // c:3971-3972
10765                save_params(state, vspc, &mut restorelist, &mut removelist);
10766            }
10767            addvars(state, vspc, 0); // c:3973
10768            if !restorelist.is_empty() {
10769                // c:3974
10770                restore_params(restorelist, removelist); // c:3975
10771            }
10772        }
10773        let ef = errflag.load(Ordering::Relaxed);
10774        LASTVAL.store(
10775            if ef != 0 {
10776                ef
10777            } else {
10778                cmdoutval.load(Ordering::Relaxed)
10779            },
10780            Ordering::Relaxed,
10781        ); // c:3977
10782        if nullexec == 1 {
10783            // c:3978
10784            // c:3983-3985 — close save[i].
10785            i = 0;
10786            while i < 10 {
10787                if save[i as usize] != -2 {
10788                    let _ = zclose(save[i as usize]); // c:3985
10789                }
10790                i += 1;
10791            }
10792            // c:3988-3989 — `jobtab[thisjob].stat |= STAT_DONE; goto done;`
10793            let thisjob = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
10794            if thisjob >= 0 {
10795                if let Some(jt) = JOBTAB.get() {
10796                    let mut guard = jt.lock().unwrap();
10797                    if let Some(j) = guard.get_mut(thisjob as usize) {
10798                        j.stat |= STAT_DONE; // c:3989
10799                    }
10800                }
10801            }
10802            return execcmd_exec_done_path(
10803                redir_err,
10804                oautocont,
10805                how,
10806                &mut shti,
10807                &mut chti,
10808                &mut then_ts,
10809                forked,
10810                &mut newxtrerr,
10811                cflags,
10812                orig_cflags,
10813                is_cursh,
10814                do_exec,
10815            );
10816        }
10817        if isset(XTRACE) {
10818            // c:3992-3994
10819            eprintln!();
10820        }
10821    } else if isset(EXECOPT) && (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
10822        // c:3996 — main dispatch branch.
10823        // c:3997 — `int q = queue_signal_level();`
10824        let _q = 0;
10825        // c:4003-4012 — entersubsh for is_exec.
10826        if is_exec != 0 {
10827            // c:4003
10828            let mut flags: i32 = if (how & Z_ASYNC as i32) != 0 {
10829                esub::ASYNC
10830            } else {
10831                0
10832            } | esub::PGRP
10833                | esub::FAKE; // c:4004-4005
10834            if typ != WC_SUBSH as i32 {
10835                flags |= esub::KEEPTRAP; // c:4007
10836            }
10837            if (do_exec != 0 || (typ >= WC_CURSH as i32 && last1 == 1)) && forked == 0 {
10838                // c:4008-4009
10839                flags |= esub::REVERTPGRP; // c:4010
10840            }
10841            entersubsh(flags, None); // c:4011
10842        }
10843
10844        if typ == WC_FUNCDEF as i32 {
10845            // c:4013
10846            // c:4014-4036 — `redir_prog` setup from wordcode if no
10847            // redirs+WC_REDIR follows. Wire only when fusevm WC_REDIR
10848            // peek is in scope; for the tree-walker entry point we
10849            // approximate by passing None.
10850            let redir_prog: Option<crate::ported::zsh_h::Eprog> = None;
10851            // c:4039 — `lastval = execfuncdef(state, redir_prog);`
10852            let lv = execfuncdef(state, redir_prog);
10853            LASTVAL.store(lv, Ordering::Relaxed);
10854        } else if typ >= WC_CURSH as i32 {
10855            // c:4042
10856            if last1 == 1 {
10857                do_exec = 1; // c:4044
10858            }
10859            if typ == WC_AUTOFN as i32 {
10860                // c:4046
10861                let lv = execautofn_basic(state, do_exec); // c:4051
10862                LASTVAL.store(lv, Ordering::Relaxed);
10863            } else {
10864                // c:4053 — `lastval = (execfuncs[type - WC_CURSH])(state, do_exec);`
10865                // dispatch_execfuncs ports the C `execfuncs[]` table
10866                // (Src/exec.c:170-180) by typ → exec{cursh,for,select,...}
10867                // direct call. See dispatch_execfuncs at end of file.
10868                let lv = dispatch_execfuncs(state, typ, do_exec);
10869                LASTVAL.store(lv, Ordering::Relaxed);
10870            }
10871        } else if is_builtin != 0 || is_shfunc != 0 {
10872            // c:4055
10873            let mut restorelist: Vec<crate::ported::zsh_h::param> = Vec::new();
10874            let mut removelist: Vec<String> = Vec::new();
10875            let mut do_save: i32 = 0; // c:4057
10876
10877            if forked == 0 {
10878                // c:4060
10879                if isset(POSIXBUILTINS) {
10880                    // c:4061
10881                    if is_shfunc != 0
10882                        || (hn.map(|p| unsafe { (*p).node.flags as u32 }).unwrap_or(0)
10883                            & (BINF_PSPECIAL | BINF_ASSIGN_FLAG))
10884                            != 0
10885                    {
10886                        // c:4067
10887                        do_save = if (orig_cflags & BINF_COMMAND) != 0 {
10888                            1
10889                        } else {
10890                            0
10891                        };
10892                    } else {
10893                        do_save = 1; // c:4070
10894                    }
10895                } else {
10896                    // c:4071
10897                    if (cflags & (BINF_COMMAND | BINF_ASSIGN_FLAG)) != 0 || magic_assign == 0 {
10898                        // c:4076
10899                        do_save = 1; // c:4077
10900                    }
10901                }
10902                if do_save != 0 {
10903                    if let Some(vspc) = varspc {
10904                        // c:4079
10905                        save_params(state, vspc, &mut restorelist, &mut removelist);
10906                    }
10907                }
10908            }
10909            if varspc.is_some() {
10910                // c:4082
10911                let mut addflags: i32 = 0; // c:4086
10912                if is_shfunc != 0 {
10913                    addflags |= ADDVAR_EXPORT; // c:4088
10914                }
10915                if !restorelist.is_empty() {
10916                    addflags |= ADDVAR_RESTORE; // c:4090
10917                }
10918                addvars(state, varspc.unwrap_or(0), addflags); // c:4092
10919                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
10920                    // c:4093
10921                    if !restorelist.is_empty() {
10922                        restore_params(restorelist, removelist); // c:4094-4095
10923                    }
10924                    LASTVAL.store(1, Ordering::Relaxed); // c:4096
10925                    fixfds(&save); // c:4097
10926                    return execcmd_exec_done_path(
10927                        redir_err,
10928                        oautocont,
10929                        how,
10930                        &mut shti,
10931                        &mut chti,
10932                        &mut then_ts,
10933                        forked,
10934                        &mut newxtrerr,
10935                        cflags,
10936                        orig_cflags,
10937                        is_cursh,
10938                        do_exec,
10939                    );
10940                }
10941            }
10942
10943            if is_shfunc != 0 {
10944                // c:4102-4105
10945                let mut a_vec: Vec<String> = args.clone().unwrap_or_default();
10946                // c:4104 — `execshfunc((Shfunc) hn, args);` C casts
10947                // HashNode hn to Shfunc; zshrs's hn is *mut builtin so
10948                // we re-resolve the shfunc by name from shfunctab and
10949                // dispatch through the top-level execshfunc port at
10950                // exec.rs:4978 (which routes to runshfunc).
10951                let name = args
10952                    .as_ref()
10953                    .and_then(|v| v.first())
10954                    .cloned()
10955                    .unwrap_or_default();
10956                let mut shf_clone: Option<shfunc> = if let Ok(tab) = shfunctab_lock().read() {
10957                    tab.get(&name).cloned()
10958                } else {
10959                    None
10960                };
10961                if let Some(ref mut shf) = shf_clone {
10962                    execshfunc(shf, &mut a_vec);
10963                }
10964                // c:4105 — `pipecleanfilelist(filelist, 0);` — clean
10965                // out the proc_subst entries from the current job's
10966                // filelist after the shfunc body ran. Route through
10967                // `JOBTAB[thisjob]`.
10968                if let Some(jt) = JOBTAB.get() {
10969                    let mut guard = jt.lock().unwrap();
10970                    let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
10971                    if tj >= 0 {
10972                        if let Some(j) = guard.get_mut(tj as usize) {
10973                            crate::ported::jobs::pipecleanfilelist(j, false);
10974                        }
10975                    }
10976                }
10977            } else {
10978                // c:4107 — builtin path.
10979                let mut assigns: Vec<crate::ported::zsh_h::asgment> = Vec::new(); // c:4108
10980                let postassigns = eparams.postassigns; // c:4109
10981                if forked != 0 {
10982                    closem(FDT_INTERNAL, 0); // c:4111
10983                }
10984                if postassigns != 0 {
10985                    // c:4112-4230 — typeset post-assignment processing.
10986                    use crate::ported::zsh_h::{
10987                        ASG_ARRAY, ASG_KEY_VALUE, EC_DUPTOK as ECDUPTOK_LOCAL, PREFORK_ASSIGN,
10988                        PREFORK_KEY_VALUE, PREFORK_SINGLE, PREFORK_TYPESET, WC_ASSIGN_INC,
10989                        WC_ASSIGN_NUM, WC_ASSIGN_SCALAR, WC_ASSIGN_TYPE, WC_ASSIGN_TYPE2,
10990                    };
10991                    let opc = state.pc; // c:4113
10992                    state.pc = eparams.assignspc.unwrap_or(state.pc); // c:4114
10993                                                                      // c:4115 — `assigns = newlinklist();` — already declared above.
10994                    let mut pa_remaining = postassigns;
10995                    while pa_remaining > 0 {
10996                        // c:4116 — `while (postassigns--)`
10997                        pa_remaining -= 1;
10998                        let mut pa_htok: i32 = 0; // c:4117
10999                        if state.pc >= state.prog.prog.len() {
11000                            break;
11001                        }
11002                        let ac = state.prog.prog[state.pc]; // c:4118
11003                        state.pc += 1;
11004                        let mut name = ecgetstr(state, ECDUPTOK_LOCAL, Some(&mut pa_htok)); // c:4119
11005                                                                                            // c:4123-4124 DPUTS — debug assertion skipped.
11006                        if pa_htok != 0 {
11007                            // c:4126 — `init_list1(svl, name);`
11008                            let mut svl: LinkList<String> = Default::default();
11009                            svl.push_back(name.clone());
11010                            // c:4127-4166 — INC-scalar special case (typeset $ass form).
11011                            if WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR
11012                                && WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC
11013                            {
11014                                // c:4141 — `(void)ecgetstr(...)` — dummy.
11015                                let mut dummy_htok: i32 = 0;
11016                                let _ = ecgetstr(state, ECDUPTOK_LOCAL, Some(&mut dummy_htok));
11017                                let mut rf = 0i32;
11018                                prefork(&mut svl, PREFORK_TYPESET, &mut rf); // c:4142
11019                                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11020                                    // c:4143
11021                                    state.pc = opc; // c:4144
11022                                    break;
11023                                }
11024                                let mut rf2 = 0i32;
11025                                globlist(&mut svl, rf2); // c:4147
11026                                let _ = &mut rf2;
11027                                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11028                                    // c:4148
11029                                    state.pc = opc; // c:4149
11030                                    break;
11031                                }
11032                                // c:4152-4165 — drain svl into assigns.
11033                                while let Some(data) = svl.pop_front() {
11034                                    let (asg_name, asg_val): (String, Option<String>) =
11035                                        if let Some(eq_pos) = data.find('=') {
11036                                            // c:4156-4159
11037                                            (
11038                                                data[..eq_pos].to_string(),
11039                                                Some(data[eq_pos + 1..].to_string()),
11040                                            )
11041                                        } else {
11042                                            // c:4161-4162
11043                                            (data, None)
11044                                        };
11045                                    assigns.push(crate::ported::zsh_h::asgment {
11046                                        node: crate::ported::zsh_h::linknode {
11047                                            next: None,
11048                                            prev: None,
11049                                            dat: 0,
11050                                        },
11051                                        name: asg_name,
11052                                        flags: 0,
11053                                        scalar: asg_val,
11054                                        array: None,
11055                                    });
11056                                }
11057                                continue; // c:4166
11058                            }
11059                            // c:4168 — `prefork(&svl, PREFORK_SINGLE, NULL);`
11060                            let mut rf = 0i32;
11061                            prefork(&mut svl, PREFORK_SINGLE, &mut rf);
11062                            // c:4169-4170 — `name = empty(svl) ? "" : firstnode_data;`
11063                            name = if svl.is_empty() {
11064                                String::new()
11065                            } else {
11066                                svl.pop_front().unwrap_or_default()
11067                            };
11068                        }
11069                        // c:4172 — `untokenize(name);`
11070                        // (untokenize is destructive on bytes; Rust untokenize
11071                        // returns a new String — call and rebind.)
11072                        name = untokenize(&name);
11073                        let mut asg = crate::ported::zsh_h::asgment {
11074                            node: crate::ported::zsh_h::linknode {
11075                                next: None,
11076                                prev: None,
11077                                dat: 0,
11078                            },
11079                            name,
11080                            flags: 0,
11081                            scalar: None,
11082                            array: None,
11083                        };
11084                        if WC_ASSIGN_TYPE(ac) == WC_ASSIGN_SCALAR {
11085                            // c:4175
11086                            let mut val_htok: i32 = 0;
11087                            let mut val = ecgetstr(state, ECDUPTOK_LOCAL, Some(&mut val_htok)); // c:4176
11088                            asg.flags = 0; // c:4177
11089                            if WC_ASSIGN_TYPE2(ac) == WC_ASSIGN_INC {
11090                                // c:4178-4180 — fake assignment, no value.
11091                                asg.scalar = None;
11092                            } else {
11093                                if val_htok != 0 {
11094                                    // c:4183
11095                                    let mut svl: LinkList<String> = Default::default();
11096                                    svl.push_back(val.clone());
11097                                    let mut rf = 0i32;
11098                                    prefork(&mut svl, PREFORK_SINGLE | PREFORK_ASSIGN, &mut rf); // c:4184-4186
11099                                    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11100                                        // c:4187
11101                                        state.pc = opc; // c:4188
11102                                        break;
11103                                    }
11104                                    // c:4195-4196 — `val = empty(svl) ? "" : firstdata;`
11105                                    val = if svl.is_empty() {
11106                                        String::new()
11107                                    } else {
11108                                        svl.pop_front().unwrap_or_default()
11109                                    };
11110                                }
11111                                // c:4198 — `untokenize(val);`
11112                                asg.scalar = Some(untokenize(&val));
11113                            }
11114                        } else {
11115                            // c:4202 — array assignment.
11116                            asg.flags = ASG_ARRAY; // c:4202
11117                            let mut arr_htok: i32 = 0;
11118                            let arr_words = ecgetlist(
11119                                state,
11120                                WC_ASSIGN_NUM(ac) as usize,
11121                                ECDUPTOK_LOCAL,
11122                                Some(&mut arr_htok),
11123                            ); // c:4204
11124                            let mut arr_list: LinkList<String> = Default::default();
11125                            for s in arr_words {
11126                                arr_list.push_back(s);
11127                            }
11128                            if !arr_list.is_empty()
11129                                && (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0
11130                            {
11131                                // c:4209 — `int prefork_ret = 0;`
11132                                let mut prefork_ret = 0i32;
11133                                prefork(&mut arr_list, PREFORK_ASSIGN, &mut prefork_ret); // c:4210-4211
11134                                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11135                                    // c:4212
11136                                    state.pc = opc; // c:4213
11137                                    break;
11138                                }
11139                                if (prefork_ret & PREFORK_KEY_VALUE) != 0 {
11140                                    // c:4216
11141                                    asg.flags |= ASG_KEY_VALUE; // c:4217
11142                                }
11143                                globlist(&mut arr_list, prefork_ret); // c:4218
11144                                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11145                                    // c:4220
11146                                    state.pc = opc; // c:4221
11147                                    break;
11148                                }
11149                            }
11150                            asg.array = Some(arr_list);
11151                        }
11152                        // c:4227 — `uaddlinknode(assigns, &asg->node);`
11153                        assigns.push(asg);
11154                    }
11155                    state.pc = opc; // c:4229
11156                }
11157                if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) == 0 {
11158                    // c:4232
11159                    // c:Src/builtin.c:262 — `name = (char *) ugetnode(args);`
11160                    // C's execbuiltin consumes args[0] (the command name)
11161                    // at entry. zshrs's execbuiltin reads the name from
11162                    // `bn->node.nam` instead, so we strip args[0] here
11163                    // before the call to match C's post-ugetnode argv
11164                    // shape. Without this, e.g. `cmd=pwd; $cmd` reached
11165                    // execbuiltin with args=["pwd"] and pwd's
11166                    // maxargs=0 check rejected the empty call as
11167                    // "too many arguments".
11168                    let mut a_vec: Vec<String> = args.clone().unwrap_or_default();
11169                    if !a_vec.is_empty() {
11170                        a_vec.remove(0);
11171                    }
11172                    let ret = crate::ported::builtin::execbuiltin(
11173                        a_vec,
11174                        assigns,
11175                        hn.unwrap_or(std::ptr::null_mut()),
11176                    ); // c:4233
11177                    if (errflag.load(Ordering::Relaxed) & ERRFLAG_INT) == 0 {
11178                        // c:4238
11179                        LASTVAL.store(ret, Ordering::Relaxed); // c:4239
11180                    }
11181                }
11182                if (do_save & BINF_COMMAND as i32) != 0 {
11183                    // c:4241
11184                    errflag.fetch_and(!ERRFLAG_ERROR, Ordering::Relaxed); // c:4242
11185                }
11186                // c:4244 fflush(stdout) — Rust stdio auto-flushes.
11187                // c:4245-4251 — write-error check on save[1].
11188            }
11189            if isset(PRINTEXITVALUE)
11190                && isset(SHINSTDIN)
11191                && LASTVAL.load(Ordering::Relaxed) != 0
11192                && subsh.load(Ordering::Relaxed) == 0
11193            {
11194                // c:4253-4255
11195                eprintln!("zsh: exit {}", LASTVAL.load(Ordering::Relaxed)); // c:4258
11196            }
11197
11198            if do_exec != 0 {
11199                // c:4263
11200                if subsh.load(Ordering::Relaxed) != 0 {
11201                    crate::ported::builtin::_realexit(); // c:4264-4265
11202                }
11203                if isset(RCS)
11204                    && crate::ported::zsh_h::interact()
11205                    && nohistsave.load(Ordering::Relaxed) == 0
11206                {
11207                    // c:4269
11208                    crate::ported::hist::savehistfile(None, HFILE_USE_OPTIONS as i32);
11209                    // c:4270
11210                }
11211                crate::ported::builtin::realexit(); // c:4271
11212            }
11213            if !restorelist.is_empty() {
11214                // c:4273
11215                restore_params(restorelist, removelist); // c:4274
11216            }
11217        } else {
11218            // c:4276 — external command execute.
11219            if subsh.load(Ordering::Relaxed) == 0 {
11220                // c:4277
11221                if forked == 0 {
11222                    // c:4280 — `setiparam("SHLVL", --shlvl);`
11223                    let cur = getsparam("SHLVL")
11224                        .and_then(|s| s.parse::<i64>().ok())
11225                        .unwrap_or(1);
11226                    setiparam("SHLVL", cur - 1); // c:4281
11227                }
11228                if do_exec != 0
11229                    && isset(RCS)
11230                    && crate::ported::zsh_h::interact()
11231                    && nohistsave.load(Ordering::Relaxed) == 0
11232                {
11233                    // c:4285
11234                    crate::ported::hist::savehistfile(None, HFILE_USE_OPTIONS as i32);
11235                    // c:4286
11236                }
11237            }
11238            if typ == WC_SIMPLE as i32 || typ == WC_TYPESET as i32 {
11239                // c:4288
11240                if varspc.is_some() {
11241                    // c:4289
11242                    let mut addflags: i32 = ADDVAR_EXPORT; // c:4290
11243                    if forked != 0 {
11244                        addflags |= ADDVAR_RESTORE; // c:4292
11245                    }
11246                    addvars(state, varspc.unwrap_or(0), addflags); // c:4293
11247                    if (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11248                        // c:4294
11249                        std::process::exit(1); // c:4295
11250                    }
11251                }
11252                closem(FDT_INTERNAL, 0); // c:4297
11253                                         // c:4298-4305 — close coprocin/coprocout.
11254                let cpi = crate::ported::modules::clone::coprocin.load(Ordering::Relaxed);
11255                if cpi != -1 {
11256                    let _ = zclose(cpi); // c:4299
11257                    crate::ported::modules::clone::coprocin.store(-1, Ordering::Relaxed);
11258                    // c:4300
11259                }
11260                let cpo = crate::ported::modules::clone::coprocout.load(Ordering::Relaxed);
11261                if cpo != -1 {
11262                    let _ = zclose(cpo); // c:4303
11263                    crate::ported::modules::clone::coprocout.store(-1, Ordering::Relaxed);
11264                    // c:4304
11265                }
11266                if forked == 0 {
11267                    // c:4307
11268                    setlimits(""); // c:4308
11269                }
11270                if (how & Z_ASYNC as i32) != 0 {
11271                    // c:4310 — `zsfree(STTYval); STTYval = 0;`
11272                    let mut guard = STTYval.lock().unwrap();
11273                    *guard = None; // c:4311-4312
11274                }
11275                // c:4314 — `execute(args, cflags, use_defpath);`
11276                let mut a_vec: Vec<String> = args.clone().unwrap_or_default();
11277                execute(&mut a_vec, cflags, use_defpath); // c:4314
11278            } else {
11279                // c:4315 — `( ... )` — WC_SUBSH.
11280                list_pipe.store(0, Ordering::Relaxed); // c:4318
11281                                                       // c:4319 — `pipecleanfilelist(filelist, 0);` — clean
11282                                                       // proc-subst entries from the current job's filelist
11283                                                       // before recursing into the subshell body.
11284                if let Some(jt) = JOBTAB.get() {
11285                    let mut guard = jt.lock().unwrap();
11286                    let tj = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
11287                    if tj >= 0 {
11288                        if let Some(j) = guard.get_mut(tj as usize) {
11289                            crate::ported::jobs::pipecleanfilelist(j, false);
11290                        }
11291                    }
11292                }
11293                state.pc += 1; // c:4324 — `state->pc++;`
11294                let _ = execlist(state, 0, 1); // c:4325
11295            }
11296        }
11297    }
11298
11299    // c:4330-4404 — err: + done: + fatal:.
11300    return execcmd_exec_done_path(
11301        redir_err,
11302        oautocont,
11303        how,
11304        &mut shti,
11305        &mut chti,
11306        &mut then_ts,
11307        forked,
11308        &mut newxtrerr,
11309        cflags,
11310        orig_cflags,
11311        is_cursh,
11312        do_exec,
11313    );
11314}
11315
11316/// Internal helper modelling the C `done:` label tail of
11317/// `execcmd_exec` at `Src/exec.c:4366-4403`. Handles POSIX special-
11318/// builtin error escalation, AUTOCONTINUE restore, STTYval clear,
11319/// shelltime stop, and newxtrerr close.
11320#[allow(clippy::too_many_arguments)]
11321fn execcmd_exec_done_path(
11322    redir_err: i32,
11323    oautocont: i32,
11324    how: i32,
11325    shti: &mut crate::ported::jobs::timeinfo,
11326    chti: &mut crate::ported::jobs::timeinfo,
11327    then_ts: &mut std::time::Instant,
11328    forked: i32,
11329    newxtrerr: &mut Option<i32>,
11330    cflags: u32,
11331    orig_cflags: u32,
11332    is_cursh: i32,
11333    do_exec: i32,
11334) {
11335    use crate::ported::zsh_h::{
11336        AUTOCONTINUE, BINF_COMMAND, BINF_EXEC, BINF_PSPECIAL, INTERACTIVE, POSIXBUILTINS, Z_TIMED,
11337    };
11338    // c:4366
11339    // c:4367-4386 — POSIX special-builtin error escalation.
11340    if isset(POSIXBUILTINS)
11341        && (cflags & (BINF_PSPECIAL | BINF_EXEC)) != 0
11342        && (orig_cflags & BINF_COMMAND) == 0
11343    {
11344        // c:4367-4369
11345        let _forked_or_subsh = forked | zsh_subshell.load(Ordering::Relaxed); // c:4376
11346                                                                              // fatal: label entry point — same handling.
11347        if redir_err != 0 || (errflag.load(Ordering::Relaxed) & ERRFLAG_ERROR) != 0 {
11348            // c:4378
11349            if !isset(INTERACTIVE) {
11350                // c:4379
11351                if _forked_or_subsh != 0 {
11352                    unsafe { libc::_exit(1) }; // c:4381
11353                } else {
11354                    std::process::exit(1); // c:4383
11355                }
11356            }
11357            errflag.fetch_or(ERRFLAG_ERROR, Ordering::Relaxed); // c:4385
11358        }
11359    }
11360    // c:4388-4389 — `if ((is_cursh || do_exec) && (how & Z_TIMED)) shelltime(...);`
11361    if (is_cursh != 0 || do_exec != 0) && (how & Z_TIMED as i32) != 0 {
11362        crate::ported::jobs::shelltime(Some(shti), Some(chti), Some(then_ts), 1);
11363        // c:4389
11364    }
11365    // c:4390-4398 — newxtrerr close.
11366    if let Some(fd) = newxtrerr.take() {
11367        // c:4390
11368        let _ = zclose(fd); // c:4396
11369    }
11370    // c:4400-4401 — `zsfree(STTYval); STTYval = 0;`
11371    {
11372        let mut guard = STTYval.lock().unwrap();
11373        *guard = None;
11374    }
11375    // c:4402-4403 — `if (oautocont >= 0) opts[AUTOCONTINUE] = oautocont;`
11376    if oautocont >= 0 {
11377        opt_state_set("autocontinue", oautocont != 0);
11378    }
11379}
11380
11381/// Internal helper modelling the C `err:` label tail of
11382/// `execcmd_exec` at `Src/exec.c:4330-4365`. Forked-child fd cleanup
11383/// + waitjobs + _realexit; non-forked: `fixfds(save)` + fall through
11384/// to done:.
11385#[allow(clippy::too_many_arguments)]
11386fn execcmd_exec_err_path(
11387    forked: i32,
11388    save: &mut [i32; 10],
11389    mfds: &mut [Option<Box<multio>>; 10],
11390    oautocont: i32,
11391    how: i32,
11392    shti: &mut crate::ported::jobs::timeinfo,
11393    chti: &mut crate::ported::jobs::timeinfo,
11394    then_ts: &mut std::time::Instant,
11395    newxtrerr: &mut Option<i32>,
11396    cflags: u32,
11397    orig_cflags: u32,
11398    is_cursh: i32,
11399    do_exec: i32,
11400    redir_err: i32,
11401) {
11402    use crate::ported::zsh_h::FDT_UNUSED;
11403    // c:4330
11404    if forked != 0 {
11405        // c:4331
11406        // c:4356-4358 — close all fds 0..10 whose fdtable entry != FDT_UNUSED.
11407        let mut i: i32 = 0;
11408        while i < 10 {
11409            if fdtable_get(i) != FDT_UNUSED {
11410                unsafe { libc::close(i) }; // c:4358
11411            }
11412            i += 1;
11413        }
11414        // c:4359 — `closem(FDT_UNUSED, 1);`
11415        closem(FDT_UNUSED, 1); // c:4359
11416                               // c:4360-4361 — `if (thisjob != -1) waitjobs();`
11417        let thisjob = THISJOB.get().map(|m| *m.lock().unwrap()).unwrap_or(-1);
11418        if thisjob != -1 {
11419            if let Some(jt) = JOBTAB.get() {
11420                let mut guard = jt.lock().unwrap();
11421                crate::ported::jobs::waitjobs(&mut guard, thisjob as usize); // c:4361
11422            }
11423        }
11424        crate::ported::builtin::_realexit(); // c:4362
11425    }
11426    fixfds(save); // c:4364
11427
11428    execcmd_exec_done_path(
11429        redir_err,
11430        oautocont,
11431        how,
11432        shti,
11433        chti,
11434        then_ts,
11435        forked,
11436        newxtrerr,
11437        cflags,
11438        orig_cflags,
11439        is_cursh,
11440        do_exec,
11441    );
11442}
11443
11444/// Internal helper dispatching `execfuncs[type - WC_CURSH]` from
11445/// `Src/exec.c:170-180`. Each branch maps to the ported wordcode-
11446/// walker function in `src/ported/exec.rs`.
11447fn dispatch_execfuncs(state: &mut estate, typ: i32, do_exec: i32) -> i32 {
11448    use crate::ported::zsh_h::{
11449        WC_ARITH, WC_AUTOFN, WC_CASE, WC_COND, WC_CURSH, WC_FOR, WC_FUNCDEF, WC_IF, WC_REPEAT,
11450        WC_SELECT, WC_SUBSH, WC_TIMED, WC_TRY, WC_WHILE,
11451    };
11452    // Port of `static int (*const execfuncs[])(Estate, int)` dispatch
11453    // table at `Src/exec.c:170-180`. C indexes by `(type - WC_CURSH)`;
11454    // Rust matches on the WC_* tag directly.
11455    match typ as wordcode {
11456        x if x == WC_CURSH => execcursh(state, do_exec),
11457        x if x == WC_FOR => execfor(state, do_exec),
11458        x if x == WC_SELECT => execselect(state, do_exec),
11459        x if x == WC_WHILE => execwhile(state, do_exec),
11460        x if x == WC_REPEAT => execrepeat(state, do_exec),
11461        x if x == WC_CASE => execcase(state, do_exec),
11462        x if x == WC_IF => execif(state, do_exec),
11463        x if x == WC_COND => execcond(state, do_exec),
11464        x if x == WC_ARITH => execarith(state, do_exec),
11465        x if x == WC_TRY => exectry(state, do_exec),
11466        x if x == WC_FUNCDEF => execfuncdef(state, None),
11467        // c:272 — execfuncs[] table dispatches `WC_AUTOFN` to
11468        // `execautofn` (the loadautofn-then-basic wrapper), not
11469        // `execautofn_basic` directly.
11470        x if x == WC_AUTOFN => execautofn(state, do_exec),
11471        x if x == WC_TIMED => exectime(state, do_exec),
11472        x if x == WC_SUBSH => execcursh(state, do_exec), // c:269 — same handler.
11473        _ => 0,
11474    }
11475}
11476
11477/// Port of `Eprog stripkshdef(Eprog prog, char *name)` from
11478/// `Src/exec.c:6286-6364`. Given an Eprog read from an autoload
11479/// file plus the function name being defined, check whether the
11480/// file consists of *exactly* one `function NAME { … }` definition
11481/// for that name. If so, return a new Eprog whose `prog`/`strs`/
11482/// `pats` slice out just the function body (so calling code can
11483/// invoke the body directly instead of re-parsing). Otherwise
11484/// return the input untouched.
11485///
11486/// Header word layout consumed (matches C `pc[…]` reads):
11487///   pc[0] = WC_LIST with `Z_SYNC|Z_END|Z_SIMPLE` flags
11488///   pc[1] = (sublist header, skipped)
11489///   pc[2] = WC_FUNCDEF
11490///   pc[3] = 1                       (single-name funcdef)
11491///   pc[4] = name-string slot        (compared to `name`)
11492///   pc[5] = sbeg  (offset into strs table)
11493///   pc[6] = nstrs (bytes of strs to copy)
11494///   pc[7] = npats (number of pattern slots to allocate)
11495///   pc[8] = WC_FUNCDEF_SKIP target  (end-of-funcdef pc)
11496///   pc[9] = (unused header word — `pc += 6` lands here as the
11497///           start of the body wordcode stream)
11498///
11499/// Returns `None` only when the input was `None` (matches C
11500/// `return NULL`). Equivalence between the original `prog` and a
11501/// successfully stripped `prog` is *not* preserved at the pointer
11502/// level (C may return the original Eprog when the file fails the
11503/// single-funcdef shape check; this Rust port does the same by
11504/// passing the box back through).
11505///
11506/// `EF_MAP` (`zcompile`d / mmap'd Eprog) path: C mutates the
11507/// existing Eprog in place, swapping its `prog` / `strs` /
11508/// `pats` to slice into the funcdef body. Rust mirrors this on
11509/// the moved-in `Box<eprog>` (no separate `free()` needed —
11510/// `Vec` drop handles the old `pats`).
11511pub fn stripkshdef(
11512    prog: Option<crate::ported::zsh_h::Eprog>,
11513    name: &str,
11514) -> Option<crate::ported::zsh_h::Eprog> {
11515    use crate::ported::parse::ecrawstr;
11516    use crate::ported::zsh_h::{
11517        wc_code, wordcode, Dash, EF_HEAP, EF_MAP, WC_FUNCDEF, WC_FUNCDEF_SKIP, WC_LIST,
11518        WC_LIST_TYPE, Z_END, Z_SIMPLE, Z_SYNC,
11519    };
11520
11521    // c:6300 — `if (!prog) return NULL;`
11522    let mut prog = prog?;
11523
11524    // c:6302-6306 — first word must be WC_LIST with all of
11525    // Z_SYNC|Z_END|Z_SIMPLE set (i.e. the trivial "single simple
11526    // sublist" wrapper around the funcdef).
11527    if prog.prog.len() < 3 {
11528        return Some(prog);
11529    }
11530    let code0: wordcode = prog.prog[0];
11531    if wc_code(code0) != WC_LIST
11532        || (WC_LIST_TYPE(code0) & (Z_SYNC | Z_END | Z_SIMPLE) as wordcode)
11533            != (Z_SYNC | Z_END | Z_SIMPLE) as wordcode
11534    {
11535        return Some(prog);
11536    }
11537    // c:6307 — `pc++;` (skip the sublist header word at pc[1]).
11538    // c:6308 — `code = *pc++;` lands `code` on pc[2], leaving the
11539    // walking cursor at pc[3] which is read directly below.
11540    let code: wordcode = prog.prog[2];
11541    let pc_after_code: usize = 3;
11542    if wc_code(code) != WC_FUNCDEF || prog.prog[pc_after_code] != 1 {
11543        return Some(prog);
11544    }
11545
11546    // c:6320 — `ptr2 = ecrawstr(prog, pc + 1, NULL);` (note: C's
11547    // `pc` is already past `code`, so `pc + 1` lands on pc[4] —
11548    // the name-string slot).
11549    let name_slot = pc_after_code + 1; // == 4
11550    let name_in_def = ecrawstr(&prog, name_slot, None);
11551
11552    // c:6320-6328 — name match, tolerating Dash-tokenised hyphens
11553    // on either side.
11554    let n1 = name.as_bytes();
11555    let n2 = name_in_def.as_bytes();
11556    let mut i = 0usize;
11557    let mut j = 0usize;
11558    while i < n1.len() && j < n2.len() {
11559        let c1 = n1[i] as char;
11560        let c2 = n2[j] as char;
11561        if c1 != c2 && c1 != Dash && c1 != '-' && c2 != Dash && c2 != '-' {
11562            break;
11563        }
11564        i += 1;
11565        j += 1;
11566    }
11567    // c:6329 — `if (*ptr1 || *ptr2) return prog;` (any unmatched
11568    // tail on either side → not the right funcdef).
11569    if i < n1.len() || j < n2.len() {
11570        return Some(prog);
11571    }
11572
11573    // c:6332-6362 — slice the funcdef body out. Layout:
11574    //   sbeg  = pc[2] (in C, == prog.prog[pc_after_code + 2] == [5])
11575    //   nstrs = pc[3] (== [6])
11576    //   npats = pc[4] (== [7])
11577    //   end   = pc + WC_FUNCDEF_SKIP(code)   (== pc_after_code + skip)
11578    //   pc   += 6  (body wordcode begins at pc_after_code + 6 == [9])
11579    let sbeg = prog.prog[pc_after_code + 2] as usize;
11580    let nstrs = prog.prog[pc_after_code + 3] as usize;
11581    let npats = prog.prog[pc_after_code + 4] as i32;
11582    let skip = WC_FUNCDEF_SKIP(code) as usize;
11583    let end_pc = pc_after_code + skip;
11584    let body_start = pc_after_code + 6;
11585    if end_pc < body_start || end_pc > prog.prog.len() {
11586        // Defensive: malformed header — return input untouched so
11587        // the caller's parse-eprog fallback re-reads from source.
11588        return Some(prog);
11589    }
11590    let nprg = end_pc - body_start;
11591    let plen = nprg * size_of::<wordcode>();
11592    let len = plen + (npats as usize) * size_of::<usize>() + nstrs;
11593
11594    // Build the new pats slice — `dummy_patprog1` slots in C; the
11595    // Rust convention (mirrors `dupeprog` at parse.rs:2716) is to
11596    // synthesize zero-initialised patprog placeholders that
11597    // pattern compile-on-first-use will overwrite.
11598    let dummy_pat = || {
11599        Box::new(crate::ported::zsh_h::patprog {
11600            startoff: 0,
11601            size: 0,
11602            mustoff: 0,
11603            patmlen: 0,
11604            globflags: 0,
11605            globend: 0,
11606            flags: 0,
11607            patnpar: 0,
11608            patstartch: 0,
11609        })
11610    };
11611    let new_pats: Vec<crate::ported::zsh_h::Patprog> =
11612        (0..npats.max(0)).map(|_| dummy_pat()).collect();
11613
11614    // c:6353 — `ret->strs = prog->strs + sbeg;` (EF_MAP) or
11615    // c:6359 — `memcpy(ret->strs, prog->strs + sbeg, nstrs);` (heap).
11616    let old_strs = prog.strs.take().unwrap_or_default();
11617    let old_bytes = old_strs.as_bytes();
11618    let new_strs = if sbeg + nstrs <= old_bytes.len() {
11619        Some(String::from_utf8_lossy(&old_bytes[sbeg..sbeg + nstrs]).into_owned())
11620    } else {
11621        Some(String::new())
11622    };
11623
11624    let new_prog: Vec<wordcode> = prog.prog[body_start..end_pc].to_vec();
11625
11626    if (prog.flags & EF_MAP) != 0 {
11627        // c:6349-6354 — in-place EF_MAP path.
11628        prog.pats = new_pats;
11629        prog.prog = new_prog;
11630        prog.strs = new_strs;
11631        prog.len = len as i32;
11632        prog.npats = npats;
11633        prog.shf = None;
11634        return Some(prog);
11635    }
11636
11637    // c:6356-6361 — heap-allocated new Eprog.
11638    let ret = Box::new(eprog {
11639        flags: EF_HEAP,
11640        len: len as i32,
11641        npats,
11642        nref: -1, // c:6363 (heap path → never refcount-freed).
11643        pats: new_pats,
11644        prog: new_prog,
11645        strs: new_strs,
11646        shf: None, // c:6363
11647        dump: None,
11648        strs_metafied: false, // native pool — clean UTF-8
11649    });
11650    Some(ret)
11651}
11652
11653#[cfg(test)]
11654mod tests {
11655    use super::*;
11656
11657    // ─── zsh-corpus pins for pure exec helpers ─────────────────────
11658
11659    /// `Src/exec.c:996-1010` — `isrelative` returns 1 for empty.
11660    #[test]
11661    fn exec_corpus_isrelative_empty_is_one() {
11662        let _g = crate::test_util::global_state_lock();
11663        assert_eq!(isrelative(""), 1, "empty path is relative");
11664    }
11665
11666    /// `isrelative("foo")` = 1 (no leading slash).
11667    #[test]
11668    fn exec_corpus_isrelative_bare_name_is_one() {
11669        let _g = crate::test_util::global_state_lock();
11670        assert_eq!(isrelative("foo"), 1);
11671        assert_eq!(isrelative("bin/cmd"), 1);
11672    }
11673
11674    /// `isrelative("/foo")` = 0 (absolute, no `./` / `../`).
11675    #[test]
11676    fn exec_corpus_isrelative_absolute_clean_is_zero() {
11677        let _g = crate::test_util::global_state_lock();
11678        assert_eq!(isrelative("/foo"), 0, "/foo is absolute");
11679        assert_eq!(isrelative("/bin/ls"), 0);
11680        assert_eq!(isrelative("/"), 0, "root is absolute");
11681    }
11682
11683    /// `isrelative("/foo/../bar")` = 1 (contains `../` component).
11684    #[test]
11685    fn exec_corpus_isrelative_absolute_with_dotdot_is_one() {
11686        let _g = crate::test_util::global_state_lock();
11687        assert_eq!(
11688            isrelative("/foo/../bar"),
11689            1,
11690            "absolute path with ../ is still 'relative' per zsh"
11691        );
11692    }
11693
11694    /// `isrelative("/foo/./bar")` = 1 (contains `./` component).
11695    #[test]
11696    fn exec_corpus_isrelative_absolute_with_dot_is_one() {
11697        let _g = crate::test_util::global_state_lock();
11698        assert_eq!(
11699            isrelative("/./x"),
11700            1,
11701            "absolute with ./ component reported relative"
11702        );
11703    }
11704
11705    /// `Src/exec.c:5300` — `is_anonymous_function_name("(anon)")` = 1.
11706    #[test]
11707    fn exec_corpus_is_anonymous_function_name_matches_sentinel() {
11708        assert_eq!(is_anonymous_function_name("(anon)"), 1);
11709    }
11710
11711    /// `is_anonymous_function_name("regular_name")` = 0.
11712    #[test]
11713    fn exec_corpus_is_anonymous_function_name_rejects_normal() {
11714        assert_eq!(is_anonymous_function_name("regular_name"), 0);
11715        assert_eq!(is_anonymous_function_name(""), 0);
11716        assert_eq!(
11717            is_anonymous_function_name("anon"),
11718            0,
11719            "plain 'anon' (no parens) is NOT the sentinel"
11720        );
11721    }
11722
11723    /// `iscom("/nonexistent/never_a_path")` = false.
11724    #[test]
11725    fn exec_corpus_iscom_missing_path_false() {
11726        assert!(!iscom("/this/path/does/not/exist/zshrs_xyz"));
11727    }
11728
11729    /// `iscom("/tmp")` is a directory not a regular file → false.
11730    #[test]
11731    fn exec_corpus_iscom_directory_false() {
11732        assert!(!iscom("/tmp"), "/tmp is a dir, not a regular command");
11733    }
11734
11735    /// `iscom("/bin/sh")` is true on POSIX systems.
11736    #[test]
11737    fn exec_corpus_iscom_known_binary_true() {
11738        // /bin/sh exists on all POSIX systems with X perms.
11739        if std::path::Path::new("/bin/sh").exists() {
11740            assert!(iscom("/bin/sh"), "/bin/sh is a real executable");
11741        }
11742    }
11743
11744    // ─── stripkshdef (Src/exec.c:6286) early-return paths ──────────
11745
11746    /// `stripkshdef(None, "foo")` → `None` (matches C `if (!prog)
11747    /// return NULL;` at exec.c:6300).
11748    #[test]
11749    fn exec_corpus_stripkshdef_null_input_returns_none() {
11750        assert!(stripkshdef(None, "foo").is_none());
11751    }
11752
11753    /// `stripkshdef` on an empty/degenerate Eprog returns the same
11754    /// Eprog unchanged (no funcdef-shape to strip).
11755    #[test]
11756    fn exec_corpus_stripkshdef_empty_prog_returns_input() {
11757        let prog = Box::new(eprog {
11758            prog: vec![],
11759            ..Default::default()
11760        });
11761        let out = stripkshdef(Some(prog), "foo");
11762        assert!(out.is_some(), "empty prog → returned unchanged");
11763        assert!(out.unwrap().prog.is_empty(), "no mutation");
11764    }
11765
11766    /// `stripkshdef` on a non-WC_LIST head returns the input
11767    /// untouched (early return at exec.c:6304-6306).
11768    #[test]
11769    fn exec_corpus_stripkshdef_non_list_head_returns_input() {
11770        use crate::ported::zsh_h::{wc_bld, WC_SUBLIST};
11771        let prog = Box::new(eprog {
11772            prog: vec![wc_bld(WC_SUBLIST, 0), 0, 0],
11773            ..Default::default()
11774        });
11775        let out = stripkshdef(Some(prog), "foo");
11776        assert!(out.is_some());
11777        // first word is the WC_SUBLIST sentinel we passed in,
11778        // unchanged (the function bailed before doing any slicing).
11779        let p = out.unwrap();
11780        use crate::ported::zsh_h::wc_code;
11781        assert_eq!(
11782            wc_code(p.prog[0]),
11783            WC_SUBLIST,
11784            "header word preserved verbatim"
11785        );
11786    }
11787
11788    // ═══════════════════════════════════════════════════════════════════
11789    // C-parity tests pinning Src/exec.c. Tests that capture KNOWN
11790    // ZSHRS BUGS use #[ignore = "ZSHRS BUG: …"].
11791    // ═══════════════════════════════════════════════════════════════════
11792
11793    /// `isrelative("/abs/path")` returns 0 (false = absolute path).
11794    /// C `Src/exec.c:996-1006` — leading `/` and no `.`/`..` components.
11795    #[test]
11796    fn isrelative_absolute_path_returns_zero() {
11797        let _g = crate::test_util::global_state_lock();
11798        assert_eq!(isrelative("/usr/local/bin"), 0);
11799    }
11800
11801    /// `isrelative("foo/bar")` returns 1 (no leading slash).
11802    #[test]
11803    fn isrelative_no_leading_slash_returns_one() {
11804        let _g = crate::test_util::global_state_lock();
11805        assert_eq!(isrelative("foo/bar"), 1);
11806    }
11807
11808    /// `isrelative("/foo/./bar")` returns 1 — contains `/./` walk.
11809    /// C c:1001 — `.` with prev `/` + next `/` triggers relative flag.
11810    #[test]
11811    fn isrelative_dot_component_returns_one() {
11812        let _g = crate::test_util::global_state_lock();
11813        assert_eq!(isrelative("/foo/./bar"), 1, "/./ in path → relative");
11814    }
11815
11816    /// `isrelative("/foo/../bar")` returns 1 — contains `/..` walk.
11817    #[test]
11818    fn isrelative_dotdot_component_returns_one() {
11819        let _g = crate::test_util::global_state_lock();
11820        assert_eq!(isrelative("/foo/../bar"), 1, "/../ in path → relative");
11821    }
11822
11823    /// `isrelative("")` returns 1 — empty input has no leading `/`.
11824    /// C c:998 — `*s != '/'` includes the NUL terminator case.
11825    #[test]
11826    fn isrelative_empty_returns_one() {
11827        let _g = crate::test_util::global_state_lock();
11828        assert_eq!(isrelative(""), 1, "empty string → not absolute");
11829    }
11830
11831    /// `isrelative("/a/.b")` returns 0 — `.b` is NOT a `/./` walk
11832    /// (followed by another non-`/` char `b`).
11833    #[test]
11834    fn isrelative_dotfile_in_path_returns_zero() {
11835        let _g = crate::test_util::global_state_lock();
11836        assert_eq!(
11837            isrelative("/usr/.config/zsh"),
11838            0,
11839            "dotfile name '.config' is NOT a relative walk"
11840        );
11841    }
11842
11843    /// `is_anonymous_function_name("(anon)")` returns 1 (true).
11844    /// C `Src/exec.c` — `!strcmp(name, ANONYMOUS_FUNCTION_NAME)`.
11845    #[test]
11846    fn is_anonymous_function_name_anon_returns_one() {
11847        let _g = crate::test_util::global_state_lock();
11848        assert_eq!(is_anonymous_function_name("(anon)"), 1);
11849    }
11850
11851    /// `is_anonymous_function_name("foo")` returns 0 (false).
11852    #[test]
11853    fn is_anonymous_function_name_normal_returns_zero() {
11854        let _g = crate::test_util::global_state_lock();
11855        assert_eq!(is_anonymous_function_name("foo"), 0);
11856        assert_eq!(is_anonymous_function_name(""), 0);
11857        assert_eq!(is_anonymous_function_name("(other)"), 0);
11858    }
11859
11860    /// `isgooderr(EACCES, "/no/such/dir")` returns true when the dir
11861    /// is not actually accessible. C `Src/exec.c:isgooderr` filters
11862    /// out "unreadable / not directory" errnos so caller doesn't
11863    /// emit spurious warnings.
11864    #[test]
11865    fn isgooderr_eacces_unreadable_dir_returns_false() {
11866        let _g = crate::test_util::global_state_lock();
11867        // /no/such/dir doesn't exist → access(X_OK) fails non-zero
11868        // → !access() is 0 (false) → returns false.
11869        assert!(
11870            !isgooderr(libc::EACCES, "/no/such/dir/zshrs_test"),
11871            "unreadable dir with EACCES should NOT be 'good error'"
11872        );
11873    }
11874
11875    // ═══════════════════════════════════════════════════════════════════
11876    // Additional C-parity tests for Src/exec.c basic accessors/predicates.
11877    // ═══════════════════════════════════════════════════════════════════
11878
11879    /// c:658 — `isgooderr(ENOENT, _)` always false (regardless of dir).
11880    /// Pin: ENOENT is NEVER a "good error" because the path itself
11881    /// doesn't exist — caller should suppress the warning.
11882    #[test]
11883    fn isgooderr_enoent_always_false() {
11884        let _g = crate::test_util::global_state_lock();
11885        assert!(!isgooderr(libc::ENOENT, "/tmp"));
11886        assert!(!isgooderr(libc::ENOENT, "/no/such/dir"));
11887        assert!(!isgooderr(libc::ENOENT, ""));
11888    }
11889
11890    /// c:658 — `isgooderr(ENOTDIR, _)` always false. A path component
11891    /// being a non-dir is a structural error, not a permission issue.
11892    #[test]
11893    fn isgooderr_enotdir_always_false() {
11894        let _g = crate::test_util::global_state_lock();
11895        assert!(!isgooderr(libc::ENOTDIR, "/tmp"));
11896        assert!(!isgooderr(libc::ENOTDIR, "/"));
11897    }
11898
11899    /// c:658 — Other errnos (EPERM, EIO, ENOMEM) are "good errors"
11900    /// because they're not the suppressed three (EACCES/ENOENT/ENOTDIR).
11901    #[test]
11902    fn isgooderr_other_errno_returns_true() {
11903        let _g = crate::test_util::global_state_lock();
11904        assert!(isgooderr(libc::EPERM, "/tmp"));
11905        assert!(isgooderr(libc::EIO, "/tmp"));
11906        assert!(isgooderr(libc::ENOMEM, "/tmp"));
11907    }
11908
11909    /// c:962 — `iscom("/tmp")` returns false (directory, not S_ISREG).
11910    #[test]
11911    fn iscom_directory_returns_false() {
11912        let _g = crate::test_util::global_state_lock();
11913        assert!(!iscom("/tmp"));
11914        assert!(!iscom("/"));
11915    }
11916
11917    /// c:962 — `iscom` on non-existent path returns false (access
11918    /// X_OK fails).
11919    #[test]
11920    fn iscom_nonexistent_path_returns_false() {
11921        let _g = crate::test_util::global_state_lock();
11922        assert!(!iscom("/no/such/path/zshrs_iscom_test"));
11923        assert!(!iscom(""));
11924    }
11925
11926    /// c:962 — `iscom("/bin/sh")` returns true on every POSIX system.
11927    #[test]
11928    #[cfg(unix)]
11929    fn iscom_bin_sh_returns_true() {
11930        let _g = crate::test_util::global_state_lock();
11931        // /bin/sh is a POSIX-required executable.
11932        assert!(iscom("/bin/sh"), "/bin/sh must be executable on POSIX");
11933    }
11934
11935    /// c:5300 — anonymous function name is exactly "(anon)" — must
11936    /// not match prefixes/suffixes/case variants.
11937    #[test]
11938    fn is_anonymous_function_name_strict_match_only() {
11939        let _g = crate::test_util::global_state_lock();
11940        assert_eq!(is_anonymous_function_name("(anon"), 0, "no trailing paren");
11941        assert_eq!(is_anonymous_function_name("anon)"), 0, "no leading paren");
11942        assert_eq!(is_anonymous_function_name("(ANON)"), 0, "wrong case");
11943        assert_eq!(
11944            is_anonymous_function_name(" (anon) "),
11945            0,
11946            "leading/trailing space"
11947        );
11948        assert_eq!(is_anonymous_function_name("(anon) "), 0, "trailing space");
11949        assert_eq!(is_anonymous_function_name(" (anon)"), 0, "leading space");
11950    }
11951
11952    /// c:5289 — `ANONYMOUS_FUNCTION_NAME` constant is exactly `"(anon)"`.
11953    /// Pin so a regen that flips parens / changes case / adds prefix
11954    /// would be caught.
11955    #[test]
11956    fn anonymous_function_name_const_is_literal_anon() {
11957        let _g = crate::test_util::global_state_lock();
11958        assert_eq!(ANONYMOUS_FUNCTION_NAME, "(anon)");
11959    }
11960
11961    /// c:147-148 — `isrelative("./")` returns 1 (dot-slash prefix
11962    /// is the canonical relative-path form).
11963    #[test]
11964    fn isrelative_dot_slash_returns_one() {
11965        let _g = crate::test_util::global_state_lock();
11966        assert_eq!(isrelative("./foo"), 1);
11967        assert_eq!(isrelative("./"), 1);
11968    }
11969
11970    /// c:147-148 — `isrelative("../foo")` returns 1.
11971    #[test]
11972    fn isrelative_dotdot_slash_returns_one() {
11973        let _g = crate::test_util::global_state_lock();
11974        assert_eq!(isrelative("../foo"), 1);
11975        assert_eq!(isrelative("../"), 1);
11976    }
11977
11978    /// c:147-148 — `/.foo` (hidden file under root) is absolute.
11979    /// Pin: only `/.` (with trailing `/`) or end-of-string counts as
11980    /// a `.` component, NOT `/.foo` (which is a normal file `.foo`).
11981    #[test]
11982    fn isrelative_root_hidden_file_returns_zero() {
11983        let _g = crate::test_util::global_state_lock();
11984        assert_eq!(isrelative("/.foo"), 0, "/.foo is absolute path to dotfile");
11985        assert_eq!(isrelative("/.bashrc"), 0, "/.bashrc is absolute");
11986    }
11987
11988    /// c:147-148 — `/..bar` (file named `..bar`) is also absolute,
11989    /// since `..bar` is a regular file name, not a `..` component.
11990    #[test]
11991    fn isrelative_root_double_dot_file_returns_zero() {
11992        let _g = crate::test_util::global_state_lock();
11993        assert_eq!(isrelative("/..bar"), 0);
11994    }
11995
11996    /// c:2652 — `setunderscore("")` clears `zunderscore` and resets
11997    /// `underscoreused` to 1 (null terminator only).
11998    #[test]
11999    fn setunderscore_empty_clears_state() {
12000        let _g = crate::test_util::global_state_lock();
12001        setunderscore(""); // initialize to known empty state
12002        let zu = zunderscore.lock().unwrap();
12003        assert!(zu.is_empty(), "zunderscore must be empty after clear");
12004        drop(zu);
12005        let used = underscoreused.load(Ordering::Relaxed);
12006        assert_eq!(used, 1, "underscoreused must be 1 (NUL only) after clear");
12007    }
12008
12009    /// c:2652 — `setunderscore(str)` sets `zunderscore=str` and
12010    /// `underscoreused = str.len()+1` (string + null terminator).
12011    #[test]
12012    fn setunderscore_with_value_stores_string_and_length() {
12013        let _g = crate::test_util::global_state_lock();
12014        setunderscore("hello");
12015        let zu = zunderscore.lock().unwrap();
12016        assert_eq!(*zu, "hello");
12017        drop(zu);
12018        let used = underscoreused.load(Ordering::Relaxed);
12019        assert_eq!(used, 6, "len('hello')+1 = 6");
12020    }
12021
12022    /// c:2656 — `underscorelen` is rounded up to 32-byte boundary
12023    /// for the bump-allocator-friendly buffer growth.
12024    #[test]
12025    fn setunderscore_rounds_underscorelen_to_32() {
12026        let _g = crate::test_util::global_state_lock();
12027        setunderscore("ab"); // len 2 + 1 = 3 → ceil(32) = 32
12028        let nl = underscorelen.load(Ordering::Relaxed);
12029        assert_eq!(nl, 32, "(2+1+31) & !31 = 32");
12030    }
12031
12032    // ═══════════════════════════════════════════════════════════════════
12033    // Additional C-parity tests for Src/exec.c cancd2 +
12034    // quote_tokenized_output.
12035    // ═══════════════════════════════════════════════════════════════════
12036
12037    /// c:6411 — `cancd2("/tmp")` returns 1 (directory with X_OK exists).
12038    #[test]
12039    #[cfg(unix)]
12040    fn cancd2_existing_dir_returns_one() {
12041        let _g = crate::test_util::global_state_lock();
12042        assert_eq!(cancd2("/tmp"), 1, "/tmp is a valid cd target");
12043    }
12044
12045    /// c:6411 — `cancd2("/nonexistent")` returns 0.
12046    #[test]
12047    fn cancd2_nonexistent_returns_zero() {
12048        let _g = crate::test_util::global_state_lock();
12049        assert_eq!(cancd2("/__never_exists_zshrs_cancd2__"), 0);
12050    }
12051
12052    /// c:6411 — `cancd2` for a file (not dir) returns 0.
12053    #[test]
12054    #[cfg(unix)]
12055    fn cancd2_regular_file_returns_zero() {
12056        let _g = crate::test_util::global_state_lock();
12057        let dir = tempfile::tempdir().unwrap();
12058        let p = dir.path().join("regular_file");
12059        std::fs::write(&p, "x").unwrap();
12060        assert_eq!(
12061            cancd2(p.to_str().unwrap()),
12062            0,
12063            "regular file not a cd target"
12064        );
12065    }
12066
12067    /// c:2114 — `quote_tokenized_output` on empty string writes nothing.
12068    #[test]
12069    fn quote_tokenized_output_empty_writes_nothing() {
12070        let _g = crate::test_util::global_state_lock();
12071        let mut buf = Vec::new();
12072        quote_tokenized_output("", &mut buf).unwrap();
12073        assert!(buf.is_empty());
12074    }
12075
12076    /// c:2114 — plain ASCII passes through unchanged.
12077    #[test]
12078    fn quote_tokenized_output_plain_ascii_unchanged() {
12079        let _g = crate::test_util::global_state_lock();
12080        let mut buf = Vec::new();
12081        quote_tokenized_output("hello", &mut buf).unwrap();
12082        assert_eq!(buf, b"hello");
12083    }
12084
12085    /// c:2143 — space gets backslash-quoted.
12086    #[test]
12087    fn quote_tokenized_output_space_backslash_quoted() {
12088        let _g = crate::test_util::global_state_lock();
12089        let mut buf = Vec::new();
12090        quote_tokenized_output("a b", &mut buf).unwrap();
12091        assert_eq!(buf, b"a\\ b");
12092    }
12093
12094    /// c:2147 — tab → $'\\t'.
12095    #[test]
12096    fn quote_tokenized_output_tab_dollar_escape() {
12097        let _g = crate::test_util::global_state_lock();
12098        let mut buf = Vec::new();
12099        quote_tokenized_output("a\tb", &mut buf).unwrap();
12100        assert_eq!(buf, b"a$'\\t'b");
12101    }
12102
12103    /// c:2151 — newline → $'\\n'.
12104    #[test]
12105    fn quote_tokenized_output_newline_dollar_escape() {
12106        let _g = crate::test_util::global_state_lock();
12107        let mut buf = Vec::new();
12108        quote_tokenized_output("a\nb", &mut buf).unwrap();
12109        assert_eq!(buf, b"a$'\\n'b");
12110    }
12111
12112    /// c:2155 — CR → $'\\r'.
12113    #[test]
12114    fn quote_tokenized_output_cr_dollar_escape() {
12115        let _g = crate::test_util::global_state_lock();
12116        let mut buf = Vec::new();
12117        quote_tokenized_output("a\rb", &mut buf).unwrap();
12118        assert_eq!(buf, b"a$'\\r'b");
12119    }
12120
12121    /// c:2128 — shell metacharacters all get backslash-quoted.
12122    #[test]
12123    fn quote_tokenized_output_shell_metas_get_backslash() {
12124        let _g = crate::test_util::global_state_lock();
12125        for c in &[b'<', b'>', b'(', b')', b'|', b'#', b'$', b'*', b'?', b'~'] {
12126            let mut buf = Vec::new();
12127            let s = String::from_utf8(vec![b'a', *c, b'b']).unwrap();
12128            quote_tokenized_output(&s, &mut buf).unwrap();
12129            assert_eq!(buf, vec![b'a', b'\\', *c, b'b'], "char {:?}", *c as char);
12130        }
12131    }
12132
12133    /// c:2158 — `=` at position 0 gets quoted (path-spec).
12134    #[test]
12135    fn quote_tokenized_output_equals_at_start_quoted() {
12136        let _g = crate::test_util::global_state_lock();
12137        let mut buf = Vec::new();
12138        quote_tokenized_output("=foo", &mut buf).unwrap();
12139        assert_eq!(buf, b"\\=foo");
12140    }
12141
12142    // ═══════════════════════════════════════════════════════════════════
12143    // Additional C-parity tests for Src/exec.c
12144    // c:1287 iscom / c:1347 isrelative / c:1398 setunderscore /
12145    // c:1468 is_anonymous_function_name / c:2208 findcmd / c:3273 parsecmd
12146    // c:1264 isgooderr / c:1226 parse_string
12147    // ═══════════════════════════════════════════════════════════════════
12148
12149    /// c:1287 — `iscom("")` empty input returns false.
12150    #[test]
12151    fn iscom_empty_string_returns_false() {
12152        let _g = crate::test_util::global_state_lock();
12153        assert!(!iscom(""), "empty cmd name → not a command");
12154    }
12155
12156    /// c:1287 — `iscom` returns bool (compile-time type pin).
12157    #[test]
12158    fn iscom_returns_bool_type() {
12159        let _g = crate::test_util::global_state_lock();
12160        let _: bool = iscom("ls");
12161    }
12162
12163    /// c:1347 — `isrelative("/abs")` returns 0 (absolute path).
12164    #[test]
12165    fn isrelative_absolute_path_returns_zero_pin() {
12166        assert_eq!(isrelative("/usr/bin"), 0, "/usr/bin is absolute");
12167        assert_eq!(isrelative("/"), 0, "/ is absolute");
12168    }
12169
12170    /// c:1347 — `isrelative("rel/path")` returns 1 (relative).
12171    #[test]
12172    fn isrelative_relative_path_returns_one_pin() {
12173        assert_eq!(isrelative("foo"), 1, "foo is relative");
12174        assert_eq!(isrelative("./foo"), 1, "./foo is relative");
12175        assert_eq!(isrelative("../foo"), 1, "../foo is relative");
12176    }
12177
12178    /// c:1347 — `isrelative("")` empty returns 1 (relative by C convention).
12179    #[test]
12180    fn isrelative_empty_returns_relative() {
12181        let r = isrelative("");
12182        assert!(r == 0 || r == 1, "must be 0 or 1");
12183    }
12184
12185    /// c:1468 — `is_anonymous_function_name` returns i32 (type pin).
12186    #[test]
12187    fn is_anonymous_function_name_returns_i32_type() {
12188        let _: i32 = is_anonymous_function_name("(anon)");
12189    }
12190
12191    /// c:1468 — `is_anonymous_function_name("")` empty returns 0.
12192    #[test]
12193    fn is_anonymous_function_name_empty_returns_zero() {
12194        assert_eq!(
12195            is_anonymous_function_name(""),
12196            0,
12197            "empty name is not anonymous"
12198        );
12199    }
12200
12201    /// c:1468 — `is_anonymous_function_name` is deterministic.
12202    #[test]
12203    fn is_anonymous_function_name_is_deterministic() {
12204        for s in ["", "name", "(anon)", "(anon: foo)"] {
12205            let first = is_anonymous_function_name(s);
12206            for _ in 0..3 {
12207                assert_eq!(
12208                    is_anonymous_function_name(s),
12209                    first,
12210                    "is_anonymous_function_name({:?}) must be deterministic",
12211                    s
12212                );
12213            }
12214        }
12215    }
12216
12217    /// c:1226 — `parse_string("")` empty returns Option<eprog> (type pin).
12218    #[test]
12219    fn parse_string_returns_option_eprog_type() {
12220        let _g = crate::test_util::global_state_lock();
12221        let _: Option<eprog> = parse_string("", 0);
12222    }
12223
12224    /// c:1398 — `setunderscore("")` empty string is safe.
12225    #[test]
12226    fn setunderscore_empty_no_panic() {
12227        let _g = crate::test_util::global_state_lock();
12228        setunderscore("");
12229    }
12230
12231    /// c:1264 — `isgooderr` returns bool (compile-time type pin).
12232    #[test]
12233    fn isgooderr_returns_bool_type() {
12234        let _: bool = isgooderr(0, "/tmp");
12235    }
12236
12237    // ═══════════════════════════════════════════════════════════════════
12238    // Additional C-parity tests for Src/exec.c
12239    // c:3325 makecline / c:4603 cancd / c:4674 simple_redir_name /
12240    // c:1287 iscom / c:1314 isreallycom / c:3076 commandnotfound
12241    // ═══════════════════════════════════════════════════════════════════
12242
12243    /// c:3325 — `makecline` returns Vec<String> (compile-time type pin).
12244    #[test]
12245    fn makecline_returns_vec_string_type() {
12246        let _g = crate::test_util::global_state_lock();
12247        let _: Vec<String> = makecline(&[]);
12248    }
12249
12250    /// c:3325 — `makecline([])` empty returns empty Vec.
12251    #[test]
12252    fn makecline_empty_input_returns_empty() {
12253        let _g = crate::test_util::global_state_lock();
12254        let r = makecline(&[]);
12255        assert!(r.is_empty(), "empty input → empty output");
12256    }
12257
12258    /// c:3325 — `makecline` preserves input order.
12259    #[test]
12260    fn makecline_preserves_input_order() {
12261        let _g = crate::test_util::global_state_lock();
12262        let input = vec!["one".to_string(), "two".to_string(), "three".to_string()];
12263        let out = makecline(&input);
12264        assert_eq!(out, input, "makecline must preserve order");
12265    }
12266
12267    /// c:3325 — `makecline` clones (output is independent of input).
12268    #[test]
12269    fn makecline_returns_independent_copy() {
12270        let _g = crate::test_util::global_state_lock();
12271        let input = vec!["a".to_string(), "b".to_string()];
12272        let out = makecline(&input);
12273        assert_eq!(out.len(), input.len(), "lengths match");
12274        // Output can be mutated without affecting input.
12275        let mut out_mut = out;
12276        out_mut.push("c".to_string());
12277        assert_eq!(input.len(), 2, "input unchanged");
12278    }
12279
12280    /// c:4603 — `cancd("")` empty path returns None.
12281    /// ZSHRS BUG: empty path returns Some(...) instead of None. C path
12282    /// at Src/exec.c:6376 enters relative-path branch which calls cancd2("")
12283    /// — that should return 0 (not a valid dir), causing the fn to fall
12284    /// through CDPATH and cd_able_vars, both of which should miss for
12285    /// the empty string. Likely cd_able_vars("") or CDPATH-with-empty-element
12286    /// is silently matching $HOME or "." here.
12287    /// C-faithful behavior: `cancd("")` enters the `!starts_with('/')`
12288    /// branch (c:6376), calls `cancd2("")` which appends to PWD →
12289    /// "PWD/" → fixdir → PWD itself → access+stat succeed → returns
12290    /// `Some(pwd)`. Verified against `/bin/zsh -fc 'cd ""; echo $?'`
12291    /// → `0` (success). The previous test expectation (None) was
12292    /// based on a misread of the C source — pin actual behavior.
12293    #[test]
12294    fn cancd_empty_returns_none() {
12295        let _g = crate::test_util::global_state_lock();
12296        // cancd("") returns Some — empty path resolves through PWD per
12297        // the cancd2 path; matches C zsh's `cd ""` exit-0 behavior.
12298        // Pin PWD to a known-existing dir so a prior test that left
12299        // PWD set to a non-directory doesn't masquerade as the bug.
12300        let saved_pwd = crate::ported::params::getsparam("PWD");
12301        crate::ported::params::setsparam("PWD", "/");
12302        let r = cancd("");
12303        if let Some(p) = saved_pwd {
12304            crate::ported::params::setsparam("PWD", &p);
12305        } else {
12306            crate::ported::params::unsetparam("PWD");
12307        }
12308        assert!(
12309            r.is_some(),
12310            "empty path → Some(pwd) per cancd2-via-PWD path"
12311        );
12312    }
12313
12314    /// c:4603 — `cancd("/")` root dir returns Some (always exists).
12315    #[test]
12316    fn cancd_root_returns_some() {
12317        let _g = crate::test_util::global_state_lock();
12318        let r = cancd("/");
12319        assert_eq!(r.as_deref(), Some("/"), "root dir cancd → Some(/)");
12320    }
12321
12322    /// c:4603 — `cancd` returns Option<String> (compile-time type pin).
12323    #[test]
12324    fn cancd_returns_option_string_type() {
12325        let _g = crate::test_util::global_state_lock();
12326        let _: Option<String> = cancd("/");
12327    }
12328
12329    /// c:4603 — `cancd("/__nonexistent__")` returns None.
12330    #[test]
12331    fn cancd_nonexistent_returns_none() {
12332        let _g = crate::test_util::global_state_lock();
12333        assert!(
12334            cancd("/__nonexistent_zshrs_dir_xyz__").is_none(),
12335            "nonexistent dir → None"
12336        );
12337    }
12338
12339    /// c:4603 — `cancd("/tmp")` exists → Some.
12340    #[test]
12341    fn cancd_tmp_returns_some() {
12342        let _g = crate::test_util::global_state_lock();
12343        let r = cancd("/tmp");
12344        assert!(r.is_some(), "/tmp exists → Some");
12345    }
12346
12347    /// c:4603 — `cancd` is deterministic for stable paths.
12348    #[test]
12349    fn cancd_is_deterministic_for_stable_paths() {
12350        let _g = crate::test_util::global_state_lock();
12351        for p in ["/", "/tmp", "/__never__"] {
12352            let first = cancd(p).is_some();
12353            for _ in 0..3 {
12354                assert_eq!(
12355                    cancd(p).is_some(),
12356                    first,
12357                    "cancd({:?}) must be deterministic",
12358                    p
12359                );
12360            }
12361        }
12362    }
12363
12364    /// c:1287 — `iscom` is deterministic for stable paths.
12365    #[test]
12366    fn iscom_is_deterministic_for_stable_paths() {
12367        let _g = crate::test_util::global_state_lock();
12368        for p in ["/tmp", "/__never__", "/bin/sh"] {
12369            let first = iscom(p);
12370            for _ in 0..3 {
12371                assert_eq!(iscom(p), first, "iscom({:?}) must be deterministic", p);
12372            }
12373        }
12374    }
12375
12376    /// c:3076 — `commandnotfound("", ...)` empty cmd returns i32.
12377    #[test]
12378    fn commandnotfound_returns_i32_type() {
12379        let _g = crate::test_util::global_state_lock();
12380        let mut args = Vec::new();
12381        let _: i32 = commandnotfound("", &mut args);
12382    }
12383}
12384
12385#[cfg(test)]
12386// Relocated from the deleted src/ported/exec_hooks.rs. These pin the
12387// no-executor fallback behavior + return types of the live-executor
12388// accessor wrappers (array/assoc/dispatch_function_call/...), which
12389// now live in this module (exec.rs) instead of the exec_hooks OnceLock
12390// layer. Behavior is identical; only the indirection was removed.
12391mod exec_accessor_tests {
12392    use super::*;
12393    use indexmap::IndexMap;
12394
12395    // ─── zsh-corpus pins: default (no-hook) fallback behavior ─────
12396
12397    /// `dispatch_function_call` returns None when no hook installed.
12398    /// Tests may run in a fresh process where no fusevm bridge wired
12399    /// the dispatch yet; pin: no-panic, None-return.
12400    #[test]
12401    fn exec_hooks_corpus_dispatch_returns_none_when_not_installed() {
12402        let _g = crate::test_util::global_state_lock();
12403        // We can't unset OnceLock once set, but if test runs first
12404        // in this process it should be None. The defensive pin is:
12405        // either None or Some — never panic.
12406        let _ = dispatch_function_call("__never_a_real_function_zshrs__", &["a".into()]);
12407        // No panic = pass.
12408    }
12409
12410    /// `execute_script` returns `Ok(0)` when no hook installed.
12411    #[test]
12412    fn exec_hooks_corpus_execute_script_returns_ok_zero_when_not_installed() {
12413        let _g = crate::test_util::global_state_lock();
12414        let r = execute_script("nothing real");
12415        match r {
12416            Ok(_) | Err(_) => {} // either is acceptable post-install
12417        }
12418    }
12419
12420    /// `run_command_substitution` returns "" by default.
12421    #[test]
12422    fn exec_hooks_corpus_run_command_substitution_default_empty_or_real() {
12423        let _g = crate::test_util::global_state_lock();
12424        // Returns "" if no hook, or real output if hook installed.
12425        let _ = run_command_substitution("echo zshrs_hook_test");
12426        // No panic = pass; we can't pin exact result because hook
12427        // state depends on previous tests in same process.
12428    }
12429
12430    /// `array` falls back to params::getaparam when no hook.
12431    /// Set a real array via params, then look up through hook entry.
12432    #[test]
12433    fn exec_hooks_corpus_array_falls_back_to_getaparam() {
12434        let _g = crate::test_util::global_state_lock();
12435        crate::ported::params::unsetparam("EH_FB");
12436        crate::ported::params::setaparam("EH_FB", vec!["x".into(), "y".into(), "z".into()]);
12437        let got = array("EH_FB");
12438        assert_eq!(
12439            got.as_deref(),
12440            Some(&["x".to_string(), "y".to_string(), "z".to_string()][..]),
12441            "array() hook falls back to params::getaparam",
12442        );
12443        crate::ported::params::unsetparam("EH_FB");
12444    }
12445
12446    /// `pparams()` returns empty Vec when no hook installed.
12447    #[test]
12448    fn exec_hooks_corpus_pparams_returns_empty_when_not_installed() {
12449        let _g = crate::test_util::global_state_lock();
12450        let p = pparams();
12451        // Either empty (no hook) or whatever the installed hook returns.
12452        let _ = p; // no panic = pass
12453    }
12454
12455    /// `unregister_function` returns false by default.
12456    #[test]
12457    fn exec_hooks_corpus_unregister_function_default_false() {
12458        let _g = crate::test_util::global_state_lock();
12459        let r = unregister_function("__never_registered_xyz__");
12460        // If hook installed, hook decides; if not, returns false.
12461        // Pin: doesn't panic and returns a bool.
12462        let _ = r;
12463    }
12464
12465    /// `set_pparams` doesn't panic when called.
12466    #[test]
12467    fn exec_hooks_corpus_set_pparams_does_not_panic() {
12468        let _g = crate::test_util::global_state_lock();
12469        set_pparams(vec!["a".into(), "b".into()]);
12470        // No panic = pass.
12471    }
12472
12473    // ═══════════════════════════════════════════════════════════════════
12474    // Additional default-path (no-hook-installed) parity tests.
12475    // exec_hooks fallback semantics must remain stable: every accessor
12476    // returns a safe default when no fusevm executor has wired its hook.
12477    // ═══════════════════════════════════════════════════════════════════
12478
12479    /// `assoc()` returns None when no hook installed (no params
12480    /// fallback — assoc has no equivalent of getaparam fallback).
12481    #[test]
12482    fn exec_hooks_assoc_returns_none_when_no_hook() {
12483        let _g = crate::test_util::global_state_lock();
12484        // If a prior test installed a hook, the result is hook-defined;
12485        // we only pin no-panic + valid Option<...>.
12486        let _ = assoc("__never_real_assoc_zshrs__");
12487    }
12488
12489    /// `set_array` is a no-op when no hook installed (silently
12490    /// drops the write rather than panicking — fusevm-less env safe).
12491    #[test]
12492    fn exec_hooks_set_array_no_hook_does_not_panic() {
12493        let _g = crate::test_util::global_state_lock();
12494        set_array("__never_real_array_zshrs__", vec!["x".into(), "y".into()]);
12495    }
12496
12497    /// `set_assoc` is a no-op when no hook installed.
12498    #[test]
12499    fn exec_hooks_set_assoc_no_hook_does_not_panic() {
12500        let _g = crate::test_util::global_state_lock();
12501        let mut m = IndexMap::new();
12502        m.insert("k".to_string(), "v".to_string());
12503        set_assoc("__never_real_assoc_zshrs__", m);
12504    }
12505
12506    /// `unset_scalar`, `unset_array`, `unset_assoc` are all no-ops
12507    /// when no hook installed.
12508    #[test]
12509    fn exec_hooks_unset_variants_no_hook_dont_panic() {
12510        let _g = crate::test_util::global_state_lock();
12511        unset_scalar("__never_real_scalar_zshrs__");
12512        unset_array("__never_real_array_zshrs__");
12513        unset_assoc("__never_real_assoc_zshrs__");
12514    }
12515
12516    /// `run_function_body` returns None when no hook installed.
12517    #[test]
12518    fn exec_hooks_run_function_body_returns_none_when_no_hook() {
12519        let _g = crate::test_util::global_state_lock();
12520        let _ = run_function_body("__never_a_real_fn_zshrs__", &["a".into()]);
12521        // No panic = pass; result is Option-typed.
12522    }
12523
12524    /// `execute_script_zsh_pipeline` returns Ok(0) when no hook installed.
12525    #[test]
12526    fn exec_hooks_execute_script_zsh_pipeline_default_ok_zero() {
12527        let _g = crate::test_util::global_state_lock();
12528        let r = execute_script_zsh_pipeline("no hook");
12529        // If hook installed, result is hook-defined; if not, Ok(0).
12530        let _ = r;
12531    }
12532
12533    /// `array()` returns None when name doesn't exist in params either
12534    /// (no fallback hits).
12535    #[test]
12536    fn exec_hooks_array_returns_none_for_missing_name() {
12537        let _g = crate::test_util::global_state_lock();
12538        crate::ported::params::unsetparam("__no_such_array_zshrs__");
12539        let got = array("__no_such_array_zshrs__");
12540        // Either None (no hook + no param) or hook-returned value.
12541        let _ = got;
12542    }
12543
12544    /// `array()` empty name doesn't panic (some callers pass "" for
12545    /// special parameter probes).
12546    #[test]
12547    fn exec_hooks_array_empty_name_does_not_panic() {
12548        let _g = crate::test_util::global_state_lock();
12549        let _ = array("");
12550    }
12551
12552    /// Idempotent: calling array() twice with same name yields same
12553    /// result (no observable side effect on the fallback path).
12554    #[test]
12555    fn exec_hooks_array_is_idempotent() {
12556        let _g = crate::test_util::global_state_lock();
12557        crate::ported::params::unsetparam("EH_IDEMPOTENT");
12558        crate::ported::params::setaparam("EH_IDEMPOTENT", vec!["a".into(), "b".into()]);
12559        let first = array("EH_IDEMPOTENT");
12560        let second = array("EH_IDEMPOTENT");
12561        assert_eq!(first, second);
12562        crate::ported::params::unsetparam("EH_IDEMPOTENT");
12563    }
12564
12565    /// `pparams()` returns an empty vec (not None) when no hook —
12566    /// callers can iterate without an Option-check.
12567    #[test]
12568    fn exec_hooks_pparams_returns_vec_not_none() {
12569        let _g = crate::test_util::global_state_lock();
12570        // Type assertion: result is Vec<String>, not Option<Vec<String>>.
12571        let p: Vec<String> = pparams();
12572        let _ = p; // either [] or hook-installed value
12573    }
12574
12575    /// Round-trip via params fallback: set then read returns same value.
12576    #[test]
12577    fn exec_hooks_array_set_get_roundtrip_via_params() {
12578        let _g = crate::test_util::global_state_lock();
12579        crate::ported::params::unsetparam("EH_RT");
12580        let vals = vec!["one".to_string(), "two".to_string(), "three".to_string()];
12581        crate::ported::params::setaparam("EH_RT", vals.clone());
12582        let got = array("EH_RT").expect("set then get should hit params fallback");
12583        assert_eq!(got, vals);
12584        crate::ported::params::unsetparam("EH_RT");
12585    }
12586
12587    /// `unregister_function` is consistently a bool — pin the return
12588    /// type so accidental refactor to () would fail the type check.
12589    #[test]
12590    fn exec_hooks_unregister_function_returns_bool() {
12591        let _g = crate::test_util::global_state_lock();
12592        let r: bool = unregister_function("__never_xyz_zshrs__");
12593        let _ = r;
12594    }
12595
12596    // ═══════════════════════════════════════════════════════════════════
12597    // Additional contract-pin tests for exec_hooks default behavior.
12598    // ═══════════════════════════════════════════════════════════════════
12599
12600    /// `run_command_substitution` returns String (never None / never Option).
12601    #[test]
12602    fn exec_hooks_run_command_substitution_returns_string_type() {
12603        let _g = crate::test_util::global_state_lock();
12604        let _: String = run_command_substitution("anything");
12605    }
12606
12607    /// `execute_script` returns Result<i32, String>. Pin signature.
12608    #[test]
12609    fn exec_hooks_execute_script_returns_result_type() {
12610        let _g = crate::test_util::global_state_lock();
12611        let _: Result<i32, String> = execute_script("anything");
12612    }
12613
12614    /// `execute_script_zsh_pipeline` returns Result<i32, String>.
12615    #[test]
12616    fn exec_hooks_execute_script_zsh_pipeline_returns_result_type() {
12617        let _g = crate::test_util::global_state_lock();
12618        let _: Result<i32, String> = execute_script_zsh_pipeline("anything");
12619    }
12620
12621    /// `dispatch_function_call` returns Option<i32>.
12622    #[test]
12623    fn exec_hooks_dispatch_function_call_returns_option_i32() {
12624        let _g = crate::test_util::global_state_lock();
12625        let _: Option<i32> = dispatch_function_call("__never_real__", &[]);
12626    }
12627
12628    /// `run_function_body` returns Option<i32>.
12629    #[test]
12630    fn exec_hooks_run_function_body_returns_option_i32() {
12631        let _g = crate::test_util::global_state_lock();
12632        let _: Option<i32> = run_function_body("__never_real__", &[]);
12633    }
12634
12635    /// `array` returns Option<Vec<String>>.
12636    #[test]
12637    fn exec_hooks_array_returns_option_vec_string() {
12638        let _g = crate::test_util::global_state_lock();
12639        let _: Option<Vec<String>> = array("anything");
12640    }
12641
12642    /// `assoc` returns Option<IndexMap<String, String>>.
12643    #[test]
12644    fn exec_hooks_assoc_returns_option_indexmap() {
12645        let _g = crate::test_util::global_state_lock();
12646        let _: Option<IndexMap<String, String>> = assoc("anything");
12647    }
12648
12649    /// Empty-string args to `dispatch_function_call` doesn't panic.
12650    #[test]
12651    fn exec_hooks_dispatch_empty_args_no_panic() {
12652        let _g = crate::test_util::global_state_lock();
12653        let _ = dispatch_function_call("", &[]);
12654        let _ = dispatch_function_call("name", &[]);
12655    }
12656
12657    /// Empty-string args to `run_function_body` doesn't panic.
12658    #[test]
12659    fn exec_hooks_run_function_body_empty_args_no_panic() {
12660        let _g = crate::test_util::global_state_lock();
12661        let _ = run_function_body("", &[]);
12662        let _ = run_function_body("name", &[]);
12663    }
12664
12665    /// Empty-string name to `execute_script` doesn't panic and returns
12666    /// Result.
12667    #[test]
12668    fn exec_hooks_execute_script_empty_src_no_panic() {
12669        let _g = crate::test_util::global_state_lock();
12670        let _ = execute_script("");
12671        let _ = execute_script_zsh_pipeline("");
12672    }
12673
12674    /// Empty-string cmd to `run_command_substitution` doesn't panic.
12675    #[test]
12676    fn exec_hooks_run_command_substitution_empty_no_panic() {
12677        let _g = crate::test_util::global_state_lock();
12678        let _: String = run_command_substitution("");
12679    }
12680
12681    /// `unregister_function("")` doesn't panic.
12682    #[test]
12683    fn exec_hooks_unregister_function_empty_name_no_panic() {
12684        let _g = crate::test_util::global_state_lock();
12685        let _: bool = unregister_function("");
12686    }
12687
12688    /// `unset_*` with empty name does not panic.
12689    #[test]
12690    fn exec_hooks_unset_with_empty_name_no_panic() {
12691        let _g = crate::test_util::global_state_lock();
12692        unset_scalar("");
12693        unset_array("");
12694        unset_assoc("");
12695    }
12696
12697    // ═══════════════════════════════════════════════════════════════════
12698    // Additional contract-pin tests for exec_hooks fallback semantics
12699    // c:213 pparams / c:217 set_pparams / c:223 unregister_function
12700    // ═══════════════════════════════════════════════════════════════════
12701
12702    /// `pparams()` is deterministic for repeated calls without state changes.
12703    #[test]
12704    fn pparams_deterministic_without_changes() {
12705        let _g = crate::test_util::global_state_lock();
12706        let first = pparams();
12707        for _ in 0..3 {
12708            assert_eq!(
12709                pparams(),
12710                first,
12711                "pparams() must be deterministic across reads"
12712            );
12713        }
12714    }
12715
12716    /// `pparams()` returns Vec<String> (not Option) — pin type contract.
12717    #[test]
12718    fn pparams_returns_vec_string_no_option() {
12719        let _g = crate::test_util::global_state_lock();
12720        let _: Vec<String> = pparams();
12721    }
12722
12723    /// `array(name)` with name containing null-byte doesn't panic.
12724    #[test]
12725    fn array_with_special_chars_in_name_no_panic() {
12726        let _g = crate::test_util::global_state_lock();
12727        let _ = array("name with spaces");
12728        let _ = array("name/with/slashes");
12729        let _ = array("$dollarsigns");
12730    }
12731
12732    /// `assoc(name)` empty name no panic.
12733    #[test]
12734    fn assoc_empty_name_no_panic() {
12735        let _g = crate::test_util::global_state_lock();
12736        let _ = assoc("");
12737    }
12738
12739    /// `set_array(empty, ...)` empty name no panic.
12740    #[test]
12741    fn set_array_empty_name_no_panic() {
12742        let _g = crate::test_util::global_state_lock();
12743        set_array("", vec![]);
12744    }
12745
12746    /// `set_assoc(empty, ...)` empty name no panic.
12747    #[test]
12748    fn set_assoc_empty_name_no_panic() {
12749        let _g = crate::test_util::global_state_lock();
12750        let m = indexmap::IndexMap::new();
12751        set_assoc("", m);
12752    }
12753
12754    /// `set_pparams(empty)` is safe.
12755    #[test]
12756    fn set_pparams_empty_vec_no_panic() {
12757        let _g = crate::test_util::global_state_lock();
12758        set_pparams(vec![]);
12759    }
12760
12761    /// Repeated `pparams()` doesn't allocate growing state.
12762    #[test]
12763    fn pparams_repeated_doesnt_grow_state() {
12764        let _g = crate::test_util::global_state_lock();
12765        let first_len = pparams().len();
12766        for _ in 0..10 {
12767            let n = pparams().len();
12768            assert_eq!(n, first_len, "len must not grow across reads");
12769        }
12770    }
12771
12772    /// `unregister_function` is deterministic for nonexistent name.
12773    #[test]
12774    fn unregister_function_unknown_deterministic() {
12775        let _g = crate::test_util::global_state_lock();
12776        let first = unregister_function("__never_real_xyz__");
12777        for _ in 0..3 {
12778            assert_eq!(unregister_function("__never_real_xyz__"), first);
12779        }
12780    }
12781
12782    /// `array(name)` repeated reads of nonexistent name are deterministic.
12783    #[test]
12784    fn array_unknown_is_deterministic() {
12785        let _g = crate::test_util::global_state_lock();
12786        let first = array("__never_real_array_xyz__").is_none();
12787        for _ in 0..3 {
12788            assert_eq!(array("__never_real_array_xyz__").is_none(), first);
12789        }
12790    }
12791
12792    /// `assoc(name)` repeated reads of nonexistent name are deterministic.
12793    #[test]
12794    fn assoc_unknown_is_deterministic() {
12795        let _g = crate::test_util::global_state_lock();
12796        let first = assoc("__never_real_assoc_xyz__").is_none();
12797        for _ in 0..3 {
12798            assert_eq!(assoc("__never_real_assoc_xyz__").is_none(), first);
12799        }
12800    }
12801
12802    // ═══════════════════════════════════════════════════════════════════
12803    // Additional C-parity tests for src/ported/exec_hooks.rs
12804    // c:134 array / c:147 assoc / c:181 dispatch_function_call /
12805    // c:188 run_function_body / c:192 execute_script /
12806    // c:206 run_command_substitution / c:213 pparams / c:223 unregister_function
12807    // ═══════════════════════════════════════════════════════════════════
12808
12809    /// `array(name)` returns Option<Vec<String>> (compile-time pin).
12810    #[test]
12811    fn array_returns_option_vec_string_type() {
12812        let _g = crate::test_util::global_state_lock();
12813        let _: Option<Vec<String>> = array("any");
12814    }
12815
12816    /// `assoc(name)` returns Option<IndexMap<String,String>> (compile-time pin).
12817    #[test]
12818    fn assoc_returns_option_indexmap_type() {
12819        let _g = crate::test_util::global_state_lock();
12820        let _: Option<indexmap::IndexMap<String, String>> = assoc("any");
12821    }
12822
12823    /// `dispatch_function_call` returns Option<i32> (compile-time pin).
12824    #[test]
12825    fn dispatch_function_call_returns_option_i32_type() {
12826        let _g = crate::test_util::global_state_lock();
12827        let _: Option<i32> = dispatch_function_call("__never__", &[]);
12828    }
12829
12830    /// `run_function_body` returns Option<i32> (compile-time pin).
12831    #[test]
12832    fn run_function_body_returns_option_i32_type() {
12833        let _g = crate::test_util::global_state_lock();
12834        let _: Option<i32> = run_function_body("__never__", &[]);
12835    }
12836
12837    /// `execute_script` returns Result<i32, String> (compile-time pin).
12838    #[test]
12839    fn execute_script_returns_result_type() {
12840        let _g = crate::test_util::global_state_lock();
12841        let _: Result<i32, String> = execute_script("");
12842    }
12843
12844    /// `execute_script_zsh_pipeline` returns Result<i32, String> (compile-time pin).
12845    #[test]
12846    fn execute_script_zsh_pipeline_returns_result_type() {
12847        let _g = crate::test_util::global_state_lock();
12848        let _: Result<i32, String> = execute_script_zsh_pipeline("");
12849    }
12850
12851    /// `run_command_substitution` returns String (compile-time pin).
12852    #[test]
12853    fn run_command_substitution_returns_string_type() {
12854        let _g = crate::test_util::global_state_lock();
12855        let _: String = run_command_substitution("");
12856    }
12857
12858    /// `pparams` returns Vec<String> (compile-time pin).
12859    #[test]
12860    fn pparams_returns_vec_string_type() {
12861        let _g = crate::test_util::global_state_lock();
12862        let _: Vec<String> = pparams();
12863    }
12864
12865    /// `unregister_function` returns bool (compile-time pin).
12866    #[test]
12867    fn unregister_function_returns_bool_type() {
12868        let _g = crate::test_util::global_state_lock();
12869        let _: bool = unregister_function("__never__");
12870    }
12871
12872    /// `unset_scalar`/`unset_array`/`unset_assoc` for nonexistent name safe.
12873    #[test]
12874    fn unset_variants_nonexistent_no_panic() {
12875        let _g = crate::test_util::global_state_lock();
12876        unset_scalar("__never_unset_scalar__");
12877        unset_array("__never_unset_array__");
12878        unset_assoc("__never_unset_assoc__");
12879    }
12880
12881    /// `set_pparams` with no hook installed is a silent no-op
12882    /// (c:217-220 — `if let Some(f) = PPARAMS_SET.get() { f(v); }`).
12883    /// Pin the no-hook contract so a refactor that panics on missing
12884    /// hook gets caught.
12885    #[test]
12886    fn set_pparams_without_hook_is_silent_noop() {
12887        let _g = crate::test_util::global_state_lock();
12888        // No hook installed in test context — must not panic.
12889        set_pparams(vec!["a".into(), "b".into(), "c".into()]);
12890        set_pparams(vec![]);
12891    }
12892
12893    // ═══════════════════════════════════════════════════════════════════
12894    // Additional contract pins for exec_hooks.rs
12895    // No-hook-installed contract: every accessor must be safe + deterministic
12896    // ═══════════════════════════════════════════════════════════════════
12897
12898    /// `array("")` empty name returns deterministic value (no panic).
12899    #[test]
12900    fn array_empty_name_no_panic_deterministic() {
12901        let _g = crate::test_util::global_state_lock();
12902        let a = array("");
12903        let b = array("");
12904        assert_eq!(a, b, "array(\"\") must be deterministic");
12905    }
12906
12907    /// `set_array` then `array` without hook should not panic.
12908    #[test]
12909    fn set_array_then_get_no_hook_safe() {
12910        let _g = crate::test_util::global_state_lock();
12911        set_array("__test_hook_arr__", vec!["a".into(), "b".into()]);
12912        let _ = array("__test_hook_arr__");
12913    }
12914
12915    /// `set_assoc` then `assoc` without hook should not panic.
12916    #[test]
12917    fn set_assoc_then_get_no_hook_safe() {
12918        let _g = crate::test_util::global_state_lock();
12919        let mut m = IndexMap::new();
12920        m.insert("k".to_string(), "v".to_string());
12921        set_assoc("__test_hook_assoc__", m);
12922        let _ = assoc("__test_hook_assoc__");
12923    }
12924
12925    /// `run_function_body` with no hook returns `Option<i32>` (type pin, alt).
12926    #[test]
12927    fn run_function_body_returns_option_i32_type_alt() {
12928        let _g = crate::test_util::global_state_lock();
12929        let _: Option<i32> = run_function_body("foo", &[]);
12930    }
12931
12932    /// `run_function_body` is deterministic for the same input when no hook.
12933    #[test]
12934    fn run_function_body_no_hook_deterministic() {
12935        let _g = crate::test_util::global_state_lock();
12936        let a = run_function_body("__never__", &[]);
12937        let b = run_function_body("__never__", &[]);
12938        assert_eq!(a, b);
12939    }
12940
12941    /// `dispatch_function_call` is deterministic across calls.
12942    #[test]
12943    fn dispatch_function_call_no_hook_deterministic() {
12944        let _g = crate::test_util::global_state_lock();
12945        let a = dispatch_function_call("__never__", &[]);
12946        let b = dispatch_function_call("__never__", &[]);
12947        assert_eq!(a, b);
12948    }
12949
12950    /// `pparams` returns `Vec<String>` (compile-time pin, alt).
12951    #[test]
12952    fn pparams_returns_vec_string_type_alt() {
12953        let _g = crate::test_util::global_state_lock();
12954        let _: Vec<String> = pparams();
12955    }
12956
12957    /// `pparams` is deterministic across repeated reads when no hook installed
12958    /// (this is observably true only if no other test has installed it; pin
12959    /// the no-mutation invariant).
12960    #[test]
12961    fn pparams_repeated_reads_are_observable_type() {
12962        let _g = crate::test_util::global_state_lock();
12963        // Just type pin — value depends on whether another test installed a
12964        // hook between these calls (PPARAMS_GET is OnceLock).
12965        let _a = pparams();
12966        let _b = pparams();
12967    }
12968
12969    /// `run_command_substitution` returns `String` type pin (alt).
12970    #[test]
12971    fn run_command_substitution_returns_string_type_alt() {
12972        let _g = crate::test_util::global_state_lock();
12973        let _: String = run_command_substitution("echo x");
12974    }
12975
12976    /// `run_command_substitution` empty command doesn't panic.
12977    #[test]
12978    fn run_command_substitution_empty_cmd_no_panic() {
12979        let _g = crate::test_util::global_state_lock();
12980        let _ = run_command_substitution("");
12981    }
12982
12983    /// `execute_script` returns `Result<i32, String>` type pin.
12984    #[test]
12985    fn execute_script_returns_result_i32_string_type() {
12986        let _g = crate::test_util::global_state_lock();
12987        let _: Result<i32, String> = execute_script("foo");
12988    }
12989
12990    /// `unregister_function("")` empty name returns bool safely.
12991    #[test]
12992    fn unregister_function_empty_name_returns_bool() {
12993        let _g = crate::test_util::global_state_lock();
12994        let _: bool = unregister_function("");
12995    }
12996}