-
Notifications
You must be signed in to change notification settings - Fork 1
/
completion.c
118 lines (96 loc) · 3.67 KB
/
completion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
** completion.c
**
** Made by (Matthieu Lucotte)
** Login <gounter@users.sourceforge.net>
**
** Started on Sun Oct 7 01:43:23 2001 Matthieu Lucotte
** Last update Mon Sep 1 22:57:07 2003
*/
#include "zssh.h"
#include <readline/readline.h>
#include <readline/history.h>
#ifdef HAVE_LIBREADLINE
/* for readline < 4.2 we have to use username_completion_function
* 4.2 we have to use rl_username_completion_function
* and so on ...
*/
#ifdef HAVE_READLINE_4_2_OR_GREATER
#define RL_USERNAME_COMPLETION_FUNCTION rl_username_completion_function
#define RL_FILENAME_COMPLETION_FUNCTION rl_filename_completion_function
#define RL_COMPLETION_MATCHES rl_completion_matches
#else /* readline < 4.2 */
#define RL_USERNAME_COMPLETION_FUNCTION username_completion_function
#define RL_FILENAME_COMPLETION_FUNCTION filename_completion_function
#define RL_COMPLETION_MATCHES completion_matches
#endif
/* **************************************************************** */
/* */
/* Interface to Readline Completion */
/* */
/* **************************************************************** */
/* Tell the GNU Readline library how to complete. We want to try to complete
on command names if this is the first word in the line, or on filenames
if not. */
void initialize_readline(void)
{
#ifdef DEBUG
printf("Using readline library version: %s\n", rl_library_version);
#endif
/* Allow conditional parsing of the ~/.inputrc file. */
rl_readline_name = "zssh";
/* inhibit default filename completion
so that if zssh_completion() fails nothing is completed */
rl_completion_entry_function = fake_generator;
/* Tell the completer that we want a crack first. */
rl_attempted_completion_function = (rl_completion_func_t *)zssh_completion;
}
/* Attempt to complete on the contents of TEXT. START and END bound the
region of rl_line_buffer that contains the word to complete. TEXT is
the word to complete. We can use the entire contents of rl_line_buffer
in case we want to do some simple parsing. Return the array of matches,
or NULL if there aren't any. */
char **zssh_completion(char *text, int start, int end)
{
char **matches;
matches = (char **)NULL;
/* printf("text: >%s<\n", text); */
/* If this word is at the start of the line, then it is a command
to complete. Otherwise it is the name of a file in the current
directory. */
if (!start)
matches = RL_COMPLETION_MATCHES(text, command_generator);
else if (text[0] == '~' && !strchr(text, '/'))
matches = RL_COMPLETION_MATCHES(text, RL_USERNAME_COMPLETION_FUNCTION);
else
matches = RL_COMPLETION_MATCHES(text, RL_FILENAME_COMPLETION_FUNCTION);
return matches;
}
/* Generator function for command completion. STATE lets us know whether
to start from scratch; without any state (i.e. STATE == 0), then we
start at the top of the list. */
char *command_generator(const char *text, int state)
{
static int list_index, len;
char *name;
/* If this is a new word to complete, initialize now. This includes
saving the length of TEXT for efficiency, and initializing the index
variable to 0. */
if (!state) {
list_index = 0;
len = strlen(text);
}
/* Return the next name which partially matches from the command list. */
while ((name = cmdtab[list_index].name)) {
list_index++;
if (strncmp(name, text, len) == 0)
return strdup(name);
}
/* If no names matched, then return NULL. */
return (char *)NULL;
}
char *fake_generator(const char *text, int state)
{
return 0;
}
#endif /* HAVE_LIBREADLINE */