complete.c File Reference

Functions related to tab-completion. More...

#include "config.h"
#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>
#include "fallback.h"
#include "util.h"
#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"
#include "parse_util.h"
#include "parser_keywords.h"
#include "halloc.h"
#include "halloc_util.h"
#include "wutil.h"
#include "path.h"

Data Structures

struct  complete_entry_opt
 Struct describing a completion option entry. More...
struct  complete_entry
 Struct describing a command completion. More...

Defines

#define COMPLETE_USER_DESC   _( L"Home for %ls" )
 Description for ~USER completion.
#define COMPLETE_VAR_DESC_VAL   _( L"Variable: %ls" )
 Description for short variables.
#define MAX_CMD_DESC_LOOKUP   10
 The maximum number of commands on which to perform description lookup.
#define CC_NOT_TESTED   0
 Condition cache value returned from hashtable when this condition has not yet been tested.
#define CC_TRUE   L"true"
 Condition cache value returned from hashtable when the condition is met.
#define CC_FALSE   L"false"
 Condition cache value returned from hashtable when the condition is not met.
#define C_(wstr)   ((wcscmp(wstr, L"")==0)?L"":wgettext(wstr))
 The special cased translation macro for completions.
#define MAX_USER_LOOKUP_TIME   0.2
 The maximum amount of time that we're willing to spend doing username tilde completion.

Typedefs

typedef struct complete_entry_opt complete_entry_opt_t
 Struct describing a completion option entry.
typedef struct complete_entry complete_entry_t
 Struct describing a command completion.

Functions

static void complete_free_entry (complete_entry_t *c)
 Free a complete_entry_t and its contents.
void completion_allocate (array_list_t *context, const wchar_t *comp, const wchar_t *desc, int flags)
 Create a new completion entry.
static void complete_destroy ()
 Destroys various structures used for tab-completion and free()s the memory used by them.
static void complete_init ()
 The init function for the completion code.
static void condition_cache_clear ()
 This command clears the cache of condition tests created by condition_test().
static int condition_test (const wchar_t *condition)
 Test if the specified script returns zero.
static void complete_free_opt_recursive (complete_entry_opt_t *o)
 Recursively free all complete_entry_opt_t structs and their contents.
static complete_entry_tcomplete_find_exact_entry (const wchar_t *cmd, const int cmd_type)
 Search for an exactly matching completion entry.
static complete_entry_tcomplete_get_exact_entry (const wchar_t *cmd, int cmd_type)
 Locate the specified entry.
void complete_set_authoritative (const wchar_t *cmd, int cmd_type, int authoritative)
 Sets whether the completion list for this command is complete.
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, const wchar_t *desc, int flags)
 Add a completion.
static complete_entry_tcomplete_remove_entry (complete_entry_t *e, wchar_t short_opt, const wchar_t *long_opt)
 Remove all completion options in the specified entry that match the specified short / long option strings.
void complete_remove (const wchar_t *cmd, int cmd_type, wchar_t short_opt, const wchar_t *long_opt)
 Remove a previously defined completion.
static void parse_cmd_string (void *context, const wchar_t *str, wchar_t **pathp, wchar_t **cmdp)
 Find the full path and commandname from a command string.
int complete_is_valid_option (const wchar_t *str, const wchar_t *opt, array_list_t *errors)
 Tests if the specified option is defined for the specified command.
int complete_is_valid_argument (const wchar_t *str, const wchar_t *opt, const wchar_t *arg)
 Tests if the specified argument is valid for the specified option and command.
static void complete_strings (array_list_t *comp_out, const wchar_t *wc_escaped, const wchar_t *desc, const wchar_t *(*desc_func)(const wchar_t *), array_list_t *possible_comp, int flags)
 Copy any strings in possible_comp which have the specified prefix to the list comp_out.
static void complete_cmd_desc (const wchar_t *cmd, array_list_t *comp)
 If command to complete is short enough, substitute the description with the whatis information for the executable.
static const wchar_t * complete_function_desc (const wchar_t *fn)
 Returns a description for the specified function.
static void complete_cmd (const wchar_t *cmd, array_list_t *comp, int use_function, int use_builtin, int use_command)
 Complete the specified command name.
static void complete_from_args (const wchar_t *str, const wchar_t *args, const wchar_t *desc, array_list_t *comp_out, int flags)
 Evaluate the argument list (as supplied by complete -a) and insert any return matching completions.
static int param_match_old (complete_entry_opt_t *e, const wchar_t *optstr)
 Match against an old style long option.
static int param_match (const complete_entry_opt_t *e, const wchar_t *optstr)
 Match a parameter.
static wchar_t * param_match2 (const complete_entry_opt_t *e, const wchar_t *optstr)
 Test if a string is an option with an argument, like --color=auto or -I/usr/include.
static int short_ok (const wchar_t *arg, wchar_t nextopt, const wchar_t *allopt)
 Tests whether a short option is a viable completion.
static void complete_load_handler (const wchar_t *cmd)
 This is an event handler triggered when the definition of a specifiec function is changed.
void complete_load (const wchar_t *name, int reload)
 Load command-specific completions for the specified command.
static int complete_param (const wchar_t *cmd_orig, const wchar_t *popt, const wchar_t *str, int use_switches, array_list_t *comp_out)
 Find completion for the argument str of command cmd_orig with previous option popt.
static void complete_param_expand (wchar_t *str, array_list_t *comp_out, int do_file)
 Perform file completion on the specified string.
static int complete_variable (const wchar_t *whole_var, int start_offset, array_list_t *comp_list)
 Complete the specified string as an environment variable.
static int try_complete_variable (const wchar_t *cmd, array_list_t *comp)
 Search the specified string for the $ sign.
static int try_complete_user (const wchar_t *cmd, array_list_t *comp)
 Try to complete the specified string as a username.
void complete (const wchar_t *cmd, array_list_t *comp)
 Find all completions of the command cmd, insert them into out.
static void append_switch (string_buffer_t *out, const wchar_t *opt, const wchar_t *argument)
 Print the GNU longopt style switch opt, and the argument argument to the specified stringbuffer, but only if arguemnt is non-null and longer than 0 characters.
void complete_print (string_buffer_t *out)
 Print a list of all current completions into the string_buffer_t.

Variables

static complete_entry_tfirst_entry = 0
 First node in the linked list of all completion entries.
static hash_table_tcondition_cache = 0
 Table of completions conditions that have already been tested and the corresponding test results.


Detailed Description

Functions related to tab-completion.

These functions are used for storing and retrieving tab-completion data, as well as for performing tab-completion.


Define Documentation

#define C_ ( wstr   )     ((wcscmp(wstr, L"")==0)?L"":wgettext(wstr))

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)

Referenced by complete_param(), and complete_print().

#define CC_FALSE   L"false"

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.

Referenced by condition_test().

#define CC_NOT_TESTED   0

Condition cache value returned from hashtable when this condition 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.

Referenced by condition_test().

#define CC_TRUE   L"true"

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.

Referenced by condition_test().

#define COMPLETE_VAR_DESC_VAL   _( L"Variable: %ls" )

Description for short variables.

The value is concatenated to this description

Referenced by complete_variable().

#define MAX_CMD_DESC_LOOKUP   10

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_USER_LOOKUP_TIME   0.2

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)


Typedef Documentation

Struct describing a completion option entry.

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 comp is also not empty, it contains a list of non-switch arguments that may only follow directly after the specified switch.


Function Documentation

void complete ( const wchar_t *  cmd,
array_list_t out 
)

Find all completions of the command cmd, insert them into out.

The caller must free the variables returned in out. The results are returned in the array_list_t 'out', in the format of wide character strings, with each element consisting of a suggested completion and a description of what kind of object this completion represents, separated by a separator of type COMPLETE_SEP.

Values returned by this function should be freed by the caller.

If we are completing a variable name or a tilde expansion user name, we do that and return. No need for any other competions.

References al_get_count(), buff, CHECK, complete_cmd(), complete_init(), complete_param(), complete_param_expand(), condition_cache_clear(), end_loop, parse_util_cmdsubst_extent(), parse_util_token_extent(), parser_keywords_is_subcommand(), TOK_ACCEPT_UNFINISHED, TOK_BACKGROUND, tok_destroy(), TOK_END, TOK_ERROR, tok_get_pos(), tok_has_next(), tok_init(), tok_last(), tok_last_type(), tok_next(), TOK_PIPE, TOK_STRING, try_complete_user(), try_complete_variable(), unescape(), and UNESCAPE_INCOMPLETE.

Referenced by builtin_complete(), builtin_read(), perf_complete(), and read_i().

void complete_add ( const wchar_t *  cmd,
int  cmd_type,
wchar_t  short_opt,
const wchar_t *  long_opt,
int  long_mode,
int  result_mode,
const wchar_t *  condition,
const wchar_t *  comp,
const wchar_t *  desc,
int  flags 
)

Add a completion.

All supplied values are copied, they should be freed by or otherwise disposed by the caller.

Examples:

The command 'gcc -o' requires that a file follows it, so the NO_COMMON option is suitable. This can be done using the following line:

complete -c gcc -s o -r

The command 'grep -d' required that one of the strings 'read', 'skip' or 'recurse' is used. As such, it is suitable to specify that a completion requires one of them. This can be done using the following line:

complete -c grep -s d -x -a "read skip recurse"

Parameters:
cmd Command to complete.
cmd_type If cmd_type is PATH, cmd will be interpreted as the absolute path of the program (optionally containing wildcards), otherwise it will be interpreted as the command name.
short_opt The single character name of an option. (-a is a short option, --all and -funroll are long options)
long_opt The multi character name of an option. (-a is a short option, --all and -funroll are long options)
long_mode Whether to use old style, single dash long options.
result_mode Whether to search further completions when this completion has been succesfully matched. If result_mode is SHARED, any other completions may also be used. If result_mode is NO_FILES, file completion should not be used, but other completions may be used. If result_mode is NO_COMMON, on option may follow it - only a parameter. If result_mode is EXCLUSIVE, no option may follow it, and file completion is not performed.
comp A space separated list of completions which may contain subshells.
desc A description of the completion.
condition a command to be run to check it this completion should be used. If condition is empty, the completion is always used.
flags A set of completion flags

References CHECK, complete_entry_opt::comp, complete_get_exact_entry(), complete_entry_opt::condition, complete_entry_opt::desc, complete_entry::first_option, complete_entry_opt::flags, halloc(), halloc_wcsdup(), complete_entry_opt::long_opt, complete_entry_opt::next, NO_COMMON, complete_entry_opt::old_mode, complete_entry_opt::result_mode, complete_entry_opt::short_opt, and complete_entry::short_opt_str.

Referenced by builtin_complete_add2().

static void complete_cmd ( const wchar_t *  cmd,
array_list_t comp,
int  use_function,
int  use_builtin,
int  use_command 
) [static]

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.

Parameters:
cmd the command string to find completions for
comp the list to add all completions to

References ACCEPT_INCOMPLETE, al_destroy(), al_get(), al_get_count(), al_init(), al_truncate(), ARRAY_SEP_STR, builtin_get_desc(), builtin_get_names(), complete_cmd_desc(), complete_function_desc(), COMPLETE_NO_CASE, complete_strings(), DIRECTORIES_ONLY, env_get(), EXECUTABLES_ONLY, EXPAND_ERROR, expand_string(), function_exists(), function_get_names(), halloc_wcsdup(), and wcsdupcat.

Referenced by complete().

static void complete_from_args ( const wchar_t *  str,
const wchar_t *  args,
const wchar_t *  desc,
array_list_t comp_out,
int  flags 
) [static]

Evaluate the argument list (as supplied by complete -a) and insert any return matching completions.

Matching is done using copy_strings_with_prefix, meaning the completion may contain wildcards. Logically, this is not always the right thing to do, but I have yet to come up with a case where this matters.

Parameters:
str The string to complete.
args The list of option arguments to be evaluated.
desc Description of the completion
comp_out The list into which the results will be inserted

References al_destroy(), al_foreach(), al_init(), complete_strings(), eval_args(), proc_pop_interactive(), and proc_push_interactive().

Referenced by complete_param().

static complete_entry_t* complete_get_exact_entry ( const wchar_t *  cmd,
int  cmd_type 
) [static]

static void complete_init (  )  [static]

The init function for the completion code.

Currently, all it really does is make sure complete_destroy is called on exit.

References complete_destroy(), global_context, halloc_register_function_void(), and is_init.

Referenced by complete(), and complete_get_exact_entry().

void complete_load ( const wchar_t *  cmd,
int  reload 
)

Load command-specific completions for the specified command.

This is done automatically whenever completing any given command, so there is no need to call this except in the case of completions with internal dependencies.

Parameters:
cmd the command for which to load command-specific completions
reload should the commands completions be reloaded, even if they where previously loaded. (This is set to true on actual completions, so that changed completion are updated in running shells)

References CHECK, complete_load_handler(), and parse_util_load().

Referenced by complete_is_valid_option(), and complete_param().

static void complete_load_handler ( const wchar_t *  cmd  )  [static]

This is an event handler triggered when the definition of a specifiec function is changed.

It automatcally removes the specified function.

This is to make sure that the function disappears if the file is removed or if ti contains a syntax error.

References COMMAND, and complete_remove().

Referenced by complete_load().

static int complete_param ( const wchar_t *  cmd_orig,
const wchar_t *  popt,
const wchar_t *  str,
int  use_switches,
array_list_t comp_out 
) [static]

void complete_print ( string_buffer_t out  ) 

void complete_set_authoritative ( const wchar_t *  cmd,
int  cmd_type,
int  authoritative 
)

Sets whether the completion list for this command is complete.

If true, any options not matching one of the provided options will be flagged as an error by syntax highlighting.

References complete_entry::authoritative, CHECK, and complete_get_exact_entry().

Referenced by builtin_complete_add().

static void complete_strings ( array_list_t comp_out,
const wchar_t *  wc_escaped,
const wchar_t *  desc,
const wchar_t *(*)(const wchar_t *)  desc_func,
array_list_t possible_comp,
int  flags 
) [static]

Copy any strings in possible_comp which have the specified prefix to the list comp_out.

The prefix may contain wildcards. The output will consist of completion_t structs.

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.

Parameters:
comp_out the destination list
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.
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
desc_func the function that generates a description for those completions witout an embedded description
possible_comp the list of possible completions to iterate over

References al_get(), al_get_count(), expand_one(), EXPAND_SKIP_CMDSUBST, EXPAND_SKIP_WILDCARDS, parse_util_unescape_wildcards(), tmp, and wildcard_complete().

Referenced by complete_cmd(), and complete_from_args().

void completion_allocate ( array_list_t context,
const wchar_t *  comp,
const wchar_t *  desc,
int  flags 
)

static int condition_test ( const wchar_t *  condition  )  [static]

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.

References CC_FALSE, CC_NOT_TESTED, CC_TRUE, DIE_MEM, exec_subshell(), hash_get(), hash_init(), hash_put(), hash_wcs_cmp(), and hash_wcs_func().

Referenced by complete_param().

static void parse_cmd_string ( void *  context,
const wchar_t *  str,
wchar_t **  pathp,
wchar_t **  cmdp 
) [static]

Find the full path and commandname from a command string.

Both pointers are allocated using halloc and will be free'd whencontext is halloc_free'd.

Use the empty string as the 'path' for commands that can not be found.

References complete_entry::cmd, and halloc_wcsdup().

Referenced by complete_is_valid_option(), and complete_param().

static int try_complete_user ( const wchar_t *  cmd,
array_list_t comp 
) [static]

Try to complete the specified string as a username.

This is used by ~USER type expansion.

Returns:
0 if unable to complete, 1 otherwise

References buffer::buff, COMPLETE_DONT_ESCAPE, COMPLETE_NO_CASE, COMPLETE_NO_SPACE, COMPLETE_USER_DESC, completion_allocate(), sb_destroy(), sb_init(), sb_printf(), str2wcs(), and timef().

Referenced by complete().

static int try_complete_variable ( const wchar_t *  cmd,
array_list_t comp 
) [static]

Search the specified string for the $ sign.

If found, try to complete as an environment variable.

Returns:
0 if unable to complete, 1 otherwise

References complete_variable().

Referenced by complete().


Generated on Sun Mar 8 15:46:54 2009 for fish by  doxygen 1.5.6