Skip to content

Commit 7e75a3e

Browse files
committed
Got subshells working.
- Addresses part of #6 - Still doesn't support functions. - Also expose the correct exit code to preexec.
1 parent e780006 commit 7e75a3e

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

bash-preexec.sh

+22-10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ if [[ "$__bp_imported" == "defined" ]]; then
4040
fi
4141
__bp_imported="defined"
4242

43+
# Should be available to each precmd and preexec
44+
# functions, should they want it.
45+
__bp_last_command_ret_value="$?"
4346

4447
# Remove ignorespace and or replace ignoreboth from HISTCONTROL
4548
# so we can accurately invoke preexec with a command from our
@@ -81,7 +84,7 @@ __bp_interactive_mode() {
8184
__bp_precmd_invoke_cmd() {
8285

8386
# Should be available to each precmd function, should it want it.
84-
local ret_value="$?"
87+
__bp_last_ret_value="$?"
8588

8689
# For every function defined in our function array. Invoke it.
8790
local precmd_function
@@ -90,7 +93,7 @@ __bp_precmd_invoke_cmd() {
9093
# Only execute this function if it actually exists.
9194
# Test existence of functions with: declare -[Ff]
9295
if type -t "$precmd_function" 1>/dev/null; then
93-
__bp_set_ret_value $ret_value
96+
__bp_set_ret_value $__bp_last_ret_value
9497
$precmd_function
9598
fi
9699
done
@@ -130,14 +133,19 @@ __bp_in_prompt_command() {
130133
# interactively, and invoke 'preexec' if so.
131134
__bp_preexec_invoke_exec() {
132135

133-
if [[ -n "$COMP_LINE" ]]
134-
then
135-
# We're in the middle of a completer. This obviously can't be
136+
# Checks if the file descriptor is not standard out (i.e. '1')
137+
# __bp_delay_install checks if we're in test. Needed for bats to run.
138+
# Prevents preexec from being invoked for functions in PS1
139+
if [[ ! -t 1 && -z "$__bp_delay_install" ]]; then
140+
return
141+
fi
142+
143+
if [[ -n "$COMP_LINE" ]]; then
144+
# We're in the middle of a completer. This obviously can't be
136145
# an interactively issued command.
137146
return
138147
fi
139-
if [[ -z "$__bp_preexec_interactive_mode" ]]
140-
then
148+
if [[ -z "$__bp_preexec_interactive_mode" ]]; then
141149
# We're doing something related to displaying the prompt. Let the
142150
# prompt set the title instead of me.
143151
return
@@ -147,16 +155,14 @@ __bp_preexec_invoke_exec() {
147155
# In other words, if you have a subshell like
148156
# (sleep 1; sleep 2)
149157
# You want to see the 'sleep 2' as a set_command_title as well.
150-
if [[ 0 -eq "$BASH_SUBSHELL" ]]
151-
then
158+
if [[ 0 -eq "$BASH_SUBSHELL" ]]; then
152159
__bp_preexec_interactive_mode=""
153160
fi
154161
fi
155162

156163
if __bp_in_prompt_command "$BASH_COMMAND"; then
157164
# If we're executing something inside our prompt_command then we don't
158165
# want to call preexec. Bash prior to 3.1 can't detect this at all :/
159-
160166
__bp_preexec_interactive_mode=""
161167
return
162168
fi
@@ -180,6 +186,7 @@ __bp_preexec_invoke_exec() {
180186
# Only execute each function if it actually exists.
181187
# Test existence of function with: declare -[fF]
182188
if type -t "$preexec_function" 1>/dev/null; then
189+
__bp_set_ret_value $__bp_last_ret_value
183190
$preexec_function "$this_command"
184191
fi
185192
done
@@ -201,6 +208,11 @@ __bp_preexec_and_precmd_install() {
201208
# Adjust our HISTCONTROL Variable if needed.
202209
__bp_adjust_histcontrol
203210

211+
212+
# Set so debug trap will work be invoked in subshells.
213+
set -o functrace > /dev/null 2>&1
214+
shopt -s extdebug > /dev/null 2>&1
215+
204216
# Take our existing prompt command and append a semicolon to it
205217
# if it doesn't already have one.
206218
local existing_prompt_command

0 commit comments

Comments
 (0)