Skip to content
This repository was archived by the owner on May 20, 2024. It is now read-only.

Commit 5e0fc9e

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Fail on unset variable expansion
1 parent 711a46f commit 5e0fc9e

29 files changed

+130
-121
lines changed

lib/psst/basic.inc.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_basic.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _basic.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _basic.inc.sh_"
1010

1111

1212
# Ensure INCLUDE_PSST is set
13-
if [ -z "$INCLUDE_PSST" ]
13+
if [ -z "${INCLUDE_PSST-}" ]
1414
then
1515
echo "INCLUDE_PSST not set!" >&2
1616
exit 1

lib/psst/basic/assert.inc.sh

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_assert.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _assert.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _assert.inc.sh_"
1010

1111

1212
##
@@ -69,11 +69,9 @@ assert_argc_psst()
6969
exit 127
7070
fi
7171

72-
_func_psst="assert_argc_psst"
73-
assert_hasarg_psst "$_func_psst" "func" "$1"
74-
assert_hasarg_psst "$_func_psst" "expected" "$2"
75-
assert_hasarg_psst "$_func_psst" "actual" "$3"
76-
unset _func_psst
72+
assert_hasarg_psst "assert_argc_psst" "func" "$1"
73+
assert_hasarg_psst "assert_argc_psst" "expected" "$2"
74+
assert_hasarg_psst "assert_argc_psst" "actual" "$3"
7775

7876
if [ "$2" -ne "$3" ]
7977
then
@@ -108,12 +106,10 @@ assert_minargc_psst()
108106
# shell in case an assertion is thrown. Thus we need to be careful to not
109107
# conflict when defining local variables.
110108

111-
_func_psst="assert_minargc_psst"
112-
assert_argc_psst "$_func_psst" 3 $#
113-
assert_hasarg_psst "$_func_psst" "func" "$1"
114-
assert_hasarg_psst "$_func_psst" "min" "$2"
115-
assert_hasarg_psst "$_func_psst" "actual" "$3"
116-
unset _func_psst
109+
assert_argc_psst "assert_minargc_psst" 3 $#
110+
assert_hasarg_psst "assert_minargc_psst" "func" "$1"
111+
assert_hasarg_psst "assert_minargc_psst" "min" "$2"
112+
assert_hasarg_psst "assert_minargc_psst" "actual" "$3"
117113

118114
if [ "$2" -gt "$3" ]
119115
then
@@ -148,12 +144,10 @@ assert_maxargc_psst()
148144
# shell in case an assertion is thrown. Thus we need to be careful to not
149145
# conflict when defining local variables.
150146

151-
_func_psst="assert_maxargc_psst"
152-
assert_argc_psst "$_func_psst" 3 $#
153-
assert_hasarg_psst "$_func_psst" "func" "$1"
154-
assert_hasarg_psst "$_func_psst" "max" "$2"
155-
assert_hasarg_psst "$_func_psst" "actual" "$3"
156-
unset _func_psst
147+
assert_argc_psst "assert_maxargc_psst" 3 $#
148+
assert_hasarg_psst "assert_maxargc_psst" "func" "$1"
149+
assert_hasarg_psst "assert_maxargc_psst" "max" "$2"
150+
assert_hasarg_psst "assert_maxargc_psst" "actual" "$3"
157151

158152
if [ "$2" -lt "$3" ]
159153
then
@@ -203,7 +197,7 @@ assert_hasarg_psst()
203197
fi
204198

205199
if { [ $# = 3 ] && [ -z "$3" ] ; } \
206-
|| { [ $# = 2 ] && [ -z "$(eval "echo \$$2")" ] ; }
200+
|| { [ $# = 2 ] && [ -z "$( eval "printf '%s' \"\${$2-}\"" )" ] ; }
207201
then
208202
printf "%s: %s of %s must not be empty!\n" \
209203
"Assertion fail" "Argument \"$2\"" "function \"$1\"" >&2

lib/psst/basic/chkcmd.inc.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_chkcmd.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _chkcmd.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _chkcmd.inc.sh_"
1010

1111

1212
# shellcheck source=assert.inc.sh
@@ -40,7 +40,7 @@ chkcmd_psst()
4040
assert_minargc_psst "$func" 1 $#
4141
assert_hasarg_psst "$func" "cmd" "$1"
4242

43-
while [ -n "$1" ]; do
43+
while [ -n "${1-}" ]; do
4444
if ! command -v "$1" >/dev/null; then
4545
return 1
4646
fi

lib/psst/basic/conv.inc.sh

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_conv.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _conv.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _conv.inc.sh_"
1010

1111

1212
# shellcheck source=test.inc.sh
@@ -38,11 +38,11 @@ INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _conv.inc.sh_"
3838
#
3939
conv_chr_psst()
4040
(
41-
charCode=$1
41+
func="chr_psst"
42+
assert_argc_psst "$func" 1 $#
43+
assert_hasarg_psst "$func" "charCode" "$1"
4244

43-
func_psst="chr_psst"
44-
assert_argc_psst "$func_psst" 1 $#
45-
assert_hasarg_psst "$func_psst" "charCode" "$charCode"
45+
charCode=$1
4646

4747
test_is_int_psst "$charCode" || return 1
4848
{ [ "$charCode" -lt 0 ] || [ "$charCode" -gt 255 ]; } && return 2
@@ -78,11 +78,11 @@ conv_chr_psst()
7878
#
7979
conv_ord_psst()
8080
(
81-
char=$1
82-
8381
func_psst="ord_psst"
8482
assert_argc_psst "$func_psst" 1 $#
8583

84+
char=$1
85+
8686
[ -n "$char" ] || return 1
8787
[ ${#char} -eq 1 ] || return 2
8888

lib/psst/basic/esc.inc.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_esc.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _esc.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _esc.inc.sh_"
1010

1111

1212
# shellcheck source=assert.inc.sh
@@ -35,10 +35,10 @@ INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _esc.inc.sh_"
3535
#
3636
esc_for_sq_psst()
3737
(
38-
value=$1
39-
4038
func="esc_for_sq_psst"
4139
assert_argc_psst "$func" 1 $#
4240

41+
value=$1
42+
4343
printf "%s" "$value" | sed "s/'/'\\\''/g"
4444
)

lib/psst/basic/globals.inc.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_globals.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _globals.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _globals.inc.sh_"
1010

1111

1212
##
@@ -44,7 +44,7 @@ readonly FS_CHAR_PSST
4444
# Falls back to 80 in case real width cannot be determined.
4545
#
4646
TERMINAL_WIDTH_PSST=80
47-
if [ -n "$COLUMNS" ]
47+
if [ -n "${COLUMNS-}" ]
4848
then
4949
TERMINAL_WIDTH_PSST="$COLUMNS"
5050
elif tput cols >/dev/null 2>&1

lib/psst/basic/ifs.inc.sh

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_ifs.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _ifs.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _ifs.inc.sh_"
1010

1111

1212
# shellcheck source=assert.inc.sh
@@ -43,9 +43,7 @@ ifs_set_psst()
4343
# variables in the main shell. Thus we need to be careful to not conflict
4444
# when defining local variables.
4545

46-
_func_psst="ifs_set_psst"
47-
assert_argc_psst "$_func_psst" 1 $#
48-
unset _func_psst
46+
assert_argc_psst "ifs_set_psst" 1 $#
4947

5048
stack_push_psst "$IFS" "ifsStack_psst" || return 1
5149
IFS=$1
@@ -72,8 +70,7 @@ ifs_restore_psst()
7270
# variables in the main shell. Thus we need to be careful to not conflict
7371
# when defining local variables.
7472

75-
_func_psst="ifs_restore_psst"
76-
assert_argc_psst "$_func_psst" 0 $#
73+
assert_argc_psst "ifs_restore_psst" 0 $#
7774
unset _func_psst
7875

7976
if ! stack_pop_psst "ifsStack_psst" IFS

lib/psst/basic/onexit.inc.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_onexit.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _onexit.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _onexit.inc.sh_"
1010

1111

1212
# shellcheck source=stack.inc.sh
@@ -28,17 +28,16 @@ INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _onexit.inc.sh_"
2828
#
2929
onexit_psst()
3030
{
31-
#codeToEval=$1
32-
3331
# We cannot use a subshell for this function as we need to register the
3432
# variables in the main shell. Thus we need to be careful to not conflict
3533
# when defining local variables.
3634

37-
_func_psst="onexit_psst"
38-
assert_argc_psst "$_func_psst" 1 $#
39-
assert_hasarg_psst "$_func_psst" "codeToEval" "$1"
35+
assert_argc_psst "onexit_psst" 1 $#
36+
assert_hasarg_psst "onexit_psst" "codeToEval" "$1"
4037
unset _func_psst
4138

39+
#codeToEval=$1
40+
4241
if ! stack_exists_psst "onExitStack_psst"
4342
then
4443
_oldtraps_psst=$(trap)

lib/psst/basic/perror.inc.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_perror.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _perror.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _perror.inc.sh_"
1010

1111

1212
# shellcheck source=print.inc.sh

lib/psst/basic/print.inc.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#!/usr/bin/env sh
22

33
# Double include protection
4-
case "$INCLUDE_SEEN_PSST" in
4+
case "${INCLUDE_SEEN_PSST-}" in
55
*_print.inc.sh_*)
66
return
77
;;
88
esac
9-
INCLUDE_SEEN_PSST="$INCLUDE_SEEN_PSST _print.inc.sh_"
9+
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-} _print.inc.sh_"
1010

1111

1212
# shellcheck source=globals.inc.sh
@@ -69,13 +69,13 @@ print_psst()
6969
#
7070
print_i_psst()
7171
(
72-
indent="$1"
73-
shift
74-
7572
func="print_i_psst"
7673
assert_minargc_psst "$func" 1 $#
7774
assert_hasarg_psst "$func" "indent" "$1"
7875

76+
indent="$1"
77+
shift
78+
7979
IFS=""
8080
string="$*"
8181

0 commit comments

Comments
 (0)