Skip to content

fix(cli): CLI entry semantics — posix -c fatal exit 127, invocation-face $0, -s in $- (closes #589 #590 #591) - #598

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/589-591-cli-entry-semantics
Oct 13, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/589-591-cli-entry-semantics

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

One CLI-entry patch, three tickets, each anchored in the vendored bash source (bash-5.3-16-gb4608166, zero copied code) and probed cell-by-cell against WSL GNU 5.3.0 (wsl -e bash, 2026-10-13).

#589 — posix special-builtin/assignment fatal exits 127 under -c

Root cause. GNU raises the fatal as a FORCE_EOF jump with last_command_exit_value = EXECUTION_FAILURE, and the entry re-maps it: shell.c:1471-1472 run_one_command (case FORCE_EOF: return last_command_exit_value = 127) for -c, while eval.c:104-109 (reader_loop) ends script input and exits with last_command_exit_value (1), and execute_cmd.c:1811 contains the jump at 1 inside subshell children. Producers: subst.c:13177-13182 (expand_word_list_internal is_nullcmd arm → posix_variable_assignment_error(0), subst.c:11181-11196), subst.c:13186-13195 (tempenv arm → bash_variable_assignment_error(is_special_builtin), subst.c:11169-11180), execute_cmd.c:3085-3089 (execute_for_command readonly bind), execute_cmd.c:3587-3592 (execute_select_command readonly bind). Rubash hardcoded ExitCode(1) at every fatal arm.

Fix. The six fatal arms (command_dispatch.rs standalone-assignments + tempenv-special, command_prepare.rs empty-words assignment, loop_select.rs both for-arms, select_exec.rs select arm) now route through expansion_fatal_status() (parameter_errors.rs:845) — the established single owner of the -c→127 / script→1 / subshell→1 mapping.

WSL 5.3.0 matrix (script + -c): nullcmd ro=2 127 (was 1) · tempenv-special ro=2 : 127 (was 1) · for-bind 127 (was 1) · select-bind 127 (was 1) · same-line ro=2; echo post 127 · (ro=2) contained, parent $? = 1, shell rc 0 · ro=2 true nonfatal 0 · x=${ro:=v} 0 · read ro 0 · stdin-script 1 · script file 1 · default mode 0. Every cell matches GNU after the fix.

#590 — $0 follows the invocation face

Root cause. shell.c:1778-1806 set_shell_name binds dollar_vars[0] = argv[0] verbatim (shell.c:1797-1799) and keys the sh/ash (shell.c:733-737) and rbash (shell.c:1258-1299) contracts off its basename. The #504 shim forwarded only sh/ash, so bash.exe -c 'echo $0' lost the face and the host reported its product name.

Fix. forwarded_invocation_name (src/bin/bash.rs) forwards sh/ash/bash/rbash (leading - login marker preserved, .exe stripped; other names still scrub). The engine consumes NIU_INVOKED_AS as the erased argv[0] — read before the process-env import so the name never becomes a shell variable, ranked behind an explicit BASH_ARGV0 (variables.c:1547-1555), feeding both set_shell_name's $0 slot and apply_invocation_shell_mode (posix on for sh, rbash restricted, login marker). Consume-on-read keeps the #504 isolation: a nested sh -c 'bash -c ...' re-forwards its own face.

Probes (shim + engine chain): sh -c 'echo $0' → sh + posix on · bash -c 'echo $0' → bash · rbash -c 'echo $0' → rbash · $NIU_INVOKED_AS invisible in the shell env · BASH_ARGV0=custom still outranks the face · explicit -c operand still wins · stdin script reports the face.

Companion note. Product hosts that consume NIU_INVOKED_AS in their own runtime (niubash Shell::new, per #504/#262) need the same one-line extension to honor the bash stem; this PR keeps the engine-side contract complete and tested (engine honors the variable directly, tests exercise the full shim→engine chain).

#591 — =s shows s in $-

Root cause. flags.c:294-306 which_set_flags appends s while read_from_stdin (shell.c:301) is set; shell.c:928 records the explicit =s at option-parse time. Rubash armed the marker only inside its own stdin drivers, so a library host driving its own stdin reader lost the letter: printf 'echo [$-]' | niu -s printed [hB] against GNU [hBs].

Fix. ShellInvocation::apply_to_executor (src/invocation.rs) arms READ_STDIN_MARKER for every =s invocation — the shared surface every host calls. -c arms nothing ([hBc] both sides), script files neither ([hB]).

Verification

closes #589, closes #590, closes #591

… and -s in $- (closes #589 #590 #591)

#589 -c fatal exit mapping: a POSIX special-builtin/assignment violation
terminated with 1 in every entry shape. GNU jumps FORCE_EOF with
last_command_exit_value = EXECUTION_FAILURE and re-maps it per entry:
shell.c:1471-1472 run_one_command ('case FORCE_EOF: return
last_command_exit_value = 127') for -c, eval.c:104-109 reader_loop keeps
last_command_exit_value (1) for script/stdin, execute_cmd.c:1811 contains
the jump at 1 inside subshell children. Producers: subst.c:13177-13182
(expand_word_list_internal is_nullcmd arm -> posix_variable_assignment_error(0)),
subst.c:13186-13195 (tempenv arm -> bash_variable_assignment_error(is_special)),
subst.c:11169-11196, execute_cmd.c:3085-3089 (execute_for_command readonly
bind), execute_cmd.c:3587-3592 (execute_select_command readonly bind).
WSL 5.3.0 oracle: nullcmd 127, tempenv-special 127, for 127, select 127,
subshell-contained 1, script/stdin 1, ordinary-builtin tempenv nonfatal 0.
The six fatal arms now route through expansion_fatal_status(), the
established context owner of that mapping (parameter_errors.rs:845).

#590 invocation face for $0: GNU set_shell_name (shell.c:1778-1806) binds
dollar_vars[0] = argv[0] (shell.c:1797-1799) and keys the sh/ash
(shell.c:733-737) and rbash (shell.c:1258-1299) contracts off its basename.
The shim forwarded only sh/ash, so 'bash.exe -c echo $0' lost the face and
the host reported its product name. forwarded_invocation_name now forwards
sh/ash/bash/rbash (leading dash preserved, .exe stripped) and the engine
consumes NIU_INVOKED_AS as the erased argv[0] — before the process-env
import, ranked behind an explicit BASH_ARGV0 (variables.c:1547-1555),
feeding set_shell_name's $0 slot and apply_invocation_shell_mode. The
stdin-script driver binds $0 from the resolved face, not the engine path.
Hosts that consume NIU_INVOKED_AS themselves keep their own flow
(niubash#262 companion applies for the bash stem).

#591 -s in $-: flags.c:294-306 which_set_flags appends 's' while
read_from_stdin (shell.c:301) is set; shell.c:928 records the explicit -s
at option-parse time. The engine's own drivers armed the marker inside
run_stdin_script / run_interactive_stdin, so a library host driving its own
stdin reader lost the letter ('printf echo [$-] | niu -s' printed [hB]
against GNU [hBs]). ShellInvocation::apply_to_executor now arms the
read-from-stdin marker for every -s invocation — the shared surface every
host calls.

Tests: tests/issue589_posix_special_fatal_c_exit_regressions.rs (10 cells
against the WSL 5.3.0 matrix), tests/issue590_dollar0_invocation_face_regressions.rs
(9 cells: shim chain per face, consume-on-read, BASH_ARGV0 ranking, -c
operand, stdin-script face), tests/issue591_stdin_script_dollar_minus_regressions.rs
(6 cells: -s/no-operand/-c/script-file shapes, process-local marker,
apply_to_executor library contract). cargo check + -D warnings clean, fmt
applied.
…n-script driver, resolve run_stdin_script conflict
@caomengxuan666
caomengxuan666 merged commit fcf7be1 into master Oct 13, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/589-591-cli-entry-semantics branch October 13, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant