Skip to content

Comments

bash: improve prior_trap processing#5142

Merged
mitchellh merged 1 commit intoghostty-org:mainfrom
jparise:bash-preexec-prior_trap
Jan 16, 2025
Merged

bash: improve prior_trap processing#5142
mitchellh merged 1 commit intoghostty-org:mainfrom
jparise:bash-preexec-prior_trap

Conversation

@jparise
Copy link
Contributor

@jparise jparise commented Jan 16, 2025

We use trap to bootstrap our installation function (__bp_install). We remove our code upon first execution but need to restore any preexisting trap calls. We previously used sed to process the trap string, but that had two downsides:

  1. sed is an external command dependency. It needs to exist on the system, and we need to invoke it in a subshell (which has some runtime cost).

  2. The regular expression pattern was imperfect and didn't handle trickier cases like ' characters in the trap string:

     $ (trap "echo 'hello'" DEBUG; trap -p DEBUG)
     hello
     trap -- 'echo '\''hello'\''' DEBUG
    

This change removes the dependency on sed by locally evaluating the trap string and extracting any prior trap. This works reliably because we control the format our trap string, which looks like this (with newlines expanded):

__bp_trap_string="$(trap -p DEBUG)"
trap - DEBUG
__bp_install

Upstream: rcaloras/bash-preexec#170

We use `trap` to bootstrap our installation function (__bp_install). We
remove our code upon first execution but need to restore any preexisting
trap calls. We previously used `sed` to process the trap string, but
that had two downsides:

1. `sed` is an external command dependency. It needs to exist on the
    system, and we need to invoke it in a subshell (which has some
    runtime cost).
2. The regular expression pattern was imperfect and didn't handle
    trickier cases like `'` characters in the trap string:

        $ (trap "echo 'hello'" DEBUG; trap -p DEBUG)
        hello
        trap -- 'echo '\''hello'\''' DEBUG

This change removes the dependency on `sed` by locally evaluating the
trap string and extracting any prior trap. This works reliably because
we control the format our trap string, which looks like this (with
newlines expanded):

    __bp_trap_string="$(trap -p DEBUG)"
    trap - DEBUG
    __bp_install
@jparise jparise mentioned this pull request Jan 16, 2025
@mitchellh mitchellh merged commit b4a3ca9 into ghostty-org:main Jan 16, 2025
30 checks passed
@github-actions github-actions bot added this to the 1.1.0 milestone Jan 16, 2025
@jparise jparise deleted the bash-preexec-prior_trap branch January 16, 2025 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants