Skip to content

Commit e4f24f8

Browse files
authored
Merge pull request #669 from danielshahaf/cdpath-and-nonexecutable-in-command-position-v1
Command position: non-executable files, non-cdable directories, CDPATH false positives
2 parents 1000da3 + f2726d0 commit e4f24f8

11 files changed

+254
-15
lines changed

docs/highlighters/main.md

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ This highlighter defines the following styles:
2727
* `precommand` - precommand modifiers (e.g., `noglob`, `builtin`)
2828
* `commandseparator` - command separation tokens (`;`, `&&`)
2929
* `hashed-command` - hashed commands
30+
* `autodirectory` - a directory name in command position when the `AUTO_CD` option is set
3031
* `path` - existing filenames
3132
* `path_pathseparator` - path separators in filenames (`/`); if unset, `path` is used (default)
3233
* `path_prefix` - prefixes of existing filenames

highlighters/main/main-highlighter.zsh

+57-9
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
: ${ZSH_HIGHLIGHT_STYLES[global-alias]:=fg=cyan}
3737
: ${ZSH_HIGHLIGHT_STYLES[precommand]:=fg=green,underline}
3838
: ${ZSH_HIGHLIGHT_STYLES[commandseparator]:=none}
39+
: ${ZSH_HIGHLIGHT_STYLES[autodirectory]:=fg=green,underline}
3940
: ${ZSH_HIGHLIGHT_STYLES[path]:=underline}
4041
: ${ZSH_HIGHLIGHT_STYLES[path_pathseparator]:=}
4142
: ${ZSH_HIGHLIGHT_STYLES[path_prefix_pathseparator]:=}
@@ -113,6 +114,7 @@ _zsh_highlight_main_calculate_fallback() {
113114
command arg0
114115
precommand arg0
115116
hashed-command arg0
117+
autodirectory arg0
116118
arg0_\* arg0
117119

118120
# TODO: Maybe these? —
@@ -1016,7 +1018,7 @@ _zsh_highlight_main_highlighter_highlight_list()
10161018
fi
10171019
_zsh_highlight_main__stack_pop 'R' reserved-word
10181020
else
1019-
if _zsh_highlight_main_highlighter_check_path $arg; then
1021+
if _zsh_highlight_main_highlighter_check_path $arg 1; then
10201022
style=$REPLY
10211023
else
10221024
style=unknown-token
@@ -1125,12 +1127,27 @@ _zsh_highlight_main_highlighter_highlight_path_separators()
11251127
# Check if $1 is a path.
11261128
# If yes, return 0 and in $REPLY the style to use.
11271129
# Else, return non-zero (and the contents of $REPLY is undefined).
1130+
#
1131+
# $2 should be non-zero iff we're in command position.
11281132
_zsh_highlight_main_highlighter_check_path()
11291133
{
11301134
_zsh_highlight_main_highlighter_expand_path "$1"
11311135
local expanded_path="$REPLY" tmp_path
1136+
integer in_command_position=$2
11321137

1133-
REPLY=path
1138+
if [[ $zsyh_user_options[autocd] == on ]]; then
1139+
integer autocd=1
1140+
else
1141+
integer autocd=0
1142+
fi
1143+
1144+
if (( in_command_position )); then
1145+
# ### Currently, this value is never returned: either it's overwritten
1146+
# ### below, or the return code is non-zero
1147+
REPLY=arg0
1148+
else
1149+
REPLY=path
1150+
fi
11341151

11351152
if [[ ${1[1]} == '=' && $1 == ??* && ${1[2]} != $'\x28' && $zsyh_user_options[equals] == 'on' && $expanded_path[1] != '/' ]]; then
11361153
REPLY=unknown-token # will error out if executed
@@ -1152,15 +1169,35 @@ _zsh_highlight_main_highlighter_check_path()
11521169
tmp_path=$tmp_path:h
11531170
done
11541171

1155-
[[ -L $expanded_path ]] && return 0
1156-
[[ -e $expanded_path ]] && return 0
1172+
if (( in_command_position )); then
1173+
if [[ -x $expanded_path ]]; then
1174+
if (( autocd )); then
1175+
if [[ -d $expanded_path ]]; then
1176+
REPLY=autodirectory
1177+
fi
1178+
return 0
1179+
elif [[ ! -d $expanded_path ]]; then
1180+
# ### This seems unreachable for the current callers
1181+
return 0
1182+
fi
1183+
fi
1184+
else
1185+
if [[ -L $expanded_path || -e $expanded_path ]]; then
1186+
return 0
1187+
fi
1188+
fi
11571189

11581190
# Search the path in CDPATH
1159-
if [[ $expanded_path != /* ]]; then
1160-
local cdpath_dir
1191+
if [[ $expanded_path != /* ]] && (( autocd || ! in_command_position )); then
11611192
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
1193+
local cdpath_dir
11621194
for cdpath_dir in $cdpath ; do
1163-
[[ -e "$cdpath_dir/$expanded_path" ]] && return 0
1195+
if [[ -d "$cdpath_dir/$expanded_path" && -x "$cdpath_dir/$expanded_path" ]]; then
1196+
if (( in_command_position && autocd )); then
1197+
REPLY=autodirectory
1198+
fi
1199+
return 0
1200+
fi
11641201
done
11651202
fi
11661203

@@ -1169,10 +1206,18 @@ _zsh_highlight_main_highlighter_check_path()
11691206

11701207
# If this word ends the buffer, check if it's the prefix of a valid path.
11711208
if (( has_end && (len == end_pos) )) &&
1209+
(( ! in_alias )) &&
11721210
[[ $WIDGET != zle-line-finish ]]; then
11731211
# TODO: When we've dropped support for pre-5.0.6 zsh, use the *(Y1) glob qualifier here.
11741212
local -a tmp
1175-
tmp=( ${expanded_path}*(N) )
1213+
if (( in_command_position )); then
1214+
# We include directories even when autocd is enabled, because those
1215+
# directories might contain executable files: e.g., BUFFER="/bi" en route
1216+
# to typing "/bin/sh".
1217+
tmp=( ${expanded_path}*(N-*,N-/) )
1218+
else
1219+
tmp=( ${expanded_path}*(N) )
1220+
fi
11761221
(( ${+tmp[1]} )) && REPLY=path_prefix && return 0
11771222
fi
11781223

@@ -1183,6 +1228,8 @@ _zsh_highlight_main_highlighter_check_path()
11831228
# Highlight an argument and possibly special chars in quotes starting at $1 in $arg
11841229
# This command will at least highlight $1 to end_pos with the default style
11851230
# If $2 is set to 0, the argument cannot be highlighted as an option.
1231+
#
1232+
# This function currently assumes it's never called for the command word.
11861233
_zsh_highlight_main_highlighter_highlight_argument()
11871234
{
11881235
local base_style=default i=$1 option_eligible=${2:-1} path_eligible=1 ret start style
@@ -1317,7 +1364,8 @@ _zsh_highlight_main_highlighter_highlight_argument()
13171364
else
13181365
base_style=numeric-fd
13191366
fi
1320-
elif _zsh_highlight_main_highlighter_check_path $arg[$1,-1]; then
1367+
# This function is currently never called for the command word, so $2 is hard-coded as 0.
1368+
elif _zsh_highlight_main_highlighter_check_path $arg[$1,-1] 0; then
13211369
base_style=$REPLY
13221370
_zsh_highlight_main_highlighter_highlight_path_separators $base_style
13231371
highlights+=($reply)

highlighters/main/test-data/abspath-in-command-position1.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
BUFFER=$'/'
3232

3333
expected_region_highlight=(
34-
'1 1 path' # /
34+
'1 1 path_prefix' # /
3535
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
setopt autocd
32+
BUFFER=$'/'
33+
34+
expected_region_highlight=(
35+
'1 1 autodirectory' # /
36+
)

highlighters/main/test-data/abspath-in-command-position3.zsh

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828
# vim: ft=zsh sw=2 ts=2 et
2929
# -------------------------------------------------------------------------------------------------
3030

31-
BUFFER=$'/bin'
31+
BUFFER=$'/bin; /bin'
3232

3333
expected_region_highlight=(
34-
'1 4 path' # /bin
34+
'1 4 unknown-token' # /bin (in middle)
35+
'5 5 commandseparator' # ;
36+
'7 10 path_prefix' # /bin (at end)
3537
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
setopt autocd
32+
BUFFER=$'/bin; /bin'
33+
34+
expected_region_highlight=(
35+
'1 4 autodirectory' # /bin (in middle)
36+
'5 5 commandseparator' # ;
37+
'7 10 autodirectory' # /bin (at end)
38+
)

highlighters/main/test-data/alias-to-dir.zsh

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ alias x=/
3232
BUFFER=$'x'
3333

3434
expected_region_highlight=(
35-
'1 1 unknown-token "issue #668"' # x (/)
35+
'1 1 unknown-token' # x (/)
3636
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
setopt autocd
32+
alias x=/
33+
BUFFER=$'x'
34+
35+
expected_region_highlight=(
36+
'1 1 alias' # x
37+
)

highlighters/main/test-data/path-dollared-word3.zsh

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
BUFFER='$PWD; ${PWD}'
3535

3636
expected_region_highlight=(
37-
"1 4 path" # $PWD
37+
"1 4 unknown-token" # $PWD (without AUTO_CD)
3838
"5 5 commandseparator" # ;
39-
"7 12 path" # ${PWD}
39+
"7 12 path_prefix" # ${PWD}
4040
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
setopt autocd
32+
BUFFER=$'$PWD; ${PWD}'
33+
34+
expected_region_highlight=(
35+
'1 4 autodirectory' # $PWD
36+
'5 5 commandseparator' # ;
37+
'7 12 autodirectory' # ${PWD}
38+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env zsh
2+
# -------------------------------------------------------------------------------------------------
3+
# Copyright (c) 2020 zsh-syntax-highlighting contributors
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without modification, are permitted
7+
# provided that the following conditions are met:
8+
#
9+
# * Redistributions of source code must retain the above copyright notice, this list of conditions
10+
# and the following disclaimer.
11+
# * Redistributions in binary form must reproduce the above copyright notice, this list of
12+
# conditions and the following disclaimer in the documentation and/or other materials provided
13+
# with the distribution.
14+
# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors
15+
# may be used to endorse or promote products derived from this software without specific prior
16+
# written permission.
17+
#
18+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
19+
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20+
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21+
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24+
# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26+
# -------------------------------------------------------------------------------------------------
27+
# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
28+
# vim: ft=zsh sw=2 ts=2 et
29+
# -------------------------------------------------------------------------------------------------
30+
31+
touch foo
32+
chmod -x foo
33+
BUFFER=$'./foo; ./foo'
34+
35+
expected_region_highlight=(
36+
'1 5 unknown-token' # ./foo (in middle)
37+
'6 6 commandseparator' # ;
38+
'8 12 unknown-token' # ./foo (at end)
39+
)

0 commit comments

Comments
 (0)