@@ -157,21 +157,38 @@ __bp_precmd_invoke_cmd() {
157157 return
158158 fi
159159 local __bp_inside_precmd=1
160+ __bp_invoke_precmd_functions " $__bp_last_ret_value " " $__bp_last_argument_prev_command "
160161
162+ __bp_set_ret_value " $__bp_last_ret_value " " $__bp_last_argument_prev_command "
163+ }
164+
165+ # This function invokes every function defined in our function array
166+ # "precmd_function". This function receives the arguments $1 and $2 for $? and
167+ # $_, respectively, which will be set for each precmd function. This function
168+ # returns the last non-zero exit status of the hook functions. If there is no
169+ # error, this function returns 0.
170+ __bp_invoke_precmd_functions () {
171+ local lastexit=$1 lastarg=$2
161172 # Invoke every function defined in our function array.
162173 local precmd_function
174+ local precmd_function_ret_value
175+ local precmd_ret_value=0
163176 for precmd_function in " ${precmd_functions[@]} " ; do
164177
165178 # Only execute this function if it actually exists.
166179 # Test existence of functions with: declare -[Ff]
167180 if type -t " $precmd_function " 1> /dev/null; then
168- __bp_set_ret_value " $__bp_last_ret_value " " $__bp_last_argument_prev_command "
181+ __bp_set_ret_value " $lastexit " " $lastarg "
169182 # Quote our function invocation to prevent issues with IFS
170183 " $precmd_function "
184+ precmd_function_ret_value=$?
185+ if [[ " $precmd_function_ret_value " != 0 ]]; then
186+ precmd_ret_value=" $precmd_function_ret_value "
187+ fi
171188 fi
172189 done
173190
174- __bp_set_ret_value " $__bp_last_ret_value "
191+ __bp_set_ret_value " $precmd_ret_value "
175192}
176193
177194# Sets a return value in $?. We may want to get access to the $? variable in our
@@ -258,7 +275,27 @@ __bp_preexec_invoke_exec() {
258275 return
259276 fi
260277
261- # Invoke every function defined in our function array.
278+ __bp_invoke_preexec_functions " ${__bp_last_ret_value:- } " " $__bp_last_argument_prev_command " " $this_command "
279+ local preexec_ret_value=$?
280+
281+ # Restore the last argument of the last executed command, and set the return
282+ # value of the DEBUG trap to be the return code of the last preexec function
283+ # to return an error.
284+ # If `extdebug` is enabled a non-zero return value from any preexec function
285+ # will cause the user's command not to execute.
286+ # Run `shopt -s extdebug` to enable
287+ __bp_set_ret_value " $preexec_ret_value " " $__bp_last_argument_prev_command "
288+ }
289+
290+ # This function invokes every function defined in our function array
291+ # "preexec_function". This function receives the arguments $1 and $2 for $?
292+ # and $_, respectively, which will be set for each preexec function. The third
293+ # argument $3 specifies the user command that is going to be executed
294+ # (corresponding to BASH_COMMAND in the DEBUG trap). This function returns the
295+ # last non-zero exit status from the preexec functions. If there is no error,
296+ # this function returns `0`.
297+ __bp_invoke_preexec_functions () {
298+ local lastexit=$1 lastarg=$2 this_command=$3
262299 local preexec_function
263300 local preexec_function_ret_value
264301 local preexec_ret_value=0
@@ -267,7 +304,7 @@ __bp_preexec_invoke_exec() {
267304 # Only execute each function if it actually exists.
268305 # Test existence of function with: declare -[fF]
269306 if type -t " $preexec_function " 1> /dev/null; then
270- __bp_set_ret_value " ${__bp_last_ret_value :- } "
307+ __bp_set_ret_value " $lastexit " " $lastarg "
271308 # Quote our function invocation to prevent issues with IFS
272309 " $preexec_function " " $this_command "
273310 preexec_function_ret_value=" $? "
@@ -276,14 +313,7 @@ __bp_preexec_invoke_exec() {
276313 fi
277314 fi
278315 done
279-
280- # Restore the last argument of the last executed command, and set the return
281- # value of the DEBUG trap to be the return code of the last preexec function
282- # to return an error.
283- # If `extdebug` is enabled a non-zero return value from any preexec function
284- # will cause the user's command not to execute.
285- # Run `shopt -s extdebug` to enable
286- __bp_set_ret_value " $preexec_ret_value " " $__bp_last_argument_prev_command "
316+ __bp_set_ret_value " $preexec_ret_value "
287317}
288318
289319__bp_install () {
0 commit comments