Skip to content

Commit

Permalink
scripts: always use double quotes and curly braces
Browse files Browse the repository at this point in the history
Command line:
shellcheck -s sh -i SC2248,SC2250 -o quote-safe-variables,require-variable-braces $(git ls-files "*.sh")

(There will be two complaints about wanting to double-quote
${CFLAGS_C99}, but it's constructing a command-line, so ignore those
warnings.)

Explanation:
- Prefer double quoting even when variables don't contain special
  characters.
  https://www.shellcheck.net/wiki/SC2248

- Prefer putting braces around variable references even when not
  strictly required.
  https://github.com/koalaman/shellcheck/wiki/SC2250

Reported by:	shellcheck 0.9.0
  • Loading branch information
gperciva committed Aug 8, 2023
1 parent 31307f9 commit a959308
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 75 deletions.
6 changes: 3 additions & 3 deletions libcperciva/POSIX/posix-cflags-filter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ if [ -z "${CC}" ]; then
exit 1
fi
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

# Find directory of this script and the source files
D=$(dirname "$0")

if ! ${CC} -O2 "$D/posix-cflags-filter.c" 2>/dev/null; then
if ${CC} "$D/posix-cflags-filter.c" 2>/dev/null; then
if ! ${CC} -O2 "${D}/posix-cflags-filter.c" 2>/dev/null; then
if ${CC} "${D}/posix-cflags-filter.c" 2>/dev/null; then
echo 'CFLAGS_FILTERED=""'
echo 'for OPT in $CFLAGS; do'
echo ' if [ "$OPT" = "-O2" ]; then'
Expand Down
42 changes: 21 additions & 21 deletions libcperciva/POSIX/posix-cflags.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ if [ -z "${CC}" ]; then
exit 1
fi
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

# Find directory of this script and the source files
D=$(dirname "$0")

# Check if we can compile & run a binary.
if ! ${CC} ${CFLAGS} "$D/posix-trivial.c" 2>/dev/null; then
if ! ${CC} ${CFLAGS} "${D}/posix-trivial.c" 2>/dev/null; then
echo "WARNING: failed to compile posix-trivial.c" 1>&2
else
# If the user hasn't disabled runtime checks...
Expand All @@ -31,28 +31,28 @@ else
fi

FIRST=YES
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-msg_nosignal.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-msg_nosignal.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_MSG_NOSIGNAL"
echo "WARNING: POSIX violation: <sys/socket.h> not defining MSG_NOSIGNAL" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-clock_realtime.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-clock_realtime.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_CLOCK_REALTIME"
echo "WARNING: POSIX violation: <time.h> not defining CLOCK_REALTIME" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "$D/posix-inet-addrstrlen.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "${D}/posix-inet-addrstrlen.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_INET_ADDRSTRLEN"
echo "WARNING: POSIX violation: <netinet/in.h> not defining INET_ADDRSTRLEN" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "$D/posix-inet6-addrstrlen.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "${D}/posix-inet6-addrstrlen.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_INET6_ADDRSTRLEN"
echo "WARNING: POSIX violation: <netinet/in.h> not defining INET6_ADDRSTRLEN" 1>&2
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-clock_gettime.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-clock_gettime.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_CLOCK_GETTIME"
echo "WARNING: POSIX violation: <time.h> not declaring clock_gettime()" 1>&2
elif [ "${DISABLE_POSIX_RUNTIME_CHECKS:-0}" -ne "0" ]; then
Expand All @@ -68,30 +68,30 @@ else
# calling process. The "( ./x 2>y ) 2>y" captures both types of error
# message.
if ! ( ./a.out 2>/dev/null ) 2>/dev/null ; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_CLOCK_GETTIME"
echo "WARNING: POSIX violation: clock_gettime() is not linkable" 1>&2
fi
fi
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "$D/posix-stat-st_mtim.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 "${D}/posix-stat-st_mtim.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_STAT_ST_MTIM"
echo "WARNING: POSIX violation: struct stat does not contain st_mtim" 1>&2
fi
CFLAGS_C99=""
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "$D/posix-restrict.c" 2>/dev/null; then
if ! ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L "${D}/posix-restrict.c" 2>/dev/null; then
echo "WARNING: POSIX violation: ${CC} does not accept the 'restrict' keyword" 1>&2
if ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -std=c99 "$D/posix-restrict.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ${CC} ${CFLAGS} -D_POSIX_C_SOURCE=200809L -std=c99 "${D}/posix-restrict.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-std=c99"
CFLAGS_C99="-std=c99"
fi
fi
if ! ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DARGNAME="" "$D/posix-abstract-declarator.c" 2>/dev/null; then
if ! ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DARGNAME="" "${D}/posix-abstract-declarator.c" 2>/dev/null; then
echo "WARNING: POSIX violation: ${CC} does not accept qualifiers in an abstract declarator" 1>&2
# Test compile with -DPOSIXFAIL_ABSTRACT_DECLARATOR
if ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DPOSIXFAIL_ABSTRACT_DECLARATOR "$D/posix-abstract-declarator.c" 2>/dev/null; then
[ ${FIRST} = "NO" ] && printf " "; FIRST=NO
if ${CC} ${CFLAGS} ${CFLAGS_C99} -D_POSIX_C_SOURCE=200809L -DPOSIXFAIL_ABSTRACT_DECLARATOR "${D}/posix-abstract-declarator.c" 2>/dev/null; then
[ "${FIRST}" = "NO" ] && printf " "; FIRST=NO
printf %s "-DPOSIXFAIL_ABSTRACT_DECLARATOR"
fi
fi
Expand Down
6 changes: 3 additions & 3 deletions libcperciva/POSIX/posix-l.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if [ -z "${CC}" ]; then
exit 1
fi
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

Expand All @@ -17,8 +17,8 @@ D=$(dirname "$0")

FIRST=YES
for LIB in rt xnet; do
if ${CC} ${CFLAGS} -l${LIB} "$D/posix-trivial.c" 2>/dev/null; then
if [ ${FIRST} = "NO" ]; then
if ${CC} ${CFLAGS} -l"${LIB}" "${D}/posix-trivial.c" 2>/dev/null; then
if [ "${FIRST}" = "NO" ]; then
printf " ";
fi
printf "%s" "-l${LIB}";
Expand Down
10 changes: 5 additions & 5 deletions libcperciva/apisupport/Build/apisupport.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Should be sourced by `command -p sh path/to/apisupport.sh "$PATH"` from
# within a Makefile.
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

Expand Down Expand Up @@ -34,17 +34,17 @@ feature() {

# Check if we can compile this feature (and any required arguments).
printf "Checking if compiler supports %s %s feature..." \
"$PLATFORM" "$FEATURE" 1>&2
"${PLATFORM}" "${FEATURE}" 1>&2
for API_CFLAGS in "$@"; do
if ${CC} ${CPPFLAGS} ${CFLAGS} ${CFLAGS_HARDCODED} \
${API_CFLAGS} "${feature_filename}" ${LDADD_EXTRA} \
${EXTRALIB} 2>>${outcc}; then
${EXTRALIB} 2>>"${outcc}"; then
rm -f a.out
break;
fi
API_CFLAGS=NOTSUPPORTED;
done
case $API_CFLAGS in
case ${API_CFLAGS} in
NOTSUPPORTED)
echo " no" 1>&2
;;
Expand All @@ -53,7 +53,7 @@ feature() {
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
;;
*)
echo " yes, via $API_CFLAGS" 1>&2
echo " yes, via ${API_CFLAGS}" 1>&2
echo "#define APISUPPORT_${PLATFORM}_${FEATURE} 1"
echo "#ifdef apisupport_dummy"
echo "export CFLAGS_${PLATFORM}_${FEATURE}=\"${API_CFLAGS}\""
Expand Down
10 changes: 5 additions & 5 deletions libcperciva/cpusupport/Build/cpusupport.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Should be sourced by `command -p sh path/to/cpusupport.sh "$PATH"` from
# within a Makefile.
if ! [ "${PATH}" = "$1" ]; then
echo "WARNING: POSIX violation: $SHELL's command -p resets \$PATH" 1>&2
echo "WARNING: POSIX violation: ${SHELL}'s command -p resets \$PATH" 1>&2
PATH=$1
fi

Expand Down Expand Up @@ -33,16 +33,16 @@ feature() {

# Check if we can compile this feature (and any required arguments).
printf "Checking if compiler supports %s %s feature..." \
"$ARCH" "$FEATURE" 1>&2
"${ARCH}" "${FEATURE}" 1>&2
for CPU_CFLAGS in "$@"; do
if ${CC} ${CPPFLAGS} ${CFLAGS} ${CFLAGS_HARDCODED} \
${CPU_CFLAGS} "${feature_filename}" 2>>${outcc}; then
${CPU_CFLAGS} "${feature_filename}" 2>>"${outcc}"; then
rm -f a.out
break;
fi
CPU_CFLAGS=NOTSUPPORTED;
done
case $CPU_CFLAGS in
case ${CPU_CFLAGS} in
NOTSUPPORTED)
echo " no" 1>&2
;;
Expand All @@ -51,7 +51,7 @@ feature() {
echo "#define CPUSUPPORT_${ARCH}_${FEATURE} 1"
;;
*)
echo " yes, via $CPU_CFLAGS" 1>&2
echo " yes, via ${CPU_CFLAGS}" 1>&2
echo "#define CPUSUPPORT_${ARCH}_${FEATURE} 1"
echo "#ifdef cpusupport_dummy"
echo "export CFLAGS_${ARCH}_${FEATURE}=\"${CPU_CFLAGS}\""
Expand Down
52 changes: 26 additions & 26 deletions release-tools/metabuild.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ CFLAGS_HARDCODED=$3
OUT=Makefile

# Check environment
if [ -z "$CPP" ]; then
if [ -z "${CPP}" ]; then
echo "Need CPP environment variable."
exit 1
fi
Expand Down Expand Up @@ -43,17 +43,17 @@ fi

copyvar() {
var=$1
if [ -n "$(${MAKEBSD} -v "$var")" ]; then
printf "%s=" "$var" >> $OUT
${MAKEBSD} -v "$var" >> $OUT
if [ -n "$(${MAKEBSD} -v "${var}")" ]; then
printf "%s=" "${var}" >> "${OUT}"
${MAKEBSD} -v "${var}" >> "${OUT}"
fi
}

addvar_lib() {
var=$1
varval=$(${MAKEBSD} -v "$var")
varval=$(${MAKEBSD} -v "${var}")
if [ -n "${varval}" ]; then
printf "%s=lib%s.a\n" "$var" "${varval}" >> $OUT
printf "%s=lib%s.a\n" "${var}" "${varval}" >> "${OUT}"
fi
}

Expand All @@ -74,7 +74,7 @@ add_makefile_prog() {
fi

# Add the (adjusted) Makefile.prog to the Makefile, and clean up
cat prog.tmp >> $OUT
cat prog.tmp >> "${OUT}"
rm prog.tmp
}

Expand All @@ -84,7 +84,7 @@ get_cpusupport_cflags() {
str=$(grep 'CPUSUPPORT CFLAGS:' "${src}" | cut -f 2- -d :)
# ${str} must be unquoted.
for X in ${str}; do
printf " \${CFLAGS_%s}" "$X"
printf " \${CFLAGS_%s}" "${X}"
done | sed 's/^ //'
}

Expand All @@ -94,7 +94,7 @@ get_apisupport_cflags() {
str=$(grep 'APISUPPORT CFLAGS:' "${src}" | cut -f 2- -d :)
# ${str} must be unquoted.
for X in ${str}; do
printf " \${CFLAGS_%s}" "$X"
printf " \${CFLAGS_%s}" "${X}"
done | sed 's/^ //'
}

Expand All @@ -111,7 +111,7 @@ add_object_files() {
OUT_CC_MID="-I${SUBDIR_DEPTH} \${IDIRS} \${CPPFLAGS} \${CFLAGS}"

# Generate build instructions for each object
for F in $OBJ; do
for F in ${OBJ}; do
S=$(${MAKEBSD} source-"${F}")
CF_MANUAL=$(${MAKEBSD} -v CFLAGS."$(basename "${S}")")
CF_CPUSUPPORT=$(get_cpusupport_cflags "${S}")
Expand All @@ -132,12 +132,12 @@ add_object_files() {
# Print the build instructions
echo " ${OUT_CC_BEGIN} ${OUT_CC_MID} ${CF} -c ${S} -o ${F}" |
tr -s ' '
done >> $OUT
done >> "${OUT}"
}

# Add header and variables
echo '.POSIX:' > $OUT
echo '# AUTOGENERATED FILE, DO NOT EDIT' >> $OUT
echo '.POSIX:' > "${OUT}"
echo '# AUTOGENERATED FILE, DO NOT EDIT' >> "${OUT}"
if [ -n "$(${MAKEBSD} -v LIB)" ]; then
addvar_lib LIB
else
Expand All @@ -147,24 +147,24 @@ copyvar MAN1

# SRCS is trickier to handle, as we need to remove any -config.h from the list.
if [ -n "$(${MAKEBSD} -v SRCS)" ]; then
printf "SRCS=" >> $OUT
printf "SRCS=" >> "${OUT}"
${MAKEBSD} -v SRCS | \
sed -e 's| apisupport-config.h||' | \
sed -e 's| cpusupport-config.h||' >> $OUT
sed -e 's| cpusupport-config.h||' >> "${OUT}"
fi
copyvar IDIRS
copyvar LDADD_REQ
copyvar SUBDIR_DEPTH
printf "RELATIVE_DIR=%s\n" "$D" >> $OUT
printf "RELATIVE_DIR=%s\n" "${D}" >> "${OUT}"

# Add all, install, clean, $PROG
if [ -n "$(${MAKEBSD} -v LIB)" ]; then
cat "${SUBDIR_DEPTH}/release-tools/Makefile.lib" >> $OUT
cat "${SUBDIR_DEPTH}/release-tools/Makefile.lib" >> "${OUT}"
elif [ -n "$(${MAKEBSD} -v SRCS)" ]; then
copyvar LIBALL
add_makefile_prog
else
printf "\nall:\n\ttrue\n\nclean:\n\ttrue\n" >> $OUT
printf "\nall:\n\ttrue\n\nclean:\n\ttrue\n" >> "${OUT}"
fi

# Add all object files (if applicable)
Expand All @@ -174,33 +174,33 @@ fi

# Add test (if applicable)
if grep -q "^test:" Makefile.BSD ; then
printf "\n" >> $OUT
printf "\n" >> "${OUT}"
awk '/^test:/, /^$/' Makefile.BSD | \
awk '$1' >> $OUT
awk '$1' >> "${OUT}"
fi

# Add perftest (if applicable)
if grep -q "^perftest:" Makefile.BSD ; then
printf "\n" >> $OUT
printf "\n" >> "${OUT}"
awk '/^perftest:/, /^$/' Makefile.BSD | \
awk '$1' >> $OUT
awk '$1' >> "${OUT}"
fi

# Add all_extra (if applicable)
if grep -q "^all_extra:" Makefile.BSD ; then
printf "\n" >> $OUT
printf "\n" >> "${OUT}"
awk '/^all_extra:/, /^$/' Makefile.BSD | \
awk '$1' >> $OUT
awk '$1' >> "${OUT}"
sed -e 's/${MAKE} ${PROG}/${MAKE} ${PROG} all_extra/' \
Makefile > Makefile.new
mv Makefile.new Makefile
fi

# Add clean_extra (if applicable)
if grep -q "^clean_extra:" Makefile.BSD ; then
printf "\n" >> $OUT
printf "\n" >> "${OUT}"
awk '/^clean_extra:/, /^$/' Makefile.BSD | \
awk '$1' >> $OUT
awk '$1' >> "${OUT}"
awk '/^clean:/ {print $0 "\tclean_extra";next}{print}' \
Makefile > Makefile.new
mv Makefile.new Makefile
Expand Down
Loading

0 comments on commit a959308

Please sign in to comment.