mirror of
https://github.com/fish-shell/fish-shell.git
synced 2025-02-22 03:10:11 +08:00
Give intelligent warning messages on zero-length variable names.
darcs-hash:20051207130330-ac50b-cc9d78ba63f592925b6076049e2c59aaaa930eec.gz
This commit is contained in:
parent
479696a8ec
commit
ab13c4caad
@ -323,7 +323,7 @@ undergoes the process of parameter expantion before it is sent on to
|
||||
the command. There are many ways in which the user can specify a
|
||||
parameter to be expanded. These include:
|
||||
|
||||
\subsection expand-wildcards Wildcards
|
||||
\subsection expand-wildcard Wildcards
|
||||
|
||||
If a star (*) or a question mark (?) is present in the parameter, \c
|
||||
fish attempts to mach the given parameter to any files in such a
|
||||
@ -381,11 +381,31 @@ href="#variables"> Environment variables</a> section.
|
||||
Example:
|
||||
|
||||
<tt> echo \$HOME</tt> prints the home directory of the current
|
||||
user. If you wish to combine environment variables with text, you can
|
||||
user.
|
||||
|
||||
If you wish to combine environment variables with text, you can
|
||||
encase the variables within braces to embed a variable inside running
|
||||
text like <tt>echo Konnichiwa {$USER}san</tt>, which will print a
|
||||
personalized Japanese greeting.
|
||||
|
||||
The {$USER}san syntax might need a bit of an elaboration. Posix
|
||||
shells allow you to specify a variable name using '$VARNAME' or
|
||||
'${VARNAME}'. Fish only supports the former, but has no support
|
||||
whatsoever for the latter or anything remotely like it. So what is
|
||||
'{$VARNAME}' then? Well, '{WHATEVER}' is <a href='#brace'>brace
|
||||
expantion</a>, the same as supported by Posix shells, i.e. 'a{b,c}d'
|
||||
-> 'abd acd' works both in bash and on fish. So '{$VARNAME}' is a
|
||||
bracket-expantion with only a single element, i.e. it becomes
|
||||
expanded to '$VARNAME', which will be variable expanded to the value
|
||||
of the variable 'VARNAME'. So you might think that the brackets don't
|
||||
actually do anything, and that is nearly the truth. The snag is that
|
||||
there once along the way was a '}' in there somewhere, and } is not a
|
||||
valid character in a variable name. So anything after the otherwise
|
||||
pointless bracket expantion becomes NOT a part of the variable name,
|
||||
even if it happens to be a legal variable name character. That's why
|
||||
'{$USER}san' works. A case of one syntax just lending itself so nicely
|
||||
to solving an unrelated problem in it's spare time.
|
||||
|
||||
Variable expantion is the only type of expantion performed on double
|
||||
quoted strings. There is, however, an important difference in how
|
||||
variables are expanded when quoted and when unquoted. An unquoted
|
||||
@ -398,6 +418,7 @@ result in exactly one argument. Undefined variables will expand to the
|
||||
empty string, and array variables will be concatenated using the space
|
||||
character.
|
||||
|
||||
|
||||
\subsection expand-home Home directory expansion
|
||||
|
||||
The ~ (tilde) character at the beginning of a parameter, followed by a
|
||||
@ -504,12 +525,12 @@ Example:
|
||||
|
||||
The following code will not output anything:
|
||||
<pre>
|
||||
if true
|
||||
begin
|
||||
# This is a nice local scope where all variables will die
|
||||
set -l pirate 'There be treasure in them thar hills'
|
||||
end
|
||||
|
||||
# This will not output anything, since the pirate is dead
|
||||
# This will not output anything, since the pirate was local
|
||||
echo $pirate
|
||||
</pre>
|
||||
|
||||
|
25
expand.c
25
expand.c
@ -67,6 +67,9 @@ parameter expansion.
|
||||
*/
|
||||
#define COMPLETE_LAST_DESC COMPLETE_SEP_STR L"Last background job"
|
||||
|
||||
#define COMPLETE_VAR_DESC L"Variable name is zero characters long."
|
||||
#define COMPLETE_VAR2_DESC L" Did you mean {$VARIABLE}? For information on how variable expantion in fish differs from Posix variable expantion, see the manual section on variable expantion by typing 'help expand-variable'."
|
||||
|
||||
/**
|
||||
String in process expansion denoting ourself
|
||||
*/
|
||||
@ -727,6 +730,28 @@ static int expand_variables( wchar_t *in, array_list_t *out )
|
||||
/* printf( "Stop for '%c'\n", in[stop_pos]);*/
|
||||
|
||||
var_len = stop_pos - start_pos;
|
||||
|
||||
if( var_len == 0 )
|
||||
{
|
||||
if( in[stop_pos] == BRACKET_BEGIN )
|
||||
{
|
||||
|
||||
error( SYNTAX_ERROR,
|
||||
COMPLETE_VAR_DESC
|
||||
COMPLETE_VAR2_DESC ,
|
||||
-1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
error( SYNTAX_ERROR,
|
||||
COMPLETE_VAR_DESC,
|
||||
-1 );
|
||||
}
|
||||
|
||||
is_ok = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
if( !(var_name = malloc( sizeof(wchar_t)*(var_len+1) )))
|
||||
{
|
||||
|
@ -22,7 +22,6 @@ complete -c help -x -a prompt -d "Help on how to set the prompt"
|
||||
complete -c help -x -a title -d "Help on how to set the titlebar message"
|
||||
complete -c help -x -a killring -d "Help on how to copy and paste"
|
||||
complete -c help -x -a editor -d "Help on editor shortcuts"
|
||||
complete -c help -x -a expand -d "Help on parameter expantion (Globbing)"
|
||||
complete -c help -x -a globbing -d "Help on parameter expantion (Globbing)"
|
||||
complete -c help -x -a variables -d "Help on environment variables"
|
||||
complete -c help -x -a color -d "Help on setting syntax highlighting colors"
|
||||
@ -31,3 +30,10 @@ complete -c help -x -a title -d "Help on changing the titlebar messages"
|
||||
complete -c help -x -a builtin-overview -d "A short summary of all builtin commands"
|
||||
complete -c help -x -a changes -d "The changelog"
|
||||
|
||||
complete -c help -x -a expand -d "Help on parameter expantion (Globbing)"
|
||||
complete -c help -x -a expand-variable -d "Help on variable exapantion \$VARNAME"
|
||||
complete -c help -x -a expand-home -d "Help on home directory expantion ~USER"
|
||||
complete -c help -x -a expand-brace -d "Help on brace expantion {a,b,c}"
|
||||
complete -c help -x -a expand-wildcard -d "Help on wildcard expantion *.*"
|
||||
complete -c help -x -a expand-command-substitution -d "Help on command substututions (SUBCOMMAND)"
|
||||
complete -c help -x -a expand-process -d "Help on process expantion %JOB"
|
||||
|
@ -158,8 +158,9 @@ function help -d "Show help for the fish shell"
|
||||
end
|
||||
|
||||
set idx_subj syntax completion editor job-control todo bugs history
|
||||
set idx_subj $idx_subj killring help color prompt title expand variables
|
||||
set idx_subj $idx_subj builtin-overview changes
|
||||
set idx_subj $idx_subj killring help color prompt title variables
|
||||
set idx_subj $idx_subj builtin-overview changes
|
||||
set idx_subj $idx_subj expand expand-variable expand-home expand-brace expand-wildcard expand-command-substitution expand-process
|
||||
|
||||
if contains -- $fish_help_item $idx_subj
|
||||
set fish_help_page "index.html\#"$fish_help_item
|
||||
|
Loading…
x
Reference in New Issue
Block a user