-
Notifications
You must be signed in to change notification settings - Fork 0
/
traps.sh
executable file
·176 lines (161 loc) · 3.76 KB
/
traps.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
# SPDX-License-Identifier: MIT
# Copyright (C) 2022 Da Xue
TRAPS_DEBUG=0
TRAPS_SIGNAL=EXIT
declare -a TRAPS_PARAMS
declare -a TRAPS_LENGTHS
TRAPS_LENGTH=
function traps_start {
if [ -z "$TRAPS_LENGTH" ]; then
TRAPS_LENGTH=0
if [ "$TRAPS_SIGNAL" = "ERR" ]; then
trap 'traps_exit $LINENO ${#FUNCNAME[@]} "${BASH_SOURCE[@]}" "${BASH_LINENO[@]}" "${FUNCNAME[@]}"' $TRAPS_SIGNAL
else
trap traps_exit $TRAPS_SIGNAL
fi
else
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: duplicate call" >&2
fi
return 1
fi
}
function traps_isEmpty {
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not started" >&2
fi
return 2
fi
if [ $TRAPS_LENGTH -gt 0 ]; then
return 1
fi
return 0
}
function traps_push {
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not started" >&2
fi
return 1
fi
TRAPS_PARAMS+=("$@")
TRAPS_LENGTHS+=($#)
((TRAPS_LENGTH=TRAPS_LENGTH+1))
}
function traps_pop {
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not started" >&2
fi
return 1
elif [ $TRAPS_LENGTH -gt 0 ]; then
local trap_length=${TRAPS_LENGTHS[@]: -1}
local trap_cmd=("${TRAPS_PARAMS[@]: -$trap_length}")
local traps_params_end=$((${#TRAPS_PARAMS[@]}-trap_length))
TRAPS_PARAMS=("${TRAPS_PARAMS[@]:0:$traps_params_end}")
TRAPS_LENGTH=$((TRAPS_LENGTH-1))
TRAPS_LENGTHS=(${TRAPS_LENGTHS[@]:0:$TRAPS_LENGTH})
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: ${trap_cmd[@]}" >&2
fi
"${trap_cmd[@]}"
else
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps empty" >&2
fi
return 1
fi
}
function traps_popUntilLength {
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not started" >&2
fi
return 1
elif [ "$1" -gt $TRAPS_LENGTH ]; then
echo "$FUNCNAME: traps length below target" >&2
return 1
fi
while [ "$TRAPS_LENGTH" -gt "$1" ]; do
traps_pop
done
}
function traps_drop {
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not started" >&2
fi
return 1
elif [ $TRAPS_LENGTH -gt 0 ]; then
local trap_length=${TRAPS_LENGTHS[@]: -1}
local traps_params_end=$((${#TRAPS_PARAMS[@]}-trap_length))
TRAPS_PARAMS=("${TRAPS_PARAMS[@]:0:$traps_params_end}")
TRAPS_LENGTH=$((TRAPS_LENGTH-1))
TRAPS_LENGTHS=(${TRAPS_LENGTHS[@]:0:$TRAPS_LENGTH})
else
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps empty" >&2
fi
return 1
fi
}
function traps_stop {
if [ "$TRAPS_LENGTH" = "0" ]; then
TRAPS_LENGTH=
trap - $TRAPS_SIGNAL
else
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: registered traps" >&2
traps_diag >&2
fi
return 1
fi
}
function traps_cancel {
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not started" >&2
fi
return 1
fi
TRAPS_PARAMS=()
TRAPS_LENGTHS=()
TRAPS_LENGTH=
trap - $TRAPS_SIGNAL
}
function traps_exit {
if [ "$TRAPS_DEBUG" -eq 1 ] && [ "$TRAPS_SIGNAL" = "ERR" ]; then
local IFS=' '
trap "traps_exit_now ${*@Q}" EXIT
else
trap - $TRAPS_SIGNAL
fi
if [ -z "$TRAPS_LENGTH" ]; then
if [ "$TRAPS_DEBUG" -eq 1 ]; then
echo "$FUNCNAME: traps not initiated" >&2
fi
return 1
fi
while [ "$TRAPS_LENGTH" -gt 0 ]; do
traps_pop
done
return 1
}
function traps_exit_now {
echo "$FUNCNAME: $TRAPS_SIGNAL in $3 line $1" >&2
local i=1
while [ $i -lt $2 ]; do
local i_func=$(($2*2+$i+2))
local i_file=$(($i+3))
local i_line=$(($2+$i+2))
echo "$FUNCNAME: trace: ${@:$i_func:1} in ${@:$i_file:1} line ${@:$i_line:1}" >&2
local i=$((i+1))
done
}
function traps_diag {
echo "$FUNCNAME params: ${TRAPS_PARAMS[@]}"
echo "$FUNCNAME lengths: ${TRAPS_LENGTHS[@]}"
echo "$FUNCNAME length: $TRAPS_LENGTH"
}