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) {
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));
}
}

View file

@ -22,7 +22,7 @@
#include <assert.h>
#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<typename T>
wcstring to_string(const T &x) {
std::wstringstream stream;
stream << x;
return stream.str();
}
class scoped_lock {
pthread_mutex_t *lock_obj;
bool locked;

View file

@ -209,31 +209,9 @@ static void complete_free_entry( complete_entry_t *c );
Create a new completion entry
*/
void completion_allocate( std::vector<completion_t> &context,
const wchar_t *comp,
const wchar_t *desc,
int flags )
void completion_allocate(std::vector<completion_t> &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<completion_t> &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<al_get_count( &names ); i++ )
const wcstring_list_t names = env_get_names(0);
for( size_t i=0; i<names.size(); i++ )
{
wchar_t *name = (wchar_t *)al_get( &names, i );
int namelen = wcslen( name );
const wcstring & env_name = names.at(i);
int namelen = env_name.size();
int match=0, match_no_case=0;
if( varlen > 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;
}

View file

@ -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<completion_t> &context,
const wchar_t *comp,
const wchar_t *desc,
int flags );
void completion_allocate(std::vector<completion_t> &completions, const wcstring &comp, const wcstring &desc, int flags);
#endif

View file

@ -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<int>(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<int>(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<int>(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<long> &idx )
static int parse_slice( 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;
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,
&paran_begin,
&paran_end,
0 ) )
&paran_begin,
&paran_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<long> 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<completion_t> &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<completion_t> &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<completion_t> &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<completion_t> &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<completion_t> &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));
}
}

View file

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

View file

@ -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 );

View file

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

View file

@ -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;
}

View file

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