aboutsummaryrefslogtreecommitdiffhomepage
path: root/complete.c
diff options
context:
space:
mode:
authorGravatar axel <axel@liljencrantz.se>2006-02-20 04:19:32 +1000
committerGravatar axel <axel@liljencrantz.se>2006-02-20 04:19:32 +1000
commit7740035d145ccb78a831007a99ee30254f639dad (patch)
tree1f1f2dbac5dfa0086467749328f2047c3302267e /complete.c
parent7dc39349978c2779d92a527e13096b93211b6344 (diff)
Remove some ancient debug messages, drop two warnings, add some braces and other minor code tweaks
darcs-hash:20060219181932-ac50b-2d032e7a64257a6ca0db078ea9c81ae04a01120f.gz
Diffstat (limited to 'complete.c')
-rw-r--r--complete.c210
1 files changed, 108 insertions, 102 deletions
diff --git a/complete.c b/complete.c
index 6caf5f9a..1e932151 100644
--- a/complete.c
+++ b/complete.c
@@ -325,7 +325,6 @@ void complete_init()
void complete_destroy()
{
complete_entry *i=first_entry, *prev;
- wchar_t *path;
while( i )
{
@@ -382,8 +381,10 @@ void complete_add( const wchar_t *cmd,
if( c == 0 )
{
if( !(c = malloc( sizeof(complete_entry) )))
+ {
die_mem();
-
+ }
+
c->next = first_entry;
first_entry = c;
@@ -396,8 +397,10 @@ void complete_add( const wchar_t *cmd,
/* wprintf( L"Add completion to option (short %lc, long %ls)\n", short_opt, long_opt );*/
if( !(opt = malloc( sizeof( complete_entry_opt ) )))
+ {
die_mem();
-
+ }
+
opt->next = c->first_option;
c->first_option = opt;
c->authorative = authorative;
@@ -410,9 +413,11 @@ void complete_add( const wchar_t *cmd,
wcsncat( c->short_opt_str,
&short_opt, 1 );
if( len == 2 )
+ {
wcscat( c->short_opt_str, L":" );
+ }
}
-
+
opt->short_opt = short_opt;
opt->result_mode = result_mode;
opt->old_mode=old_mode;
@@ -426,7 +431,9 @@ void complete_add( const wchar_t *cmd,
opt->desc = intern( desc );
}
else
+ {
opt->desc = L"";
+ }
}
void complete_remove( const wchar_t *cmd,
@@ -435,6 +442,7 @@ void complete_remove( const wchar_t *cmd,
const wchar_t *long_opt )
{
complete_entry *e, *eprev=0, *enext=0;
+
for( e = first_entry; e; e=enext )
{
enext=e->next;
@@ -461,9 +469,9 @@ void complete_remove( const wchar_t *cmd,
{
wchar_t *pos;
/* fwprintf( stderr,
- L"remove option -%lc --%ls\n",
- o->short_opt?o->short_opt:L' ',
- o->long_opt );
+ L"remove option -%lc --%ls\n",
+ o->short_opt?o->short_opt:L' ',
+ o->long_opt );
*/
if( o->short_opt )
{
@@ -538,10 +546,14 @@ static void parse_cmd_string( const wchar_t *str,
/* Make sure the path is not included in the command */
cmd = wcsrchr( str, L'/' );
if( cmd != 0 )
+ {
cmd++;
+ }
else
+ {
cmd = (wchar_t *)str;
-
+ }
+
*pathp=path;
*cmdp=cmd;
}
@@ -550,24 +562,20 @@ int complete_is_valid_option( const wchar_t *str,
const wchar_t *opt,
array_list_t *errors )
{
-
complete_entry *i;
complete_entry_opt *o;
wchar_t *cmd, *path;
int found_match = 0;
int authorative = 1;
-
int opt_found=0;
-
hash_table_t gnu_match_hash;
-
int is_gnu_opt=0;
int is_old_opt=0;
int is_short_opt=0;
int is_gnu_exact=0;
int gnu_opt_len=0;
-
char *short_validated;
+
/*
Check some generic things like -- and - options.
*/
@@ -575,22 +583,31 @@ int complete_is_valid_option( const wchar_t *str,
{
case 0:
+ {
return 1;
-
-
+ }
+
case 1:
+ {
return opt[0] == L'-';
-
+ }
+
case 2:
+ {
if( wcscmp( L"--", opt ) == 0 )
+ {
return 1;
+ }
+ break;
+ }
}
-
+
if( opt[0] != L'-' )
{
if( errors )
+ {
al_push( errors, wcsdup(L"Option does not begin with a '-'") );
-
+ }
return 0;
}
@@ -610,13 +627,15 @@ int complete_is_valid_option( const wchar_t *str,
{
wchar_t *opt_end = wcschr(opt, L'=' );
if( opt_end )
+ {
gnu_opt_len = (opt_end-opt)-2;
+ }
else
+ {
gnu_opt_len = wcslen(opt)-2;
-// fwprintf( stderr, L"Length %d optend %d\n", gnu_opt_len, opt_end );
-
+ }
}
-
+
parse_cmd_string( str, &path, &cmd );
/*
@@ -648,10 +667,9 @@ int complete_is_valid_option( const wchar_t *str,
{
if( o->old_mode )
continue;
- //fwprintf( stderr, L"Compare \'%ls\' against \'%ls\'\n", &opt[2], o->long_opt );
+
if( wcsncmp( &opt[2], o->long_opt, gnu_opt_len )==0)
{
- //fwprintf( stderr, L"Found gnu match %ls\n", o->long_opt );
hash_put( &gnu_match_hash, o->long_opt, L"" );
if( (wcsncmp( &opt[2],
o->long_opt,
@@ -671,7 +689,6 @@ int complete_is_valid_option( const wchar_t *str,
if( wcscmp( &opt[1], o->long_opt )==0)
{
-// fwprintf( stderr, L"Found old match %ls\n", o->long_opt );
opt_found = 1;
is_old_opt = 1;
break;
@@ -701,22 +718,16 @@ int complete_is_valid_option( const wchar_t *str,
nopt[1]=opt[1];
nopt[2]=L'\0';
- //fwprintf( stderr, L"Pos %d, shortopt %lc has argument\n", a-opt, *a );
short_validated[a-opt] =
complete_is_valid_argument( str, nopt, &opt[2]);
}
else
{
- // fwprintf( stderr, L"Pos %d, shortopt %lc is ok\n", a-opt, *a );
short_validated[a-opt]=1;
}
-
}
}
-
}
-
-
}
free( path );
@@ -736,7 +747,6 @@ int complete_is_valid_option( const wchar_t *str,
{
if ( !short_validated[j])
{
- // fwprintf( stderr, L"Pos %d, shortopt %lc is not ok\n", j, opt[j] );
if( errors )
{
wchar_t str[2];
@@ -755,7 +765,6 @@ int complete_is_valid_option( const wchar_t *str,
if( is_gnu_opt )
{
-// fwprintf( stderr, L"Found %d matches\n", hash_get_count( &gnu_match_hash ) );
opt_found = is_gnu_exact || (hash_get_count( &gnu_match_hash )==1);
if( errors && !opt_found )
{
@@ -792,11 +801,14 @@ int complete_is_valid_argument( const wchar_t *str,
static const wchar_t *complete_get_desc_suffix( const wchar_t *suff_orig )
{
- int len = wcslen(suff_orig );
-
+ int len;
wchar_t *suff;
wchar_t *pos;
+ wchar_t *tmp;
+ wchar_t *desc;
+ len = wcslen(suff_orig );
+
if( len == 0 )
return COMPLETE_FILE_DESC;
@@ -819,20 +831,17 @@ static const wchar_t *complete_get_desc_suffix( const wchar_t *suff_orig )
}
}
- wchar_t *tmp = escape( suff, 1 );
+ tmp = escape( suff, 1 );
free(suff);
suff = tmp;
-
- wchar_t *desc = (wchar_t *)hash_get( suffix_hash, suff );
+ desc = (wchar_t *)hash_get( suffix_hash, suff );
if( !desc )
{
-
wchar_t *cmd = wcsdupcat( SUFFIX_CMD_STR, suff );
if( cmd )
{
-
array_list_t l;
al_init( &l );
@@ -1000,7 +1009,7 @@ static void copy_strings_with_prefix( array_list_t *comp_out,
wchar_t *wc, *tmp;
tmp = expand_one( 0,
- wcsdup(wc_escaped), EXPAND_SKIP_SUBSHELL | EXPAND_SKIP_WILDCARDS);
+ wcsdup(wc_escaped), EXPAND_SKIP_SUBSHELL | EXPAND_SKIP_WILDCARDS);
if(!tmp)
return;
@@ -1012,12 +1021,9 @@ static void copy_strings_with_prefix( array_list_t *comp_out,
wc = parse_util_unescape_wildcards( tmp );
free(tmp);
-
-// int str_len = wcslen( str );
for( i=0; i<al_get_count( possible_comp ); i++ )
{
wchar_t *next_str = (wchar_t *)al_get( possible_comp, i );
-// fwprintf( stderr, L"try wc %ls against %ls\n", wc, next_str );
wildcard_complete( next_str, wc, desc, desc_func, comp_out );
}
@@ -1262,59 +1268,59 @@ static void complete_cmd( const wchar_t *cmd,
}
-// fwprintf( stderr, L"cdpath %ls\n", cdpath_cpy );
/*
Tab complete implicit cd for directories in CDPATH
*/
-
if( cmd[0] != L'/' && ( wcsncmp( cmd, L"./", 2 )!=0) )
{
- for( nxt_path = wcstok( cdpath_cpy, ARRAY_SEP_STR, &state );
- nxt_path != 0;
- nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
- {
- int i;
- array_list_t tmp;
- wchar_t *nxt_completion=
- wcsdupcat2( nxt_path,
- (nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"),
- cmd,
- 0 );
- if( ! nxt_completion )
- continue;
-
- al_init( &tmp );
-
- if( expand_string( 0,
- nxt_completion,
- &tmp,
- ACCEPT_INCOMPLETE | DIRECTORIES_ONLY ) != EXPAND_ERROR )
+ for( nxt_path = wcstok( cdpath_cpy, ARRAY_SEP_STR, &state );
+ nxt_path != 0;
+ nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
{
+ int i;
+ array_list_t tmp;
+ wchar_t *nxt_completion=
+ wcsdupcat2( nxt_path,
+ (nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"),
+ cmd,
+ 0 );
+ if( ! nxt_completion )
+ {
+ continue;
+ }
+
+ al_init( &tmp );
- for( i=0; i<al_get_count(&tmp); i++ )
+ if( expand_string( 0,
+ nxt_completion,
+ &tmp,
+ ACCEPT_INCOMPLETE | DIRECTORIES_ONLY ) != EXPAND_ERROR )
{
- wchar_t *nxt = (wchar_t *)al_get( &tmp, i );
- wchar_t *desc = wcsrchr( nxt, COMPLETE_SEP );
- if( desc )
+ for( i=0; i<al_get_count(&tmp); i++ )
{
- int is_valid = desc && (wcscmp(desc+1,
- COMPLETE_DIRECTORY_DESC)==0);
- if( is_valid )
- {
- al_push( comp, nxt );
- }
- else
+ wchar_t *nxt = (wchar_t *)al_get( &tmp, i );
+
+ wchar_t *desc = wcsrchr( nxt, COMPLETE_SEP );
+ if( desc )
{
- free(nxt);
+ int is_valid = desc && (wcscmp(desc+1,
+ COMPLETE_DIRECTORY_DESC)==0);
+ if( is_valid )
+ {
+ al_push( comp, nxt );
+ }
+ else
+ {
+ free(nxt);
+ }
}
}
}
- }
- al_destroy( &tmp );
- }
+ al_destroy( &tmp );
+ }
}
@@ -1493,8 +1499,6 @@ static int complete_param( wchar_t *cmd_orig,
if( ( (!wildcard_match( match, i->cmd ) ) ) )
continue;
-/* wprintf( L"Found matching command %ls\n", i->cmd ); */
-
use_common=1;
if( str[0] == L'-' )
{
@@ -1505,8 +1509,6 @@ static int complete_param( wchar_t *cmd_orig,
wchar_t *arg;
if( (arg=param_match2( o, str ))!=0 && condition_test( o->condition ))
{
-
-/* wprintf( L"Use option with desc %ls\n", o->desc ); */
use_common &= ((o->result_mode & NO_COMMON )==0);
use_files &= ((o->result_mode & NO_FILES )==0);
complete_from_args( arg, o->comp, o->desc, comp_out );
@@ -1583,7 +1585,6 @@ static int complete_param( wchar_t *cmd_orig,
if( (o->short_opt == L'\0' ) && (o->long_opt[0]==L'\0'))
{
use_files &= ((o->result_mode & NO_FILES )==0);
-// debug( 0, L"Running argument command %ls", o->comp );
complete_from_args( str, o->comp, o->desc, comp_out );
}
@@ -1629,21 +1630,23 @@ static int complete_param( wchar_t *cmd_orig,
if( o->old_mode || !(o->result_mode & NO_COMMON ) )
{
al_push( comp_out,
- wcsdupcat2(&((wchar_t *)whole_opt.buff)[wcslen(str)], COMPLETE_SEP_STR, o->desc, (void *)0) );
-// fwprintf( stderr, L"Add without param %ls\n", o->long_opt );
+ wcsdupcat2( &((wchar_t *)whole_opt.buff)[wcslen(str)],
+ COMPLETE_SEP_STR,
+ o->desc,
+ (void *)0) );
}
if( !o->old_mode && ( wcslen(o->comp) || (o->result_mode & NO_COMMON ) ) )
{
al_push( comp_out,
- wcsdupcat2(&((wchar_t *)whole_opt.buff)[wcslen(str)], L"=", COMPLETE_SEP_STR, o->desc, (void *)0) );
-// fwprintf( stderr, L"Add with param %ls\n", o->long_opt );
+ wcsdupcat2( &((wchar_t *)whole_opt.buff)[wcslen(str)],
+ L"=",
+ COMPLETE_SEP_STR,
+ o->desc,
+ (void *)0) );
}
-
-// fwprintf( stderr, L"Matching long option %ls\n", o->long_opt );
}
sb_destroy( &whole_opt );
-
}
}
}
@@ -1661,13 +1664,16 @@ static void complete_param_expand( wchar_t *str,
int do_file )
{
wchar_t *comp_str;
+
if( (wcsncmp( str, L"--", 2 )) == 0 && (comp_str = wcschr(str, L'=' ) ) )
{
comp_str++;
}
else
+ {
comp_str = str;
-
+ }
+
debug( 2,
L"expand_string( \"%ls\", comp_out, EXPAND_SKIP_SUBSHELL | ACCEPT_INCOMPLETE | %ls );",
comp_str,
@@ -1692,13 +1698,12 @@ static int complete_variable( const wchar_t *var,
array_list_t names;
al_init( &names );
env_get_names( &names, 0 );
-/* wprintf( L"Search string %ls\n", var );*/
-/* wprintf( L"Got %d variables\n", al_get_count( &names ) );*/
for( i=0; i<al_get_count( &names ); i++ )
{
wchar_t *name = (wchar_t *)al_get( &names, i );
int namelen = wcslen( name );
+
if( varlen > namelen )
continue;
@@ -1726,7 +1731,10 @@ static int complete_variable( const wchar_t *var,
}
/**
- Search the specified string for the \$ sign, try to complete as an environment variable
+ Search the specified string for the \$ sign. If found, try to
+ complete as an environment variable.
+
+ \return 0 if unable to complete, 1 otherwise
*/
static int try_complete_variable( const wchar_t *cmd,
array_list_t *comp )
@@ -1752,9 +1760,11 @@ static int try_complete_variable( const wchar_t *cmd,
}
/**
- Try to complete the specified string as a username. This is used by ~USER type expansion.
-*/
+ Try to complete the specified string as a username. This is used by
+ ~USER type expansion.
+ \return 0 if unable to complete, 1 otherwise
+*/
static int try_complete_user( const wchar_t *cmd,
array_list_t *comp )
{
@@ -1763,7 +1773,6 @@ static int try_complete_user( const wchar_t *cmd,
int mode = 0;
int res = 0;
-
for( p=cmd; *p; p++ )
{
switch( mode )
@@ -1822,13 +1831,10 @@ static int try_complete_user( const wchar_t *cmd,
struct passwd *pw;
int name_len = wcslen( user_name );
-/* wprintf( L"Complete name \'%ls\'\n", user_name );*/
-
setpwent();
while((pw=getpwent()) != 0)
{
-/* wprintf( L"Try %ls\n", pw->pw_name );*/
wchar_t *pw_name = str2wcs( pw->pw_name );
if( pw_name )
{