From 70deca2e2c1d4bc508c2642a577cd4d939d17e7e Mon Sep 17 00:00:00 2001 From: raizo62 Date: Sun, 31 Mar 2019 19:52:19 +0200 Subject: [PATCH] Add the function "zsh_highlight_main__test_type_builtin" also the change of PATH can't modify the behavior of the commands in "zsh_highlight_main__type" In _zsh_highlight_main__type : - the calls to "builtin type -w -- $1" are replaced by call to "sh_highlight_main__test_type_builtin" - if ZSH_HIGHLIGHT_SPECIAL_PATH exists but is empty, "foo" in "sudo foo" is always red - if ZSH_HIGHLIGHT_SPECIAL_PATH is not empty, zsh searchs in the PATH "ZSH_HIGHLIGHT_SPECIAL_PATH" to know if the command "foo" in "sudo foo" exists - if "foo" is in ZSH_HIGHLIGHT_SPECIAL_COMMAND then "Admin" says that user has access to this command : foo in "sudo foo" is green --- highlighters/main/main-highlighter.zsh | 34 ++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index eb49d8a..c0762d2 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -127,6 +127,18 @@ _zsh_highlight_main_calculate_fallback() { done } +_zsh_highlight_main__test_type_builtin() { + + if [[ -n ${ZSH_HIGHLIGHT_SPECIAL_PATH} ]]; then + if [ "$this_word" = ":sudo_opt::start:" ]; then + # ZSH_HIGHLIGHT_SPECIAL_PATH exist is not empty : "Admin" says that user has access to commands in these paths with sudo + local PATH="${ZSH_HIGHLIGHT_SPECIAL_PATH}" + fi + fi + + builtin type -w -- $1 +} + # Get the type of a command. # # Uses the zsh/parameter module if available to avoid forks, and a @@ -152,6 +164,24 @@ _zsh_highlight_main__type() { fi fi + if [[ -v ZSH_HIGHLIGHT_SPECIAL_COMMAND ]]; then + if (( ${ZSH_HIGHLIGHT_SPECIAL_COMMAND[(I)$1]} )); then + # ZSH_HIGHLIGHT_SPECIAL_COMMAND exists : if foo of "sudo foo" is in ZSH_HIGHLIGHT_SPECIAL_COMMAND then "Admin" says that user has access to this command + REPLY=command + return 0 + fi + fi + + if [[ -v ZSH_HIGHLIGHT_SPECIAL_PATH ]]; then + if [ "$this_word" = ":sudo_opt::start:" ]; then + if [ -z "${ZSH_HIGHLIGHT_SPECIAL_PATH}" ]; then + # ZSH_HIGHLIGHT_SPECIAL_PATH exist and is empty : "Admin" wants to say that user has no access to sudo : foo in "sudo foo" is red + REPLY=none + return 0 + fi + fi + fi + # Main logic if (( $#options_to_set )); then setopt localoptions $options_to_set; @@ -186,7 +216,7 @@ _zsh_highlight_main__type() { elif { [[ $1 != */* ]] || is-at-least 5.3 } && # Add a subshell to avoid a zsh upstream bug; see issue #606. # ### Remove the subshell when we stop supporting zsh 5.7.1 (I assume 5.8 will have the bugfix). - ! (builtin type -w -- $1) >/dev/null 2>&1; then + ! (_zsh_highlight_main__test_type_builtin $1) >/dev/null 2>&1; then REPLY=none fi fi @@ -201,7 +231,7 @@ _zsh_highlight_main__type() { # starts with an arithmetic expression [«((…))» as the first thing inside # «$(…)»], which is area that has had some parsing bugs before 5.6 # (approximately). - REPLY="${$(:; (( aliases_allowed )) || unalias -- $1 2>/dev/null; LC_ALL=C builtin type -w -- $1 2>/dev/null)##*: }" + REPLY="${$(:; (( aliases_allowed )) || unalias -- $1 2>/dev/null; LC_ALL=C _zsh_highlight_main__test_type_builtin $1 2>/dev/null)##*: }" if [[ $REPLY == 'alias' ]]; then may_cache=0 fi