Remove some old functions from expand.cpp that now have modern replacements

This commit is contained in:
ridiculousfish 2012-01-31 21:09:11 -08:00
parent bb19fe703a
commit 409f70c7f3

View file

@ -931,274 +931,6 @@ static int parse_slice2( const wchar_t *in, wchar_t **end_ptr, std::vector<long>
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_idx_list ); j++)
{
long tmp = al_get_long( var_idx_list, j );
if( tmp < 0 )
{
tmp = al_get_count( &var_item_list)+tmp+1;
}
/*
Check that we are within array
bounds. If not, truncate the list to
exit.
*/
if( tmp < 1 || tmp > 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; j<al_get_count( &var_item_list); j++ )
{
wchar_t *next = (wchar_t *)al_get( &var_item_list, j );
if( is_ok )
{
if( j != 0 )
sb_append( &res, L" " );
sb_append( &res, next );
}
free( next );
}
sb_append( &res, &in[stop_pos] );
is_ok &= expand_variables( parser, (wchar_t *)res.buff, out, i );
}
else
{
for( j=0; j<al_get_count( &var_item_list); j++ )
{
wchar_t *next = (wchar_t *)al_get( &var_item_list, j );
if( is_ok && (i == 0) && (!in[stop_pos]) )
{
al_push( out, next );
}
else
{
if( is_ok )
{
new_len = wcslen(in) - (stop_pos-start_pos+1);
new_len += wcslen( next) +2;
if( !(new_in = (wchar_t *)malloc( sizeof(wchar_t)*new_len )))
{
DIE_MEM();
}
else
{
wcslcpy( new_in, in, start_pos );
if(start_pos>1 && 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<completion_t> &out, int last_idx );
static int expand_variables2( parser_t &parser, const wcstring &instr, std::vector<completion_t> &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,
&paran_begin,
&paran_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( slice_idx ); i++ )
{
long idx = al_get_long( slice_idx, i );
if( idx < 0 )
{
idx = al_get_count( sub_res ) + idx + 1;
}
if( idx < 1 || idx > 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<al_get_count( sub_res ); i++ )
{
wchar_t *sub_item, *sub_item2;
sub_item = (wchar_t *)al_get( sub_res, i );
sub_item2 = escape( sub_item, 1 );
free(sub_item);
int item_len = wcslen( sub_item2 );
for( j=0; j<al_get_count( tail_expand ); j++ )
{
string_buffer_t whole_item;
wchar_t *tail_item = (wchar_t *)al_get( tail_expand, j );
sb_init( &whole_item );
sb_append_substring( &whole_item, in, len1 );
sb_append_char( &whole_item, INTERNAL_SEPARATOR );
sb_append_substring( &whole_item, sub_item2, item_len );
sb_append_char( &whole_item, INTERNAL_SEPARATOR );
sb_append( &whole_item, tail_item );
al_push( out, whole_item.buff );
}
free( sub_item2 );
}
free(in);
al_foreach( tail_expand, &free );
halloc_free( context );
return 1;
}
/**
Perform cmdsubst expansion
*/
static int expand_cmdsubst2( parser_t &parser, const wcstring &input, std::vector<completion_t> &outList )
static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector<completion_t> &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<completion_t> 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<completion_t> &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<completion_t> &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<al_get_count( in ); i++ )
{
wchar_t *next;
*/
/*
We accept incomplete strings here, since complete uses
expand_string to expand incomplete strings from the
commandline.
*/
/* int unescape_flags = UNESCAPE_SPECIAL | UNESCAPE_INCOMPLETE;
next = expand_unescape( parser, (wchar_t *)al_get( in, i ), unescape_flags );
free( (void *)al_get( in, i ) );
if( !next )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
continue;
}
if( EXPAND_SKIP_VARIABLES & flags )
{
wchar_t *tmp;
for( tmp=next; *tmp; tmp++ )
if( *tmp == VARIABLE_EXPAND )
*tmp = L'$';
al_push( out, next );
}
else
{
if(!expand_variables( parser, next, out, wcslen(next)-1 ))
{
al_destroy( in );
al_destroy( out );
return EXPAND_ERROR;
}
}
}
al_truncate( in, 0 );
in = &list2;
out = &list1;
for( i=0; i<al_get_count( in ); i++ )
{
wchar_t *next = (wchar_t *)al_get( in, i );
if( !next )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
continue;
}
if( !expand_brackets( parser, next, flags, out ))
{
al_destroy( in );
al_destroy( out );
return EXPAND_ERROR;
}
}
al_truncate( in, 0 );
in = &list1;
out = &list2;
for( i=0; i<al_get_count( in ); i++ )
{
wchar_t *next = (wchar_t *)al_get( in, i );
if( !next )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
continue;
}
if( !(next=expand_tilde_internal_compat( next ) ) )
{
al_destroy( in );
al_destroy( out );
return EXPAND_ERROR;
}
if( flags & ACCEPT_INCOMPLETE )
{
if( *next == PROCESS_EXPAND )
{*/
/*
If process expansion matches, we are not
interested in other completions, so we
short-circut and return
*/
/* expand_pid( next, flags, end_out );
al_destroy( in );
al_destroy( out );
return EXPAND_OK;
}
else
{
al_push( out, next );
}
}
else
{
if( !expand_pid( next, flags, out ) )
{
al_destroy( in );
al_destroy( out );
return EXPAND_ERROR;
}
}
}
al_truncate( in, 0 );
in = &list2;
out = &list1;
for( i=0; i<al_get_count( in ); i++ )
{
wchar_t *next = (wchar_t *)al_get( in, i );
int wc_res;
if( !next )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
continue;
}
remove_internal_separator( next, EXPAND_SKIP_WILDCARDS & flags );
if( ((flags & ACCEPT_INCOMPLETE) && (!(flags & EXPAND_SKIP_WILDCARDS))) ||
wildcard_has( next, 1 ) )
{
const wchar_t *start, *rest;
array_list_t *list = out;
if( next[0] == '/' )
{
start = L"/";
rest = &next[1];
}
else
{
start = L"";
rest = next;
}
if( flags & ACCEPT_INCOMPLETE )
{
list = end_out;
}
wc_res = wildcard_expand( rest, start, flags, list );
free( next );
if( !(flags & ACCEPT_INCOMPLETE) )
{
switch( wc_res )
{
case 0:
{
if( !(flags & ACCEPT_INCOMPLETE) )
{
if( res == EXPAND_OK )
res = EXPAND_WILDCARD_NO_MATCH;
break;
}
}
case 1:
{
int j;
res = EXPAND_WILDCARD_MATCH;
sort_list( out );
for( j=0; j<al_get_count( out ); j++ )
{
wchar_t *next = (wchar_t *)al_get( out, j );
if( !next )
{
debug( 2, L"Got null string on line %d of file %s", __LINE__, __FILE__ );
continue;
}
al_push( end_out, next );
}
al_truncate( out, 0 );
break;
}
case -1:
{
al_foreach( out, &free );
al_destroy( in );
al_destroy( out );
return EXPAND_ERROR;
}
}
}
}
else
{
if( flags & ACCEPT_INCOMPLETE)
{
free( next );
}
else
{
al_push( end_out, next );
}
}
}
al_destroy( in );
al_destroy( out );
}
if( context )
{
for( i=start_count; i<al_get_count( end_out ); i++ )
{
halloc_register( context, (void *)al_get( end_out, i ) );
}
}
return res;
}
*/
wchar_t *expand_one( void *context, const wchar_t *string, int flags )
{
std::vector<completion_t> l;