From 7a0b353cea6da0df35cc9191fcd96d7eed6c0bcc Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 20 Jan 2022 03:45:56 -0800 Subject: [PATCH 1/4] lib/preexec: rename `vendor/init.d/preexec.bash` --- bash_it.sh | 9 --------- clean_files.txt | 2 +- {vendor/init.d => lib}/preexec.bash | 2 +- test/plugins/cmd-returned-notify.plugin.bats | 1 - test/test_helper_libs.bash | 1 + 5 files changed, 3 insertions(+), 12 deletions(-) rename {vendor/init.d => lib}/preexec.bash (89%) mode change 100644 => 100755 test/plugins/cmd-returned-notify.plugin.bats diff --git a/bash_it.sh b/bash_it.sh index 59c6ed8e6f..527c9f537f 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -51,15 +51,6 @@ for _bash_it_config_file in $LIB; do fi done -# Load vendors -BASH_IT_LOG_PREFIX="vendor: " -for _bash_it_vendor_init in "${BASH_IT}"/vendor/init.d/*.bash; do - _log_debug "Loading \"$(basename "${_bash_it_vendor_init}" .bash)\"..." - # shellcheck disable=SC1090 - source "${_bash_it_vendor_init}" -done -unset _bash_it_vendor_init - BASH_IT_LOG_PREFIX="core: main: " # Load the global "enabled" directory # "family" param is empty so that files get sources in glob order diff --git a/clean_files.txt b/clean_files.txt index f4ab4b1969..1a6bac3313 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -82,6 +82,7 @@ completion/available/wpscan.completion.bash # libraries lib/helpers.bash lib/log.bash +lib/preexec.bash lib/utilities.bash # plugins @@ -165,4 +166,3 @@ themes/purity # vendor init files # vendor/.gitattributes -vendor/init.d diff --git a/vendor/init.d/preexec.bash b/lib/preexec.bash similarity index 89% rename from vendor/init.d/preexec.bash rename to lib/preexec.bash index 6cfa7b0abb..1035b11f5f 100644 --- a/vendor/init.d/preexec.bash +++ b/lib/preexec.bash @@ -8,7 +8,7 @@ # Disable immediate `$PROMPT_COMMAND` modification __bp_delay_install="delayed" -# shellcheck source-path=SCRIPTDIR/../github.com/rcaloras/bash-preexec +# shellcheck source-path=SCRIPTDIR/../vendor/github.com/rcaloras/bash-preexec source "${BASH_IT?}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh" # Block damanaging user's `$HISTCONTROL` diff --git a/test/plugins/cmd-returned-notify.plugin.bats b/test/plugins/cmd-returned-notify.plugin.bats old mode 100644 new mode 100755 index daf58330b3..6f3cf25a24 --- a/test/plugins/cmd-returned-notify.plugin.bats +++ b/test/plugins/cmd-returned-notify.plugin.bats @@ -2,7 +2,6 @@ load ../test_helper load ../test_helper_libs -load ../../vendor/init.d/preexec load ../../plugins/available/cmd-returned-notify.plugin diff --git a/test/test_helper_libs.bash b/test/test_helper_libs.bash index 57115e7e89..cc585fad29 100644 --- a/test/test_helper_libs.bash +++ b/test/test_helper_libs.bash @@ -4,3 +4,4 @@ load "${BASH_IT}/lib/log.bash" load "${BASH_IT}/lib/utilities.bash" load "${BASH_IT}/lib/helpers.bash" load "${BASH_IT}/lib/search.bash" +load "${BASH_IT}/lib/preexec.bash" From 9f146f937a287fa979c9d9c4fd4f3586bb022b59 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 28 Dec 2021 23:58:34 -0800 Subject: [PATCH 2/4] lib/preexec: consolidate helper functions Define the helper functions for `bash-preexec.sh` immediately after importing it, rather than in `lib/theme`. - `__check_precmd_conflict()` and `save_append_prompt_command()` are generally useful and not theme-specific. - Add matching `__check_preexec_conflict()` and `safe_append_preexec()`. --- lib/preexec.bash | 63 ++++++++++++++++++++++++++++++++++++++++++ themes/base.theme.bash | 38 ------------------------- 2 files changed, 63 insertions(+), 38 deletions(-) diff --git a/lib/preexec.bash b/lib/preexec.bash index 1035b11f5f..bb90e1e7d3 100644 --- a/lib/preexec.bash +++ b/lib/preexec.bash @@ -4,6 +4,7 @@ # Load the `bash-preexec.sh` library, and define helper functions ## Prepare, load, fix, and install `bash-preexec.sh` +: "${PROMPT_COMMAND:=}" # Disable immediate `$PROMPT_COMMAND` modification __bp_delay_install="delayed" @@ -23,3 +24,65 @@ set +T # Modify `$PROMPT_COMMAND` now __bp_install_after_session_init + +## Helper functions +function __check_precmd_conflict() { + local f # TODO: trim whitespace like preexec does + for f in "${precmd_functions[@]}"; do + if [[ "${f}" == "${1}" ]]; then + return 0 + fi + done + return 1 +} + +function __check_preexec_conflict() { + local f # TODO: trim whitespace like preexec does + for f in "${preexec_functions[@]}"; do + if [[ "${f}" == "${1}" ]]; then + return 0 + fi + done + return 1 +} + +function safe_append_prompt_command { + local prompt_re + + if [ "${__bp_imported:-missing}" == "defined" ]; then + # We are using bash-preexec + if ! __check_precmd_conflict "${1}"; then + precmd_functions+=("${1}") + fi + else + # Set OS dependent exact match regular expression + if [[ ${OSTYPE} == darwin* ]]; then + # macOS + prompt_re="[[:<:]]${1}[[:>:]]" + else + # Linux, FreeBSD, etc. + prompt_re="\<${1}\>" + fi + + if [[ ${PROMPT_COMMAND} =~ ${prompt_re} ]]; then + return + elif [[ -z ${PROMPT_COMMAND} ]]; then + PROMPT_COMMAND="${1}" + else + PROMPT_COMMAND="${1};${PROMPT_COMMAND}" + fi + fi +} + +function safe_append_preexec { + local prompt_re + + if [ "${__bp_imported:-missing}" == "defined" ]; then + # We are using bash-preexec + if ! __check_preexec_conflict "${1}"; then + preexec_functions+=("${1}") + fi + else + : #can't... + fi +} diff --git a/themes/base.theme.bash b/themes/base.theme.bash index 9e4a2562fa..a7e999617b 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -583,44 +583,6 @@ function aws_profile { fi } -function __check_precmd_conflict() { - local f - for f in "${precmd_functions[@]}"; do - if [[ "${f}" == "${1}" ]]; then - return 0 - fi - done - return 1 -} - -function safe_append_prompt_command { - local prompt_re - - if [ "${__bp_imported:-missing}" == "defined" ]; then - # We are using bash-preexec - if ! __check_precmd_conflict "${1}"; then - precmd_functions+=("${1}") - fi - else - # Set OS dependent exact match regular expression - if [[ ${OSTYPE} == darwin* ]]; then - # macOS - prompt_re="[[:<:]]${1}[[:>:]]" - else - # Linux, FreeBSD, etc. - prompt_re="\<${1}\>" - fi - - if [[ ${PROMPT_COMMAND[*]:-} =~ ${prompt_re} ]]; then - return - elif [[ -z ${PROMPT_COMMAND} ]]; then - PROMPT_COMMAND="${1}" - else - PROMPT_COMMAND="${1};${PROMPT_COMMAND}" - fi - fi -} - function _save-and-reload-history() { local autosave=${1:-0} [[ $autosave -eq 1 ]] && history -a && history -c && history -r From 7770e8cbb9a9a2bd2d2c3be200a0f4639a53af88 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Mon, 3 Jan 2022 17:49:55 -0800 Subject: [PATCH 3/4] lib/preexec: log an error if `bash-preexec` not loaded --- lib/preexec.bash | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/preexec.bash b/lib/preexec.bash index bb90e1e7d3..0d86017689 100644 --- a/lib/preexec.bash +++ b/lib/preexec.bash @@ -83,6 +83,6 @@ function safe_append_preexec { preexec_functions+=("${1}") fi else - : #can't... + _log_error "${FUNCNAME[0]}: can't append to preexec hook because _bash-preexec.sh_ hasn't been loaded" fi } From ae8c9c08a3c3f298409ed9efb755eeffe0d2afb2 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Mon, 3 Jan 2022 18:00:11 -0800 Subject: [PATCH 4/4] lib/preexec: trim whitespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Alsö, use `_bash-it-array-contains-element()` --- lib/preexec.bash | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/lib/preexec.bash b/lib/preexec.bash index 0d86017689..d0d5b6da74 100644 --- a/lib/preexec.bash +++ b/lib/preexec.bash @@ -27,32 +27,25 @@ __bp_install_after_session_init ## Helper functions function __check_precmd_conflict() { - local f # TODO: trim whitespace like preexec does - for f in "${precmd_functions[@]}"; do - if [[ "${f}" == "${1}" ]]; then - return 0 - fi - done - return 1 + local f + __bp_trim_whitespace f "${1?}" + ! _bash-it-array-contains-element "${f}" "${precmd_functions[@]}" } function __check_preexec_conflict() { - local f # TODO: trim whitespace like preexec does - for f in "${preexec_functions[@]}"; do - if [[ "${f}" == "${1}" ]]; then - return 0 - fi - done - return 1 + local f + __bp_trim_whitespace f "${1?}" + ! _bash-it-array-contains-element "${f}" "${preexec_functions[@]}" } function safe_append_prompt_command { - local prompt_re + local prompt_re f + __bp_trim_whitespace f "${1?}" if [ "${__bp_imported:-missing}" == "defined" ]; then # We are using bash-preexec - if ! __check_precmd_conflict "${1}"; then - precmd_functions+=("${1}") + if ! __check_precmd_conflict "${f}"; then + precmd_functions+=("${f}") fi else # Set OS dependent exact match regular expression @@ -75,12 +68,13 @@ function safe_append_prompt_command { } function safe_append_preexec { - local prompt_re + local prompt_re f + __bp_trim_whitespace f "${1?}" if [ "${__bp_imported:-missing}" == "defined" ]; then # We are using bash-preexec - if ! __check_preexec_conflict "${1}"; then - preexec_functions+=("${1}") + if ! __check_preexec_conflict "${f}"; then + preexec_functions+=("${f}") fi else _log_error "${FUNCNAME[0]}: can't append to preexec hook because _bash-preexec.sh_ hasn't been loaded"