@@ -18,34 +18,29 @@ core.trap_add() {
18
18
fi
19
19
local function=" $1 "
20
20
21
- # validation
21
+ # Validation
22
22
if [ -z " $function " ]; then
23
- core.print_error ' First argument must not be empty'
24
- return 1
23
+ core.panic ' First argument must not be empty'
25
24
fi
26
25
27
26
if (( $# <= 1 )) ; then
28
- core.print_error ' Must specify at least one signal'
29
- return 1
27
+ core.panic ' Must specify at least one signal'
30
28
fi
31
29
for signal_spec in " ${@: 2} " ; do
32
30
if [ -z " $signal_spec " ]; then
33
- core.print_error ' Signal must not be an empty string'
34
- return 1
31
+ core.panic ' Signal must not be an empty string'
35
32
fi
36
33
37
34
local regex=' ^[0-9]+$'
38
35
if [[ " $signal_spec " =~ $regex ]]; then
39
- core.print_error ' Passing numbers for the signal specs is prohibited'
40
- return 1
36
+ core.panic ' Passing numbers for the signal specs is prohibited'
41
37
fi ; unset regex
42
38
signal_spec=${signal_spec# SIG}
43
39
if ! declare -f " $function " & > /dev/null; then
44
- core.print_error " Function '$function ' is not defined"
45
- return 1
40
+ core.panic " Function '$function ' is not defined"
46
41
fi
47
42
48
- # start
43
+ # Start
49
44
___global_trap_table___[" $signal_spec " ]=" ${___global_trap_table___[$signal_spec]} " $' \x1C ' " $function "
50
45
51
46
# rho (WET)
@@ -55,8 +50,7 @@ core.trap_add() {
55
50
if ! eval " $global_trap_handler_name () {
56
51
core.util.trap_handler_common '$signal_spec '
57
52
}" ; then
58
- core.print_error ' Could not eval function'
59
- return 1
53
+ core.panic ' Could not eval function'
60
54
fi
61
55
# shellcheck disable=SC2064
62
56
trap " $global_trap_handler_name " " $signal_spec "
@@ -78,34 +72,29 @@ core.trap_remove() {
78
72
fi
79
73
local function=" $1 "
80
74
81
- # validation
75
+ # Validation
82
76
if [ -z " $function " ]; then
83
- core.print_error ' First argument must not be empty'
84
- return 1
77
+ core.panic ' First argument must not be empty'
85
78
fi
86
79
87
80
if (( $# <= 1 )) ; then
88
- core.print_error ' Must specify at least one signal'
89
- return 1
81
+ core.panic ' Must specify at least one signal'
90
82
fi
91
83
for signal_spec in " ${@: 2} " ; do
92
84
if [ -z " $signal_spec " ]; then
93
- core.print_error ' Signal must not be an empty string'
94
- return 1
85
+ core.panic ' Signal must not be an empty string'
95
86
fi
96
87
97
88
local regex=' ^[0-9]+$'
98
89
if [[ " $signal_spec " =~ $regex ]]; then
99
- core.print_error ' Passing numbers for the signal specs is prohibited'
100
- return 1
90
+ core.panic ' Passing numbers for the signal specs is prohibited'
101
91
fi ; unset regex
102
92
signal_spec=" ${signal_spec# SIG} "
103
93
if ! declare -f " $function " & > /dev/null; then
104
- core.print_error " Function '$function ' is not defined"
105
- return 1
94
+ core.panic " Function '$function ' is not defined"
106
95
fi
107
96
108
- # start
97
+ # Start
109
98
local -a trap_handlers=()
110
99
local new_trap_handlers=
111
100
IFS=$' \x1C ' read -ra trap_handlers <<< " ${___global_trap_table___[$signal_spec]}"
@@ -147,13 +136,11 @@ core.shopt_push() {
147
136
local shopt_name=" $2 "
148
137
149
138
if [ -z " $shopt_action " ]; then
150
- core.print_error ' First argument cannot be empty'
151
- return 1
139
+ core.panic ' First argument cannot be empty'
152
140
fi
153
141
154
142
if [ -z " $shopt_name " ]; then
155
- core.print_error ' Second argument cannot be empty'
156
- return 1
143
+ core.panic ' Second argument cannot be empty'
157
144
fi
158
145
159
146
local -i previous_shopt_errcode=
@@ -165,17 +152,14 @@ core.shopt_push() {
165
152
166
153
if [ " $shopt_action " = ' -s' ]; then
167
154
if shopt -s " $shopt_name " ; then : ; else
168
- # on error, option will not be set
169
- return $?
155
+ core.panic " Could not set shopt option" $?
170
156
fi
171
157
elif [ " $shopt_action " = ' -u' ]; then
172
158
if shopt -u " $shopt_name " ; then : ; else
173
- # on error, option will not be set
174
- return $?
159
+ core.panic " Could not unset shopt option" $?
175
160
fi
176
161
else
177
- core.print_error " Accepted actions are either '-s' or '-u'"
178
- return 1
162
+ core.panic " Accepted actions are either '-s' or '-u'"
179
163
fi
180
164
181
165
if (( previous_shopt_errcode == 0 )) ; then
@@ -197,23 +181,19 @@ core.shopt_pop() {
197
181
fi
198
182
199
183
if (( ${# ___global_shopt_stack___[@]} == 0 )) ; then
200
- core.print_error ' Unable to pop as nothing is in the shopt stack'
201
- return 1
184
+ core.panic ' Unable to pop as nothing is in the shopt stack'
202
185
fi
203
186
204
187
if (( ${# ___global_shopt_stack___[@]} & 1 )) ; then
205
- core.print_error ' Shopt stack is malformed'
206
- return 1
188
+ core.panic ' Shopt stack is malformed'
207
189
fi
208
190
209
191
# Stack now guaranteed to have at least 2 elements (so the following accessors won't error)
210
192
local shopt_action=" ${___global_shopt_stack___[-2]} "
211
193
local shopt_name=" ${___global_shopt_stack___[-1]} "
212
194
213
195
if shopt -u " $shopt_name " ; then : ; else
214
- local errcode=$?
215
- core.print_error ' Could not restore previous shopt option'
216
- return $errcode
196
+ core.panic ' Could not restore previous shopt option' $?
217
197
fi
218
198
219
199
___global_shopt_stack___=(" ${___global_shopt_stack___[@]:: ${# ___global_shopt_stack___[@]} -2} " )
@@ -232,13 +212,11 @@ core.err_set() {
232
212
ERRCODE=$1
233
213
ERR=$2
234
214
else
235
- core.print_error ' Incorrect function arguments'
236
- return 1
215
+ core.panic ' Incorrect function arguments'
237
216
fi
238
217
239
218
if [ -z " $ERR " ]; then
240
- core.print_error " Argument for 'ERR' cannot be empty"
241
- return 1
219
+ core.panic " Argument for 'ERR' cannot be empty"
242
220
fi
243
221
}
244
222
@@ -307,6 +285,7 @@ core.print_stacktrace() {
307
285
done ; unset -v i
308
286
309
287
if [ " $cd_failed " = ' yes' ]; then
288
+ # Do NOT 'core.panic'
310
289
core.print_error " A 'cd' failed, so the stacktrace may include relative paths"
311
290
fi
312
291
} >&2
@@ -337,7 +316,16 @@ core.print_info() {
337
316
338
317
# @description Use when a serious fault occurs. It will print the current ERR (if it exists)
339
318
core.panic () {
340
- local code=" ${1-1} "
319
+ local code=' 1'
320
+
321
+ if [[ $1 =~ [0-9]+ ]]; then
322
+ code=$1
323
+ elif [ -n " $1 " ]; then
324
+ if [ -n " $2 " ]; then
325
+ code=$2
326
+ fi
327
+ core.print_error " $1 "
328
+ fi
341
329
342
330
if core.err_exists; then
343
331
core.err_print
@@ -390,7 +378,7 @@ core.get_package_info() {
390
378
local toml_file=" $basalt_package_dir /basalt.toml"
391
379
392
380
if [ ! -f " $toml_file " ]; then
393
- core.print_error " File '$toml_file ' could not be found"
381
+ core.panic " File '$toml_file ' could not be found"
394
382
fi
395
383
396
384
local regex=$' ^[ \t ]*' " ${key_name} " $' [ \t ]*=[ \t ]*[\' "](.*)[\' "]'
0 commit comments