From 2b82cd70372eee23e6b9d9a106a21d804396e256 Mon Sep 17 00:00:00 2001
From: Andre Maroneze <andre.maroneze@cea.fr>
Date: Wed, 24 Jul 2024 12:27:45 +0200
Subject: [PATCH] [analysis-scripts] apply shellcheck suggestions and a few
 more improvements

---
 share/analysis-scripts/creduce.sh                  | 14 +++++++-------
 share/analysis-scripts/script_for_creduce_fatal.sh |  5 +++--
 .../script_for_creduce_non_fatal.sh                | 10 ++++++----
 3 files changed, 16 insertions(+), 13 deletions(-)

diff --git a/share/analysis-scripts/creduce.sh b/share/analysis-scripts/creduce.sh
index 634eff77eb2..47f59372e29 100755
--- a/share/analysis-scripts/creduce.sh
+++ b/share/analysis-scripts/creduce.sh
@@ -113,14 +113,14 @@ script_for_creduce="./script_for_creduce.sh"
 if [ -z "${CREDUCE+x}" ]; then
     # Now some distributions have 'cvise' instead of 'creduce', so try it
     # if found in PATH
-    if command -v cvise 2>&1 >/dev/null; then
+    if command -v cvise >/dev/null 2>&1; then
         CREDUCE="cvise"
     else
         CREDUCE="creduce"
     fi
 fi
 
-if ! command -v "$CREDUCE" 2>&1 >/dev/null; then
+if ! command -v "$CREDUCE" >/dev/null 2>&1; then
     echo "cvise/creduce not found; install it in the PATH or"
     echo "put it in environment variable CREDUCE."
     exit 1
@@ -137,7 +137,7 @@ for f in "$@"; do
         too_many_sources+=" $f"
     fi
 done
-if [ ! -z "$too_many_sources" ]; then
+if [ -n "$too_many_sources" ]; then
     echo "error: too many sources; only the first argument must be a file: $file"
     echo "       remove these from the command-line:$too_many_sources"
     exit 1
@@ -182,7 +182,7 @@ fi
 #
 #### End of command-line and environment validation
 
-if [[ ! "$@" =~ no-autoload-plugins ]]; then
+if [[ ! "$*" =~ no-autoload-plugins ]]; then
     echo "********************************************************************"
     echo "Hint: consider using -no-autoload-plugins -load-module [modules]"
     echo "      for faster reduction"
@@ -208,7 +208,7 @@ else
     cpp_retcode=$?
     set -e
     if [ $cpp_retcode -ne 0 ]; then
-        echo "error trying to get preprocessing flags (exit code: $cpp_retcode): $FRAMAC -print-cpp-commands $@ $file"
+        echo "error trying to get preprocessing flags (exit code: $cpp_retcode): $FRAMAC -print-cpp-commands $* $file"
         exit $cpp_retcode
     fi
     CPP=$(echo "$cpp_output" | \
@@ -269,7 +269,7 @@ else
 fi
 "$SED" -i "s|@FRAMAC@|$FRAMAC|g" "$script_for_creduce"
 "$SED" -i "s|@BASE@|$base|g" "$script_for_creduce"
-"$SED" -i "s|@FCFLAGS@|$(echo $@ | tr "'" "\\'")|g" "$script_for_creduce"
+"$SED" -i "s|@FCFLAGS@|$(echo "$@" | tr "'" "\\'")|g" "$script_for_creduce"
 chmod u+x "$script_for_creduce"
 
 trap '{ echo "Creduce interrupted!"; echo ""; echo "(partially) reduced file: $dir_for_reduction/$base"; exit 0; }' SIGINT
@@ -288,7 +288,7 @@ if [ $? -ne 0 ]; then
     echo "       check the options given to Frama-C."
     echo "       If you edited '$script_for_creduce', check it as well."
     echo ""
-    if [ $(wc -l /tmp/script_for_creduce.out | cut -d' ' -f1) -gt 20 ]; then
+    if [ "$(wc -l /tmp/script_for_creduce.out | cut -d' ' -f1)" -gt 20 ]; then
         echo "# Script output (first 20 lines):"
         head -n 20 /tmp/script_for_creduce.out
         echo "(...) [truncated; full output in /tmp/script_for_creduce.out]"
diff --git a/share/analysis-scripts/script_for_creduce_fatal.sh b/share/analysis-scripts/script_for_creduce_fatal.sh
index 8757dfc58b5..2df57aa70fa 100644
--- a/share/analysis-scripts/script_for_creduce_fatal.sh
+++ b/share/analysis-scripts/script_for_creduce_fatal.sh
@@ -29,10 +29,11 @@
 set +e
 "@FRAMAC@" "@BASE@" @FCFLAGS@
 retcode=$?
+set -e
+
 # see cmdline.ml for the different exit codes returned by Frama-C
-if [ $retcode -eq 125 -o $retcode -eq 4 ]; then
+if [ $retcode -eq 125 ] || [ $retcode -eq 4 ]; then
     exit 0
 else
     exit 1
 fi
-set -e
diff --git a/share/analysis-scripts/script_for_creduce_non_fatal.sh b/share/analysis-scripts/script_for_creduce_non_fatal.sh
index f9d9bf48c00..754d288aef8 100644
--- a/share/analysis-scripts/script_for_creduce_non_fatal.sh
+++ b/share/analysis-scripts/script_for_creduce_non_fatal.sh
@@ -32,7 +32,7 @@ fc_out=$(mktemp creduce_fc_XXXXXX.log)
 
 # We always check that the reduced file remains valid C code.
 set -o pipefail
-@CPP@ "@BASE@" 2>&1 | tee $cc_out
+@CPP@ "@BASE@" 2>&1 | tee "$cc_out"
 set +o pipefail
 
 ### Examples of conditions to be maintained by C-Reduce; copy and adapt
@@ -49,10 +49,12 @@ set +o pipefail
 
 ##########
 
+set +e # allow Frama-C to fail so we can retrieve its exit code
 set -o pipefail
-"@FRAMAC@" "@BASE@" @FCFLAGS@ 2>&1 | tee $fc_out
-fc_retcode=$(echo ${PIPESTATUS[0]})
+"@FRAMAC@" "@BASE@" @FCFLAGS@ 2>&1 | tee "$fc_out"
+fc_retcode=${PIPESTATUS[0]}
 set +o pipefail
+set -e
 
 ### Examples of conditions to be maintained by C-Reduce; copy and adapt
 #
@@ -69,4 +71,4 @@ set +o pipefail
 ##########
 
 ### Cleanup
-rm -f $cc_out $fc_out
+rm -f "$cc_out" "$fc_out"
-- 
GitLab