Skip to content

Commit cbd3390

Browse files
committed
Add APIs for external precmd/preexec integrations
* Rename public functions and variables as "bash_preexec_*" * Remove the compatibility variable name "__bp_install_string" * Add a note on the "trace" function attribute * Preserve the previous exit status and argument * Test the installation of convenience functions * Test "bash_preexec_uninstall"
1 parent befc702 commit cbd3390

File tree

2 files changed

+122
-26
lines changed

2 files changed

+122
-26
lines changed

bash-preexec.sh

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ __bp_inside_precmd=0
7171
__bp_inside_preexec=0
7272

7373
# Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
74-
__bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
74+
bash_preexec_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
75+
76+
# The command string that is registered to the DEBUG trap.
77+
# shellcheck disable=SC2016
78+
bash_preexec_trapdebug_string='__bp_preexec_invoke_exec "$_"'
7579

7680
# Fails if any of the given variables are readonly
7781
# Reference https://stackoverflow.com/a/4441178
@@ -157,7 +161,7 @@ __bp_precmd_invoke_cmd() {
157161
return
158162
fi
159163
local __bp_inside_precmd=1
160-
__bp_invoke_precmd_functions "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
164+
bash_preexec_invoke_precmd_functions "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
161165

162166
__bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
163167
}
@@ -167,7 +171,7 @@ __bp_precmd_invoke_cmd() {
167171
# $_, respectively, which will be set for each precmd function. This function
168172
# returns the last non-zero exit status of the hook functions. If there is no
169173
# error, this function returns 0.
170-
__bp_invoke_precmd_functions() {
174+
bash_preexec_invoke_precmd_functions() {
171175
local lastexit=$1 lastarg=$2
172176
# Invoke every function defined in our function array.
173177
local precmd_function
@@ -277,7 +281,7 @@ __bp_preexec_invoke_exec() {
277281
return
278282
fi
279283

280-
__bp_invoke_preexec_functions "${__bp_last_ret_value:-}" "$__bp_last_argument_prev_command" "$this_command"
284+
bash_preexec_invoke_preexec_functions "${__bp_last_ret_value:-}" "$__bp_last_argument_prev_command" "$this_command"
281285
local preexec_ret_value=$?
282286

283287
# Restore the last argument of the last executed command, and set the return
@@ -296,7 +300,7 @@ __bp_preexec_invoke_exec() {
296300
# (corresponding to BASH_COMMAND in the DEBUG trap). This function returns the
297301
# last non-zero exit status from the preexec functions. If there is no error,
298302
# this function returns `0`.
299-
__bp_invoke_preexec_functions() {
303+
bash_preexec_invoke_preexec_functions() {
300304
local lastexit=$1 lastarg=$2 this_command=$3
301305
local preexec_function
302306
local preexec_function_ret_value
@@ -324,7 +328,8 @@ __bp_install() {
324328
return 1
325329
fi
326330

327-
trap '__bp_preexec_invoke_exec "$_"' DEBUG
331+
# shellcheck disable=SC2064
332+
trap "$bash_preexec_trapdebug_string" DEBUG
328333

329334
# Preserve any prior DEBUG trap as a preexec function
330335
local prior_trap
@@ -357,7 +362,7 @@ __bp_install() {
357362
# Remove setting our trap install string and sanitize the existing prompt command string
358363
existing_prompt_command="${PROMPT_COMMAND:-}"
359364
# Edge case of appending to PROMPT_COMMAND
360-
existing_prompt_command="${existing_prompt_command//$__bp_install_string/:}" # no-op
365+
existing_prompt_command="${existing_prompt_command//$bash_preexec_install_string/:}" # no-op
361366
existing_prompt_command="${existing_prompt_command//$'\n':$'\n'/$'\n'}" # remove known-token only
362367
existing_prompt_command="${existing_prompt_command//$'\n':;/$'\n'}" # remove known-token only
363368
__bp_sanitize_string existing_prompt_command "$existing_prompt_command"
@@ -376,10 +381,13 @@ __bp_install() {
376381
PROMPT_COMMAND+=$'\n__bp_interactive_mode'
377382
fi
378383

379-
# Add two functions to our arrays for convenience
380-
# of definition.
381-
precmd_functions+=(precmd)
382-
preexec_functions+=(preexec)
384+
# Add two functions to our arrays for convenience of definition only when
385+
# the functions have not yet added.
386+
if [[ ! ${__bp_installed_convenience_functions-} ]]; then
387+
__bp_installed_convenience_functions=1
388+
precmd_functions+=(precmd)
389+
preexec_functions+=(preexec)
390+
fi
383391

384392
# Invoke our two functions manually that were added to $PROMPT_COMMAND
385393
__bp_precmd_invoke_cmd
@@ -401,8 +409,46 @@ __bp_install_after_session_init() {
401409
PROMPT_COMMAND=${sanitized_prompt_command}$'\n'
402410
fi
403411
# shellcheck disable=SC2179 # PROMPT_COMMAND is not an array in bash <= 5.0
404-
PROMPT_COMMAND+=${__bp_install_string}
412+
PROMPT_COMMAND+=${bash_preexec_install_string}
413+
}
414+
415+
# Remove hooks installed in the DEBUG trap and PROMPT_COMMAND.
416+
bash_preexec_uninstall() {
417+
# Remove __bp_install hook from PROMPT_COMMAND
418+
# shellcheck disable=SC2178 # PROMPT_COMMAND is not an array in bash <= 5.0
419+
if [[ ${PROMPT_COMMAND-} == *"$bash_preexec_install_string"* ]]; then
420+
PROMPT_COMMAND="${PROMPT_COMMAND//${bash_preexec_install_string}[;$'\n']}" # Edge case of appending to PROMPT_COMMAND
421+
PROMPT_COMMAND="${PROMPT_COMMAND//$bash_preexec_install_string}"
422+
fi
423+
424+
# Remove precmd hook from PROMPT_COMMAND
425+
local i prompt_command
426+
for i in "${!PROMPT_COMMAND[@]}"; do
427+
prompt_command=${PROMPT_COMMAND[i]}
428+
case $prompt_command in
429+
__bp_precmd_invoke_cmd | __bp_interactive_mode)
430+
prompt_command= ;;
431+
*)
432+
prompt_command=${prompt_command/#$'__bp_precmd_invoke_cmd\n'/$'\n'}
433+
prompt_command=${prompt_command%$'\n__bp_interactive_mode'}
434+
prompt_command=${prompt_command#$'\n'}
435+
esac
436+
PROMPT_COMMAND[i]=$prompt_command
437+
done
438+
439+
# Remove preexec hook in the DEBUG trap
440+
local q="'" Q="'\''"
441+
if [[ $(trap -p DEBUG) == "trap -- '${bash_preexec_trapdebug_string//$q/$Q}' DEBUG" ]]; then
442+
if [[ ${__bp_trap_string-} ]]; then
443+
eval -- "$__bp_trap_string"
444+
else
445+
trap - DEBUG
446+
fi
447+
fi
405448
}
449+
# Note: We need to add "trace" attribute to the function so that "trap - DEBUG"
450+
# inside the function takes an effect.
451+
declare -ft bash_preexec_uninstall
406452

407453
# Run our install so long as we're not delaying it.
408454
if [[ -z "${__bp_delay_install:-}" ]]; then

test/bash-preexec.bats

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ set_exit_code_and_run_precmd() {
7676

7777
# Assert that before running, the command contains the install string, and
7878
# afterwards it does not
79-
[[ "$PROMPT_COMMAND" == *"$__bp_install_string"* ]] || return 1
79+
[[ "$PROMPT_COMMAND" == *"$bash_preexec_install_string"* ]] || return 1
8080

8181
eval_PROMPT_COMMAND
8282

83-
[[ "$PROMPT_COMMAND" != *"$__bp_install_string"* ]] || return 1
83+
[[ "$PROMPT_COMMAND" != *"$bash_preexec_install_string"* ]] || return 1
8484
}
8585

8686
@test "__bp_install should preserve an existing DEBUG trap" {
@@ -103,6 +103,56 @@ set_exit_code_and_run_precmd() {
103103
(( trap_count_snapshot < trap_invoked_count ))
104104
}
105105

106+
@test "__bp_install should register convenience functions \"preexec\" and \"precmd\" only once" {
107+
precmd_functions=()
108+
preexec_functions=()
109+
__bp_install
110+
bash_preexec_uninstall
111+
__bp_install
112+
113+
count=0
114+
for hook in "${precmd_functions[@]}"; do
115+
if [[ "$hook" == precmd ]] ; then
116+
count=$((count+1))
117+
fi
118+
done
119+
[ "$count" == 1 ]
120+
121+
count=0
122+
for hook in "${preexec_functions[@]}"; do
123+
if [[ "$hook" == preexec ]] ; then
124+
count=$((count+1))
125+
fi
126+
done
127+
[ "$count" == 1 ]
128+
}
129+
130+
@test "bash_preexec_uninstall should remove the hooks in DEBUG and PROMPT_COMMAND" {
131+
__bp_install
132+
133+
q="'" Q="'\''"
134+
[[ "$(join_PROMPT_COMMAND)" == *"__bp_precmd_invoke_cmd"* ]] || return 1
135+
[[ "$(join_PROMPT_COMMAND)" == *"__bp_interactive_mode"* ]] || return 1
136+
[ "$(trap -p DEBUG)" == "trap -- '${bash_preexec_trapdebug_string//$q/$Q}' DEBUG" ]
137+
138+
bash_preexec_uninstall
139+
140+
q="'" Q="'\''"
141+
[[ "$(join_PROMPT_COMMAND)" != *"__bp_precmd_invoke_cmd"* ]] || return 1
142+
[[ "$(join_PROMPT_COMMAND)" != *"__bp_interactive_mode"* ]] || return 1
143+
[ "$(trap -p DEBUG)" != "trap -- '${bash_preexec_trapdebug_string//$q/$Q}' DEBUG" ]
144+
}
145+
146+
@test "bash_preexec_uninstall should remove the unprocessed __bp_install hook in PROMPT_COMMAND" {
147+
__bp_install_after_session_init
148+
149+
[[ "$PROMPT_COMMAND" == *"$bash_preexec_install_string"* ]]
150+
151+
bash_preexec_uninstall
152+
153+
[[ "$PROMPT_COMMAND" != *"$bash_preexec_install_string"* ]]
154+
}
155+
106156
@test "__bp_sanitize_string should remove semicolons and trim space" {
107157

108158
__bp_sanitize_string output " true1; "$'\n'
@@ -308,71 +358,71 @@ set_exit_code_and_run_precmd() {
308358
[ $status -eq 1 ]
309359
}
310360

311-
@test "__bp_invoke_precmd_functions should be transparent for \$? and \$_" {
361+
@test "bash_preexec_invoke_precmd_functions should be transparent for \$? and \$_" {
312362
tester1() { test1_lastexit=$? test1_lastarg=$_; }
313363
tester2() { test2_lastexit=$? test2_lastarg=$_; }
314364
precmd_functions=(tester1 tester2)
315365
trap - DEBUG # remove the Bats stack-trace trap so $_ doesn't get overwritten
316-
__bp_invoke_precmd_functions 111 'vxxJlwNx9VPJDA' || true
366+
bash_preexec_invoke_precmd_functions 111 'vxxJlwNx9VPJDA' || true
317367

318368
[ "$test1_lastexit" == 111 ]
319369
[ "$test1_lastarg" == 'vxxJlwNx9VPJDA' ]
320370
[ "$test2_lastexit" == 111 ]
321371
[ "$test2_lastarg" == 'vxxJlwNx9VPJDA' ]
322372
}
323373

324-
@test "__bp_invoke_precmd_functions returns the last non-zero exit status" {
374+
@test "bash_preexec_invoke_precmd_functions returns the last non-zero exit status" {
325375
tester1() { return 91; }
326376
tester2() { return 38; }
327377
tester3() { return 0; }
328378
precmd_functions=(tester1 tester2 tester3)
329379
status=0
330-
__bp_invoke_precmd_functions 1 'lastarg' || status=$?
380+
bash_preexec_invoke_precmd_functions 1 'lastarg' || status=$?
331381

332382
[ "$status" == 38 ]
333383

334384
precmd_functions=(tester3)
335385
status=0
336-
__bp_invoke_precmd_functions 1 'lastarg' || status=$?
386+
bash_preexec_invoke_precmd_functions 1 'lastarg' || status=$?
337387

338388
[ "$status" == 0 ]
339389
}
340390

341-
@test "__bp_invoke_preexec_functions should be transparent for \$? and \$_" {
391+
@test "bash_preexec_invoke_preexec_functions should be transparent for \$? and \$_" {
342392
tester1() { test1_lastexit=$? test1_lastarg=$_; }
343393
tester2() { test2_lastexit=$? test2_lastarg=$_; }
344394
preexec_functions=(tester1 tester2)
345395
trap - DEBUG # remove the Bats stack-trace trap so $_ doesn't get overwritten
346-
__bp_invoke_preexec_functions 87 'ehQrzHTHtE2E7Q' 'command' || true
396+
bash_preexec_invoke_preexec_functions 87 'ehQrzHTHtE2E7Q' 'command' || true
347397

348398
[ "$test1_lastexit" == 87 ]
349399
[ "$test1_lastarg" == 'ehQrzHTHtE2E7Q' ]
350400
[ "$test2_lastexit" == 87 ]
351401
[ "$test2_lastarg" == 'ehQrzHTHtE2E7Q' ]
352402
}
353403

354-
@test "__bp_invoke_preexec_functions returns the last non-zero exit status" {
404+
@test "bash_preexec_invoke_preexec_functions returns the last non-zero exit status" {
355405
tester1() { return 52; }
356406
tester2() { return 112; }
357407
tester3() { return 0; }
358408
preexec_functions=(tester1 tester2 tester3)
359409
status=0
360-
__bp_invoke_preexec_functions 1 'lastarg' 'command' || status=$?
410+
bash_preexec_invoke_preexec_functions 1 'lastarg' 'command' || status=$?
361411

362412
[ "$status" == 112 ]
363413

364414
preexec_functions=(tester3)
365415
status=0
366-
__bp_invoke_preexec_functions 1 'lastarg' 'command' || status=$?
416+
bash_preexec_invoke_preexec_functions 1 'lastarg' 'command' || status=$?
367417

368418
[ "$status" == 0 ]
369419
}
370420

371-
@test "__bp_invoke_preexec_functions should supply a current command in the first argument" {
421+
@test "bash_preexec_invoke_preexec_functions should supply a current command in the first argument" {
372422
tester1() { test1_bash_command=$1; }
373423
tester2() { test2_bash_command=$1; }
374424
preexec_functions=(tester1 tester2)
375-
__bp_invoke_preexec_functions 1 'lastarg' 'UEVkErELArSwjA' || true
425+
bash_preexec_invoke_preexec_functions 1 'lastarg' 'UEVkErELArSwjA' || true
376426

377427
[ "$test1_bash_command" == 'UEVkErELArSwjA' ]
378428
[ "$test2_bash_command" == 'UEVkErELArSwjA' ]

0 commit comments

Comments
 (0)