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/lib/preexec.bash b/lib/preexec.bash new file mode 100644 index 0000000000..d0d5b6da74 --- /dev/null +++ b/lib/preexec.bash @@ -0,0 +1,82 @@ +# shellcheck shell=bash +# shellcheck disable=SC2034 +# +# 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" + +# 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` +function __bp_adjust_histcontrol() { :; } + +# Don't fail on readonly variables +function __bp_require_not_readonly() { :; } + +# Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040 +__bp_enable_subshells= # blank +set +T + +# Modify `$PROMPT_COMMAND` now +__bp_install_after_session_init + +## Helper functions +function __check_precmd_conflict() { + local f + __bp_trim_whitespace f "${1?}" + ! _bash-it-array-contains-element "${f}" "${precmd_functions[@]}" +} + +function __check_preexec_conflict() { + local f + __bp_trim_whitespace f "${1?}" + ! _bash-it-array-contains-element "${f}" "${preexec_functions[@]}" +} + +function safe_append_prompt_command { + local prompt_re f + __bp_trim_whitespace f "${1?}" + + if [ "${__bp_imported:-missing}" == "defined" ]; then + # We are using bash-preexec + if ! __check_precmd_conflict "${f}"; then + precmd_functions+=("${f}") + 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 f + __bp_trim_whitespace f "${1?}" + + if [ "${__bp_imported:-missing}" == "defined" ]; then + # We are using bash-preexec + 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" + fi +} 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" 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 diff --git a/vendor/init.d/preexec.bash b/vendor/init.d/preexec.bash deleted file mode 100644 index 6cfa7b0abb..0000000000 --- a/vendor/init.d/preexec.bash +++ /dev/null @@ -1,25 +0,0 @@ -# shellcheck shell=bash -# shellcheck disable=SC2034 -# -# Load the `bash-preexec.sh` library, and define helper functions - -## Prepare, load, fix, and install `bash-preexec.sh` - -# Disable immediate `$PROMPT_COMMAND` modification -__bp_delay_install="delayed" - -# shellcheck source-path=SCRIPTDIR/../github.com/rcaloras/bash-preexec -source "${BASH_IT?}/vendor/github.com/rcaloras/bash-preexec/bash-preexec.sh" - -# Block damanaging user's `$HISTCONTROL` -function __bp_adjust_histcontrol() { :; } - -# Don't fail on readonly variables -function __bp_require_not_readonly() { :; } - -# Disable trap DEBUG on subshells - https://github.com/Bash-it/bash-it/pull/1040 -__bp_enable_subshells= # blank -set +T - -# Modify `$PROMPT_COMMAND` now -__bp_install_after_session_init