Cleanup al_list from builtin_complete.cpp

This commit is contained in:
ridiculousfish 2012-02-08 00:45:07 -08:00
parent 191eeab589
commit 063fc0c48b
4 changed files with 57 additions and 204 deletions

View file

@ -45,15 +45,15 @@ static const wchar_t *temporary_buffer;
static void builtin_complete_add2( const wchar_t *cmd,
int cmd_type,
const wchar_t *short_opt,
array_list_t *gnu_opt,
array_list_t *old_opt,
const wcstring_list_t &gnu_opt,
const wcstring_list_t &old_opt,
int result_mode,
const wchar_t *condition,
const wchar_t *comp,
const wchar_t *desc,
int flags )
{
int i;
size_t i;
const wchar_t *s;
for( s=short_opt; *s; s++ )
@ -70,12 +70,12 @@ static void builtin_complete_add2( const wchar_t *cmd,
flags );
}
for( i=0; i<al_get_count( gnu_opt ); i++ )
for( i=0; i<gnu_opt.size(); i++ )
{
complete_add( cmd,
cmd_type,
0,
(wchar_t *)al_get(gnu_opt, i ),
gnu_opt.at(i).c_str(),
0,
result_mode,
condition,
@ -84,12 +84,12 @@ static void builtin_complete_add2( const wchar_t *cmd,
flags );
}
for( i=0; i<al_get_count( old_opt ); i++ )
for( i=0; i<old_opt.size(); i++ )
{
complete_add( cmd,
cmd_type,
0,
(wchar_t *)al_get(old_opt, i ),
old_opt.at(i).c_str(),
1,
result_mode,
condition,
@ -98,7 +98,7 @@ static void builtin_complete_add2( const wchar_t *cmd,
flags );
}
if( al_get_count( old_opt )+al_get_count( gnu_opt )+wcslen(short_opt) == 0 )
if( old_opt.size() == 0 && gnu_opt.size() == 0 && wcslen(short_opt) == 0 )
{
complete_add( cmd,
cmd_type,
@ -116,11 +116,11 @@ static void builtin_complete_add2( const wchar_t *cmd,
/**
Silly function
*/
static void builtin_complete_add( array_list_t *cmd,
array_list_t *path,
static void builtin_complete_add( const wcstring_list_t &cmd,
const wcstring_list_t &path,
const wchar_t *short_opt,
array_list_t *gnu_opt,
array_list_t *old_opt,
wcstring_list_t &gnu_opt,
wcstring_list_t &old_opt,
int result_mode,
int authoritative,
const wchar_t *condition,
@ -128,11 +128,9 @@ static void builtin_complete_add( array_list_t *cmd,
const wchar_t *desc,
int flags )
{
int i;
for( i=0; i<al_get_count( cmd ); i++ )
for( size_t i=0; i<cmd.size(); i++ )
{
builtin_complete_add2( (const wchar_t *)al_get( cmd, i ),
builtin_complete_add2( cmd.at(i).c_str(),
COMMAND,
short_opt,
gnu_opt,
@ -145,16 +143,16 @@ static void builtin_complete_add( array_list_t *cmd,
if( authoritative != -1 )
{
complete_set_authoritative( (const wchar_t *)al_get( cmd, i ),
complete_set_authoritative( cmd.at(i).c_str(),
COMMAND,
authoritative );
}
}
for( i=0; i<al_get_count( path ); i++ )
for( size_t i=0; i<path.size(); i++ )
{
builtin_complete_add2( (const wchar_t *)al_get( path, i ),
builtin_complete_add2( path.at(i).c_str(),
PATH,
short_opt,
gnu_opt,
@ -167,7 +165,7 @@ static void builtin_complete_add( array_list_t *cmd,
if( authoritative != -1 )
{
complete_set_authoritative( (const wchar_t *)al_get( path, i ),
complete_set_authoritative( path.at(i).c_str(),
PATH,
authoritative );
}
@ -178,37 +176,35 @@ static void builtin_complete_add( array_list_t *cmd,
/**
Silly function
*/
static void builtin_complete_remove3( wchar_t *cmd,
static void builtin_complete_remove3( const wchar_t *cmd,
int cmd_type,
wchar_t short_opt,
array_list_t *long_opt )
const wcstring_list_t &long_opt )
{
int i;
for( i=0; i<al_get_count( long_opt ); i++ )
for( size_t i=0; i<long_opt.size(); i++ )
{
complete_remove( cmd,
cmd_type,
short_opt,
(wchar_t *)al_get( long_opt, i ) );
long_opt.at(i).c_str());
}
}
/**
Silly function
*/
static void builtin_complete_remove2( wchar_t *cmd,
static void builtin_complete_remove2( const wchar_t *cmd,
int cmd_type,
const wchar_t *short_opt,
array_list_t *gnu_opt,
array_list_t *old_opt )
const wcstring_list_t &gnu_opt,
const wcstring_list_t &old_opt )
{
const wchar_t *s = (wchar_t *)short_opt;
if( *s )
{
for( ; *s; s++ )
{
if( al_get_count( old_opt) + al_get_count( gnu_opt ) == 0 )
if( old_opt.size() == 0 && gnu_opt.size() == 0 )
{
complete_remove(cmd,
cmd_type,
@ -248,27 +244,24 @@ static void builtin_complete_remove2( wchar_t *cmd,
/**
Silly function
*/
static void builtin_complete_remove( array_list_t *cmd,
array_list_t *path,
static void builtin_complete_remove( const wcstring_list_t &cmd,
const wcstring_list_t &path,
const wchar_t *short_opt,
array_list_t *gnu_opt,
array_list_t *old_opt )
{
int i;
for( i=0; i<al_get_count( cmd ); i++ )
const wcstring_list_t &gnu_opt,
const wcstring_list_t &old_opt )
{
for( size_t i=0; i<cmd.size(); i++ )
{
builtin_complete_remove2( (wchar_t *)al_get( cmd, i ),
builtin_complete_remove2( cmd.at(i).c_str(),
COMMAND,
short_opt,
gnu_opt,
old_opt );
}
for( i=0; i<al_get_count( path ); i++ )
for( size_t i=0; i<path.size(); i++ )
{
builtin_complete_remove2( (wchar_t *)al_get( path, i ),
builtin_complete_remove2( path.at(i).c_str(),
PATH,
short_opt,
gnu_opt,
@ -300,21 +293,17 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
int flags = COMPLETE_AUTO_SPACE;
string_buffer_t short_opt;
array_list_t gnu_opt, old_opt;
wcstring_list_t gnu_opt, old_opt;
const wchar_t *comp=L"", *desc=L"", *condition=L"";
const wchar_t *do_complete = 0;
array_list_t cmd;
array_list_t path;
wcstring_list_t cmd;
wcstring_list_t path;
static int recursion_level=0;
al_init( &cmd );
al_init( &path );
sb_init( &short_opt );
al_init( &gnu_opt );
al_init( &old_opt );
argc = builtin_count_args( argv );
@ -438,7 +427,10 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
wchar_t *a = unescape( woptarg, 1);
if( a )
{
al_push( (opt=='p'?&path:&cmd), a );
if (opt=='p')
path.push_back(a);
else
cmd.push_back(a);
}
else
{
@ -465,11 +457,11 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
break;
case 'l':
al_push( &gnu_opt, woptarg );
gnu_opt.push_back(woptarg);
break;
case 'o':
al_push( &old_opt, woptarg );
old_opt.push_back(woptarg);
break;
case 'a':
@ -598,7 +590,7 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
res = 1;
}
else if( (al_get_count( &cmd) == 0 ) && (al_get_count( &path) == 0 ) )
else if( cmd.empty() && path.empty() )
{
/* No arguments specified, meaning we print the definitions of
* all specified completions to stdout.*/
@ -608,19 +600,19 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
{
if( remove )
{
builtin_complete_remove( &cmd,
&path,
builtin_complete_remove( cmd,
path,
(wchar_t *)short_opt.buff,
&gnu_opt,
&old_opt );
gnu_opt,
old_opt );
}
else
{
builtin_complete_add( &cmd,
&path,
builtin_complete_add( cmd,
path,
(wchar_t *)short_opt.buff,
&gnu_opt,
&old_opt,
gnu_opt,
old_opt,
result_mode,
authoritative,
condition,
@ -632,14 +624,5 @@ static int builtin_complete( parser_t &parser, wchar_t **argv )
}
}
al_foreach( &cmd, &free );
al_foreach( &path, &free );
al_destroy( &cmd );
al_destroy( &path );
sb_destroy( &short_opt );
al_destroy( &gnu_opt );
al_destroy( &old_opt );
return res;
}

View file

@ -118,35 +118,6 @@ parameter expansion.
*/
#define UNCLEAN L"$*?\\\"'({})"
/* An adapter class for calling functions that expect a wchar_t * and array_list_t */
class wcstring_adapter
{
private:
std::vector<wcstring> *outputs;
public:
wchar_t *str;
array_list_t lst;
wcstring_adapter(const wcstring &in, std::vector<wcstring> &outs)
{
outputs = &outs;
str = wcsdup(in.c_str());
al_init(&lst);
}
~wcstring_adapter()
{
int i, max = al_get_count(&lst);
for (i=0; i < max; i++) {
wchar_t *tmp = (wchar_t *)al_get(&lst, i);
outputs->push_back(tmp);
free(tmp);
}
al_destroy(&lst);
//str is free'd by the function we called
}
};
int expand_is_clean( const wchar_t *in )
{
@ -688,13 +659,6 @@ static int expand_pid( const wcstring &instr,
return 1;
}
/*
static int expand_pid2( const wcstring &in, int flags, std::vector<completion_t> &outputs )
{
wcstring_adapter adapter(in, outputs);
return expand_pid(adapter.str, flags, &adapter.lst);
}
*/
void expand_variable_error( parser_t &parser, const wchar_t *token, int token_pos, int error_pos )
{
@ -1194,13 +1158,6 @@ static int expand_brackets(parser_t &parser, const wchar_t *in, int flags, std::
return 1;
}
/*
static int expand_brackets2( parser_t &parser, const wcstring &in, int flags, std::vector<wcstring> &outputs )
{
wcstring_adapter adapter(in, outputs);
return expand_brackets(parser, adapter.str, flags, &adapter.lst);
}
*/
/**
Perform cmdsubst expansion
*/

View file

@ -108,53 +108,6 @@ static void err( const wchar_t *blah, ... )
wprintf( L"\n" );
}
/**
Test stack functionality
*/
static int stack_test( int elements )
{
long i;
int res=1;
array_list_t s;
al_init( &s );
for( i=0; i<elements; i++ )
{
long foo;
al_push_long( &s, i);
al_push_long( &s, i);
if( (foo=al_pop_long( &s )) != i )
{
err( L"Unexpected data" );
res = 0;
break;
}
}
for( i=0; i<elements; i++ )
{
long foo;
if( (foo=al_pop_long( &s )) != (elements-i-1) )
{
err( L"Unexpected data" );
res = 0;
break;
}
}
al_destroy( &s );
return res;
}
/**
Hash function for pointers
*/
@ -252,40 +205,6 @@ static int hash_test( long elements )
}
/**
Arraylist test
*/
static void al_test( int sz)
{
long i;
array_list_t l;
al_init( &l );
al_set_long( &l, 1, 7L );
al_set_long( &l, sz, 7L );
if( al_get_count( &l ) != maxi( sz+1, 2 ) )
err( L"Wrong number of elements in array list" );
for( i=0; i<al_get_count( &l ); i++ )
{
long val = al_get_long( &l, i );
if( (i == 1) || (i==sz))
{
if( val != 7 )
err( L"Canary changed to %d at index %d", val, i );
}
else
{
if( val != 0 )
err( L"False canary %d found at index %d", val, i );
}
}
}
/**
Stringbuffer test
*/
@ -333,7 +252,6 @@ static void test_util()
for( i=0; i<18; i++ )
{
long t1, t2;
stack_test( 1<<i );
t1 = get_time();
hash_test( 1<<i );
t2 = get_time();
@ -341,7 +259,6 @@ static void test_util()
say( L"Hashtable uses %f microseconds per element at size %d",
((double)(t2-t1))/(1<<i),
1<<i );
al_test( 1<<i );
}
sb_test();

View file

@ -1043,9 +1043,7 @@ static void highlight_universal_internal( const wchar_t * buff,
*/
if( (buff[pos] == L'\'') || (buff[pos] == L'\"') )
{
array_list_t l;
al_init( &l );
std::deque<long> lst;
int level=0;
wchar_t prev_q=0;
@ -1066,7 +1064,7 @@ static void highlight_universal_internal( const wchar_t * buff,
if( level == 0 )
{
level++;
al_push_long( &l, (long)(str-buff) );
lst.push_back((long)(str-buff));
prev_q = *str;
}
else
@ -1076,7 +1074,7 @@ static void highlight_universal_internal( const wchar_t * buff,
long pos1, pos2;
level--;
pos1 = al_pop_long( &l );
pos1 = lst.back();
pos2 = str-buff;
if( pos1==pos || pos2==pos )
{
@ -1090,7 +1088,7 @@ static void highlight_universal_internal( const wchar_t * buff,
else
{
level++;
al_push_long( &l, (long)(str-buff) );
lst.push_back((long)(str-buff));
prev_q = *str;
}
}
@ -1102,8 +1100,6 @@ static void highlight_universal_internal( const wchar_t * buff,
str++;
}
al_destroy( &l );
if( !match_found )
color[pos] = HIGHLIGHT_ERROR<<16;