From 7fcf0c6b33e3f2bf0b51f50430aa9a4b076bdadf Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Sat, 11 Sep 2021 15:10:08 +0900 Subject: [PATCH 01/10] fix(badblocks): protect against `failglob` --- completions/badblocks | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions/badblocks b/completions/badblocks index 431723b1200..108b8915877 100644 --- a/completions/badblocks +++ b/completions/badblocks @@ -17,7 +17,7 @@ _comp_cmd_badblocks() if [[ $cur == -* ]]; then # Filter out -w (dangerous) and -X (internal use) - COMPREPLY=($(compgen -X -[wX] -W '$(_parse_usage "$1")' -- "$cur")) + COMPREPLY=($(compgen -X '-[wX]' -W '$(_parse_usage "$1")' -- "$cur")) return fi From a3071f3ec307db96a7d63e258094c2a4743e8624 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Mon, 1 May 2023 17:29:32 +0900 Subject: [PATCH 02/10] fix(complete): remove empty elements --- completions/complete | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions/complete b/completions/complete index 772aa0655c8..0e3f9d77162 100644 --- a/completions/complete +++ b/completions/complete @@ -40,7 +40,7 @@ _comp_cmd_complete() local -a opts=($(compgen -W '$(_parse_usage help "-s $1")' -- "$cur")) # -F, -C do not work the expected way with compgen [[ $1 != *compgen ]] || opts=("${opts[@]//-[FC]/}") - COMPREPLY=($(compgen -W '"${opts[@]}"' -- "$cur")) + COMPREPLY=($(compgen -W '"${opts[@]}"' -X '' -- "$cur")) else COMPREPLY=($(compgen -A command -- "$cur")) fi From 86f913455936b064b9aea9f5876a6a41d3667925 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Mon, 1 May 2023 18:33:20 +0900 Subject: [PATCH 03/10] fix(_adb): avoid non-POSIX \? in BRE POSIX Basic Regular Expressions (BRE) does not support \?. Instead, we can use \{0,1\}. Also, `grep` and `sed` can be combined. --- completions/_adb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/completions/_adb b/completions/_adb index d1e3a8af5c6..f44c457fea1 100644 --- a/completions/_adb +++ b/completions/_adb @@ -5,9 +5,9 @@ _comp_cmd_adb__command_usage() { - COMPREPLY=($(compgen -W \ - '$("$1" help 2>&1 | command grep "^ *\(adb \)\? *$2 " \ - | command sed -e "s/[]|[]/\n/g" | _parse_help -)' -- "$cur")) + COMPREPLY=($(compgen -W '$("$1" help 2>&1 | \ + command sed -e "/^ *\(adb \)\{0,1\} *$2 /!d;s/[]|[]/\n/g" | \ + _parse_help -)' -- "$cur")) } _comp_cmd_adb() From 7bf4ec271be5b29e5e511d460aa87811b70742f6 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Mon, 1 May 2023 20:24:50 +0900 Subject: [PATCH 04/10] fix(gnokii): remove redundant LANG=C LC_ALL=C is already specified inside _parse_help, so we do not need to specify LANG=C at the caller side. --- completions/gnokii | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/completions/gnokii b/completions/gnokii index c45f6b31e2f..7d717158fda 100644 --- a/completions/gnokii +++ b/completions/gnokii @@ -221,8 +221,7 @@ _comp_cmd_gnokii() esac fi - # safer to use LANG=C - local all_cmd="$(LANG=C _parse_help "$1" "--help all")" + local all_cmd="$(_parse_help "$1" "--help all")" # these 2 below are allowed in combination with others local main_cmd=$(command grep -v -- '--config\|--phone' <<<"$all_cmd") From 3a8a2ede974d520208cc5ab006781e287f4fe09f Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 2 May 2023 05:04:06 +0900 Subject: [PATCH 05/10] fix(gnokii): avoid non-POSIX BRE \| \| is not supported by POSIX Basic Regular Expressions (BRE). Another way is to use newline to separate the two matches in `grep` pattern, but this is not a commonly-used approach. We instead use `sed` to remove the two matches separately. --- completions/gnokii | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/completions/gnokii b/completions/gnokii index 7d717158fda..9563f45573d 100644 --- a/completions/gnokii +++ b/completions/gnokii @@ -222,9 +222,9 @@ _comp_cmd_gnokii() fi local all_cmd="$(_parse_help "$1" "--help all")" - # these 2 below are allowed in combination with others - local main_cmd=$(command grep -v -- '--config\|--phone' <<<"$all_cmd") + # these 2 below are allowed in combination with others + local main_cmd=$(command sed -e '/--config/d;/--phone/d' <<<"$all_cmd") # don't provide main command completions if one is # already on the command line [[ $COMP_LINE =~ $(tr ' ' '\b|' <<<"$main_cmd") ]] && return From fb130ad1616453e69207cdeb618750b5f34e63e8 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 2 May 2023 05:08:10 +0900 Subject: [PATCH 06/10] fix(gnokii): avoid non-POSIX ERE \b The word-boundary anchor \b is not supported by POSIX Extended Regular Expressions (ERE). Also, the previous implementation did not consider the word-boyndary for the last element. We here explicitly match the trailing /[^_[:alnum:]]/ or /$/. Another issue is that the previous implementation did not work because of contaminated newlines in the ERE. We now split the results by newlines and then join the elements with IFS='|'. --- completions/gnokii | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/completions/gnokii b/completions/gnokii index 9563f45573d..e7e13745b1e 100644 --- a/completions/gnokii +++ b/completions/gnokii @@ -224,10 +224,14 @@ _comp_cmd_gnokii() local all_cmd="$(_parse_help "$1" "--help all")" # these 2 below are allowed in combination with others - local main_cmd=$(command sed -e '/--config/d;/--phone/d' <<<"$all_cmd") + local main_cmd + _comp_split -l main_cmd "$(command sed -e '/--config/d;/--phone/d' <<<"$all_cmd")" # don't provide main command completions if one is # already on the command line - [[ $COMP_LINE =~ $(tr ' ' '\b|' <<<"$main_cmd") ]] && return + local IFS='|' + local regex_main_cmd="(${main_cmd[*]})($|[^_[:alnum:]])" + IFS=$' \t\n' + [[ $COMP_LINE =~ $regex_main_cmd ]] && return COMPREPLY=($(compgen -W "$all_cmd" -- "$cur")) } && From c7877e96a8b8d213edadbf69050e639cf037fe9d Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 2 May 2023 05:34:56 +0900 Subject: [PATCH 07/10] fix(_udevcmd): protect against `localvar_inherit` --- completions/_udevadm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions/_udevadm b/completions/_udevadm index 0b207c1f1bf..536fc31178c 100644 --- a/completions/_udevadm +++ b/completions/_udevadm @@ -8,7 +8,7 @@ _comp_cmd_udevadm() local cur prev words cword was_split comp_args _comp_initialize -s -- "$@" || return - local i udevcmd has_udevcmd="" + local i udevcmd="" has_udevcmd="" for ((i = 1; i < cword; i++)); do if [[ ${words[i]} != -* ]]; then udevcmd=${words[i]} From 683a47628a8b4479a6451125cb850a1dda0be649 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 2 May 2023 05:36:46 +0900 Subject: [PATCH 08/10] fix(_udevcmd): use param $2 of `_parse_help` --- completions/_udevadm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/completions/_udevadm b/completions/_udevadm index 536fc31178c..7559404e29e 100644 --- a/completions/_udevadm +++ b/completions/_udevadm @@ -69,7 +69,7 @@ _comp_cmd_udevadm() if [[ $cur == -* ]]; then COMPREPLY=($(compgen -W \ - '$("$1" ${udevcmd-} --help 2>/dev/null | _parse_help -)' -- "$cur")) + '$(_parse_help "$1" "${udevcmd-} --help")' -- "$cur")) [[ ${COMPREPLY-} == *= ]] && compopt -o nospace fi } && From c60919644cb43f6d36e46e3a52ec97dc40bba191 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Tue, 2 May 2023 05:44:38 +0900 Subject: [PATCH 09/10] fix(postfix): use param $2 of `_parse_help` --- completions/postfix | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/completions/postfix b/completions/postfix index 7b445ff54c5..d5899742145 100644 --- a/completions/postfix +++ b/completions/postfix @@ -17,11 +17,8 @@ _comp_cmd_postfix() esac if [[ $cur == -* ]]; then - COMPREPLY=($( - compgen -W \ - '$(_bashcomp_try_faketty "$1" --help 2>&1 | _parse_usage -)' \ - -- "$cur" - )) + COMPREPLY=($(compgen -W \ + '$(_parse_usage _bashcomp_try_faketty "$1 --help")' -- "$cur")) return fi From 39a01d3ca03e11018032ba1d52053150543a2dc0 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Wed, 3 May 2023 06:39:46 +0900 Subject: [PATCH 10/10] fix(gnokii): escape ERE special characters in constructed regex --- completions/gnokii | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/completions/gnokii b/completions/gnokii index e7e13745b1e..785631c17c1 100644 --- a/completions/gnokii +++ b/completions/gnokii @@ -225,7 +225,8 @@ _comp_cmd_gnokii() # these 2 below are allowed in combination with others local main_cmd - _comp_split -l main_cmd "$(command sed -e '/--config/d;/--phone/d' <<<"$all_cmd")" + _comp_split -l main_cmd "$(command sed -e '/--config/d;/--phone/d' -e \ + 's/[][\(){}|^$*+?.]/\\&/g' <<<"$all_cmd")" # don't provide main command completions if one is # already on the command line local IFS='|'