More work to migrate off of ad-hoc data structures

This commit is contained in:
ridiculousfish 2011-12-31 15:57:30 -08:00
parent 78322a6321
commit bef046a51a
8 changed files with 134 additions and 242 deletions

View file

@ -188,7 +188,7 @@ static int count_char( const wchar_t *str, wchar_t c )
wchar_t *builtin_help_get( const wchar_t *name )
{
array_list_t lst;
wcstring_list_t lst;
string_buffer_t cmd;
wchar_t *name_esc;
@ -198,9 +198,8 @@ wchar_t *builtin_help_get( const wchar_t *name )
using halloc.
*/
static string_buffer_t *out = 0;
int i;
size_t i;
al_init( &lst );
sb_init( &cmd );
if( !out )
@ -216,17 +215,15 @@ wchar_t *builtin_help_get( const wchar_t *name )
sb_printf( &cmd, L"__fish_print_help %ls", name_esc );
if( exec_subshell( (wchar_t *)cmd.buff, &lst ) >= 0 )
if( exec_subshell2( (wchar_t *)cmd.buff, lst ) >= 0 )
{
for( i=0; i<al_get_count( &lst); i++ )
for( i=0; i<lst.size(); i++ )
{
sb_append( out, (wchar_t *)al_get( &lst, i ) );
sb_append( out, lst.at(i).c_str() );
sb_append( out, L"\n" );
}
}
al_foreach( &lst, &free );
al_destroy( &lst );
sb_destroy( &cmd );
free( name_esc );
@ -412,37 +409,29 @@ static void builtin_missing_argument( const wchar_t *cmd, const wchar_t *opt )
*/
static void builtin_bind_list()
{
array_list_t lst;
int i;
size_t i;
wcstring_list_t lst;
input_mapping_get_names( lst );
al_init( &lst );
input_mapping_get_names( &lst );
for( i=0; i<al_get_count(&lst); i++ )
for( i=0; i<lst.size(); i++ )
{
wchar_t *seq = (wchar_t *)al_get( &lst, i );
wcstring seq = lst.at(i);
const wchar_t *tname = input_terminfo_get_name( seq );
wchar_t *ecmd = escape( input_mapping_get( seq ), 1 );
wcstring ecmd;
input_mapping_get(seq, ecmd);
ecmd = escape_string(ecmd, 1);
if( tname )
wcstring tname;
if( input_terminfo_get_name(seq, tname) )
{
sb_printf( sb_out, L"bind -k %ls %ls\n", tname, ecmd );
sb_printf( sb_out, L"bind -k %ls %ls\n", tname.c_str(), ecmd.c_str() );
}
else
{
wchar_t *eseq = escape( seq, 1 );
sb_printf( sb_out, L"bind %ls %ls\n", eseq, ecmd );
free( eseq );
const wcstring eseq = escape_string( seq, 1 );
sb_printf( sb_out, L"bind %ls %ls\n", eseq.c_str(), ecmd.c_str() );
}
free( ecmd );
}
al_destroy( &lst );
}
/**
@ -553,18 +542,15 @@ static void builtin_bind_erase( wchar_t **seq, int all )
{
if( all )
{
int i;
array_list_t lst;
al_init( &lst );
size_t i;
wcstring_list_t lst;
input_mapping_get_names( lst );
input_mapping_get_names( &lst );
for( i=0; i<al_get_count( &lst ); i++ )
for( i=0; i<lst.size(); i++ )
{
input_mapping_erase( (wchar_t *)al_get( &lst, i ) );
input_mapping_erase( lst.at(i).c_str() );
}
al_destroy( &lst );
}
else
{

View file

@ -112,48 +112,6 @@ static int pq_compare( void *e1, void *e2 )
return (intptr_t)e1-(intptr_t)e2;
}
/**
Test priority queue functionality
*/
static void pq_test( int elements )
{
int i;
int prev;
int *count = (int *)calloc( sizeof(int), 100 );
priority_queue_t q;
pq_init( &q, pq_compare );
for( i=0; i<elements; i++ )
{
long foo = rand() % 100;
// printf( "Adding %d\n", foo );
pq_put( &q, (void *)foo );
count[foo]++;
}
prev = 100;
for( i=0; i<elements; i++ )
{
long pos = (long)pq_get( &q );
count[ pos ]--;
if( pos > prev )
err( L"Wrong order of elements in priority_queue_t" );
prev = pos;
}
for( i=0; i<100; i++ )
{
if( count[i] != 0 )
{
err( L"Wrong number of elements in priority_queue_t" );
}
}
}
/**
Test stack functionality
@ -379,7 +337,6 @@ static void test_util()
for( i=0; i<18; i++ )
{
long t1, t2;
pq_test( 1<<i );
stack_test( 1<<i );
t1 = get_time();
hash_test( 1<<i );

157
input.cpp
View file

@ -84,13 +84,14 @@
/**
Struct representing a keybinding. Returned by input_get_mappings.
*/
typedef struct
struct input_mapping_t
{
const wchar_t *seq; /**< Character sequence which generates this event */
const wchar_t *command; /**< command that should be evaluated by this mapping */
wcstring seq; /**< Character sequence which generates this event */
wcstring command; /**< command that should be evaluated by this mapping */
}
input_mapping_t;
input_mapping_t(const wcstring &s, const wcstring &c) : seq(s), command(c) {}
};
/**
A struct representing the mapping from a terminfo key name to a terminfo character sequence
@ -226,11 +227,10 @@ static const wchar_t code_arr[] =
}
;
/**
Mappings for the current input mode
*/
static array_list_t mappings = {0,0,0};
std::vector<input_mapping_t> mapping_list;
/**
List of all terminfo mappings
@ -259,7 +259,7 @@ static void input_terminfo_destroy();
void input_mapping_add( const wchar_t *sequence,
const wchar_t *command )
{
int i;
size_t i;
CHECK( sequence, );
CHECK( command, );
@ -267,21 +267,16 @@ void input_mapping_add( const wchar_t *sequence,
// debug( 0, L"Add mapping from %ls to %ls", escape(sequence, 1), escape(command, 1 ) );
for( i=0; i<al_get_count( &mappings); i++ )
for( i=0; i<mapping_list.size(); i++ )
{
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i );
if( wcscmp( m->seq, sequence ) == 0 )
input_mapping_t &m = mapping_list.at(i);
if( m.seq == sequence )
{
m->command = intern(command);
m.command = command;
return;
}
}
input_mapping_t *m = (input_mapping_t *)malloc( sizeof( input_mapping_t ) );
m->seq = intern( sequence );
m->command = intern(command);
al_push( &mappings, m );
mapping_list.push_back(input_mapping_t(sequence, command));
}
/**
@ -336,7 +331,7 @@ int input_init()
/*
If we have no keybindings, add a few simple defaults
*/
if( !al_get_count( &mappings ) )
if( mapping_list.size() )
{
input_mapping_add( L"", L"self-insert" );
input_mapping_add( L"\n", L"execute" );
@ -357,9 +352,6 @@ void input_destroy()
is_init=0;
al_foreach( &mappings, &free );
al_destroy( &mappings );
input_common_destroy();
if( del_curterm( cur_term ) == ERR )
@ -374,9 +366,9 @@ void input_destroy()
/**
Perform the action of the specified binding
*/
static wint_t input_exec_binding( input_mapping_t *m, const wchar_t *seq )
static wint_t input_exec_binding( const input_mapping_t &m, const wcstring &seq )
{
wchar_t code = input_function_get_code( m->command );
wchar_t code = input_function_get_code( m.command );
if( code != -1 )
{
switch( code )
@ -403,7 +395,7 @@ static wint_t input_exec_binding( input_mapping_t *m, const wchar_t *seq )
*/
int last_status = proc_get_last_status();
eval( m->command, 0, TOP );
eval( m.command.c_str(), 0, TOP );
proc_set_last_status( last_status );
@ -427,7 +419,7 @@ static wint_t input_exec_binding( input_mapping_t *m, const wchar_t *seq )
/**
Try reading the specified function mapping
*/
static wint_t input_try_mapping( input_mapping_t *m)
static wint_t input_try_mapping( const input_mapping_t &m)
{
int j, k;
wint_t c=0;
@ -436,22 +428,20 @@ static wint_t input_try_mapping( input_mapping_t *m)
Check if the actual function code of this mapping is on the stack
*/
c = input_common_readch( 0 );
if( c == input_function_get_code( m->command ) )
if( c == input_function_get_code( m.command ) )
{
return input_exec_binding( m, m->seq );
return input_exec_binding( m, m.seq );
}
input_unreadch( c );
if( m->seq != 0 )
{
for( j=0; m->seq[j] != L'\0' &&
m->seq[j] == (c=input_common_readch( j>0 )); j++ )
const wchar_t *str = m.seq.c_str();
for( j=0; str[j] != L'\0' &&
str[j] == (c=input_common_readch( j>0 )); j++ )
;
if( m->seq[j] == L'\0' )
if( str[j] == L'\0' )
{
return input_exec_binding( m, m->seq );
return input_exec_binding( m, m.seq );
}
else
{
@ -461,8 +451,7 @@ static wint_t input_try_mapping( input_mapping_t *m)
input_unreadch(c);
for( k=j-1; k>=0; k-- )
{
input_unreadch( m->seq[k] );
}
input_unreadch( m.seq[k] );
}
}
return 0;
@ -477,7 +466,7 @@ void input_unreadch( wint_t ch )
wint_t input_readch()
{
int i;
size_t i;
CHECK_BLOCK( R_NULL );
@ -492,17 +481,17 @@ wint_t input_readch()
while( 1 )
{
input_mapping_t *generic = 0;
for( i=0; i<al_get_count( &mappings); i++ )
const input_mapping_t *generic = 0;
for( i=0; i<mapping_list.size(); i++ )
{
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i );
const input_mapping_t &m = mapping_list.at(i);
wint_t res = input_try_mapping( m );
if( res )
return res;
if( wcslen( m->seq) == 0 )
if( m.seq.length() == 0 )
{
generic = m;
generic = &m;
}
}
@ -521,7 +510,7 @@ wint_t input_readch()
;
arr[0] = input_common_readch(0);
return input_exec_binding( generic, arr );
return input_exec_binding( *generic, arr );
}
/*
@ -531,63 +520,57 @@ wint_t input_readch()
input_common_readch( 0 ); }
}
void input_mapping_get_names( array_list_t *list )
void input_mapping_get_names( wcstring_list_t &lst )
{
int i;
size_t i;
for( i=0; i<al_get_count( &mappings ); i++ )
for( i=0; i<mapping_list.size(); i++ )
{
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i );
al_push( list, m->seq );
const input_mapping_t &m = mapping_list.at(i);
lst.push_back(wcstring(m.seq));
}
}
int input_mapping_erase( const wchar_t *sequence )
bool input_mapping_erase( const wchar_t *sequence )
{
int ok = 0;
int i;
size_t sz = al_get_count( &mappings );
ASSERT_IS_MAIN_THREAD();
bool result = false;
size_t i, sz = mapping_list.size();
for( i=0; i<sz; i++ )
{
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i );
if( !wcscmp( sequence, m->seq ) )
const input_mapping_t &m = mapping_list.at(i);
if( sequence == m.seq )
{
if( i != (sz-1 ) )
{
al_set( &mappings, i, al_get( &mappings, sz -1 ) );
mapping_list[i] = mapping_list[sz-1];
}
al_truncate( &mappings, sz-1 );
ok = 1;
free( m );
mapping_list.pop_back();
result = true;
break;
}
}
return ok;
return result;
}
const wchar_t *input_mapping_get( const wchar_t *sequence )
bool input_mapping_get( const wcstring &sequence, wcstring &cmd )
{
int i;
size_t sz = al_get_count( &mappings );
size_t i, sz = mapping_list.size();
for( i=0; i<sz; i++ )
{
input_mapping_t *m = (input_mapping_t *)al_get( &mappings, i );
if( !wcscmp( sequence, m->seq ) )
const input_mapping_t &m = mapping_list.at(i);
if( sequence == m.seq )
{
return m->command;
cmd = m.command;
return true;
}
}
return 0;
return false;
}
/**
@ -805,19 +788,12 @@ const wchar_t *input_terminfo_get_sequence( const wchar_t *name )
}
const wchar_t *input_terminfo_get_name( const wchar_t *seq )
bool input_terminfo_get_name( const wcstring &seq, wcstring &name )
{
int i;
static string_buffer_t *buff = 0;
CHECK( seq, 0 );
input_init();
if( !buff )
{
buff = sb_halloc( global_context );
}
for( i=0; i<al_get_count( terminfo_mappings ); i++ )
{
terminfo_mapping_t *m = (terminfo_mapping_t *)al_get( terminfo_mappings, i );
@ -827,17 +803,14 @@ const wchar_t *input_terminfo_get_name( const wchar_t *seq )
continue;
}
sb_clear( buff );
sb_printf( buff, L"%s", m->seq );
if( !wcscmp( seq, (wchar_t *)buff->buff ) )
{
return m->name;
const wcstring map_buf = format_string(L"%s", m->seq);
if (map_buf == seq) {
name = m->name;
return true;
}
}
return 0;
return false;
}
void input_terminfo_get_names( array_list_t *lst, int skip_null )
@ -861,7 +834,7 @@ void input_terminfo_get_names( array_list_t *lst, int skip_null )
void input_function_get_names( array_list_t *lst )
{
int i;
size_t i;
CHECK( lst, );
@ -871,13 +844,13 @@ void input_function_get_names( array_list_t *lst )
}
}
wchar_t input_function_get_code( const wchar_t *name )
wchar_t input_function_get_code( const wcstring &name )
{
int i;
size_t i;
for( i = 0; i<(sizeof( code_arr )/sizeof(wchar_t)) ; i++ )
{
if( wcscmp( name, name_arr[i] ) == 0 )
if( name == name_arr[i] )
{
return code_arr[i];
}

14
input.h
View file

@ -97,19 +97,19 @@ void input_unreadch( wint_t ch );
void input_mapping_add( const wchar_t *sequence, const wchar_t *command );
/**
Insert all mapping names into the specified array_list_t
Insert all mapping names into the specified wcstring_list_t
*/
void input_mapping_get_names( array_list_t *list );
void input_mapping_get_names( wcstring_list_t &lst );
/**
Erase binding for specified key sequence
*/
int input_mapping_erase( const wchar_t *sequence );
bool input_mapping_erase( const wchar_t *sequence );
/**
Return the command bound to the specified key sequence
Gets the command bound to the specified key sequence. Returns true if it exists, false if not.
*/
const wchar_t *input_mapping_get( const wchar_t *sequence );
bool input_mapping_get( const wcstring &sequence, wcstring &cmd );
/**
Return the sequence for the terminfo variable of the specified name.
@ -122,7 +122,7 @@ const wchar_t *input_terminfo_get_sequence( const wchar_t *name );
/**
Return the name of the terminfo variable with the specified sequence
*/
const wchar_t *input_terminfo_get_name( const wchar_t *seq );
bool input_terminfo_get_name( const wcstring &seq, wcstring &name );
/**
Return a list of all known terminfo names
@ -133,7 +133,7 @@ void input_terminfo_get_names( array_list_t *lst, int skip_null );
/**
Returns the input function code for the given input function name.
*/
wchar_t input_function_get_code( const wchar_t *name );
wchar_t input_function_get_code( const wcstring &name );
/**
Returns a list of all existing input function names

View file

@ -30,6 +30,7 @@ static hash_table_t *intern_static_table=0;
const wchar_t *intern( const wchar_t *in )
{
ASSERT_IS_MAIN_THREAD();
const wchar_t *res=0;
// debug( 0, L"intern %ls", in );

View file

@ -223,31 +223,21 @@ static void kill_check_x_buffer()
if( (disp = env_get( L"DISPLAY" )) )
{
int i;
wchar_t *cmd = L"xsel -t 500 -b";
wchar_t *new_cut_buffer=0;
array_list_t list;
al_init( &list );
if( exec_subshell( cmd, &list ) != -1 )
size_t i;
wcstring cmd = L"xsel -t 500 -b";
wcstring new_cut_buffer=L"";
wcstring_list_t list;
if( exec_subshell2( cmd, list ) != -1 )
{
for( i=0; i<al_get_count( &list ); i++ )
for( i=0; i<list.size(); i++ )
{
wchar_t *next_line = escape( (wchar_t *)al_get( &list, i ), 0 );
if( i==0 )
{
new_cut_buffer = next_line;
}
else
{
wchar_t *old = new_cut_buffer;
new_cut_buffer= wcsdupcat( new_cut_buffer, L"\\n", next_line );
free( old );
free( next_line );
}
wcstring next_line = escape_string( list.at(i), 0 );
if (i > 0) new_cut_buffer += L"\\n";
new_cut_buffer += next_line;
}
if( new_cut_buffer )
if( new_cut_buffer.size() > 0 )
{
/*
The buffer is inserted with backslash escapes,
@ -255,29 +245,14 @@ static void kill_check_x_buffer()
etc. anyway.
*/
if( cut_buffer != 0 )
if (cut_buffer == NULL || cut_buffer != new_cut_buffer)
{
if( wcscmp( new_cut_buffer, cut_buffer ) == 0 )
{
free( new_cut_buffer );
new_cut_buffer = 0;
}
else
{
free( cut_buffer );
cut_buffer = 0;
}
}
if( cut_buffer == 0 )
{
cut_buffer = new_cut_buffer;
free(cut_buffer);
cut_buffer = wcsdup(new_cut_buffer.c_str());
kill_add_internal( cut_buffer );
}
}
}
al_foreach( &list, &free );
al_destroy( &list );
}
}

View file

@ -1200,7 +1200,7 @@ int wildcard_expand( const wchar_t *wc,
return res;
}
int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, int flags, std::vector<wcstring> &outputs )
int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, int flags, wcstring_list_t &outputs )
{
array_list_t lst;
al_init(&lst);

View file

@ -70,7 +70,7 @@ int wildcard_expand( const wchar_t *wc,
int flags,
array_list_t *out );
int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, int flags, std::vector<wcstring> &out );
int wildcard_expand_string(const wcstring &wc, const wcstring &base_dir, int flags, wcstring_list_t &out );
/**
Test whether the given wildcard matches the string