From 62f49c55cec0dc8b4b2b41d1eff4f1a02ba18431 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Wed, 1 Feb 2012 16:27:14 -0800 Subject: [PATCH] Clean up uses of completion_t --- builtin.cpp | 4 +- common.h | 9 ++- complete.cpp | 80 +++++++------------------- complete.h | 30 +++++++--- expand.cpp | 157 +++++++++++---------------------------------------- expand.h | 2 +- parser.cpp | 16 ++---- reader.h | 2 +- wildcard.cpp | 7 +-- wildcard.h | 2 +- 10 files changed, 91 insertions(+), 218 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index a618479b9..fc0d5afdc 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -3717,9 +3717,7 @@ wcstring_list_t builtin_get_names(void) void builtin_get_names(std::vector &list) { for (size_t i=0; i < BUILTIN_COUNT; i++) { - completion_t data_to_push; - data_to_push.completion = builtin_datas[i].name; - list.push_back( data_to_push ); + list.push_back(completion_t(builtin_datas[i].name)); } } diff --git a/common.h b/common.h index 03114addf..6aed5efcc 100644 --- a/common.h +++ b/common.h @@ -22,7 +22,7 @@ #include #include "util.h" -struct completion_t; +class completion_t; /* Common string type */ typedef std::wstring wcstring; @@ -298,6 +298,13 @@ T from_string(const wcstring &x) { return result; } +template +wcstring to_string(const T &x) { + std::wstringstream stream; + stream << x; + return stream.str(); +} + class scoped_lock { pthread_mutex_t *lock_obj; bool locked; diff --git a/complete.cpp b/complete.cpp index 75771143b..9b745dfcd 100644 --- a/complete.cpp +++ b/complete.cpp @@ -209,31 +209,9 @@ static void complete_free_entry( complete_entry_t *c ); Create a new completion entry */ -void completion_allocate( std::vector &context, - const wchar_t *comp, - const wchar_t *desc, - int flags ) +void completion_allocate(std::vector &completions, const wcstring &comp, const wcstring &desc, int flags) { -// completion_t *res = (completion_t *)halloc( context, sizeof( completion_t) ); - completion_t res; - - res.completion = comp; - if( desc ) - res.description = desc; - - if( flags & COMPLETE_AUTO_SPACE ) - { - int len = wcslen(comp); - - flags = flags & (~COMPLETE_AUTO_SPACE); - - if( ( len > 0 ) && ( wcschr( L"/=@:", comp[ len - 1 ] ) != 0 ) ) - flags |= COMPLETE_NO_SPACE; - - } - - res.flags = flags; - context.push_back( res ); + completions.push_back(completion_t(comp, desc, flags)); } /** @@ -1116,9 +1094,7 @@ static void complete_cmd( const wchar_t *cmd, if( use_command ) { - if( expand_string2( wcsdup(cmd), - comp, - ACCEPT_INCOMPLETE | EXECUTABLES_ONLY ) != EXPAND_ERROR ) + if( expand_string2(cmd, comp, ACCEPT_INCOMPLETE | EXECUTABLES_ONLY ) != EXPAND_ERROR ) { complete_cmd_desc( cmd, comp ); } @@ -1189,9 +1165,7 @@ static void complete_cmd( const wchar_t *cmd, //function_get_names( &possible_comp, cmd[0] == L'_' ); wcstring_list_t names = function_get_names(cmd[0] == L'_' ); for (size_t i=0; i < names.size(); i++) { - completion_t data_to_push; - data_to_push.completion = names.at(i); - possible_comp.push_back( data_to_push ); + possible_comp.push_back(completion_t(names.at(i))); } complete_strings( comp, cmd, 0, &complete_function_desc, possible_comp, 0 ); @@ -1383,7 +1357,6 @@ static int complete_param( const wchar_t *cmd_orig, complete_entry_t *i; complete_entry_opt_t *o; - array_list_t matches; wchar_t *cmd, *path; int use_common=1, use_files=1; @@ -1393,8 +1366,6 @@ static int complete_param( const wchar_t *cmd_orig, complete_load( cmd, 1 ); - al_init( &matches ); - for( i=first_entry; i; i=i->next ) { wchar_t *match = i->cmd_type?path:cmd; @@ -1631,72 +1602,61 @@ static int complete_variable( const wchar_t *whole_var, int start_offset, std::vector &comp_list ) { - int i; const wchar_t *var = &whole_var[start_offset]; int varlen = wcslen( var ); int res = 0; - array_list_t names; - al_init( &names ); - env_get_names( &names, 0 ); - - for( i=0; i namelen ) continue; - match = ( wcsncmp( var, name, varlen) == 0 ); + match = string_prefixes_string(var, env_name); if( !match ) { - match_no_case = ( wcsncasecmp( var, name, varlen) == 0 ); + match_no_case = ( wcsncasecmp( var, env_name.c_str(), varlen) == 0 ); } if( match || match_no_case ) { - const env_var_t value_unescaped = env_get_string( name ); + const env_var_t value_unescaped = env_get_string( env_name.c_str() ); if( !value_unescaped.missing() ) { - string_buffer_t desc; - string_buffer_t comp; + wcstring comp; int flags = 0; int offset = 0; - sb_init( &comp ); if( match ) { - sb_append( &comp, &name[varlen] ); + comp.append(env_name.c_str() + varlen); offset = varlen; } else { - sb_append_substring( &comp, whole_var, start_offset ); - sb_append( &comp, name ); + comp.append(whole_var, start_offset); + comp.append(env_name); flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE; } wcstring value = expand_escape_variable2( value_unescaped ); - - sb_init( &desc ); - sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value.c_str() ); - + + wcstring desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str()); completion_allocate( comp_list, - (wchar_t *)comp.buff, - (wchar_t *)desc.buff, + comp.c_str(), + desc.c_str(), flags ); res =1; - - sb_destroy( &desc ); - sb_destroy( &comp ); } } } - al_destroy( &names ); return res; } diff --git a/complete.h b/complete.h index cb7c2e28e..daf95d408 100644 --- a/complete.h +++ b/complete.h @@ -102,9 +102,14 @@ -struct completion_t +class completion_t { +private: + /* No public default constructor */ + completion_t(){ } +public: + /** The completion string */ @@ -126,12 +131,21 @@ struct completion_t is case insensitive. */ int flags; - - completion_t() : flags(0) { } + + completion_t(const wcstring &comp, const wcstring &desc = L"", int flags_val = 0) : completion(comp), description(desc), flags(flags_val) { + if( flags & COMPLETE_AUTO_SPACE ) + { + flags = flags & ~COMPLETE_AUTO_SPACE; + size_t len = completion.size(); + if (len > 0 && ( wcschr( L"/=@:", comp.at(len-1)) != 0 )) + flags |= COMPLETE_NO_SPACE; + + } + } bool operator < (const completion_t& rhs) const { return this->completion < rhs.completion; } bool operator == (const completion_t& rhs) const { return this->completion == rhs.completion; } - bool operator != (const completion_t& rhs) const { return this->completion != rhs.completion; } + bool operator != (const completion_t& rhs) const { return ! (*this == rhs); } } ; @@ -257,15 +271,13 @@ void complete_load( const wchar_t *cmd, int reload ); /** Create a new completion entry - \param context The halloc context to use for allocating new memory + \param completions The array of completions to append to \param comp The completion string \param desc The description of the completion \param flags completion flags + */ -void completion_allocate( std::vector &context, - const wchar_t *comp, - const wchar_t *desc, - int flags ); +void completion_allocate(std::vector &completions, const wcstring &comp, const wcstring &desc, int flags); #endif diff --git a/expand.cpp b/expand.cpp index 122876e9e..bc23d5826 100644 --- a/expand.cpp +++ b/expand.cpp @@ -385,7 +385,6 @@ static int find_process( const wchar_t *proc, wchar_t *cmd=0; int sz=0; int found = 0; - wchar_t *result; job_t *j; @@ -441,11 +440,8 @@ static int find_process( const wchar_t *proc, { { - result = (wchar_t *)malloc(sizeof(wchar_t)*16 ); - swprintf( result, 16, L"%d", j->pgid ); - completion_t data_to_push; - data_to_push.completion = result; - out.push_back( data_to_push); + wcstring result = format_string(L"%ld", (long)j->pgid); + out.push_back(completion_t(result)); found = 1; } } @@ -474,11 +470,8 @@ static int find_process( const wchar_t *proc, } else { - result = (wchar_t *)malloc(sizeof(wchar_t)*16 ); - swprintf( result, 16, L"%d", j->pgid ); - completion_t data_to_push; - data_to_push.completion = result; - out.push_back( data_to_push); + wcstring result = format_string(L"%ld", (long)j->pgid); + out.push_back(completion_t(result)); found = 1; } } @@ -513,11 +506,8 @@ static int find_process( const wchar_t *proc, } else { - result = (wchar_t *)malloc(sizeof(wchar_t)*16 ); - swprintf( result, 16, L"%d", p->pid ); - completion_t data_to_push; - data_to_push.completion = result; - out.push_back( data_to_push ); + wcstring result = to_string(p->pid); + out.push_back(completion_t(result)); found = 1; } } @@ -631,12 +621,8 @@ static int find_process( const wchar_t *proc, } else { - wchar_t *res = wcsdup(name); - if( res ) { - completion_t data_to_push; - data_to_push.completion = res; - out.push_back( data_to_push ); - } + if (name) + out.push_back(completion_t(name)); } } } @@ -663,9 +649,7 @@ static int expand_pid( const wcstring &instr, if( instr.empty() || instr.at(0) != PROCESS_EXPAND ) { - completion_t data_to_push; - data_to_push.completion = instr; - out.push_back( data_to_push ); + out.push_back(completion_t(instr)); return 1; } @@ -692,28 +676,18 @@ static int expand_pid( const wcstring &instr, { if( wcscmp( (in+1), SELF_STR )==0 ) { - wchar_t str[32]; - swprintf( str, 32, L"%d", getpid() ); - - completion_t data_to_push; - data_to_push.completion = str; - - out.push_back( data_to_push ); + + const wcstring pid_str = to_string(getpid()); + out.push_back(completion_t(pid_str)); return 1; } if( wcscmp( (in+1), LAST_STR )==0 ) { - wchar_t *str; - if( proc_last_bg_pid > 0 ) { - str = (wchar_t *)malloc( sizeof(wchar_t)*32); - swprintf( str, 32, L"%d", proc_last_bg_pid ); - completion_t data_to_push; - data_to_push.completion = str; - - out.push_back( data_to_push); + const wcstring pid_str = to_string(proc_last_bg_pid); + out.push_back( completion_t(pid_str)); } return 1; @@ -821,54 +795,7 @@ void expand_variable_error( parser_t &parser, const wchar_t *token, int token_po /** Parse an array slicing specification */ -static int parse_slice( const wchar_t *in, wchar_t **end_ptr, array_list_t *idx ) -{ - - - wchar_t *end; - - int pos = 1; - -// debug( 0, L"parse_slice on '%ls'", in ); - - - while( 1 ) - { - long tmp; - - while( iswspace(in[pos]) || (in[pos]==INTERNAL_SEPARATOR)) - pos++; - - if( in[pos] == L']' ) - { - pos++; - break; - } - - errno=0; - tmp = wcstol( &in[pos], &end, 10 ); - if( ( errno ) || ( end == &in[pos] ) ) - { - return 1; - } -// debug( 0, L"Push idx %d", tmp ); - - al_push_long( idx, tmp ); - pos = end-in; - } - - if( end_ptr ) - { -// debug( 0, L"Remainder is '%ls', slice def was %d characters long", in+pos, pos ); - - *end_ptr = (wchar_t *)(in+pos); - } -// debug( 0, L"ok, done" ); - - return 0; -} - -static int parse_slice2( const wchar_t *in, wchar_t **end_ptr, std::vector &idx ) +static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector &idx ) { @@ -1002,7 +929,7 @@ static int expand_variables_internal( parser_t &parser, wchar_t * const in, std: wchar_t *slice_end; all_vars=0; - if( parse_slice2( in + stop_pos, &slice_end, var_idx_list ) ) + if( parse_slice( in + stop_pos, &slice_end, var_idx_list ) ) { parser.error( SYNTAX_ERROR, -1, @@ -1084,9 +1011,7 @@ static int expand_variables_internal( parser_t &parser, wchar_t * const in, std: const wcstring &next = var_item_list.at(j); if( is_ok && (i == 0) && (!in[stop_pos]) ) { - completion_t data_to_push; - data_to_push.completion = next; - out.push_back( data_to_push ); + out.push_back(completion_t(next)); } else { @@ -1150,9 +1075,7 @@ static int expand_variables_internal( parser_t &parser, wchar_t * const in, std: if( !empty ) { - completion_t data_to_push; - data_to_push.completion = in; - out.push_back( data_to_push ); + out.push_back(completion_t(in)); } return is_ok; @@ -1249,9 +1172,7 @@ static int expand_brackets(parser_t &parser, const wchar_t *in, int flags, std:: if( bracket_begin == 0 ) { - completion_t data_to_push; - data_to_push.completion = in; - out.push_back( data_to_push ); + out.push_back(completion_t(in)); return 1; } @@ -1316,21 +1237,19 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector const wchar_t * const in = input.c_str(); - completion_t data_to_push; int parse_ret; switch( parse_ret = parse_util_locate_cmdsubst(in, - ¶n_begin, - ¶n_end, - 0 ) ) + ¶n_begin, + ¶n_end, + 0 ) ) { case -1: parser.error( SYNTAX_ERROR, - -1, - L"Mismatched parans" ); + -1, + L"Mismatched parans" ); return 0; case 0: - data_to_push.completion = input; - outList.push_back(data_to_push); + outList.push_back(completion_t(input)); return 1; case 1: @@ -1355,7 +1274,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector std::vector slice_idx; wchar_t *slice_end; - if( parse_slice2( tail_begin, &slice_end, slice_idx ) ) + if( parse_slice( tail_begin, &slice_end, slice_idx ) ) { parser.error( SYNTAX_ERROR, -1, L"Invalid index value" ); return 0; @@ -1429,9 +1348,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector whole_item.append(tail_item); //al_push( out, whole_item.buff ); - completion_t data_to_push; - data_to_push.completion = whole_item; - outList.push_back(data_to_push); + outList.push_back(completion_t(whole_item)); } } @@ -1600,9 +1517,7 @@ int expand_string2( const wcstring &input, std::vector &output, in if( (!(flags & ACCEPT_INCOMPLETE)) && expand_is_clean( input.c_str() ) ) { - completion_t data_to_push; - data_to_push.completion = input; - output.push_back(data_to_push); + output.push_back(completion_t(input)); return EXPAND_OK; } @@ -1618,9 +1533,7 @@ int expand_string2( const wcstring &input, std::vector &output, in parser.error( CMDSUBST_ERROR, -1, L"Command substitutions not allowed" ); return EXPAND_ERROR; } - completion_t data_to_push; - data_to_push.completion = input; - list1.push_back(data_to_push); + list1.push_back(completion_t(input)); } else { @@ -1649,9 +1562,7 @@ int expand_string2( const wcstring &input, std::vector &output, in next[i] = L'$'; } } - completion_t data_to_push; - data_to_push.completion = next; - out->push_back(data_to_push); + out->push_back(completion_t(next)); } else { @@ -1702,9 +1613,7 @@ int expand_string2( const wcstring &input, std::vector &output, in } else { - completion_t data_to_push; - data_to_push.completion = next; - out->push_back(data_to_push); + out->push_back(completion_t(next)); } } else @@ -1798,9 +1707,7 @@ int expand_string2( const wcstring &input, std::vector &output, in } else { - completion_t data_to_push; - data_to_push.completion = next; - output.push_back(data_to_push); + output.push_back(completion_t(next)); } } diff --git a/expand.h b/expand.h index 1f188fe14..81a231dd0 100644 --- a/expand.h +++ b/expand.h @@ -65,7 +65,7 @@ */ #define EXPAND_RESERVED_END 0xf000f -struct completion_t; +class completion_t; enum { diff --git a/parser.cpp b/parser.cpp index c27392d21..340267701 100644 --- a/parser.cpp +++ b/parser.cpp @@ -1872,9 +1872,7 @@ int parser_t::parse_job( process_t *p, } } } - completion_t data_to_push; - data_to_push.completion = nxt; - args.push_back( data_to_push ); + args.push_back(completion_t(nxt)); } if( error_code == 0 ) @@ -1922,12 +1920,8 @@ int parser_t::parse_job( process_t *p, // al_truncate( args, 0 ); args.clear(); // al_push( args, halloc_wcsdup( j, L"cd" ) ); - completion_t comp; - comp.completion = L"cd"; - args.push_back(comp); - completion_t comp2; - comp2.completion = tmp; - args.push_back( comp2 ); + args.push_back(completion_t(L"cd")); + args.push_back(completion_t(tmp)); /* If we have defined a wrapper around cd, use it, otherwise use the cd builtin @@ -2108,9 +2102,7 @@ int parser_t::parse_job( process_t *p, const wcstring sub_block(tok_string( tok ) + current_tokenizer_pos, end_pos - current_tokenizer_pos); p->type = INTERNAL_BLOCK; - completion_t data_to_push; - data_to_push.completion = sub_block; - args.at( 0 ) = data_to_push; + args.at( 0 ) = completion_t(sub_block); tok_set_pos( tok, end_pos ); diff --git a/reader.h b/reader.h index 6f99ba4f7..05c10642b 100644 --- a/reader.h +++ b/reader.h @@ -16,7 +16,7 @@ #include "io.h" class parser_t; -struct completion_t; +class completion_t; /** Read commands from \c fd until encountering EOF diff --git a/wildcard.cpp b/wildcard.cpp index 50610f80a..bdafe3318 100644 --- a/wildcard.cpp +++ b/wildcard.cpp @@ -870,8 +870,7 @@ static int wildcard_expand_internal( const wchar_t *wc, else { res = 1; - completion_t data_to_push; - data_to_push.completion = base_dir; + completion_t data_to_push(base_dir); if ( std::find( out.begin(), out.end(), data_to_push ) != out.end() ){ out.push_back( data_to_push); } @@ -944,9 +943,7 @@ static int wildcard_expand_internal( const wchar_t *wc, } else { - completion_t data_to_push; - data_to_push.completion = long_name; - out.push_back( data_to_push ); + out.push_back( completion_t(long_name) ); } res = 1; } diff --git a/wildcard.h b/wildcard.h index 891b0b5f5..9da7d716b 100644 --- a/wildcard.h +++ b/wildcard.h @@ -24,7 +24,7 @@ #define WILDCARD_RESERVED 0xf400 -struct completion_t; +class completion_t; /** Enumeration of all wildcard types */