From de2405b35aff7cc179c102a4f9c815a50025b601 Mon Sep 17 00:00:00 2001 From: axel Date: Sat, 22 Apr 2006 00:29:39 +1000 Subject: [PATCH] Add more descriptive error messages when trying to use variables in command names darcs-hash:20060421142939-ac50b-d1809fa4687706b433d1d0a5e0a0c9d791de510f.gz --- builtin.c | 6 +++--- common.c | 6 +++--- common.h | 9 ++++++++- parser.c | 36 ++++++++++++++++++++++++++++++------ 4 files changed, 44 insertions(+), 13 deletions(-) diff --git a/builtin.c b/builtin.c index 584b973ea..67dfb03ec 100644 --- a/builtin.c +++ b/builtin.c @@ -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" ), diff --git a/common.c b/common.c index 6d226e158..0bb4a021a 100644 --- a/common.c +++ b/common.c @@ -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; } diff --git a/common.h b/common.h index dc8502e81..60bcb8787 100644 --- a/common.h +++ b/common.h @@ -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 ); /** diff --git a/parser.c b/parser.c index ec67350b1..5e4a2f708 100644 --- a/parser.c +++ b/parser.c @@ -1981,7 +1981,8 @@ 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 + 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;