From 409f70c7f3836457cb39113aec36e2c0622e0a61 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 31 Jan 2012 21:09:11 -0800 Subject: [PATCH] Remove some old functions from expand.cpp that now have modern replacements --- expand.cpp | 722 +---------------------------------------------------- 1 file changed, 3 insertions(+), 719 deletions(-) diff --git a/expand.cpp b/expand.cpp index 0f59c56f0..cadab4ad6 100644 --- a/expand.cpp +++ b/expand.cpp @@ -931,274 +931,6 @@ static int parse_slice2( const wchar_t *in, wchar_t **end_ptr, std::vector happens, don't edit it unless you know exactly what you are doing, and do proper testing afterwards. */ -static int expand_variables( parser_t &parser, wchar_t *in, array_list_t *out, int last_idx ) -{ - wchar_t c; - wchar_t prev_char=0; - int i, j; - int is_ok= 1; - int empty=0; - - static string_buffer_t *var_tmp = 0; - static array_list_t *var_idx_list = 0; - - CHECK( in, 0 ); - CHECK( out, 0 ); - - if( !var_tmp ) - { - var_tmp = sb_halloc( global_context ); - if( !var_tmp ) - DIE_MEM(); - } - else - { - sb_clear(var_tmp ); - } - - if( !var_idx_list ) - { - var_idx_list = al_halloc( global_context ); - if( !var_idx_list ) - DIE_MEM(); - } - else - { - al_truncate( var_idx_list, 0 ); - } - - for( i=last_idx; (i>=0) && is_ok && !empty; i-- ) - { - c = in[i]; - if( ( c == VARIABLE_EXPAND ) || (c == VARIABLE_EXPAND_SINGLE ) ) - { - int start_pos = i+1; - int stop_pos; - int var_len, new_len; - const wchar_t * var_val; - wchar_t * new_in; - int is_single = (c==VARIABLE_EXPAND_SINGLE); - int var_name_stop_pos; - - stop_pos = start_pos; - - while( 1 ) - { - if( !(in[stop_pos ]) ) - break; - if( !( iswalnum( in[stop_pos] ) || - (wcschr(L"_", in[stop_pos])!= 0) ) ) - break; - - stop_pos++; - } - var_name_stop_pos = stop_pos; - -/* printf( "Stop for '%c'\n", in[stop_pos]);*/ - - var_len = stop_pos - start_pos; - - if( var_len == 0 ) - { - expand_variable_error( parser, in, stop_pos-1, -1 ); - - is_ok = 0; - break; - } - - sb_append_substring( var_tmp, &in[start_pos], var_len ); - var_val = expand_var( (wchar_t *)var_tmp->buff ); - - if( var_val ) - { - int all_vars=1; - array_list_t var_item_list; - al_init( &var_item_list ); - - if( in[stop_pos] == L'[' ) - { - wchar_t *slice_end; - all_vars=0; - - if( parse_slice( &in[stop_pos], &slice_end, var_idx_list ) ) - { - parser.error( SYNTAX_ERROR, - -1, - L"Invalid index value" ); - is_ok = 0; - } - stop_pos = (slice_end-in); - } - - if( is_ok ) - { - tokenize_variable_array( var_val, &var_item_list ); - if( !all_vars ) - { - int j; - for( j=0; j al_get_count( &var_item_list ) ) - { - parser.error( SYNTAX_ERROR, - -1, - ARRAY_BOUNDS_ERR ); - is_ok=0; - al_truncate( var_idx_list, j ); - break; - } - else - { - /* Replace each index in var_idx_list inplace with the string value at the specified index */ - al_set( var_idx_list, j, wcsdup((const wchar_t *)al_get( &var_item_list, tmp-1 ) ) ); - } - } - /* Free strings in list var_item_list and truncate it */ - al_foreach( &var_item_list, &free ); - al_truncate( &var_item_list, 0 ); - /* Add items from list idx back to list l */ - al_push_all( &var_item_list, var_idx_list ); - } - } - - if( is_ok ) - { - - if( is_single ) - { - string_buffer_t res; - in[i]=0; - - sb_init( &res ); - sb_append( &res, in ); - sb_append_char( &res, INTERNAL_SEPARATOR ); - - for( j=0; j1 && new_in[start_pos-2]!=VARIABLE_EXPAND) - { - new_in[start_pos-1]=INTERNAL_SEPARATOR; - new_in[start_pos]=L'\0'; - } - else - new_in[start_pos-1]=L'\0'; - - wcscat( new_in, next ); - wcscat( new_in, &in[stop_pos] ); - - is_ok &= expand_variables( parser, new_in, out, i ); - } - } - free( next ); - } - - } - } - } - - free(in); - al_destroy( &var_item_list ); - return is_ok; - } - else - { - /* - Expand a non-existing variable - */ - if( c == VARIABLE_EXPAND ) - { - /* - Regular expansion, i.e. expand this argument to nothing - */ - empty = 1; - } - else - { - /* - Expansion to single argument. - */ - string_buffer_t res; - sb_init( &res ); - - in[i]=0; - - sb_append( &res, in ); - sb_append( &res, &in[stop_pos] ); - - is_ok &= expand_variables( parser, (wchar_t *)res.buff, out, i ); - free(in); - return is_ok; - } - } - - - } - - prev_char = c; - } - - if( !empty ) - { - al_push( out, in ); - } - else - { - free( in ); - } - - return is_ok; -} - static int expand_variables_internal( parser_t &parser, wchar_t * const in, std::vector &out, int last_idx ); static int expand_variables2( parser_t &parser, const wcstring &instr, std::vector &out, int last_idx ) { @@ -1569,160 +1301,10 @@ static int expand_brackets2( parser_t &parser, const wcstring &in, int flags, st return expand_brackets(parser, adapter.str, flags, &adapter.lst); } */ -/** - Perform cmdsubst expansion -*/ -static int expand_cmdsubst( parser_t &parser, wchar_t *in, array_list_t *out ) -{ - wchar_t *paran_begin=0, *paran_end=0; - int len1; - wchar_t prev=0; - wchar_t *subcmd; - array_list_t *sub_res, *tail_expand; - int i, j; - const wchar_t *item_begin; - wchar_t *tail_begin = 0; - void *context; - - CHECK( in, 0 ); - CHECK( out, 0 ); - - - - switch( parse_util_locate_cmdsubst(in, - ¶n_begin, - ¶n_end, - 0 ) ) - { - case -1: - parser.error( SYNTAX_ERROR, - -1, - L"Mismatched parans" ); - return 0; - case 0: - al_push( out, in ); - return 1; - case 1: - - break; - } - - context = halloc( 0, 0 ); - - len1 = (paran_begin-in); - prev=0; - item_begin = paran_begin+1; - - sub_res = al_halloc( context ); - if( !(subcmd = (wchar_t *)halloc( context, sizeof(wchar_t)*(paran_end-paran_begin) ))) - { - halloc_free( context ); - return 0; - } - - wcslcpy( subcmd, paran_begin+1, paran_end-paran_begin ); - subcmd[ paran_end-paran_begin-1]=0; - - if( exec_subshell( subcmd, sub_res) == -1 ) - { - halloc_free( context ); - parser.error( CMDSUBST_ERROR, -1, L"Unknown error while evaulating command substitution" ); - return 0; - } - - tail_begin = paran_end + 1; - if( *tail_begin == L'[' ) - { - array_list_t *slice_idx = al_halloc( context ); - wchar_t *slice_end; - - if( parse_slice( tail_begin, &slice_end, slice_idx ) ) - { - halloc_free( context ); - parser.error( SYNTAX_ERROR, -1, L"Invalid index value" ); - return 0; - } - else - { - array_list_t *sub_res2 = al_halloc( context ); - tail_begin = slice_end; - for( i=0; i al_get_count( sub_res ) ) - { - halloc_free( context ); - parser.error( SYNTAX_ERROR, -1, L"Invalid index value" ); - return 0; - } - - idx = idx-1; - - al_push( sub_res2, al_get( sub_res, idx ) ); -// debug( 0, L"Pushing item '%ls' with index %d onto sliced result", al_get( sub_res, idx ), idx ); - - al_set( sub_res, idx, 0 ); - } - al_foreach( sub_res, &free ); - sub_res = sub_res2; - } - } - - tail_expand = al_halloc( context ); - - /* - Recursively call ourselves to expand any remaining command - substitutions. The result of this recursive call usiung the tail - of the string is inserted into the tail_expand array list - */ - expand_cmdsubst( parser, wcsdup(tail_begin), tail_expand ); - - /* - Combine the result of the current command substitution with the - result of the recursive tail expansion - */ - for( i=0; i &outList ) +static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector &outList ) { wchar_t *paran_begin=0, *paran_end=0; int len1; @@ -1813,7 +1395,7 @@ static int expand_cmdsubst2( parser_t &parser, const wcstring &input, std::vecto of the string is inserted into the tail_expand array list */ std::vector tail_expand; - expand_cmdsubst2( parser, tail_begin, tail_expand ); + expand_cmdsubst( parser, tail_begin, tail_expand ); /* Combine the result of the current command substitution with the @@ -2042,7 +1624,7 @@ int expand_string2( const wcstring &input, std::vector &output, in } else { - int cmdsubst_ok = expand_cmdsubst2(parser, input, list1); + int cmdsubst_ok = expand_cmdsubst(parser, input, list1); if (! cmdsubst_ok) return EXPAND_ERROR; } @@ -2227,304 +1809,6 @@ int expand_string2( const wcstring &input, std::vector &output, in return res; } -/** - The real expansion function. expand_one is just a wrapper around this one. -*/ -/* -int expand_string( void *context, - wchar_t *str, - array_list_t *end_out, - int flags ) -{ - array_list_t list1, list2; - array_list_t *in, *out; - parser_t &parser = parser_t::principal_parser(); - - int i; - int cmdsubst_ok = 1; - int res = EXPAND_OK; - int start_count = al_get_count( end_out ); - - CHECK( str, EXPAND_ERROR ); - CHECK( end_out, EXPAND_ERROR ); - - if( (!(flags & ACCEPT_INCOMPLETE)) && expand_is_clean( str ) ) - { - halloc_register( context, str ); - al_push( end_out, str ); - return EXPAND_OK; - } - - al_init( &list1 ); - al_init( &list2 ); - - if( EXPAND_SKIP_CMDSUBST & flags ) - { - wchar_t *begin, *end; - - if( parse_util_locate_cmdsubst( str, - &begin, - &end, - 1 ) != 0 ) - { - parser.error( CMDSUBST_ERROR, -1, L"Command substitutions not allowed" ); - free( str ); - al_destroy( &list1 ); - al_destroy( &list2 ); - return EXPAND_ERROR; - } - al_push( &list1, str ); - } - else - { - cmdsubst_ok = expand_cmdsubst( parser, str, &list1 ); - } - - if( !cmdsubst_ok ) - { - al_destroy( &list1 ); - return EXPAND_ERROR; - } - else - { - in = &list1; - out = &list2; - - for( i=0; i l;