diff options
author | axel <axel@liljencrantz.se> | 2006-07-20 23:02:46 +1000 |
---|---|---|
committer | axel <axel@liljencrantz.se> | 2006-07-20 23:02:46 +1000 |
commit | 5c8d9bd13c82c66fcb0b15d2844359dc3c306d96 (patch) | |
tree | 26624c5003f1467b4411b453c775e8b229de3223 | |
parent | 1c9a173b3dbb2f89658ad26cc6ede6fe9afed706 (diff) |
Minor duplicate code removal
darcs-hash:20060720130246-ac50b-b41daebb302e1cbadf2a4cf812d936edbda85926.gz
-rw-r--r-- | expand.c | 102 | ||||
-rw-r--r-- | expand.h | 33 | ||||
-rw-r--r-- | parser.c | 72 |
3 files changed, 88 insertions, 119 deletions
@@ -47,6 +47,26 @@ parameter expansion. #include "halloc_util.h" /** + Error issued on invalid variable name +*/ +#define COMPLETE_VAR_DESC _( L"The '$' character begins a variable name. The character '%lc', which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.") + +/** + Error issued on invalid variable name +*/ +#define COMPLETE_VAR_NULL_DESC _( L"The '$' begins a variable name. It was given at the end of an argument. Variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.") + +/** + Error issued on invalid variable name +*/ +#define COMPLETE_VAR_BRACKET_DESC _( L"Did you mean {$VARIABLE}? The '$' character begins a variable name. A bracket, which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'." ) + +/** + Error issued on invalid variable name +*/ +#define COMPLETE_VAR_PARAN_DESC _( L"Did you mean (COMMAND)? In fish, the '$' character is only used for accessing variables. To learn more about command substitution in fish, type 'help expand-command-substitution'.") + +/** Description for child process */ #define COMPLETE_CHILD_PROCESS_DESC _( L"Child process") @@ -649,6 +669,49 @@ static int expand_pid( wchar_t *in, return 1; } + +void expand_variable_error( const wchar_t *token, int token_pos, int error_pos ) +{ + int stop_pos = token_pos+1; + + switch( token[stop_pos] ) + { + case BRACKET_BEGIN: + { + error( SYNTAX_ERROR, + error_pos, + COMPLETE_VAR_BRACKET_DESC ); + break; + } + + case INTERNAL_SEPARATOR: + { + error( SYNTAX_ERROR, + error_pos, + COMPLETE_VAR_PARAN_DESC ); + break; + } + + case 0: + { + error( SYNTAX_ERROR, + error_pos, + COMPLETE_VAR_NULL_DESC ); + break; + } + + default: + { + error( SYNTAX_ERROR, + error_pos, + COMPLETE_VAR_DESC, + token[stop_pos] ); + break; + } + } +} + + /** Expand all environment variables in the string *ptr. @@ -735,43 +798,8 @@ static int expand_variables( wchar_t *in, array_list_t *out, int last_idx ) if( var_len == 0 ) { - switch( in[stop_pos] ) - { - case BRACKET_BEGIN: - { - error( SYNTAX_ERROR, - -1, - COMPLETE_VAR_BRACKET_DESC ); - break; - } - - case INTERNAL_SEPARATOR: - { - error( SYNTAX_ERROR, - -1, - COMPLETE_VAR_PARAN_DESC ); - break; - } - - case 0: - { - error( SYNTAX_ERROR, - -1, - COMPLETE_VAR_NULL_DESC ); - break; - } - - default: - { - error( SYNTAX_ERROR, - -1, - COMPLETE_VAR_DESC, - in[stop_pos] ); - break; - } - } - - + expand_variable_error( in, stop_pos-1, -1 ); + is_ok = 0; break; } @@ -113,26 +113,6 @@ enum #define ARRAY_SEP_STR L"\x1e" /** - Error issued on invalid variable name -*/ -#define COMPLETE_VAR_DESC _( L"The '$' character begins a variable name. The character '%lc', which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.") - -/** - Error issued on invalid variable name -*/ -#define COMPLETE_VAR_NULL_DESC _( L"The '$' begins a variable name. It was given at the end of an argument. Variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'.") - -/** - Error issued on invalid variable name -*/ -#define COMPLETE_VAR_BRACKET_DESC _( L"Did you mean {$VARIABLE}? The '$' character begins a variable name. A bracket, which directly followed a '$', is not allowed as a part of a variable name, and variable names may not be zero characters long. To learn more about variable expansion in fish, type 'help expand-variable'." ) - -/** - Error issued on invalid variable name -*/ -#define COMPLETE_VAR_PARAN_DESC _( L"Did you mean (COMMAND)? In fish, the '$' character is only used for accessing variables. To learn more about command substitution in fish, type 'help expand-command-substitution'.") - -/** Error issued on array out of bounds */ #define ARRAY_BOUNDS_ERR _(L"Array index out of bounds") @@ -207,4 +187,17 @@ wchar_t *expand_tilde(wchar_t *in); */ int expand_is_clean( const wchar_t *in ); +/** + Perform error reporting for a syntax error related to the variable + expansion beginning at the specified character of the specified + token. This function will call the error function with an + explanatory string about what is wrong with the specified token. + + \param token The token containing the error + \param token_pos The position where the expansion begins + \param error_pos The position on the line to report to the error function. +*/ +void expand_variable_error( const wchar_t *token, int token_pos, int error_pos ); + + #endif @@ -2850,70 +2850,18 @@ static int parser_test_argument( const wchar_t *arg, string_buffer_t *out, const case VARIABLE_EXPAND: case VARIABLE_EXPAND_SINGLE: { - switch( *(pos+1)) + wchar_t n = *(pos+1); + + if( n != VARIABLE_EXPAND && + n != VARIABLE_EXPAND_SINGLE && + !wcsvarchr(n) ) { - case BRACKET_BEGIN: - { - err=1; - if( out ) - { - error( SYNTAX_ERROR, - offset, - COMPLETE_VAR_BRACKET_DESC ); - - print_errors( out, prefix); - } - break; - } - - case INTERNAL_SEPARATOR: - { - err=1; - if( out ) - { - error( SYNTAX_ERROR, - offset, - COMPLETE_VAR_PARAN_DESC ); - print_errors( out, prefix); - } - break; - } - - case 0: - { - err=1; - if( out ) - { - error( SYNTAX_ERROR, - offset, - COMPLETE_VAR_NULL_DESC ); - print_errors( out, prefix); - } - break; - } - - default: + err=1; + if( out ) { - wchar_t n = *(pos+1); - - if( n != VARIABLE_EXPAND && - n != VARIABLE_EXPAND_SINGLE && - !wcsvarchr(n) ) - { - err=1; - if( out ) - { - error( SYNTAX_ERROR, - offset, - COMPLETE_VAR_DESC, - *(pos+1) ); - print_errors( out, prefix); - } - } - - break; + expand_variable_error( unesc, pos-unesc, offset ); + print_errors( out, prefix); } - } break; @@ -3053,7 +3001,7 @@ int parser_test( const wchar_t * buff, int mark = tok_get_pos( &tok ); had_cmd = 1; arg_count=0; - + if( !(cmd = expand_one( context, wcsdup( tok_last( &tok ) ), EXPAND_SKIP_SUBSHELL | EXPAND_SKIP_VARIABLES ) ) ) |