Skip to content

WIP: compgen族 #1794-#1799 (user/group数据库, command组序, -C COMP_*, -W IFS, compopt帧, variable集合) - #1829

Merged
caomengxuan666 merged 6 commits into
masterfrom
fix/compgen-1794-1799
Oct 27, 2026
Merged

caomengxuan666 merged 6 commits into
masterfrom
fix/compgen-1794-1799

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

WIP: compgen/compopt builtin divergence clearance (diff-hunt adjudicated, GNU bash 5.3.20 oracle). One commit per issue; cluster report in the wave workspace.

Fixes #1794
Fixes #1795
Fixes #1796
Fixes #1797
Fixes #1798
Fixes #1799

…unt databases

GNU lib/readline/complete.c:2377 rl_username_completion_function walks
getpwent() entries in database order, and bashline.c:2723
bash_groupname_completion_function walks getgrent(); neither consults
the environment. rubash returned only USER/LOGNAME/USERNAME (resp.
GROUP/GROUPNAME/USERDOMAIN) values, and nothing when those were unset.

On unix the generators now parse /etc/passwd resp. /etc/group first
colon fields in file order, mirroring the #1044 services walk.
Windows has no such text database, so the environment fallback is
kept there as a deliberate platform divergence.

Regression: tests/issue1794_user_group_completion.rs (database
self-oracle, prefix filtering, env-only names excluded).

Refs #1794
bashline.c:2005 command_word_completion_function maps over its
sources in a fixed order: case 0 aliases, case 1 shell reserved
words (word_token_alist order), case 2 function names, case 3 shell
builtins (shell_builtins table order), then the PATH walk.
command_completion_candidates emitted builtins and keywords first.

The SHELL_KEYWORDS/SHELL_BUILTINS tables already match the oracle's
table order verbatim (compgen -A keyword / -A builtin full dumps on
GNU bash 5.3.20); only the group emission order changes. The #1089
no-dedup rule across sources is untouched.

Regression: tests/issue1795_command_completion_order.rs (empty PATH
dir isolates the groups; alias leads; function sits between keywords
and builtins; PATH echo not deduplicated).

Refs #1795
pcomplete.c:929 bind_compfunc_variables binds COMP_WORDS and
COMP_CWORD only in the exported==0 (function) arm; gen_command_matches
(pcomplete.c:1156) calls it with exported=1 for -C, binding only
COMP_LINE/COMP_POINT/COMP_TYPE/COMP_KEY. rubash bound COMP_CWORD=0
(and an empty COMP_WORDS when unset) for -C too, so the command
observed a cword GNU never sets. Oracle-verified on GNU bash 5.3.20:
unset in a fresh shell, caller presets pass through untouched,
unbind_compfunc_variables (pcomplete.c:976) removes all six either
way.

bind_compfunc_variables gains the exported parameter; the -F caller
passes false (the #1088 COMP_WORDS pass-through stays in the
function arm), the -C caller passes true. The live-completion
binding path is unchanged.

Regression: tests/issue1796_compgen_command_comp_vars.rs.

Refs #1796
pcomplete.c:863 gen_wordlist_matches splits with subst.c:2616
split_at_delims at delims == NULL, which reads ifs_value: the
current $IFS (tempenv frame included), defaulting to space/tab/
newline, empty meaning no splitting. With delims == NULL the
empty-field branch never fires, so a maximal delimiter run is one
separator and no empty fields appear. Grouping follows subst.c:2199
skip_to_delim. rubash split on shell whitespace only: a newline in
the wordlist never split and a tempenv IFS was ignored.

expand_compgen_wordlist now reads the executor's current IFS and
splits with a new split_at_delims port (split_compgen_wordlist):
pieces keep their quotes and a top-level-quoted mark, then flow
through the same two shellexp arms as before. The shared
alias_helpers splitter is untouched. Side effect matching GNU: an
escaped-space piece (a\ b) now stays one field instead of being
re-split by the unquoted arm.

Regression: tests/issue1797_compgen_wordlist_ifs.rs (all shapes
produced by the GNU bash 5.3.20 oracle).

Refs #1797
…riables

pcomplete.c it_init_variables lists variables.c:4319
all_visible_variables(). Three set differences (oracle-verified on
GNU bash 5.3.20): HISTCMD is registered by variables.c:1873
INIT_DYNAMIC_VAR and always lists, but was missing; PIPESTATUS has
no cell until variables.c:6291 set_pipestatus_array materializes
one (any completed command does), yet was always listed; THIS_SH is
not a GNU variable, yet the engine-injected spelling leaked in,
while a caller-supplied value that init keeps (sh/bash wrapper)
must list like any imported variable.

The appended dynamic list drops PIPESTATUS and gains HISTCMD.
PIPESTATUS joins only while a visible cell lives, via a new
CompgenDynamic.pipestatus fact the executor reads from the cell
shape: -A variable takes {LiveArray, Scalar}, -A arrayvar takes
{LiveArray} (its unconditional removal contradicted the cell rule
in its own comment and the post-pipeline oracle listing). THIS_SH
is hidden exactly when its value equals the injected current-exe
spelling.

Regression: tests/issue1799_variable_completion_set.rs.

Refs #1799
GNU installs a builtin's redirections before it runs
(execute_cmd.c do_redirections), so everything produced during
compgen's resolution — the -F/-C warning, the completion
function's own stdout/stderr writes, a nested compopt's 'not
currently executing completion function' diagnostic
(complete.def:870, gated on a completion flag compgen never
sets), an external command's stderr, the -C command's stderr —
travels through the compgen command's fd 1/2 frame, in
execution order; the -C command's stdout is still captured as
candidates.

rubash's buffered model deferred the builtin's redirects to
flush time: the nested compopt diagnostic went to the real
stderr even under 2>/dev/null, the function's own writes leaked
likewise, and the buffered end-of-builtin flush re-opened the
targets and truncated the -F warning away whenever the function
existed (the #1090/#1041 hazard realized on the will_run path).

execute_completion_builtin now binds the command's own fd 1/2
redirects into the fd table for the resolution and the final
output when a -F/-C target will run and the command carries
such redirects: the warning is written through the frame
first, resolution runs under it (write_default_stdout/stderr
and the fd machinery honor it natively, and command
substitution still rebinds fd 1 for the -C capture), the saved
table is restored on every exit path, and the buffered flush is
skipped while framed. A redirect that fails to open aborts the
builtin before the function runs, as GNU's do_redirections
failure does. Commands without their own stdio redirects keep
the buffered path untouched (ambient pipeline/capture frames
already resolve correctly there).

Regression: tests/issue1798_compgen_redirect_frame.rs — the
cc08 compopt shape under 2>/dev/null, function stderr/stdout
into the targets in order, -C stderr framed with stdout still
captured, 2>&1 merged ordering, command substitution, and the
bad-target failure.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 27, 2026 08:13
@caomengxuan666
caomengxuan666 merged commit 8549e1a into master Oct 27, 2026
8 of 9 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/compgen-1794-1799 branch October 30, 2026 01:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment