From a839b6aae188f3d86ee84395b93fab819ba31bc1 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 5 Feb 2026 18:45:04 +0900 Subject: [PATCH 1/6] refactor: remove unnecessary transformation for splitting --- completions-core/apache2ctl.bash | 5 ++--- completions-core/bk.bash | 2 +- completions-core/tshark.bash | 6 +++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/completions-core/apache2ctl.bash b/completions-core/apache2ctl.bash index 08077888c99..17e1f6024df 100644 --- a/completions-core/apache2ctl.bash +++ b/completions-core/apache2ctl.bash @@ -6,10 +6,9 @@ _comp_cmd_apache2ctl() _comp_initialize -- "$@" || return local APWORDS - APWORDS=$("$1" 2>&1 >/dev/null | _comp_awk 'NR<2 { print $3; exit }' | - tr "|" " ") + APWORDS=$("$1" 2>&1 >/dev/null | _comp_awk 'NR < 2 { print $3; exit }') - _comp_compgen -- -W "$APWORDS" + _comp_compgen_split -F $' \t\n|' -- "$APWORDS" } && complete -F _comp_cmd_apache2ctl apache2ctl diff --git a/completions-core/bk.bash b/completions-core/bk.bash index 5a9ec1be8a1..e08b32fde5d 100644 --- a/completions-core/bk.bash +++ b/completions-core/bk.bash @@ -7,7 +7,7 @@ _comp_cmd_bk() _comp_initialize -- "$@" || return local BKCMDS=$(bk help topics 2>/dev/null | - _comp_awk '/^ bk/ { print $2 }' | xargs printf '%s ') + _comp_awk '/^ bk/ { print $2 }') _comp_compgen -- -W "$BKCMDS" _comp_compgen -a filedir diff --git a/completions-core/tshark.bash b/completions-core/tshark.bash index 04ed4260a9e..4f32dfdc7cd 100644 --- a/completions-core/tshark.bash +++ b/completions-core/tshark.bash @@ -23,8 +23,8 @@ _comp_cmd_tshark() _comp_compgen -c "${cur#*:}" filedir else [[ -v _comp_cmd_tshark__prefs ]] || - _comp_cmd_tshark__prefs=$("$1" -G defaultprefs 2>/dev/null | command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p' | - tr '\n' ' ') + _comp_cmd_tshark__prefs=$("$1" -G defaultprefs 2>/dev/null | + command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p') : ${prefix:=} _comp_compgen -P "$prefix" -- -W "$_comp_cmd_tshark__prefs" [[ ${COMPREPLY-} == *: ]] && compopt -o nospace @@ -73,7 +73,7 @@ _comp_cmd_tshark() -*O) [[ -v _comp_cmd_tshark__protocols ]] || _comp_cmd_tshark__protocols=$("$1" -G protocols 2>/dev/null | - cut -f 3 | tr '\n' ' ') + cut -f 3) _comp_delimited , -W "$_comp_cmd_tshark__protocols" return ;; From 414e26971789075a188df754fcf2e822faef4686 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 5 Feb 2026 18:45:24 +0900 Subject: [PATCH 2/6] refactor(gcc): simplify a sed expression --- completions-core/gcc.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions-core/gcc.bash b/completions-core/gcc.bash index ed5bc9b3297..6ca8934c72c 100644 --- a/completions-core/gcc.bash +++ b/completions-core/gcc.bash @@ -12,7 +12,7 @@ _comp_cmd_gcc() local cc=$("$1" -print-prog-name=cc1 2>/dev/null) [[ $cc ]] || return _comp_compgen_split -- "$("$cc" --help 2>/dev/null | tr '\t' ' ' | - command sed -e '/^ *-/!d' -e 's/ *-\([^][ <>]*\).*/-\1/')" + command sed -n 's/^ \{1,\}\(-[^][ <>]*\).*/\1/p')" [[ ${COMPREPLY-} == *= ]] && compopt -o nospace else _comp_compgen_filedir From 2e08ec436fb5f8fe3aac1236e75b26483ac0bcec Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 5 Feb 2026 18:46:07 +0900 Subject: [PATCH 3/6] refactor(tshark): remove unnecessary variable initialization The variable "prefix" is explicitly initialized when it is declared, so we do not need to explicitly set it to an empty string on this line. --- completions-core/tshark.bash | 1 - 1 file changed, 1 deletion(-) diff --git a/completions-core/tshark.bash b/completions-core/tshark.bash index 4f32dfdc7cd..dbc354cdcaf 100644 --- a/completions-core/tshark.bash +++ b/completions-core/tshark.bash @@ -25,7 +25,6 @@ _comp_cmd_tshark() [[ -v _comp_cmd_tshark__prefs ]] || _comp_cmd_tshark__prefs=$("$1" -G defaultprefs 2>/dev/null | command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p') - : ${prefix:=} _comp_compgen -P "$prefix" -- -W "$_comp_cmd_tshark__prefs" [[ ${COMPREPLY-} == *: ]] && compopt -o nospace fi From 0f4795974607cf4ba93cb9f32b0318b65325fc31 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 5 Feb 2026 18:52:45 +0900 Subject: [PATCH 4/6] fix(update-rc.d): fix the usage of the X optarg The current implementation does not work for several reasons. * First, for the selection "|" to work, one needs to enclose the entire pattern with "@()". The option argument of the "-X" option is not a regular expression, but an extended glob pattern. * Next, the option argument of the "-X" option should not be singly quoted in the present case because command substitutions in the option argument of "-X" is not going to be expanded. * Also, one also wants to quote the word of the here string. In Bash < 4.4, the word of the here string is subject to word splitting, so it may be affected by IFS. In addition, we actually do not need to use the "tr" command. We can reduce the spawn cost by using "${words[*]}" with an appropriate value of IFS. --- completions-core/update-rc.d.bash | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/completions-core/update-rc.d.bash b/completions-core/update-rc.d.bash index a5040d44724..4c5f6ba82a7 100644 --- a/completions-core/update-rc.d.bash +++ b/completions-core/update-rc.d.bash @@ -17,8 +17,9 @@ _comp_cmd_update_rc_d() options=(-f -n) if [[ $cword -eq 1 || $prev == -* ]]; then + local IFS='|' _comp_compgen -- -W '"${options[@]}" ${services[@]+"${services[@]}"}' \ - -X '$(tr " " "|" <<<${words[@]})' + -X "@(${words[*]}))" elif ((${#services[@]})) && [[ $prev == ?($(tr " " "|" <<<"${services[*]}")) ]]; then _comp_compgen -- -W 'remove defaults start stop' elif [[ $prev == defaults && $cur == [0-9] ]]; then From dd5cf12c4428ecedf4162c7f3c27093faf735ab7 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Thu, 5 Feb 2026 19:12:22 +0900 Subject: [PATCH 5/6] refactor(modules): merge grep/awk/sed processing --- completions-fallback/modules.bash | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/completions-fallback/modules.bash b/completions-fallback/modules.bash index 7e7003468d8..4f7a7164ed6 100644 --- a/completions-fallback/modules.bash +++ b/completions-fallback/modules.bash @@ -53,8 +53,8 @@ _comp_cmd_module() # First parameter on line -- we expect it to be a mode selection local options - options=$(module help 2>&1 | command grep -E '^[[:space:]]*\+' | - _comp_awk '{print $2}' | command sed -e 's/|/ /g' | sort) + options=$(module help 2>&1 | _comp_awk '/^[[:space:]]*\+/ { + $0 = $2; gsub(/\|/, " "); print }' | sort) _comp_compgen -- -W "$options" From dfacd2beb74be7cdeb43d49cbeffb79f2cfe2a5a Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Fri, 13 Feb 2026 08:23:20 +0900 Subject: [PATCH 6/6] fix(tshark): rename "_cmp_cmd_tshark__{ => mut_}{prefs,protocols}" "_comp_cmd_tshark__{prefs,protocols}" are mutable global variables used to cache the prefixes and protocols, so they should have "_mut_" in the variable names. --- completions-core/tshark.bash | 12 ++++++------ test/t/test_tshark.py | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/completions-core/tshark.bash b/completions-core/tshark.bash index dbc354cdcaf..edea1f3341d 100644 --- a/completions-core/tshark.bash +++ b/completions-core/tshark.bash @@ -22,10 +22,10 @@ _comp_cmd_tshark() if [[ $cur == *:* ]]; then _comp_compgen -c "${cur#*:}" filedir else - [[ -v _comp_cmd_tshark__prefs ]] || - _comp_cmd_tshark__prefs=$("$1" -G defaultprefs 2>/dev/null | + [[ -v _comp_cmd_tshark__mut_prefs ]] || + _comp_cmd_tshark__mut_prefs=$("$1" -G defaultprefs 2>/dev/null | command sed -ne 's/^#\{0,1\}\([a-z0-9_.-]\{1,\}:\).*/\1/p') - _comp_compgen -P "$prefix" -- -W "$_comp_cmd_tshark__prefs" + _comp_compgen -P "$prefix" -- -W "$_comp_cmd_tshark__mut_prefs" [[ ${COMPREPLY-} == *: ]] && compopt -o nospace fi return @@ -70,10 +70,10 @@ _comp_cmd_tshark() return ;; -*O) - [[ -v _comp_cmd_tshark__protocols ]] || - _comp_cmd_tshark__protocols=$("$1" -G protocols 2>/dev/null | + [[ -v _comp_cmd_tshark__mut_protocols ]] || + _comp_cmd_tshark__mut_protocols=$("$1" -G protocols 2>/dev/null | cut -f 3) - _comp_delimited , -W "$_comp_cmd_tshark__protocols" + _comp_delimited , -W "$_comp_cmd_tshark__mut_protocols" return ;; -*T) diff --git a/test/t/test_tshark.py b/test/t/test_tshark.py index d44e62d48c7..d7799323340 100644 --- a/test/t/test_tshark.py +++ b/test/t/test_tshark.py @@ -1,7 +1,6 @@ import pytest -@pytest.mark.bashcomp(ignore_env=r"^\+_comp_cmd_tshark__pr(ef|otocol)s=") class TestTshark: @pytest.mark.complete("tshark -", require_cmd=True) def test_1(self, completion):