1
1
# shellcheck shell=bash
2
2
3
3
core.init () {
4
+ # TODO: way below should error if any variables are not set
5
+
4
6
if [ ${___global_bash_core_has_init__+x} ]; then
5
7
return
6
8
fi
@@ -10,10 +12,9 @@ core.init() {
10
12
declare -ag ___global_shopt_stack___=()
11
13
}
12
14
13
- # @description Get version of the package, from the point of the
14
- # callsite. In other words, it returns the version of the package
15
- # that has the file containing the direct caller of this function
16
- # @set REPLY The full path to the directory
15
+ # @description Get version of the package, from the point of the callsite. In other words, it
16
+ # returns the version of the package that has the file containing the direct caller of this
17
+ # function @set REPLY The full path to the directory
17
18
core.get_package_dir () {
18
19
# local start_dir="${1:-"${BASH_SOURCE[1]}"}"
19
20
@@ -30,13 +31,20 @@ core.trap_add() {
30
31
local signal_spec=" $2 "
31
32
32
33
# validation
34
+ if [ -z " $function " ]; then
35
+ printf ' %s\n' " Error: core.trap_add: First argument cannot be empty"
36
+ return 1
37
+ fi
38
+ if [ -z " $signal_spec " ]; then
39
+ printf ' %s\n' " Error: core.trap_add: Second argument cannot be empty"
40
+ return 1
41
+ fi
33
42
local regex=' ^[0-9]+$'
34
43
if [[ " $signal_spec " =~ $regex ]]; then
35
44
printf ' %s\n' " Error: core.trap_add: Passing numbers for the signal specs is prohibited"
36
45
return 1
37
46
fi ; unset regex
38
47
signal_spec=" ${signal_spec# SIG} "
39
-
40
48
if ! declare -f " $function " & > /dev/null; then
41
49
printf ' %s\n' " Error: core.trap_add: Function '$function ' not defined" >&2
42
50
return 1
@@ -46,21 +54,14 @@ core.trap_add() {
46
54
___global_trap_table___[" $signal_spec " ]=" ${___global_trap_table___[$signal_spec]} " $' \x1C ' " $function "
47
55
48
56
local global_trap_handler_name=
49
- printf -v global_trap_handler_name ' %q' " __global_trap_${signal_spec} _handler___"
50
-
51
- eval ' if ! ' " $global_trap_handler_name " ' () {
52
- local trap_handlers= trap_handler=
53
- IFS=$' " '\x1C'" ' read -ra trap_handlers <<< "${___global_trap_table___[$signal_spec]}"
54
- for trap_func in "${trap_handlers[@]}"; do
55
- if declare -f "$trap_handler"; then
56
- :
57
- else
58
- printf "%s\n" "Warn: core.trap_add: Function ' " '\$ function'" ' registered for signal ' " '\$ signal_spec'" ' no longer exists" >&2
59
- fi
60
- done
61
- }; then
62
- false
63
- fi'
57
+ printf -v global_trap_handler_name ' %q' " ___global_trap_${signal_spec} _handler___"
58
+
59
+ if ! eval " $global_trap_handler_name () {
60
+ core.trap_common_global_handler " $signal_spec "
61
+ }" ; then
62
+ printf ' %s\n' " Error: core.trap_add: Could not eval function"
63
+ return 1
64
+ fi
64
65
trap " $global_trap_handler_name " " $signal_spec "
65
66
}
66
67
@@ -69,13 +70,20 @@ core.trap_remove() {
69
70
local signal_spec=" $2 "
70
71
71
72
# validation
73
+ if [ -z " $function " ]; then
74
+ printf ' %s\n' " Error: core.trap_add: First argument cannot be empty"
75
+ return 1
76
+ fi
77
+ if [ -z " $signal_spec " ]; then
78
+ printf ' %s\n' " Error: core.trap_add: Second argument cannot be empty"
79
+ return 1
80
+ fi
72
81
local regex=' ^[0-9]+$'
73
82
if [[ " $signal_spec " =~ $regex ]]; then
74
83
printf ' %s\n' " Error: core.trap_add: Passing numbers for the signal specs is prohibited"
75
84
return 1
76
85
fi ; unset regex
77
86
signal_spec=" ${signal_spec# SIG} "
78
-
79
87
if ! declare -f " $function " & > /dev/null; then
80
88
printf ' %s\n' " Error: core.trap_add: Function '$function ' not defined" >&2
81
89
return 1
0 commit comments