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

Commit 588efca

Browse files
CodingMarkusCodingMarkus
CodingMarkus
authored and
CodingMarkus
committed
Support removing on-exit calls
1 parent f6363e2 commit 588efca

File tree

2 files changed

+131
-18
lines changed

2 files changed

+131
-18
lines changed

lib/psst/basic/onexit.inc.sh

Lines changed: 82 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# Double include protection
44
case "${INCLUDE_SEEN_PSST-}" in
5-
*_onexit_*) return
5+
*:onexit:*) return
66
esac
77
INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:onexit:"
88

@@ -16,8 +16,31 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:onexit:"
1616
# shellcheck source=esc.inc.sh
1717
. "$INCLUDE_PSST/basic/esc.inc.sh"
1818

19-
# shellcheck source=stack.inc.sh
20-
. "$INCLUDE_PSST/basic/stack.inc.sh"
19+
# shellcheck source=ifs.inc.sh
20+
. "$INCLUDE_PSST/basic/ifs.inc.sh"
21+
22+
# shellcheck source=list.inc.sh
23+
. "$INCLUDE_PSST/basic/list.inc.sh"
24+
25+
26+
##
27+
# VARIABLE
28+
# __OnExit_PSST
29+
#
30+
# SUMMARY
31+
# List storing commands to be excuted on exit.
32+
#
33+
__OnExit_PSST=''
34+
35+
36+
##
37+
# VARIABLE
38+
# __OnExitSet_PSST
39+
#
40+
# SUMMARY
41+
# Whether on exit trap has been set already
42+
#
43+
__OnExitSet_PSST=0
2144

2245

2346
##
@@ -31,8 +54,10 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:onexit:"
3154
# codeToEval: String to be passed to `eval`.
3255
#
3356
# SAMPLE
34-
# on_exit_psst "rm -rf /tmp/someTempFile.tmp"
57+
# onexit_psst "rm -rf /tmp/someTempFile.tmp"
3558
#
59+
# NOTE
60+
# `codeToEval` must not contain the character `$RS_CHAR_PSST`.
3661
onexit_psst()
3762
{
3863
# We cannot use a subshell for this function as we need to register the
@@ -42,9 +67,15 @@ onexit_psst()
4267
assert_argc_psst 'onexit_psst' 1 $#
4368
assert_hasarg_psst 'onexit_psst' 'codeToEval' "$1"
4469

70+
# shellcheck disable=SC2016
71+
case $1 in
72+
*$RS_CHAR_PSST*) assert_func_fail_psst "onexit_psst" \
73+
'Argument "codeToEval" must not contain $RS_CHAR_PSST'
74+
esac
75+
4576
#codeToEval=$1
4677

47-
if ! stack_exists_psst 'onExitStack_psst'
78+
if [ $__OnExitSet_PSST -eq 0 ]
4879
then
4980
_oldtraps_onexit_psst=$( trap )
5081
_oldexit_onexit_psst=$(
@@ -54,23 +85,55 @@ onexit_psst()
5485
)
5586

5687
if [ -n "$_oldexit_onexit_psst" ]
57-
then
58-
_oldexit_onexit_psst=$( esc_for_sq_psst "$_oldexit_onexit_psst" )
88+
then
89+
_oldexit_onexit_psst=$( esc_squotes_psst "$_oldexit_onexit_psst" )
5990
# shellcheck disable=SC2064
6091
trap "eval '$_oldexit_onexit_psst' ; __onexit_run_psst" EXIT
6192
else
6293
# shellcheck disable=SC2064
6394
trap '__onexit_run_psst' EXIT
6495
fi
6596

97+
__OnExitSet_PSST=1
98+
6699
unset _oldtraps_onexit_psst
67100
unset _oldexit_onexit_psst
68101
fi
69102

70-
stack_push_psst 'onExitStack_psst' "$1"
103+
__OnExit_PSST="$1$RS_CHAR_PSST$__OnExit_PSST"
71104
}
72105

73106

107+
108+
##
109+
# FUNCTION
110+
# onexit_remove_psst <codeNotToEval>
111+
#
112+
# SUMMARY
113+
# Remove code to be executed when process or subprocess exists.
114+
#
115+
# PARAMETERS
116+
# codeNotToEval: Code to be removed from the onexit list.
117+
#
118+
# SAMPLE
119+
# # Temp file has already been deleted
120+
# onexit_remove_psst "rm -rf /tmp/someTempFile.tmp"
121+
#
122+
onexit_remove_psst()
123+
{
124+
# We cannot use a subshell for this function as we need to register the
125+
# variables in the main shell. Thus we need to be careful to not conflict
126+
# when defining local variables.
127+
128+
assert_argc_psst 'onexit_remove_psst' 1 $#
129+
assert_hasarg_psst 'onexit_remove_psst' 'codeNotToEval' "$1"
130+
131+
#codeNotToEval=$1
132+
__OnExit_PSST=$( list_remove_value_psst "$__OnExit_PSST" "$1" )
133+
}
134+
135+
136+
74137
##
75138
# FUNCTION
76139
# __onexit_run_psst
@@ -87,9 +150,15 @@ __onexit_run_psst()
87150
# variables in the main shell. Thus we need to be careful to not conflict
88151
# when defining local variables.
89152

90-
_evalMe_psst=
91-
while stack_pop_psst 'onExitStack_psst' _evalMe_psst
92-
do
93-
eval "$_evalMe_psst"
94-
done
153+
ifs_set_psst "$RS_CHAR_PSST"
154+
for _eval_onexit_psst in $__OnExit_PSST
155+
do
156+
eval "$_eval_onexit_psst"
157+
done
158+
159+
__OnExit_PSST=''
160+
__OnExitSet_PSST=0
161+
unset _eval_onexit_psst
162+
163+
set -e
95164
}

test/basic/bin/test_onexit.sh

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ fi
1414
# shellcheck source=../../../lib/psst/basic.inc.sh
1515
. "$INCLUDE_PSST/basic.inc.sh"
1616

17+
1718
# =============================================================================
19+
# On Exit
1820

1921
# Must accept exactly one argument
2022
set +e
@@ -31,15 +33,57 @@ set +e
3133
[ $? = 127 ] || test_fail_psst $LINENO
3234
set -e
3335

36+
# Argument must not contain $RS_CHAR_PSST
37+
set +e
38+
( onexit_psst "$ $RS_CHAR_PSST" 2>/dev/null )
39+
[ $? = 127 ] || test_fail_psst $LINENO
40+
set -e
41+
42+
3443
# Code gets execute on exit
35-
[ "$( onexit_psst "echo test_success" )" = "test_success" ] \
44+
[ "$( onexit_psst 'printf "test_success"' )" = "test_success" ] \
3645
|| test_fail_psst $LINENO
3746

3847

3948
# Multiple evaluations are possible
4049
multi=$(
41-
onexit_psst "printf \"test1\""
42-
onexit_psst "printf \"test2\""
50+
onexit_psst 'printf "test1"'
51+
onexit_psst 'printf "test2"'
4352
)
44-
{ [ "$multi" = "test1test2" ] || [ "$multi" = "test2test1" ]; } \
45-
|| test_fail_psst $LINENO
53+
[ "$multi" = "test2test1" ] || test_fail_psst $LINENO
54+
55+
56+
57+
# =============================================================================
58+
# On Exit Remvoe
59+
60+
# Must accept exactly one argument
61+
set +e
62+
( onexit_remove_psst 2>/dev/null )
63+
[ $? = 127 ] || test_fail_psst $LINENO
64+
65+
( onexit_remove_psst 1 2 2>/dev/null )
66+
[ $? = 127 ] || test_fail_psst $LINENO
67+
set -e
68+
69+
# Argument must not be empty
70+
set +e
71+
( onexit_remove_psst '' 2>/dev/null )
72+
[ $? = 127 ] || test_fail_psst $LINENO
73+
set -e
74+
75+
76+
# Removed code is not executed
77+
multi=$(
78+
onexit_psst 'printf "test1"'
79+
onexit_psst 'printf "test2"'
80+
onexit_remove_psst 'printf "test2"'
81+
)
82+
[ "$multi" = "test1" ] || test_fail_psst $LINENO
83+
84+
multi=$(
85+
onexit_psst 'printf "test1"'
86+
onexit_psst 'printf "test2"'
87+
onexit_remove_psst 'printf "test1"'
88+
)
89+
[ "$multi" = "test2" ] || test_fail_psst $LINENO

0 commit comments

Comments
 (0)