Add more descriptive error messages when trying to use variables in command names

darcs-hash:20060421142939-ac50b-d1809fa4687706b433d1d0a5e0a0c9d791de510f.gz
This commit is contained in:
axel 2006-04-22 00:29:39 +10:00
parent a7f977836a
commit de2405b35a
4 changed files with 44 additions and 13 deletions

View file

@ -1029,7 +1029,7 @@ static int builtin_function( wchar_t **argv )
{
event_t *e;
if( !wcsvarname( woptarg ) )
if( wcsvarname( woptarg ) )
{
sb_printf( sb_err,
_( L"%ls: Invalid variable name '%ls'\n" ),
@ -1145,7 +1145,7 @@ static int builtin_function( wchar_t **argv )
argc-woptind );
res=1;
}
else if( !(is_binding?wcsbindingname( argv[woptind] ) : wcsvarname( argv[woptind] ) ))
else if( !(is_binding?wcsbindingname( argv[woptind] ) : !wcsvarname( argv[woptind] ) ))
{
sb_printf( sb_err,
_( L"%ls: Illegal function name '%ls'\n" ),
@ -2584,7 +2584,7 @@ static int builtin_for( wchar_t **argv )
argv[0] );
builtin_print_help( argv[0], sb_err );
}
else if ( !wcsvarname(argv[1]) )
else if ( wcsvarname(argv[1]) )
{
sb_printf( sb_err,
_( L"%ls: '%ls' is not a valid variable name\n" ),

View file

@ -400,17 +400,17 @@ wchar_t **strv2wcsv( const char **in )
}
int wcsvarname( wchar_t *str )
wchar_t *wcsvarname( wchar_t *str )
{
while( *str )
{
if( (!iswalnum(*str)) && (*str != L'_' ) )
{
return 0;
return str;
}
str++;
}
return 1;
return 0;
}

View file

@ -138,7 +138,14 @@ wchar_t *wcsdupcat2( const wchar_t *a, ... );
Test if the given string is a valid variable name
*/
int wcsvarname( wchar_t *str );
/**
Test if the given string is a valid variable name.
\return null if this is a valid name, and a pointer to the first invalid character otherwise
*/
wchar_t *wcsvarname( wchar_t *str );
/**

View file

@ -1981,6 +1981,7 @@ static int parse_job( process_t *p,
else
{
int tmp;
wchar_t *cmd = (wchar_t *)al_get( args, 0 );
/*
We couln't find the specified command.
@ -1996,19 +1997,42 @@ static int parse_job( process_t *p,
cause the job to silently not execute. We
also print an error message.
*/
if( wcschr( (wchar_t *)al_get( args, 0 ), L'=' ) )
if( wcschr( cmd, L'=' ) )
{
debug( 0,
COMMAND_ASSIGN_ERR_MSG,
(wchar_t *)al_get( args, 0 ) );
}
else if(cmd[0]==L'$')
{
wchar_t *val = env_get( cmd+1 );
if( val )
{
debug( 0,
_(L"Variables may not be used as commands. Define a function like 'function %ls; %ls $argv; end' instead. For more information, see the help section for the function command by typing 'help function'." ),
cmd+1,
val,
cmd );
}
else
{
debug( 0,
_(L"Variables may not be used as commands. Define a function instead. For more information, see the help section for the function command by typing 'help function'." ),
cmd );
}
}
else if(wcschr( cmd, L'$' ))
{
debug( 0,
_(L"Commands may not contain variables. Use the eval builtin instead, like 'eval %ls'. For more information, see the help section for the eval command by typing 'help eval'." ),
cmd,
cmd );
}
else
{
debug( 0,
_(L"Unknown command '%ls'"),
(wchar_t *)al_get( args, 0 ) );
cmd );
}
tmp = current_tokenizer_pos;