From cb1688f0233d53cfca8078f812c824213a5617fd Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:07:39 -0600 Subject: wip: vi external edit and dot-repeat, in-process cd subshell, perf test The working tree the 2026-11-28 review was written against, committed unchanged so the review fixes can sit on top of it. - vi mode: `v` edits the line in $VISUAL, else $EDITOR (edit_external), and `.` repeats the last change, count and all. - `( cd dir )` runs in-process with the directory saved and restored, instead of forking, and its invalidation of the cached logical cwd. - tests/perf: a start-up cost guard; tests/run.sh runs that directory. - README: set +o pipetag for ASCII builds. - formal/zash-spec.txt regenerated as text. - .clang-tidy: two checks re-enabled (no findings after the change). - tests/scripts/webdocs: a `#` inside a fenced block is not a title. --- src/exec.h | 1 + 1 file changed, 1 insertion(+) (limited to 'src/exec.h') diff --git a/src/exec.h b/src/exec.h index 90bc8c9..88b0b95 100644 --- a/src/exec.h +++ b/src/exec.h @@ -110,6 +110,7 @@ enum { }; void find_command(const char *name, cmdinfo *ci, unsigned flags); +void curdir_invalidate(void); void func_define(const char *name, node *body, arena *owner); bool func_unset(const char *name); void func_each(void (*fn)(const char *name, void *arg), void *arg); -- cgit From 9cdf5f548dc8e1d5c6315b304819bf0fd60f802f Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:18:38 -0600 Subject: fix(exec): run the ERR trap when the last command is exec'd `trap "echo ERR" ERR; /bin/false` printed nothing: the shell replaced itself with the failing program and was gone before the trap could run. The same shortcut made `trap ... ; (/bin/false)` silently skip it. trap_after_last() is true when the EXIT or the ERR trap is set, and both the exec shortcut in run_external() and the in-process subshell shortcut in eval_subshell() now use it instead of trap_has_exit(). tests/zash/errtrap-exec.sh covers the external command, a builtin, a subshell and the two traps together; the first and third lines fail without the change. ksh93 prints ERR for all four. --- src/exec.c | 4 ++-- src/exec.h | 1 + src/trap.c | 13 +++++++++++++ tests/zash/errtrap-exec.exp | 10 ++++++++++ tests/zash/errtrap-exec.sh | 8 ++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 tests/zash/errtrap-exec.exp create mode 100644 tests/zash/errtrap-exec.sh (limited to 'src/exec.h') diff --git a/src/exec.c b/src/exec.c index 0a3b640..43765f6 100644 --- a/src/exec.c +++ b/src/exec.c @@ -957,7 +957,7 @@ static int run_external(simple_cmd *sc, unsigned flags, int in, int out, plan_close(&plan); return 2; } - if ((flags & EV_EXIT) && !async && !trap_has_exit()) { + if ((flags & EV_EXIT) && !async && !trap_after_last()) { /* Nothing comes after this command: become it. */ plan_apply(&plan); exec_path(sc->cmd.path, sc->argv, envp); @@ -1908,7 +1908,7 @@ static int eval_case(node *n, unsigned flags) { * the shell was about to exit anyway. */ static int eval_subshell(node *n, unsigned flags) { - if (!(flags & EV_EXIT) || trap_has_exit()) { + if (!(flags & EV_EXIT) || trap_after_last()) { if (!job_launching && !opt.errexit && subshell_in_process(n)) { simple_cmd sc; varscope vs; diff --git a/src/exec.h b/src/exec.h index 88b0b95..89df201 100644 --- a/src/exec.h +++ b/src/exec.h @@ -215,6 +215,7 @@ void trap_poll(void); void trap_reset_child(void); void trap_reset_exec(void); bool trap_has_exit(void); +bool trap_after_last(void); void trap_run_exit(void); const sigset_t *trap_spawn_defaults(void); int builtin_trap(int argc, char **argv); diff --git a/src/trap.c b/src/trap.c index b72c552..0e5a0a2 100644 --- a/src/trap.c +++ b/src/trap.c @@ -397,6 +397,19 @@ bool trap_has_exit(void) { return trap_action[0] && *trap_action[0]; } +/** + * @brief Must something run after the last command? + * + * The EXIT trap and the ERR trap both run after the command that ends the + * shell, so when either is set the last command may not replace the + * shell: the process would be gone before the trap could run. + * + * @return true if the EXIT or the ERR trap is set. + */ +bool trap_after_last(void) { + return trap_has_exit() || trap_err_set; +} + /** * @brief Run the EXIT trap, once. */ diff --git a/tests/zash/errtrap-exec.exp b/tests/zash/errtrap-exec.exp new file mode 100644 index 0000000..216c976 --- /dev/null +++ b/tests/zash/errtrap-exec.exp @@ -0,0 +1,10 @@ +ERR +[1] +ERR +[1] +ERR +[1] +ERR +BYE +[1] +[status 0] diff --git a/tests/zash/errtrap-exec.sh b/tests/zash/errtrap-exec.sh new file mode 100644 index 0000000..3acf6e1 --- /dev/null +++ b/tests/zash/errtrap-exec.sh @@ -0,0 +1,8 @@ +# The ERR trap runs for the last command too. A failing external command +# as the last thing a shell runs is exec'd, and the shell is gone before +# the trap could run, unless the trap holds that shortcut back. So does a +# subshell that would otherwise run in the parent. +"$TSH" -c 'trap "echo ERR" ERR; /bin/false'; echo "[$?]" +"$TSH" -c 'trap "echo ERR" ERR; false'; echo "[$?]" +"$TSH" -c 'trap "echo ERR" ERR; (/bin/false)'; echo "[$?]" +"$TSH" -c 'trap "echo ERR" ERR; trap "echo BYE" EXIT; /bin/false'; echo "[$?]" -- cgit From 270ac6676c05470223c5fc09fb3230b97acfbe2b Mon Sep 17 00:00:00 2001 From: Sean Kurtz Date: Sat, 28 Nov 2026 00:44:42 -0600 Subject: fix(signal): install SIG_DFL with sigaction, not signal() Two places reset a signal to its default: main() for an inherited SIGCHLD (waitpid() fails with ECHILD when a parent left it ignored) and the pipeline child that writes a long builtin output and must die on SIGPIPE. Both went through signal(), which differs from sigaction() in restart and mask behaviour; they now share trap.c's set_handler() through a small trap_sig_default(), the function trap_reset() already uses. --- src/exec.c | 2 +- src/exec.h | 1 + src/main.c | 2 +- src/trap.c | 11 +++++++++++ 4 files changed, 14 insertions(+), 2 deletions(-) (limited to 'src/exec.h') diff --git a/src/exec.c b/src/exec.c index 43765f6..e7365ba 100644 --- a/src/exec.c +++ b/src/exec.c @@ -1372,7 +1372,7 @@ static void run_deferred(stage *st, int nc, int i, bool stream) { s->out = -1; for (int j = 0; j < nc; j++) close_stage(&st[j]); /* or we would never see EPIPE */ - signal(SIGPIPE, SIG_DFL); + trap_sig_default(SIGPIPE); output direct = {o.buf, o.len, o.len, out, 0}; out_flush(&direct); _exit(0); diff --git a/src/exec.h b/src/exec.h index 89df201..6bbe6f6 100644 --- a/src/exec.h +++ b/src/exec.h @@ -215,6 +215,7 @@ void trap_poll(void); void trap_reset_child(void); void trap_reset_exec(void); bool trap_has_exit(void); +void trap_sig_default(int sig); bool trap_after_last(void); void trap_run_exit(void); const sigset_t *trap_spawn_defaults(void); diff --git a/src/main.c b/src/main.c index 518dc45..8b4b516 100644 --- a/src/main.c +++ b/src/main.c @@ -426,7 +426,7 @@ int main(int argc, char **argv) { stack_init(); /* A parent may leave SIGCHLD ignored, and then waitpid fails with ECHILD * for every child: reset it before anything is spawned. */ - signal(SIGCHLD, SIG_DFL); + trap_sig_default(SIGCHLD); invoked_as = argv[0] ? argv[0] : "zash"; arg0 = invoked_as[0] == '-' ? invoked_as + 1 : invoked_as; if (arg0[0] != '/' && strchr(arg0, '/')) { diff --git a/src/trap.c b/src/trap.c index 0e5a0a2..4e57d7c 100644 --- a/src/trap.c +++ b/src/trap.c @@ -131,6 +131,17 @@ static void set_handler(int sig, void (*handler)(int)) { sigaction(sig, &sa, nullptr); } +/** + * @brief Put a signal back to SIG_DFL. + * + * For the two places that must not inherit or keep a handler: a parent's + * ignored SIGCHLD (waitpid then fails with ECHILD for every child) and a + * pipeline child that must die on SIGPIPE. + * + * @param sig The signal. + */ +void trap_sig_default(int sig) { set_handler(sig, SIG_DFL); } + /** * @brief What a signal does in this shell when no trap is set for it. */ -- cgit