diff options
author | Tom de Vries <tdevries@suse.de> | 2024-11-18 11:42:44 +0100 |
---|---|---|
committer | Tom de Vries <tdevries@suse.de> | 2024-11-18 11:42:44 +0100 |
commit | c6f2bd9d1089f30b038999a486772a5abc9df692 (patch) | |
tree | 20fcf5dc7ef516649002e79384b42eace96f2723 | |
parent | b0cc81e87087bb8a6b12dc1e4fd7f2591927977b (diff) | |
download | binutils-c6f2bd9d1089f30b038999a486772a5abc9df692.zip binutils-c6f2bd9d1089f30b038999a486772a5abc9df692.tar.gz binutils-c6f2bd9d1089f30b038999a486772a5abc9df692.tar.bz2 |
[gdb/contrib] Fix spellcheck.sh for bash < 5.1
Since commit 5cb0406bb64 ("[gdb/contrib] Handle capitalized words in
spellcheck.sh"), spellcheck.sh uses '${pat@u}' which is available starting
bash 5.1, and consequently the script breaks with bash 4.4.
Fix this by checking for the bash version, and using an alternative
implementation for bash < 5.1.
Tested on x86_64-linux.
-rwxr-xr-x | gdb/contrib/spellcheck.sh | 46 |
1 files changed, 44 insertions, 2 deletions
diff --git a/gdb/contrib/spellcheck.sh b/gdb/contrib/spellcheck.sh index d9ee797..d15e124 100755 --- a/gdb/contrib/spellcheck.sh +++ b/gdb/contrib/spellcheck.sh @@ -29,6 +29,32 @@ dictionary=$cache_dir/$cache_file local_dictionary=$scriptdir/common-misspellings.txt cache_file2=spell-check.pat1 +bash_version_at_least () +{ + local major + major="$1" + local minor + minor="$2" + + if [ "$bash_major" = "" ]; then + bash_major=$(echo "$BASH_VERSION" | awk -F '.' '{print $1}') + bash_minor=$(echo "$BASH_VERSION" | awk -F '.' '{print $2}') + fi + + if [ "$bash_major" -lt "$major" ]; then + # Major version less then required, return false. + return 1 + fi + + if [ "$bash_major" -gt "$major" ]; then + # Major version more then required, return true. + return 0 + fi + + # Check minor version. + [ "$bash_minor" -ge "$minor" ] +} + # Separators: space, slash, tab, colon, comma. declare -a grep_separators grep_separators=( @@ -343,7 +369,13 @@ find_files_matching_word () "${grep_separators[@]}" \ "${grep_post[@]}") - pat="(${pat@u}|$pat)" + if bash_version_at_least 5 1; then + patc=${pat@u} + else + # shellcheck disable=SC2001 + patc=$(echo "$pat" | sed 's/^\(.\)/\u\1/') + fi + pat="($patc|$pat)" pat="$before$pat$after" @@ -372,10 +404,20 @@ replace_word_in_file () "${sed_separators[@]}" \ "${sed_post[@]}") + if bash_version_at_least 5 1; then + wordc=${word@u} + replacementc=${replacement@u} + else + # shellcheck disable=SC2001 + wordc=$(echo "$word" | sed 's/^\(.\)/\u\1/') + # shellcheck disable=SC2001 + replacementc=$(echo "$replacement" | sed 's/^\(.\)/\u\1/') + fi + local repl1 local repl2 repl1="s%$before$word$after%\1$replacement\2%g" - repl2="s%$before${word@u}$after%\1${replacement@u}\2%g" + repl2="s%$before$wordc$after%\1$replacementc\2%g" sed -i \ "$repl1;$repl2" \ |