11# shellcheck shell=bash
22
33core.init () {
4+ # TODO: way below should error if any variables are not set
5+
46 if [ ${___global_bash_core_has_init__+x} ]; then
57 return
68 fi
@@ -10,10 +12,9 @@ core.init() {
1012 declare -ag ___global_shopt_stack___=()
1113}
1214
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
1718core.get_package_dir () {
1819 # local start_dir="${1:-"${BASH_SOURCE[1]}"}"
1920
@@ -30,13 +31,20 @@ core.trap_add() {
3031 local signal_spec=" $2 "
3132
3233 # 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
3342 local regex=' ^[0-9]+$'
3443 if [[ " $signal_spec " =~ $regex ]]; then
3544 printf ' %s\n' " Error: core.trap_add: Passing numbers for the signal specs is prohibited"
3645 return 1
3746 fi ; unset regex
3847 signal_spec=" ${signal_spec# SIG} "
39-
4048 if ! declare -f " $function " & > /dev/null; then
4149 printf ' %s\n' " Error: core.trap_add: Function '$function ' not defined" >&2
4250 return 1
@@ -46,21 +54,14 @@ core.trap_add() {
4654 ___global_trap_table___[" $signal_spec " ]=" ${___global_trap_table___[$signal_spec]} " $' \x1C ' " $function "
4755
4856 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
6465 trap " $global_trap_handler_name " " $signal_spec "
6566}
6667
@@ -69,13 +70,20 @@ core.trap_remove() {
6970 local signal_spec=" $2 "
7071
7172 # 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
7281 local regex=' ^[0-9]+$'
7382 if [[ " $signal_spec " =~ $regex ]]; then
7483 printf ' %s\n' " Error: core.trap_add: Passing numbers for the signal specs is prohibited"
7584 return 1
7685 fi ; unset regex
7786 signal_spec=" ${signal_spec# SIG} "
78-
7987 if ! declare -f " $function " & > /dev/null; then
8088 printf ' %s\n' " Error: core.trap_add: Function '$function ' not defined" >&2
8189 return 1
0 commit comments