📄 complete.c
字号:
t++;
*t = '\0';
fetch_hosts (includefile);
continue;
}
/* Skip IP #s. */
for (; buffer[i] && !cr_whitespace (buffer[i]); i++);
/* Get the host names separated by white space. */
while (buffer[i] && buffer[i] != '#'){
for (; i && cr_whitespace (buffer[i]); i++);
if (buffer[i] == '#')
continue;
for (start = i; buffer[i] && !cr_whitespace (buffer[i]); i++);
if (i - start == 0)
continue;
name = (char *) xmalloc (i - start + 1, "Hostname completion");
strncpy (name, buffer + start, i - start);
name [i - start] = 0;
{
char **host_p;
if (hosts_p - hosts >= hosts_alloclen){
int j = hosts_p - hosts;
hosts = realloc ((void *)hosts, ((hosts_alloclen += 30) + 1) * sizeof (char *));
hosts_p = hosts + j;
}
for (host_p = hosts; host_p < hosts_p; host_p++)
if (!strcmp (name, *host_p))
break; /* We do not want any duplicates */
if (host_p == hosts_p){
*(hosts_p++) = name;
*hosts_p = NULL;
} else
free (name);
}
}
}
fclose (file);
}
char *hostname_completion_function (char *text, int state)
{
static char **host_p;
static int textstart, textlen;
if (!state){ /* Initialization stuff */
char *p;
if (hosts != NULL){
for (host_p = hosts; *host_p; host_p++)
free (*host_p);
free (hosts);
}
hosts = (char **) xmalloc (((hosts_alloclen = 30) + 1) * sizeof (char *), "Hostname completion");
*hosts = NULL;
hosts_p = hosts;
fetch_hosts ((p = getenv ("HOSTFILE")) ? p : "/etc/hosts");
host_p = hosts;
textstart = (*text == '@') ? 1 : 0;
textlen = strlen (text + textstart);
}
while (*host_p){
if (!textlen)
break; /* Match all of them */
else if (!strncmp (text + textstart, *host_p, textlen))
break;
host_p++;
}
if (!*host_p){
for (host_p = hosts; *host_p; host_p++)
free (*host_p);
free (hosts);
hosts = NULL;
return NULL;
} else {
char *temp = xmalloc (2 + strlen (*host_p), "Hostname completion");
if (textstart)
*temp = '@';
strcpy (temp + textstart, *host_p);
host_p++;
return temp;
}
}
/* This is the function to call when the word to complete is in a position
where a command word can be found. It looks around $PATH, looking for
commands that match. It also scans aliases, function names, and the
table of shell built-ins. */
char *command_completion_function (char *text, int state)
{
static int isabsolute;
static int phase;
static int text_len;
static char **words;
static char *path;
static char *cur_path;
static char *cur_word;
static int init_state;
static char *bash_reserved [] = { "if", "then", "else", "elif", "fi",
"case", "esac", "for", "select", "while",
"until", "do", "done", "in", "function" , 0};
static char *bash_builtins [] = { "alias", "bg", "bind", "break", "builtin",
"cd", "command", "continue", "declare",
"dirs", "echo", "enable", "eval", "exec",
"exit", "export", "fc", "fg", "getopts",
"hash", "help", "history", "jobs", "kill",
"let", "local", "logout", "popd", "pushd",
"pwd", "read", "readonly", "return", "set",
"shift", "source", "suspend", "test",
"times", "trap", "type", "typeset",
"ulimit", "umask", "unalias", "unset",
"wait" , 0};
char *p, *found;
if (!state){ /* Initialize us a little bit */
isabsolute = strchr (text, PATH_SEP) != 0;
look_for_executables = isabsolute ? 1 : 2;
if (!isabsolute){
words = bash_reserved;
phase = 0;
text_len = strlen (text);
p = getenv ("PATH");
if (!p)
path = NULL;
else {
path = xmalloc (strlen (p) + 2, "Command completion");
strcpy (path, p);
path [strlen (p) + 1] = 0;
p = strchr (path, PATH_ENV_SEP);
while (p){
*p = 0;
p = strchr (p + 1, PATH_ENV_SEP);
}
}
}
}
if (isabsolute){
p = filename_completion_function (text, state);
if (!p)
look_for_executables = 0;
return p;
}
found = NULL;
switch (phase){
case 0: /* Reserved words */
while (*words){
if (!strncmp (*words, text, text_len))
return strdup (*(words++));
words++;
}
phase++;
words = bash_builtins;
case 1: /* Builtin commands */
while (*words){
if (!strncmp (*words, text, text_len))
return strdup (*(words++));
words++;
}
phase++;
if (!path)
break;
cur_path = path;
cur_word = NULL;
case 2: /* And looking through the $PATH */
while (!found){
if (!cur_word){
char *expanded;
if (!*cur_path)
break;
expanded = tilde_expand (cur_path);
if (!expanded){
free (path);
path = NULL;
return NULL;
}
p = canonicalize_pathname (expanded);
cur_word = xmalloc (strlen (p) + 2 + text_len, "Command completion");
strcpy (cur_word, p);
if (cur_word [strlen (cur_word) - 1] != PATH_SEP)
strcat (cur_word, PATH_SEP_STR);
strcat (cur_word, text);
free (p);
cur_path = strchr (cur_path, 0) + 1;
init_state = state;
}
found = filename_completion_function (cur_word, state - init_state);
if (!found){
free (cur_word);
cur_word = NULL;
}
}
}
if (!found){
look_for_executables = 0;
if (path)
free (path);
return NULL;
}
if ((p = strrchr (found, PATH_SEP)) != NULL){
p++;
p = strdup (p);
free (found);
return p;
}
return found;
}
int match_compare (const void *a, const void *b)
{
return strcmp (*(char **)a, *(char **)b);
}
/* Returns an array of char * matches with the longest common denominator
in the 1st entry. Then a NULL terminated list of different possible
completions follows.
You have to supply your own CompletionFunction with the word you
want to complete as the first argument and an count of previous matches
as the second.
In case no matches were found we return NULL. */
char **completion_matches (char *text, CompletionFunction entry_function)
{
/* Number of slots in match_list. */
int match_list_size;
/* The list of matches. */
char **match_list = (char **) xmalloc (((match_list_size = 30) + 1) * sizeof (char *), "completion match list");
/* Number of matches actually found. */
int matches = 0;
/* Temporary string binder. */
char *string;
match_list[1] = NULL;
while ((string = (*entry_function) (text, matches)) != NULL){
if (matches + 1 == match_list_size)
match_list = (char **) realloc (match_list, ((match_list_size += 30) + 1) * sizeof (char *));
match_list[++matches] = string;
match_list[matches + 1] = NULL;
}
/* If there were any matches, then look through them finding out the
lowest common denominator. That then becomes match_list[0]. */
if (matches)
{
register int i = 1;
int low = 4096; /* Count of max-matched characters. */
/* If only one match, just use that. */
if (matches == 1){
match_list[0] = match_list[1];
match_list[1] = (char *)NULL;
} else {
int j;
qsort (match_list + 1, matches, sizeof (char *), match_compare);
/* And compare each member of the list with
the next, finding out where they stop matching.
If we find two equal strings, we have to put one away... */
j = i + 1;
while (j < matches + 1)
{
register int c1, c2, si;
for (si = 0;(c1 = match_list [i][si]) && (c2 = match_list [j][si]); si++)
if (c1 != c2) break;
if (!c1 && !match_list [j][si]){ /* Two equal strings */
free (match_list [j]);
j++;
if (j > matches)
break;
} else
if (low > si) low = si;
if (i + 1 != j) /* So there's some gap */
match_list [i + 1] = match_list [j];
i++; j++;
}
matches = i;
match_list [matches + 1] = NULL;
match_list[0] = xmalloc (low + 1, "Completion matching list");
strncpy (match_list[0], match_list[1], low);
match_list[0][low] = 0;
}
} else { /* There were no matches. */
free (match_list);
match_list = NULL;
}
return match_list;
}
int check_is_cd (char *text, int start, int flags)
{
char *p, *q = text + start;
for (p = text; *p && p < q && (*p == ' ' || *p == '\t'); p++);
if (((flags & INPUT_COMPLETE_COMMANDS) &&
!strncmp (p, "cd", 2) && (p [2] == ' ' || p [2] == '\t') &&
p + 2 < q) ||
(flags & INPUT_COMPLETE_CD))
return 1;
return 0;
}
/* Returns an array of matches, or NULL if none. */
char **try_complete (char *text, int *start, int *end, int flags)
{
int in_command_position = 0, i;
char *word, c;
char **matches = NULL;
char *command_separator_chars = ";|&{(`";
char *p = NULL, *q = NULL, *r = NULL;
int is_cd = check_is_cd (text, *start, flags);
ignore_filenames = 0;
c = text [*end];
text [*end] = 0;
word = strdup (text + *start);
text [*end] = c;
/* Determine if this could be a command word. It is if it appears at
the start of the line (ignoring preceding whitespace), or if it
appears after a character that separates commands. And we have to
be in a INPUT_COMPLETE_COMMANDS flagged Input line. */
if (!is_cd && (flags & INPUT_COMPLETE_COMMANDS)){
i = *start - 1;
while (i > -1 && (text[i] == ' ' || text[i] == '\t'))
i--;
if (i < 0)
in_command_position++;
else if (strchr (command_separator_chars, text[i])){
register int this_char, prev_char;
in_command_position++;
if (i){
/* Handle the two character tokens `>&', `<&', and `>|'.
We are not in a command position after one of these. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -