2006-11-29 22:21:02 +08:00
|
|
|
/** \file complete.c Functions related to tab-completion.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
These functions are used for storing and retrieving tab-completion data, as well as for performing tab-completion.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2006-08-11 09:18:35 +08:00
|
|
|
#include "config.h"
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <wctype.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
2006-02-08 23:29:09 +08:00
|
|
|
#include <wchar.h>
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-02-28 21:17:16 +08:00
|
|
|
|
|
|
|
#include "fallback.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "util.h"
|
2006-02-28 21:17:16 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "tokenizer.h"
|
|
|
|
#include "wildcard.h"
|
|
|
|
#include "proc.h"
|
|
|
|
#include "parser.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "complete.h"
|
|
|
|
#include "builtin.h"
|
|
|
|
#include "env.h"
|
|
|
|
#include "exec.h"
|
|
|
|
#include "expand.h"
|
|
|
|
#include "common.h"
|
|
|
|
#include "reader.h"
|
|
|
|
#include "history.h"
|
|
|
|
#include "intern.h"
|
2006-01-31 00:51:50 +08:00
|
|
|
#include "parse_util.h"
|
2007-04-22 17:50:26 +08:00
|
|
|
#include "parser_keywords.h"
|
2006-06-12 22:12:33 +08:00
|
|
|
#include "halloc.h"
|
2006-02-09 23:50:20 +08:00
|
|
|
#include "halloc_util.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
#include "wutil.h"
|
2006-10-19 19:50:23 +08:00
|
|
|
#include "path.h"
|
2012-01-24 03:42:41 +08:00
|
|
|
#include "builtin_scripts.h"
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/*
|
2006-02-04 05:30:06 +08:00
|
|
|
Completion description strings, mostly for different types of files, such as sockets, block devices, etc.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-02-04 05:30:06 +08:00
|
|
|
There are a few more completion description strings defined in
|
|
|
|
expand.c. Maybe all completion description strings should be defined
|
|
|
|
in the same file?
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
Description for ~USER completion
|
|
|
|
*/
|
2008-01-14 03:32:21 +08:00
|
|
|
#define COMPLETE_USER_DESC _( L"Home for %ls" )
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Description for short variables. The value is concatenated to this description
|
|
|
|
*/
|
2007-02-17 19:05:55 +08:00
|
|
|
#define COMPLETE_VAR_DESC_VAL _( L"Variable: %ls" )
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
The maximum number of commands on which to perform description
|
|
|
|
lookup. The lookup process is quite time consuming, so this should
|
|
|
|
be set to a pretty low number.
|
|
|
|
*/
|
|
|
|
#define MAX_CMD_DESC_LOOKUP 10
|
|
|
|
|
|
|
|
/**
|
|
|
|
Condition cache value returned from hashtable when this condition
|
2006-02-04 05:30:06 +08:00
|
|
|
has not yet been tested. This value is NULL, so that when the hash
|
|
|
|
table returns NULL, this wil be seen as an untested condition.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
#define CC_NOT_TESTED 0
|
|
|
|
|
|
|
|
/**
|
2006-02-04 05:30:06 +08:00
|
|
|
Condition cache value returned from hashtable when the condition is
|
|
|
|
met. This can be any value, that is a valid pointer, and that is
|
|
|
|
different from CC_NOT_TESTED and CC_FALSE.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
#define CC_TRUE L"true"
|
|
|
|
|
|
|
|
/**
|
2006-02-04 05:30:06 +08:00
|
|
|
Condition cache value returned from hashtable when the condition is
|
|
|
|
not met. This can be any value, that is a valid pointer, and that
|
|
|
|
is different from CC_NOT_TESTED and CC_TRUE.
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
#define CC_FALSE L"false"
|
|
|
|
|
2006-06-08 07:56:01 +08:00
|
|
|
/**
|
|
|
|
The special cased translation macro for completions. The empty
|
|
|
|
string needs to be special cased, since it can occur, and should
|
|
|
|
not be translated. (Gettext returns the version information as the
|
|
|
|
response)
|
|
|
|
*/
|
2006-11-29 22:18:22 +08:00
|
|
|
#ifdef USE_GETTEXT
|
2007-10-14 05:26:06 +08:00
|
|
|
#define C_(wstr) ((wcscmp(wstr, L"")==0)?L"":wgettext(wstr))
|
2006-11-29 22:18:22 +08:00
|
|
|
#else
|
|
|
|
#define C_(string) (string)
|
|
|
|
#endif
|
|
|
|
|
2006-06-08 07:56:01 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
/**
|
|
|
|
The maximum amount of time that we're willing to spend doing
|
|
|
|
username tilde completion. This special limit has been coded in
|
|
|
|
because user lookup can be extremely slow in cases of a humongous
|
|
|
|
LDAP database. (Google, I'm looking at you)
|
|
|
|
*/
|
|
|
|
#define MAX_USER_LOOKUP_TIME 0.2
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
2006-01-31 03:53:10 +08:00
|
|
|
Struct describing a completion option entry.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
If short_opt and long_opt are both zero, the comp field must not be
|
|
|
|
empty and contains a list of arguments to the command.
|
|
|
|
|
|
|
|
If either short_opt or long_opt are non-zero, they specify a switch
|
|
|
|
for the command. If \c comp is also not empty, it contains a list
|
2006-02-04 05:30:06 +08:00
|
|
|
of non-switch arguments that may only follow directly after the
|
2011-12-27 11:18:46 +08:00
|
|
|
specified switch.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
typedef struct complete_entry_opt
|
|
|
|
{
|
|
|
|
/** Short style option */
|
|
|
|
wchar_t short_opt;
|
|
|
|
/** Long style option */
|
|
|
|
const wchar_t *long_opt;
|
|
|
|
/** Arguments to the option */
|
|
|
|
const wchar_t *comp;
|
|
|
|
/** Description of the completion */
|
|
|
|
const wchar_t *desc;
|
|
|
|
/** Condition under which to use the option */
|
|
|
|
const wchar_t *condition;
|
2006-02-04 05:30:06 +08:00
|
|
|
/** Must be one of the values SHARED, NO_FILES, NO_COMMON,
|
|
|
|
EXCLUSIVE, and determines how completions should be performed
|
|
|
|
on the argument after the switch. */
|
2005-09-20 21:26:39 +08:00
|
|
|
int result_mode;
|
|
|
|
/** True if old style long options are used */
|
|
|
|
int old_mode;
|
2006-02-04 05:30:06 +08:00
|
|
|
/** Next option in the linked list */
|
2005-09-20 21:26:39 +08:00
|
|
|
struct complete_entry_opt *next;
|
2007-03-01 05:43:27 +08:00
|
|
|
/** Completion flags */
|
|
|
|
int flags;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-10-24 19:03:52 +08:00
|
|
|
complete_entry_opt_t;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Struct describing a command completion
|
|
|
|
*/
|
2012-02-09 06:47:50 +08:00
|
|
|
struct completion_entry_t
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
/** True if command is a path */
|
|
|
|
int cmd_type;
|
|
|
|
|
|
|
|
/** Command string */
|
2012-02-09 06:47:50 +08:00
|
|
|
wcstring cmd;
|
2005-09-20 21:26:39 +08:00
|
|
|
/** String containing all short option characters */
|
2012-02-09 06:47:50 +08:00
|
|
|
wcstring short_opt_str;
|
2005-09-20 21:26:39 +08:00
|
|
|
/** Linked list of all options */
|
2006-10-24 19:03:52 +08:00
|
|
|
complete_entry_opt_t *first_option;
|
2005-09-20 21:26:39 +08:00
|
|
|
/** True if no other options than the ones supplied are possible */
|
2007-08-02 01:35:24 +08:00
|
|
|
int authoritative;
|
2012-02-09 06:47:50 +08:00
|
|
|
};
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
/** Linked list of all completion entries */
|
|
|
|
typedef std::list<completion_entry_t *> completion_entry_list_t;
|
|
|
|
static completion_entry_list_t completion_entries;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Table of completions conditions that have already been tested and
|
|
|
|
the corresponding test results
|
|
|
|
*/
|
|
|
|
static hash_table_t *condition_cache=0;
|
|
|
|
|
2012-01-26 10:40:08 +08:00
|
|
|
/* Autoloader for completions */
|
|
|
|
class completion_autoload_t : public autoload_t {
|
|
|
|
public:
|
|
|
|
completion_autoload_t();
|
|
|
|
virtual void command_removed(const wcstring &cmd);
|
|
|
|
};
|
|
|
|
|
|
|
|
static completion_autoload_t completion_autoloader;
|
|
|
|
|
|
|
|
/** Constructor */
|
|
|
|
completion_autoload_t::completion_autoload_t() : autoload_t(L"fish_complete_path",
|
|
|
|
internal_completion_scripts,
|
|
|
|
sizeof internal_completion_scripts / sizeof *internal_completion_scripts)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Callback when an autoloaded completion is removed */
|
|
|
|
void completion_autoload_t::command_removed(const wcstring &cmd) {
|
|
|
|
complete_remove( cmd.c_str(), COMMAND, 0, 0 );
|
|
|
|
}
|
|
|
|
|
2012-01-24 03:42:41 +08:00
|
|
|
|
2007-02-17 19:05:55 +08:00
|
|
|
/**
|
|
|
|
Create a new completion entry
|
|
|
|
|
|
|
|
*/
|
2012-02-02 08:27:14 +08:00
|
|
|
void completion_allocate(std::vector<completion_t> &completions, const wcstring &comp, const wcstring &desc, int flags)
|
2007-02-17 19:05:55 +08:00
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
completions.push_back(completion_t(comp, desc, flags));
|
2007-02-17 19:05:55 +08:00
|
|
|
}
|
|
|
|
|
2006-07-22 18:16:51 +08:00
|
|
|
/**
|
2012-02-08 13:23:12 +08:00
|
|
|
The init function for the completion code. Does nothing.
|
2006-07-22 18:16:51 +08:00
|
|
|
*/
|
|
|
|
static void complete_init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
This command clears the cache of condition tests created by \c condition_test().
|
|
|
|
*/
|
|
|
|
static void condition_cache_clear()
|
|
|
|
{
|
|
|
|
if( condition_cache )
|
|
|
|
{
|
|
|
|
hash_destroy( condition_cache );
|
2006-01-31 03:53:10 +08:00
|
|
|
free( condition_cache );
|
2005-09-20 21:26:39 +08:00
|
|
|
condition_cache = 0;
|
2006-01-31 03:53:10 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test if the specified script returns zero. The result is cached, so
|
|
|
|
that if multiple completions use the same condition, it needs only
|
|
|
|
be evaluated once. condition_cache_clear must be called after a
|
|
|
|
completion run to make sure that there are no stale completions.
|
|
|
|
*/
|
|
|
|
static int condition_test( const wchar_t *condition )
|
|
|
|
{
|
|
|
|
const void *test_res;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !condition || !wcslen(condition) )
|
|
|
|
{
|
|
|
|
// fwprintf( stderr, L"No condition specified\n" );
|
|
|
|
return 1;
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !condition_cache )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
condition_cache = (hash_table_t *)malloc( sizeof( hash_table_t ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !condition_cache )
|
|
|
|
{
|
2006-07-03 18:39:57 +08:00
|
|
|
DIE_MEM();
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
|
|
|
hash_init( condition_cache,
|
2005-09-20 21:26:39 +08:00
|
|
|
&hash_wcs_func,
|
|
|
|
&hash_wcs_cmp );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
test_res = hash_get( condition_cache, condition );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if (test_res == CC_NOT_TESTED )
|
|
|
|
{
|
2012-02-08 15:35:41 +08:00
|
|
|
test_res = exec_subshell( condition)?CC_FALSE:CC_TRUE;
|
2005-09-20 21:26:39 +08:00
|
|
|
hash_put( condition_cache, condition, test_res );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
|
|
|
Restore previous status information
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
if( wcscmp( (const wchar_t *)test_res, CC_TRUE ) == 0 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-01-31 03:53:10 +08:00
|
|
|
return 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2006-10-24 19:03:52 +08:00
|
|
|
Recursively free all complete_entry_opt_t structs and their contents
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2006-10-24 19:03:52 +08:00
|
|
|
static void complete_free_opt_recursive( complete_entry_opt_t *o )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-01-28 11:24:16 +08:00
|
|
|
if( !o )
|
|
|
|
return;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-28 11:24:16 +08:00
|
|
|
complete_free_opt_recursive( o->next );
|
2006-11-16 21:08:33 +08:00
|
|
|
halloc_free( o );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Search for an exactly matching completion entry
|
|
|
|
*/
|
2012-02-09 06:47:50 +08:00
|
|
|
static completion_entry_t *complete_find_exact_entry( const wchar_t *cmd,
|
2006-11-29 22:21:02 +08:00
|
|
|
const int cmd_type )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
for (completion_entry_list_t::iterator iter = completion_entries.begin(); iter != completion_entries.end(); iter++)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
completion_entry_t *entry = *iter;
|
|
|
|
if (entry->cmd == cmd && cmd_type == entry->cmd_type)
|
|
|
|
return entry;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2012-02-09 06:47:50 +08:00
|
|
|
return NULL;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
/**
|
|
|
|
Locate the specified entry. Create it if it doesn't exist.
|
|
|
|
*/
|
2012-02-09 06:47:50 +08:00
|
|
|
static completion_entry_t *complete_get_exact_entry( const wchar_t *cmd,
|
2007-01-28 21:40:59 +08:00
|
|
|
int cmd_type )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
completion_entry_t *c;
|
2006-10-19 19:50:23 +08:00
|
|
|
|
2006-07-22 18:16:51 +08:00
|
|
|
complete_init();
|
|
|
|
|
2006-06-08 08:01:45 +08:00
|
|
|
c = complete_find_exact_entry( cmd, cmd_type );
|
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
if( c == NULL )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
c = new completion_entry_t();
|
|
|
|
completion_entries.push_front(c);
|
2005-09-20 21:26:39 +08:00
|
|
|
c->first_option = 0;
|
2012-02-09 06:47:50 +08:00
|
|
|
c->cmd = cmd;
|
2005-09-20 21:26:39 +08:00
|
|
|
c->cmd_type = cmd_type;
|
2012-02-09 06:47:50 +08:00
|
|
|
c->short_opt_str = L"";
|
2007-08-02 01:35:24 +08:00
|
|
|
c->authoritative = 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-08-02 01:35:24 +08:00
|
|
|
void complete_set_authoritative( const wchar_t *cmd,
|
2009-02-03 06:46:45 +08:00
|
|
|
int cmd_type,
|
|
|
|
int authoritative )
|
2007-01-28 21:40:59 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
completion_entry_t *c;
|
2007-01-28 21:40:59 +08:00
|
|
|
|
|
|
|
CHECK( cmd, );
|
|
|
|
c = complete_get_exact_entry( cmd, cmd_type );
|
2007-08-02 01:35:24 +08:00
|
|
|
c->authoritative = authoritative;
|
2007-01-28 21:40:59 +08:00
|
|
|
}
|
2007-01-28 11:24:16 +08:00
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
|
|
|
|
void complete_add( const wchar_t *cmd,
|
|
|
|
int cmd_type,
|
|
|
|
wchar_t short_opt,
|
|
|
|
const wchar_t *long_opt,
|
|
|
|
int old_mode,
|
|
|
|
int result_mode,
|
|
|
|
const wchar_t *condition,
|
|
|
|
const wchar_t *comp,
|
2007-03-01 05:43:27 +08:00
|
|
|
const wchar_t *desc,
|
|
|
|
int flags )
|
2007-01-28 21:40:59 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
completion_entry_t *c;
|
2007-01-28 21:40:59 +08:00
|
|
|
complete_entry_opt_t *opt;
|
|
|
|
|
|
|
|
CHECK( cmd, );
|
|
|
|
|
|
|
|
c = complete_get_exact_entry( cmd, cmd_type );
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
opt = (complete_entry_opt_t *)halloc( 0, sizeof( complete_entry_opt_t ) );
|
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
opt->next = c->first_option;
|
|
|
|
c->first_option = opt;
|
|
|
|
if( short_opt != L'\0' )
|
|
|
|
{
|
|
|
|
int len = 1 + ((result_mode & NO_COMMON) != 0);
|
2012-02-09 06:47:50 +08:00
|
|
|
|
|
|
|
c->short_opt_str.push_back(short_opt);
|
2007-01-28 21:40:59 +08:00
|
|
|
if( len == 2 )
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
c->short_opt_str.push_back(L':');
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2007-01-28 21:40:59 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
opt->short_opt = short_opt;
|
|
|
|
opt->result_mode = result_mode;
|
|
|
|
opt->old_mode=old_mode;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
opt->comp = comp?halloc_wcsdup(opt, comp):L"";
|
|
|
|
opt->condition = condition?halloc_wcsdup(opt, condition):L"";
|
|
|
|
opt->long_opt = long_opt?halloc_wcsdup(opt, long_opt):L"" ;
|
2007-03-01 05:43:27 +08:00
|
|
|
opt->flags = flags;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-28 21:40:59 +08:00
|
|
|
if( desc && wcslen( desc ) )
|
|
|
|
{
|
|
|
|
opt->desc = halloc_wcsdup( opt, desc );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
opt->desc = L"";
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-12-14 07:58:38 +08:00
|
|
|
/**
|
|
|
|
Remove all completion options in the specified entry that match the
|
2012-02-09 06:47:50 +08:00
|
|
|
specified short / long option strings. Returns true if it is now
|
|
|
|
empty and should be deleted, false if it's not empty.
|
2006-12-14 07:58:38 +08:00
|
|
|
*/
|
2012-02-09 06:47:50 +08:00
|
|
|
static bool complete_remove_entry( completion_entry_t *e, wchar_t short_opt, const wchar_t *long_opt )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-02-20 02:19:32 +08:00
|
|
|
|
2006-12-14 07:58:38 +08:00
|
|
|
complete_entry_opt_t *o, *oprev=0, *onext=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 07:58:38 +08:00
|
|
|
if(( short_opt == 0 ) && (long_opt == 0 ) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-12-14 07:58:38 +08:00
|
|
|
complete_free_opt_recursive( e->first_option );
|
|
|
|
e->first_option=0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 07:58:38 +08:00
|
|
|
for( o= e->first_option; o; o=onext )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-12-14 07:58:38 +08:00
|
|
|
onext=o->next;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 07:58:38 +08:00
|
|
|
if( ( short_opt==o->short_opt ) ||
|
|
|
|
( wcscmp( long_opt, o->long_opt ) == 0 ) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-12-14 07:58:38 +08:00
|
|
|
/* fwprintf( stderr,
|
2009-02-03 06:46:45 +08:00
|
|
|
L"remove option -%lc --%ls\n",
|
|
|
|
o->short_opt?o->short_opt:L' ',
|
|
|
|
o->long_opt );
|
2006-12-14 07:58:38 +08:00
|
|
|
*/
|
|
|
|
if( o->short_opt )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
size_t idx = e->short_opt_str.find(o->short_opt);
|
|
|
|
if (idx != wcstring::npos)
|
|
|
|
{
|
|
|
|
/* Consume all colons */
|
|
|
|
size_t first_non_colon = idx + 1;
|
|
|
|
while (first_non_colon < e->short_opt_str.size() && e->short_opt_str.at(first_non_colon) == L':')
|
|
|
|
first_non_colon++;
|
|
|
|
e->short_opt_str.erase(idx, first_non_colon - idx);
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 07:58:38 +08:00
|
|
|
if( oprev == 0 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-12-14 07:58:38 +08:00
|
|
|
e->first_option = o->next;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-14 07:58:38 +08:00
|
|
|
oprev->next = o->next;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-12-14 07:58:38 +08:00
|
|
|
free( o );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-12-14 07:58:38 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
oprev = o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-09 06:47:50 +08:00
|
|
|
|
|
|
|
return e->first_option == 0;
|
2006-12-14 07:58:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void complete_remove( const wchar_t *cmd,
|
|
|
|
int cmd_type,
|
|
|
|
wchar_t short_opt,
|
|
|
|
const wchar_t *long_opt )
|
|
|
|
{
|
|
|
|
CHECK( cmd, );
|
2012-02-09 06:47:50 +08:00
|
|
|
for (completion_entry_list_t::iterator iter = completion_entries.begin(); iter != completion_entries.end(); ) {
|
|
|
|
completion_entry_t *e = *iter;
|
|
|
|
bool delete_it = false;
|
|
|
|
if(cmd_type == e->cmd_type && cmd == e->cmd) {
|
|
|
|
delete_it = complete_remove_entry( e, short_opt, long_opt );
|
|
|
|
}
|
|
|
|
|
|
|
|
if (delete_it) {
|
|
|
|
/* Delete this entry */
|
|
|
|
iter = completion_entries.erase(iter);
|
|
|
|
delete e;
|
|
|
|
} else {
|
|
|
|
/* Don't delete it */
|
|
|
|
iter++;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-08 14:44:10 +08:00
|
|
|
/**
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
int complete_is_valid_option( const wchar_t *str,
|
|
|
|
const wchar_t *opt,
|
2012-01-16 06:24:58 +08:00
|
|
|
array_list_t *errors,
|
|
|
|
bool allow_autoload )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-10-24 19:03:52 +08:00
|
|
|
complete_entry_opt_t *o;
|
2012-02-08 14:44:10 +08:00
|
|
|
wcstring cmd, path;
|
2005-09-20 21:26:39 +08:00
|
|
|
int found_match = 0;
|
2007-08-02 01:35:24 +08:00
|
|
|
int authoritative = 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
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;
|
2012-02-08 14:44:10 +08:00
|
|
|
|
|
|
|
std::vector<char> short_validated;
|
2006-02-20 02:19:32 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-06-21 08:48:36 +08:00
|
|
|
CHECK( str, 0 );
|
|
|
|
CHECK( opt, 0 );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
|
|
|
Check some generic things like -- and - options.
|
|
|
|
*/
|
|
|
|
switch( wcslen(opt ) )
|
|
|
|
{
|
|
|
|
|
|
|
|
case 0:
|
|
|
|
case 1:
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2007-03-24 19:14:55 +08:00
|
|
|
return 1;
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case 2:
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
if( wcscmp( L"--", opt ) == 0 )
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
return 1;
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( opt[0] != L'-' )
|
|
|
|
{
|
|
|
|
if( errors )
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
al_push( errors, wcsdup(L"Option does not begin with a '-'") );
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-08 14:44:10 +08:00
|
|
|
short_validated.resize(wcslen(opt), 0);
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
hash_init( &gnu_match_hash,
|
|
|
|
&hash_wcs_func,
|
|
|
|
&hash_wcs_cmp );
|
|
|
|
|
|
|
|
is_gnu_opt = opt[1]==L'-';
|
|
|
|
if( is_gnu_opt )
|
|
|
|
{
|
2012-01-06 05:58:48 +08:00
|
|
|
const wchar_t *opt_end = wcschr(opt, L'=' );
|
2005-09-20 21:26:39 +08:00
|
|
|
if( opt_end )
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
gnu_opt_len = (opt_end-opt)-2;
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
else
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
gnu_opt_len = wcslen(opt)-2;
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-08 14:44:10 +08:00
|
|
|
parse_cmd_string( str, path, cmd );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Make sure completions are loaded for the specified command
|
|
|
|
*/
|
2012-02-08 14:44:10 +08:00
|
|
|
if (allow_autoload) complete_load( cmd, false );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
for (completion_entry_list_t::iterator iter = completion_entries.begin(); iter != completion_entries.end(); iter++)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
const completion_entry_t *i = *iter;
|
2012-02-08 14:44:10 +08:00
|
|
|
const wcstring &match = i->cmd_type?path:cmd;
|
2005-09-20 21:26:39 +08:00
|
|
|
const wchar_t *a;
|
|
|
|
|
|
|
|
if( !wildcard_match( match, i->cmd ) )
|
2006-02-22 23:41:52 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
continue;
|
2006-02-22 23:41:52 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
found_match = 1;
|
|
|
|
|
2007-08-02 01:35:24 +08:00
|
|
|
if( !i->authoritative )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-08-02 01:35:24 +08:00
|
|
|
authoritative = 0;
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if( is_gnu_opt )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( o = i->first_option; o; o=o->next )
|
|
|
|
{
|
|
|
|
if( o->old_mode )
|
2006-02-22 23:41:52 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
continue;
|
2006-02-22 23:41:52 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( wcsncmp( &opt[2], o->long_opt, gnu_opt_len )==0)
|
|
|
|
{
|
|
|
|
hash_put( &gnu_match_hash, o->long_opt, L"" );
|
|
|
|
if( (wcsncmp( &opt[2],
|
|
|
|
o->long_opt,
|
|
|
|
wcslen( o->long_opt) )==0) )
|
2006-02-22 23:41:52 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
is_gnu_exact=1;
|
2006-02-22 23:41:52 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check for old style options */
|
|
|
|
for( o = i->first_option; o; o=o->next )
|
|
|
|
{
|
|
|
|
if( !o->old_mode )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
if( wcscmp( &opt[1], o->long_opt )==0)
|
|
|
|
{
|
|
|
|
opt_found = 1;
|
|
|
|
is_old_opt = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_old_opt )
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
for( a = &opt[1]; *a; a++ )
|
|
|
|
{
|
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
//PCA Rewrite this
|
|
|
|
wchar_t *str_pos = wcschr(i->short_opt_str.c_str(), *a);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
if (str_pos )
|
|
|
|
{
|
|
|
|
if( *(str_pos +1)==L':' )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This is a short option with an embedded argument,
|
|
|
|
call complete_is_valid_argument on the argument.
|
|
|
|
*/
|
|
|
|
wchar_t nopt[3];
|
|
|
|
nopt[0]=L'-';
|
|
|
|
nopt[1]=opt[1];
|
|
|
|
nopt[2]=L'\0';
|
|
|
|
|
2012-02-08 14:44:10 +08:00
|
|
|
short_validated.at(a-opt) =
|
2005-09-20 21:26:39 +08:00
|
|
|
complete_is_valid_argument( str, nopt, &opt[2]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-08 14:44:10 +08:00
|
|
|
short_validated.at(a-opt)=1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-02 01:35:24 +08:00
|
|
|
if( authoritative )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
if( !is_gnu_opt && !is_old_opt )
|
|
|
|
is_short_opt = 1;
|
|
|
|
|
|
|
|
|
|
|
|
if( is_short_opt )
|
|
|
|
{
|
2012-01-14 15:44:18 +08:00
|
|
|
size_t j;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
opt_found=1;
|
|
|
|
for( j=1; j<wcslen(opt); j++)
|
|
|
|
{
|
2012-02-08 14:44:10 +08:00
|
|
|
if ( !short_validated.at(j))
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
if( errors )
|
|
|
|
{
|
|
|
|
wchar_t str[2];
|
|
|
|
str[0] = opt[j];
|
|
|
|
str[1]=0;
|
|
|
|
al_push( errors,
|
2007-09-29 05:32:27 +08:00
|
|
|
wcsdupcat(_( L"Unknown option: " ), L"'", str, L"'" ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
opt_found = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if( is_gnu_opt )
|
|
|
|
{
|
|
|
|
opt_found = is_gnu_exact || (hash_get_count( &gnu_match_hash )==1);
|
|
|
|
if( errors && !opt_found )
|
|
|
|
{
|
|
|
|
if( hash_get_count( &gnu_match_hash )==0)
|
|
|
|
{
|
|
|
|
al_push( errors,
|
2007-09-29 05:32:27 +08:00
|
|
|
wcsdupcat( _(L"Unknown option: "), L"'", opt, L"\'" ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
al_push( errors,
|
2007-09-29 05:32:27 +08:00
|
|
|
wcsdupcat( _(L"Multiple matches for option: "), L"'", opt, L"\'" ) );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
hash_destroy( &gnu_match_hash );
|
|
|
|
|
2012-01-16 06:24:58 +08:00
|
|
|
return (authoritative && found_match)?opt_found:1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int complete_is_valid_argument( const wchar_t *str,
|
|
|
|
const wchar_t *opt,
|
|
|
|
const wchar_t *arg )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
Copy any strings in possible_comp which have the specified prefix
|
2007-02-19 07:25:20 +08:00
|
|
|
to the list comp_out. The prefix may contain wildcards. The output
|
|
|
|
will consist of completion_t structs.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
There are three ways to specify descriptions for each
|
|
|
|
completion. Firstly, if a description has already been added to the
|
|
|
|
completion, it is _not_ replaced. Secondly, if the desc_func
|
|
|
|
function is specified, use it to determine a dynamic
|
|
|
|
completion. Thirdly, if none of the above are available, the desc
|
|
|
|
string is used as a description.
|
|
|
|
|
|
|
|
\param comp_out the destination list
|
|
|
|
\param wc_escaped the prefix, possibly containing wildcards. The wildcard should not have been unescaped, i.e. '*' should be used for any string, not the ANY_STRING character.
|
2006-01-10 01:17:25 +08:00
|
|
|
\param desc the default description, used for completions with no embedded description. The description _may_ contain a COMPLETE_SEP character, if not, one will be prefixed to it
|
2005-09-20 21:26:39 +08:00
|
|
|
\param desc_func the function that generates a description for those completions witout an embedded description
|
|
|
|
\param possible_comp the list of possible completions to iterate over
|
|
|
|
*/
|
2007-02-17 19:05:55 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
static void complete_strings( std::vector<completion_t> &comp_out,
|
2007-02-17 19:05:55 +08:00
|
|
|
const wchar_t *wc_escaped,
|
|
|
|
const wchar_t *desc,
|
|
|
|
const wchar_t *(*desc_func)(const wchar_t *),
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &possible_comp,
|
2007-02-25 18:37:15 +08:00
|
|
|
int flags )
|
2007-02-17 19:05:55 +08:00
|
|
|
{
|
2012-01-30 18:23:58 +08:00
|
|
|
wcstring tmp = wc_escaped;
|
|
|
|
if (! expand_one(tmp, EXPAND_SKIP_CMDSUBST | EXPAND_SKIP_WILDCARDS))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const wchar_t *wc = parse_util_unescape_wildcards( tmp.c_str() );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-30 14:06:58 +08:00
|
|
|
for( size_t i=0; i< possible_comp.size(); i++ )
|
2007-02-17 19:05:55 +08:00
|
|
|
{
|
2012-01-17 00:56:47 +08:00
|
|
|
wcstring temp = possible_comp.at( i ).completion;
|
|
|
|
const wchar_t *next_str = temp.empty()?NULL:temp.c_str();
|
2007-04-21 03:55:06 +08:00
|
|
|
|
2007-02-17 19:05:55 +08:00
|
|
|
if( next_str )
|
2007-02-24 16:11:31 +08:00
|
|
|
{
|
2007-02-25 18:37:15 +08:00
|
|
|
wildcard_complete( next_str, wc, desc, desc_func, comp_out, flags );
|
2007-02-24 16:11:31 +08:00
|
|
|
}
|
2007-02-17 19:05:55 +08:00
|
|
|
}
|
|
|
|
|
2012-01-30 18:23:58 +08:00
|
|
|
free( (void *)wc );
|
2007-02-17 19:05:55 +08:00
|
|
|
}
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
If command to complete is short enough, substitute
|
|
|
|
the description with the whatis information for the executable.
|
|
|
|
*/
|
2012-01-17 00:56:47 +08:00
|
|
|
static void complete_cmd_desc( const wchar_t *cmd, std::vector<completion_t> &comp )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
const wchar_t *cmd_start;
|
|
|
|
int cmd_len;
|
2006-11-16 21:04:00 +08:00
|
|
|
int skip;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !cmd )
|
|
|
|
return;
|
|
|
|
|
2006-01-10 01:17:25 +08:00
|
|
|
cmd_start=wcsrchr(cmd, L'/');
|
|
|
|
|
|
|
|
if( cmd_start )
|
|
|
|
cmd_start++;
|
|
|
|
else
|
2005-09-20 21:26:39 +08:00
|
|
|
cmd_start = cmd;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
cmd_len = wcslen(cmd_start);
|
|
|
|
|
|
|
|
/*
|
|
|
|
Using apropos with a single-character search term produces far
|
|
|
|
to many results - require at least two characters if we don't
|
|
|
|
know the location of the whatis-database.
|
|
|
|
*/
|
2006-02-04 01:27:36 +08:00
|
|
|
if(cmd_len < 2 )
|
2005-09-20 21:26:39 +08:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( wildcard_has( cmd_start, 0 ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2006-11-16 21:04:00 +08:00
|
|
|
skip = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-30 15:22:42 +08:00
|
|
|
for( size_t i=0; i< comp.size(); i++ )
|
2006-11-16 21:04:00 +08:00
|
|
|
{
|
2012-01-17 00:56:47 +08:00
|
|
|
const completion_t &c = comp.at ( i );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
if( c.completion.empty() || (c.completion[c.completion.size()-1] != L'/' ))
|
2006-11-16 21:04:00 +08:00
|
|
|
{
|
|
|
|
skip = 0;
|
|
|
|
break;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-11-16 21:04:00 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-11-16 21:04:00 +08:00
|
|
|
if( skip )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-11-16 21:04:00 +08:00
|
|
|
|
2012-02-03 04:04:04 +08:00
|
|
|
wcstring lookup_cmd(L"__fish_describe_command ");
|
|
|
|
lookup_cmd.append(escape_string(cmd_start, 1));
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-03 04:04:04 +08:00
|
|
|
std::map<wcstring, wcstring> lookup;
|
2006-02-08 23:29:09 +08:00
|
|
|
|
2006-01-08 10:56:56 +08:00
|
|
|
/*
|
|
|
|
First locate a list of possible descriptions using a single
|
|
|
|
call to apropos or a direct search if we know the location
|
|
|
|
of the whatis database. This can take some time on slower
|
|
|
|
systems with a large set of manuals, but it should be ok
|
|
|
|
since apropos is only called once.
|
|
|
|
*/
|
2012-02-03 04:04:04 +08:00
|
|
|
wcstring_list_t list;
|
2012-02-08 15:35:41 +08:00
|
|
|
if( exec_subshell( lookup_cmd, list ) != -1 )
|
2006-10-19 23:36:03 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-10-19 23:36:03 +08:00
|
|
|
/*
|
|
|
|
Then discard anything that is not a possible completion and put
|
|
|
|
the result into a hashtable with the completion as key and the
|
|
|
|
description as value.
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2006-10-19 23:36:03 +08:00
|
|
|
Should be reasonably fast, since no memory allocations are needed.
|
|
|
|
*/
|
2012-02-03 04:04:04 +08:00
|
|
|
for( size_t i=0; i < list.size(); i++ )
|
2006-10-19 23:36:03 +08:00
|
|
|
{
|
2012-02-03 04:04:04 +08:00
|
|
|
const wcstring &elstr = list.at(i);
|
|
|
|
|
|
|
|
const wcstring fullkey(elstr, wcslen(cmd_start));
|
|
|
|
|
|
|
|
size_t tab_idx = fullkey.find(L'\t');
|
|
|
|
if( tab_idx == wcstring::npos )
|
2006-10-19 23:36:03 +08:00
|
|
|
continue;
|
2012-02-03 04:04:04 +08:00
|
|
|
|
|
|
|
const wcstring key(fullkey, 0, tab_idx);
|
|
|
|
wcstring val(fullkey, tab_idx + 1);
|
2007-01-07 22:24:30 +08:00
|
|
|
|
2006-10-19 23:36:03 +08:00
|
|
|
/*
|
|
|
|
And once again I make sure the first character is uppercased
|
|
|
|
because I like it that way, and I get to decide these
|
|
|
|
things.
|
|
|
|
*/
|
2012-02-03 04:04:04 +08:00
|
|
|
if (! val.empty())
|
|
|
|
val[0]=towupper(val[0]);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-03 04:04:04 +08:00
|
|
|
lookup[key] = val;
|
2006-10-19 23:36:03 +08:00
|
|
|
}
|
2006-01-08 10:56:56 +08:00
|
|
|
|
2006-10-19 23:36:03 +08:00
|
|
|
/*
|
|
|
|
Then do a lookup on every completion and if a match is found,
|
|
|
|
change to the new description.
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2006-10-19 23:36:03 +08:00
|
|
|
This needs to do a reallocation for every description added, but
|
|
|
|
there shouldn't be that many completions, so it should be ok.
|
|
|
|
*/
|
2012-01-30 15:22:42 +08:00
|
|
|
for( size_t i=0; i<comp.size(); i++ )
|
2006-10-19 23:36:03 +08:00
|
|
|
{
|
2012-02-03 04:04:04 +08:00
|
|
|
completion_t &completion = comp.at(i);
|
|
|
|
const wcstring &el = completion.completion;
|
|
|
|
if (el.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
std::map<wcstring, wcstring>::iterator new_desc_iter = lookup.find(el);
|
|
|
|
if (new_desc_iter != lookup.end())
|
|
|
|
completion.description = new_desc_iter->second;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2006-06-17 21:07:08 +08:00
|
|
|
/**
|
|
|
|
Returns a description for the specified function
|
|
|
|
*/
|
2006-01-24 07:32:13 +08:00
|
|
|
static const wchar_t *complete_function_desc( const wchar_t *fn )
|
|
|
|
{
|
|
|
|
const wchar_t *res = function_get_desc( fn );
|
|
|
|
|
|
|
|
if( !res )
|
|
|
|
res = function_get_definition( fn );
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Complete the specified command name. Search for executables in the
|
|
|
|
path, executables defined using an absolute path, functions,
|
|
|
|
builtins and directories for implicit cd commands.
|
|
|
|
|
|
|
|
\param cmd the command string to find completions for
|
|
|
|
|
2006-01-24 07:33:47 +08:00
|
|
|
\param comp the list to add all completions to
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
static void complete_cmd( const wchar_t *cmd,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp,
|
2009-02-03 06:46:45 +08:00
|
|
|
int use_function,
|
|
|
|
int use_builtin,
|
|
|
|
int use_command )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
wchar_t *path_cpy;
|
|
|
|
wchar_t *nxt_path;
|
|
|
|
wchar_t *state;
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> possible_comp;
|
2005-09-20 21:26:39 +08:00
|
|
|
wchar_t *nxt_completion;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-02-09 03:48:51 +08:00
|
|
|
wchar_t *cdpath_cpy = wcsdup(L".");
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( (wcschr( cmd, L'/') != 0) || (cmd[0] == L'~' ) )
|
|
|
|
{
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
if( use_command )
|
2005-12-04 00:43:56 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-06 08:42:24 +08:00
|
|
|
if( expand_string(cmd, comp, ACCEPT_INCOMPLETE | EXECUTABLES_ONLY ) != EXPAND_ERROR )
|
2006-12-14 08:03:26 +08:00
|
|
|
{
|
2007-02-19 07:25:20 +08:00
|
|
|
complete_cmd_desc( cmd, comp );
|
2006-12-14 08:03:26 +08:00
|
|
|
}
|
2005-12-04 00:43:56 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2006-12-14 08:03:26 +08:00
|
|
|
if( use_command )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-14 18:42:17 +08:00
|
|
|
const env_var_t path = env_get_string(L"PATH");
|
|
|
|
if( !path.missing() )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-10 04:10:03 +08:00
|
|
|
path_cpy = wcsdup( path.c_str() );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
for( nxt_path = wcstok( path_cpy, ARRAY_SEP_STR, &state );
|
2007-10-28 17:06:05 +08:00
|
|
|
nxt_path != 0;
|
|
|
|
nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
|
2006-12-14 08:03:26 +08:00
|
|
|
{
|
2012-01-30 15:22:42 +08:00
|
|
|
size_t prev_count;
|
2007-10-28 17:06:05 +08:00
|
|
|
int path_len = wcslen(nxt_path);
|
|
|
|
int add_slash;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-10-28 17:06:05 +08:00
|
|
|
if( !path_len )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-01-19 02:33:19 +08:00
|
|
|
|
2007-10-28 17:06:05 +08:00
|
|
|
add_slash = nxt_path[path_len-1]!=L'/';
|
2007-09-29 05:32:27 +08:00
|
|
|
nxt_completion = wcsdupcat( nxt_path,
|
2009-02-03 06:46:45 +08:00
|
|
|
add_slash?L"/":L"",
|
|
|
|
cmd );
|
2006-12-14 08:03:26 +08:00
|
|
|
if( ! nxt_completion )
|
|
|
|
continue;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
prev_count = comp.size() ;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-06 08:42:24 +08:00
|
|
|
if( expand_string(
|
2009-02-03 06:46:45 +08:00
|
|
|
nxt_completion,
|
|
|
|
comp,
|
|
|
|
ACCEPT_INCOMPLETE |
|
|
|
|
EXECUTABLES_ONLY ) != EXPAND_ERROR )
|
2006-10-05 05:42:04 +08:00
|
|
|
{
|
2012-01-30 15:22:42 +08:00
|
|
|
for( size_t i=prev_count; i< comp.size(); i++ )
|
2007-10-28 17:06:05 +08:00
|
|
|
{
|
2012-01-17 00:56:47 +08:00
|
|
|
completion_t &c = comp.at( i );
|
|
|
|
if(c.flags & COMPLETE_NO_CASE )
|
2007-10-28 17:06:05 +08:00
|
|
|
{
|
2012-01-17 00:56:47 +08:00
|
|
|
c.completion += add_slash ;
|
2007-10-28 17:06:05 +08:00
|
|
|
}
|
|
|
|
}
|
2006-10-05 05:42:04 +08:00
|
|
|
}
|
2006-12-14 08:03:26 +08:00
|
|
|
}
|
|
|
|
free( path_cpy );
|
2007-03-18 06:56:11 +08:00
|
|
|
complete_cmd_desc( cmd, comp );
|
2006-10-18 23:51:51 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/*
|
|
|
|
These return the original strings - don't free them
|
|
|
|
*/
|
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
if( use_function )
|
|
|
|
{
|
2012-01-14 15:44:18 +08:00
|
|
|
//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++) {
|
2012-02-02 08:27:14 +08:00
|
|
|
possible_comp.push_back(completion_t(names.at(i)));
|
2012-01-14 15:44:18 +08:00
|
|
|
}
|
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
complete_strings( comp, cmd, 0, &complete_function_desc, possible_comp, 0 );
|
2006-12-14 08:03:26 +08:00
|
|
|
}
|
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
possible_comp.clear();
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
if( use_builtin )
|
|
|
|
{
|
2012-02-01 11:47:56 +08:00
|
|
|
builtin_get_names( possible_comp );
|
2012-01-17 00:56:47 +08:00
|
|
|
complete_strings( comp, cmd, 0, &builtin_get_desc, possible_comp, 0 );
|
2006-12-14 08:03:26 +08:00
|
|
|
}
|
2012-01-17 00:56:47 +08:00
|
|
|
// al_destroy( &possible_comp );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
if( use_builtin || (use_function && function_exists( L"cd") ) )
|
2006-02-08 23:29:09 +08:00
|
|
|
{
|
2006-12-14 08:03:26 +08:00
|
|
|
/*
|
|
|
|
Tab complete implicit cd for directories in CDPATH
|
|
|
|
*/
|
|
|
|
if( cmd[0] != L'/' && ( wcsncmp( cmd, L"./", 2 )!=0) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-12-14 08:03:26 +08:00
|
|
|
for( nxt_path = wcstok( cdpath_cpy, ARRAY_SEP_STR, &state );
|
|
|
|
nxt_path != 0;
|
|
|
|
nxt_path = wcstok( 0, ARRAY_SEP_STR, &state) )
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2006-12-14 08:03:26 +08:00
|
|
|
wchar_t *nxt_completion=
|
2007-09-29 05:32:27 +08:00
|
|
|
wcsdupcat( nxt_path,
|
2009-02-03 06:46:45 +08:00
|
|
|
(nxt_path[wcslen(nxt_path)-1]==L'/'?L"":L"/"),
|
|
|
|
cmd );
|
2006-12-14 08:03:26 +08:00
|
|
|
if( ! nxt_completion )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-06 08:42:24 +08:00
|
|
|
if( expand_string( nxt_completion,
|
2007-02-19 07:25:20 +08:00
|
|
|
comp,
|
2006-12-14 08:03:26 +08:00
|
|
|
ACCEPT_INCOMPLETE | DIRECTORIES_ONLY ) != EXPAND_ERROR )
|
|
|
|
{
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
free( cdpath_cpy );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Evaluate the argument list (as supplied by complete -a) and insert
|
2006-01-10 01:40:32 +08:00
|
|
|
any return matching completions. Matching is done using \c
|
2005-09-20 21:26:39 +08:00
|
|
|
copy_strings_with_prefix, meaning the completion may contain
|
|
|
|
wildcards. Logically, this is not always the right thing to do, but
|
2006-01-10 01:40:32 +08:00
|
|
|
I have yet to come up with a case where this matters.
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
\param str The string to complete.
|
|
|
|
\param args The list of option arguments to be evaluated.
|
|
|
|
\param desc Description of the completion
|
|
|
|
\param comp_out The list into which the results will be inserted
|
|
|
|
*/
|
|
|
|
static void complete_from_args( const wchar_t *str,
|
|
|
|
const wchar_t *args,
|
|
|
|
const wchar_t *desc,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp_out,
|
2007-03-01 05:43:27 +08:00
|
|
|
int flags )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> possible_comp;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-01-21 03:24:43 +08:00
|
|
|
parser_t parser(PARSER_TYPE_COMPLETIONS_ONLY);
|
2006-02-16 21:36:32 +08:00
|
|
|
proc_push_interactive(0);
|
2012-01-29 16:41:39 +08:00
|
|
|
parser.eval_args( args, possible_comp );
|
|
|
|
|
2006-02-16 21:36:32 +08:00
|
|
|
proc_pop_interactive();
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
complete_strings( comp_out, str, desc, 0, possible_comp, flags );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
// al_foreach( &possible_comp, &free );
|
|
|
|
// al_destroy( &possible_comp );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Match against an old style long option
|
|
|
|
*/
|
2006-10-24 19:03:52 +08:00
|
|
|
static int param_match_old( complete_entry_opt_t *e,
|
2007-01-19 00:45:28 +08:00
|
|
|
const wchar_t *optstr )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
return (optstr[0] == L'-') && (wcscmp( e->long_opt, &optstr[1] ) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Match a parameter
|
|
|
|
*/
|
2007-01-19 00:45:28 +08:00
|
|
|
static int param_match( const complete_entry_opt_t *e,
|
|
|
|
const wchar_t *optstr )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
if( e->short_opt != L'\0' &&
|
|
|
|
e->short_opt == optstr[1] )
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
if( !e->old_mode && (wcsncmp( L"--", optstr, 2 ) == 0 ))
|
|
|
|
{
|
|
|
|
if( wcscmp( e->long_opt, &optstr[2] ) == 0 )
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Test if a string is an option with an argument, like --color=auto or -I/usr/include
|
|
|
|
*/
|
2007-01-19 00:45:28 +08:00
|
|
|
static wchar_t *param_match2( const complete_entry_opt_t *e,
|
|
|
|
const wchar_t *optstr )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
if( e->short_opt != L'\0' && e->short_opt == optstr[1] )
|
2007-01-19 00:45:28 +08:00
|
|
|
return (wchar_t *)&optstr[2];
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !e->old_mode && (wcsncmp( L"--", optstr, 2 ) == 0) )
|
|
|
|
{
|
|
|
|
int len = wcslen( e->long_opt );
|
|
|
|
|
|
|
|
if( wcsncmp( e->long_opt, &optstr[2],len ) == 0 )
|
|
|
|
{
|
|
|
|
if( optstr[len+2] == L'=' )
|
2007-01-19 00:45:28 +08:00
|
|
|
return (wchar_t *)&optstr[len+3];
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Tests whether a short option is a viable completion
|
|
|
|
*/
|
2012-02-09 06:47:50 +08:00
|
|
|
static int short_ok( const wcstring &arg_str, wchar_t nextopt, const wcstring &allopt_str )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
const wchar_t *arg = arg_str.c_str();
|
|
|
|
const wchar_t *allopt = allopt_str.c_str();
|
2007-01-19 00:45:28 +08:00
|
|
|
const wchar_t *ptr;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
if( arg[0] != L'-')
|
|
|
|
return arg[0] == L'\0';
|
|
|
|
if( arg[1] == L'-' )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( wcschr( arg, nextopt ) != 0 )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for( ptr = arg+1; *ptr; ptr++ )
|
|
|
|
{
|
2012-01-06 05:58:48 +08:00
|
|
|
const wchar_t *tmp = wcschr( allopt, *ptr );
|
2005-09-20 21:26:39 +08:00
|
|
|
/* Unknown option */
|
|
|
|
if( tmp == 0 )
|
|
|
|
{
|
|
|
|
/*fwprintf( stderr, L"Unknown option %lc", *ptr );*/
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( *(tmp+1) == L':' )
|
|
|
|
{
|
|
|
|
/* fwprintf( stderr, L"Woot %ls", allopt );*/
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-02-08 14:44:10 +08:00
|
|
|
void complete_load( const wcstring &name, bool reload )
|
2006-02-08 17:20:05 +08:00
|
|
|
{
|
2012-01-26 10:40:08 +08:00
|
|
|
completion_autoloader.load( name, reload );
|
2006-02-08 17:20:05 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
Find completion for the argument str of command cmd_orig with
|
|
|
|
previous option popt. Insert results into comp_out. Return 0 if file
|
|
|
|
completion should be disabled, 1 otherwise.
|
|
|
|
*/
|
2007-01-19 00:45:28 +08:00
|
|
|
static int complete_param( const wchar_t *cmd_orig,
|
|
|
|
const wchar_t *popt,
|
|
|
|
const wchar_t *str,
|
2007-01-10 20:45:28 +08:00
|
|
|
int use_switches,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp_out )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2006-10-24 19:03:52 +08:00
|
|
|
complete_entry_opt_t *o;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
int use_common=1, use_files=1;
|
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
wcstring cmd, path;
|
|
|
|
parse_cmd_string(cmd_orig, path, cmd);
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2012-02-08 14:44:10 +08:00
|
|
|
complete_load( cmd, true );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
for (completion_entry_list_t::iterator iter = completion_entries.begin(); iter != completion_entries.end(); iter++)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
completion_entry_t *i = *iter;
|
|
|
|
const wcstring &match = i->cmd_type?path:cmd;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
if( ( (!wildcard_match( match, i->cmd ) ) ) )
|
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
continue;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
use_common=1;
|
|
|
|
if( use_switches )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
if( str[0] == L'-' )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
/* Check if we are entering a combined option and argument
|
|
|
|
(like --color=auto or -I/usr/include) */
|
|
|
|
for( o = i->first_option; o; o=o->next )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
wchar_t *arg;
|
|
|
|
if( (arg=param_match2( o, str ))!=0 && condition_test( o->condition ))
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
use_common &= ((o->result_mode & NO_COMMON )==0);
|
|
|
|
use_files &= ((o->result_mode & NO_FILES )==0);
|
2007-03-01 05:43:27 +08:00
|
|
|
complete_from_args( arg, o->comp, C_(o->desc), comp_out, o->flags );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2007-01-10 20:45:28 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
2007-01-10 20:45:28 +08:00
|
|
|
else if( popt[0] == L'-' )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
/* Set to true if we found a matching old-style switch */
|
|
|
|
int old_style_match = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
/*
|
|
|
|
If we are using old style long options, check for them
|
|
|
|
first
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
for( o = i->first_option; o; o=o->next )
|
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
if( o->old_mode )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
if( param_match_old( o, popt ) && condition_test( o->condition ))
|
|
|
|
{
|
|
|
|
old_style_match = 1;
|
|
|
|
use_common &= ((o->result_mode & NO_COMMON )==0);
|
|
|
|
use_files &= ((o->result_mode & NO_FILES )==0);
|
2007-03-01 05:43:27 +08:00
|
|
|
complete_from_args( str, o->comp, C_(o->desc), comp_out, o->flags );
|
2007-01-10 20:45:28 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
/*
|
|
|
|
No old style option matched, or we are not using old
|
|
|
|
style options. We check if any short (or gnu style
|
|
|
|
options do.
|
|
|
|
*/
|
|
|
|
if( !old_style_match )
|
|
|
|
{
|
|
|
|
for( o = i->first_option; o; o=o->next )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Gnu-style options with _optional_ arguments must
|
|
|
|
be specified as a single token, so that it can
|
|
|
|
be differed from a regular argument.
|
|
|
|
*/
|
|
|
|
if( !o->old_mode && wcslen(o->long_opt) && !(o->result_mode & NO_COMMON) )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( param_match( o, popt ) && condition_test( o->condition ))
|
|
|
|
{
|
|
|
|
use_common &= ((o->result_mode & NO_COMMON )==0);
|
|
|
|
use_files &= ((o->result_mode & NO_FILES )==0);
|
2007-03-01 05:43:27 +08:00
|
|
|
complete_from_args( str, o->comp, C_(o->desc), comp_out, o->flags );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( use_common )
|
|
|
|
{
|
|
|
|
|
|
|
|
for( o = i->first_option; o; o=o->next )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
If this entry is for the base command,
|
|
|
|
check if any of the arguments match
|
|
|
|
*/
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !condition_test( o->condition ))
|
|
|
|
continue;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
if( (o->short_opt == L'\0' ) && (o->long_opt[0]==L'\0'))
|
|
|
|
{
|
|
|
|
use_files &= ((o->result_mode & NO_FILES )==0);
|
2007-03-01 05:43:27 +08:00
|
|
|
complete_from_args( str, o->comp, C_(o->desc), comp_out, o->flags );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
if( wcslen(str) > 0 && use_switches )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
Check if the short style option matches
|
|
|
|
*/
|
|
|
|
if( o->short_opt != L'\0' &&
|
|
|
|
short_ok( str, o->short_opt, i->short_opt_str ) )
|
|
|
|
{
|
2006-06-08 07:56:01 +08:00
|
|
|
const wchar_t *desc = C_(o->desc );
|
2007-02-18 20:08:41 +08:00
|
|
|
wchar_t completion[2];
|
|
|
|
completion[0] = o->short_opt;
|
|
|
|
completion[1] = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-02-18 20:08:41 +08:00
|
|
|
completion_allocate( comp_out, completion, desc, 0 );
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Check if the long style option matches
|
|
|
|
*/
|
|
|
|
if( o->long_opt[0] != L'\0' )
|
|
|
|
{
|
2007-03-01 05:43:27 +08:00
|
|
|
int match=0, match_no_case=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
wcstring whole_opt;
|
|
|
|
whole_opt.append(o->old_mode?L"-":L"--");
|
|
|
|
whole_opt.append(o->long_opt);
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
match = string_prefixes_string(str, whole_opt);
|
2007-03-01 05:43:27 +08:00
|
|
|
|
|
|
|
if( !match )
|
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
match_no_case = wcsncasecmp( str, whole_opt.c_str(), wcslen(str) )==0;
|
2007-03-01 05:43:27 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-03-01 05:43:27 +08:00
|
|
|
if( match || match_no_case )
|
2006-01-31 03:53:10 +08:00
|
|
|
{
|
2007-02-18 20:08:41 +08:00
|
|
|
int has_arg=0; /* Does this switch have any known arguments */
|
|
|
|
int req_arg=0; /* Does this switch _require_ an argument */
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2007-03-01 05:43:27 +08:00
|
|
|
int offset = 0;
|
|
|
|
int flags = 0;
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-03-01 05:43:27 +08:00
|
|
|
if( match )
|
|
|
|
offset = wcslen( str );
|
|
|
|
else
|
|
|
|
flags = COMPLETE_NO_CASE;
|
|
|
|
|
2006-11-08 02:19:11 +08:00
|
|
|
has_arg = !!wcslen( o->comp );
|
|
|
|
req_arg = (o->result_mode & NO_COMMON );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2006-11-08 02:19:11 +08:00
|
|
|
if( !o->old_mode && ( has_arg && !req_arg ) )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-02-18 20:08:41 +08:00
|
|
|
/*
|
|
|
|
Optional arguments to a switch can
|
|
|
|
only be handled using the '=', so we
|
|
|
|
add it as a completion. By default
|
|
|
|
we avoid using '=' and instead rely
|
|
|
|
on '--switch switch-arg', since it
|
|
|
|
is more commonly supported by
|
|
|
|
homebrew getopt-like functions.
|
|
|
|
*/
|
|
|
|
string_buffer_t completion;
|
|
|
|
sb_init( &completion );
|
2007-03-01 05:43:27 +08:00
|
|
|
|
2007-02-18 20:08:41 +08:00
|
|
|
sb_printf( &completion,
|
2011-12-27 11:18:46 +08:00
|
|
|
L"%ls=",
|
2012-02-09 06:47:50 +08:00
|
|
|
whole_opt.c_str()+offset );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-02-18 20:08:41 +08:00
|
|
|
completion_allocate( comp_out,
|
|
|
|
(wchar_t *)completion.buff,
|
|
|
|
C_(o->desc),
|
2011-12-27 11:18:46 +08:00
|
|
|
flags );
|
|
|
|
|
2007-02-18 20:08:41 +08:00
|
|
|
sb_destroy( &completion );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-02-18 20:08:41 +08:00
|
|
|
completion_allocate( comp_out,
|
2012-02-09 06:47:50 +08:00
|
|
|
whole_opt.c_str() + offset,
|
2007-02-18 20:08:41 +08:00
|
|
|
C_(o->desc),
|
2007-03-01 05:43:27 +08:00
|
|
|
flags );
|
2011-12-27 11:18:46 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
return use_files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Perform file completion on the specified string
|
|
|
|
*/
|
2012-02-06 16:57:43 +08:00
|
|
|
static void complete_param_expand( const wchar_t *str,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp_out,
|
2005-09-20 21:26:39 +08:00
|
|
|
int do_file )
|
|
|
|
{
|
2012-02-06 16:57:43 +08:00
|
|
|
const wchar_t *comp_str;
|
2007-02-19 07:25:20 +08:00
|
|
|
int flags;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( (wcsncmp( str, L"--", 2 )) == 0 && (comp_str = wcschr(str, L'=' ) ) )
|
|
|
|
{
|
|
|
|
comp_str++;
|
|
|
|
}
|
|
|
|
else
|
2006-02-20 02:19:32 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
comp_str = str;
|
2006-02-20 02:19:32 +08:00
|
|
|
}
|
2006-10-19 23:36:03 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
flags = EXPAND_SKIP_CMDSUBST |
|
|
|
|
ACCEPT_INCOMPLETE |
|
2007-02-19 07:25:20 +08:00
|
|
|
(do_file?0:EXPAND_SKIP_WILDCARDS);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-06 08:42:24 +08:00
|
|
|
if( expand_string( wcsdup(comp_str),
|
2007-02-19 07:25:20 +08:00
|
|
|
comp_out,
|
|
|
|
flags ) == EXPAND_ERROR )
|
2006-10-19 23:36:03 +08:00
|
|
|
{
|
2006-10-24 19:03:52 +08:00
|
|
|
debug( 3, L"Error while expanding string '%ls'", comp_str );
|
2006-10-19 23:36:03 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2007-02-17 19:05:55 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
/**
|
|
|
|
Complete the specified string as an environment variable
|
|
|
|
*/
|
2007-03-25 03:07:38 +08:00
|
|
|
static int complete_variable( const wchar_t *whole_var,
|
2009-02-03 06:46:45 +08:00
|
|
|
int start_offset,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp_list )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-03-25 03:07:38 +08:00
|
|
|
const wchar_t *var = &whole_var[start_offset];
|
2005-09-20 21:26:39 +08:00
|
|
|
int varlen = wcslen( var );
|
|
|
|
int res = 0;
|
2012-02-02 08:27:14 +08:00
|
|
|
|
|
|
|
const wcstring_list_t names = env_get_names(0);
|
|
|
|
for( size_t i=0; i<names.size(); i++ )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
const wcstring & env_name = names.at(i);
|
|
|
|
int namelen = env_name.size();
|
2011-12-27 11:18:46 +08:00
|
|
|
int match=0, match_no_case=0;
|
2006-02-20 02:19:32 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( varlen > namelen )
|
|
|
|
continue;
|
|
|
|
|
2012-02-02 08:27:14 +08:00
|
|
|
match = string_prefixes_string(var, env_name);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
if( !match )
|
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
match_no_case = ( wcsncasecmp( var, env_name.c_str(), varlen) == 0 );
|
2007-03-25 03:07:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( match || match_no_case )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
const env_var_t value_unescaped = env_get_string( env_name.c_str() );
|
2012-01-14 18:42:17 +08:00
|
|
|
if( !value_unescaped.missing() )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
wcstring comp;
|
2007-03-25 03:07:38 +08:00
|
|
|
int flags = 0;
|
|
|
|
int offset = 0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
if( match )
|
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
comp.append(env_name.c_str() + varlen);
|
2007-03-25 03:07:38 +08:00
|
|
|
offset = varlen;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-02-02 08:27:14 +08:00
|
|
|
comp.append(whole_var, start_offset);
|
|
|
|
comp.append(env_name);
|
2008-01-14 09:00:32 +08:00
|
|
|
flags = COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE;
|
2007-03-25 03:07:38 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-08 16:15:06 +08:00
|
|
|
wcstring value = expand_escape_variable( value_unescaped );
|
2012-02-02 08:27:14 +08:00
|
|
|
|
|
|
|
wcstring desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str());
|
2011-12-27 11:18:46 +08:00
|
|
|
completion_allocate( comp_list,
|
2012-02-02 08:27:14 +08:00
|
|
|
comp.c_str(),
|
|
|
|
desc.c_str(),
|
2009-02-03 06:46:45 +08:00
|
|
|
flags );
|
2007-02-17 19:05:55 +08:00
|
|
|
res =1;
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-20 02:19:32 +08:00
|
|
|
Search the specified string for the \$ sign. If found, try to
|
2011-12-27 11:18:46 +08:00
|
|
|
complete as an environment variable.
|
2006-02-20 02:19:32 +08:00
|
|
|
|
|
|
|
\return 0 if unable to complete, 1 otherwise
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
2006-01-31 03:53:10 +08:00
|
|
|
static int try_complete_variable( const wchar_t *cmd,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
int len = wcslen( cmd );
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for( i=len-1; i>=0; i-- )
|
|
|
|
{
|
|
|
|
if( cmd[i] == L'$' )
|
|
|
|
{
|
|
|
|
/* wprintf( L"Var prefix \'%ls\'\n", &cmd[i+1] );*/
|
2007-03-25 03:07:38 +08:00
|
|
|
return complete_variable( cmd, i+1, comp );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
if( !isalnum(cmd[i]) && cmd[i]!=L'_' )
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-02-20 02:19:32 +08:00
|
|
|
Try to complete the specified string as a username. This is used by
|
|
|
|
~USER type expansion.
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2006-02-20 02:19:32 +08:00
|
|
|
\return 0 if unable to complete, 1 otherwise
|
|
|
|
*/
|
2006-01-31 03:53:10 +08:00
|
|
|
static int try_complete_user( const wchar_t *cmd,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2009-02-03 06:46:45 +08:00
|
|
|
const wchar_t *first_char=cmd;
|
|
|
|
int res=0;
|
|
|
|
double start_time = timef();
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
if( *first_char ==L'~' && !wcschr(first_char, L'/'))
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2009-02-03 06:46:45 +08:00
|
|
|
const wchar_t *user_name = first_char+1;
|
2012-01-06 05:58:48 +08:00
|
|
|
const wchar_t *name_end = wcschr( user_name, L'~' );
|
2009-02-03 06:46:45 +08:00
|
|
|
if( name_end == 0 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2009-02-03 06:46:45 +08:00
|
|
|
struct passwd *pw;
|
|
|
|
int name_len = wcslen( user_name );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
setpwent();
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
while((pw=getpwent()) != 0)
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2009-02-03 06:46:45 +08:00
|
|
|
double current_time = timef();
|
|
|
|
wchar_t *pw_name;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
if( current_time - start_time > 0.2 )
|
2009-02-03 06:46:45 +08:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
pw_name = str2wcs( pw->pw_name );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
if( pw_name )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2009-02-03 06:46:45 +08:00
|
|
|
if( wcsncmp( user_name, pw_name, name_len )==0 )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2009-02-03 06:46:45 +08:00
|
|
|
string_buffer_t desc;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
sb_init( &desc );
|
|
|
|
sb_printf( &desc,
|
|
|
|
COMPLETE_USER_DESC,
|
|
|
|
pw_name );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
completion_allocate( comp,
|
2009-02-03 06:46:45 +08:00
|
|
|
&pw_name[name_len],
|
|
|
|
(wchar_t *)desc.buff,
|
|
|
|
COMPLETE_NO_SPACE );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
res=1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
sb_destroy( &desc );
|
|
|
|
}
|
|
|
|
else if( wcsncasecmp( user_name, pw_name, name_len )==0 )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
string_buffer_t name;
|
|
|
|
string_buffer_t desc;
|
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
sb_init( &name );
|
|
|
|
sb_init( &desc );
|
|
|
|
sb_printf( &name,
|
|
|
|
L"~%ls",
|
|
|
|
pw_name );
|
|
|
|
sb_printf( &desc,
|
|
|
|
COMPLETE_USER_DESC,
|
|
|
|
pw_name );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
|
|
|
completion_allocate( comp,
|
2009-02-03 06:46:45 +08:00
|
|
|
(wchar_t *)name.buff,
|
|
|
|
(wchar_t *)desc.buff,
|
|
|
|
COMPLETE_NO_CASE | COMPLETE_DONT_ESCAPE | COMPLETE_NO_SPACE );
|
|
|
|
res=1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
sb_destroy( &desc );
|
|
|
|
sb_destroy( &name );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2009-02-03 06:46:45 +08:00
|
|
|
free( pw_name );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
2009-02-03 06:46:45 +08:00
|
|
|
endpwent();
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-03 06:46:45 +08:00
|
|
|
return res;
|
|
|
|
}
|
2007-02-09 17:33:50 +08:00
|
|
|
|
2012-02-06 08:42:24 +08:00
|
|
|
void complete( const wchar_t *cmd,
|
2012-01-17 00:56:47 +08:00
|
|
|
std::vector<completion_t> &comp )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-06 16:57:43 +08:00
|
|
|
const wchar_t *tok_begin, *tok_end, *cmdsubst_begin, *cmdsubst_end, *prev_begin, *prev_end;
|
2006-01-31 00:51:50 +08:00
|
|
|
wchar_t *buff;
|
2005-09-20 21:26:39 +08:00
|
|
|
tokenizer tok;
|
2012-02-06 16:57:43 +08:00
|
|
|
const wchar_t *current_token=0, *current_command=0, *prev_token=0;
|
2005-09-20 21:26:39 +08:00
|
|
|
int on_command=0;
|
|
|
|
int pos;
|
|
|
|
int done=0;
|
2006-07-12 22:22:42 +08:00
|
|
|
int cursor_pos;
|
2006-12-14 08:03:26 +08:00
|
|
|
int use_command = 1;
|
|
|
|
int use_function = 1;
|
|
|
|
int use_builtin = 1;
|
2007-01-10 20:45:28 +08:00
|
|
|
int had_ddash = 0;
|
2007-03-25 03:07:38 +08:00
|
|
|
|
2006-06-21 08:48:36 +08:00
|
|
|
CHECK( cmd, );
|
2012-01-17 00:56:47 +08:00
|
|
|
// CHECK( comp, );
|
2006-07-11 06:39:56 +08:00
|
|
|
|
2006-07-22 18:16:51 +08:00
|
|
|
complete_init();
|
|
|
|
|
2006-07-11 06:39:56 +08:00
|
|
|
// debug( 1, L"Complete '%ls'", cmd );
|
2006-07-12 22:22:42 +08:00
|
|
|
|
|
|
|
cursor_pos = wcslen(cmd );
|
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
parse_util_cmdsubst_extent( cmd, cursor_pos, &cmdsubst_begin, &cmdsubst_end );
|
|
|
|
parse_util_token_extent( cmd, cursor_pos, &tok_begin, &tok_end, &prev_begin, &prev_end );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
if( !cmdsubst_begin )
|
2006-01-31 03:53:10 +08:00
|
|
|
done=1;
|
2005-09-20 21:26:39 +08:00
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
/**
|
|
|
|
If we are completing a variable name or a tilde expansion user
|
|
|
|
name, we do that and return. No need for any other competions.
|
2005-09-20 21:26:39 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
if( !done )
|
|
|
|
{
|
2007-03-25 03:07:38 +08:00
|
|
|
if( try_complete_variable( tok_begin, comp ) || try_complete_user( tok_begin, comp ))
|
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
done=1;
|
2007-03-25 03:07:38 +08:00
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !done )
|
|
|
|
{
|
2007-03-25 03:07:38 +08:00
|
|
|
pos = cursor_pos-(cmdsubst_begin-cmd);
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
buff = wcsndup( cmdsubst_begin, cmdsubst_end-cmdsubst_begin );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !buff )
|
|
|
|
done=1;
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !done )
|
|
|
|
{
|
|
|
|
int had_cmd=0;
|
|
|
|
int end_loop=0;
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
tok_init( &tok, buff, TOK_ACCEPT_UNFINISHED );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-12-26 06:00:44 +08:00
|
|
|
while( tok_has_next( &tok) && !end_loop )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2007-01-11 01:34:45 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
switch( tok_last_type( &tok ) )
|
|
|
|
{
|
2007-01-11 01:34:45 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_STRING:
|
2006-12-14 08:03:26 +08:00
|
|
|
{
|
2007-01-11 01:34:45 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
wchar_t *ncmd = tok_last( &tok );
|
2007-01-16 09:59:38 +08:00
|
|
|
int is_ddash = (wcscmp( ncmd, L"--" ) == 0) && ( (tok_get_pos( &tok )+2) < pos );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( !had_cmd )
|
|
|
|
{
|
2007-01-11 01:34:45 +08:00
|
|
|
|
2007-04-22 17:50:26 +08:00
|
|
|
if( parser_keywords_is_subcommand( ncmd ) )
|
2006-12-14 08:03:26 +08:00
|
|
|
{
|
|
|
|
if( wcscmp( ncmd, L"builtin" )==0)
|
|
|
|
{
|
|
|
|
use_function = 0;
|
|
|
|
use_command = 0;
|
|
|
|
use_builtin = 1;
|
|
|
|
}
|
|
|
|
else if( wcscmp( ncmd, L"command" )==0)
|
|
|
|
{
|
|
|
|
use_command = 1;
|
|
|
|
use_function = 0;
|
|
|
|
use_builtin = 0;
|
|
|
|
}
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
2006-12-14 08:03:26 +08:00
|
|
|
}
|
|
|
|
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-10 20:45:28 +08:00
|
|
|
if( !is_ddash ||
|
2007-01-11 01:34:45 +08:00
|
|
|
( (use_command && use_function && use_builtin ) ) )
|
2006-12-14 08:03:26 +08:00
|
|
|
{
|
2007-01-11 01:34:45 +08:00
|
|
|
int token_end;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-06 16:57:43 +08:00
|
|
|
free( (void *)current_command );
|
2007-01-11 01:34:45 +08:00
|
|
|
current_command = wcsdup( ncmd );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-11 01:34:45 +08:00
|
|
|
token_end = tok_get_pos( &tok ) + wcslen( ncmd );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-11 01:34:45 +08:00
|
|
|
on_command = (pos <= token_end );
|
2006-12-14 08:03:26 +08:00
|
|
|
had_cmd=1;
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2007-01-10 20:45:28 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( is_ddash )
|
|
|
|
{
|
|
|
|
had_ddash = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
2006-12-14 08:03:26 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_END:
|
|
|
|
case TOK_PIPE:
|
|
|
|
case TOK_BACKGROUND:
|
2007-01-11 01:34:45 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
had_cmd=0;
|
2007-01-10 20:45:28 +08:00
|
|
|
had_ddash = 0;
|
2006-12-14 08:03:26 +08:00
|
|
|
use_command = 1;
|
|
|
|
use_function = 1;
|
|
|
|
use_builtin = 1;
|
2005-09-20 21:26:39 +08:00
|
|
|
break;
|
2007-01-11 01:34:45 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
case TOK_ERROR:
|
2007-01-11 01:34:45 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
end_loop=1;
|
|
|
|
break;
|
2007-01-11 01:34:45 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
2007-01-11 01:34:45 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( tok_get_pos( &tok ) >= pos )
|
2007-01-11 01:34:45 +08:00
|
|
|
{
|
2005-09-20 21:26:39 +08:00
|
|
|
end_loop=1;
|
2007-01-11 01:34:45 +08:00
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
tok_next( &tok );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tok_destroy( &tok );
|
2005-10-12 03:48:31 +08:00
|
|
|
free( buff );
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Get the string to complete
|
|
|
|
*/
|
|
|
|
|
2007-03-25 03:07:38 +08:00
|
|
|
current_token = wcsndup( tok_begin, cursor_pos-(tok_begin-cmd) );
|
2005-12-26 06:00:44 +08:00
|
|
|
|
|
|
|
prev_token = prev_begin ? wcsndup( prev_begin, prev_end - prev_begin ): wcsdup(L"");
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-07-11 06:39:56 +08:00
|
|
|
// debug( 0, L"on_command: %d, %ls %ls\n", on_command, current_command, current_token );
|
2006-12-14 08:03:26 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
Check if we are using the 'command' or 'builtin' builtins
|
2007-01-11 01:34:45 +08:00
|
|
|
_and_ we are writing a switch instead of a command. In that
|
2006-12-14 08:03:26 +08:00
|
|
|
case, complete using the builtins completions, not using a
|
|
|
|
subcommand.
|
|
|
|
*/
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
if( (on_command || (wcscmp( current_token, L"--" ) == 0 ) ) &&
|
2011-12-27 11:18:46 +08:00
|
|
|
(current_token[0] == L'-') &&
|
2006-12-14 08:03:26 +08:00
|
|
|
!(use_command && use_function && use_builtin ) )
|
|
|
|
{
|
2012-02-06 16:57:43 +08:00
|
|
|
free( (void *)current_command );
|
2006-12-14 08:03:26 +08:00
|
|
|
if( use_command == 0 )
|
|
|
|
current_command = wcsdup( L"builtin" );
|
|
|
|
else
|
|
|
|
current_command = wcsdup( L"command" );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
had_cmd = 1;
|
|
|
|
on_command = 0;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
/*
|
|
|
|
Use command completions if in between commands
|
|
|
|
*/
|
2006-05-22 06:32:13 +08:00
|
|
|
if( !had_cmd )
|
|
|
|
{
|
|
|
|
on_command=1;
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-12-14 08:03:26 +08:00
|
|
|
/*
|
|
|
|
We don't want these to be null
|
|
|
|
*/
|
|
|
|
|
2006-05-22 06:32:13 +08:00
|
|
|
if( !current_token )
|
|
|
|
{
|
|
|
|
current_token = wcsdup(L"");
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-22 06:32:13 +08:00
|
|
|
if( !current_command )
|
|
|
|
{
|
|
|
|
current_command = wcsdup(L"");
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-05-22 06:32:13 +08:00
|
|
|
if( !prev_token )
|
|
|
|
{
|
|
|
|
prev_token = wcsdup(L"");
|
|
|
|
}
|
2006-12-14 08:03:26 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
if( current_token && current_command && prev_token )
|
|
|
|
{
|
|
|
|
if( on_command )
|
|
|
|
{
|
|
|
|
/* Complete command filename */
|
|
|
|
complete_cmd( current_token,
|
2006-12-14 08:03:26 +08:00
|
|
|
comp, use_function, use_builtin, use_command );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-01-10 20:45:28 +08:00
|
|
|
int do_file=0;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-19 00:45:28 +08:00
|
|
|
wchar_t *current_command_unescape = unescape( current_command, 0 );
|
|
|
|
wchar_t *prev_token_unescape = unescape( prev_token, 0 );
|
|
|
|
wchar_t *current_token_unescape = unescape( current_token, UNESCAPE_INCOMPLETE );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-27 01:14:13 +08:00
|
|
|
if( current_token_unescape && prev_token_unescape && current_token_unescape )
|
|
|
|
{
|
2011-12-27 11:18:46 +08:00
|
|
|
do_file = complete_param( current_command_unescape,
|
|
|
|
prev_token_unescape,
|
|
|
|
current_token_unescape,
|
|
|
|
!had_ddash,
|
2007-01-27 01:14:13 +08:00
|
|
|
comp );
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-19 00:45:28 +08:00
|
|
|
free( current_command_unescape );
|
|
|
|
free( prev_token_unescape );
|
|
|
|
free( current_token_unescape );
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2006-08-27 08:57:40 +08:00
|
|
|
/*
|
|
|
|
If we have found no command specific completions at
|
|
|
|
all, fall back to using file completions.
|
|
|
|
*/
|
2012-01-17 00:56:47 +08:00
|
|
|
if( comp.empty() )
|
2006-08-27 08:57:40 +08:00
|
|
|
do_file = 1;
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2007-01-19 00:45:28 +08:00
|
|
|
/*
|
|
|
|
This function wants the unescaped string
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
complete_param_expand( current_token, comp, do_file );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-12-27 11:18:46 +08:00
|
|
|
|
2012-02-06 16:57:43 +08:00
|
|
|
free( (void *)current_token );
|
|
|
|
free( (void *)current_command );
|
|
|
|
free( (void *)prev_token );
|
2006-05-15 06:39:03 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
condition_cache_clear();
|
|
|
|
|
2007-02-09 17:33:50 +08:00
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
2012-01-17 00:56:47 +08:00
|
|
|
|
|
|
|
|
2005-10-24 23:26:25 +08:00
|
|
|
/**
|
|
|
|
Print the GNU longopt style switch \c opt, and the argument \c
|
|
|
|
argument to the specified stringbuffer, but only if arguemnt is
|
|
|
|
non-null and longer than 0 characters.
|
|
|
|
*/
|
2005-09-20 21:26:39 +08:00
|
|
|
static void append_switch( string_buffer_t *out,
|
2012-02-09 06:47:50 +08:00
|
|
|
const wcstring &opt,
|
|
|
|
const wcstring &argument )
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
2012-02-09 06:47:50 +08:00
|
|
|
if( argument.empty() )
|
2005-09-20 21:26:39 +08:00
|
|
|
return;
|
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
wcstring esc = escape_string( argument, 1 );
|
|
|
|
sb_printf( out, L" --%ls %ls", opt.c_str(), esc.c_str() );
|
2005-09-20 21:26:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void complete_print( string_buffer_t *out )
|
|
|
|
{
|
2006-10-18 23:51:51 +08:00
|
|
|
CHECK( out, );
|
|
|
|
|
2012-02-09 06:47:50 +08:00
|
|
|
for (completion_entry_list_t::iterator iter = completion_entries.begin(); iter != completion_entries.end(); iter++)
|
|
|
|
{
|
|
|
|
completion_entry_t *e = *iter;
|
2006-10-24 19:03:52 +08:00
|
|
|
complete_entry_opt_t *o;
|
2005-09-20 21:26:39 +08:00
|
|
|
for( o= e->first_option; o; o=o->next )
|
|
|
|
{
|
2012-01-14 15:44:18 +08:00
|
|
|
const wchar_t *modestr[] =
|
2005-09-20 21:26:39 +08:00
|
|
|
{
|
|
|
|
L"",
|
|
|
|
L" --no-files",
|
|
|
|
L" --require-parameter",
|
|
|
|
L" --exclusive"
|
|
|
|
}
|
|
|
|
;
|
|
|
|
|
|
|
|
sb_printf( out,
|
|
|
|
L"complete%ls",
|
|
|
|
modestr[o->result_mode] );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
append_switch( out,
|
|
|
|
e->cmd_type?L"path":L"command",
|
|
|
|
e->cmd );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
|
|
|
|
if( o->short_opt != 0 )
|
|
|
|
{
|
|
|
|
sb_printf( out,
|
|
|
|
L" --short-option '%lc'",
|
|
|
|
o->short_opt );
|
|
|
|
}
|
2006-01-31 03:53:10 +08:00
|
|
|
|
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
append_switch( out,
|
|
|
|
o->old_mode?L"old-option":L"long-option",
|
|
|
|
o->long_opt );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
append_switch( out,
|
|
|
|
L"description",
|
2006-06-08 07:56:01 +08:00
|
|
|
C_(o->desc) );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
append_switch( out,
|
|
|
|
L"arguments",
|
|
|
|
o->comp );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
append_switch( out,
|
|
|
|
L"condition",
|
|
|
|
o->condition );
|
2006-01-31 03:53:10 +08:00
|
|
|
|
2005-09-20 21:26:39 +08:00
|
|
|
sb_printf( out, L"\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|