@@ -157,21 +157,38 @@ __bp_precmd_invoke_cmd() {
157
157
return
158
158
fi
159
159
local __bp_inside_precmd=1
160
+ __bp_invoke_precmd_functions " $__bp_last_ret_value " " $__bp_last_argument_prev_command "
160
161
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
161
172
# Invoke every function defined in our function array.
162
173
local precmd_function
174
+ local precmd_function_ret_value
175
+ local precmd_ret_value=0
163
176
for precmd_function in " ${precmd_functions[@]} " ; do
164
177
165
178
# Only execute this function if it actually exists.
166
179
# Test existence of functions with: declare -[Ff]
167
180
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 "
169
182
# Quote our function invocation to prevent issues with IFS
170
183
" $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
171
188
fi
172
189
done
173
190
174
- __bp_set_ret_value " $__bp_last_ret_value "
191
+ __bp_set_ret_value " $precmd_ret_value "
175
192
}
176
193
177
194
# Sets a return value in $?. We may want to get access to the $? variable in our
@@ -260,7 +277,27 @@ __bp_preexec_invoke_exec() {
260
277
return
261
278
fi
262
279
263
- # Invoke every function defined in our function array.
280
+ __bp_invoke_preexec_functions " ${__bp_last_ret_value:- } " " $__bp_last_argument_prev_command " " $this_command "
281
+ local preexec_ret_value=$?
282
+
283
+ # Restore the last argument of the last executed command, and set the return
284
+ # value of the DEBUG trap to be the return code of the last preexec function
285
+ # to return an error.
286
+ # If `extdebug` is enabled a non-zero return value from any preexec function
287
+ # will cause the user's command not to execute.
288
+ # Run `shopt -s extdebug` to enable
289
+ __bp_set_ret_value " $preexec_ret_value " " $__bp_last_argument_prev_command "
290
+ }
291
+
292
+ # This function invokes every function defined in our function array
293
+ # "preexec_function". This function receives the arguments $1 and $2 for $?
294
+ # and $_, respectively, which will be set for each preexec function. The third
295
+ # argument $3 specifies the user command that is going to be executed
296
+ # (corresponding to BASH_COMMAND in the DEBUG trap). This function returns the
297
+ # last non-zero exit status from the preexec functions. If there is no error,
298
+ # this function returns `0`.
299
+ __bp_invoke_preexec_functions () {
300
+ local lastexit=$1 lastarg=$2 this_command=$3
264
301
local preexec_function
265
302
local preexec_function_ret_value
266
303
local preexec_ret_value=0
@@ -269,7 +306,7 @@ __bp_preexec_invoke_exec() {
269
306
# Only execute each function if it actually exists.
270
307
# Test existence of function with: declare -[fF]
271
308
if type -t " $preexec_function " 1> /dev/null; then
272
- __bp_set_ret_value " ${__bp_last_ret_value :- } "
309
+ __bp_set_ret_value " $lastexit " " $lastarg "
273
310
# Quote our function invocation to prevent issues with IFS
274
311
" $preexec_function " " $this_command "
275
312
preexec_function_ret_value=" $? "
@@ -278,14 +315,7 @@ __bp_preexec_invoke_exec() {
278
315
fi
279
316
fi
280
317
done
281
-
282
- # Restore the last argument of the last executed command, and set the return
283
- # value of the DEBUG trap to be the return code of the last preexec function
284
- # to return an error.
285
- # If `extdebug` is enabled a non-zero return value from any preexec function
286
- # will cause the user's command not to execute.
287
- # Run `shopt -s extdebug` to enable
288
- __bp_set_ret_value " $preexec_ret_value " " $__bp_last_argument_prev_command "
318
+ __bp_set_ret_value " $preexec_ret_value "
289
319
}
290
320
291
321
__bp_install () {
0 commit comments