From 8a2737566c57404a238bee8b14edff9f5ea334ed Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 7 Feb 2012 22:44:10 -0800 Subject: [PATCH] Excised some more halloc --- complete.cpp | 64 +++++++++++++++++++++++++--------------------------- complete.h | 2 +- fish.cpp | 21 +++++------------ output.cpp | 54 +++++++++++--------------------------------- path.cpp | 12 ++++++++++ path.h | 1 + 6 files changed, 64 insertions(+), 90 deletions(-) diff --git a/complete.cpp b/complete.cpp index 8ae81f59b..c1ac911f8 100644 --- a/complete.cpp +++ b/complete.cpp @@ -299,16 +299,6 @@ static void complete_free_opt_recursive( complete_entry_opt_t *o ) halloc_free( o ); } -/** - Free a complete_entry_t and its contents -*/ -static void complete_free_entry( complete_entry_t *c ) -{ -// free( c->cmd ); - free( c->short_opt_str ); - complete_free_opt_recursive( c->first_option ); - free( c ); -} /** Search for an exactly matching completion entry @@ -584,6 +574,24 @@ static void parse_cmd_string( void *context, *cmdp=cmd; } +/** + Find the full path and commandname from a command string 'str'. +*/ +static void parse_cmd_string(const wcstring &str, wcstring &path, wcstring &cmd) { + if (! path_get_path_string(str, path)) { + /** Use the empty string as the 'path' for commands that can not be found. */ + path = L""; + } + + /* Make sure the path is not included in the command */ + size_t last_slash = str.find_last_of(L'/'); + if (last_slash != wcstring::npos) { + cmd = str.substr(last_slash + 1); + } else { + cmd = str; + } +} + int complete_is_valid_option( const wchar_t *str, const wchar_t *opt, array_list_t *errors, @@ -591,7 +599,7 @@ int complete_is_valid_option( const wchar_t *str, { complete_entry_t *i; complete_entry_opt_t *o; - wchar_t *cmd, *path; + wcstring cmd, path; int found_match = 0; int authoritative = 1; int opt_found=0; @@ -601,9 +609,9 @@ int complete_is_valid_option( const wchar_t *str, int is_short_opt=0; int is_gnu_exact=0; int gnu_opt_len=0; - char *short_validated; + + std::vector short_validated; - void *context; CHECK( str, 0 ); CHECK( opt, 0 ); @@ -639,17 +647,9 @@ int complete_is_valid_option( const wchar_t *str, return 0; } - context = halloc( 0, 0 ); - - if( !(short_validated = (char *)halloc( context, wcslen( opt ) ))) - { - DIE_MEM(); - } - - - - memset( short_validated, 0, wcslen( opt ) ); + short_validated.resize(wcslen(opt), 0); + hash_init( &gnu_match_hash, &hash_wcs_func, &hash_wcs_cmp ); @@ -668,16 +668,16 @@ int complete_is_valid_option( const wchar_t *str, } } - parse_cmd_string( context, str, &path, &cmd ); + parse_cmd_string( str, path, cmd ); /* Make sure completions are loaded for the specified command */ - if (allow_autoload) complete_load( cmd, 0 ); + if (allow_autoload) complete_load( cmd, false ); for( i=first_entry; i; i=i->next ) { - wchar_t *match = i->cmd_type?path:cmd; + const wcstring &match = i->cmd_type?path:cmd; const wchar_t *a; if( !wildcard_match( match, i->cmd ) ) @@ -756,12 +756,12 @@ int complete_is_valid_option( const wchar_t *str, nopt[1]=opt[1]; nopt[2]=L'\0'; - short_validated[a-opt] = + short_validated.at(a-opt) = complete_is_valid_argument( str, nopt, &opt[2]); } else { - short_validated[a-opt]=1; + short_validated.at(a-opt)=1; } } } @@ -782,7 +782,7 @@ int complete_is_valid_option( const wchar_t *str, opt_found=1; for( j=1; jnext ) { diff --git a/complete.h b/complete.h index df14ea872..269c9f103 100644 --- a/complete.h +++ b/complete.h @@ -256,7 +256,7 @@ int complete_is_valid_argument( const wchar_t *str, \param cmd the command for which to load command-specific completions \param reload should the commands completions be reloaded, even if they where previously loaded. (This is set to true on actual completions, so that changed completion are updated in running shells) */ -void complete_load( const wchar_t *cmd, int reload ); +void complete_load( const wcstring &cmd, bool reload ); /** Create a new completion entry diff --git a/fish.cpp b/fish.cpp index 363338145..9b57bb1f4 100644 --- a/fish.cpp +++ b/fish.cpp @@ -73,10 +73,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ static int read_init() { - wchar_t *config_dir; - wchar_t *config_dir_escaped; - void *context; - string_buffer_t *eval_buff; parser_t &parser = parser_t::principal_parser(); parser.eval( L"builtin . " DATADIR "/fish/config.fish 2>/dev/null", 0, TOP ); @@ -85,24 +81,19 @@ static int read_init() /* We need to get the configuration directory before we can source the user configuration file */ - context = halloc( 0, 0 ); - eval_buff = sb_halloc( context ); - config_dir = path_get_config( context ); + wcstring config_dir; /* - If config_dir is null then we have no configuration directory + If path_get_config returns false then we have no configuration directory and no custom config to load. */ - if( config_dir ) + if (path_get_config(config_dir)) { - config_dir_escaped = escape( config_dir, 1 ); - sb_printf( eval_buff, L"builtin . %ls/config.fish 2>/dev/null", config_dir_escaped ); - parser.eval( (wchar_t *)eval_buff->buff, 0, TOP ); - free( config_dir_escaped ); + wcstring config_dir_escaped = escape_string( config_dir, 1 ); + wcstring eval_buff = format_string(L"builtin . %ls/config.fish 2>/dev/null", config_dir_escaped.c_str()); + parser.eval( eval_buff.c_str(), 0, TOP ); } - halloc_free( context ); - return 1; } diff --git a/output.cpp b/output.cpp index 32d4fe133..979d9c38e 100644 --- a/output.cpp +++ b/output.cpp @@ -102,18 +102,7 @@ static const int col_idx[]= 6, 7, FISH_COLOR_NORMAL, -} - ; - -/** - Size of writestr_buff -*/ -static size_t writestr_buff_sz=0; - -/** - Temp buffer used for converting from wide to narrow strings -*/ -static char *writestr_buff = 0; +}; /** The function used for output @@ -127,14 +116,6 @@ static int (*out)(char c) = &writeb_internal; static wchar_t *current_term = 0; -/** - Cleanup function. Run automatically through halloc -*/ -static void output_destroy() -{ - free( writestr_buff ); -} - void output_set_writer( int (*writer)(char) ) { CHECK( writer, ); @@ -436,39 +417,30 @@ void writestr( const wchar_t *str ) } len++; - - /* - Reallocate if needed - */ - if( writestr_buff_sz < len ) - { - if( !writestr_buff ) - { - halloc_register_function_void( global_context, &output_destroy ); - } - writestr_buff = (char *)realloc( writestr_buff, len ); - if( !writestr_buff ) - { - DIE_MEM(); - } - writestr_buff_sz = len; - } - /* Convert */ - wcstombs( writestr_buff, + char *buffer, static_buffer[256]; + if (len <= sizeof static_buffer) + buffer = static_buffer; + else + buffer = new char[len]; + + wcstombs( buffer, str, - writestr_buff_sz ); + len ); /* Write */ - for( pos = writestr_buff; *pos; pos++ ) + for( pos = buffer; *pos; pos++ ) { out( *pos ); } + + if (buffer != static_buffer) + delete[] buffer; } diff --git a/path.cpp b/path.cpp index a959f4106..930f8563e 100644 --- a/path.cpp +++ b/path.cpp @@ -252,6 +252,18 @@ wchar_t *path_get_path( const wchar_t *cmd ) return 0; } +bool path_get_path_string(const wcstring &cmd, wcstring &output) +{ + bool success = false; + wchar_t *tmp = path_get_path(cmd.c_str()); + if (tmp) { + output = tmp; + free(tmp); + success = true; + } + return success; +} + bool path_get_cdpath_string(const wcstring &dir_str, wcstring &result, const env_vars &vars) { diff --git a/path.h b/path.h index 8f63d14ea..d4169e11a 100644 --- a/path.h +++ b/path.h @@ -34,6 +34,7 @@ bool path_get_config(wcstring &path); wchar_t *path_get_path( const wchar_t *cmd ); class env_vars; +bool path_get_path_string(const wcstring &cmd, wcstring &output); bool path_get_path_string(const wcstring &cmd, wcstring &output, const env_vars &vars); /**