From 8a8f2b29ca45a797eadbdb0600982c174174ab5e Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Sun, 30 Oct 2016 02:05:31 -0400 Subject: [PATCH] Fix most shell hygiene issues found by shellcheck Signed-off-by: Anders Kaseorg --- macosx/build.sh | 6 +-- scripts/wrap-compiler-for-flag-check | 2 +- src/tests/e2e-failure.test | 7 +-- src/tests/e2e-success.test | 7 +-- src/tests/e2e-test | 47 +++++++++++-------- src/tests/e2e-test-server | 15 ++++-- src/tests/e2e-test-subrs | 8 ++-- src/tests/emulation-80th-column.test | 11 +++-- src/tests/emulation-ascii-iso-8859.test | 7 +-- src/tests/emulation-attributes.test | 23 ++++----- src/tests/emulation-back-tab.test | 19 ++++---- src/tests/emulation-cursor-motion.test | 11 +++-- src/tests/emulation-multiline-scroll.test | 11 +++-- src/tests/emulation-wrap-across-frames.test | 7 +-- src/tests/prediction-unicode.test | 9 ++-- src/tests/pty-deadlock.test | 15 +++--- src/tests/server-network-timeout.test | 25 +++++----- .../unicode-combine-fallback-assert.test | 7 +-- src/tests/unicode-later-combining.test | 9 ++-- src/tests/window-resize.test | 13 ++--- 20 files changed, 144 insertions(+), 115 deletions(-) diff --git a/macosx/build.sh b/macosx/build.sh index d92fcdb..7740fcc 100755 --- a/macosx/build.sh +++ b/macosx/build.sh @@ -88,11 +88,11 @@ done # Build fat binaries # XXX will break with spaces in pathname for prog in local/bin/mosh-client local/bin/mosh-server; do - archprogs= + archprogs=() for arch in $BUILT_ARCHS; do - archprogs="$archprogs ${PREFIX}_${arch}/$prog" + archprogs+=("${PREFIX}_${arch}/$prog") done - lipo -create $archprogs -output "${PREFIX}/$prog" + lipo -create "${archprogs[@]}" -output "${PREFIX}/$prog" done perl -wlpi -e 's{#!/usr/bin/env perl}{#!/usr/bin/perl}' "$PREFIX/local/bin/mosh" diff --git a/scripts/wrap-compiler-for-flag-check b/scripts/wrap-compiler-for-flag-check index 579f577..d2e9dd8 100755 --- a/scripts/wrap-compiler-for-flag-check +++ b/scripts/wrap-compiler-for-flag-check @@ -7,7 +7,7 @@ # Ideally the search string would also include 'clang: ' but this output might # depend on clang's argv[0]. -if out=`"$@" 2>&1`; then +if out=$("$@" 2>&1); then echo "$out" if echo "$out" | grep 'warning: argument unused' >/dev/null; then echo "$0: found clang warning" diff --git a/src/tests/e2e-failure.test b/src/tests/e2e-failure.test index 34147e9..0385295 100755 --- a/src/tests/e2e-failure.test +++ b/src/tests/e2e-failure.test @@ -1,10 +1,11 @@ #!/bin/sh -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline variant different + e2e-test "$0" baseline variant different exit fi @@ -22,5 +23,5 @@ case $1 in baseline|variant) baseline;; *) - fail "unknown test argument %s\n" $1;; + fail "unknown test argument %s\n" "$1";; esac diff --git a/src/tests/e2e-success.test b/src/tests/e2e-success.test index 3134b62..3225e76 100755 --- a/src/tests/e2e-success.test +++ b/src/tests/e2e-success.test @@ -1,10 +1,11 @@ #!/bin/sh -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline direct variant verify same + e2e-test "$0" baseline direct variant verify same exit fi @@ -22,5 +23,5 @@ case $1 in baseline|direct|variant) baseline;; *) - fail "unknown test argument %s\n" $1;; + fail "unknown test argument %s\n" "$1";; esac diff --git a/src/tests/e2e-test b/src/tests/e2e-test index 387919d..0952cf9 100755 --- a/src/tests/e2e-test +++ b/src/tests/e2e-test @@ -19,11 +19,11 @@ dump_logs() { dir=$1 shift - testname=$(basename $dir .d) + testname=$(basename "$dir" .d) for logfile in $dir/*.tmux.log; do - printf "travis_fold:start:%s-%s\n" $testname $(basename $logfile) - cat $logfile - printf "travis_fold:end:%s-%s\n" $testname $(basename $logfile) + printf "travis_fold:start:%s-%s\n" "$testname" "$(basename "$logfile")" + cat "$logfile" + printf "travis_fold:end:%s-%s\n" "$testname" "$(basename "$logfile")" done } @@ -51,7 +51,7 @@ test_exitstatus() status=$1 shift error "$@" - exit $status + exit "$status" } @@ -67,8 +67,8 @@ tmux_check() version_major=${version%%.*} version_minor=${version##*.} # need version 1.8 for capture-pane - if [ $version_major -lt 1 ] || - [ $version_major -eq 1 -a $version_minor -lt 8 ]; then + if [ "$version_major" -lt 1 ] || + { [ "$version_major" -eq 1 ] && [ "$version_minor" -lt 8 ]; }; then error "tmux version %s too old\n" "$version" return 1 fi @@ -89,7 +89,7 @@ ssh_localhost_check() # on verbosity and log stderr. mosh_client() { - if [ -z "$MOSH_CLIENT" -o -z "$MOSH_E2E_TEST" ]; then + if [ -z "$MOSH_CLIENT" ] || [ -z "$MOSH_E2E_TEST" ]; then test_error "mosh_client: variables missing\n" fi exec 2> "${MOSH_E2E_TEST}.client.stderr" @@ -98,7 +98,7 @@ mosh_client() mosh_server() { - if [ -z "$MOSH_SERVER" -o -z "$MOSH_E2E_TEST" ]; then + if [ -z "$MOSH_SERVER" ] || [ -z "$MOSH_E2E_TEST" ]; then test_error "mosh_server: variables missing\n" fi exec 2> "${MOSH_E2E_TEST}.server.stderr" @@ -109,9 +109,9 @@ mosh_server() # Set up environment if [ -z "$srcdir" ]; then - : ${srcdir:=$PWD} + srcdir=$PWD else - srcdir="$(cd $srcdir && pwd)" + srcdir="$(cd "$srcdir" && pwd)" if [ $? -ne 0 ]; then error "can't cd to srcdir: %s\n" "$srcdir" exit 99 @@ -119,7 +119,7 @@ else fi # Wrappers. -case "$(basename $0)" in +case "$(basename "$0")" in mosh-client) mosh_client "$@" exit @@ -145,14 +145,21 @@ test_name=$1 shift test_args=$@ # XXX could use AM testsubdir macro instead -test_dir=$(basename ${test_name}).d +test_dir=$(basename "${test_name}").d test_script="${test_name}" rm -rf "${test_dir}" mkdir "${test_dir}" -trap 'rv=$?; if test $rv -ne 0; then dump_logs '"$test_dir $test_args"'; fi; exit $rv' EXIT +on_exit() { + rv=$? + if test $rv -ne 0; then + dump_logs "$test_dir" $test_args + fi + exit $rv +} +trap on_exit EXIT # Set up tests to run. server_tests= @@ -170,13 +177,15 @@ for i in $test_args; do post) post=1;; mosh-args) - mosh_args=$(${test_script} mosh-args);; + mosh_args=$("${test_script}" mosh-args);; client-args) - export MOSH_CLIENT_ARGS=$(${test_script} client-args);; + MOSH_CLIENT_ARGS=$("${test_script}" client-args) + export MOSH_CLIENT_ARGS;; server-args) - export MOSH_SERVER_ARGS=$(${test_script} server-args);; + MOSH_SERVER_ARGS=$("${test_script}" server-args) + export MOSH_SERVER_ARGS;; *) - error "unknown test type argument %s", $i + error "unknown test type argument %s", "$i" exit 99 ;; esac @@ -225,7 +234,7 @@ for run in $server_tests; do || [ ! -s "${test_dir}/${run}.exitstatus" ]; then test_error "server harness failure on test %s\n" "$run" fi - read server_rv < "${test_dir}/${run}.exitstatus" + read -r server_rv < "${test_dir}/${run}.exitstatus" if [ "$server_rv" -ne 0 ]; then test_error "server harness exited with status %s\n" "$server_rv" fi diff --git a/src/tests/e2e-test-server b/src/tests/e2e-test-server index 4a92915..c720ca8 100755 --- a/src/tests/e2e-test-server +++ b/src/tests/e2e-test-server @@ -13,9 +13,14 @@ if [ $# -lt 2 ]; then fi testname=$1 shift -rm -f $testname.capture $testname.exitstatus +rm -f "$testname.capture" "$testname.exitstatus" trap ":" TERM HUP QUIT # If the session closes on us, let the test we're running drive. -trap 'rv=$?; echo $rv > $testname.exitstatus; exit $rv' EXIT +on_exit() { + rv=$? + echo $rv > "$testname.exitstatus" + exit $rv +} +trap on_exit EXIT # check for tmux if [ -z "$TMUX_PANE" ]; then printf "not running under tmux\n" >&2 @@ -29,7 +34,7 @@ sleep 1 printf "@@@ server complete @@@" >&2 i=0 while [ $i -lt 60 ]; do - if grep -q "@@@ server complete @@@" $testname.tmux.log; then + if grep -q "@@@ server complete @@@" "$testname.tmux.log"; then break fi i=$((i+1)) @@ -40,11 +45,11 @@ if [ $i -ge 60 ]; then exit 99 fi # capture screen -if ! tmux capture-pane -et $TMUX_PANE; then +if ! tmux capture-pane -et "$TMUX_PANE"; then printf "tmux capture-pane failed, erroring test\n" >&2 exit 99 fi -if ! tmux save-buffer $testname.capture; then +if ! tmux save-buffer "$testname.capture"; then printf "tmux save-buffer failed, erroring test\n" >&2 exit 99 fi diff --git a/src/tests/e2e-test-subrs b/src/tests/e2e-test-subrs index 3a7b4a9..4ace310 100644 --- a/src/tests/e2e-test-subrs +++ b/src/tests/e2e-test-subrs @@ -17,7 +17,7 @@ sleepf() seq_function() { - if [ $# -lt 1 -o $# -gt 3 ]; then + if [ $# -lt 1 ] || [ $# -gt 3 ]; then echo "bad args" >&2 fi first=$1 @@ -35,9 +35,9 @@ seq_function() ;; esac while :; do - printf '%d\n' $first + printf '%d\n' "$first" first=$(( first + incr )) - if [ $first -gt $last ]; then + if [ "$first" -gt "$last" ]; then break fi done @@ -52,5 +52,5 @@ fi chr() { - printf "\\$(printf %03o $1)" + printf '%b' "\\0$(printf %03o "$1")" } diff --git a/src/tests/emulation-80th-column.test b/src/tests/emulation-80th-column.test index 041863d..b33cc44 100755 --- a/src/tests/emulation-80th-column.test +++ b/src/tests/emulation-80th-column.test @@ -8,11 +8,12 @@ # line). # -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline post + e2e-test "$0" baseline post exit fi @@ -25,7 +26,7 @@ baseline() { # We need to control CR and LF individually for this test. TERMIO=$(stty -g) - trap "stty $TERMIO" EXIT + trap 'stty "$TERMIO"' EXIT stty raw printf '\033[H\033[J' for lines in $(seq 1 25); do @@ -41,7 +42,7 @@ post() # If hidden 80th column is working properly, then the lines # will have no blank lines in between and we should see 23 # of them. - if [ $(grep -c "EEEEEEEEEE" $(basename $0).d/baseline.capture) -ne 23 ]; then + if [ "$(grep -c "EEEEEEEEEE" "$(basename "$0").d/baseline.capture")" -ne 23 ]; then exit 1 fi } @@ -52,5 +53,5 @@ case $1 in post) post;; *) - fail "unknown test argument %s\n" $1;; + fail "unknown test argument %s\n" "$1";; esac diff --git a/src/tests/emulation-ascii-iso-8859.test b/src/tests/emulation-ascii-iso-8859.test index 3cb2055..b50a001 100755 --- a/src/tests/emulation-ascii-iso-8859.test +++ b/src/tests/emulation-ascii-iso-8859.test @@ -4,11 +4,12 @@ # This validates display of ASCII and ISO-8859-1 characters. # -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline direct verify + e2e-test "$0" baseline direct verify exit fi @@ -41,5 +42,5 @@ case $1 in baseline|direct) baseline;; *) - fail "unknown test argument %s\n" $1;; + fail "unknown test argument %s\n" "$1";; esac diff --git a/src/tests/emulation-attributes.test b/src/tests/emulation-attributes.test index 955029f..85f3e70 100755 --- a/src/tests/emulation-attributes.test +++ b/src/tests/emulation-attributes.test @@ -10,11 +10,12 @@ # compares on these values fail though they are visually identical. # -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline direct verify + e2e-test "$0" baseline direct verify exit fi @@ -26,7 +27,7 @@ fi baseline() { # Strip our name to the last dash-separated word before the .test suffix. - testname=$(basename $1) + testname=$(basename "$1") testname=${testname%%.test} testname=${testname##*-} @@ -44,35 +45,35 @@ baseline() # 16-color attributes. 16color) for attr in $(seq 30 37) $(seq 39 47) 49; do - printf '\033[%dmE\033[m ' $attr + printf '\033[%dmE\033[m ' "$attr" done printf '\n' ;; # First 8 256-color attributes. Comparing mosh and tmux fails. 256color8) for attr in $(seq 0 7); do - printf '\033[38;5;%dmE\033[m ' $attr - printf '\033[48;5;%dmM\033[m ' $attr + printf '\033[38;5;%dmE\033[m ' "$attr" + printf '\033[48;5;%dmM\033[m ' "$attr" done printf '\n' ;; # Last 248 256-color attributes. 256color248) for attr in $(seq 8 255); do - printf '\033[38;5;%dmE\033[m ' $attr - printf '\033[48;5;%dmM\033[m ' $attr + printf '\033[38;5;%dmE\033[m ' "$attr" + printf '\033[48;5;%dmM\033[m ' "$attr" done printf '\n' ;; *) - fail "unknown test name %s\n" $1 + fail "unknown test name %s\n" "$1" ;; esac } case $1 in baseline|direct) - baseline $0;; + baseline "$0";; *) - fail "unknown test argument %s\n" $1;; + fail "unknown test argument %s\n" "$1";; esac diff --git a/src/tests/emulation-back-tab.test b/src/tests/emulation-back-tab.test index cf95ed7..3b24357 100755 --- a/src/tests/emulation-back-tab.test +++ b/src/tests/emulation-back-tab.test @@ -4,11 +4,12 @@ # This test is for issue 539 on github. # -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline post + e2e-test "$0" baseline post exit fi @@ -29,17 +30,17 @@ baseline() post() { # Basic previously-failing case. - if grep -q 'hello, wurldo' $(basename $0).d/baseline.capture; then + if grep -q 'hello, wurldo' "$(basename "$0").d/baseline.capture"; then exit 1 fi - if ! grep -q 'hello, world' $(basename $0).d/baseline.capture; then + if ! grep -q 'hello, world' "$(basename "$0").d/baseline.capture"; then exit 99 fi # New test cases for new code. - if ! grep -q 'oello, wurld' $(basename $0).d/baseline.capture || - ! grep -q '9ello, wurld' $(basename $0).d/baseline.capture || - ! grep -q 'hello, wurld t' $(basename $0).d/baseline.capture || - ! grep -E -q '^ {79}#$' $(basename $0).d/baseline.capture; then + if ! grep -q 'oello, wurld' "$(basename "$0").d/baseline.capture" || + ! grep -q '9ello, wurld' "$(basename "$0").d/baseline.capture" || + ! grep -q 'hello, wurld t' "$(basename "$0").d/baseline.capture" || + ! grep -E -q '^ {79}#$' "$(basename "$0").d/baseline.capture"; then exit 1 fi exit 0 @@ -51,5 +52,5 @@ case $1 in post) post;; *) - fail "unknown test argument %s\n" $1;; + fail "unknown test argument %s\n" "$1";; esac diff --git a/src/tests/emulation-cursor-motion.test b/src/tests/emulation-cursor-motion.test index 8f44144..40ea8b9 100755 --- a/src/tests/emulation-cursor-motion.test +++ b/src/tests/emulation-cursor-motion.test @@ -5,11 +5,12 @@ # motions in Mosh. # -. $(dirname $0)/e2e-test-subrs +# shellcheck source=e2e-test-subrs +. "$(dirname "$0")/e2e-test-subrs" PATH=$PATH:.:$srcdir # Top-level wrapper. if [ $# -eq 0 ]; then - e2e-test $0 baseline direct verify + e2e-test "$0" baseline direct verify exit fi @@ -22,8 +23,8 @@ baseline() { printf '\033[H\033[J' - while read x y text; do - printf '\033[%d;%dH%s' $y $x $text + while read -r x y text; do + printf '\033[%d;%dH%s' "$y" "$x" "$text" sleepf done <