2
2
3
3
# Double include protection
4
4
case " ${INCLUDE_SEEN_PSST-} " in
5
- * _onexit_ * ) return
5
+ * :onexit: * ) return
6
6
esac
7
7
INCLUDE_SEEN_PSST=" ${INCLUDE_SEEN_PSST-} :onexit:"
8
8
@@ -16,8 +16,31 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:onexit:"
16
16
# shellcheck source=esc.inc.sh
17
17
. " $INCLUDE_PSST /basic/esc.inc.sh"
18
18
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
21
44
22
45
23
46
# #
@@ -31,8 +54,10 @@ INCLUDE_SEEN_PSST="${INCLUDE_SEEN_PSST-}:onexit:"
31
54
# codeToEval: String to be passed to `eval`.
32
55
#
33
56
# SAMPLE
34
- # on_exit_psst "rm -rf /tmp/someTempFile.tmp"
57
+ # onexit_psst "rm -rf /tmp/someTempFile.tmp"
35
58
#
59
+ # NOTE
60
+ # `codeToEval` must not contain the character `$RS_CHAR_PSST`.
36
61
onexit_psst ()
37
62
{
38
63
# We cannot use a subshell for this function as we need to register the
@@ -42,9 +67,15 @@ onexit_psst()
42
67
assert_argc_psst ' onexit_psst' 1 $#
43
68
assert_hasarg_psst ' onexit_psst' ' codeToEval' " $1 "
44
69
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
+
45
76
# codeToEval=$1
46
77
47
- if ! stack_exists_psst ' onExitStack_psst '
78
+ if [ $__OnExitSet_PSST -eq 0 ]
48
79
then
49
80
_oldtraps_onexit_psst=$( trap )
50
81
_oldexit_onexit_psst=$(
@@ -54,23 +85,55 @@ onexit_psst()
54
85
)
55
86
56
87
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 " )
59
90
# shellcheck disable=SC2064
60
91
trap " eval '$_oldexit_onexit_psst ' ; __onexit_run_psst" EXIT
61
92
else
62
93
# shellcheck disable=SC2064
63
94
trap ' __onexit_run_psst' EXIT
64
95
fi
65
96
97
+ __OnExitSet_PSST=1
98
+
66
99
unset _oldtraps_onexit_psst
67
100
unset _oldexit_onexit_psst
68
101
fi
69
102
70
- stack_push_psst ' onExitStack_psst ' " $1 "
103
+ __OnExit_PSST= " $1 $RS_CHAR_PSST$__OnExit_PSST "
71
104
}
72
105
73
106
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
+
74
137
# #
75
138
# FUNCTION
76
139
# __onexit_run_psst
@@ -87,9 +150,15 @@ __onexit_run_psst()
87
150
# variables in the main shell. Thus we need to be careful to not conflict
88
151
# when defining local variables.
89
152
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
95
164
}
0 commit comments