Clean up uses of completion_t

This commit is contained in:
ridiculousfish 2012-02-01 16:27:14 -08:00
parent 0b4b6c498d
commit 62f49c55ce
10 changed files with 91 additions and 218 deletions

View file

@ -3717,9 +3717,7 @@ wcstring_list_t builtin_get_names(void)
void builtin_get_names(std::vector<completion_t> &list) { void builtin_get_names(std::vector<completion_t> &list) {
for (size_t i=0; i < BUILTIN_COUNT; i++) { for (size_t i=0; i < BUILTIN_COUNT; i++) {
completion_t data_to_push; list.push_back(completion_t(builtin_datas[i].name));
data_to_push.completion = builtin_datas[i].name;
list.push_back( data_to_push );
} }
} }

View file

@ -22,7 +22,7 @@
#include <assert.h> #include <assert.h>
#include "util.h" #include "util.h"
struct completion_t; class completion_t;
/* Common string type */ /* Common string type */
typedef std::wstring wcstring; typedef std::wstring wcstring;
@ -298,6 +298,13 @@ T from_string(const wcstring &x) {
return result; return result;
} }
template<typename T>
wcstring to_string(const T &x) {
std::wstringstream stream;
stream << x;
return stream.str();
}
class scoped_lock { class scoped_lock {
pthread_mutex_t *lock_obj; pthread_mutex_t *lock_obj;
bool locked; bool locked;

View file

@ -209,31 +209,9 @@ static void complete_free_entry( complete_entry_t *c );
Create a new completion entry Create a new completion entry
*/ */
void completion_allocate( std::vector<completion_t> &context, void completion_allocate(std::vector<completion_t> &completions, const wcstring &comp, const wcstring &desc, int flags)
const wchar_t *comp,
const wchar_t *desc,
int flags )
{ {
// completion_t *res = (completion_t *)halloc( context, sizeof( completion_t) ); completions.push_back(completion_t(comp, desc, flags));
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 );
} }
/** /**
@ -1116,9 +1094,7 @@ static void complete_cmd( const wchar_t *cmd,
if( use_command ) if( use_command )
{ {
if( expand_string2( wcsdup(cmd), if( expand_string2(cmd, comp, ACCEPT_INCOMPLETE | EXECUTABLES_ONLY ) != EXPAND_ERROR )
comp,
ACCEPT_INCOMPLETE | EXECUTABLES_ONLY ) != EXPAND_ERROR )
{ {
complete_cmd_desc( cmd, comp ); 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'_' ); //function_get_names( &possible_comp, cmd[0] == L'_' );
wcstring_list_t names = function_get_names(cmd[0] == L'_' ); wcstring_list_t names = function_get_names(cmd[0] == L'_' );
for (size_t i=0; i < names.size(); i++) { for (size_t i=0; i < names.size(); i++) {
completion_t data_to_push; possible_comp.push_back(completion_t(names.at(i)));
data_to_push.completion = names.at(i);
possible_comp.push_back( data_to_push );
} }
complete_strings( comp, cmd, 0, &complete_function_desc, possible_comp, 0 ); 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_t *i;
complete_entry_opt_t *o; complete_entry_opt_t *o;
array_list_t matches;
wchar_t *cmd, *path; wchar_t *cmd, *path;
int use_common=1, use_files=1; int use_common=1, use_files=1;
@ -1393,8 +1366,6 @@ static int complete_param( const wchar_t *cmd_orig,
complete_load( cmd, 1 ); complete_load( cmd, 1 );
al_init( &matches );
for( i=first_entry; i; i=i->next ) for( i=first_entry; i; i=i->next )
{ {
wchar_t *match = i->cmd_type?path:cmd; wchar_t *match = i->cmd_type?path:cmd;
@ -1631,72 +1602,61 @@ static int complete_variable( const wchar_t *whole_var,
int start_offset, int start_offset,
std::vector<completion_t> &comp_list ) std::vector<completion_t> &comp_list )
{ {
int i;
const wchar_t *var = &whole_var[start_offset]; const wchar_t *var = &whole_var[start_offset];
int varlen = wcslen( var ); int varlen = wcslen( var );
int res = 0; int res = 0;
array_list_t names;
al_init( &names ); const wcstring_list_t names = env_get_names(0);
env_get_names( &names, 0 ); for( size_t i=0; i<names.size(); i++ )
for( i=0; i<al_get_count( &names ); i++ )
{ {
wchar_t *name = (wchar_t *)al_get( &names, i ); const wcstring & env_name = names.at(i);
int namelen = wcslen( name ); int namelen = env_name.size();
int match=0, match_no_case=0; int match=0, match_no_case=0;
if( varlen > namelen ) if( varlen > namelen )
continue; continue;
match = ( wcsncmp( var, name, varlen) == 0 ); match = string_prefixes_string(var, env_name);
if( !match ) 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 ) 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() ) if( !value_unescaped.missing() )
{ {
string_buffer_t desc; wcstring comp;
string_buffer_t comp;
int flags = 0; int flags = 0;
int offset = 0; int offset = 0;
sb_init( &comp );
if( match ) if( match )
{ {
sb_append( &comp, &name[varlen] ); comp.append(env_name.c_str() + varlen);
offset = varlen; offset = varlen;
} }
else else
{ {
sb_append_substring( &comp, whole_var, start_offset ); comp.append(whole_var, start_offset);
sb_append( &comp, name ); comp.append(env_name);
flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE; flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE;
} }
wcstring value = expand_escape_variable2( value_unescaped ); wcstring value = expand_escape_variable2( value_unescaped );
sb_init( &desc ); wcstring desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
sb_printf( &desc, COMPLETE_VAR_DESC_VAL, value.c_str() );
completion_allocate( comp_list, completion_allocate( comp_list,
(wchar_t *)comp.buff, comp.c_str(),
(wchar_t *)desc.buff, desc.c_str(),
flags ); flags );
res =1; res =1;
sb_destroy( &desc );
sb_destroy( &comp );
} }
} }
} }
al_destroy( &names );
return res; return res;
} }

View file

@ -102,9 +102,14 @@
struct completion_t class completion_t
{ {
private:
/* No public default constructor */
completion_t(){ }
public:
/** /**
The completion string The completion string
*/ */
@ -126,12 +131,21 @@ struct completion_t
is case insensitive. is case insensitive.
*/ */
int flags; 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->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 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 comp The completion string
\param desc The description of the completion \param desc The description of the completion
\param flags completion flags \param flags completion flags
*/ */
void completion_allocate( std::vector<completion_t> &context, void completion_allocate(std::vector<completion_t> &completions, const wcstring &comp, const wcstring &desc, int flags);
const wchar_t *comp,
const wchar_t *desc,
int flags );
#endif #endif

View file

@ -385,7 +385,6 @@ static int find_process( const wchar_t *proc,
wchar_t *cmd=0; wchar_t *cmd=0;
int sz=0; int sz=0;
int found = 0; int found = 0;
wchar_t *result;
job_t *j; job_t *j;
@ -441,11 +440,8 @@ static int find_process( const wchar_t *proc,
{ {
{ {
result = (wchar_t *)malloc(sizeof(wchar_t)*16 ); wcstring result = format_string(L"%ld", (long)j->pgid);
swprintf( result, 16, L"%d", j->pgid ); out.push_back(completion_t(result));
completion_t data_to_push;
data_to_push.completion = result;
out.push_back( data_to_push);
found = 1; found = 1;
} }
} }
@ -474,11 +470,8 @@ static int find_process( const wchar_t *proc,
} }
else else
{ {
result = (wchar_t *)malloc(sizeof(wchar_t)*16 ); wcstring result = format_string(L"%ld", (long)j->pgid);
swprintf( result, 16, L"%d", j->pgid ); out.push_back(completion_t(result));
completion_t data_to_push;
data_to_push.completion = result;
out.push_back( data_to_push);
found = 1; found = 1;
} }
} }
@ -513,11 +506,8 @@ static int find_process( const wchar_t *proc,
} }
else else
{ {
result = (wchar_t *)malloc(sizeof(wchar_t)*16 ); wcstring result = to_string<int>(p->pid);
swprintf( result, 16, L"%d", p->pid ); out.push_back(completion_t(result));
completion_t data_to_push;
data_to_push.completion = result;
out.push_back( data_to_push );
found = 1; found = 1;
} }
} }
@ -631,12 +621,8 @@ static int find_process( const wchar_t *proc,
} }
else else
{ {
wchar_t *res = wcsdup(name); if (name)
if( res ) { out.push_back(completion_t(name));
completion_t data_to_push;
data_to_push.completion = res;
out.push_back( data_to_push );
}
} }
} }
} }
@ -663,9 +649,7 @@ static int expand_pid( const wcstring &instr,
if( instr.empty() || instr.at(0) != PROCESS_EXPAND ) if( instr.empty() || instr.at(0) != PROCESS_EXPAND )
{ {
completion_t data_to_push; out.push_back(completion_t(instr));
data_to_push.completion = instr;
out.push_back( data_to_push );
return 1; return 1;
} }
@ -692,28 +676,18 @@ static int expand_pid( const wcstring &instr,
{ {
if( wcscmp( (in+1), SELF_STR )==0 ) if( wcscmp( (in+1), SELF_STR )==0 )
{ {
wchar_t str[32];
swprintf( str, 32, L"%d", getpid() ); const wcstring pid_str = to_string<int>(getpid());
out.push_back(completion_t(pid_str));
completion_t data_to_push;
data_to_push.completion = str;
out.push_back( data_to_push );
return 1; return 1;
} }
if( wcscmp( (in+1), LAST_STR )==0 ) if( wcscmp( (in+1), LAST_STR )==0 )
{ {
wchar_t *str;
if( proc_last_bg_pid > 0 ) if( proc_last_bg_pid > 0 )
{ {
str = (wchar_t *)malloc( sizeof(wchar_t)*32); const wcstring pid_str = to_string<int>(proc_last_bg_pid);
swprintf( str, 32, L"%d", proc_last_bg_pid ); out.push_back( completion_t(pid_str));
completion_t data_to_push;
data_to_push.completion = str;
out.push_back( data_to_push);
} }
return 1; 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 Parse an array slicing specification
*/ */
static int parse_slice( const wchar_t *in, wchar_t **end_ptr, array_list_t *idx ) static int parse_slice( const wchar_t *in, wchar_t **end_ptr, std::vector<long> &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<long> &idx )
{ {
@ -1002,7 +929,7 @@ static int expand_variables_internal( parser_t &parser, wchar_t * const in, std:
wchar_t *slice_end; wchar_t *slice_end;
all_vars=0; 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, parser.error( SYNTAX_ERROR,
-1, -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); const wcstring &next = var_item_list.at(j);
if( is_ok && (i == 0) && (!in[stop_pos]) ) if( is_ok && (i == 0) && (!in[stop_pos]) )
{ {
completion_t data_to_push; out.push_back(completion_t(next));
data_to_push.completion = next;
out.push_back( data_to_push );
} }
else else
{ {
@ -1150,9 +1075,7 @@ static int expand_variables_internal( parser_t &parser, wchar_t * const in, std:
if( !empty ) if( !empty )
{ {
completion_t data_to_push; out.push_back(completion_t(in));
data_to_push.completion = in;
out.push_back( data_to_push );
} }
return is_ok; 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 ) if( bracket_begin == 0 )
{ {
completion_t data_to_push; out.push_back(completion_t(in));
data_to_push.completion = in;
out.push_back( data_to_push );
return 1; 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(); const wchar_t * const in = input.c_str();
completion_t data_to_push;
int parse_ret; int parse_ret;
switch( parse_ret = parse_util_locate_cmdsubst(in, switch( parse_ret = parse_util_locate_cmdsubst(in,
&paran_begin, &paran_begin,
&paran_end, &paran_end,
0 ) ) 0 ) )
{ {
case -1: case -1:
parser.error( SYNTAX_ERROR, parser.error( SYNTAX_ERROR,
-1, -1,
L"Mismatched parans" ); L"Mismatched parans" );
return 0; return 0;
case 0: case 0:
data_to_push.completion = input; outList.push_back(completion_t(input));
outList.push_back(data_to_push);
return 1; return 1;
case 1: case 1:
@ -1355,7 +1274,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector
std::vector<long> slice_idx; std::vector<long> slice_idx;
wchar_t *slice_end; 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" ); parser.error( SYNTAX_ERROR, -1, L"Invalid index value" );
return 0; return 0;
@ -1429,9 +1348,7 @@ static int expand_cmdsubst( parser_t &parser, const wcstring &input, std::vector
whole_item.append(tail_item); whole_item.append(tail_item);
//al_push( out, whole_item.buff ); //al_push( out, whole_item.buff );
completion_t data_to_push; outList.push_back(completion_t(whole_item));
data_to_push.completion = whole_item;
outList.push_back(data_to_push);
} }
} }
@ -1600,9 +1517,7 @@ int expand_string2( const wcstring &input, std::vector<completion_t> &output, in
if( (!(flags & ACCEPT_INCOMPLETE)) && expand_is_clean( input.c_str() ) ) if( (!(flags & ACCEPT_INCOMPLETE)) && expand_is_clean( input.c_str() ) )
{ {
completion_t data_to_push; output.push_back(completion_t(input));
data_to_push.completion = input;
output.push_back(data_to_push);
return EXPAND_OK; return EXPAND_OK;
} }
@ -1618,9 +1533,7 @@ int expand_string2( const wcstring &input, std::vector<completion_t> &output, in
parser.error( CMDSUBST_ERROR, -1, L"Command substitutions not allowed" ); parser.error( CMDSUBST_ERROR, -1, L"Command substitutions not allowed" );
return EXPAND_ERROR; return EXPAND_ERROR;
} }
completion_t data_to_push; list1.push_back(completion_t(input));
data_to_push.completion = input;
list1.push_back(data_to_push);
} }
else else
{ {
@ -1649,9 +1562,7 @@ int expand_string2( const wcstring &input, std::vector<completion_t> &output, in
next[i] = L'$'; next[i] = L'$';
} }
} }
completion_t data_to_push; out->push_back(completion_t(next));
data_to_push.completion = next;
out->push_back(data_to_push);
} }
else else
{ {
@ -1702,9 +1613,7 @@ int expand_string2( const wcstring &input, std::vector<completion_t> &output, in
} }
else else
{ {
completion_t data_to_push; out->push_back(completion_t(next));
data_to_push.completion = next;
out->push_back(data_to_push);
} }
} }
else else
@ -1798,9 +1707,7 @@ int expand_string2( const wcstring &input, std::vector<completion_t> &output, in
} }
else else
{ {
completion_t data_to_push; output.push_back(completion_t(next));
data_to_push.completion = next;
output.push_back(data_to_push);
} }
} }

View file

@ -65,7 +65,7 @@
*/ */
#define EXPAND_RESERVED_END 0xf000f #define EXPAND_RESERVED_END 0xf000f
struct completion_t; class completion_t;
enum enum
{ {

View file

@ -1872,9 +1872,7 @@ int parser_t::parse_job( process_t *p,
} }
} }
} }
completion_t data_to_push; args.push_back(completion_t(nxt));
data_to_push.completion = nxt;
args.push_back( data_to_push );
} }
if( error_code == 0 ) if( error_code == 0 )
@ -1922,12 +1920,8 @@ int parser_t::parse_job( process_t *p,
// al_truncate( args, 0 ); // al_truncate( args, 0 );
args.clear(); args.clear();
// al_push( args, halloc_wcsdup( j, L"cd" ) ); // al_push( args, halloc_wcsdup( j, L"cd" ) );
completion_t comp; args.push_back(completion_t(L"cd"));
comp.completion = L"cd"; args.push_back(completion_t(tmp));
args.push_back(comp);
completion_t comp2;
comp2.completion = tmp;
args.push_back( comp2 );
/* /*
If we have defined a wrapper around cd, use it, If we have defined a wrapper around cd, use it,
otherwise use the cd builtin 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); const wcstring sub_block(tok_string( tok ) + current_tokenizer_pos, end_pos - current_tokenizer_pos);
p->type = INTERNAL_BLOCK; p->type = INTERNAL_BLOCK;
completion_t data_to_push; args.at( 0 ) = completion_t(sub_block);
data_to_push.completion = sub_block;
args.at( 0 ) = data_to_push;
tok_set_pos( tok, tok_set_pos( tok,
end_pos ); end_pos );

View file

@ -16,7 +16,7 @@
#include "io.h" #include "io.h"
class parser_t; class parser_t;
struct completion_t; class completion_t;
/** /**
Read commands from \c fd until encountering EOF Read commands from \c fd until encountering EOF

View file

@ -870,8 +870,7 @@ static int wildcard_expand_internal( const wchar_t *wc,
else else
{ {
res = 1; res = 1;
completion_t data_to_push; completion_t data_to_push(base_dir);
data_to_push.completion = base_dir;
if ( std::find( out.begin(), out.end(), data_to_push ) != out.end() ){ if ( std::find( out.begin(), out.end(), data_to_push ) != out.end() ){
out.push_back( data_to_push); out.push_back( data_to_push);
} }
@ -944,9 +943,7 @@ static int wildcard_expand_internal( const wchar_t *wc,
} }
else else
{ {
completion_t data_to_push; out.push_back( completion_t(long_name) );
data_to_push.completion = long_name;
out.push_back( data_to_push );
} }
res = 1; res = 1;
} }

View file

@ -24,7 +24,7 @@
#define WILDCARD_RESERVED 0xf400 #define WILDCARD_RESERVED 0xf400
struct completion_t; class completion_t;
/** /**
Enumeration of all wildcard types Enumeration of all wildcard types
*/ */