From bef046a51a26b87cb5eb8a6e159b78c931a2e253 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sat, 31 Dec 2011 15:57:30 -0800 Subject: [PATCH] More work to migrate off of ad-hoc data structures --- builtin.cpp | 66 +++++++---------- fish_tests.cpp | 43 ----------- input.cpp | 191 +++++++++++++++++++++---------------------------- input.h | 14 ++-- intern.cpp | 1 + kill.cpp | 57 +++++---------- wildcard.cpp | 2 +- wildcard.h | 2 +- 8 files changed, 134 insertions(+), 242 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 181c2cef4..ca0d5a73a 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -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 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< 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; iseq, 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,35 +428,32 @@ 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++ ) - ; - - if( m->seq[j] == L'\0' ) - { - return input_exec_binding( m, m->seq ); - } - else - { - /* - Return the read characters - */ - input_unreadch(c); - for( k=j-1; k>=0; k-- ) - { - input_unreadch( m->seq[k] ); - } - } - } + 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( str[j] == L'\0' ) + { + return input_exec_binding( m, m.seq ); + } + else + { + /* + Return the read characters + */ + input_unreadch(c); + for( k=j-1; k>=0; 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; iseq) == 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; iseq ); + 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; iseq ) ) + 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; iseq ) ) + 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,18 +788,11 @@ 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; iseq ); - - if( !wcscmp( seq, (wchar_t *)buff->buff ) ) - { - return m->name; - } - } - - return 0; + const wcstring map_buf = format_string(L"%s", m->seq); + if (map_buf == seq) { + name = m->name; + return true; + } + } + 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]; } diff --git a/input.h b/input.h index cd275674f..99840c7db 100644 --- a/input.h +++ b/input.h @@ -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 diff --git a/intern.cpp b/intern.cpp index d52857f1e..2fed376fb 100644 --- a/intern.cpp +++ b/intern.cpp @@ -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 ); diff --git a/kill.cpp b/kill.cpp index 8d61bfcc4..36426af51 100644 --- a/kill.cpp +++ b/kill.cpp @@ -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 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( 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; - kill_add_internal( cut_buffer ); - } + if (cut_buffer == NULL || 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 ); } } diff --git a/wildcard.cpp b/wildcard.cpp index 02c651c7e..3e168bb2d 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -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 &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); diff --git a/wildcard.h b/wildcard.h index 22d136cd3..c82ae1f26 100644 --- a/wildcard.h +++ b/wildcard.h @@ -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 &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