Non-existent filename after redirection as error?
#645 opened on 2019年12月1日
説明
Consider the command line % <foo cat <CURSOR>. When foo exists, we highlight it as a file. When foo does not exist, why don't we highlight it as unknown-token?
Cases that should raise errors, but currently don't:
The NO_CLOBBER cases:
-
> foo echo <CURSOR>whenfooexists andsetopt NO_CLOBBERis in effectNote:
setopt noclobber; echo > /dev/ttyworks even though/dev/ttyexists: https://github.com/zsh-users/zsh/blob/b13e8d16cf3239bd8b4b0434055d25fe4b3d7e7c/Src/exec.c#L2138-L2168 -
>> foo echo <CURSOR>whenfoodoesn't exist andsetopt NO_CLOBBER NO_APPEND_CREATEis in effect
The EPERM cases:
-
< foo cat <CURSOR>whenfoois not readable("Doesn't exist" is a special case of "is not readable".)
-
<>foo cat <CURSOR>whenfoois either not readable or not writable -
>! foo echo <CURSOR>whenfoois not writable -
>! foo echo <CURSOR>whenfoodoesn't exist andfoo(:h)is not writable
All these cases (the NO_CLOBBER ones and the EPERM ones) should be unknown-token.
Cases that create a new file:
-
> foo echo <CURSOR>whenfoodoesn't exist andsetopt CLOBBERis in effect. -
>! foo echo <CURSOR>whenfoodoesn't exist -
>> foo echo <CURSOR>whenfoodoesn't exist andsetopt CLOBBERorsetopt APPEND_CREATEis in effect. -
>>! foo echo <CURSOR>whenfoodoesn't exist
Question: In these four cases — all of them create foo when it didn't exist before — should foo be highlighted as default, or in a new "not a filename yet but will be after this command-line is run" style?
Cases that I don't propose to change:
-
< foo cat <CURSOR>whenfoois readable -
>> foo echo <CURSOR>whenfoois writable
For these cases, we might add unit tests, but no behaviour change is needed.
Cases that are ineligible for highlighting as a filename:
-
<&4,>&4— #694 -
<&-,>&-— fdf23e06c754b791850a2a55a7f8dc6df964407a -
<&p,>&p— fdf23e06c754b791850a2a55a7f8dc6df964407aAll of these are special-cased in the syntax and are never considered as filenames.
I think they shouldn't be highlighted as filenames, ever, so if someone does
>&footo truncate./fooand then tries to do the same to their file./4or./-or./p, the syntax highlighting will indicate their mistake. -
<< foo,<<- foo, and<<< foo
Question: Should foo in the heredoc/herestring syntax eligible to be highlighted as a filename?
On the one hand, these redirections are part of the shell syntax and are never taken as filenames. On the other hand, filenames are highlighted basically whenever they appear as a separate, unquoted shell word, even in contexts like [[ /etc != /usr ]] where they're just strings. I think I'm leaning towards letting foo be highlighted as a filename here, but I could be convinced otherwise.
- Review all other output redirection operators: e.g.,
>&foo,&>foo,>|,>&!,&>!,>>&,&>>,>>&,>>&!,&>>!, etc., and handle them appropriately. (For example,>&fooshould be handled like>foo, whenfooisn't one of the aforementioned special cases.) - Review the
>&$myfdsyntax.